All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PrintScene.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib 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 Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file PrintScene.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "PrintScene.h"
30 #include "../../core/pattern/mvc/ItemObserver.h"
31 #include "../../core/PaperConfig.h"
32 #include "../../core/pattern/singleton/Context.h"
33 #include "Scene.h"
34 
35 // STL
36 #include <sstream>
37 
38 // Qt
39 #include <QGraphicsScene>
40 #include <QGraphicsItem>
41 #include <QPrinter>
42 #include <QPainter>
43 #include <QPrintPreviewDialog>
44 #include <QFileDialog>
45 #include <QApplication>
46 #include <QMessageBox>
47 
48 te::layout::PrintScene::PrintScene( QGraphicsScene* scene, PaperConfig* config ):
49  m_scene(scene),
50  m_config(config),
51  m_printState(te::layout::NoPrinter)
52 {
53 
54 }
55 
57 {
58 
59 }
60 
62 {
63  if(!m_scene)
64  return;
65 
66  QPrinter* printer = createPrinter();
67 
68  QPrintPreviewDialog *preview = new QPrintPreviewDialog(printer);
69  connect(preview, SIGNAL(paintRequested(QPrinter*)), SLOT(printPaper(QPrinter*)));
70 
71  if(preview->exec() == QDialog::Rejected || m_printState == te::layout::PrintingScene)
72  {
73  if(printer)
74  {
75  delete printer;
76  printer = 0;
77  }
78  }
79 
80  if(preview)
81  {
82  delete preview;
83  preview = 0;
84 
85  Scene* sc = dynamic_cast<Scene*>(m_scene);
86  if(sc)
87  {
88  sc->redrawItems();
89  }
90  }
91 }
92 
93 void te::layout::PrintScene::printPaper( QPrinter* printer )
94 {
95  if(!printer)
96  return;
97 
98  //Impressão de parte da Cena
99  //Não é necessário mudar a escala do View
100 
101  QPainter newPainter(printer);
102  newPainter.setRenderHint(QPainter::Antialiasing);
103 
104  double dpiX = Context::getInstance().getDpiX();
105  double dpiY = Context::getInstance().getDpiY();
106 
107  Context::getInstance().setDpiX(printer->logicalDpiX());
108  Context::getInstance().setDpiY(printer->logicalDpiY());
109 
110  Scene* sc = dynamic_cast<Scene*>(m_scene);
111  if(!sc)
112  return;
113 
114  double zoomFactor = Context::getInstance().getZoomFactor();
116  sc->onChangeZoomFactor(1.);
117 
118  renderScene(&newPainter, printer);
119 
120  Context::getInstance().setZoomFactor(zoomFactor);
123  sc->onChangeZoomFactor(zoomFactor);
124 }
125 
127 {
128  if(!m_scene)
129  return 0;
130 
132 
133  if(!conf)
134  return 0;
135 
136  double w = 0;
137  double h = 0;
138  conf->getPaperSize(w, h);
139 
140  QPrinter* printer = new QPrinter(QPrinter::HighResolution);
141  QSizeF sf(w, h);
142  printer->setPaperSize(sf, QPrinter::Millimeter);
143 
144  if(conf->getPaperOrientantion() == Portrait)
145  {
146  printer->setOrientation( QPrinter::Portrait );
147  }
148  else
149  {
150  printer->setOrientation( QPrinter::Landscape );
151  }
152  printer->pageRect(QPrinter::Millimeter);
153 
154  return printer;
155 }
156 
157 void te::layout::PrintScene::renderScene( QPainter* newPainter, QPrinter* printer )
158 {
159  if(!m_scene)
160  return;
161 
162  if(!newPainter)
163  return;
164 
165  if(!printer)
166  return;
167 
168  if(m_printState == NoPrinter)
169  {
170  m_printState = PreviewScene;
171  }
172  else if(m_printState == PreviewScene)
173  {
174  m_printState = PrintingScene;
175  }
176 
177  double w = 0;
178  double h = 0;
179 
181  conf->getPaperSize(w, h);
182 
183  //Box Paper in the Scene (Source)
184  QRectF mmSourceRect(0, 0, w, h);
185 
186  //Paper size using the printer dpi (Target)
187  QRect pxTargetRect(0, 0, newPainter->device()->width(), newPainter->device()->height());
188 
189  /* Print Paper (Scene to Printer)
190  draw items with printer painter */
191 
192  //Convert Paper Size world to screen coordinate. Uses dpi printer.
193  QSizeF paperPixelBox = printer->paperSize(QPrinter::DevicePixel);
194 
195  //Mirroring Y-Axis
196  newPainter->translate( paperPixelBox.width() / 2, paperPixelBox.height() / 2 );
197  newPainter->scale( 1, -1 );
198  newPainter->translate( -(paperPixelBox.width() / 2), -(paperPixelBox.height() / 2) );
199 
200  deselectAllItems();
201 
202  m_scene->render(newPainter, pxTargetRect, mmSourceRect);
203 }
204 
206 {
207  QWidget* wg = (QWidget*)QApplication::desktop();
208  QString fileName = QFileDialog::getSaveFileName(wg, tr("Save Image File"), QDir::currentPath(), tr("PDF Files (*.pdf)"));
209 
210  if(fileName.isEmpty())
211  {
212  return false;
213  }
214 
215  QPrinter* printer= createPrinter();
216 
217  printer->setOutputFormat(QPrinter::PdfFormat);
218  printer->setOutputFileName(fileName);
219  printPaper(printer);
220 
221  if(printer)
222  {
223  delete printer;
224  printer = 0;
225  }
226 
227  QMessageBox msgBox;
228  msgBox.setIcon(QMessageBox::Information);
229  msgBox.setText("PDF exported successfully!");
230  msgBox.exec();
231 
232  return true;
233 }
234 
236 {
237  if(!m_scene)
238  return;
239 
240  QList<QGraphicsItem*> graphicsItems = m_scene->items();
241  foreach( QGraphicsItem *item, graphicsItems)
242  {
243  if (item)
244  {
245  item->setSelected(false);
246  }
247  }
248 }
landScape orientation
Definition: AbstractType.h:86
PreviewScene
Definition: AbstractType.h:48
void setDpiY(double dpiY)
Definition: Context.cpp:188
Class responsible for paper setting. Size, orientation, custom size, etc.
Definition: PaperConfig.h:45
virtual bool exportToPDF()
Definition: PrintScene.cpp:205
PrintingScene
Definition: AbstractType.h:48
void setZoomFactor(double zoomFactor)
Change current zoom factor. Ex.: 0.5 (50%)
Definition: Context.cpp:108
Class representing the scene. This scene is child of QGraphicsScene, part of Graphics View Framework...
PrintScene(QGraphicsScene *scene, PaperConfig *config)
Definition: PrintScene.cpp:48
portrait orientation
Definition: AbstractType.h:85
virtual LayoutOrientationType getPaperOrientantion()
Returns paper orientation type enum.
void setDpiX(double dpiX)
Definition: Context.cpp:178
virtual void onChangeZoomFactor(double currentFactor)
It is called immediately when the zoom factor is changed in the Context.
Definition: Scene.cpp:794
static Context & getInstance()
It returns a reference to the singleton instance.
virtual void renderScene(QPainter *newPainter, QPrinter *printer)
Definition: PrintScene.cpp:157
virtual void printPreview()
Definition: PrintScene.cpp:61
virtual void redrawItems()
Definition: Scene.cpp:759
virtual void printPaper(QPrinter *printer)
Definition: PrintScene.cpp:93
Class representing the scene. This scene is child of QGraphicsScene, part of Graphics View Framework...
Definition: Scene.h:80
virtual void getPaperSize(double &w, double &h)
Returns paper size. Height and Width.
Definition: PaperConfig.cpp:61
PaperConfig * getPaperConfig() const
Returns paper setting.
Definition: Context.cpp:198
virtual void deselectAllItems()
Definition: PrintScene.cpp:235
double getZoomFactor()
Returns current zoom factor. Ex.: 0.5 (50%)
Definition: Context.cpp:103
virtual QPrinter * createPrinter()
Definition: PrintScene.cpp:126
Class responsible for printing the entire content or part of the scene. As the scene is upside down...