Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
saveasimagedialog.cpp
Package: tiled-qt-0.4.0.zip [view]
Upload User: chipsz
Upload Date: 2021-05-21
Package Size: 830k
Code Size: 5k
Category:
GIS program
Development Platform:
QT
- /*
- * Tiled Map Editor (Qt)
- * Copyright 2009 Tiled (Qt) developers (see AUTHORS file)
- *
- * This file is part of Tiled (Qt).
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307, USA.
- */
- #include "saveasimagedialog.h"
- #include "ui_saveasimagedialog.h"
- #include "map.h"
- #include "mapdocument.h"
- #include "maprenderer.h"
- #include "objectgroup.h"
- #include "tilelayer.h"
- #include "utils.h"
- #include <QFileDialog>
- #include <QMessageBox>
- #include <QImageWriter>
- using namespace Tiled;
- using namespace Tiled::Internal;
- QString SaveAsImageDialog::mPath;
- SaveAsImageDialog::SaveAsImageDialog(MapDocument *mapDocument,
- const QString &fileName,
- qreal currentScale,
- QWidget *parent)
- : QDialog(parent)
- , mUi(new Ui::SaveAsImageDialog)
- , mMapDocument(mapDocument)
- , mCurrentScale(currentScale)
- {
- mUi->setupUi(this);
- // Default to the last chosen location
- QString suggestion = mPath;
- // Suggest a nice name for the image
- if (!fileName.isEmpty()) {
- QFileInfo fileInfo(fileName);
- const QString path = fileInfo.path();
- const QString baseName = fileInfo.completeBaseName();
- if (suggestion.isEmpty())
- suggestion = path;
- suggestion += QLatin1Char('/');
- suggestion += baseName;
- suggestion += QLatin1String(".png");
- } else {
- suggestion += QLatin1Char('/');
- suggestion += QLatin1String("map.png");
- }
- mUi->fileNameEdit->setText(suggestion);
- connect(mUi->browseButton, SIGNAL(clicked()), SLOT(browse()));
- connect(mUi->fileNameEdit, SIGNAL(textChanged(QString)),
- this, SLOT(updateAcceptEnabled()));
- }
- SaveAsImageDialog::~SaveAsImageDialog()
- {
- delete mUi;
- }
- void SaveAsImageDialog::accept()
- {
- const QString fileName = mUi->fileNameEdit->text();
- if (fileName.isEmpty())
- return;
- if (QFile::exists(fileName)) {
- const QMessageBox::StandardButton button =
- QMessageBox::warning(this,
- tr("Save as Image"),
- tr("%1 already exists.n"
- "Do you want to replace it?")
- .arg(QFileInfo(fileName).fileName()),
- QMessageBox::Yes | QMessageBox::No,
- QMessageBox::No);
- if (button != QMessageBox::Yes)
- return;
- }
- const bool visibleLayersOnly = mUi->visibleLayersOnly->isChecked();
- const bool useCurrentScale = mUi->currentZoomLevel->isChecked();
- MapRenderer *renderer = mMapDocument->renderer();
- QSize mapSize = renderer->mapSize();
- if (useCurrentScale)
- mapSize *= mCurrentScale;
- QImage image(mapSize, QImage::Format_ARGB32);
- image.fill(Qt::transparent);
- QPainter painter(&image);
- if (useCurrentScale && mCurrentScale != qreal(1)) {
- painter.setRenderHints(QPainter::SmoothPixmapTransform |
- QPainter::HighQualityAntialiasing);
- painter.setTransform(QTransform::fromScale(mCurrentScale,
- mCurrentScale));
- }
- foreach (const Layer *layer, mMapDocument->map()->layers()) {
- if (visibleLayersOnly && !layer->isVisible())
- continue;
- const TileLayer *tileLayer = dynamic_cast<const TileLayer*>(layer);
- const ObjectGroup *objGroup = dynamic_cast<const ObjectGroup*>(layer);
- if (tileLayer) {
- renderer->drawTileLayer(&painter, tileLayer);
- } else if (objGroup) {
- QColor color = objGroup->color();
- if (!color.isValid())
- color = Qt::gray;
- // TODO: Support colors for different object types
- foreach (const MapObject *object, objGroup->objects())
- renderer->drawMapObject(&painter, object, color);
- }
- }
- image.save(fileName);
- mPath = QFileInfo(fileName).path();
- QDialog::accept();
- }
- void SaveAsImageDialog::browse()
- {
- // Don't confirm overwrite here, since we'll confirm when the user presses
- // the Save button
- const QString filter = Utils::writableImageFormatsFilter();
- QString f = QFileDialog::getSaveFileName(this, tr("Image"),
- mUi->fileNameEdit->text(),
- filter, 0,
- QFileDialog::DontConfirmOverwrite);
- if (!f.isEmpty()) {
- mUi->fileNameEdit->setText(f);
- mPath = f;
- }
- }
- void SaveAsImageDialog::updateAcceptEnabled()
- {
- QPushButton *saveButton = mUi->buttonBox->button(QDialogButtonBox::Save);
- saveButton->setEnabled(!mUi->fileNameEdit->text().isEmpty());
- }