saveasimagedialog.cpp
Upload User: chipsz
Upload Date: 2021-05-21
Package Size: 830k
Code Size: 5k
Category:

GIS program

Development Platform:

QT

  1. /*
  2.  * Tiled Map Editor (Qt)
  3.  * Copyright 2009 Tiled (Qt) developers (see AUTHORS file)
  4.  *
  5.  * This file is part of Tiled (Qt).
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify it
  8.  * under the terms of the GNU General Public License as published by the Free
  9.  * Software Foundation; either version 2 of the License, or (at your option)
  10.  * any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful, but WITHOUT
  13.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  15.  * more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License along with
  18.  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  19.  * Place, Suite 330, Boston, MA 02111-1307, USA.
  20.  */
  21. #include "saveasimagedialog.h"
  22. #include "ui_saveasimagedialog.h"
  23. #include "map.h"
  24. #include "mapdocument.h"
  25. #include "maprenderer.h"
  26. #include "objectgroup.h"
  27. #include "tilelayer.h"
  28. #include "utils.h"
  29. #include <QFileDialog>
  30. #include <QMessageBox>
  31. #include <QImageWriter>
  32. using namespace Tiled;
  33. using namespace Tiled::Internal;
  34. QString SaveAsImageDialog::mPath;
  35. SaveAsImageDialog::SaveAsImageDialog(MapDocument *mapDocument,
  36.                                      const QString &fileName,
  37.                                      qreal currentScale,
  38.                                      QWidget *parent)
  39.     : QDialog(parent)
  40.     , mUi(new Ui::SaveAsImageDialog)
  41.     , mMapDocument(mapDocument)
  42.     , mCurrentScale(currentScale)
  43. {
  44.     mUi->setupUi(this);
  45.     // Default to the last chosen location
  46.     QString suggestion = mPath;
  47.     // Suggest a nice name for the image
  48.     if (!fileName.isEmpty()) {
  49.         QFileInfo fileInfo(fileName);
  50.         const QString path = fileInfo.path();
  51.         const QString baseName = fileInfo.completeBaseName();
  52.         if (suggestion.isEmpty())
  53.             suggestion = path;
  54.         suggestion += QLatin1Char('/');
  55.         suggestion += baseName;
  56.         suggestion += QLatin1String(".png");
  57.     } else {
  58.         suggestion += QLatin1Char('/');
  59.         suggestion += QLatin1String("map.png");
  60.     }
  61.     mUi->fileNameEdit->setText(suggestion);
  62.     connect(mUi->browseButton, SIGNAL(clicked()), SLOT(browse()));
  63.     connect(mUi->fileNameEdit, SIGNAL(textChanged(QString)),
  64.             this, SLOT(updateAcceptEnabled()));
  65. }
  66. SaveAsImageDialog::~SaveAsImageDialog()
  67. {
  68.     delete mUi;
  69. }
  70. void SaveAsImageDialog::accept()
  71. {
  72.     const QString fileName = mUi->fileNameEdit->text();
  73.     if (fileName.isEmpty())
  74.         return;
  75.     if (QFile::exists(fileName)) {
  76.         const QMessageBox::StandardButton button =
  77.                 QMessageBox::warning(this,
  78.                                      tr("Save as Image"),
  79.                                      tr("%1 already exists.n"
  80.                                         "Do you want to replace it?")
  81.                                      .arg(QFileInfo(fileName).fileName()),
  82.                                      QMessageBox::Yes | QMessageBox::No,
  83.                                      QMessageBox::No);
  84.         if (button != QMessageBox::Yes)
  85.             return;
  86.     }
  87.     const bool visibleLayersOnly = mUi->visibleLayersOnly->isChecked();
  88.     const bool useCurrentScale = mUi->currentZoomLevel->isChecked();
  89.     MapRenderer *renderer = mMapDocument->renderer();
  90.     QSize mapSize = renderer->mapSize();
  91.     if (useCurrentScale)
  92.         mapSize *= mCurrentScale;
  93.     QImage image(mapSize, QImage::Format_ARGB32);
  94.     image.fill(Qt::transparent);
  95.     QPainter painter(&image);
  96.     if (useCurrentScale && mCurrentScale != qreal(1)) {
  97.         painter.setRenderHints(QPainter::SmoothPixmapTransform |
  98.                                QPainter::HighQualityAntialiasing);
  99.         painter.setTransform(QTransform::fromScale(mCurrentScale,
  100.                                                    mCurrentScale));
  101.     }
  102.     foreach (const Layer *layer, mMapDocument->map()->layers()) {
  103.         if (visibleLayersOnly && !layer->isVisible())
  104.             continue;
  105.         const TileLayer *tileLayer = dynamic_cast<const TileLayer*>(layer);
  106.         const ObjectGroup *objGroup = dynamic_cast<const ObjectGroup*>(layer);
  107.         if (tileLayer) {
  108.             renderer->drawTileLayer(&painter, tileLayer);
  109.         } else if (objGroup) {
  110.             QColor color = objGroup->color();
  111.             if (!color.isValid())
  112.                 color = Qt::gray;
  113.             // TODO: Support colors for different object types
  114.             foreach (const MapObject *object, objGroup->objects())
  115.                 renderer->drawMapObject(&painter, object, color);
  116.         }
  117.     }
  118.     image.save(fileName);
  119.     mPath = QFileInfo(fileName).path();
  120.     QDialog::accept();
  121. }
  122. void SaveAsImageDialog::browse()
  123. {
  124.     // Don't confirm overwrite here, since we'll confirm when the user presses
  125.     // the Save button
  126.     const QString filter = Utils::writableImageFormatsFilter();
  127.     QString f = QFileDialog::getSaveFileName(this, tr("Image"),
  128.                                              mUi->fileNameEdit->text(),
  129.                                              filter, 0,
  130.                                              QFileDialog::DontConfirmOverwrite);
  131.     if (!f.isEmpty()) {
  132.         mUi->fileNameEdit->setText(f);
  133.         mPath = f;
  134.     }
  135. }
  136. void SaveAsImageDialog::updateAcceptEnabled()
  137. {
  138.     QPushButton *saveButton = mUi->buttonBox->button(QDialogButtonBox::Save);
  139.     saveButton->setEnabled(!mUi->fileNameEdit->text().isEmpty());
  140. }