All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DrawLayerThread.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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 DrawLayerThread.cpp
22 
23  \brief This class represents a thread responsible to draw a given layer. Basically, this class receives draw layer requests and generates a QImage as result.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/Exception.h"
28 #include "../../../geometry/Envelope.h"
29 #include "../../../maptools/AbstractLayer.h"
30 #include "DrawLayerThread.h"
31 #include "Canvas.h"
32 
34  : QThread(parent),
35  m_interval(100)
36 {
37  // Signals & slots
38  connect(&m_feedback, SIGNAL(timeout()), SLOT(sendFeedback()));
39  connect(this, SIGNAL(started()), SLOT(onStarted()));
40  connect(this, SIGNAL(finished()), SLOT(onFinished()));
41 }
42 
44 {
45  wait();
46 }
47 
48 void te::qt::widgets::DrawLayerThread::draw(te::map::AbstractLayer* layer, const te::gm::Envelope& box, int srid, const QSize& size, const int& index)
49 {
50  /* Note: For while... Actually, I would like to can stop the current draw process and restart with the new request box! te::common::TaskProgress?! */
51  if(isRunning())
52  return;
53 
54  // Storing the values
55  m_layer = layer;
56  m_env = box;
57  m_srid = srid;
58  m_index = index;
59 
60  m_finishedWithSuccess = true;
61  m_errorMessage.clear();
62 
63  // Creates the result image
64  if(m_image.size() != size)
65  {
66  m_image = QImage(size, QImage::Format_ARGB32_Premultiplied);
67  m_image.fill(qRgba(0, 0, 0, 0));
68  }
69 
70  // Requests the thread execution!
71  start();
72 }
73 
75 {
76  return m_finishedWithSuccess;
77 }
78 
80 {
81  return m_errorMessage;
82 }
83 
85 {
86  return m_layer;
87 }
88 
90 {
91  // Prepares the canvas
92  Canvas canvas(&m_image);
93  //canvas.setRenderHint(QPainter::Antialiasing);
94  canvas.calcAspectRatio(m_env.m_llx, m_env.m_lly, m_env.m_urx, m_env.m_ury);
95  canvas.setWindow(m_env.m_llx, m_env.m_lly, m_env.m_urx, m_env.m_ury);
96  canvas.clear();
97 
98  while(true)
99  {
100  // Let's draw!
101  try
102  {
103  m_layer->draw(&canvas, m_env, m_srid);
104  break;
105  }
106  catch(const te::da::Exception& e)
107  {
109  {
110  m_finishedWithSuccess = false;
111  m_errorMessage = QString(tr("The layer") + " %1 " + tr("could not be drawn! Details:") + " %2").arg(m_layer->getTitle().c_str()).arg(e.what());
112  break;
113  }
114  msleep(100);
115  }
116  catch(const std::exception& e)
117  {
118  m_finishedWithSuccess = false;
119  m_errorMessage = QString(tr("The layer") + " %1 " + tr("could not be drawn! Details:") + " %2").arg(m_layer->getTitle().c_str()).arg(e.what());
120  break;
121  }
122  }
123 }
124 
126 {
127  m_feedback.start(m_interval);
128 }
129 
131 {
132  emit feedback(m_image);
133 }
134 
136 {
137  emit drawLayerFinished(m_index, m_image);
138  m_image.fill(qRgba(0, 0, 0, 0));
139  m_feedback.stop();
140 }
QTimer m_feedback
Timer used to send feedback. The feedback will be sent right after timeout() QTimer&#39;s signal...
This is the base class for layers.
Definition: AbstractLayer.h:76
bool finishedWithSuccess() const
This method tells if the thread finished with success.
DrawLayerThread(QObject *parent=0)
It constructs a new thread responsible to draw a given layer.
A canvas built on top of Qt.
void draw(te::map::AbstractLayer *layer, const te::gm::Envelope &box, int srid, const QSize &size, const int &index)
This method is used to request the draw of the given layer.
A canvas built on top of Qt.
Definition: Canvas.h:54
void calcAspectRatio(double &llx, double &lly, double &urx, double &ury, const te::map::AlignType hAlign=te::map::Center, const te::map::AlignType vAlign=te::map::Center)
It calculates the best aspect ratio for world (or window) coordinates area (supposing a cartesian ref...
Definition: Canvas.cpp:160
void clear()
It clears the canvas content and fills with the background color.
Definition: Canvas.cpp:222
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void setWindow(const double &llx, const double &lly, const double &urx, const double &ury)
It sets the world (or window) coordinates area (supposing a cartesian reference system).
Definition: Canvas.cpp:147
QString getErrorMessage() const
This method returns an error message if the thread has not finished with success. Otherwise...
This class represents a thread responsible to draw a given layer. Basically, this class receives draw...
te::map::AbstractLayer * getLayer() const
This method returns the layer handled by this thread.