00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <globalsearch/ui/abstractdialog.h>
00019
00020 #include <globalsearch/tracker.h>
00021 #include <globalsearch/queuemanager.h>
00022
00023 #include <openbabel/oberror.h>
00024
00025 #include <QtGui/QApplication>
00026 #include <QtGui/QLabel>
00027 #include <QtGui/QFileDialog>
00028 #include <QtGui/QPushButton>
00029 #include <QtGui/QMessageBox>
00030
00031 #include <QtCore/QSettings>
00032 #include <QtCore/QtConcurrentRun>
00033
00034 using namespace std;
00035 using namespace Avogadro;
00036
00037 namespace GlobalSearch {
00038
00039 AbstractDialog::AbstractDialog( GLWidget *glWidget,
00040 QWidget *parent,
00041 Qt::WindowFlags f ) :
00042 QDialog( parent, f ),
00043 m_opt(0),
00044 m_glWidget(glWidget)
00045 {
00046
00047
00048 OpenBabel::obErrorLog.StopLogging();
00049
00050
00051 progMutex = new QMutex;
00052 progTimer = new QTimer;
00053 }
00054
00055 void AbstractDialog::initialize()
00056 {
00057 connect(ui_push_begin, SIGNAL(clicked()),
00058 this, SLOT(startSearch()));
00059 connect(ui_push_save, SIGNAL(clicked()),
00060 this, SLOT(saveSession()));
00061 connect(ui_push_resume, SIGNAL(clicked()),
00062 this, SLOT(resumeSession()));
00063
00064 connect(m_opt->tracker(), SIGNAL(newStructureAdded(GlobalSearch::Structure*)),
00065 this, SLOT(saveSession()));
00066 connect(m_opt->queue(), SIGNAL(structureUpdated(GlobalSearch::Structure*)),
00067 this, SLOT(saveSession()));
00068 connect(m_opt, SIGNAL(sessionStarted()),
00069 this, SLOT(updateGUI()));
00070 connect(m_opt, SIGNAL(sessionStarted()),
00071 this, SLOT(lockGUI()));
00072 connect(m_opt, SIGNAL(readOnlySessionStarted()),
00073 this, SLOT(updateGUI()));
00074 connect(m_opt, SIGNAL(readOnlySessionStarted()),
00075 this, SLOT(lockGUI()));
00076 connect(m_opt->queue(), SIGNAL(newStatusOverview(int,int,int)),
00077 this, SLOT(updateStatus(int,int,int)));
00078 connect(this, SIGNAL(sig_updateStatus(int,int,int)),
00079 this, SLOT(updateStatus_(int,int,int)));
00080
00081 connect(progTimer, SIGNAL(timeout()),
00082 this, SLOT(repaintProgressBar_()),
00083 Qt::QueuedConnection);
00084 connect(this, SIGNAL(sig_startProgressUpdate(const QString &, int, int)),
00085 this, SLOT(startProgressUpdate_(const QString &, int, int)),
00086 Qt::QueuedConnection);
00087 connect(this, SIGNAL(sig_stopProgressUpdate()),
00088 this, SLOT(stopProgressUpdate_()),
00089 Qt::QueuedConnection);
00090 connect(this, SIGNAL(sig_updateProgressMinimum(int)),
00091 this, SLOT(updateProgressMinimum_(int)),
00092 Qt::QueuedConnection);
00093 connect(this, SIGNAL(sig_updateProgressMaximum(int)),
00094 this, SLOT(updateProgressMaximum_(int)),
00095 Qt::QueuedConnection);
00096 connect(this, SIGNAL(sig_updateProgressValue(int)),
00097 this, SLOT(updateProgressValue_(int)),
00098 Qt::QueuedConnection);
00099 connect(this, SIGNAL(sig_updateProgressLabel(const QString &)),
00100 this, SLOT(updateProgressLabel_(const QString &)),
00101 Qt::QueuedConnection);
00102 connect(this, SIGNAL(sig_repaintProgressBar()),
00103 this, SLOT(repaintProgressBar_()),
00104 Qt::QueuedConnection);
00105
00106 connect(m_opt, SIGNAL(warningStatement(const QString &)),
00107 this, SLOT(newWarning(const QString &)),
00108 Qt::QueuedConnection);
00109 connect(m_opt, SIGNAL(debugStatement(const QString &)),
00110 this, SLOT(newDebug(const QString &)),
00111 Qt::QueuedConnection);
00112 connect(m_opt, SIGNAL(errorStatement(const QString &)),
00113 this, SLOT(newError(const QString &)),
00114 Qt::QueuedConnection);
00115 connect(this, SIGNAL(sig_errorBox(const QString &)),
00116 this, SLOT(errorBox_(const QString &)),
00117 Qt::QueuedConnection);
00118
00119
00120 ui_tabs->setCurrentIndex(0);
00121
00122
00123 ui_label_prog->setVisible(false);
00124 ui_progbar->setVisible(false);
00125 readSettings();
00126
00127
00128 ui_push_save->setEnabled(false);
00129
00130 }
00131
00132 AbstractDialog::~AbstractDialog()
00133 {
00134 delete m_opt;
00135 }
00136
00137 void AbstractDialog::disconnectGUI() {
00138 emit tabsDisconnectGUI();
00139 disconnect(m_opt, SIGNAL(sessionStarted()),
00140 this, SLOT(updateGUI()));
00141 disconnect(m_opt, SIGNAL(readOnlySessionStarted()),
00142 this, SLOT(updateGUI()));
00143 disconnect(this, SIGNAL(sig_updateStatus(int,int,int)),
00144 this, SLOT(updateStatus_(int,int,int)));
00145 }
00146
00147 void AbstractDialog::lockGUI() {
00148 ui_push_resume->setDisabled(true);
00149 ui_push_begin->setDisabled(true);
00150
00151 ui_push_save->setEnabled(true);
00152 emit tabsLockGUI();
00153 }
00154
00155 void AbstractDialog::updateGUI() {
00156 setWindowTitle(QString("%1 - %2 @ %3%4")
00157 .arg(m_opt->getIDString())
00158 .arg(m_opt->description)
00159 .arg(m_opt->host)
00160 .arg( m_opt->readOnly ?
00161 " (Read-Only mode)" :
00162 "" )
00163 );
00164 emit tabsUpdateGUI();
00165 }
00166
00167 bool AbstractDialog::startProgressUpdate(const QString &text, int min, int max)
00168 {
00169 if (!this->progMutex->tryLock())
00170 return false;
00171 emit sig_startProgressUpdate(text,min,max);
00172 return true;
00173 }
00174
00175 void AbstractDialog::stopProgressUpdate()
00176 {
00177 emit sig_stopProgressUpdate();
00178 this->progMutex->unlock();
00179 }
00180
00181 void AbstractDialog::resumeSession()
00182 {
00183 QString filename;
00184 QFileDialog dialog (NULL,
00185 QString("Select .state file to resume"),
00186 m_opt->filePath,
00187 "*.state;;*.*");
00188 dialog.selectFile(tr("%1.state")
00189 .arg(m_opt->getIDString().toLower()));
00190 dialog.setFileMode(QFileDialog::ExistingFile);
00191 if (dialog.exec())
00192 filename = dialog.selectedFiles().first();
00193 else {
00194 return;
00195 }
00196
00197 QtConcurrent::run(this, &AbstractDialog::resumeSession_, filename);
00198 }
00199
00200 void AbstractDialog::resumeSession_(const QString &filename)
00201 {
00202 m_opt->emitStartingSession();
00203 bool notify = startProgressUpdate(tr("Resuming session..."), 0, 0);
00204 m_opt->tracker()->lockForWrite();
00205 m_opt->tracker()->deleteAllStructures();
00206 m_opt->tracker()->unlock();
00207 if (!m_opt->load(filename)) {
00208 if (notify)
00209 stopProgressUpdate();
00210 m_opt->isStarting = false;
00211 return;
00212 }
00213
00214 writeSettings();
00215 if (notify)
00216 stopProgressUpdate();
00217
00218
00219 if (m_opt->readOnly)
00220 m_opt->emitReadOnlySessionStarted();
00221 else
00222 m_opt->emitSessionStarted();
00223 }
00224
00225 void AbstractDialog::updateStatus_(int opt, int run, int fail) {
00226 ui_label_opt->setText(QString::number(opt));
00227 ui_label_run->setText(QString::number(run));
00228 ui_label_fail->setText(QString::number(fail));
00229 }
00230
00231 void AbstractDialog::startProgressUpdate_(const QString & text, int min, int max) {
00232 ui_progbar->reset();
00233 ui_progbar->setRange(min, max);
00234 ui_progbar->setValue(min);
00235 ui_label_prog->setText(text);
00236 ui_progbar->setVisible(true);
00237 ui_label_prog->setVisible(true);
00238 repaintProgressBar();
00239 progTimer->start(1000);
00240 }
00241
00242 void AbstractDialog::stopProgressUpdate_() {
00243 ui_progbar->reset();
00244 ui_label_prog->setText("");
00245 ui_progbar->setVisible(false);
00246 ui_label_prog->setVisible(false);
00247 progTimer->stop();
00248 repaintProgressBar();
00249 }
00250
00251 void AbstractDialog::updateProgressMinimum_(int min) {
00252 ui_progbar->setMinimum(min);
00253 repaintProgressBar();
00254 }
00255
00256 void AbstractDialog::updateProgressMaximum_(int max) {
00257 ui_progbar->setMaximum(max);
00258 repaintProgressBar();
00259 }
00260
00261 void AbstractDialog::updateProgressValue_(int val) {
00262 ui_progbar->setValue(val);
00263 repaintProgressBar();
00264 }
00265
00266 void AbstractDialog::updateProgressLabel_(const QString & text) {
00267 ui_label_prog->setText(text);
00268 repaintProgressBar();
00269 }
00270
00271 void AbstractDialog::repaintProgressBar_() {
00272 ui_label_prog->repaint();
00273 ui_progbar->repaint();
00274 }
00275
00277 void AbstractDialog::reemitTabsWriteSettings(const QString &filename)
00278 {
00279 if (QThread::currentThread() == qApp->thread()) {
00280
00281 emit tabsWriteSettingsDirect(filename);
00282 } else {
00283
00284 emit tabsWriteSettingsBlockingQueued(filename);
00285 }
00286 }
00287
00288 void AbstractDialog::reemitTabsReadSettings(const QString &filename)
00289 {
00290 if (QThread::currentThread() == qApp->thread()) {
00291
00292 emit tabsReadSettingsDirect(filename);
00293 } else {
00294
00295 emit tabsReadSettingsBlockingQueued(filename);
00296 }
00297 }
00299 }