1 /*
2     Scan Tailor - Interactive post-processing tool for scanned pages.
3     Copyright (C) 2007-2009  Joseph Artsimovich <joseph_a@mail.ru>
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "ProjectCreationContext.h"
20 #include <boost/lambda/bind.hpp>
21 #include <boost/lambda/lambda.hpp>
22 #include <cassert>
23 #include "FixDpiDialog.h"
24 #include "ProjectFilesDialog.h"
25 
ProjectCreationContext(QWidget * parent)26 ProjectCreationContext::ProjectCreationContext(QWidget* parent) : m_layoutDirection(Qt::LeftToRight), m_parent(parent) {
27   showProjectFilesDialog();
28 }
29 
~ProjectCreationContext()30 ProjectCreationContext::~ProjectCreationContext() {
31   // Deleting a null pointer is OK.
32   delete m_projectFilesDialog;
33   delete m_fixDpiDialog;
34 }
35 
36 namespace {
37 template <typename T>
allDpisOK(const T & container)38 bool allDpisOK(const T& container) {
39   using namespace boost::lambda;
40 
41   return std::find_if(container.begin(), container.end(), !bind(&ImageFileInfo::isDpiOK, _1)) == container.end();
42 }
43 }  // anonymous namespace
44 
projectFilesSubmitted()45 void ProjectCreationContext::projectFilesSubmitted() {
46   m_files = m_projectFilesDialog->inProjectFiles();
47   m_outDir = m_projectFilesDialog->outputDirectory();
48   m_layoutDirection = Qt::LeftToRight;
49   if (m_projectFilesDialog->isRtlLayout()) {
50     m_layoutDirection = Qt::RightToLeft;
51   }
52 
53   if (!m_projectFilesDialog->isDpiFixingForced() && allDpisOK(m_files)) {
54     emit done(this);
55   } else {
56     showFixDpiDialog();
57   }
58 }
59 
projectFilesDialogDestroyed()60 void ProjectCreationContext::projectFilesDialogDestroyed() {
61   if (!m_fixDpiDialog) {
62     deleteLater();
63   }
64 }
65 
fixedDpiSubmitted()66 void ProjectCreationContext::fixedDpiSubmitted() {
67   m_files = m_fixDpiDialog->files();
68   emit done(this);
69 }
70 
fixDpiDialogDestroyed()71 void ProjectCreationContext::fixDpiDialogDestroyed() {
72   deleteLater();
73 }
74 
showProjectFilesDialog()75 void ProjectCreationContext::showProjectFilesDialog() {
76   assert(!m_projectFilesDialog);
77   m_projectFilesDialog = new ProjectFilesDialog(m_parent);
78   m_projectFilesDialog->setAttribute(Qt::WA_DeleteOnClose);
79   m_projectFilesDialog->setAttribute(Qt::WA_QuitOnClose, false);
80   if (m_parent) {
81     m_projectFilesDialog->setWindowModality(Qt::WindowModal);
82   }
83   connect(m_projectFilesDialog, SIGNAL(accepted()), this, SLOT(projectFilesSubmitted()));
84   connect(m_projectFilesDialog, SIGNAL(destroyed(QObject*)), this, SLOT(projectFilesDialogDestroyed()));
85   m_projectFilesDialog->show();
86 }
87 
showFixDpiDialog()88 void ProjectCreationContext::showFixDpiDialog() {
89   assert(!m_fixDpiDialog);
90   m_fixDpiDialog = new FixDpiDialog(m_files, m_parent);
91   m_fixDpiDialog->setAttribute(Qt::WA_DeleteOnClose);
92   m_fixDpiDialog->setAttribute(Qt::WA_QuitOnClose, false);
93   if (m_parent) {
94     m_fixDpiDialog->setWindowModality(Qt::WindowModal);
95   }
96   connect(m_fixDpiDialog, SIGNAL(accepted()), this, SLOT(fixedDpiSubmitted()));
97   connect(m_fixDpiDialog, SIGNAL(destroyed(QObject*)), this, SLOT(fixDpiDialogDestroyed()));
98   m_fixDpiDialog->show();
99 }
100