Logo Search packages:      
Sourcecode: qelectrotech version File versions  Download package

qetprintpreviewdialog.cpp

/*
      Copyright 2006-2009 Xavier Guerrin
      This file is part of QElectroTech.
      
      QElectroTech is free software: you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation, either version 2 of the License, or
      (at your option) any later version.
      
      QElectroTech is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.
      
      You should have received a copy of the GNU General Public License
      along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "qetprintpreviewdialog.h"
#include "diagramschooser.h"
#include "qeticons.h"

/**
      Constructeur
      @param printer Imprimante a utiliser pour
      @param widget  Widget parent
*/
00027 QETPrintPreviewDialog::QETPrintPreviewDialog(QETProject *project, QPrinter *printer, QWidget *widget, Qt::WindowFlags f) :
      QDialog(widget, f),
      project_(project),
      printer_(printer)
{
      setWindowTitle(tr("QElectroTech : Aper\347u avant impression"));
      build();
      
      connect(preview_,       SIGNAL(paintRequested(QPrinter *)), this,     SLOT(requestPaint(QPrinter *)));
      connect(diagrams_list_, SIGNAL(selectionChanged()),         preview_, SLOT(updatePreview()));
      connect(diagrams_list_, SIGNAL(selectionChanged()),         this,     SLOT(checkDiagramsCount()));
      
      setWindowState(Qt::WindowMaximized);
}

/**
      Destructeur
*/
00045 QETPrintPreviewDialog::~QETPrintPreviewDialog() {
}

/**
      @return le widget permettant de choisir les schemas a imprimer.
*/
00051 DiagramsChooser *QETPrintPreviewDialog::diagramsChooser() {
      return(diagrams_list_);
}

/**
      @return true si l'option "Adapter le schema a la page" est activee
*/
00058 bool QETPrintPreviewDialog::fitDiagramsToPages() const {
      return(fit_diagram_to_page_ -> isChecked());
}

/**
      Passe a la premiere page
*/
00065 void QETPrintPreviewDialog::firstPage() {
      preview_ -> setCurrentPage(1);
}

/**
      Passe a la page precedente
*/
00072 void QETPrintPreviewDialog::previousPage() {
      int preview_previous_page = preview_ -> currentPage() - 1;
      preview_ -> setCurrentPage(qMax(preview_previous_page, 0));
}

/**
      Passe a la page suivante
*/
00080 void QETPrintPreviewDialog::nextPage() {
      int preview_next_page = preview_ -> currentPage() + 1;
      preview_ -> setCurrentPage(qMin(preview_next_page, preview_ -> numPages()));
}

/**
      Passe a la derniere page
*/
00088 void QETPrintPreviewDialog::lastPage() {
      preview_ -> setCurrentPage(preview_ -> numPages());
}

/**
      Copnfigure la mise en page
*/
00095 void QETPrintPreviewDialog::pageSetup() {
      QPageSetupDialog page_setup_dialog(printer_, this);
      if (page_setup_dialog.exec() == QDialog::Accepted) {
            preview_ -> updatePreview();
            updateZoomList();
      }
}

/**
      Utilise ou non toute la page sans teni compte des marges
      @param full_page true pour utiliser toute la page, false sinon
*/
00107 void QETPrintPreviewDialog::useFullPage(bool full_page) {
      printer_ -> setFullPage(full_page);
      preview_ -> updatePreview();
      updateZoomList();
}

/**
      Fait tenir ou non chaque schema sur une page
      @param fit_diagram true pour adapter chaque schema sur une page, false sinon
*/
00117 void QETPrintPreviewDialog::fitDiagramToPage(bool /*fit_diagram*/) {
      preview_ -> updatePreview();
      updateZoomList();
}

/**
      Effectue l'action "zoom avant" sur l'apercu avant impression
*/
00125 void QETPrintPreviewDialog::zoomIn() {
      preview_ -> zoomIn(4.0/3.0);
      updateZoomList();
}

/**
      Effectue l'action "zoom arriere" sur l'apercu avant impression
*/
00133 void QETPrintPreviewDialog::zoomOut() {
      preview_ -> zoomOut(4.0/3.0);
      updateZoomList();
}

