All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TitleItem.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 TitleItem.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "TitleItem.h"
30 #include "../../core/pattern/mvc/ItemController.h"
31 #include "../../core/AbstractScene.h"
32 #include "../../core/pattern/mvc/Observable.h"
33 #include "../../../color/RGBAColor.h"
34 #include "../../../qt/widgets/Utils.h"
35 #include "../../../geometry/Envelope.h"
36 #include "../../../common/STLUtils.h"
37 #include "../../core/Font.h"
38 #include "../../core/pattern/singleton/Context.h"
39 #include "../../item/TitleModel.h"
40 #include "../../core/Utils.h"
41 
42 // Qt
43 #include <QTextDocument>
44 #include <QFont>
45 #include <QTextCursor>
46 #include <QTextTableCell>
47 #include <QTextTableFormat>
48 #include <QTextLength>
49 #include <QTextCharFormat>
50 #include <QTextList>
51 
53  TextItem(controller, o)
54 {
55  init();
56 }
57 
59 {
60  if(m_table)
61  {
62  delete m_table;
63  m_table = 0;
64  }
65 }
66 
68 {
69  TitleModel* model = dynamic_cast<TitleModel*>(m_model);
70 
71  if(!model)
72  return;
73 
74  updateDocument();
75 
76  resetEdit();
77 }
78 
80 {
81  if(!m_model)
82  return;
83 
84  if(!document())
85  return;
86 
87  updateDocument();
88 
89  refreshDocument();
90 
91  update();
92 }
93 
95 {
96  TitleModel* model = dynamic_cast<TitleModel*>(m_model);
97 
98  if(!model)
99  return;
100 
101  document()->clear();
102 
103  QTextDocument* doc = document();
104 
105  QTextCursor cursor(doc);
106  cursor.movePosition(QTextCursor::Start);
107 
108  QBrush blackBrush(Qt::SolidPattern);
109 
110  QTextTableFormat tableFormat;
111  tableFormat.setAlignment(Qt::AlignLeft);
112 
113  QVector<QTextLength> constraints;
114 
115  for(int c = 0 ; c < 2 ; ++c)
116  {
117  constraints << QTextLength(QTextLength::PercentageLength, 100);
118  }
119 
120  tableFormat.setColumnWidthConstraints(constraints);
121 
122  tableFormat.setHeaderRowCount(1);
123 
124  m_table = cursor.insertTable(2, 1, tableFormat);
125 
126  QTextCharFormat format = cursor.charFormat();
127  format.setFontPointSize(model->getFont().getPointSize());
128 
129  te::color::RGBAColor headerVtrColor = model->getHeaderVerticalColor();
130  QColor qHeaderVtrColor(headerVtrColor.getRed(), headerVtrColor.getGreen(),
131  headerVtrColor.getBlue(), headerVtrColor.getAlpha());
132 
133  te::color::RGBAColor headerHrzColor = model->getHeaderHorizontalColor();
134  QColor qHeaderHrzColor(headerHrzColor.getRed(), headerHrzColor.getGreen(),
135  headerHrzColor.getBlue(), headerHrzColor.getAlpha());
136 
137  te::color::RGBAColor borderColor = model->getBorderGridColor();
138  QColor qBorderColor(borderColor.getRed(), borderColor.getGreen(),
139  borderColor.getBlue(), borderColor.getAlpha());
140 
141  te::color::RGBAColor evenRowColor = model->getEvenRow();
142  QColor qEvenRowColor(evenRowColor.getRed(), evenRowColor.getGreen(),
143  evenRowColor.getBlue(), evenRowColor.getAlpha());
144 
145  te::color::RGBAColor oddRowColor = model->getOddRow();
146  QColor qOddRowColor(oddRowColor.getRed(), oddRowColor.getGreen(),
147  oddRowColor.getBlue(), oddRowColor.getAlpha());
148 
149  //Header
150  QBrush headerVrt(qHeaderVtrColor);
151  QTextTableCell cellTwo = m_table->cellAt(0, 0);
152  QTextCharFormat fmtTwo = cellTwo.format();
153  fmtTwo.setBackground(headerVrt);
154  cellTwo.setFormat(fmtTwo);
155  QTextCursor cellCursorTwo = cellTwo.firstCursorPosition();
156  std::string title = model->getTitle();
157  cellCursorTwo.insertText(title.c_str(), format);
158 
159  // table
160  QBrush headerHrz(qHeaderHrzColor);
161  QTextTableCell cellOne = m_table->cellAt(1, 0);
162  QTextCharFormat fmtOne = cellOne.format();
163  fmtOne.setBackground(headerHrz);
164  cellOne.setFormat(fmtOne);
165  std::string txt = model->getText();
166  QTextCursor cellCursorOne = cellOne.firstCursorPosition();
167  cellCursorOne.insertText(txt.c_str(), format);
168 }
169 
171 {
172  if(!m_model)
173  return;
174 
175  TitleModel* model = dynamic_cast<TitleModel*>(m_model);
176 
177  if(!model)
178  return;
179 
180  QImage img = createImage();
181  QPointF pp = scenePos();
182  te::gm::Envelope box(pp.x(), pp.y(), pp.x() + img.widthMM(), pp.y() + img.heightMM());
183 
184  model->setBox(box);
185 
186  // Update Model
187 
188  // Header
189  QTextTableCell cellOne = m_table->cellAt(0, 0);
190  QTextCursor cellCursorOne = cellOne.firstCursorPosition();
191  std::string txtOne = cellCursorOne.block().text().toStdString();
192  model->setTitle(txtOne);
193 
194  // Table
195  QTextTableCell cellTwo = m_table->cellAt(1, 0);
196  QTextCursor cellCursorTwo = cellTwo.firstCursorPosition();
197  std::string txtTwo = cellCursorTwo.block().text().toStdString();
198  model->setText(txtTwo);
199 }
Abstract class to represent an observable. "Model" part of MVC component.
Definition: Observable.h:56
Class responsible for maintaining the drawing context of a MVC component. It is always used by the "M...
Definition: ContextItem.h:49
virtual void setText(std::string txt)
Definition: TextModel.cpp:99
virtual te::color::RGBAColor getEvenRow()
Definition: TitleModel.cpp:344
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295
virtual te::color::RGBAColor getOddRow()
Definition: TitleModel.cpp:354
int getPointSize()
Returns point size of the font.
Definition: Font.cpp:71
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
virtual std::string getText()
Definition: TextModel.cpp:104
virtual void updateDocument()
Definition: TitleItem.cpp:94
Abstract class to represent a controller. "Controller" part of MVC component. All classes representin...
virtual void updateObserver(ContextItem context)
Reimplemented from TextItem.
Definition: TitleItem.cpp:79
virtual te::color::RGBAColor getBorderGridColor()
Definition: TitleModel.cpp:384
virtual ~TitleItem()
Destructor.
Definition: TitleItem.cpp:58
virtual void init()
Definition: TitleItem.cpp:67
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
virtual void setTitle(std::string title)
Definition: TitleModel.cpp:269
Class that represents a "Model" part of Title MVC component. Its coordinate system is the same of sce...
Definition: TitleModel.h:52
Class daughter of te::layout::TextItem representing a grid with two cells which can be inserted texts...
virtual te::color::RGBAColor getHeaderVerticalColor()
Definition: TitleModel.cpp:404
virtual std::string getTitle()
Definition: TitleModel.cpp:274
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
TitleItem(ItemController *controller, Observable *o)
Constructor.
Definition: TitleItem.cpp:52
virtual Font getFont()
Definition: TextModel.cpp:109
virtual void setBox(te::gm::Envelope box)
Change the bounding rectangle.
virtual void refreshDocument()
Definition: TitleItem.cpp:170
virtual te::color::RGBAColor getHeaderHorizontalColor()
Definition: TitleModel.cpp:394
Class that represents text. This object is of type QGraphicsTextItem. He is directly editable via use...
Definition: TextItem.h:70