Engauge Digitizer  2
DlgImportCroppingNonPdf.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "DlgImportCroppingNonPdf.h"
8 #include "EngaugeAssert.h"
9 #include "Logger.h"
10 #include "MainWindow.h"
11 #include "NonPdfCropping.h"
12 #include <QApplication>
13 #include <QGraphicsPixmapItem>
14 #include <QGraphicsScene>
15 #include <QImage>
16 #include <QLabel>
17 #include <QLayout>
18 #include <QPushButton>
19 #include <QSettings>
20 #include <QSpinBox>
21 #include <QTimer>
22 #include "Settings.h"
23 #include "ViewPreview.h"
24 
25 int DlgImportCroppingNonPdf::MINIMUM_DIALOG_WIDTH = 350;
26 int DlgImportCroppingNonPdf::MINIMUM_PREVIEW_HEIGHT = 200;
27 
29  m_fileName (fileName),
30  m_pixmap (0)
31 {
32  LOG4CPP_INFO_S ((*mainCat)) << "DlgImportCroppingNonPdf::DlgImportCroppingNonPdf";
33 
34  setWindowTitle (tr ("Image File Import Cropping"));
35  setModal (true);
36 
37  QWidget *subPanel = new QWidget ();
38  QGridLayout *layout = new QGridLayout (subPanel);
39  subPanel->setLayout (layout);
40 
41  int row = 0;
42 
43  createPreview (layout, row);
44  finishPanel (subPanel);
45  updatePreview ();
46 
47  // Bring the two middle columns together
48  layout->setColumnStretch (0, 1);
49  layout->setColumnStretch (1, 0);
50  layout->setColumnStretch (2, 0);
51  layout->setColumnStretch (3, 1);
52 }
53 
54 DlgImportCroppingNonPdf::~DlgImportCroppingNonPdf()
55 {
56  LOG4CPP_INFO_S ((*mainCat)) << "DlgImportCroppingNonPdf::~DlgImportCroppingNonPdf";
57 }
58 
59 void DlgImportCroppingNonPdf::createNonPdfCropping ()
60 {
61  // Create frame that shows what will be included, and what will be excluded, during the import
62  m_nonPdfCropping = new NonPdfCropping (*m_scenePreview,
63  *m_viewPreview);
64 }
65 
66 void DlgImportCroppingNonPdf::createPreview (QGridLayout *layout,
67  int &row)
68 {
69  LOG4CPP_INFO_S ((*mainCat)) << "DlgImportCroppingNonPdf::createPreview";
70 
71  QLabel *labelPreview = new QLabel (tr ("Preview"));
72  layout->addWidget (labelPreview, row++, 0, 1, 1, Qt::AlignLeft);
73 
74  m_scenePreview = new QGraphicsScene (this);
75  m_viewPreview = new ViewPreview (m_scenePreview,
76  ViewPreview::VIEW_ASPECT_RATIO_ONE_TO_ONE,
77  this);
78  m_viewPreview->setWhatsThis (tr ("Preview window that shows what part of the image will be imported. "
79  "The image portion inside the rectangular frame will be imported from the currently selected page. "
80  "The frame can be moved and resized by dragging the corner handles."));
81  m_viewPreview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
82  m_viewPreview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
83  m_viewPreview->setMinimumHeight (MINIMUM_PREVIEW_HEIGHT);
84  layout->addWidget (m_viewPreview, row++, 0, 1, 4);
85 
86  // More preview initialization
87  initializeFrameGeometryAndPixmap (); // Before first call to updatePreview
88  createNonPdfCropping ();
89 }
90 
91 void DlgImportCroppingNonPdf::finishPanel (QWidget *subPanel)
92 {
93  const int STRETCH_OFF = 0, STRETCH_ON = 1;
94 
95  QVBoxLayout *panelLayout = new QVBoxLayout (this);
96 
97  setMinimumWidth (MINIMUM_DIALOG_WIDTH);
98  setLayout (panelLayout);
99 
100  panelLayout->addWidget (subPanel);
101  panelLayout->setStretch (panelLayout->count () - 1, STRETCH_ON);
102 
103  QWidget *panelButtons = new QWidget (this);
104  QHBoxLayout *buttonLayout = new QHBoxLayout (panelButtons);
105 
106  QHBoxLayout *layoutRightSide = new QHBoxLayout;
107 
108  QWidget *widgetRightSide = new QWidget;
109  widgetRightSide->setLayout (layoutRightSide);
110  buttonLayout->addWidget (widgetRightSide);
111 
112  QSpacerItem *spacerExpanding = new QSpacerItem (40, 5, QSizePolicy::Expanding, QSizePolicy::Expanding);
113  layoutRightSide->addItem (spacerExpanding);
114 
115  m_btnOk = new QPushButton (tr ("Ok"));
116  layoutRightSide->addWidget (m_btnOk, 0, Qt::AlignRight);
117  connect (m_btnOk, SIGNAL (released ()), this, SLOT (slotOk ()));
118 
119  QSpacerItem *spacerFixed = new QSpacerItem (40, 5, QSizePolicy::Fixed, QSizePolicy::Fixed);
120  layoutRightSide->addItem (spacerFixed);
121 
122  m_btnCancel = new QPushButton (tr ("Cancel"));
123  layoutRightSide->addWidget (m_btnCancel, 0, Qt::AlignRight);
124  connect (m_btnCancel, SIGNAL (released ()), this, SLOT (slotCancel ()));
125 
126  panelLayout->addWidget (panelButtons, STRETCH_ON);
127  panelLayout->setStretch (panelLayout->count () - 1, STRETCH_OFF);
128 }
129 
131 {
132  // If the entire page was to be returned, then this method would simply return m_image. However, only the framed
133  // portion is to be returned
134  ENGAUGE_ASSERT (m_nonPdfCropping != 0);
135  QRectF rectFramePixels = m_nonPdfCropping->frameRect ();
136 
137  return m_image.copy (rectFramePixels.toRect ());
138 }
139 
140 void DlgImportCroppingNonPdf::initializeFrameGeometryAndPixmap ()
141 {
142  m_image = loadImage ();
143  QGraphicsPixmapItem *pixmap = new QGraphicsPixmapItem (QPixmap::fromImage (m_image));
144  m_scenePreview->addItem (pixmap);
145 
146  // Force resize so image fills preview area. We do this only once initially for speed
147  m_viewPreview->setSceneRect (pixmap->boundingRect ());
148 }
149 
150 QImage DlgImportCroppingNonPdf::loadImage () const
151 {
152  QImage image;
153  image.load (m_fileName);
154 
155  return image;
156 }
157 
158 void DlgImportCroppingNonPdf::saveGeometryToSettings()
159 {
160  // Store the settings for use by showEvent
161  QSettings settings;
162  settings.beginGroup (SETTINGS_GROUP_IMPORT_CROPPING);
163  settings.setValue (SETTINGS_IMPORT_CROPPING_POS, saveGeometry ());
164  settings.endGroup();
165 }
166 
167 void DlgImportCroppingNonPdf::showEvent (QShowEvent * /* event */)
168 {
169  QSettings settings;
170  settings.beginGroup (SETTINGS_GROUP_IMPORT_CROPPING);
171  if (settings.contains (SETTINGS_IMPORT_CROPPING_POS)) {
172 
173  // Restore the settings that were stored by the last call to saveGeometryToSettings
174  restoreGeometry (settings.value (SETTINGS_IMPORT_CROPPING_POS).toByteArray ());
175  }
176 }
177 
178 void DlgImportCroppingNonPdf::slotCancel ()
179 {
180  LOG4CPP_INFO_S ((*mainCat)) << "DlgImportCroppingNonPdf::slotCancel";
181 
182  // Restore cursor in case updatePreview has not already completed and then restored it
183  QApplication::restoreOverrideCursor ();
184 
185  setResult (QDialog::Rejected);
186  saveGeometryToSettings();
187  hide();
188 }
189 
190 void DlgImportCroppingNonPdf::slotOk ()
191 {
192  LOG4CPP_INFO_S ((*mainCat)) << "DlgImportCroppingNonPdf::slotOk";
193 
194  // Restore cursor in case updatePreview has not already completed and then restored it
195  QApplication::restoreOverrideCursor ();
196 
197  setResult (QDialog::Accepted);
198  saveGeometryToSettings();
199  hide();
200 }
201 
202 void DlgImportCroppingNonPdf::updatePreview ()
203 {
204  LOG4CPP_INFO_S ((*mainCat)) << "DlgImportCroppingNonPdf::updatePreview";
205 
206  if (m_pixmap != 0) {
207  m_scenePreview->removeItem (m_pixmap);
208  }
209 
210  m_image = loadImage ();
211  m_pixmap = new QGraphicsPixmapItem (QPixmap::fromImage (m_image));
212  m_scenePreview->addItem (m_pixmap);
213 
214  // Calculations for preview updating are now over
215  QApplication::restoreOverrideCursor ();
216 }
This class shows a frame around the selected portion of the import preview window.
QRectF frameRect() const
Frame rectangle selected by user.
Class that modifies QGraphicsView to automatically expand/shrink the view to fit the window...
Definition: ViewPreview.h:14
DlgImportCroppingNonPdf(const QString &fileName)
Single constructor.
virtual void showEvent(QShowEvent *event)
Do preparation before dialog is displayed.
QImage image() const
Image that was selected. Value is null if loading failed.