/**
      Met en place le dialogue
*/
00141 void QETPrintPreviewDialog::build() {
      preview_ = new QPrintPreviewWidget(printer_);
      diagrams_label_       = new QLabel(tr("Sch\351mas \340 imprimer\240:"));
      diagrams_list_        = new DiagramsChooser(project_);
      toggle_diagrams_list_ = new QAction(QET::Icons::Diagram,              tr("Cacher la liste des sch\351mas"),            this);
      toggle_print_options_ = new QAction(QET::Icons::Configure,            tr("Cacher les options d'impression"),           this);
      adjust_width_         = new QAction(QET::Icons::ViewFitWidth,         tr("Ajuster la largeur"),                        this);
      adjust_page_          = new QAction(QET::Icons::ViewFitWindow,        tr("Ajuster la page"),                           this);
      zoom_out_             = new QAction(QET::Icons::ZoomOut,              tr("Zoom arri\350re"),                           this);
      zoom_box_             = new QComboBox(this);
      zoom_in_              = new QAction(QET::Icons::ZoomIn,               tr("Zoom avant"),                                this);
      landscape_            = new QAction(QET::Icons::PrintLandscape,       tr("Paysage"),                                   this);
      portrait_             = new QAction(QET::Icons::PrintPortrait,        tr("Portrait"),                                  this);
      first_page_           = new QAction(QET::Icons::ArrowLeftDouble,      tr("Premi\350re page"),                          this);
      previous_page_        = new QAction(QET::Icons::ArrowLeft,            tr("Page pr\351c\351dente"),                     this);
      next_page_            = new QAction(QET::Icons::ArrowRight,           tr("Page suivante"),                             this);
      last_page_            = new QAction(QET::Icons::ArrowRightDouble,     tr("Derni\350re page"),                          this);
      single_page_view_     = new QAction(QET::Icons::SinglePage,           tr("Afficher une seule page"),                   this);
      facing_pages_view_    = new QAction(QET::Icons::PrintTwoPages,        tr("Afficher deux pages"),                       this);
      all_pages_view_       = new QAction(QET::Icons::PrintAllPages,        tr("Afficher un aper\347u de toutes les pages"), this);
      page_setup_           = new QAction(QET::Icons::DocumentPrintFrame,   tr("Mise en page"),                              this);
      
      toggle_diagrams_list_ -> setCheckable(true);
      toggle_diagrams_list_ -> setChecked(true);
      toggle_print_options_ -> setCheckable(true);
      toggle_print_options_ -> setChecked(true);
      
#ifdef Q_OS_WIN32
      /*
            Sous Windows, le QPageSetupDialog utilise le dialogue natif ; ce
            dernier ne peut gerer que les imprimantes physiques ("native
            printers" ).
            cf avertissement : QAbstractPageSetupDialog::QAbstractPageSetupDialog:
            Page setup dialog cannot be used on non-native printers
      */
      if (!(printer_ -> outputFileName().isEmpty())) {
            page_setup_ -> setEnabled(false);
            page_setup_ -> setText(tr("Mise en page (non disponible sous Windows pour l'impression PDF/PS)"));
      }
#endif
      
      toolbar_ = new QToolBar();
      toolbar_ -> addAction(toggle_diagrams_list_);
      toolbar_ -> addAction(toggle_print_options_);
      toolbar_ -> addSeparator();
      toolbar_ -> addAction(adjust_width_);
      toolbar_ -> addAction(adjust_page_);
      toolbar_ -> addAction(zoom_out_);
      toolbar_ -> addWidget(zoom_box_);
      toolbar_ -> addAction(zoom_in_);
      toolbar_ -> addSeparator();
      toolbar_ -> addAction(landscape_);
      toolbar_ -> addAction(portrait_);
      toolbar_ -> addSeparator();
      toolbar_ -> addAction(first_page_);
      toolbar_ -> addAction(previous_page_);
      toolbar_ -> addAction(next_page_);
      toolbar_ -> addAction(last_page_);
      toolbar_ -> addSeparator();
      toolbar_ -> addAction(single_page_view_);
      toolbar_ -> addAction(facing_pages_view_);
      toolbar_ -> addAction(all_pages_view_);
      toolbar_ -> addSeparator();
      toolbar_ -> addAction(page_setup_);
      
      print_options_box_= new QGroupBox(tr("Options d'impression"));
      use_full_page_ = new QCheckBox(tr("Utiliser toute la feuille"));
      use_full_page_label_ = new QLabel(tr(
            "Si cette option est coch\351e, les marges de la feuille seront "
            "ignor\351es et toute sa surface sera utilis\351e pour l'impression. "
            "Cela peut ne pas \352tre support\351 par votre imprimante."
      ));
      use_full_page_label_ -> setWordWrap(true);
      use_full_page_label_ -> setContentsMargins(20, 0, 0, 0);
      fit_diagram_to_page_ = new QCheckBox(tr("Adapter le sch\351ma \340 la page"));
      fit_diagram_to_page_label_ = new QLabel(tr(
            "Si cette option est coch\351e, le sch\351ma sera agrandi ou "
            "r\351tr\351ci de fa\347on \340 remplir toute la surface imprimable "
            "d'une et une seule page."
      ));
      fit_diagram_to_page_label_ -> setWordWrap(true);
      fit_diagram_to_page_label_ -> setContentsMargins(20, 0, 0, 0);
      fit_diagram_to_page_ -> setChecked(true);
      
      buttons_ = new QDialogButtonBox();
      buttons_ -> addButton(new QPushButton(QET::Icons::DocumentPrint, tr("Imprimer")), QDialogButtonBox::AcceptRole);
      buttons_ -> addButton(QDialogButtonBox::Cancel);
      
      connect(toggle_diagrams_list_, SIGNAL(toggled(bool)), this,     SLOT(setDiagramsListVisible(bool)));
      connect(toggle_print_options_, SIGNAL(toggled(bool)), this,     SLOT(setPrintOptionsVisible(bool)));
      connect(adjust_width_,         SIGNAL(triggered()),   preview_, SLOT(fitToWidth()));
      connect(adjust_page_,          SIGNAL(triggered()),   preview_, SLOT(fitInView()));
      connect(zoom_out_,             SIGNAL(triggered()),   this,     SLOT(zoomOut()));
      connect(zoom_in_,              SIGNAL(triggered()),   this,     SLOT(zoomIn()));
      connect(landscape_,            SIGNAL(triggered()),   preview_, SLOT(setLandscapeOrientation()));
      connect(portrait_,             SIGNAL(triggered()),   preview_, SLOT(setPortraitOrientation()));
      connect(first_page_,           SIGNAL(triggered()),   this,     SLOT(firstPage()));
      connect(previous_page_,        SIGNAL(triggered()),   this,     SLOT(previousPage()));
      connect(next_page_,            SIGNAL(triggered()),   this,     SLOT(nextPage()));
      connect(last_page_,            SIGNAL(triggered()),   this,     SLOT(lastPage()));
      connect(single_page_view_,     SIGNAL(triggered()),   preview_, SLOT(setSinglePageViewMode()));
      connect(facing_pages_view_,    SIGNAL(triggered()),   preview_, SLOT(setFacingPagesViewMode()));
      connect(all_pages_view_,       SIGNAL(triggered()),   preview_, SLOT(setAllPagesViewMode()));
      connect(page_setup_,           SIGNAL(triggered()),   this,     SLOT(pageSetup()));
      
      connect(use_full_page_,        SIGNAL(toggled(bool)), this, SLOT(useFullPage(bool)));
      connect(fit_diagram_to_page_,  SIGNAL(toggled(bool)), this, SLOT(fitDiagramToPage(bool)));
      
      connect(preview_,  SIGNAL(previewChanged()),         this, SLOT(updateZoomList()));
      connect(zoom_box_, SIGNAL(currentIndexChanged(int)), this, SLOT(updatePreviewZoom()));
      
      connect(buttons_, SIGNAL(accepted()), this, SLOT(accept()));
      connect(buttons_, SIGNAL(rejected()), this, SLOT(reject()));
      
      hlayout0_ = new QHBoxLayout();
      vlayout0_ = new QVBoxLayout();
      vlayout1_ = new QVBoxLayout();
      vlayout2_ = new QVBoxLayout();
      
      vlayout1_ -> addWidget(use_full_page_);
      vlayout1_ -> addWidget(use_full_page_label_);
      vlayout1_ -> addWidget(fit_diagram_to_page_);
      vlayout1_ -> addWidget(fit_diagram_to_page_label_);
      print_options_box_ -> setLayout(vlayout1_);
      
      vlayout2_ -> addWidget(diagrams_label_);
      vlayout2_ -> addWidget(diagrams_list_);
      
      hlayout0_ -> addLayout(vlayout2_);
      hlayout0_ -> addWidget(preview_);
      
      vlayout0_ -> addWidget(toolbar_);
      vlayout0_ -> addLayout(hlayout0_);
      vlayout0_ -> addWidget(print_options_box_);
      vlayout0_ -> addWidget(buttons_);
      
      setLayout(vlayout0_);
      updateZoomList();
}

