Changeset 430bd7f7e9 in tspsg for src/mainwindow.cpp


Ignore:
Timestamp:
Jul 31, 2009, 8:23:07 PM (15 years ago)
Author:
Oleksii Serdiuk
Branches:
0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
Children:
ec54b4490b
Parents:
b5c9bcb585
Message:

+ Finished solving algorithm (needs thorough testing).
+ Solution can be saved to HTML or OpenDocument? format.
+ Added VERSIONINFO resource for windows builds.

  • Updated translations to have unified terminology everywhere.

NB: This will be the first public alpha build.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mainwindow.cpp

    rb5c9bcb585 r430bd7f7e9  
    1 /*
    2  *  TSPSG - TSP Solver and Generator
     1        /*
     2 *  TSPSG: TSP Solver and Generator
    33 *  Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name>
    44 *
     
    3030        loadLanguage();
    3131        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);
    3236#ifdef Q_OS_WINCE
    3337        // A little hack for toolbar icons to have sane size.
     
    4650        connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered()));
    4751        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()));
    4954        connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered()));
    5055        connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool)));
     
    5964        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int)));
    6065QRect rect = geometry();
    61 #ifdef Q_OS_WINCE
    62         // 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 :-\
    6766        setCentralWidget(tabWidget);
    68 #else
     67#ifndef Q_OS_WINCE
    6968        if (settings->value("SavePos",false).toBool()) {
    7069                // Loading of saved window state
     
    9291        taskView->resizeRowsToContents();
    9392#endif // Q_OS_WINCE
     93}
     94
     95void MainWindow::enableSolutionActions(bool enable)
     96{
     97        actionFileSaveAsSolution->setEnabled(enable);
     98        solutionText->setEnabled(enable);
     99        if (!enable)
     100                output.clear();
    94101}
    95102
     
    141148}
    142149
     150void MainWindow::initDocStyleSheet()
     151{
     152QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>();
     153QColor 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
    143162void MainWindow::spinCitiesValueChanged(int n)
    144163{
     
    166185        tspmodel->clear();
    167186        setWindowModified(false);
     187        tabWidget->setCurrentIndex(0);
     188        solutionText->clear();
     189        enableSolutionActions(false);
    168190}
    169191
     
    190212        tspmodel->loadTask(files.first());
    191213        setWindowModified(false);
    192 }
    193 
    194 void MainWindow::actionFileSaveTaskTriggered()
     214        solutionText->clear();
     215        enableSolutionActions(false);
     216}
     217
     218void MainWindow::actionFileSaveAsTaskTriggered()
    195219{
    196220        saveTask();
     221}
     222
     223void MainWindow::actionFileSaveAsSolutionTriggered()
     224{
     225static QString selectedFile;
     226        if (selectedFile.isEmpty())
     227                selectedFile = "solution.html";
     228QFileDialog sd(this);
     229        sd.setAcceptMode(QFileDialog::AcceptSave);
     230QStringList 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;
     237QStringList files = sd.selectedFiles();
     238        if (files.empty())
     239                return;
     240        selectedFile = files.first();
     241QTextDocumentWriter 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());
    197245}
    198246
     
    219267{
    220268SettingsDialog 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        }
    222280}
    223281
     
    245303}
    246304
     305void MainWindow::outputMatrix(tMatrix matrix, QStringList &output, int nRow, int nCol)
     306{
     307int n = spinCities->value();
     308QString 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
    247326void MainWindow::buttonSolveClicked()
    248327{
    249         // TODO: Task solving goes here :-)
    250328tMatrix matrix;
    251 double *row;
     329QList<double> row;
    252330int n = spinCities->value();
    253331bool ok;
    254332        for (int r = 0; r < n; r++) {
    255                 row = new double[n];
     333                row.clear();
    256334                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));
    258336                        if (!ok) {
    259337                                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();
     
    266344sStep *root = solver.solve(spinCities->value(),matrix);
    267345        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));
     348QColor 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>");
     355sStep *step = root;
     356        n = 1;
     357QString 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>&nbsp;</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>&nbsp;&nbsp;" + 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();
    270384}
    271385
     
    274388        // TODO: Normal about window :-)
    275389QString 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"));
    277392        about += QString::fromUtf8("Target OS: %1\n").arg(OS);
    278393        about += "Qt library:\n";
Note: See TracChangeset for help on using the changeset viewer.