All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HorizontalRuler.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 HorizontalRulerModel.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "HorizontalRuler.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 
38 {
39 
40 }
41 
43 {
44 
45 }
46 
47 void te::layout::HorizontalRuler::drawRuler( QGraphicsView* view, QPainter* painter, double scale )
48 {
49  if(!painter)
50  return;
51 
52  if(!view)
53  return;
54 
55  if(!m_visible)
56  return;
57 
58  m_blockSize = 10;
59  m_middleBlockSize = 5;
60  m_smallBlockSize = 1;
61  m_longLine = 7.;
62  m_mediumLine = 6.5;
63  m_smallLine = 4.5;
64  m_height = 20;
65  m_cornerSize = 20;
66  m_spacingLineText = 9.5;
67 
69  if(!paperConfig)
70  return;
71 
72  QBrush brush = painter->brush();
73 
74  QBrush bhWhite(QColor(255,255,255,255));
75  QBrush bhGreyBack(QColor(145,145,145,255));
76  QBrush bhGreyBox(QColor(180,180,180,255));
77 
78  QPen pen(QColor(0,0,0,255));
79 
80  double zoomFactor = Context::getInstance().getZoomFactor();
81  zoomFactor = 1. / scale; //Keeps the appearance of the ruler to 100%
82 
83  QPointF ll = view->mapToScene(0, view->height());
84  QPointF ur = view->mapToScene(view->width(), 0);
85 
86  double h = 0;
87  double w = 0;
88 
89  paperConfig->getPaperSize(w, h);
90 
91  //Horizontal Ruler
92  QRectF rfH(QPointF(ll.x(), ur.y()), QPointF(ur.x(), ur.y() - m_height * zoomFactor));
93  QRectF rfBackH(QPointF(ll.x() + (m_cornerSize * zoomFactor), ur.y()), QPointF(ur.x(), ur.y() - (m_height - 1.5) * zoomFactor));
94  QRectF rfPaperH(QPointF(0, ur.y()), QPointF(w, ur.y() - (m_height - 1.5) * zoomFactor));
95  QLineF rfLineH(QPointF(ll.x() + (m_cornerSize * zoomFactor), ur.y() - m_height * zoomFactor), QPointF(ur.x(), ur.y() - m_height * zoomFactor));
96 
97  //Rect corner
98  QRectF rfRectCorner(QPointF(ll.x(), ur.y()), QPointF(ll.x() + m_cornerSize * zoomFactor, ur.y() - m_height * zoomFactor));
99 
100  painter->save();
101  painter->setPen(Qt::NoPen);
102 
103  //Horizontal Ruler
104  painter->setBrush(bhGreyBox);
105  painter->drawRect(rfH);
106 
107  painter->setBrush(bhGreyBack);
108  painter->drawRect(rfBackH);
109 
110  painter->setBrush(bhWhite);
111  painter->drawRect(rfPaperH);
112 
113  painter->setBrush(brush);
114 
115  pen.setWidth(0.1);
116  painter->setPen(pen);
117 
118  painter->drawLine(rfLineH);
119 
120  Utils* utils = Context::getInstance().getUtils();
121 
122  double wtxt = 0;
123  double htxt = 0;
124 
125  QFont ft("Arial");
126  ft.setPointSizeF(6);
127  painter->setFont(ft);
128 
129  std::stringstream ss;
130 
131  double llx = rfBackH.bottomLeft().x();
132  double lly = rfBackH.bottomLeft().y();
133  double urx = rfBackH.topRight().x();
134 
135  double y = lly;
136 
137  //Horizontal Ruler Marks
138  QLineF box;
139 
140  for(int i = (int)llx ; i < (int) urx ; ++i)
141  {
142  if((i % (int)(m_blockSize)) == 0)
143  {
144  box = QLineF(QPointF(i, y), QPointF(i, y + m_longLine * zoomFactor));
145 
146  std::stringstream ss;//create a stringstream
147  ss << i;//add number to the stream
148 
149  utils->textBoundingBox(wtxt, htxt, ss.str());
150 
151  QPoint p = view->mapFromScene(QPointF((double)i - (wtxt/2.), y + m_spacingLineText * zoomFactor));
152 
153  //Keeps the size of the text.(Aspect)
154  painter->setMatrixEnabled(false);
155  painter->drawText(p, ss.str().c_str());
156  painter->setMatrixEnabled(true);
157  }
158  else if((i % (int)(m_middleBlockSize)) == 0)
159  {
160  box = QLineF(QPointF(i, y), QPointF(i, y + m_mediumLine * zoomFactor));
161  }
162  else if((i % (int)(m_smallBlockSize)) == 0)
163  {
164  box = QLineF(QPointF(i, y), QPointF(i, y + m_smallLine * zoomFactor));
165  }
166 
167  painter->drawLine(box);
168  }
169 
170  painter->setBrush(bhGreyBox);
171  painter->setPen(Qt::NoPen);
172  painter->drawRect(rfRectCorner);
173 
174  painter->restore();
175 }
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
virtual void drawRuler(QGraphicsView *view, QPainter *painter, double scale)
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
double getZoomFactor()
Returns current zoom factor. Ex.: 0.5 (50%)
Definition: Context.cpp:103
Utility class with functions to manipulate the canvas and conversion between projections.
Definition: Utils.h:61
Class that represents a horizontal ruler with the coordinate system in mm.