| 1245 | } |
| 1246 | |
| 1247 | void PrintWidget::exportToImage() |
| 1248 | { |
| 1249 | static const QString filter_template(QString::fromLatin1("%1 (%2)")); |
| 1250 | QStringList filters = { filter_template.arg(tr("PNG"), QString::fromLatin1("*.png")), |
| 1251 | filter_template.arg(tr("BMP"), QString::fromLatin1("*.bmp")), |
| 1252 | filter_template.arg(tr("TIFF"), QString::fromLatin1("*.tif *.tiff")), |
| 1253 | filter_template.arg(tr("JPEG"), QString::fromLatin1("*.jpg *.jpeg")), |
| 1254 | tr("All files (*.*)") }; |
| 1255 | QString path = FileDialog::getSaveFileName(this, tr("Export map ..."), {}, filters.join(QString::fromLatin1(";;"))); |
| 1256 | if (path.isEmpty()) |
| 1257 | return; |
| 1258 | |
| 1259 | if (!path.endsWith(QLatin1String(".png"), Qt::CaseInsensitive) |
| 1260 | && !path.endsWith(QLatin1String(".bmp"), Qt::CaseInsensitive) |
| 1261 | && !path.endsWith(QLatin1String(".tif"), Qt::CaseInsensitive) && !path.endsWith(QLatin1String(".tiff"), Qt::CaseInsensitive) |
| 1262 | && !path.endsWith(QLatin1String(".jpg"), Qt::CaseInsensitive) && !path.endsWith(QLatin1String(".jpeg"), Qt::CaseInsensitive) ) |
| 1263 | { |
| 1264 | path.append(QString::fromLatin1(".png")); |
| 1265 | } |
| 1266 | |
| 1267 | qreal pixel_per_mm = map_printer->getOptions().resolution / 25.4; |
| 1268 | int print_width = qRound(map_printer->getPrintAreaPaperSize().width() * pixel_per_mm); |
| 1269 | int print_height = qRound(map_printer->getPrintAreaPaperSize().height() * pixel_per_mm); |
| 1270 | QImage image(print_width, print_height, QImage::Format_ARGB32_Premultiplied); |
| 1271 | if (image.isNull()) |
| 1272 | { |
| 1273 | QMessageBox::warning(this, tr("Error"), tr("Failed to prepare the image. Not enough memory.")); |
| 1274 | return; |
| 1275 | } |
| 1276 | |
| 1277 | int dots_per_meter = qRound(pixel_per_mm * 1000); |
| 1278 | image.setDotsPerMeterX(dots_per_meter); |
| 1279 | image.setDotsPerMeterY(dots_per_meter); |
| 1280 | |
| 1281 | image.fill(QColor(Qt::white)); |
| 1282 | |
| 1283 | #if 0 // Pointless unless drawPage drives the event loop and sends progress |
| 1284 | PrintProgressDialog progress(map_printer, main_window); |
| 1285 | progress.setWindowTitle(tr("Export map ...")); |
| 1286 | #endif |
| 1287 | |
| 1288 | // Export the map |
| 1289 | QPainter p(&image); |
| 1290 | map_printer->drawPage(&p, map_printer->getPrintArea(), &image); |
| 1291 | p.end(); |
| 1292 | if (!image.save(path)) |
| 1293 | { |
| 1294 | QMessageBox::warning(this, tr("Error"), tr("Failed to save the image. Does the path exist? Do you have sufficient rights?")); |
| 1295 | } |
| 1296 | else |
| 1297 | { |
| 1298 | main_window->showStatusBarMessage(tr("Exported successfully to %1").arg(path), 4000); |
| 1299 | if (world_file_check->isChecked()) |
| 1300 | exportWorldFile(path); /// \todo Handle errors |
| 1301 | emit finished(0); |
| 1302 | } |
| 1303 | } |
| 1304 |
nothing calls this directly
no test coverage detected