00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <globalsearch/ui/abstractedittab.h>
00018
00019 #include <globalsearch/macros.h>
00020 #include <globalsearch/optbase.h>
00021 #include <globalsearch/ui/abstractdialog.h>
00022
00023 #include <QtGui/QComboBox>
00024 #include <QtGui/QDialog>
00025 #include <QtGui/QFileDialog>
00026 #include <QtGui/QLineEdit>
00027 #include <QtGui/QListWidget>
00028 #include <QtGui/QPushButton>
00029 #include <QtGui/QTextEdit>
00030
00031 #include <QtCore/QDebug>
00032
00033 namespace GlobalSearch {
00034
00035 AbstractEditTab::AbstractEditTab( AbstractDialog *parent, OptBase *p ) :
00036 AbstractTab(parent, p),
00037 ui_combo_queueInterfaces(0),
00038 ui_combo_optimizers(0),
00039 ui_combo_templates(0),
00040 ui_edit_user1(0), ui_edit_user2(0), ui_edit_user3(0), ui_edit_user4(0),
00041 ui_list_edit(0),
00042 ui_list_optStep(0),
00043 ui_push_add(0),
00044 ui_push_help(0),
00045 ui_push_loadScheme(0),
00046 ui_push_optimizerConfig(0),
00047 ui_push_queueInterfaceConfig(0),
00048 ui_push_remove(0),
00049 ui_push_saveScheme(0),
00050 ui_edit_edit(0)
00051 {
00052 }
00053
00054 void AbstractEditTab::initialize()
00055 {
00056 ui_edit_edit->setCurrentFont(QFont("Courier"));
00057
00058
00059 connect(this, SIGNAL(optimizerChanged(Optimizer*)),
00060 m_opt, SLOT(setOptimizer(Optimizer*)),
00061 Qt::DirectConnection);
00062 connect(this, SIGNAL(queueInterfaceChanged(QueueInterface*)),
00063 m_opt, SLOT(setQueueInterface(QueueInterface*)),
00064 Qt::DirectConnection);
00065
00066
00067 connect(this, SIGNAL(optimizerChanged(Optimizer*)),
00068 m_dialog, SIGNAL(tabsUpdateGUI()));
00069 connect(this, SIGNAL(queueInterfaceChanged(QueueInterface*)),
00070 m_dialog, SIGNAL(tabsUpdateGUI()));
00071
00072
00073 connect(this, SIGNAL(optimizerChanged(Optimizer*)),
00074 this, SLOT(populateTemplates()));
00075 connect(this, SIGNAL(queueInterfaceChanged(QueueInterface*)),
00076 this, SLOT(populateTemplates()));
00077 connect(this, SIGNAL(optimizerChanged(Optimizer*)),
00078 this, SLOT(populateOptStepList()));
00079 connect(this, SIGNAL(queueInterfaceChanged(QueueInterface*)),
00080 this, SLOT(populateOptStepList()));
00081 connect(ui_push_optimizerConfig, SIGNAL(clicked()),
00082 this, SLOT(configureOptimizer()));
00083 connect(ui_push_queueInterfaceConfig, SIGNAL(clicked()),
00084 this, SLOT(configureQueueInterface()));
00085 connect(ui_push_help, SIGNAL(clicked()),
00086 this, SLOT(showHelp()));
00087 connect(ui_edit_edit, SIGNAL(textChanged()),
00088 this, SLOT(saveCurrentTemplate()));
00089 connect(ui_combo_templates, SIGNAL(currentIndexChanged(int)),
00090 this, SLOT(updateEditWidget()));
00091 connect(ui_push_add, SIGNAL(clicked()),
00092 this, SLOT(appendOptStep()));
00093 connect(ui_push_remove, SIGNAL(clicked()),
00094 this, SLOT(removeCurrentOptStep()));
00095 connect(ui_list_optStep, SIGNAL(currentRowChanged(int)),
00096 this, SLOT(updateEditWidget()));
00097 connect(ui_edit_user1, SIGNAL(editingFinished()),
00098 this, SLOT(updateUserValues()));
00099 connect(ui_edit_user2, SIGNAL(editingFinished()),
00100 this, SLOT(updateUserValues()));
00101 connect(ui_edit_user3, SIGNAL(editingFinished()),
00102 this, SLOT(updateUserValues()));
00103 connect(ui_edit_user4, SIGNAL(editingFinished()),
00104 this, SLOT(updateUserValues()));
00105 connect(ui_combo_optimizers, SIGNAL(currentIndexChanged(int)),
00106 this, SLOT(updateOptimizer()));
00107 connect(ui_combo_queueInterfaces, SIGNAL(currentIndexChanged(int)),
00108 this, SLOT(updateQueueInterface()));
00109 connect(ui_push_saveScheme, SIGNAL(clicked()),
00110 this, SLOT(saveScheme()));
00111 connect(ui_push_loadScheme, SIGNAL(clicked()),
00112 this, SLOT(loadScheme()));
00113
00114
00115 unsigned int index;
00116
00117
00118 ui_combo_queueInterfaces->clear();
00119 index = 0;
00120 for (QList<QueueInterface*>::const_iterator
00121 it = m_queueInterfaces.constBegin(),
00122 it_end = m_queueInterfaces.constEnd();
00123 it != it_end;
00124 ++it) {
00125 ui_combo_queueInterfaces->insertItem(index++,
00126 (*it)->getIDString());
00127 }
00128
00129
00130 ui_combo_optimizers->clear();
00131 index = 0;
00132 for (QList<Optimizer*>::const_iterator
00133 it = m_optimizers.constBegin(),
00134 it_end = m_optimizers.constEnd();
00135 it != it_end;
00136 ++it) {
00137 ui_combo_optimizers->insertItem(index++,
00138 (*it)->getIDString());
00139 }
00140
00141 ui_combo_queueInterfaces->setCurrentIndex(0);
00142 ui_combo_optimizers->setCurrentIndex(0);
00143
00144 AbstractTab::initialize();
00145 updateGUI();
00146 }
00147
00148 AbstractEditTab::~AbstractEditTab()
00149 {
00150 }
00151
00152 void AbstractEditTab::updateGUI()
00153 {
00154 if (!m_isInitialized) {
00155 return;
00156 }
00157 Q_ASSERT_X(m_queueInterfaces.contains(m_opt->queueInterface()) ||
00158 m_opt->queueInterface() == 0, Q_FUNC_INFO,
00159 "Current queue interface is unknown to AbstractEditTab.");
00160 Q_ASSERT_X(m_optimizers.contains(m_opt->optimizer()) ||
00161 m_opt->optimizer() == 0, Q_FUNC_INFO,
00162 "Current optimizer is unknown to AbstractEditTab.");
00163 Q_ASSERT(m_optimizers.size() == ui_combo_optimizers->count());
00164 Q_ASSERT(m_queueInterfaces.size() == ui_combo_queueInterfaces->count());
00165
00166 if (m_opt->optimizer()) {
00167 int optIndex = m_optimizers.indexOf(m_opt->optimizer());
00168 ui_combo_optimizers->setCurrentIndex(optIndex);
00169 }
00170
00171 if (m_opt->queueInterface()) {
00172 int qiIndex = m_queueInterfaces.indexOf(m_opt->queueInterface());
00173 ui_combo_queueInterfaces->setCurrentIndex(qiIndex);
00174 if (m_opt->queueInterface()->hasDialog()) {
00175 ui_push_queueInterfaceConfig->setEnabled(true);
00176 } else {
00177 ui_push_queueInterfaceConfig->setEnabled(false);
00178 }
00179 }
00180 else {
00181 ui_push_queueInterfaceConfig->setEnabled(false);
00182 }
00183
00184 populateTemplates();
00185 populateOptStepList();
00186
00187 updateEditWidget();
00188
00189 ui_edit_user1->setText( m_opt->optimizer()->getUser1());
00190 ui_edit_user2->setText( m_opt->optimizer()->getUser2());
00191 ui_edit_user3->setText( m_opt->optimizer()->getUser3());
00192 ui_edit_user4->setText( m_opt->optimizer()->getUser4());
00193 }
00194
00195 void AbstractEditTab::lockGUI()
00196 {
00197 ui_combo_optimizers->setDisabled(true);
00198 ui_combo_queueInterfaces->setDisabled(true);
00199 }
00200
00201 void AbstractEditTab::showHelp()
00202 {
00203 QMessageBox::information(m_dialog,
00204 "Template Help",
00205 m_opt->getTemplateKeywordHelp());
00206 }
00207
00208 void AbstractEditTab::updateQueueInterface()
00209 {
00210 Q_ASSERT_X(m_queueInterfaces.contains(m_opt->queueInterface()) ||
00211 m_opt->queueInterface() == 0, Q_FUNC_INFO,
00212 "Current queue interface is unknown to AbstractEditTab.");
00213
00214 unsigned int newQiIndex = ui_combo_queueInterfaces->currentIndex();
00215
00216 Q_ASSERT(newQiIndex <= m_queueInterfaces.size() - 1);
00217
00218
00219 if (m_queueInterfaces.indexOf(m_opt->queueInterface()) ==
00220 newQiIndex &&
00221 m_opt->queueInterface() != 0) {
00222 return;
00223 }
00224
00225 QueueInterface *qi = m_queueInterfaces[newQiIndex];
00226
00227 if (qi->hasDialog()) {
00228 ui_push_queueInterfaceConfig->setEnabled(true);
00229 } else {
00230 ui_push_queueInterfaceConfig->setEnabled(false);
00231 }
00232
00233 emit queueInterfaceChanged(qi);
00234 }
00235
00236 void AbstractEditTab::updateOptimizer()
00237 {
00238 Q_ASSERT_X(m_optimizers.contains(m_opt->optimizer()) ||
00239 m_opt->optimizer() == 0, Q_FUNC_INFO,
00240 "Current optimizer is unknown to AbstractEditTab.");
00241
00242 unsigned int newOptimizerIndex = ui_combo_optimizers->currentIndex();
00243
00244 Q_ASSERT(newOptimizerIndex <= m_optimizers.size() - 1);
00245
00246
00247 if (m_optimizers.indexOf(m_opt->optimizer()) ==
00248 newOptimizerIndex &&
00249 m_opt->optimizer() != 0) {
00250 return;
00251 }
00252
00253 Optimizer *o = m_optimizers[newOptimizerIndex];
00254
00255 if (o->hasDialog()) {
00256 ui_push_optimizerConfig->setEnabled(true);
00257 } else {
00258 ui_push_optimizerConfig->setEnabled(false);
00259 }
00260
00261 emit optimizerChanged(o);
00262 }
00263
00264 void AbstractEditTab::configureQueueInterface()
00265 {
00266 Q_ASSERT(m_opt->queueInterface());
00267 Q_ASSERT(m_opt->queueInterface()->hasDialog());
00268
00269 QDialog *d = m_opt->queueInterface()->dialog();
00270 Q_ASSERT(d != 0);
00271
00272 d->show();
00273 d->exec();
00274 }
00275
00276 void AbstractEditTab::configureOptimizer()
00277 {
00278 Q_ASSERT(m_opt->optimizer());
00279 Q_ASSERT(m_opt->optimizer()->hasDialog());
00280
00281 QDialog *d = m_opt->optimizer()->dialog();
00282 Q_ASSERT(d != 0);
00283
00284 d->show();
00285 d->exec();
00286 }
00287
00288 QStringList AbstractEditTab::getTemplateNames()
00289 {
00290 if (!m_isInitialized) {
00291 return QStringList();
00292 }
00293 QStringList templateNames = m_opt->optimizer()->getTemplateNames();
00294 templateNames.append(m_opt->queueInterface()->getTemplateFileNames());
00295 qSort(templateNames);
00296 return templateNames;
00297 }
00298
00299 void AbstractEditTab::populateTemplates()
00300 {
00301 if (!m_isInitialized) {
00302 return;
00303 }
00304 ui_combo_templates->blockSignals(true);
00305 ui_combo_templates->clear();
00306 ui_combo_templates->insertItems(0, getTemplateNames());
00307 ui_combo_templates->blockSignals(false);
00308 ui_combo_templates->setCurrentIndex(0);
00309 }
00310
00311 void AbstractEditTab::updateEditWidget()
00312 {
00313 if (!m_isInitialized) {
00314 return;
00315 }
00316 QStringList filenames = getTemplateNames();
00317 int templateInd = ui_combo_templates->currentIndex();
00318 QString templateName = ui_combo_templates->currentText();
00319 Q_ASSERT(templateInd >= 0 && templateInd < filenames.size());
00320 Q_ASSERT(templateName.compare(filenames.at(templateInd)) == 0);
00321
00322 if (m_opt->optimizer()->getNumberOfOptSteps() !=
00323 ui_list_optStep->count()) {
00324 populateOptStepList();
00325 }
00326
00327 int optStepIndex = ui_list_optStep->currentRow();
00328 Q_ASSERT(optStepIndex >= 0 &&
00329 optStepIndex < m_opt->optimizer()->getNumberOfOptSteps());
00330
00331
00332
00333
00334
00335
00336 ui_list_edit->setVisible(false);
00337 ui_edit_edit->setVisible(true);
00338
00339
00340 Q_ASSERT(getTemplateNames().contains(templateName));
00341 QString text = m_opt->optimizer()->getTemplate(templateName, optStepIndex);
00342
00343 ui_edit_edit->blockSignals(true);
00344 ui_edit_edit->setText(text);
00345 ui_edit_edit->setCurrentFont(QFont("Courier"));
00346 ui_edit_edit->blockSignals(false);
00347 }
00348
00349 void AbstractEditTab::saveCurrentTemplate()
00350 {
00351 QStringList filenames = getTemplateNames();
00352 if (filenames.isEmpty()) {
00353 ui_list_edit->setVisible(false);
00354 ui_edit_edit->setVisible(false);
00355
00356 if (m_opt->optimizer()->getNumberOfOptSteps() !=
00357 ui_list_optStep->count()) {
00358 this->populateOptStepList();
00359 }
00360 return;
00361 }
00362 int templateInd = ui_combo_templates->currentIndex();
00363 QString templateName = ui_combo_templates->currentText();
00364 Q_ASSERT(templateInd >= 0 && templateInd < filenames.size());
00365 Q_ASSERT(templateName.compare(filenames.at(templateInd)) == 0);
00366
00367 if (m_opt->optimizer()->getNumberOfOptSteps() !=
00368 ui_list_optStep->count()) {
00369 populateOptStepList();
00370 }
00371
00372 int optStepIndex = ui_list_optStep->currentRow();
00373 Q_ASSERT(optStepIndex >= 0 &&
00374 optStepIndex < m_opt->optimizer()->getNumberOfOptSteps());
00375
00376
00377
00378
00379 QString text = ui_edit_edit->document()->toPlainText();
00380
00381 m_opt->optimizer()->setTemplate(templateName, text, optStepIndex);
00382
00383 ui_edit_edit->setCurrentFont(QFont("Courier"));
00384 }
00385
00386 void AbstractEditTab::updateUserValues()
00387 {
00388 m_opt->optimizer()->setUser1(ui_edit_user1->text());
00389 m_opt->optimizer()->setUser2(ui_edit_user2->text());
00390 m_opt->optimizer()->setUser3(ui_edit_user3->text());
00391 m_opt->optimizer()->setUser4(ui_edit_user4->text());
00392 }
00393
00394 void AbstractEditTab::populateOptStepList()
00395 {
00396 if (!m_isInitialized) {
00397 return;
00398 }
00399 ui_list_optStep->blockSignals(true);
00400 ui_list_optStep->clear();
00401
00402
00403 if (!m_opt->optimizer() || !m_opt->queueInterface()) {
00404 ui_list_optStep->blockSignals(false);
00405 return;
00406 }
00407
00408 int currentOptStep = ui_list_optStep->currentRow();
00409 const int maxSteps = m_opt->optimizer()->getNumberOfOptSteps();
00410 if (currentOptStep < 0) currentOptStep = 0;
00411 if (currentOptStep >= maxSteps) currentOptStep = maxSteps - 1;
00412
00413 for (int i = 1; i <= maxSteps; ++i) {
00414 ui_list_optStep->addItem(tr("Optimization %1").arg(i));
00415 }
00416 ui_list_optStep->blockSignals(false);
00417
00418 ui_list_optStep->setCurrentRow(currentOptStep);
00419 }
00420
00421 void AbstractEditTab::appendOptStep()
00422 {
00423
00424 const int maxSteps = m_opt->optimizer()->getNumberOfOptSteps();
00425 const int currentOptStep = ui_list_optStep->currentRow();
00426 Q_ASSERT(currentOptStep >= 0 && currentOptStep < maxSteps);
00427
00428
00429
00430 QStringList templates = getTemplateNames();
00431
00432 for (QStringList::const_iterator
00433 it = templates.constBegin(),
00434 it_end = templates.constEnd();
00435 it != it_end;
00436 ++it) {
00437 m_opt->optimizer()->
00438 appendTemplate(*it, m_opt->optimizer()->
00439 getTemplate(*it, currentOptStep));
00440 }
00441
00442 populateOptStepList();
00443 }
00444
00445 void AbstractEditTab::removeCurrentOptStep()
00446 {
00447
00448 const int maxSteps = m_opt->optimizer()->getNumberOfOptSteps();
00449 const int currentOptStep = ui_list_optStep->currentRow();
00450 Q_ASSERT(currentOptStep >= 0 && currentOptStep < maxSteps);
00451
00452 m_opt->optimizer()->removeAllTemplatesForOptStep(currentOptStep);
00453
00454 populateOptStepList();
00455 }
00456
00457 void AbstractEditTab::saveScheme()
00458 {
00459 SETTINGS("");
00460 QString filename = settings->value(m_opt->getIDString().toLower() +
00461 "/edit/schemePath/", "").toString();
00462 QFileDialog dialog (NULL, tr("Save Optimization Scheme as..."),
00463 filename, "*.scheme;;*.state;;*.*");
00464 dialog.setAcceptMode(QFileDialog::AcceptSave);
00465 dialog.selectFile(m_opt->optimizer()->getIDString() + ".scheme");
00466 dialog.setFileMode(QFileDialog::AnyFile);
00467 if (dialog.exec())
00468 filename = dialog.selectedFiles().first();
00469 else {
00470 return;
00471 }
00472 settings->setValue(m_opt->getIDString().toLower() +
00473 "/edit/schemePath/", filename);
00474 DESTROY_SETTINGS("");
00475 writeSettings(filename);
00476 }
00477
00478 void AbstractEditTab::loadScheme()
00479 {
00480 SETTINGS("");
00481 QString filename = settings->value(m_opt->getIDString().toLower() +
00482 "/edit/schemePath/", "").toString();
00483 QFileDialog dialog (NULL, tr("Select Optimization Scheme to load..."),
00484 filename, "*.scheme;;*.state;;*.*");
00485 dialog.setAcceptMode(QFileDialog::AcceptOpen);
00486 dialog.setFileMode(QFileDialog::ExistingFile);
00487 if (dialog.exec())
00488 filename = dialog.selectedFiles().first();
00489 else {
00490 return;
00491 }
00492 settings->setValue(m_opt->getIDString().toLower() +
00493 "/edit/schemePath/", filename);
00494 DESTROY_SETTINGS("");
00495 readSettings(filename);
00496 }
00497
00498 }