umbrello API Documentation

umlviewimageexporter.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   copyright (C) 2006-2007                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 // own header
00013 #include "umlviewimageexporter.h"
00014 
00015 // include files for Qt
00016 #include <qstring.h>
00017 #include <qstringlist.h>
00018 
00019 //kde include files
00020 #include <klocale.h>
00021 #include <kfiledialog.h>
00022 #include <kurl.h>
00023 #include <kmessagebox.h>
00024 #include <kio/netaccess.h>
00025 
00026 // application specific includes
00027 #include "umlviewimageexportermodel.h"
00028 #include "uml.h"
00029 #include "umldoc.h"
00030 #include "umlview.h"
00031 
00032 
00033 UMLViewImageExporter::UMLViewImageExporter(UMLView* view) {
00034     m_view = view;
00035     m_imageMimeType = UMLApp::app()->getImageMimeType();
00036 }
00037 
00038 void UMLViewImageExporter::exportView() {
00039     if (!prepareExportView()) {
00040         return;
00041     }
00042 
00043     UMLApp *app = UMLApp::app();
00044 
00045     // export the view
00046     app->getDocument()->writeToStatusBar(i18n("Exporting view..."));
00047     QString error = UMLViewImageExporterModel().exportView(m_view,
00048                             UMLViewImageExporterModel::mimeTypeToImageType(m_imageMimeType), m_imageURL);
00049     if (!error.isNull()) {
00050         KMessageBox::error(app, i18n("An error happened when exporting the image:\n") + error);
00051     }
00052     app->getDocument()->writeToStatusBar(i18n("Ready."));
00053 }
00054 
00055 bool UMLViewImageExporter::prepareExportView() {
00056     bool exportPrepared = false;
00057 
00058     do {
00059         if (!getParametersFromUser()) {
00060             return false;
00061         }
00062 
00063         // check if the file exists
00064         if (KIO::NetAccess::exists(m_imageURL, true, UMLApp::app())) {
00065             int wantSave = KMessageBox::warningContinueCancel(0,
00066                                 i18n("The selected file %1 exists.\nDo you want to overwrite it?").arg(m_imageURL.prettyURL()),
00067                                 i18n("File Already Exists"), i18n("&Overwrite"));
00068             if (wantSave == KMessageBox::Continue) {
00069                 exportPrepared = true;
00070             }
00071         } else {
00072             exportPrepared = true;
00073         }
00074     } while (!exportPrepared);
00075 
00076     return true;
00077 }
00078 
00079 bool UMLViewImageExporter::getParametersFromUser() {
00080     UMLApp *app = UMLApp::app();
00081 
00082     // configure & show the file dialog
00083     KFileDialog fileDialog(QString::null, QString::null, m_view,
00084                            ":export-image", true);
00085     prepareFileDialog(fileDialog);
00086     fileDialog.exec();
00087 
00088     if (fileDialog.selectedURL().isEmpty())
00089         return false;
00090     m_view->clearSelected();   // Thanks to Peter Soetens for the idea
00091 
00092     // update image url and mime type
00093     m_imageMimeType = fileDialog.currentMimeFilter();
00094     app->setImageMimeType(m_imageMimeType);
00095     m_imageURL = fileDialog.selectedURL();
00096 
00097     // check if the extension is the extension of the mime type
00098     QFileInfo info(m_imageURL.filename());
00099     QString ext = info.extension(false);
00100     QString extDef = UMLViewImageExporterModel::mimeTypeToImageType(m_imageMimeType);
00101     if(ext != extDef) {
00102         m_imageURL.setFileName(m_imageURL.fileName() + '.' + extDef);
00103     }
00104 
00105     return true;
00106 }
00107 
00108 void UMLViewImageExporter::prepareFileDialog(KFileDialog &fileDialog) {
00109     // get all supported mime types
00110     QStringList mimeTypes = UMLViewImageExporterModel::supportedMimeTypes();
00111 
00112     fileDialog.setCaption(i18n("Save As"));
00113     fileDialog.setOperationMode(KFileDialog::Saving);
00114     fileDialog.setMimeFilter(mimeTypes, m_imageMimeType);
00115 
00116     // set a sensible default filename
00117     if (m_imageURL.isEmpty()) {
00118         KURL docURL = UMLApp::app()->getDocument()->URL();
00119         KURL directory = docURL;
00120         directory.setPath(docURL.directory());
00121 
00122         fileDialog.setURL(directory);
00123         fileDialog.setSelection(m_view->getName() + '.' + UMLViewImageExporterModel::mimeTypeToImageType(m_imageMimeType));
00124     } else {
00125         fileDialog.setURL(m_imageURL);
00126         fileDialog.setSelection(m_imageURL.fileName());
00127     }
00128 }
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:08:02 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003