Changeset b26dc16dcf in tspsg for src
- Timestamp:
- Sep 10, 2012, 10:40:06 AM (12 years ago)
- Branches:
- appveyor, imgbot, master, readme
- Children:
- f48433245d
- Parents:
- bf92f16303
- git-author:
- Oleksii Serdiuk <contacts@…> (09/10/12 10:40:06)
- git-committer:
- Oleksii Serdiuk <contacts@…> (09/10/12 13:33:32)
- Location:
- src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/globals.h
rbf92f16303 rb26dc16dcf 138 138 } 139 139 140 /*! 141 * \brief Converts \a in into Base64 format with lines wrapped at 64 characters. 142 * \param in A byte array to be converted. 143 * \return Converted byte array. 144 */ 145 inline QByteArray toWrappedBase64(const QByteArray &in) 146 { 147 QByteArray out, base64(in.toBase64()); 148 for (int i = 0; i <= base64.size() - 64; i += 64) { 149 out.append(QByteArray::fromRawData(base64.data() + i, 64)).append('\n'); 150 } 151 if (int rest = base64.size() % 64) 152 out.append(QByteArray::fromRawData(base64.data() + base64.size() - rest, rest)); 153 return out; 154 } 155 140 156 #ifndef HANDHELD 141 157 /*! -
src/mainwindow.cpp
rbf92f16303 rb26dc16dcf 283 283 } 284 284 #endif 285 QByteArray imgdata; 286 bool embed = settings->value("Output/EmbedGraphIntoHTML", DEF_EMBED_GRAPH_INTO_HTML).toBool(); 285 287 if (selectedFile.endsWith(".htm", Qt::CaseInsensitive) || selectedFile.endsWith(".html", Qt::CaseInsensitive)) { 286 QFile file(selectedFile);287 if (!file.open(QFile::WriteOnly )) {288 QFile file(selectedFile); 289 if (!file.open(QFile::WriteOnly | QFile::Text)) { 288 290 QApplication::restoreOverrideCursor(); 289 291 QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution.\nError: %1").arg(file.errorString())); 290 292 return; 291 293 } 292 QFileInfo fi(selectedFile); 293 QString format = settings->value("Output/GraphImageFormat", DEF_GRAPH_IMAGE_FORMAT).toString(); 294 295 QString html = solutionText->document()->toHtml("UTF-8"); 296 html.replace(QRegExp("font-family:([^;]*);"), 297 "font-family:\\1, 'DejaVu Sans Mono', 'Courier New', Courier, monospace;"); 298 html.replace(QRegExp("<style ([^>]*)>"), QString("<style \\1>\n" 299 "body { color: %1 }\n" 300 "td { border-style: solid; border-width: 1px; border-color: %2; }") 301 .arg(settings->value("Output/Colors/Font", DEF_TEXT_COLOR).toString(), 302 settings->value("Output/Colors/TableBorder", DEF_TABLE_COLOR).toString())); 303 304 QFileInfo fi(selectedFile); 305 QString format = settings->value("Output/GraphImageFormat", DEF_GRAPH_IMAGE_FORMAT).toString(); 294 306 #if !defined(NOSVG) 295 307 if (!QImageWriter::supportedImageFormats().contains(format.toAscii()) && (format != "svg")) { … … 300 312 settings->remove("Output/GraphImageFormat"); 301 313 } 302 QString html = solutionText->document()->toHtml("UTF-8");303 304 html.replace(QRegExp("font-family:([^;]*);"), "font-family:\\1, 'DejaVu Sans Mono', 'Courier New', Courier, monospace;");305 html.replace(QRegExp("<style ([^>]*)>"), QString("<style \\1>\n"306 "body { color: %1 }\n"307 "td { border-style: solid; border-width: 1px; border-color: %2; }")308 .arg(settings->value("Output/Colors/Font", DEF_TEXT_COLOR).toString(),309 settings->value("Output/Colors/TableBorder", DEF_TABLE_COLOR).toString()));310 314 311 315 if (!graph.isNull()) { 312 QString img = fi.completeBaseName() + "." + format; 313 bool embed = settings->value("Output/EmbedGraphIntoHTML", DEF_EMBED_GRAPH_INTO_HTML).toBool(); 314 QByteArray data; 315 QBuffer buf(&data); 316 if (!embed) { 317 html.replace(QRegExp("<img\\s+src=\"tspsg://graph.pic\""), QString("<img src=\"%1\" alt=\"%2\"").arg(img, tr("Solution Graph"))); 316 imgdata = generateImage(format); 317 if (imgdata.isEmpty()) { 318 return; 318 319 } 319 320 // Saving solution graph in SVG or supported raster format (depending on settings and SVG support) 321 #if !defined(NOSVG) 322 if (format == "svg") { 323 QSvgGenerator svg; 324 svg.setSize(QSize(graph.width() + 2, graph.height() + 2)); 325 svg.setResolution(graph.logicalDpiX()); 326 svg.setFileName(fi.path() + "/" + img); 327 svg.setTitle(tr("Solution Graph")); 328 svg.setDescription(tr("Generated with %1").arg(QCoreApplication::applicationName())); 329 QPainter p; 330 p.begin(&svg); 331 p.drawPicture(1, 1, graph); 332 p.end(); 320 if (embed) { 321 QString fmt = format; 322 if (format == "svg") 323 fmt.append("+xml"); 324 html.replace(QRegExp("<img\\s+src=\"tspsg://graph.pic\""), 325 QString("<img src=\"data:image/%1;base64,%2\" alt=\"%3\"") 326 .arg(fmt, toWrappedBase64(imgdata), tr("Solution Graph"))); 333 327 } else { 334 #endif // NOSVG 335 QImage i(graph.width() + 2, graph.height() + 2, QImage::Format_ARGB32); 336 i.fill(0x00FFFFFF); 337 QPainter p; 338 p.begin(&i); 339 p.drawPicture(1, 1, graph); 340 p.end(); 341 QImageWriter pic; 342 if (embed) { 343 pic.setDevice(&buf); 344 pic.setFormat(format.toAscii()); 345 } else { 346 pic.setFileName(fi.path() + "/" + img); 347 } 348 if (pic.supportsOption(QImageIOHandler::Description)) { 349 pic.setText("Title", "Solution Graph"); 350 pic.setText("Software", QCoreApplication::applicationName()); 351 } 352 if (format == "png") 353 pic.setQuality(5); 354 else if (format == "jpeg") 355 pic.setQuality(80); 356 if (!pic.write(i)) { 357 QApplication::restoreOverrideCursor(); 358 QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution graph.\nError: %1").arg(pic.errorString())); 359 return; 360 } 361 #if !defined(NOSVG) 328 html.replace(QRegExp("<img\\s+src=\"tspsg://graph.pic\""), 329 QString("<img src=\"%1\" alt=\"%2\"") 330 .arg(fi.completeBaseName() + "." + format, tr("Solution Graph"))); 362 331 } 363 #endif // NOSVG 364 if (embed) { 365 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"))); 366 } 367 } 332 } 333 368 334 // Saving solution text as HTML 369 335 QTextStream ts(&file); 370 336 ts.setCodec(QTextCodec::codecForName("UTF-8")); 371 ts << html ;337 ts << html << endl; 372 338 file.close(); 339 if (!embed) { 340 QFile img(fi.path() + "/" + fi.completeBaseName() + "." + format); 341 if (!img.open(QFile::WriteOnly)) { 342 QApplication::restoreOverrideCursor(); 343 QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution graph.\nError: %1").arg(img.errorString())); 344 return; 345 } 346 if (img.write(imgdata) != imgdata.size()) { 347 QApplication::restoreOverrideCursor(); 348 QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution graph.\nError: %1").arg(img.errorString())); 349 } 350 img.close(); 351 } 373 352 } else { 374 353 QTextDocumentWriter dw(selectedFile); … … 1278 1257 } 1279 1258 1259 QByteArray MainWindow::generateImage(const QString &format) 1260 { 1261 if (graph.isNull()) 1262 return QByteArray(); 1263 1264 QByteArray data; 1265 QBuffer buf(&data); 1266 // Saving solution graph in SVG or supported raster format (depending on settings and SVG support) 1267 #if !defined(NOSVG) 1268 if (format == "svg") { 1269 QSvgGenerator svg; 1270 svg.setSize(QSize(graph.width() + 2, graph.height() + 2)); 1271 svg.setResolution(graph.logicalDpiX()); 1272 svg.setOutputDevice(&buf); 1273 svg.setTitle(tr("Solution Graph")); 1274 svg.setDescription(tr("Generated with %1").arg(QCoreApplication::applicationName())); 1275 QPainter p; 1276 p.begin(&svg); 1277 p.drawPicture(1, 1, graph); 1278 p.end(); 1279 } else { 1280 #endif // NOSVG 1281 QImage i(graph.width() + 2, graph.height() + 2, QImage::Format_ARGB32); 1282 i.fill(0x00FFFFFF); 1283 QPainter p; 1284 p.begin(&i); 1285 p.drawPicture(1, 1, graph); 1286 p.end(); 1287 QImageWriter pic; 1288 pic.setDevice(&buf); 1289 pic.setFormat(format.toAscii()); 1290 if (pic.supportsOption(QImageIOHandler::Description)) { 1291 pic.setText("Title", "Solution Graph"); 1292 pic.setText("Software", QCoreApplication::applicationName()); 1293 } 1294 if (format == "png") 1295 pic.setQuality(5); 1296 else if (format == "jpeg") 1297 pic.setQuality(80); 1298 if (!pic.write(i)) { 1299 QApplication::restoreOverrideCursor(); 1300 QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution graph.\nError: %1").arg(pic.errorString())); 1301 return QByteArray(); 1302 } 1303 #if !defined(NOSVG) 1304 } 1305 #endif // NOSVG 1306 return data; 1307 } 1308 1280 1309 void MainWindow::initDocStyleSheet() 1281 1310 { -
src/mainwindow.h
rbf92f16303 rb26dc16dcf 145 145 void drawNode(QPainter &pic, int nstep, bool left = false, SStep *step = NULL); 146 146 void dropEvent(QDropEvent *ev); 147 QByteArray generateImage(const QString &format); 147 148 void initDocStyleSheet(); 148 149 void loadLangList();
Note: See TracChangeset
for help on using the changeset viewer.