QElectroTech  0.70
qetprintpreviewdialog.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech 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 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech 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 QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "qetprintpreviewdialog.h"
19 #include "diagramschooser.h"
20 #include "exportproperties.h"
21 #include "exportpropertieswidget.h"
22 #include "qeticons.h"
23 
24 #include <QPrintPreviewWidget>
25 #include <QPageSetupDialog>
26 #include <algorithm>
27 
35 QETPrintPreviewDialog::QETPrintPreviewDialog(QETProject *project, QPrinter *printer, QWidget *widget, Qt::WindowFlags f) :
36  QDialog(widget, f),
37  project_(project),
38  printer_(printer)
39 {
40  setWindowTitle(tr("QElectroTech : Aperçu avant impression"));
41  build();
42 
43  connect(preview_, SIGNAL(paintRequested(QPrinter *)), this, SLOT(requestPaint(QPrinter *)));
44  connect(diagrams_list_, SIGNAL(selectionChanged()), preview_, SLOT(updatePreview()));
45  connect(diagrams_list_, SIGNAL(selectionChanged()), this, SLOT(checkDiagramsCount()));
46 
47  setWindowState(windowState() | Qt::WindowMaximized);
48 }
49 
54 }
55 
60  return(diagrams_list_);
61 }
62 
67  return(fit_diagram_to_page_ -> isChecked());
68 }
69 
75 }
76 
81  preview_ -> setCurrentPage(1);
82 }
83 
88  int preview_previous_page = preview_ -> currentPage() - 1;
89  preview_ -> setCurrentPage(qMax(preview_previous_page, 0));
90 }
91 
96  int preview_next_page = preview_ -> currentPage() + 1;
97  preview_ -> setCurrentPage(qMin(preview_next_page, preview_ -> pageCount()));
98 }
99 
104  preview_ -> setCurrentPage(preview_ -> pageCount());
105 }
106 
111  QPageSetupDialog page_setup_dialog(printer_, this);
112  if (page_setup_dialog.exec() == QDialog::Accepted) {
113  preview_ -> updatePreview();
114  updateZoomList();
115  }
116 }
117 
123  printer_ -> setFullPage(full_page);
124  preview_ -> updatePreview();
125  updateZoomList();
126 }
127 
133  Q_UNUSED(fit_diagram);
134  preview_ -> updatePreview();
135  updateZoomList();
136 }
137 
142  preview_ -> zoomIn(4.0/3.0);
143  updateZoomList();
144 }
145 
150  preview_ -> zoomOut(4.0/3.0);
151  updateZoomList();
152 }
153 
158  diagrams_list_ -> setSelectedAllDiagrams(true);
159 }
160 
165  diagrams_list_ -> setSelectedAllDiagrams(false);
166 }
167 
172  preview_ = new QPrintPreviewWidget(printer_);
173  diagrams_label_ = new QLabel(tr("Folios à imprimer :"));
175  diagrams_select_all_ = new QPushButton(tr("Tout cocher"));
176  diagrams_select_none_ = new QPushButton(tr("Tout décocher"));
177  toggle_diagrams_list_ = new QAction(QET::Icons::Diagram, tr("Cacher la liste des folios"), this);
178  toggle_print_options_ = new QAction(QET::Icons::Configure, tr("Cacher les options d'impression"), this);
179  adjust_width_ = new QAction(QET::Icons::ViewFitWidth, tr("Ajuster la largeur"), this);
180  adjust_page_ = new QAction(QET::Icons::ViewFitWindow, tr("Ajuster la page"), this);
181  zoom_out_ = new QAction(QET::Icons::ZoomOut, tr("Zoom arrière"), this);
182  zoom_box_ = new QComboBox(this);
183  zoom_in_ = new QAction(QET::Icons::ZoomIn, tr("Zoom avant"), this);
184  landscape_ = new QAction(QET::Icons::PrintLandscape, tr("Paysage"), this);
185  portrait_ = new QAction(QET::Icons::PrintPortrait, tr("Portrait"), this);
186  first_page_ = new QAction(QET::Icons::ArrowLeftDouble, tr("Première page"), this);
187  previous_page_ = new QAction(QET::Icons::ArrowLeft, tr("Page précédente"), this);
188  next_page_ = new QAction(QET::Icons::ArrowRight, tr("Page suivante"), this);
189  last_page_ = new QAction(QET::Icons::ArrowRightDouble, tr("Dernière page"), this);
190  single_page_view_ = new QAction(QET::Icons::SinglePage, tr("Afficher une seule page"), this);
191  facing_pages_view_ = new QAction(QET::Icons::PrintTwoPages, tr("Afficher deux pages"), this);
192  all_pages_view_ = new QAction(QET::Icons::PrintAllPages, tr("Afficher un aperçu de toutes les pages"), this);
193  page_setup_ = new QAction(QET::Icons::DocumentPrintFrame, tr("Mise en page"), this);
194 
195  toggle_diagrams_list_ -> setCheckable(true);
196  toggle_diagrams_list_ -> setChecked(true);
197  toggle_print_options_ -> setCheckable(true);
198  toggle_print_options_ -> setChecked(true);
199 
200 #ifdef Q_OS_WIN32
201  /*
202  Sous Windows, le QPageSetupDialog utilise le dialogue natif ; ce
203  dernier ne peut gerer que les imprimantes physiques ("native
204  printers" ).
205  cf avertissement : QAbstractPageSetupDialog::QAbstractPageSetupDialog:
206  Page setup dialog cannot be used on non-native printers
207  */
208  if (!(printer_ -> outputFileName().isEmpty())) {
209  page_setup_ -> setEnabled(false);
210  page_setup_ -> setText(tr("Mise en page (non disponible sous Windows pour l'impression PDF/PS)"));
211  }
212 #endif
213 
214  toolbar_ = new QToolBar();
215  toolbar_ -> addAction(toggle_diagrams_list_);
216  toolbar_ -> addAction(toggle_print_options_);
217  toolbar_ -> addSeparator();
218  toolbar_ -> addAction(adjust_width_);
219  toolbar_ -> addAction(adjust_page_);
220  toolbar_ -> addAction(zoom_out_);
221  toolbar_ -> addWidget(zoom_box_);
222  toolbar_ -> addAction(zoom_in_);
223  toolbar_ -> addSeparator();
224  toolbar_ -> addAction(landscape_);
225  toolbar_ -> addAction(portrait_);
226  toolbar_ -> addSeparator();
227  toolbar_ -> addAction(first_page_);
228  toolbar_ -> addAction(previous_page_);
229  toolbar_ -> addAction(next_page_);
230  toolbar_ -> addAction(last_page_);
231  toolbar_ -> addSeparator();
232  toolbar_ -> addAction(single_page_view_);
233  toolbar_ -> addAction(facing_pages_view_);
234  toolbar_ -> addAction(all_pages_view_);
235  toolbar_ -> addSeparator();
236  toolbar_ -> addAction(page_setup_);
237 
238  print_options_box_= new QGroupBox(tr("Options d'impression"));
239  use_full_page_ = new QCheckBox(tr("Utiliser toute la feuille"));
240  use_full_page_ -> setChecked(printer_ -> fullPage());
241  use_full_page_label_ = new QLabel(tr(
242  "Si cette option est cochée, les marges de la feuille seront "
243  "ignorées et toute sa surface sera utilisée pour l'impression. "
244  "Cela peut ne pas être supporté par votre imprimante."
245  ));
246  use_full_page_label_ -> setWordWrap(true);
247  use_full_page_label_ -> setContentsMargins(20, 0, 0, 0);
248  fit_diagram_to_page_ = new QCheckBox(tr("Adapter le folio à la page"));
249  fit_diagram_to_page_label_ = new QLabel(tr(
250  "Si cette option est cochée, le folio sera agrandi ou "
251  "rétréci de façon à remplir toute la surface imprimable "
252  "d'une et une seule page."
253  ));
254  fit_diagram_to_page_label_ -> setWordWrap(true);
255  fit_diagram_to_page_label_ -> setContentsMargins(20, 0, 0, 0);
256  fit_diagram_to_page_ -> setChecked(true);
257 
258  // recupere les parametres d'export definis dans la configuration de l'application
259  ExportProperties default_print_properties = ExportProperties::defaultPrintProperties();
260 
261  render_properties_ = new ExportPropertiesWidget(default_print_properties);
262  render_properties_ -> setPrintingMode(true);
263 
264  buttons_ = new QDialogButtonBox();
265  buttons_ -> addButton(new QPushButton(QET::Icons::DocumentPrint, tr("Imprimer")), QDialogButtonBox::AcceptRole);
266  buttons_ -> addButton(QDialogButtonBox::Cancel);
267 
268  connect(diagrams_select_all_, SIGNAL(released()), this, SLOT(selectAllDiagrams()));
269  connect(diagrams_select_none_, SIGNAL(released()), this, SLOT(selectNoDiagram()));
270  connect(toggle_diagrams_list_, SIGNAL(toggled(bool)), this, SLOT(setDiagramsListVisible(bool)));
271  connect(toggle_print_options_, SIGNAL(toggled(bool)), this, SLOT(setPrintOptionsVisible(bool)));
272  connect(adjust_width_, SIGNAL(triggered()), preview_, SLOT(fitToWidth()));
273  connect(adjust_page_, SIGNAL(triggered()), preview_, SLOT(fitInView()));
274  connect(zoom_out_, SIGNAL(triggered()), this, SLOT(zoomOut()));
275  connect(zoom_in_, SIGNAL(triggered()), this, SLOT(zoomIn()));
276  connect(landscape_, SIGNAL(triggered()), preview_, SLOT(setLandscapeOrientation()));
277  connect(portrait_, SIGNAL(triggered()), preview_, SLOT(setPortraitOrientation()));
278  connect(first_page_, SIGNAL(triggered()), this, SLOT(firstPage()));
279  connect(previous_page_, SIGNAL(triggered()), this, SLOT(previousPage()));
280  connect(next_page_, SIGNAL(triggered()), this, SLOT(nextPage()));
281  connect(last_page_, SIGNAL(triggered()), this, SLOT(lastPage()));
282  connect(single_page_view_, SIGNAL(triggered()), preview_, SLOT(setSinglePageViewMode()));
283  connect(facing_pages_view_, SIGNAL(triggered()), preview_, SLOT(setFacingPagesViewMode()));
284  connect(all_pages_view_, SIGNAL(triggered()), preview_, SLOT(setAllPagesViewMode()));
285  connect(page_setup_, SIGNAL(triggered()), this, SLOT(pageSetup()));
286 
287  connect(use_full_page_, SIGNAL(toggled(bool)), this, SLOT(useFullPage(bool)));
288  connect(fit_diagram_to_page_, SIGNAL(toggled(bool)), this, SLOT(fitDiagramToPage(bool)));
289 
290  connect(render_properties_, SIGNAL(optionChanged()), preview_, SLOT(updatePreview()));
291 
292  connect(preview_, SIGNAL(previewChanged()), this, SLOT(updateZoomList()));
293  connect(zoom_box_, SIGNAL(currentIndexChanged(int)), this, SLOT(updatePreviewZoom()));
294 
295  connect(buttons_, SIGNAL(accepted()), this, SLOT(accept()));
296  connect(buttons_, SIGNAL(rejected()), this, SLOT(reject()));
297 
298  hlayout0_ = new QHBoxLayout();
299  vlayout0_ = new QVBoxLayout();
300  vlayout1_ = new QVBoxLayout();
301  vlayout2_ = new QVBoxLayout();
302 
303  vlayout1_ -> addWidget(use_full_page_);
304  vlayout1_ -> addWidget(use_full_page_label_);
305  vlayout1_ -> addWidget(fit_diagram_to_page_);
307  print_options_box_ -> setLayout(vlayout1_);
308 
309  vlayout2_ -> addWidget(diagrams_label_);
310  vlayout2_ -> addWidget(diagrams_list_);
311  vlayout2_ -> addWidget(diagrams_select_all_);
312  vlayout2_ -> addWidget(diagrams_select_none_);
313 
314  hlayout0_ -> addLayout(vlayout2_);
315  hlayout0_ -> addWidget(preview_);
316 
317  vlayout0_ -> addWidget(toolbar_);
318  vlayout0_ -> addLayout(hlayout0_);
319  vlayout0_ -> addWidget(render_properties_);
320  vlayout0_ -> addWidget(print_options_box_);
321  vlayout0_ -> addWidget(buttons_);
322 
323  setLayout(vlayout0_);
324  updateZoomList();
325 }
326 
333 void QETPrintPreviewDialog::requestPaint(QPrinter *printer) {
334  emit(
336  diagrams_list_ -> selectedDiagrams(),
337  fit_diagram_to_page_ -> isChecked(),
339  printer
340  )
341  );
342 }
343 
349  int diagrams_count = diagrams_list_ -> selectedDiagrams().count();
350 
351  // desactive le premier bouton de la liste (= le bouton "Imprimer")
352  QList<QAbstractButton *> buttons = buttons_ -> buttons();
353  if (buttons.count()) buttons[0] -> setEnabled(diagrams_count);
354 }
355 
361  diagrams_label_ -> setVisible(display);
362  diagrams_list_ -> setVisible(display);
363  diagrams_select_all_ -> setVisible(display);
364  diagrams_select_none_ -> setVisible(display);
365 
366  if (display) {
367  toggle_diagrams_list_ -> setText(tr("Cacher la liste des folios"));
368  } else {
369  toggle_diagrams_list_ -> setText(tr("Afficher la liste des folios"));
370  }
371 }
372 
379  print_options_box_ -> setVisible(display);
380  render_properties_ -> setVisible(display);
381 
382  if (display) {
383  toggle_print_options_ -> setText(tr("Cacher les options d'impression"));
384  } else {
385  toggle_print_options_ -> setText(tr("Afficher les options d'impression"));
386  }
387 }
388 
393  // recupere le zooom courant
394  qreal current_zoom = preview_ -> zoomFactor();
395  bool current_zoom_is_not_null = bool(int(current_zoom * 100.0));
396 
397  // liste des zooms par defaut
398  QList<qreal> zooms_real;
399  zooms_real << 0.25 << 0.5 << 0.75 << 1.0 << 1.5 << 2.0 << 4.0 << 8.0;
400 
401  // ajout du zoom en cours
402  if (current_zoom_is_not_null && (!zooms_real.contains(current_zoom))) {
403  zooms_real << current_zoom;
404  std::sort(zooms_real.begin(), zooms_real.end());
405  }
406 
407  // remplissage de la liste deroulante
408  int current_zoom_index = -1;
409  zoom_box_ -> blockSignals(true);
410  zoom_box_ -> clear();
411  foreach (qreal z, zooms_real) {
412  zoom_box_ -> addItem(QString(tr("%1 %")).arg(z * 100.0, 0, 'f', 2), z);
413  if (z == current_zoom) current_zoom_index = zoom_box_ -> count() - 1;
414  }
415  zoom_box_ -> setCurrentIndex(current_zoom_index);
416  zoom_box_ -> blockSignals(false);
417 }
418 
423  preview_ -> setZoomFactor(
424  zoom_box_ -> itemData(zoom_box_ -> currentIndex()).toDouble()
425  );
426  updateZoomList();
427 }
ExportProperties exportProperties() const
QIcon PrintTwoPages
Definition: qeticons.cpp:142
QIcon SinglePage
Definition: qeticons.cpp:160
QIcon ArrowRight
Definition: qeticons.cpp:30
DiagramsChooser * diagrams_list_
DiagramsChooser * diagramsChooser()
QIcon PrintLandscape
Definition: qeticons.cpp:140
QIcon DocumentPrintFrame
Definition: qeticons.cpp:57
QPushButton * diagrams_select_all_
QETPrintPreviewDialog(QETProject *, QPrinter *, QWidget *=nullptr, Qt::WindowFlags=nullptr)
QIcon ZoomOut
Definition: qeticons.cpp:181
QIcon Configure
Definition: qeticons.cpp:38
QIcon ViewFitWidth
Definition: qeticons.cpp:170
ExportPropertiesWidget * render_properties_
QIcon ArrowLeft
Definition: qeticons.cpp:28
QPrintPreviewWidget * preview_
QIcon ZoomIn
Definition: qeticons.cpp:179
QIcon PrintAllPages
Definition: qeticons.cpp:138
QDialogButtonBox * buttons_
QIcon ArrowRightDouble
Definition: qeticons.cpp:31
QIcon DocumentPrint
Definition: qeticons.cpp:56
QIcon ArrowLeftDouble
Definition: qeticons.cpp:29
QIcon tr
Definition: qeticons.cpp:204
static ExportProperties defaultPrintProperties()
ExportProperties::defaultPrintProperties.
QIcon PrintPortrait
Definition: qeticons.cpp:141
void paintRequested(const QList< Diagram *> &, bool, const ExportProperties, QPrinter *)
QPushButton * diagrams_select_none_
QIcon Cancel
Definition: qeticons.cpp:34
QIcon ViewFitWindow
Definition: qeticons.cpp:171
QIcon Diagram
Definition: qeticons.cpp:43