Changeset 42 in tspsg-svn for trunk/src/mainwindow.cpp
- Timestamp:
- Jul 31, 2009, 8:23:07 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/mainwindow.cpp
r41 r42 1 /*2 * TSPSG -TSP Solver and Generator1 /* 2 * TSPSG: TSP Solver and Generator 3 3 * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> 4 4 * … … 30 30 loadLanguage(); 31 31 setupUi(this); 32 initDocStyleSheet(); 33 solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); 34 solutionText->setTextColor(settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>()); 35 solutionText->setWordWrapMode(QTextOption::WordWrap); 32 36 #ifdef Q_OS_WINCE 33 37 // A little hack for toolbar icons to have sane size. … … 46 50 connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered())); 47 51 connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered())); 48 connect(actionFileSaveTask,SIGNAL(triggered()),this,SLOT(actionFileSaveTaskTriggered())); 52 connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered())); 53 connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered())); 49 54 connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered())); 50 55 connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool))); … … 59 64 connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int))); 60 65 QRect rect = geometry(); 61 #ifdef Q_OS_WINCE62 // HACK: Fix for all tabWidget elements becoming "unclickable" if making it central widget.63 /* rect.setSize(QApplication::desktop()->availableGeometry().size());64 rect.setHeight(rect.height() - (QApplication::desktop()->screenGeometry().height() - QApplication::desktop()->availableGeometry().height()));65 tabWidget->resize(rect.width(),rect.height() - toolBar->iconSize().height());*/66 // Somehow, this works now. No more "unclickable" elements :-\67 66 setCentralWidget(tabWidget); 68 # else67 #ifndef Q_OS_WINCE 69 68 if (settings->value("SavePos",false).toBool()) { 70 69 // Loading of saved window state … … 92 91 taskView->resizeRowsToContents(); 93 92 #endif // Q_OS_WINCE 93 } 94 95 void MainWindow::enableSolutionActions(bool enable) 96 { 97 actionFileSaveAsSolution->setEnabled(enable); 98 solutionText->setEnabled(enable); 99 if (!enable) 100 output.clear(); 94 101 } 95 102 … … 141 148 } 142 149 150 void MainWindow::initDocStyleSheet() 151 { 152 QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); 153 QColor hilight; 154 if (color.value() < 192) 155 hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2)); 156 else 157 hilight.setHsv(color.hue(),color.saturation(),color.value() / 2); 158 solutionText->document()->setDefaultStyleSheet("* {color: " + color.name() +";} p {margin: 0px 10px;} table {margin: 5px;} td {padding: 1px 5px;} .hasalts {color: " + hilight.name() + ";} .selected {color: #A00000; font-weight: bold;} .alternate {color: #008000; font-weight: bold;}"); 159 solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); 160 } 161 143 162 void MainWindow::spinCitiesValueChanged(int n) 144 163 { … … 166 185 tspmodel->clear(); 167 186 setWindowModified(false); 187 tabWidget->setCurrentIndex(0); 188 solutionText->clear(); 189 enableSolutionActions(false); 168 190 } 169 191 … … 190 212 tspmodel->loadTask(files.first()); 191 213 setWindowModified(false); 192 } 193 194 void MainWindow::actionFileSaveTaskTriggered() 214 solutionText->clear(); 215 enableSolutionActions(false); 216 } 217 218 void MainWindow::actionFileSaveAsTaskTriggered() 195 219 { 196 220 saveTask(); 221 } 222 223 void MainWindow::actionFileSaveAsSolutionTriggered() 224 { 225 static QString selectedFile; 226 if (selectedFile.isEmpty()) 227 selectedFile = "solution.html"; 228 QFileDialog sd(this); 229 sd.setAcceptMode(QFileDialog::AcceptSave); 230 QStringList filters(trUtf8("HTML Files") + " (*.html *.htm)"); 231 filters.append(trUtf8("OpenDocument Files") + " (*.odt)"); 232 filters.append(trUtf8("All Files") + " (*)"); 233 sd.setNameFilters(filters); 234 sd.selectFile(selectedFile); 235 if (sd.exec() != QDialog::Accepted) 236 return; 237 QStringList files = sd.selectedFiles(); 238 if (files.empty()) 239 return; 240 selectedFile = files.first(); 241 QTextDocumentWriter dw(selectedFile); 242 if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive))) 243 dw.setFormat("plaintext"); 244 dw.write(solutionText->document()); 197 245 } 198 246 … … 219 267 { 220 268 SettingsDialog sd(this); 221 sd.exec(); 269 if (sd.exec() != QDialog::Accepted) 270 return; 271 if (sd.colorChanged() || sd.fontChanged()) { 272 initDocStyleSheet(); 273 if (!output.isEmpty() && sd.colorChanged() && (QMessageBox(QMessageBox::Question,trUtf8("Settings Changed"),trUtf8("You have changed color settings.\nDo you wish to apply them to current solution text?"),QMessageBox::Yes | QMessageBox::No,this).exec() == QMessageBox::Yes)) { 274 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 275 solutionText->clear(); 276 solutionText->setHtml(output.join("")); 277 QApplication::restoreOverrideCursor(); 278 } 279 } 222 280 } 223 281 … … 245 303 } 246 304 305 void MainWindow::outputMatrix(tMatrix matrix, QStringList &output, int nRow, int nCol) 306 { 307 int n = spinCities->value(); 308 QString line=""; 309 output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); 310 for (int r = 0; r < n; r++) { 311 line = "<tr>"; 312 for (int c = 0; c < n; c++) { 313 if (matrix[r][c] == INFINITY) 314 line += "<td align=\"center\">"INFSTR"</td>"; 315 else if ((r == nRow) && (c == nCol)) 316 line += "<td align=\"center\" class=\"selected\">" + QVariant(matrix[r][c]).toString() + "</td>"; 317 else 318 line += "<td align=\"center\">" + QVariant(matrix[r][c]).toString() + "</td>"; 319 } 320 line += "</tr>"; 321 output.append(line); 322 } 323 output.append("</table>"); 324 } 325 247 326 void MainWindow::buttonSolveClicked() 248 327 { 249 // TODO: Task solving goes here :-)250 328 tMatrix matrix; 251 double *row;329 QList<double> row; 252 330 int n = spinCities->value(); 253 331 bool ok; 254 332 for (int r = 0; r < n; r++) { 255 row = new double[n];333 row.clear(); 256 334 for (int c = 0; c < n; c++) { 257 row [c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok);335 row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); 258 336 if (!ok) { 259 337 QMessageBox(QMessageBox::Critical,trUtf8("Data error"),QString(trUtf8("Error in cell [Row %1; Column %2]: Invalid data format.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec(); … … 266 344 sStep *root = solver.solve(spinCities->value(),matrix); 267 345 if (!root) 268 QMessageBox(QMessageBox::Critical,trUtf8("Solution error"),trUtf8("There was an error while solving the task."),QMessageBox::Ok,this).exec(); 269 // tabWidget->setCurrentIndex(1); 346 return; 347 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 348 QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); 349 output.clear(); 350 output.append("<p>" + trUtf8("Variant #%1").arg(spinVariant->value()) + "</p>"); 351 output.append("<p>" + trUtf8("Task:") + "</p>"); 352 outputMatrix(matrix,output); 353 output.append("<hr>"); 354 output.append("<p>" + trUtf8("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>"); 355 sStep *step = root; 356 n = 1; 357 QString path = ""; 358 while (n <= spinCities->value()) { 359 if (step->prNode->prNode != NULL || (step->prNode->prNode == NULL && step->plNode->prNode == NULL)) { 360 if (n != spinCities->value()) { 361 output.append("<p>" + trUtf8("Step #%1").arg(n++) + "</p>"); 362 outputMatrix(step->matrix,output,step->candidate.nRow,step->candidate.nCol); 363 if (step->alts) 364 output.append("<p class=\"hasalts\">" + trUtf8("This step has alternate candidates for branching.") + "</p>"); 365 output.append("<p> </p>"); 366 } 367 path += QString(" (%1,%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1); 368 } 369 if (step->prNode->prNode != NULL) 370 step = step->prNode; 371 else if (step->plNode->prNode != NULL) 372 step = step->plNode; 373 else 374 break; 375 } 376 output.append("<p>" + trUtf8("Optimal path:") + "</p>"); 377 output.append("<p> " + path + "</p>"); 378 output.append("<p>" + trUtf8("The price is <b>%1</b> units.").arg(step->price) + "</p>"); 379 solutionText->setHtml(output.join("")); 380 solutionText->setDocumentTitle(trUtf8("Solution of Variant #%1 task").arg(spinVariant->value())); 381 enableSolutionActions(); 382 tabWidget->setCurrentIndex(1); 383 QApplication::restoreOverrideCursor(); 270 384 } 271 385 … … 274 388 // TODO: Normal about window :-) 275 389 QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n"); 276 about += QString::fromUtf8(" Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n").arg(QDate::currentDate().toString("yyyy")); 390 about += QString::fromUtf8(" Version: "BUILD_VERSION" ("BUILD_STATUS")\n"); 391 about += QString::fromUtf8(" Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n").arg(QDate::currentDate().toString("yyyy")); 277 392 about += QString::fromUtf8("Target OS: %1\n").arg(OS); 278 393 about += "Qt library:\n";
Note: See TracChangeset
for help on using the changeset viewer.