From 769d83d494ab9b7eee90366421f653fa8c6922ca Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Sat, 15 Mar 2014 18:47:43 +0100 Subject: [PATCH] Handle output file suffix in a sensible manner in ImageDrawer --- datamanager.cpp | 2 +- imagedrawer.cpp | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/datamanager.cpp b/datamanager.cpp index e3fd161..81ed7af 100644 --- a/datamanager.cpp +++ b/datamanager.cpp @@ -489,7 +489,7 @@ void DataManager::onExportGraphToImage() relToY = ctrl->valueToRel(dlg.toY()); ImageDrawer imageDrawer(ctrl); - drwRet = imageDrawer.render(dlg.path() + "." + dlg.imageFormat(), dlg.imageFormat().toLatin1(), dlg.imageWidth(), dlg.imageHeight(), + drwRet = imageDrawer.render(dlg.path(), dlg.imageFormat().toLatin1(), dlg.imageWidth(), dlg.imageHeight(), relFromX, relFromY, relToX, relToY, gl); switch (drwRet) { case ImageDrawer::ReturnCode::SUCCESS: diff --git a/imagedrawer.cpp b/imagedrawer.cpp index e7e8125..af82ad6 100644 --- a/imagedrawer.cpp +++ b/imagedrawer.cpp @@ -41,8 +41,10 @@ ImageDrawer::ImageDrawer(std::shared_ptr controller, QObject* ImageDrawer::ReturnCode ImageDrawer::render(const QString& filename, const QByteArray format, const int width, const int height, const double fromX, const double fromY, const double toX, const double toY, const GraphLayers layers) { + QString formatSuffix(format); + QString augFilename; QImage image; - QFile file(filename); + int dotIdx; if (m_controller == nullptr) return ReturnCode::E_NO_CONTROLLER; @@ -52,12 +54,24 @@ ImageDrawer::ReturnCode ImageDrawer::render(const QString& filename, const QByte if (!draw(fromX, fromY, toX, toY, layers)) return ReturnCode::E_CANNOT_DRAW; - m_imageWriter.setFileName(filename); + /* Check the extension */ + dotIdx = filename.lastIndexOf('.'); + if (dotIdx > 0) { + QString realSuffix = filename.mid(dotIdx+1); + if (realSuffix.compare(QString(formatSuffix)) != 0) + augFilename = filename + "." + formatSuffix; + else + augFilename = filename; + } else + augFilename = filename + "." + formatSuffix; + + m_imageWriter.setFileName(augFilename); m_imageWriter.setFormat(format); image = m_pixmap->toImage(); if (image.isNull()) return ReturnCode::E_CANNOT_CREATE_IMAGE; + QFile file(augFilename); if (file.exists()) { int ret = QMessageBox::question(nullptr, "Data export", "The selected file already exists. Do you wish to overwrite it?"); if (ret == QMessageBox::No) -- 2.43.5