"Fossies" - the Fresh Open Source Software Archive

Member "src/controller.cpp" (28 Sep 2008, 19347 Bytes) of package /windows/misc/PosteRazor-1.9.5-Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "controller.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2     PosteRazor - Make your own poster!
    3     Copyright (C) 2005-2008 by Alessandro Portale
    4     http://posterazor.sourceforge.net/
    5 
    6     This file is part of PosteRazor
    7 
    8     PosteRazor is free software; you can redistribute it and/or modify
    9     it under the terms of the GNU General Public License as published by
   10     the Free Software Foundation, either version 3 of the License, or
   11     (at your option) any later version.
   12 
   13     PosteRazor is distributed in the hope that it will be useful,
   14     but WITHOUT ANY WARRANTY; without even the implied warranty of
   15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16     GNU General Public License for more details.
   17 
   18     You should have received a copy of the GNU General Public License
   19     along with PosteRazor; if not, write to the Free Software
   20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
   21 */
   22 
   23 #include "controller.h"
   24 #include "mainwindow.h"
   25 #include "wizardcontroller.h"
   26 #include <QSettings>
   27 #include <QFileDialog>
   28 #include <QMessageBox>
   29 #include <QUrl>
   30 #include <QDesktopServices>
   31 #include <QTranslator>
   32 
   33 const QLatin1String settingsKey_LaunchPDFApplication("LaunchPDFApplication");
   34 const QLatin1String settingsKey_TranslationName("TranslationName");
   35 const QLatin1String settingsKey_ImageLoadPath("ImageLoadPath");
   36 const QLatin1String settingsKey_PosterSavePath("PosterSavePath");
   37 
   38 Controller::Controller(PosteRazorCore *posteRazorCore, MainWindow *mainWindow, QObject *parent)
   39     : QObject(parent)
   40     , m_posteRazorCore(posteRazorCore)
   41     , m_mainWindow(mainWindow)
   42     , m_launchPDFApplication(true)
   43 {
   44     connect(m_mainWindow, SIGNAL(paperFormatChanged(const QString&)), SLOT(setPaperFormat(const QString&)));
   45     connect(m_mainWindow, SIGNAL(paperOrientationChanged(QPrinter::Orientation)), SLOT(setPaperOrientation(QPrinter::Orientation)));
   46     connect(m_mainWindow, SIGNAL(paperBorderTopChanged(double)), SLOT(setPaperBorderTop(double)));
   47     connect(m_mainWindow, SIGNAL(paperBorderRightChanged(double)), SLOT(setPaperBorderRight(double)));
   48     connect(m_mainWindow, SIGNAL(paperBorderBottomChanged(double)), SLOT(setPaperBorderBottom(double)));
   49     connect(m_mainWindow, SIGNAL(paperBorderLeftChanged(double)), SLOT(setPaperBorderLeft(double)));
   50     connect(m_mainWindow, SIGNAL(useCustomPaperSizeChanged(bool)), SLOT(setUseCustomPaperSize(bool)));
   51     connect(m_mainWindow, SIGNAL(paperCustomWidthChanged(double)), SLOT(setCustomPaperWidth(double)));
   52     connect(m_mainWindow, SIGNAL(paperCustomHeightChanged(double)), SLOT(setCustomPaperHeight(double)));
   53     connect(m_mainWindow, SIGNAL(overlappingWidthChanged(double)), SLOT(setOverlappingWidth(double)));
   54     connect(m_mainWindow, SIGNAL(overlappingHeightChanged(double)), SLOT(setOverlappingHeight(double)));
   55     connect(m_mainWindow, SIGNAL(overlappingPositionChanged(Qt::Alignment)), SLOT(setOverlappingPosition(Qt::Alignment)));
   56     connect(m_mainWindow, SIGNAL(posterWidthAbsoluteChanged(double)), SLOT(setPosterWidthAbsolute(double)));
   57     connect(m_mainWindow, SIGNAL(posterHeightAbsoluteChanged(double)), SLOT(setPosterHeightAbsolute(double)));
   58     connect(m_mainWindow, SIGNAL(posterWidthPagesChanged(double)), SLOT(setPosterWidthPages(double)));
   59     connect(m_mainWindow, SIGNAL(posterHeightPagesChanged(double)), SLOT(setPosterHeightPages(double)));
   60     connect(m_mainWindow, SIGNAL(posterSizePercentualChanged(double)), SLOT(setPosterSizePercentual(double)));
   61     connect(m_mainWindow, SIGNAL(posterAlignmentChanged(Qt::Alignment)), SLOT(setPosterAlignment(Qt::Alignment)));
   62     connect(m_mainWindow, SIGNAL(savePosterSignal()), SLOT(savePoster()));
   63     connect(m_mainWindow, SIGNAL(launchPDFApplicationChanged(bool)), SLOT(setLaunchPDFApplication(bool)));
   64     connect(m_mainWindow, SIGNAL(loadImageSignal()), SLOT(loadInputImage()));
   65     connect(m_mainWindow, SIGNAL(translationChanged(const QString&)), SLOT(loadTranslation(const QString&)));
   66     connect(m_mainWindow, SIGNAL(unitOfLengthChanged(const QString&)), SLOT(setUnitOfLength(const QString&)));
   67     connect(m_mainWindow, SIGNAL(openPosteRazorWebsiteSignal()), SLOT(openPosteRazorWebsite()));
   68     connect(m_mainWindow, SIGNAL(needsPaint(PaintCanvasInterface*, const QVariant&)), m_posteRazorCore, SLOT(paintOnCanvas(PaintCanvasInterface*, const QVariant&)));
   69     connect(m_posteRazorCore, SIGNAL(previewImageChanged(const QImage&)), m_mainWindow, SLOT(setPreviewImage(const QImage&)));
   70 
   71     m_wizardController = new WizardController(m_mainWindow, this);
   72 
   73     if (!m_posteRazorCore->imageIOLibraryAboutText().isEmpty()) {
   74         QAction *aboutAction = new QAction(m_mainWindow);
   75         aboutAction->setText(QLatin1String("A&bout ") + m_posteRazorCore->imageIOLibraryName());
   76         connect (aboutAction, SIGNAL(triggered()), SLOT(showExtraAboutDialog()));
   77         m_mainWindow->addAboutDialogAction(aboutAction);
   78     }
   79 
   80     updateDialog();
   81     setDialogPosterSizeMode();
   82 
   83     m_translator = new QTranslator(this);
   84     loadTranslation(QString());
   85 }
   86 
   87 void Controller::updateDialog()
   88 {
   89     const QString unitOfLength = Types::unitsOfLength().value(m_posteRazorCore->unitOfLength()).first;
   90     m_mainWindow->setCurrentUnitOfLength(unitOfLength);
   91     setDialogPaperOptions();
   92     setDialogPosterOptions();
   93     setDialogOverlappingOptions();
   94     setDialogImageInfoFields();
   95     setDialogSaveOptions();
   96     updatePreview();
   97 }
   98 
   99 void Controller::updatePreview()
  100 {
  101     m_mainWindow->updatePreview();
  102 }
  103 
  104 void Controller::setUnitOfLength(Types::UnitsOfLength unit)
  105 {
  106     m_posteRazorCore->setUnitOfLength(unit);
  107     updateDialog();
  108 }
  109 
  110 void Controller::setPaperFormat(const QString &format)
  111 {
  112     m_posteRazorCore->setPaperFormat(format);
  113     setDialogPosterOptions();
  114     setDialogPaperBorders();
  115     updatePreview();
  116 }
  117 
  118 void Controller::setPaperOrientation(QPrinter::Orientation orientation)
  119 {
  120     m_posteRazorCore->setPaperOrientation(orientation);
  121     setDialogPosterOptions();
  122     setDialogPaperBorders();
  123     updatePreview();
  124 }
  125 
  126 void Controller::setPaperBorderTop(double border)
  127 {
  128     m_posteRazorCore->setPaperBorderTop(border);
  129     setDialogPosterOptions();
  130     updatePreview();
  131 }
  132 
  133 void Controller::setPaperBorderRight(double border)
  134 {
  135     m_posteRazorCore->setPaperBorderRight(border);
  136     setDialogPosterOptions();
  137     updatePreview();
  138 }
  139 
  140 void Controller::setPaperBorderBottom(double border)
  141 {
  142     m_posteRazorCore->setPaperBorderBottom(border);
  143     setDialogPosterOptions();
  144     updatePreview();
  145 }
  146 
  147 void Controller::setPaperBorderLeft(double border)
  148 {
  149     m_posteRazorCore->setPaperBorderLeft(border);
  150     setDialogPosterOptions();
  151     updatePreview();
  152 }
  153 
  154 void Controller::setCustomPaperWidth(double width)
  155 {
  156     m_posteRazorCore->setCustomPaperWidth(width);
  157     setDialogPosterOptions();
  158     setDialogPaperBorders();
  159     updatePreview();
  160 }
  161 
  162 void Controller::setCustomPaperHeight(double height)
  163 {
  164     m_posteRazorCore->setCustomPaperHeight(height);
  165     setDialogPosterOptions();
  166     setDialogPaperBorders();
  167     updatePreview();
  168 }
  169 
  170 void Controller::setUseCustomPaperSize(bool useIt)
  171 {
  172     m_posteRazorCore->setUseCustomPaperSize(useIt);
  173     setDialogPosterOptions();
  174     setDialogPaperBorders();
  175     updatePreview();
  176 }
  177 
  178 void Controller::setOverlappingWidth(double width)
  179 {
  180     m_posteRazorCore->setOverlappingWidth(width);
  181     setDialogPosterOptions();
  182     updatePreview();
  183 }
  184 
  185 void Controller::setOverlappingHeight(double height)
  186 {
  187     m_posteRazorCore->setOverlappingHeight(height);
  188     setDialogPosterOptions();
  189     updatePreview();
  190 }
  191 
  192 void Controller::setOverlappingPosition(Qt::Alignment position)
  193 {
  194     m_posteRazorCore->setOverlappingPosition(position);
  195     updatePreview();
  196 }
  197 
  198 void Controller::setPosterWidthAbsolute(double width)
  199 {
  200     m_posteRazorCore->setPosterWidth(Types::PosterSizeModeAbsolute, width);
  201     setDialogPosterDimensions(Types::PosterSizeModeAbsolute, true);
  202 }
  203 
  204 void Controller::setPosterHeightAbsolute(double height)
  205 {
  206     m_posteRazorCore->setPosterHeight(Types::PosterSizeModeAbsolute, height);
  207     setDialogPosterDimensions(Types::PosterSizeModeAbsolute, false);
  208 }
  209 
  210 void Controller::setPosterWidthPages(double width)
  211 {
  212     m_posteRazorCore->setPosterWidth(Types::PosterSizeModePages, width);
  213     setDialogPosterDimensions(Types::PosterSizeModePages, true);
  214 }
  215 
  216 void Controller::setPosterHeightPages(double height)
  217 {
  218     m_posteRazorCore->setPosterHeight(Types::PosterSizeModePages, height);
  219     setDialogPosterDimensions(Types::PosterSizeModePages, false);
  220 }
  221 
  222 void Controller::setPosterSizePercentual(double percent)
  223 {
  224     m_posteRazorCore->setPosterHeight(Types::PosterSizeModePercentual, percent);
  225     setDialogPosterDimensions(Types::PosterSizeModePercentual, false);
  226 }
  227 
  228 void Controller::setPosterSizeMode(Types::PosterSizeModes mode)
  229 {
  230     m_posteRazorCore->setPosterSizeMode(mode);
  231 }
  232 
  233 void Controller::setPosterAlignment(Qt::Alignment alignment)
  234 {
  235     m_posteRazorCore->setPosterAlignment(alignment);
  236     updatePreview();
  237 }
  238 
  239 void Controller::setLaunchPDFApplication(bool launch)
  240 {
  241     m_launchPDFApplication = launch;
  242     setDialogSaveOptions();
  243 }
  244 
  245 void Controller::setDialogSaveOptions()
  246 {
  247     m_mainWindow->setLaunchPDFApplication(m_launchPDFApplication);
  248 }
  249 
  250 void Controller::setDialogPosterSizeMode()
  251 {
  252     m_mainWindow->setPosterSizeMode(m_posteRazorCore->posterSizeMode());
  253 }
  254 
  255 void Controller::setDialogPosterOptions()
  256 {
  257     setDialogPosterDimensions(Types::PosterSizeModeNone, true);
  258     m_mainWindow->setPosterAlignment(m_posteRazorCore->posterAlignment());
  259     m_mainWindow->setPosterSizeMode(m_posteRazorCore->posterSizeMode());
  260 }
  261 
  262 void Controller::setDialogPosterDimensions(Types::PosterSizeModes excludedMode, bool widthExcluded)
  263 {
  264     const QSizeF posterSizeAbsolute = m_posteRazorCore->posterSize(Types::PosterSizeModeAbsolute);
  265     const QSizeF posterSizePages = m_posteRazorCore->posterSize(Types::PosterSizeModePages);
  266     const QSizeF posterSizePercentual = m_posteRazorCore->posterSize(Types::PosterSizeModePercentual);
  267     if (excludedMode != Types::PosterSizeModeAbsolute || !widthExcluded)
  268         m_mainWindow->setPosterWidthAbsolute(posterSizeAbsolute.width());
  269     if (excludedMode != Types::PosterSizeModeAbsolute || widthExcluded)
  270         m_mainWindow->setPosterHeightAbsolute(posterSizeAbsolute.height());
  271     if (excludedMode != Types::PosterSizeModePages || !widthExcluded)
  272         m_mainWindow->setPosterWidthPages(posterSizePages.width());
  273     if (excludedMode != Types::PosterSizeModePages || widthExcluded)
  274         m_mainWindow->setPosterHeightPages(posterSizePages.height());
  275     if (excludedMode != Types::PosterSizeModePercentual)
  276         m_mainWindow->setPosterSizePercentual(posterSizePercentual.width());
  277     updatePreview();
  278 }
  279 
  280 void Controller::setDialogPaperOptions()
  281 {
  282     setDialogPaperBorders();
  283     setDialogCustomPaperDimensions();
  284     m_mainWindow->setUseCustomPaperSize(m_posteRazorCore->usesCustomPaperSize());
  285     m_mainWindow->setPaperFormat(m_posteRazorCore->paperFormat());
  286     m_mainWindow->setPaperOrientation(m_posteRazorCore->paperOrientation());
  287 }
  288 
  289 void Controller::setDialogPaperBorders()
  290 {
  291     m_mainWindow->setPaperBorderTop(m_posteRazorCore->paperBorderTop());
  292     m_mainWindow->setPaperBorderRight(m_posteRazorCore->paperBorderRight());
  293     m_mainWindow->setPaperBorderBottom(m_posteRazorCore->paperBorderBottom());
  294     m_mainWindow->setPaperBorderLeft(m_posteRazorCore->paperBorderLeft());
  295 }
  296 
  297 void Controller::setDialogCustomPaperDimensions()
  298 {
  299     m_mainWindow->setCustomPaperSize(m_posteRazorCore->customPaperSize());
  300 }
  301 
  302 void Controller::setDialogImageInfoFields()
  303 {
  304     if (m_posteRazorCore->isImageLoaded()) {
  305         m_mainWindow->updateImageInfoFields (
  306             m_posteRazorCore->inputImageSizePixels(),
  307             m_posteRazorCore->inputImageSize(),
  308             m_posteRazorCore->inputImageVerticalDpi(),
  309             m_posteRazorCore->inputImageHorizontalDpi(),
  310             m_posteRazorCore->inputImageColorType(),
  311             m_posteRazorCore->inputImageBitsPerPixel()
  312         );
  313     }
  314 }
  315 
  316 void Controller::setDialogOverlappingDimensions()
  317 {
  318     m_mainWindow->setOverlappingWidth(m_posteRazorCore->overlappingWidth());
  319     m_mainWindow->setOverlappingHeight(m_posteRazorCore->overlappingHeight());
  320 }
  321 
  322 void Controller::setDialogOverlappingOptions()
  323 {
  324     setDialogOverlappingDimensions();
  325     m_mainWindow->setOverlappingPosition(m_posteRazorCore->overlappingPosition());
  326 }
  327 
  328 void Controller::readSettings(const QSettings *settings)
  329 {
  330     m_posteRazorCore->readSettings(settings);
  331     m_launchPDFApplication = settings->value(settingsKey_LaunchPDFApplication, m_launchPDFApplication).toBool();
  332     m_translationName = settings->value(settingsKey_TranslationName, m_translationName).toString();
  333     loadTranslation(m_translationName);
  334     updateDialog();
  335 }
  336 
  337 void Controller::writeSettings(QSettings *settings) const
  338 {
  339     settings->setValue(settingsKey_LaunchPDFApplication, m_launchPDFApplication);
  340     if (!m_translationName.isEmpty())
  341         settings->setValue(settingsKey_TranslationName, m_translationName);
  342     m_posteRazorCore->writeSettings(settings);
  343 }
  344 
  345 void Controller::loadInputImage()
  346 {
  347     QStringList allFilters;
  348     QStringList allWildcards;
  349 
  350     const QVector<QPair<QStringList, QString> > &formats = m_posteRazorCore->imageFormats();
  351     for (int i = 0; i < formats.count(); i++) {
  352         QStringList formatWildcards;
  353         foreach (const QString &extension, formats.at(i).first)
  354             formatWildcards << "*." + extension;
  355         allWildcards << formatWildcards;
  356         QString formatName = formats.at(i).second;
  357         // Some Open File dialogs (at least OSX) ar irritated if there are brackes in the file type name
  358         formatName.remove('(');
  359         formatName.remove(')');
  360         allFilters << formatName + " (" +  formatWildcards.join(" ") + ")";
  361     }
  362     allFilters.prepend(QCoreApplication::translate("Main window", "All image formats") + " (" +  allWildcards.join(" ") + ")");
  363 
  364     QSettings loadPathSettings;
  365 
  366     QString loadFileName = QFileDialog::getOpenFileName (
  367         m_mainWindow,
  368         QCoreApplication::translate("Main window", "Load an input image"),
  369         loadPathSettings.value(settingsKey_ImageLoadPath,
  370 #if QT_VERSION >= 0x040400
  371             QDesktopServices::storageLocation(QDesktopServices::PicturesLocation)
  372 #else
  373             "."
  374 #endif
  375             ).toString(),
  376         allFilters.join(";;")
  377     );
  378 
  379     if (!loadFileName.isEmpty()) {
  380         const bool successful = loadInputImage(loadFileName);
  381         if (successful)
  382             loadPathSettings.setValue(settingsKey_ImageLoadPath,
  383                 QDir::toNativeSeparators(QFileInfo(loadFileName).absolutePath()));
  384     }
  385 }
  386 
  387 bool Controller::loadInputImage(const QString &fileName)
  388 {
  389     QString loadErrorMessage;
  390     const bool successful = loadInputImage(fileName, loadErrorMessage);
  391     if (!successful)
  392         QMessageBox::critical(m_mainWindow, "", QCoreApplication::translate("Main window", "The image '%1' could not be loaded.")
  393             .arg(QFileInfo(fileName).fileName()));
  394     return successful;
  395 }
  396 
  397 bool Controller::loadInputImage(const QString &imageFileName, QString &errorMessage)
  398 {
  399     const bool result = m_posteRazorCore->loadInputImage(imageFileName, errorMessage);
  400     if (result) {
  401         updateDialog();
  402         m_mainWindow->showImageFileName(imageFileName);
  403     }
  404     return result;
  405 }
  406 
  407 int Controller::savePoster(const QString &fileName) const
  408 {
  409     const int result = m_posteRazorCore->savePoster(fileName);
  410     if (result == 0 && m_launchPDFApplication)
  411         QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
  412     return result;
  413 }
  414 
  415 void Controller::savePoster() const
  416 {
  417     QSettings savePathSettings;
  418 
  419     QString saveFileName = savePathSettings.value(settingsKey_PosterSavePath,
  420 #if QT_VERSION >= 0x040400
  421         QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)
  422 #else
  423         "."
  424 #endif
  425         ).toString();
  426     bool fileExistsAskUserForOverwrite = false;
  427 
  428     do {
  429         saveFileName = QFileDialog::getSaveFileName(
  430             m_mainWindow,
  431             QCoreApplication::translate("Main window", "Save the poster"),
  432             saveFileName,
  433             QLatin1String("Portable Document format (*.pdf)"),
  434             NULL,
  435             QFileDialog::DontConfirmOverwrite
  436         );
  437 
  438         if (!saveFileName.isEmpty()) {
  439             const QFileInfo saveFileInfo(saveFileName);
  440             if (saveFileInfo.suffix().toLower() != QLatin1String("pdf"))
  441                 saveFileName.append(".pdf");
  442 
  443             fileExistsAskUserForOverwrite = QFileInfo(saveFileName).exists();
  444 
  445             if (!fileExistsAskUserForOverwrite
  446                 || QMessageBox::Yes == (QMessageBox::question(m_mainWindow, "", QCoreApplication::translate("Main window", "The file '%1' already exists.\nDo you want to overwrite it?").arg(saveFileInfo.fileName()), QMessageBox::Yes, QMessageBox::No))
  447                 ) {
  448                 int result = savePoster(saveFileName.toAscii());
  449                 if (result != 0)
  450                     QMessageBox::critical(m_mainWindow, "", QCoreApplication::translate("Main window", "The file '%1' could not be saved.").arg(saveFileInfo.fileName()), QMessageBox::Ok, QMessageBox::NoButton);
  451                 else
  452                     savePathSettings.setValue(settingsKey_PosterSavePath,
  453                         QDir::toNativeSeparators(QFileInfo(saveFileName).absolutePath()));
  454                 fileExistsAskUserForOverwrite = false;
  455             }
  456         } else {
  457             break;
  458         }
  459     } while (fileExistsAskUserForOverwrite);
  460 }
  461 
  462 void Controller::loadTranslation(const QString &localeName)
  463 {
  464     const QString saneLocaleName = localeName.isEmpty()?QLocale::system().name():localeName;
  465     const QString translationFileName(":/Translations/" + saneLocaleName);
  466     if (m_translator->load(translationFileName)) {
  467         QCoreApplication::removeTranslator(m_translator);
  468         QCoreApplication::installTranslator(m_translator);
  469         m_mainWindow->setCurrentTranslation(saneLocaleName);
  470         if (!localeName.isEmpty())
  471             m_translationName = localeName;
  472         m_wizardController->updateDialogWizardStepDescription();
  473     }
  474 }
  475 
  476 void Controller::setUnitOfLength(const QString &unit)
  477 {
  478     m_posteRazorCore->setUnitOfLength(Types::unitOfLenthFromString(unit));
  479     updateDialog();
  480 }
  481 
  482 void Controller::openPosteRazorWebsite()
  483 {
  484     QDesktopServices::openUrl(QCoreApplication::translate("Help", "http://posterazor.sourceforge.net/", "Only translate, if the website has this language."));
  485 }
  486 
  487 void Controller::showExtraAboutDialog()
  488 {
  489     const QString title = QLatin1String("About ") + m_posteRazorCore->imageIOLibraryName();
  490     QMessageBox::about(
  491         m_mainWindow, title,
  492         QString(QLatin1String("<h3>%1</h3>%2")) // QMessageBox::aboutQt() also uses <h3>
  493             .arg(title)
  494             .arg(Types::newlineToParagraph(m_posteRazorCore->imageIOLibraryAboutText()))
  495     );
  496 }