Changeset f0464480db in tspsg for src/mainwindow.cpp


Ignore:
Timestamp:
Dec 16, 2009, 11:22:05 PM (14 years ago)
Author:
Oleksii Serdiuk
Branches:
0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
Children:
0bd0e1aca7
Parents:
53f11f0e6c
Message:

+ CTSPSolver class now deletes all solution tree on cleanup and delete to avoid memory leaks.
+ List of alternate candidates for branching is now saved in every solution step and displayed on output.
+ New struct TCandidate that represents a candidate for branching.

  • Renamed sStep to SStep and tMatrix to TMatrix.
  • Made a better and more readable About window.
  • English translation is now loaded too. This is needed to support plurals (e.g., 1 candidate, 4 candidates).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mainwindow.cpp

    r53f11f0e6c rf0464480db  
    302302{
    303303//! \todo TODO: Normal about window :-)
    304 QString about = QString::fromUtf8("TSPSG: TSP Solver and Generator\n");
    305         about += QString::fromUtf8("    Version: "BUILD_VERSION"\n");
    306         about += QString::fromUtf8("    Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n").arg(QDate::currentDate().toString("yyyy"));
    307         about += QString::fromUtf8("Target OS: %1\n").arg(OS);
    308         about += "Qt library:\n";
    309         about += QString::fromUtf8("    Compile time: %1\n").arg(QT_VERSION_STR);
    310         about += QString::fromUtf8("    Runtime: %1\n").arg(qVersion());
    311         about += QString::fromUtf8("Built on %1 at %2\n").arg(__DATE__).arg(__TIME__);
    312         about += QString::fromUtf8(VERSIONID"\n\n");
    313         about += QString::fromUtf8("Algorithm: %1\n").arg(CTSPSolver::getVersionId());
    314         about += "\n";
    315         about += "TSPSG is licensed under the terms of the GNU General Public License. You should have received a copy of the GNU General Public License along with TSPSG.";
    316         QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec();
     304QString about = QString::fromUtf8("<b>TSPSG: TSP Solver and Generator</b><br>");
     305        about += QString::fromUtf8("&nbsp;&nbsp;&nbsp;&nbsp;Version: <b>"BUILD_VERSION"</b><br>");
     306        about += QString::fromUtf8("&nbsp;&nbsp;&nbsp;&nbsp;Copyright: <b>&copy; 2007-%1 Lёppa</b><br>").arg(QDate::currentDate().toString("yyyy"));
     307        about += QString::fromUtf8("&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sourceforge.net/</a></b><br>");
     308        about += "<br>";
     309        about += QString::fromUtf8("Target OS: <b>%1</b><br>").arg(OS);
     310        about += "Qt library:<br>";
     311        about += QString::fromUtf8("&nbsp;&nbsp;&nbsp;&nbsp;Build time: <b>%1</b><br>").arg(QT_VERSION_STR);
     312        about += QString::fromUtf8("&nbsp;&nbsp;&nbsp;&nbsp;Runtime: <b>%1</b><br>").arg(qVersion());
     313        about += QString::fromUtf8("Built on <b>%1</b> at <b>%2</b><br>").arg(__DATE__).arg(__TIME__);
     314        about += QString::fromUtf8("Id: <b>"VERSIONID"</b><br>");
     315        about += QString::fromUtf8("Algorithm: <b>%1</b><br>").arg(CTSPSolver::getVersionId());
     316        about += "<br>";
     317        about += "TSPSG is free software: you can redistribute it and/or modify it<br>"
     318                "under the terms of the GNU General Public License as published<br>"
     319                "by the Free Software Foundation, either version 3 of the License,<br>"
     320                "or (at your option) any later version.<br>"
     321                "<br>"
     322                "TSPSG is distributed in the hope that it will be useful, but<br>"
     323                "WITHOUT ANY WARRANTY; without even the implied warranty of<br>"
     324                "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>"
     325                "GNU General Public License for more details.<br>"
     326                "<br>"
     327                "You should have received a copy of the GNU General Public License<br>"
     328                "along with TSPSG.  If not, see <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a>.";
     329
     330QDialog *dlg = new QDialog(this);
     331QLabel *lblIcon = new QLabel(dlg);
     332QTextBrowser *txtAbout = new QTextBrowser(dlg);
     333QVBoxLayout *vb1 = new QVBoxLayout(),
     334        *vb2 = new QVBoxLayout();
     335QHBoxLayout *hb = new QHBoxLayout();
     336QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, dlg);
     337
     338        lblIcon->setPixmap(QPixmap(":/images/tspsg.png").scaledToWidth(64, Qt::SmoothTransformation));
     339
     340        vb1->addWidget(lblIcon);
     341        vb1->addStretch();
     342
     343//      txtAbout->setTextInteractionFlags(txtAbout->textInteractionFlags() ^ Qt::TextEditable);
     344        txtAbout->setWordWrapMode(QTextOption::NoWrap);
     345        txtAbout->setOpenExternalLinks(true);
     346        txtAbout->setHtml(about);
     347        txtAbout->moveCursor(QTextCursor::Start);
     348
     349        hb->addLayout(vb1);
     350        hb->addWidget(txtAbout);
     351
     352        vb2->addLayout(hb);
     353        vb2->addWidget(bb);
     354
     355        dlg->setWindowTitle(trUtf8("About TSPSG"));
     356        dlg->setLayout(vb2);
     357
     358        connect(bb, SIGNAL(accepted()), dlg, SLOT(accept()));
     359
     360        dlg->resize(475, 350);
     361        dlg->exec();
     362
     363        delete dlg;
    317364}
    318365
     
    331378void MainWindow::buttonSolveClicked()
    332379{
    333 tMatrix matrix;
     380TMatrix matrix;
    334381QList<double> row;
    335382int n = spinCities->value();
     
    347394        }
    348395CTSPSolver solver;
    349 sStep *root = solver.solve(n,matrix,this);
     396SStep *root = solver.solve(n,matrix,this);
    350397        if (!root)
    351398                return;
     
    358405        output.append("<hr>");
    359406        output.append("<p>" + trUtf8("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>");
    360 sStep *step = root;
     407SStep *step = root;
    361408        n = 1;
    362409        while (n <= spinCities->value()) {
    363                 if (step->prNode->prNode != NULL || (step->prNode->prNode == NULL && step->plNode->prNode == NULL)) {
     410                if (step->prNode->prNode != NULL || ((step->prNode->prNode == NULL) && (step->plNode->prNode == NULL))) {
    364411                        if (n != spinCities->value()) {
    365412                                output.append("<p>" + trUtf8("Step #%1").arg(n++) + "</p>");
    366                                 outputMatrix(step->matrix,output,step->candidate.nRow,step->candidate.nCol);
    367                                 if (step->alts)
    368                                         output.append("<p class=\"hasalts\">" + trUtf8("This step has alternate candidates for branching.") + "</p>");
     413                                outputMatrix(*step, output);
     414                                output.append("<p>" + trUtf8("Selected candidate for branching: %1.").arg(trUtf8("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)) + "</p>");
     415                                if (!step->alts.empty()) {
     416TCandidate cand;
     417QString alts;
     418                                        foreach(cand, step->alts) {
     419                                                if (!alts.isEmpty())
     420                                                        alts += ", ";
     421                                                alts += trUtf8("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1);
     422                                        }
     423                                        output.append("<p class=\"hasalts\">" + trUtf8("%n alternate candidate(s) for branching: %1.","",step->alts.count()).arg(alts) + "</p>");
     424                                }
    369425                                output.append("<p>&nbsp;</p>");
    370426                        }
     
    392448
    393449        // Scrolling to the end of text.
    394 QTextCursor cursor(solutionText->textCursor());
    395         cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
    396         solutionText->setTextCursor(cursor);
     450        solutionText->moveCursor(QTextCursor::End);
    397451
    398452        enableSolutionActions();
     
    536590                qtTranslator = NULL;
    537591        }
    538         qtTranslator = new QTranslator();
    539592static QTranslator *translator; // Application translator
    540593        if (translator) {
     
    543596        }
    544597        translator = new QTranslator();
    545         if (lng.compare("en") && !lng.startsWith("en_")) {
     598        if ((lng.compare("en") != 0) && !lng.startsWith("en_")) {
    546599                // Trying to load system Qt library translation...
     600                qtTranslator = new QTranslator();
    547601                if (qtTranslator->load("qt_" + lng,QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
    548602                        qApp->installTranslator(qtTranslator);
    549                 else
    550                         // No luck. Let's try to load bundled one.
     603                else {
     604                        // No luck. Let's try to load a bundled one.
    551605                        if (qtTranslator->load("qt_" + lng,PATH_I18N))
    552606                                qApp->installTranslator(qtTranslator);
     
    556610                                qtTranslator = NULL;
    557611                        }
    558                 // Now let's load application translation.
    559                 if (translator->load(lng,PATH_I18N))
    560                         qApp->installTranslator(translator);
    561                 else {
     612                }
     613        }
     614        // Now let's load application translation.
     615        if (translator->load(lng,PATH_I18N))
     616                qApp->installTranslator(translator);
     617        else {
     618                delete translator;
     619                translator = NULL;
     620                if ((lng.compare("en") != 0) && !lng.startsWith("en_")) {
    562621                        if (!ad)
    563622                                QMessageBox(QMessageBox::Warning,trUtf8("Language Change"),trUtf8("Unable to load translation language."),QMessageBox::Ok,this).exec();
    564                         delete translator;
    565                         translator = NULL;
    566623                        return false;
    567624                }
     
    583640}
    584641
    585 void MainWindow::outputMatrix(const tMatrix &matrix, QStringList &output, int nRow, int nCol)
     642void MainWindow::outputMatrix(const TMatrix &matrix, QStringList &output)
    586643{
    587644int n = spinCities->value();
     
    593650                        if (matrix.at(r).at(c) == INFINITY)
    594651                                line += "<td align=\"center\">"INFSTR"</td>";
    595                         else if ((r == nRow) && (c == nCol))
    596                                 line += "<td align=\"center\" class=\"selected\">" + QVariant(matrix.at(r).at(c)).toString() + "</td>";
    597652                        else
    598653                                line += "<td align=\"center\">" + QVariant(matrix.at(r).at(c)).toString() + "</td>";
     654                }
     655                line += "</tr>";
     656                output.append(line);
     657        }
     658        output.append("</table>");
     659}
     660
     661void MainWindow::outputMatrix(const SStep &step, QStringList &output)
     662{
     663int n = spinCities->value();
     664QString line="";
     665        output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
     666        for (int r = 0; r < n; r++) {
     667                line = "<tr>";
     668                for (int c = 0; c < n; c++) {
     669                        if (step.matrix.at(r).at(c) == INFINITY)
     670                                line += "<td align=\"center\">"INFSTR"</td>";
     671                        else if ((r == step.candidate.nRow) && (c == step.candidate.nCol))
     672                                line += "<td align=\"center\" class=\"selected\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>";
     673                        else {
     674TCandidate cand;
     675                                cand.nRow = r;
     676                                cand.nCol = c;
     677                                if (step.alts.contains(cand))
     678                                        line += "<td align=\"center\" class=\"alternate\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>";
     679                                else
     680                                        line += "<td align=\"center\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>";
     681                        }
    599682                }
    600683                line += "</tr>";
Note: See TracChangeset for help on using the changeset viewer.