/**
      Ce slot prive emet le signal paintRequested avec :
        * la liste des schemas a imprimer / selectionnes
        * un booleen indiquant s'il faut adapter les schemas aux pages ou non
        * l'imprimante a utiliser
*/
00287 void QETPrintPreviewDialog::requestPaint(QPrinter *printer) {
      emit(
            paintRequested(
                  diagrams_list_ -> selectedDiagrams(),
                  fit_diagram_to_page_ -> isChecked(),
                  printer
            )
      );
}

/**
      Ce slot prive verifie que le nombre de schemas a imprimer est bien superieur
      a 0 et active ou desactive le bouton "Imprimer" en consequence.
*/
00301 void QETPrintPreviewDialog::checkDiagramsCount() {
      int diagrams_count = diagrams_list_ -> selectedDiagrams().count();
      
      // desactive le premier bouton de la liste (= le bouton "Imprimer")
      QList<QAbstractButton *> buttons = buttons_ -> buttons();
      if (buttons.count()) buttons[0] -> setEnabled(diagrams_count);
}

/**
      Ce slot prive affiche ou cache la liste des schemas
      @param display true pour affiche la liste des schemas, false pour la cacher
*/
00313 void QETPrintPreviewDialog::setDiagramsListVisible(bool display) {
      diagrams_label_ -> setVisible(display);
      diagrams_list_  -> setVisible(display);
      
      if (display) {
            toggle_diagrams_list_ -> setText(tr("Cacher la liste des sch\351mas"));
      } else {
            toggle_diagrams_list_ -> setText(tr("Afficher la liste des sch\351mas"));
      }
}

