Changeset 162 in tspsg-svn for trunk/src/mainwindow.cpp
- Timestamp:
- Apr 14, 2011, 9:30:23 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/mainwindow.cpp
r161 r162 180 180 tabWidget->setCurrentIndex(0); 181 181 solutionText->clear(); 182 graph = QPicture(); 182 183 toggleSolutionActions(false); 183 184 QApplication::restoreOverrideCursor(); … … 274 275 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 275 276 #ifndef QT_NO_PRINTER 276 if (selectedFile.endsWith(".pdf",Qt::CaseInsensitive)) { 277 QPrinter printer(QPrinter::HighResolution); 278 printer.setOutputFormat(QPrinter::PdfFormat); 279 printer.setOutputFileName(selectedFile); 280 solutionText->document()->print(&printer); 277 if (selectedFile.endsWith(".pdf", Qt::CaseInsensitive)) { 278 printer->setOutputFileName(selectedFile); 279 solutionText->document()->print(printer); 280 printer->setOutputFileName(QString()); 281 281 QApplication::restoreOverrideCursor(); 282 282 return; … … 293 293 QString format = settings->value("Output/GraphImageFormat", DEF_GRAPH_IMAGE_FORMAT).toString(); 294 294 #if !defined(NOSVG) 295 if (!QImageWriter::supportedImageFormats().contains(format.toAscii()) && (format != "svg")) {295 if (!QImageWriter::supportedImageFormats().contains(format.toAscii()) && (format != "svg")) { 296 296 #else // NOSVG 297 if (!QImageWriter::supportedImageFormats().contains(format.toAscii())) {297 if (!QImageWriter::supportedImageFormats().contains(format.toAscii())) { 298 298 #endif // NOSVG 299 format = DEF_GRAPH_IMAGE_FORMAT;300 settings->remove("Output/GraphImageFormat");301 }302 QString html = solutionText->document()->toHtml("UTF-8") ,303 img = fi.completeBaseName() + "." + format; 299 format = DEF_GRAPH_IMAGE_FORMAT; 300 settings->remove("Output/GraphImageFormat"); 301 } 302 QString html = solutionText->document()->toHtml("UTF-8"); 303 304 304 html.replace(QRegExp("font-family:([^;]*);"), "font-family:\\1, 'DejaVu Sans Mono', 'Courier New', Courier, monospace;"); 305 html.replace(QRegExp("<img\\s+src=\"tspsg://graph.pic\""), QString("<img src=\"%1\" alt=\"%2\"").arg(img, tr("Solution Graph"))); 306 305 306 if (!graph.isNull()) { 307 QString img = fi.completeBaseName() + "." + format; 308 bool embed = settings->value("Output/EmbedGraphIntoHTML", DEF_EMBED_GRAPH_INTO_HTML).toBool(); 309 QByteArray data; 310 QBuffer buf(&data); 311 if (!embed) { 312 html.replace(QRegExp("<img\\s+src=\"tspsg://graph.pic\""), QString("<img src=\"%1\" alt=\"%2\"").arg(img, tr("Solution Graph"))); 313 } 314 315 // Saving solution graph in SVG or supported raster format (depending on settings and SVG support) 316 #if !defined(NOSVG) 317 if (format == "svg") { 318 QSvgGenerator svg; 319 svg.setSize(QSize(graph.width() + 2, graph.height() + 2)); 320 svg.setResolution(graph.logicalDpiX()); 321 svg.setFileName(fi.path() + "/" + img); 322 svg.setTitle(tr("Solution Graph")); 323 svg.setDescription(tr("Generated with %1").arg(QCoreApplication::applicationName())); 324 QPainter p; 325 p.begin(&svg); 326 p.drawPicture(1, 1, graph); 327 p.end(); 328 } else { 329 #endif // NOSVG 330 QImage i(graph.width() + 2, graph.height() + 2, QImage::Format_ARGB32); 331 i.fill(0x00FFFFFF); 332 QPainter p; 333 p.begin(&i); 334 p.drawPicture(1, 1, graph); 335 p.end(); 336 QImageWriter pic; 337 if (embed) { 338 pic.setDevice(&buf); 339 pic.setFormat(format.toAscii()); 340 } else { 341 pic.setFileName(fi.path() + "/" + img); 342 } 343 if (pic.supportsOption(QImageIOHandler::Description)) { 344 pic.setText("Title", "Solution Graph"); 345 pic.setText("Software", QCoreApplication::applicationName()); 346 } 347 if (format == "png") 348 pic.setQuality(5); 349 else if (format == "jpeg") 350 pic.setQuality(80); 351 if (!pic.write(i)) { 352 QApplication::restoreOverrideCursor(); 353 QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution graph.\nError: %1").arg(pic.errorString())); 354 return; 355 } 356 #if !defined(NOSVG) 357 } 358 #endif // NOSVG 359 if (embed) { 360 html.replace(QRegExp("<img\\s+src=\"tspsg://graph.pic\""), QString("<img src=\"data:image/%1;base64,%2\" alt=\"%3\"").arg(format, data.toBase64(), tr("Solution Graph"))); 361 } 362 } 307 363 // Saving solution text as HTML 308 364 QTextStream ts(&file); … … 310 366 ts << html; 311 367 file.close(); 312 313 // Saving solution graph as SVG or PNG (depending on settings and SVG support)314 #if !defined(NOSVG)315 if (format == "svg") {316 QSvgGenerator svg;317 svg.setSize(QSize(graph.width() + 2, graph.height() + 2));318 svg.setResolution(graph.logicalDpiX());319 svg.setFileName(fi.path() + "/" + img);320 svg.setTitle(tr("Solution Graph"));321 svg.setDescription(tr("Generated with %1").arg(QCoreApplication::applicationName()));322 QPainter p;323 p.begin(&svg);324 p.drawPicture(1, 1, graph);325 p.end();326 } else {327 #endif // NOSVG328 QImage i(graph.width() + 2, graph.height() + 2, QImage::Format_ARGB32);329 i.fill(0x00FFFFFF);330 QPainter p;331 p.begin(&i);332 p.drawPicture(1, 1, graph);333 p.end();334 QImageWriter pic(fi.path() + "/" + img);335 if (pic.supportsOption(QImageIOHandler::Description)) {336 pic.setText("Title", "Solution Graph");337 pic.setText("Software", QCoreApplication::applicationName());338 }339 if (format == "png")340 pic.setQuality(5);341 else if (format == "jpeg")342 pic.setQuality(80);343 if (!pic.write(i)) {344 QApplication::restoreOverrideCursor();345 QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution graph.\nError: %1").arg(pic.errorString()));346 return;347 }348 #if !defined(NOSVG)349 }350 #endif // NOSVG351 368 } else { 352 369 QTextDocumentWriter dw(selectedFile); … … 743 760 tl = NULL; 744 761 } else { 745 // tl->SetProgressState(winId(), TBPF_INDETERMINATE);746 762 tl->SetProgressValue(winId(), 0, n * 2); 747 763 } … … 769 785 #ifdef Q_WS_WIN32 770 786 if (tl != NULL) { 771 // tl->SetProgressValue(winId(), n, n * 2);772 787 tl->SetProgressState(winId(), TBPF_ERROR); 773 788 } … … 818 833 819 834 QPainter pic; 820 if (settings->value("Output/ShowGraph", DEF_SHOW_GRAPH).toBool()) { 835 bool dograph = settings->value("Output/GenerateGraph", DEF_GENERATE_GRAPH).toBool(); 836 if (dograph) { 821 837 pic.begin(&graph); 822 838 pic.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); 823 839 QFont font = qvariant_cast<QFont>(settings->value("Output/Font", QFont(DEF_FONT_FACE))); 824 //! \todo FIXME: Get rid of the "magic number" in the code. 825 font.setPixelSize(logicalDpiX() / 7); 840 font.setStyleHint(QFont::Monospace); 841 // Font size in pixels = graph node radius / 2.75. 842 // See MainWindow::drawNode() for graph node radius calcualtion description. 843 font.setPixelSize(logicalDpiX() * (settings->value("Output/GraphWidth", DEF_GRAPH_WIDTH).toReal() / CM_IN_INCH) / 4.5 / 2.75); 826 844 if (settings->value("Output/HQGraph", DEF_HQ_GRAPH).toBool()) { 827 845 font.setWeight(QFont::DemiBold); 828 font.setPixelSize(font.pixelSize() * 2);846 font.setPixelSize(font.pixelSize() * HQ_FACTOR); 829 847 } 830 848 pic.setFont(font); … … 832 850 if (settings->value("Output/HQGraph", DEF_HQ_GRAPH).toBool()) { 833 851 QPen pen = pic.pen(); 834 pen.setWidth( 2);852 pen.setWidth(HQ_FACTOR); 835 853 pic.setPen(pen); 836 854 } 837 855 pic.setBackgroundMode(Qt::OpaqueMode); 856 } else { 857 graph = QPicture(); 838 858 } 839 859 … … 847 867 cur.insertText(tr("Task:"), fmt_default); 848 868 outputMatrix(cur, matrix); 849 if ( settings->value("Output/ShowGraph", DEF_SHOW_GRAPH).toBool()) {869 if (dograph) { 850 870 #ifdef _T_T_L_ 851 871 _b_ _i_ _z_ _a_ _r_ _r_ _e_ … … 904 924 outputMatrix(cur, *step); 905 925 } 906 cur.insertBlock(fmt_paragraph); 926 if (step->alts.empty()) 927 cur.insertBlock(fmt_lastparagraph); 928 else 929 cur.insertBlock(fmt_paragraph); 907 930 cur.insertText(tr("Selected route %1 %2 part.").arg((step->next == SStep::RightBranch) ? tr("with") : tr("without")).arg(tr("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)), fmt_default); 908 931 if (!step->alts.empty()) { … … 914 937 alts += tr("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1); 915 938 } 916 cur.insertBlock(fmt_ paragraph);939 cur.insertBlock(fmt_lastparagraph); 917 940 cur.insertText(tr("%n alternate candidate(s) for branching: %1.", "", step->alts.count()).arg(alts), fmt_altlist); 918 941 } 919 cur.insertBlock(fmt_paragraph);920 cur.insertText(" ", fmt_default);921 942 cur.endEditBlock(); 922 943 923 if ( settings->value("Output/ShowGraph", DEF_SHOW_GRAPH).toBool()) {944 if (dograph) { 924 945 if (step->prNode != NULL) 925 946 drawNode(pic, n, false, step->prNode); … … 947 968 cur.insertBlock(fmt_paragraph); 948 969 if (solver.isOptimal()) 949 cur.insertText(tr("Optimal path:") );970 cur.insertText(tr("Optimal path:"), fmt_default); 950 971 else 951 cur.insertText(tr("Resulting path:") );972 cur.insertText(tr("Resulting path:"), fmt_default); 952 973 953 974 cur.insertBlock(fmt_paragraph); 954 975 cur.insertText(" " + solver.getSortedPath(tr("City %1"))); 955 976 956 cur.insertBlock(fmt_paragraph); 977 if (solver.isOptimal()) 978 cur.insertBlock(fmt_paragraph); 979 else 980 cur.insertBlock(fmt_lastparagraph); 957 981 if (isInteger(step->price)) 958 982 cur.insertHtml("<p>" + tr("The price is <b>%n</b> unit(s).", "", qRound(step->price)) + "</p>"); … … 961 985 if (!solver.isOptimal()) { 962 986 cur.insertBlock(fmt_paragraph); 963 cur.insertText(" ");964 cur.insertBlock(fmt_paragraph);965 987 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>"); 966 988 } 967 989 cur.endEditBlock(); 968 990 969 if ( settings->value("Output/ShowGraph", DEF_SHOW_GRAPH).toBool()) {991 if (dograph) { 970 992 pic.end(); 971 993 … … 980 1002 img.setName("tspsg://graph.pic"); 981 1003 if (settings->value("Output/HQGraph", DEF_HQ_GRAPH).toBool()) { 982 img.setWidth(i.width() / 2);983 img.setHeight(i.height() / 2);1004 img.setWidth(i.width() / HQ_FACTOR); 1005 img.setHeight(i.height() / HQ_FACTOR); 984 1006 } else { 985 1007 img.setWidth(i.width()); … … 1167 1189 void MainWindow::drawNode(QPainter &pic, int nstep, bool left, SStep *step) 1168 1190 { 1169 qreal r; 1170 //! \todo FIXME: Get rid of the "magic numbers" in the code. 1191 qreal r; // Radius of graph node 1192 // We calculate r from the full graph width in centimeters: 1193 // r = width in pixels / 4.5. 1194 // width in pixels = DPI * width in inches. 1195 // width in inches = width in cm / cm in inch. 1196 r = logicalDpiX() * (settings->value("Output/GraphWidth", DEF_GRAPH_WIDTH).toReal() / CM_IN_INCH) / 4.5; 1171 1197 if (settings->value("Output/HQGraph", DEF_HQ_GRAPH).toBool()) 1172 r = logicalDpiX() / 1.27; 1173 else 1174 r = logicalDpiX() / 2.54; 1198 r *= HQ_FACTOR; 1175 1199 #ifdef Q_WS_S60 1176 1200 /*! \hack HACK: Solution graph on Symbian is visually larger than on … … 1247 1271 fmt_paragraph.setBottomMargin(0); 1248 1272 fmt_paragraph.setLeftMargin(10); 1273 1274 fmt_lastparagraph.setTopMargin(5); 1275 fmt_lastparagraph.setRightMargin(10); 1276 fmt_lastparagraph.setBottomMargin(15); 1277 fmt_lastparagraph.setLeftMargin(10); 1249 1278 1250 1279 fmt_table.setTopMargin(5);
Note: See TracChangeset
for help on using the changeset viewer.