| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "inputgeneratorwidget.h" |
| 7 |
#include "batchjob.h" |
| 8 |
#include "molequeuedialog.h" |
| 9 |
#include "molequeuemanager.h" |
| 10 |
#include "ui_inputgeneratorwidget.h" |
| 11 |
|
| 12 |
#include <avogadro/qtgui/filebrowsewidget.h> |
| 13 |
#include <avogadro/qtgui/generichighlighter.h> |
| 14 |
#include <avogadro/qtgui/molecule.h> |
| 15 |
|
| 16 |
#include <QtWidgets/QComboBox> |
| 17 |
#include <QtWidgets/QDoubleSpinBox> |
| 18 |
#include <QtWidgets/QFileDialog> |
| 19 |
#include <QtWidgets/QFormLayout> |
| 20 |
#include <QtWidgets/QLabel> |
| 21 |
#include <QtWidgets/QLineEdit> |
| 22 |
#include <QtWidgets/QMessageBox> |
| 23 |
#include <QtWidgets/QSpinBox> |
| 24 |
#include <QtWidgets/QTabWidget> |
| 25 |
#include <QtWidgets/QTextEdit> |
| 26 |
|
| 27 |
#include <QtCore/QDebug> |
| 28 |
#include <QtCore/QJsonDocument> |
| 29 |
#include <QtCore/QPointer> |
| 30 |
#include <QtCore/QSettings> |
| 31 |
#include <QtCore/QTimer> |
| 32 |
|
| 33 |
namespace Avogadro::MoleQueue { |
| 34 |
|
| 35 |
InputGeneratorWidget::InputGeneratorWidget(QWidget* parent_) |
| 36 |
: QtGui::JsonWidget(parent_), m_ui(new Ui::InputGeneratorWidget), |
| 37 |
m_updatePending(false), m_inputGenerator(QString()) |
| 38 |
{ |
| 39 |
m_ui->setupUi(this); |
| 40 |
m_ui->warningTextButton->setIcon(QIcon::fromTheme("dialog-warning")); |
| 41 |
|
| 42 |
connectButtons(); |
| 43 |
} |
| 44 |
|
| 45 |
InputGeneratorWidget::~InputGeneratorWidget() |
| 46 |
{ |
| 47 |
delete m_ui; |
| 48 |
} |
| 49 |
|
| 50 |
void InputGeneratorWidget::setInputGeneratorScript(const QString& scriptFile) |
| 51 |
{ |
| 52 |
m_inputGenerator.setScriptFilePath(scriptFile); |
| 53 |
m_ui->debugCheckBox->setChecked(m_inputGenerator.debug()); |
| 54 |
updateOptions(); |
| 55 |
resetWarningDisplay(); |
| 56 |
} |
| 57 |
|
| 58 |
void InputGeneratorWidget::reloadOptions() |
| 59 |
{ |
| 60 |
m_ui->debugCheckBox->setChecked(m_inputGenerator.debug()); |
| 61 |
updateOptions(); |
| 62 |
resetWarningDisplay(); |
| 63 |
} |
| 64 |
|
| 65 |
void InputGeneratorWidget::setMolecule(QtGui::Molecule* mol) |
| 66 |
{ |
| 67 |
if (mol == m_molecule) |
| 68 |
return; |
| 69 |
|
| 70 |
if (m_molecule) |
| 71 |
m_molecule->disconnect(this); |
| 72 |
|
| 73 |
m_molecule = mol; |
| 74 |
if (mol) { |
| 75 |
// make sure to call the base class method |
| 76 |
QtGui::JsonWidget::setMolecule(mol); |
| 77 |
|
| 78 |
connect(mol, SIGNAL(changed(unsigned int)), SLOT(updatePreviewText())); |
| 79 |
connect(mol, SIGNAL(changed(unsigned int)), SLOT(updateTitlePlaceholder())); |
| 80 |
} |
| 81 |
|
| 82 |
updateTitlePlaceholder(); |
| 83 |
updatePreviewTextImmediately(); |
| 84 |
} |
| 85 |
|
| 86 |
bool InputGeneratorWidget::configureBatchJob(BatchJob& batch) const |
| 87 |
{ |
| 88 |
if (!m_batchMode) |
| 89 |
return false; |
| 90 |
|
| 91 |
QJsonObject mqOpts = promptForBatchJobOptions(); |
| 92 |
if (mqOpts.empty()) |
| 93 |
return false; |
| 94 |
|
| 95 |
JobObject job; |
| 96 |
job.fromJson(mqOpts); |
| 97 |
|
| 98 |
QJsonObject calcOpts; |
| 99 |
calcOpts[QLatin1String("options")] = collectOptions(); |
| 100 |
|
| 101 |
// Set job description from title: |
| 102 |
QString description; |
| 103 |
if (!optionString("Title", description) || description.isEmpty()) |
| 104 |
description = generateJobTitle(); |
| 105 |
job.setDescription(description); |
| 106 |
|
| 107 |
mqOpts = job.json(); |
| 108 |
|
| 109 |
batch.setInputGeneratorOptions(calcOpts); |
| 110 |
batch.setMoleQueueOptions(mqOpts); |
| 111 |
|
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
void InputGeneratorWidget::setBatchMode(bool m) |
| 116 |
{ |
| 117 |
if (m_batchMode != m) { |
| 118 |
m_batchMode = m; |
| 119 |
foreach (QTextEdit* edit, m_textEdits) |
| 120 |
edit->setReadOnly(m_batchMode); |
| 121 |
m_ui->computeButton->setVisible(!m_batchMode); |
| 122 |
m_ui->generateButton->setVisible(!m_batchMode); |
| 123 |
m_ui->closeButton->setText(m_batchMode ? tr("Continue") : tr("Close")); |
| 124 |
updateTitlePlaceholder(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
void InputGeneratorWidget::showEvent(QShowEvent* e) |
| 129 |
{ |
| 130 |
QWidget::showEvent(e); |
| 131 |
|
| 132 |
if (m_molecule != nullptr) { |
| 133 |
int charge = static_cast<int>(m_molecule->totalCharge()); |
| 134 |
int multiplicity = static_cast<int>(m_molecule->totalSpinMultiplicity()); |
| 135 |
setOption("Charge", charge); |
| 136 |
setOption("Multiplicity", multiplicity); |
| 137 |
} |
| 138 |
|
| 139 |
// Update the preview text if an update was requested while hidden. Use a |
| 140 |
// single shot to allow the dialog to show before popping up any warnings. |
| 141 |
if (m_updatePending) |
| 142 |
QTimer::singleShot(0, this, SLOT(updatePreviewTextImmediately())); |
| 143 |
} |
| 144 |
|
| 145 |
void InputGeneratorWidget::updatePreviewText() |
| 146 |
{ |
| 147 |
if (m_updatePending) |
| 148 |
return; |
| 149 |
|
| 150 |
m_updatePending = true; |
| 151 |
QTimer::singleShot(250, this, SLOT(updatePreviewTextImmediately())); |
| 152 |
} |
| 153 |
|
| 154 |
void InputGeneratorWidget::updatePreviewTextImmediately() |
| 155 |
{ |
| 156 |
// If the dialog is not shown, delay the update in case we need to prompt the |
| 157 |
// user to overwrite changes. Set the m_updatePending flag to true so we'll |
| 158 |
// know to update in the show event. |
| 159 |
if (!isVisible()) { |
| 160 |
m_updatePending = true; |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
// Reset the update throttling |
| 165 |
m_updatePending = false; |
| 166 |
|
| 167 |
// Have any buffers been modified? |
| 168 |
if (!m_dirtyTextEdits.isEmpty()) { |
| 169 |
QStringList buffers; |
| 170 |
foreach (QTextEdit* edit, m_dirtyTextEdits) |
| 171 |
buffers << m_textEdits.key(edit, tr("Unknown")); |
| 172 |
QString message = tr("The following file(s) have been modified:\n\n%1\n\n" |
| 173 |
"Would you like to overwrite your changes to reflect " |
| 174 |
"the new geometry or job options?", |
| 175 |
"", buffers.size()) |
| 176 |
.arg(buffers.join("\n")); |
| 177 |
int response = QMessageBox::question( |
| 178 |
this, tr("Overwrite modified input files?"), message, |
| 179 |
QMessageBox::Yes | QMessageBox::No, QMessageBox::No); |
| 180 |
if (static_cast<QMessageBox::StandardButton>(response) != |
| 181 |
QMessageBox::Yes) { |
| 182 |
// Prevent updates while restoring the option cache: |
| 183 |
bool oldUpdatePending = m_updatePending; |
| 184 |
m_updatePending = true; |
| 185 |
// Restore cached options. |
| 186 |
applyOptions(m_optionCache); |
| 187 |
m_updatePending = oldUpdatePending; |
| 188 |
return; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if (!m_molecule) |
| 193 |
return; |
| 194 |
|
| 195 |
// Generate the input files |
| 196 |
QJsonObject inputOptions; |
| 197 |
inputOptions["options"] = collectOptions(); |
| 198 |
bool success = m_inputGenerator.generateInput(inputOptions, *m_molecule); |
| 199 |
|
| 200 |
if (!m_inputGenerator.warningList().isEmpty()) { |
| 201 |
QString warningHtml; |
| 202 |
warningHtml += "<style>li{color:red;}h3{font-weight:bold;}</style>"; |
| 203 |
warningHtml += |
| 204 |
"<h3>" + tr("Problems occurred during input generation:") + "</h3>"; |
| 205 |
warningHtml += "<ul>"; |
| 206 |
foreach (const QString& warning, m_inputGenerator.warningList()) |
| 207 |
warningHtml += QString("<li>%1</li>").arg(warning); |
| 208 |
warningHtml += "</ul>"; |
| 209 |
|
| 210 |
setWarning(warningHtml); |
| 211 |
} else { |
| 212 |
resetWarningDisplay(); |
| 213 |
} |
| 214 |
|
| 215 |
if (!success) { |
| 216 |
showError(m_inputGenerator.errorList().join("\n\n")); |
| 217 |
m_inputGenerator.clearErrors(); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
// Store the currently displayed tab |
| 222 |
QPointer<QWidget> currentWidget(m_ui->tabWidget->currentWidget()); |
| 223 |
|
| 224 |
// Ensure that the correct tabs are shown: |
| 225 |
QStringList fileNames = m_inputGenerator.fileNames(); |
| 226 |
// Remove unneeded tabs |
| 227 |
foreach (const QString& tabName, m_textEdits.keys()) { |
| 228 |
if (!fileNames.contains(tabName)) { |
| 229 |
QTextEdit* edit = m_textEdits.value(tabName); |
| 230 |
int index = m_ui->tabWidget->indexOf(edit); |
| 231 |
m_ui->tabWidget->removeTab(index); |
| 232 |
m_textEdits.remove(tabName); |
| 233 |
delete edit; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// Add new tabs |
| 238 |
foreach (const QString& fileName, fileNames) { |
| 239 |
if (m_textEdits.contains(fileName)) |
| 240 |
continue; |
| 241 |
auto* edit = new QTextEdit(this); |
| 242 |
edit->setObjectName(fileName); |
| 243 |
edit->setFontFamily("monospace"); |
| 244 |
connect(edit, SIGNAL(textChanged()), this, SLOT(textEditModified())); |
| 245 |
m_ui->tabWidget->addTab(edit, fileName); |
| 246 |
m_textEdits.insert(fileName, edit); |
| 247 |
} |
| 248 |
|
| 249 |
// Sort and update |
| 250 |
int index = 0; |
| 251 |
foreach (const QString& fileName, fileNames) { |
| 252 |
QTextEdit* edit = m_textEdits.value(fileName); |
| 253 |
int tabIndex = m_ui->tabWidget->indexOf(edit); |
| 254 |
if (tabIndex != index) { |
| 255 |
m_ui->tabWidget->removeTab(tabIndex); |
| 256 |
m_ui->tabWidget->insertTab(index, edit, fileName); |
| 257 |
} |
| 258 |
|
| 259 |
QtGui::GenericHighlighter* highlighter = |
| 260 |
m_inputGenerator.createFileHighlighter(fileName); |
| 261 |
if (highlighter) { |
| 262 |
highlighter->setParent(this); |
| 263 |
highlighter->setDocument(edit->document()); |
| 264 |
} |
| 265 |
|
| 266 |
edit->setText(m_inputGenerator.fileContents(fileName)); |
| 267 |
edit->document()->setModified(false); |
| 268 |
++index; |
| 269 |
} |
| 270 |
|
| 271 |
// Reset dirty buffer list and cached option list |
| 272 |
m_dirtyTextEdits.clear(); |
| 273 |
m_optionCache = collectOptions(); |
| 274 |
|
| 275 |
// Restore current tab |
| 276 |
if (!currentWidget.isNull()) |
| 277 |
m_ui->tabWidget->setCurrentWidget(currentWidget); |
| 278 |
} |
| 279 |
|
| 280 |
void InputGeneratorWidget::defaultsClicked() |
| 281 |
{ |
| 282 |
setOptionDefaults(); |
| 283 |
updatePreviewTextImmediately(); |
| 284 |
} |
| 285 |
|
| 286 |
void InputGeneratorWidget::generateClicked() |
| 287 |
{ |
| 288 |
if (m_textEdits.size() == 1) |
| 289 |
saveSingleFile(m_textEdits.keys().first()); |
| 290 |
else if (m_textEdits.size() > 1) |
| 291 |
saveDirectory(); |
| 292 |
else |
| 293 |
showError(tr("No input files to save!")); |
| 294 |
} |
| 295 |
|
| 296 |
void InputGeneratorWidget::computeClicked() |
| 297 |
{ |
| 298 |
// Verify that molequeue is running: |
| 299 |
MoleQueueManager& mqManager = MoleQueueManager::instance(); |
| 300 |
if (!mqManager.connectIfNeeded()) { |
| 301 |
QMessageBox::information(this, tr("Cannot connect to MoleQueue"), |
| 302 |
tr("Cannot connect to MoleQueue server. Please " |
| 303 |
"ensure that it is running and try again.")); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
// Collect info for the MoleQueueDialog: |
| 308 |
const QString mainFileName = m_inputGenerator.mainFileName(); |
| 309 |
|
| 310 |
QString description; |
| 311 |
if (!optionString("Title", description) || description.isEmpty()) |
| 312 |
description = generateJobTitle(); |
| 313 |
|
| 314 |
QString coresString; |
| 315 |
int numCores = |
| 316 |
optionString("Processor Cores", coresString) ? coresString.toInt() : 1; |
| 317 |
|
| 318 |
JobObject job; |
| 319 |
job.setProgram(m_inputGenerator.displayName()); |
| 320 |
job.setDescription(description); |
| 321 |
job.setValue("numberOfCores", numCores); |
| 322 |
for (QMap<QString, QTextEdit*>::const_iterator it = m_textEdits.constBegin(), |
| 323 |
itEnd = m_textEdits.constEnd(); |
| 324 |
it != itEnd; ++it) { |
| 325 |
const QString& fileName = it.key(); |
| 326 |
if (fileName != mainFileName) |
| 327 |
job.appendAdditionalInputFile(fileName, it.value()->toPlainText()); |
| 328 |
else |
| 329 |
job.setInputFile(fileName, it.value()->toPlainText()); |
| 330 |
} |
| 331 |
|
| 332 |
MoleQueueDialog::SubmitStatus result = MoleQueueDialog::submitJob( |
| 333 |
this, tr("Submit %1 Calculation").arg(m_inputGenerator.displayName()), job, |
| 334 |
MoleQueueDialog::WaitForSubmissionResponse | |
| 335 |
MoleQueueDialog::SelectProgramFromTemplate); |
| 336 |
|
| 337 |
switch (result) { |
| 338 |
default: |
| 339 |
case MoleQueueDialog::SubmissionSuccessful: |
| 340 |
case MoleQueueDialog::SubmissionFailed: |
| 341 |
case MoleQueueDialog::SubmissionAttempted: |
| 342 |
case MoleQueueDialog::SubmissionAborted: |
| 343 |
// The dialog handles these cases adequately, we don't need to do |
| 344 |
// anything. |
| 345 |
break; |
| 346 |
|
| 347 |
case MoleQueueDialog::JobFailed: |
| 348 |
// Inform the user: |
| 349 |
QMessageBox::information(this, tr("Job Failed"), |
| 350 |
tr("The job did not complete successfully."), |
| 351 |
QMessageBox::Ok); |
| 352 |
break; |
| 353 |
|
| 354 |
case MoleQueueDialog::JobFinished: |
| 355 |
// Let the world know that the job is ready to open. job has been |
| 356 |
// overwritten with the final job details. |
| 357 |
emit openJobOutput(job); |
| 358 |
// Hide the parent if it's a dialog: |
| 359 |
if (auto* dlg = qobject_cast<QDialog*>(parent())) |
| 360 |
dlg->hide(); |
| 361 |
break; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
void InputGeneratorWidget::setWarning(const QString& warn) |
| 366 |
{ |
| 367 |
qWarning() << tr("Script returns warnings:\n") << warn; |
| 368 |
|
| 369 |
m_ui->warningText->setText(warn); |
| 370 |
m_ui->warningBox->show(); |
| 371 |
} |
| 372 |
|
| 373 |
void InputGeneratorWidget::toggleWarningText() |
| 374 |
{ |
| 375 |
if (m_ui->warningText->isVisible()) |
| 376 |
hideWarningText(); |
| 377 |
else |
| 378 |
showWarningText(); |
| 379 |
} |
| 380 |
|
| 381 |
void InputGeneratorWidget::showWarningText() |
| 382 |
{ |
| 383 |
m_ui->warningText->show(); |
| 384 |
m_ui->warningTextButton->setText(tr("Hide &Warnings")); |
| 385 |
} |
| 386 |
|
| 387 |
void InputGeneratorWidget::hideWarningText() |
| 388 |
{ |
| 389 |
m_ui->warningText->hide(); |
| 390 |
m_ui->warningTextButton->setText(tr("Show &Warnings")); |
| 391 |
} |
| 392 |
|
| 393 |
void InputGeneratorWidget::resetWarningDisplay() |
| 394 |
{ |
| 395 |
m_ui->warningBox->hide(); |
| 396 |
showWarningText(); |
| 397 |
} |
| 398 |
|
| 399 |
void InputGeneratorWidget::showError(const QString& err) |
| 400 |
{ |
| 401 |
qWarning() << err; |
| 402 |
|
| 403 |
QWidget* theParent = |
| 404 |
this->isVisible() ? this : qobject_cast<QWidget*>(parent()); |
| 405 |
QDialog dlg(theParent); |
| 406 |
auto* vbox = new QVBoxLayout(); |
| 407 |
auto* label = new QLabel(tr("An error has occurred:")); |
| 408 |
vbox->addWidget(label); |
| 409 |
auto* textBrowser = new QTextBrowser(); |
| 410 |
|
| 411 |
// adjust the size of the text browser to ~80 char wide, ~20 lines high |
| 412 |
QSize theSize = textBrowser->sizeHint(); |
| 413 |
QFontMetrics metrics(textBrowser->currentFont()); |
| 414 |
int charWidth = metrics.horizontalAdvance("i7OPlmWn9/") / 10; |
| 415 |
int charHeight = metrics.lineSpacing(); |
| 416 |
theSize.setWidth(80 * charWidth); |
| 417 |
theSize.setHeight(20 * charHeight); |
| 418 |
textBrowser->setMinimumSize(theSize); |
| 419 |
textBrowser->setText(err); |
| 420 |
vbox->addWidget(textBrowser); |
| 421 |
dlg.setLayout(vbox); |
| 422 |
|
| 423 |
dlg.exec(); |
| 424 |
} |
| 425 |
|
| 426 |
void InputGeneratorWidget::textEditModified() |
| 427 |
{ |
| 428 |
if (auto* edit = qobject_cast<QTextEdit*>(sender())) { |
| 429 |
if (edit->document()->isModified()) { |
| 430 |
if (!m_dirtyTextEdits.contains(edit)) |
| 431 |
m_dirtyTextEdits << edit; |
| 432 |
} else { |
| 433 |
m_dirtyTextEdits.removeOne(edit); |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
void InputGeneratorWidget::updateTitlePlaceholder() |
| 439 |
{ |
| 440 |
if (auto* titleEdit = |
| 441 |
qobject_cast<QLineEdit*>(m_widgets.value("Title", nullptr))) { |
| 442 |
titleEdit->setPlaceholderText(generateJobTitle()); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
QString InputGeneratorWidget::settingsKey(const QString& identifier) const |
| 447 |
{ |
| 448 |
return QString("quantumInput/%1/%2") |
| 449 |
.arg(m_inputGenerator.displayName(), identifier); |
| 450 |
} |
| 451 |
|
| 452 |
void InputGeneratorWidget::saveSingleFile(const QString& fileName) |
| 453 |
{ |
| 454 |
QSettings settings; |
| 455 |
QString filePath = settings.value(settingsKey("outputDirectory")).toString(); |
| 456 |
if (filePath.isEmpty()) |
| 457 |
filePath = QDir::homePath(); |
| 458 |
filePath = QFileDialog::getSaveFileName(this, tr("Select output filename"), |
| 459 |
filePath + "/" + fileName); |
| 460 |
|
| 461 |
// User cancel: |
| 462 |
if (filePath.isNull()) |
| 463 |
return; |
| 464 |
|
| 465 |
settings.setValue(settingsKey("outputDirectory"), |
| 466 |
QFileInfo(filePath).absoluteDir().absolutePath()); |
| 467 |
|
| 468 |
QTextEdit* edit = m_textEdits.value(fileName, nullptr); |
| 469 |
if (!edit) { |
| 470 |
showError(tr("Internal error: could not find text widget for filename '%1'") |
| 471 |
.arg(fileName)); |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
QFile file(filePath); |
| 476 |
bool success = false; |
| 477 |
if (file.open(QFile::WriteOnly | QFile::Text)) { |
| 478 |
if (file.write(edit->toPlainText().toLocal8Bit()) > 0) { |
| 479 |
success = true; |
| 480 |
} |
| 481 |
file.close(); |
| 482 |
} |
| 483 |
|
| 484 |
if (!success) { |
| 485 |
QMessageBox::critical( |
| 486 |
this, tr("Output Error"), |
| 487 |
tr("Failed to write to file %1.").arg(file.fileName())); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
void InputGeneratorWidget::saveDirectory() |
| 492 |
{ |
| 493 |
QSettings settings; |
| 494 |
QString directory = settings.value(settingsKey("outputDirectory")).toString(); |
| 495 |
if (directory.isEmpty()) |
| 496 |
directory = QDir::homePath(); |
| 497 |
directory = QFileDialog::getExistingDirectory( |
| 498 |
this, tr("Select output directory"), directory); |
| 499 |
|
| 500 |
// User cancel: |
| 501 |
if (directory.isNull()) |
| 502 |
return; |
| 503 |
|
| 504 |
settings.setValue(settingsKey("outputDirectory"), directory); |
| 505 |
QDir dir(directory); |
| 506 |
|
| 507 |
QStringList fileNames = m_textEdits.keys(); |
| 508 |
|
| 509 |
// Check for problems: |
| 510 |
QStringList errors; |
| 511 |
bool fatalError = false; |
| 512 |
|
| 513 |
do { // Do/while to break on fatal errors |
| 514 |
if (!dir.exists()) { |
| 515 |
errors << tr("%1: Directory does not exist!").arg(dir.absolutePath()); |
| 516 |
fatalError = true; |
| 517 |
break; |
| 518 |
} |
| 519 |
|
| 520 |
if (!dir.isReadable()) { |
| 521 |
errors << tr("%1: Directory cannot be read!").arg(dir.absolutePath()); |
| 522 |
fatalError = true; |
| 523 |
break; |
| 524 |
} |
| 525 |
|
| 526 |
foreach (const QString& fileName, fileNames) { |
| 527 |
QFileInfo info(dir.absoluteFilePath(fileName)); |
| 528 |
|
| 529 |
if (info.exists()) { |
| 530 |
errors |
| 531 |
<< tr("%1: File will be overwritten.").arg(info.absoluteFilePath()); |
| 532 |
} |
| 533 |
|
| 534 |
// Attempt to open the file for writing |
| 535 |
if (!QFile(info.absoluteFilePath()).open(QFile::WriteOnly)) { |
| 536 |
errors << tr("%1: File is not writable.").arg(info.absoluteFilePath()); |
| 537 |
fatalError = true; |
| 538 |
break; |
| 539 |
} |
| 540 |
} |
| 541 |
} while (false); // only run once |
| 542 |
|
| 543 |
// Handle fatal errors: |
| 544 |
if (fatalError) { |
| 545 |
QString formattedError; |
| 546 |
switch (errors.size()) { |
| 547 |
case 0: |
| 548 |
formattedError = |
| 549 |
tr("The input files cannot be written due to an unknown error."); |
| 550 |
break; |
| 551 |
case 1: |
| 552 |
formattedError = |
| 553 |
tr("The input files cannot be written:\n\n%1").arg(errors.first()); |
| 554 |
break; |
| 555 |
default: { |
| 556 |
// If a fatal error occurred, it will be last one in the list. Pop it |
| 557 |
// off and tell the user that it was the reason we had to stop. |
| 558 |
QString fatal = errors.last(); |
| 559 |
QStringList tmp(errors); |
| 560 |
tmp.pop_back(); |
| 561 |
formattedError = |
| 562 |
tr("The input files cannot be written:\n\n%1\n\nWarnings:\n\n%2") |
| 563 |
.arg(fatal, tmp.join("\n")); |
| 564 |
break; |
| 565 |
} |
| 566 |
} |
| 567 |
showError(formattedError); |
| 568 |
return; |
| 569 |
} |
| 570 |
|
| 571 |
// Non-fatal errors: |
| 572 |
if (!errors.isEmpty()) { |
| 573 |
QString formattedError = tr("Warning:\n\n%1\n\nWould you like to continue?") |
| 574 |
.arg(errors.join("\n")); |
| 575 |
|
| 576 |
QMessageBox::StandardButton reply = |
| 577 |
QMessageBox::warning(this, tr("Write input files"), formattedError, |
| 578 |
QMessageBox::Yes | QMessageBox::No, QMessageBox::No); |
| 579 |
|
| 580 |
if (reply != QMessageBox::Yes) |
| 581 |
return; |
| 582 |
} |
| 583 |
|
| 584 |
foreach (const QString& fileName, fileNames) { |
| 585 |
QTextEdit* edit = m_textEdits.value(fileName); |
| 586 |
QFile file(dir.absoluteFilePath(fileName)); |
| 587 |
bool success = false; |
| 588 |
if (file.open(QFile::WriteOnly | QFile::Text)) { |
| 589 |
if (file.write(edit->toPlainText().toLocal8Bit()) > 0) { |
| 590 |
success = true; |
| 591 |
} |
| 592 |
file.close(); |
| 593 |
} |
| 594 |
|
| 595 |
if (!success) { |
| 596 |
QMessageBox::critical( |
| 597 |
this, tr("Output Error"), |
| 598 |
tr("Failed to write to file %1.").arg(file.fileName())); |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
QJsonObject InputGeneratorWidget::promptForBatchJobOptions() const |
| 604 |
{ |
| 605 |
// Verify that molequeue is running: |
| 606 |
MoleQueueManager& mqManager = MoleQueueManager::instance(); |
| 607 |
if (!mqManager.connectIfNeeded()) { |
| 608 |
QMessageBox::information(this->parentWidget(), |
| 609 |
tr("Cannot connect to MoleQueue"), |
| 610 |
tr("Cannot connect to MoleQueue server. Please " |
| 611 |
"ensure that it is running and try again.")); |
| 612 |
return QJsonObject(); |
| 613 |
} |
| 614 |
|
| 615 |
QString coresString; |
| 616 |
int numCores = |
| 617 |
optionString("Processor Cores", coresString) ? coresString.toInt() : 1; |
| 618 |
|
| 619 |
JobObject job; |
| 620 |
job.setProgram(m_inputGenerator.displayName()); |
| 621 |
job.setValue("numberOfCores", numCores); |
| 622 |
|
| 623 |
if (!MoleQueueDialog::promptForJobOptions(this->parentWidget(), |
| 624 |
tr("Configure Job"), job)) { |
| 625 |
return QJsonObject(); |
| 626 |
} |
| 627 |
|
| 628 |
return job.json(); |
| 629 |
} |
| 630 |
|
| 631 |
void InputGeneratorWidget::connectButtons() |
| 632 |
{ |
| 633 |
connect(m_ui->debugCheckBox, SIGNAL(toggled(bool)), &m_inputGenerator, |
| 634 |
SLOT(setDebug(bool))); |
| 635 |
connect(m_ui->debugCheckBox, SIGNAL(toggled(bool)), |
| 636 |
SLOT(updatePreviewText())); |
| 637 |
connect(m_ui->defaultsButton, SIGNAL(clicked()), SLOT(defaultsClicked())); |
| 638 |
connect(m_ui->generateButton, SIGNAL(clicked()), SLOT(generateClicked())); |
| 639 |
connect(m_ui->closeButton, SIGNAL(clicked()), SIGNAL(closeClicked())); |
| 640 |
connect(m_ui->warningTextButton, SIGNAL(clicked()), |
| 641 |
SLOT(toggleWarningText())); |
| 642 |
|
| 643 |
// disable the compute button if Molequeue is not running |
| 644 |
MoleQueueManager& mqManager = MoleQueueManager::instance(); |
| 645 |
if (!mqManager.connectIfNeeded()) { |
| 646 |
m_ui->computeButton->setEnabled(false); |
| 647 |
} else { |
| 648 |
connect(m_ui->computeButton, SIGNAL(clicked()), SLOT(computeClicked())); |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
void InputGeneratorWidget::updateOptions() |
| 653 |
{ |
| 654 |
m_options = m_inputGenerator.options(); |
| 655 |
|
| 656 |
if (m_inputGenerator.hasErrors()) { |
| 657 |
showError(m_inputGenerator.errorList().join("\n\n")); |
| 658 |
m_inputGenerator.clearErrors(); |
| 659 |
} |
| 660 |
|
| 661 |
m_centralWidget = m_ui->optionsWidget; |
| 662 |
|
| 663 |
// Create the widgets, etc for the gui |
| 664 |
buildOptionGui(); |
| 665 |
setOptionDefaults(); |
| 666 |
} |
| 667 |
|
| 668 |
} // namespace Avogadro::MoleQueue |