Changeset 317ba0432e in tspsg for src


Ignore:
Timestamp:
Apr 19, 2010, 10:33:02 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:
b574c383b7
Parents:
a23f4c292f
git-author:
Oleksii Serdiuk <contacts@…> (04/19/10 22:33:02)
git-committer:
Oleksii Serdiuk <contacts@…> (06/29/12 19:41:27)
Message:
  • Improved the solution output generation algorithm. Now it works directly with QTextDocument instead of "feeding" it with HTML. Thanks to this the solution output generation is now about 1.65 times faster.
  • Added .txt extension to ChangeLog?, INSTALL and README files to simplify opening these files in Windows.
  • Made a small improvement to Settings Dialog to follow the current application palette.
  • Output text color if not set is now determmined based on the current application palette.
Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/defaults.h

    ra23f4c292f r317ba0432e  
    9191        #define DEF_FONT_SIZE 10
    9292#endif // Q_OS_SYMBIAN
    93 //! Default font color
    94 #define DEF_FONT_COLOR Qt::black
     93//! Default text color
     94#define DEF_TEXT_COLOR QPalette().color(QPalette::Text)
     95//! Default selected candidate color
     96#define DEF_SELECTED_COLOR QColor(0x008000)
     97//! Default alternate candidate color
     98#define DEF_ALTERNATE_COLOR QColor(0xA00000)
    9599
    96100#endif // DEFAULTS_H
  • src/mainwindow.cpp

    ra23f4c292f r317ba0432e  
    512512        pd.setValue(0);
    513513
     514#ifdef DEBUG
     515QTime t;
     516        t.start();
     517#endif
    514518        solutionText->clear();
    515         solutionText->setDocumentTitle(tr("Solution of Variant #%1 task").arg(spinVariant->value()));
    516         solutionText->append("<p>" + tr("Variant #%1").arg(spinVariant->value()) + "</p>");
    517         solutionText->append("<p>" + tr("Task:") + "</p>");
    518         solutionText->append(outputMatrix(matrix));
    519         solutionText->append("<hr><p>" + tr("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>");
     519#ifdef DEBUG
     520        qDebug() << "Clear:" << t.elapsed();
     521        t.restart();
     522#endif
     523        solutionText->setDocumentTitle(tr("Solution of Variant #%1 Task").arg(spinVariant->value()));
     524
     525QTextDocument *doc = solutionText->document();
     526QTextCursor cur(doc);
     527
     528        cur.beginEditBlock();
     529        cur.setBlockFormat(fmt_paragraph);
     530        cur.insertText(tr("Variant #%1").arg(spinVariant->value()), fmt_default);
     531        cur.insertBlock(fmt_paragraph);
     532        cur.insertText(tr("Task:"));
     533        outputMatrix(cur, matrix);
     534        cur.insertHtml("<hr>");
     535        cur.insertBlock(fmt_paragraph);
     536        cur.insertText(tr("Solution of Variant #%1 Task").arg(spinVariant->value()), fmt_default);
     537        cur.endEditBlock();
     538
    520539SStep *step = root;
    521540        n = 1;
     
    523542        while (n < spinCities->value()) {
    524543                if (pd.wasCanceled()) {
     544                        pd.setLabelText(tr("Cleaning up..."));
     545                        pd.setMaximum(0);
     546                        pd.setCancelButton(NULL);
     547                        pd.show();
     548                        QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
     549                        solver.cleanup(true);
    525550                        solutionText->clear();
     551                        toggleSolutionActions(false);
    526552                        return;
    527553                }
     
    530556                if (step->prNode->prNode != NULL || ((step->prNode->prNode == NULL) && (step->plNode->prNode == NULL))) {
    531557                        if (n != spinCities->value()) {
    532                                 solutionText->append("<p>" + tr("Step #%1").arg(n++) + "</p>");
     558                                cur.beginEditBlock();
     559                                cur.insertBlock(fmt_paragraph);
     560                                cur.insertText(tr("Step #%1").arg(n++));
    533561                                if (settings->value("Output/ShowMatrix", DEF_SHOW_MATRIX).toBool() && (!settings->value("Output/UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT).toBool() || (settings->value("Output/UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT).toBool() && (spinCities->value() <= settings->value("Output/ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt())))) {
    534                                         solutionText->append(outputMatrix(*step));
     562                                        outputMatrix(cur, *step);
    535563                                }
    536                                 solutionText->append("<p>" + tr("Selected candidate for branching: %1.").arg(tr("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)) + "</p>");
     564                                cur.insertBlock(fmt_paragraph);
     565                                cur.insertText(tr("Selected candidate for branching: %1.").arg(tr("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)), fmt_default);
    537566                                if (!step->alts.empty()) {
    538567SCandidate cand;
     
    543572                                                alts += tr("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1);
    544573                                        }
    545                                         solutionText->append("<p class=\"hasalts\">" + tr("%n alternate candidate(s) for branching: %1.","",step->alts.count()).arg(alts) + "</p>");
     574                                        cur.insertBlock(fmt_paragraph);
     575                                        cur.insertText(tr("%n alternate candidate(s) for branching: %1.", "", step->alts.count()).arg(alts), fmt_altlist);
    546576                                }
    547                                 solutionText->append("<p>&nbsp;</p>");
     577                                cur.insertBlock(fmt_paragraph);
     578                                cur.insertText(" ", fmt_default);
     579                                cur.endEditBlock();
    548580                        }
    549581                }
     
    558590        pd.setValue(n);
    559591
     592        cur.beginEditBlock();
     593        cur.insertBlock(fmt_paragraph);
    560594        if (solver.isOptimal())
    561                 solutionText->append("<p>" + tr("Optimal path:") + "</p>");
     595                cur.insertText(tr("Optimal path:"));
    562596        else
    563                 solutionText->append("<p>" + tr("Resulting path:") + "</p>");
    564         solutionText->append("<p>&nbsp;&nbsp;" + solver.getSortedPath() + "</p>");
     597                cur.insertText(tr("Resulting path:"));
     598
     599        cur.insertBlock(fmt_paragraph);
     600        cur.insertText("  " + solver.getSortedPath());
     601
     602        cur.insertBlock(fmt_paragraph);
    565603        if (isInteger(step->price))
    566                 solutionText->append("<p>" + tr("The price is <b>%n</b> unit(s).", "", qRound(step->price)) + "</p>");
     604                cur.insertHtml("<p>" + tr("The price is <b>%n</b> unit(s).", "", qRound(step->price)) + "</p>");
    567605        else
    568                 solutionText->append("<p>" + tr("The price is <b>%1</b> units.").arg(step->price, 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()) + "</p>");
     606                cur.insertHtml("<p>" + tr("The price is <b>%1</b> units.").arg(step->price, 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()) + "</p>");
    569607        if (!solver.isOptimal()) {
    570                 solutionText->append("<p>&nbsp;</p>");
    571                 solutionText->append("<p>" + tr("<b>WARNING!!!</b><br>This result is a record, but it may not be optimal.<br>Iterations need to be continued to check whether this result is optimal or get an optimal one.") + "</p>");
    572         }
     608                cur.insertBlock(fmt_paragraph);
     609                cur.insertText(" ");
     610                cur.insertBlock(fmt_paragraph);
     611                cur.insertHtml("<p>" + tr("<b>WARNING!!!</b><br>This result is a record, but it may not be optimal.<br>Iterations need to be continued to check whether this result is optimal or get an optimal one.") + "</p>");
     612        }
     613        cur.endEditBlock();
     614#ifdef DEBUG
     615        qDebug() << "Generate:" << t.elapsed();
     616#endif
    573617
    574618        if (settings->value("Output/ScrollToEnd", DEF_SCROLL_TO_END).toBool()) {
    575                 // Scrolling to the end of text.
     619                // Scrolling to the end of the text.
    576620                solutionText->moveCursor(QTextCursor::End);
    577621        } else
     
    701745void MainWindow::initDocStyleSheet()
    702746{
    703 QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>();
     747        solutionText->document()->setDefaultFont(settings->value("Output/Font", QFont(DEF_FONT_FAMILY, DEF_FONT_SIZE)).value<QFont>());
     748
     749        fmt_paragraph.setTopMargin(0);
     750        fmt_paragraph.setRightMargin(10);
     751        fmt_paragraph.setBottomMargin(0);
     752        fmt_paragraph.setLeftMargin(10);
     753
     754        fmt_table.setTopMargin(5);
     755        fmt_table.setRightMargin(10);
     756        fmt_table.setBottomMargin(5);
     757        fmt_table.setLeftMargin(10);
     758        fmt_table.setBorder(0);
     759        fmt_table.setBorderStyle(QTextFrameFormat::BorderStyle_None);
     760        fmt_table.setCellSpacing(5);
     761
     762        fmt_center.setAlignment(Qt::AlignHCenter);
     763
     764QColor color = settings->value("Output/Colors/Text", DEF_TEXT_COLOR).value<QColor>();
    704765QColor hilight;
    705766        if (color.value() < 192)
    706                 hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2));
     767                hilight.setHsv(color.hue(), color.saturation(), 127 + qRound(color.value() / 2));
    707768        else
    708                 hilight.setHsv(color.hue(),color.saturation(),color.value() / 2);
    709         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;}");
    710         solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>());
     769                hilight.setHsv(color.hue(), color.saturation(), color.value() / 2);
     770
     771        solutionText->document()->setDefaultStyleSheet(QString("* {color: %1;}").arg(color.name()));
     772        fmt_default.setForeground(QBrush(color));
     773
     774        fmt_selected.setForeground(QBrush(settings->value("Output/Colors/Selected", DEF_SELECTED_COLOR).value<QColor>()));
     775        fmt_selected.setFontWeight(QFont::Bold);
     776
     777        fmt_alternate.setForeground(QBrush(settings->value("Output/Colors/Alternate", DEF_ALTERNATE_COLOR).value<QColor>()));
     778        fmt_alternate.setFontWeight(QFont::Bold);
     779        fmt_altlist.setForeground(QBrush(hilight));
     780
     781        solutionText->setTextColor(color);
    711782}
    712783
     
    811882}
    812883
    813 QString MainWindow::outputMatrix(const TMatrix &matrix) const
     884void MainWindow::outputMatrix(QTextCursor &cur, const TMatrix &matrix)
    814885{
    815886int n = spinCities->value();
    816 QString output(""), line;
    817         output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
     887QTextTable *table = cur.insertTable(n, n, fmt_table);
     888
    818889        for (int r = 0; r < n; r++) {
    819                 line = "<tr>";
    820890                for (int c = 0; c < n; c++) {
     891                        cur = table->cellAt(r, c).firstCursorPosition();
     892                        cur.setBlockFormat(fmt_center);
     893                        cur.setBlockCharFormat(fmt_default);
    821894                        if (matrix.at(r).at(c) == INFINITY)
    822                                 line += "<td align=\"center\">"INFSTR"</td>";
     895                                cur.insertText(INFSTR);
    823896                        else
    824                                 line += isInteger(matrix.at(r).at(c)) ? QString("<td align=\"center\">%1</td>").arg(matrix.at(r).at(c)) : QString("<td align=\"center\">%1</td>").arg(matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
    825                 }
    826                 line += "</tr>";
    827                 output.append(line);
    828         }
    829         output.append("</table>");
    830         return output;
    831 }
    832 
    833 QString MainWindow::outputMatrix(const SStep &step) const
     897                                cur.insertText(isInteger(matrix.at(r).at(c)) ? QString("%1").arg(matrix.at(r).at(c)) : QString("%1").arg(matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()));
     898                }
     899                QApplication::processEvents();
     900        }
     901        cur.movePosition(QTextCursor::End);
     902}
     903
     904void MainWindow::outputMatrix(QTextCursor &cur, const SStep &step)
    834905{
    835906int n = spinCities->value();
    836 QString output(""), line;
    837         output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
     907QTextTable *table = cur.insertTable(n, n, fmt_table);
     908
    838909        for (int r = 0; r < n; r++) {
    839                 line = "<tr>";
    840910                for (int c = 0; c < n; c++) {
     911                        cur = table->cellAt(r, c).firstCursorPosition();
     912                        cur.setBlockFormat(fmt_center);
    841913                        if (step.matrix.at(r).at(c) == INFINITY)
    842                                 line += "<td align=\"center\">"INFSTR"</td>";
     914                                cur.insertText(INFSTR, fmt_default);
    843915                        else if ((r == step.candidate.nRow) && (c == step.candidate.nCol))
    844                                 line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\" class=\"selected\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\" class=\"selected\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
     916                                cur.insertText(isInteger(step.matrix.at(r).at(c)) ? QString("%1").arg(step.matrix.at(r).at(c)) : QString("%1").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()), fmt_selected);
    845917                        else {
    846918SCandidate cand;
     
    848920                                cand.nCol = c;
    849921                                if (step.alts.contains(cand))
    850                                         line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\" class=\"alternate\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\" class=\"alternate\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
     922                                        cur.insertText(isInteger(step.matrix.at(r).at(c)) ? QString("%1").arg(step.matrix.at(r).at(c)) : QString("%1").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()), fmt_alternate);
    851923                                else
    852                                         line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
     924                                        cur.insertText(isInteger(step.matrix.at(r).at(c)) ? QString("%1").arg(step.matrix.at(r).at(c)) : QString("%1").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()), fmt_default);
    853925                        }
    854926                }
    855                 line += "</tr>";
    856                 output.append(line);
    857         }
    858         output.append("</table>");
    859         return output;
     927                QApplication::processEvents();
     928        }
     929
     930        cur.movePosition(QTextCursor::End);
    860931}
    861932
     
    9581029#endif // Q_OS_WINCE_WM
    9591030
    960         solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>());
    961         solutionText->setTextColor(settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>());
     1031        solutionText->document()->setDefaultFont(settings->value("Output/Font", QFont(DEF_FONT_FAMILY, DEF_FONT_SIZE)).value<QFont>());
    9621032        solutionText->setWordWrapMode(QTextOption::WordWrap);
    9631033
  • src/mainwindow.h

    ra23f4c292f r317ba0432e  
    101101#endif // Q_OS_WINCE_WM
    102102
     103        // Formats
     104        QTextTableFormat fmt_table;
     105        QTextBlockFormat fmt_paragraph,
     106                fmt_center;
     107        QTextCharFormat fmt_default,
     108                fmt_selected,
     109                fmt_alternate,
     110                fmt_altlist;
     111
    103112        void closeEvent(QCloseEvent *ev);
    104113        bool hasUpdater() const;
     
    107116        bool loadLanguage(const QString &lang = QString());
    108117        bool maybeSave();
    109         QString outputMatrix(const TMatrix &matrix) const;
    110         QString outputMatrix(const SStep &step) const;
     118        void outputMatrix(QTextCursor &cur, const TMatrix &matrix);
     119        void outputMatrix(QTextCursor &cur, const SStep &step);
    111120        void retranslateUi(bool all = true);
    112121        bool saveTask();
  • src/settingsdialog.cpp

    ra23f4c292f r317ba0432e  
    4141        buttonBox->button(QDialogButtonBox::Cancel)->setStatusTip(tr("Close without saving preferences"));
    4242        buttonBox->button(QDialogButtonBox::Cancel)->setCursor(QCursor(Qt::PointingHandCursor));
     43
     44QPalette p = bgWhite->palette();
     45        p.setColor(QPalette::Window, p.color(QPalette::Base));
     46        bgWhite->setPalette(p);
     47
     48        p = lineHorizontal->palette();
     49        p.setColor(QPalette::Window, p.color(QPalette::Text));
     50        lineHorizontal->setPalette(p);
    4351
    4452#ifdef HANDHELD
     
    195203
    196204        font = settings->value("Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>();
    197         color = settings->value("Color",DEF_FONT_COLOR).value<QColor>();
     205        color = settings->value("Colors/Text", DEF_TEXT_COLOR).value<QColor>();
    198206        settings->endGroup();
    199207
     
    239247                if (QMessageBox::question(this, tr("Settings Reset"), tr("Do you really want to <b>reset all application settings to their defaults</b>?"), QMessageBox::RestoreDefaults | QMessageBox::Cancel) == QMessageBox::RestoreDefaults) {
    240248                        _newFont = (font != QFont(DEF_FONT_FAMILY, DEF_FONT_SIZE));
    241                         _newColor = (color != DEF_FONT_COLOR);
     249                        _newColor = (color != DEF_TEXT_COLOR);
    242250                        settings->remove("");
    243251                        settings->setValue("SettingsReset", true);
     
    280288                settings->setValue("Font", font);
    281289        if (_newColor)
    282                 settings->setValue("Color", color);
     290                settings->setValue("Colors/Text", color);
    283291        settings->endGroup();
    284292        QDialog::accept();
Note: See TracChangeset for help on using the changeset viewer.