/**
      Ce slot prive affiche ou cache les options d'impression
      @param display true pour affiche les options d'impression, false pour les
      cacher
*/
00329 void QETPrintPreviewDialog::setPrintOptionsVisible(bool display) {
      print_options_box_ -> setVisible(display);
      
      if (display) {
            toggle_print_options_ -> setText(tr("Cacher les options d'impression"));
      } else {
            toggle_print_options_ -> setText(tr("Afficher les options d'impression"));
      }
}

/**
      Met a jour la liste des zooms disponibles
*/
00342 void QETPrintPreviewDialog::updateZoomList() {
      // recupere le zooom courant
      qreal current_zoom = preview_ -> zoomFactor();
      bool current_zoom_is_not_null = bool(int(current_zoom * 100.0));
      
      // liste des zooms par defaut
      QList<qreal> zooms_real;
      zooms_real << 0.25 << 0.5 << 0.75 << 1.0 << 1.5 << 2.0 << 4.0 << 8.0;
      
      // ajout du zoom en cours
      if (current_zoom_is_not_null && (!zooms_real.contains(current_zoom))) {
            zooms_real << current_zoom;
            qSort(zooms_real.begin(), zooms_real.end());
      }
      
      // remplissage de la liste deroulante
      int current_zoom_index = -1;
      zoom_box_ -> blockSignals(true);
      zoom_box_ -> clear();
      foreach (qreal z, zooms_real) {
            zoom_box_ -> addItem(QString(tr("%1 %")).arg(z * 100.0, 0, 'f', 2), z);
            if (z == current_zoom) current_zoom_index = zoom_box_ -> count() - 1;
      }
      zoom_box_ -> setCurrentIndex(current_zoom_index);
      zoom_box_ -> blockSignals(false);
}

/**
      Change le zoom de l'apercu en fonctiopn du contenu du zoom selectionne
*/
00372 void QETPrintPreviewDialog::updatePreviewZoom() {
      preview_ -> setZoomFactor(
            zoom_box_ -> itemData(zoom_box_ -> currentIndex()).toDouble()
      );
      updateZoomList();
}

Generated by  Doxygen 1.6.0   Back to index