All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
VerticalRuler.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 VerticalRuler.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "VerticalRuler.h"
30 #include "../../core/PaperConfig.h"
31 #include "../../core/pattern/singleton/Context.h"
32 #include "../../core/Utils.h"
33 
34 // STL
35 #include <sstream>
36 
37 // Qt
38 #include <QTransform>
39 #include <QPainter>
40 
42 {
43 
44 }
45 
47 {
48 
49 }
50 
51 void te::layout::VerticalRuler::drawRuler( QGraphicsView* view, QPainter* painter, double scale )
52 {
53  if(!painter)
54  return;
55 
56  if(!view)
57  return;
58 
59  if(!m_visible)
60  return;
61 
63  if(!paperConfig)
64  return;
65 
66  m_blockSize = 10;
67  m_middleBlockSize = 5;
68  m_smallBlockSize = 1;
69  m_longLine = 7.;
70  m_mediumLine = 6.5;
71  m_smallLine = 4.5;
72  m_height = 20;
73  m_cornerSize = 20;
74  m_spacingLineText = 9.5;
75 
76  QBrush brush = painter->brush();
77 
78  QBrush bhWhite(QColor(255,255,255,255));
79  QBrush bhGreyBack(QColor(145,145,145,255));
80  QBrush bhGreyBox(QColor(180,180,180,255));
81 
82  QPen pen(QColor(0,0,0,255));
83 
84  double zoomFactor = 1. / scale; //Keeps the appearance of the ruler to 100%
85 
86  QPointF ll = view->mapToScene(0, view->height());
87  QPointF ur = view->mapToScene(view->width(), 0);
88 
89  double h = 0;
90  double w = 0;
91 
92  paperConfig->getPaperSize(w, h);
93 
94  //Vertical Ruler
95  QRectF rfV(QPointF(ll.x(), ll.y()), QPointF(ll.x() + m_height * zoomFactor, ur.y()));
96  QRectF rfBackV(QPointF(ll.x(), ll.y()), QPointF(ll.x() + (m_cornerSize - 1.5) * zoomFactor, ur.y() - ((m_height) * zoomFactor)));
97  QRectF rfPaperV(QPointF(ll.x(), 0), QPointF(ll.x() + (m_cornerSize - 1.5) * zoomFactor, h));
98  QLineF rfLineV(QPointF(ll.x() + m_cornerSize * zoomFactor, ll.y()), QPointF(ll.x() + m_height * zoomFactor, ur.y() - (m_height * zoomFactor)));
99 
100  //Rect corner
101  QRectF rfRectCorner(QPointF(ll.x(), ur.y()), QPointF(ll.x() + m_cornerSize * zoomFactor, ur.y() - m_height * zoomFactor));
102 
103  painter->save();
104  painter->setPen(Qt::NoPen);
105 
106  //Vertical Ruler
107  painter->setBrush(bhGreyBox);
108  painter->drawRect(rfV);
109 
110  painter->setBrush(bhGreyBack);
111  painter->drawRect(rfBackV);
112 
113  painter->setBrush(bhWhite);
114  painter->drawRect(rfPaperV);
115 
116  painter->setBrush(brush);
117 
118  pen.setWidth(0.1);
119  painter->setPen(pen);
120 
121  painter->drawLine(rfLineV);
122 
123  Utils* utils = Context::getInstance().getUtils();
124 
125  double wtxt = 0;
126  double htxt = 0;
127 
128  QFont ft("Arial");
129  ft.setPointSizeF(6);
130  painter->setFont(ft);
131 
132  double urx = rfBackV.topRight().x();
133  double ury = rfBackV.bottomLeft().y();
134  double lly = rfBackV.topRight().y();
135 
136  double x = urx;
137 
138  //Horizontal Ruler Marks
139  QLineF box;
140 
141  for(int i = (int)lly ; i < (int) ury ; ++i)
142  {
143  if((i % (int)m_blockSize) == 0)
144  {
145  box = QLineF(QPointF(x, i), QPointF(x - m_longLine * zoomFactor, i));
146 
147  std::stringstream ss;//create a stringstream
148  ss << i;//add number to the stream
149 
150  utils->textBoundingBox(wtxt, htxt, ss.str());
151  QPointF pTranslate = QPointF(x - m_spacingLineText * zoomFactor, i);
152 
153  QPointF p1 = view->mapFromScene(pTranslate);
154  p1.setY(p1.y() + wtxt/2.);
155 
156  painter->save();
157 
158  QTransform m;
159  m.translate( p1.x(), p1.y() );
160  m.rotate(-90);
161  m.translate( -p1.x(), -p1.y() );
162 
163  painter->setMatrixEnabled(false);
164  painter->setTransform(m);
165  painter->setFont(ft);
166  painter->drawText(p1, ss.str().c_str());
167 
168  painter->restore();
169  }
170  else if((i % (int)m_middleBlockSize) == 0)
171  {
172  box = QLineF(QPointF(x, i), QPointF(x - m_mediumLine * zoomFactor, i));
173  }
174  else if((i % (int)m_smallBlockSize) == 0)
175  {
176  box = QLineF(QPointF(x, i), QPointF(x - m_smallLine * zoomFactor, i));
177  }
178 
179  painter->drawLine(box);
180  }
181 
182  painter->setBrush(bhGreyBox);
183  painter->setPen(Qt::NoPen);
184  painter->drawRect(rfRectCorner);
185 
186  painter->restore();
187 }
virtual void drawRuler(QGraphicsView *view, QPainter *painter, double scale)
Class responsible for paper setting. Size, orientation, custom size, etc.
Definition: PaperConfig.h:45
virtual void textBoundingBox(double &w, double &h, std::string txt)
A method that calculates the height and width of a text.
Definition: Utils.cpp:271
static Context & getInstance()
It returns a reference to the singleton instance.
Utils * getUtils()
Returns pointer with functions to manipulate the canvas and conversion between projections.
Definition: Context.cpp:153
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
Utility class with functions to manipulate the canvas and conversion between projections.
Definition: Utils.h:61
Class that represents a vertical ruler with the coordinate system in mm.