All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MenuBuilder.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 MenuItem.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "MenuBuilder.h"
30 #include "../../../core/pattern/mvc/ItemObserver.h"
31 #include "../../../outside/GridSettingsModel.h"
32 #include "../../../outside/GridSettingsController.h"
33 #include "../../../core/pattern/mvc/OutsideObserver.h"
34 #include "../../outside/GridSettingsOutside.h"
35 #include "../../../core/pattern/mvc/ItemController.h"
36 #include "../../../core/enum/Enums.h"
37 #include "../../../outside/TextGridSettingsModel.h"
38 #include "../../../outside/TextGridSettingsController.h"
39 #include "../../outside/TextGridSettingsOutside.h"
40 #include "../pattern/command/ChangePropertyCommand.h"
41 #include "../../../core/pattern/singleton/Context.h"
42 #include "../Scene.h"
43 #include "PropertiesUtils.h"
44 
45 //STL
46 #include <string>
47 
48 // Qt
49 #include <QAction>
50 #include <QGraphicsItem>
51 #include <QImageReader>
52 #include <QString>
53 #include <QByteArray>
54 #include <QFileDialog>
55 #include <QImage>
56 #include <QMessageBox>
57 #include <QMenu>
58 #include <QWidget>
59 #include <QPoint>
60 #include <QFontDialog>
61 #include <QFont>
62 #include <QColorDialog>
63 #include <QColor>
64 #include <QUndoCommand>
65 #include <QApplication>
66 #include <QRect>
67 #include <QDesktopWidget>
68 
70  DialogPropertiesBrowser(0,0,parent),
71  m_menu(0),
72  m_propUtils(0)
73 {
75  connect(this, SIGNAL(changeDlgProperty(Property)), this, SLOT(onChangeDlgProperty(Property)));
76 }
77 
79 {
80  if(m_propUtils)
81  {
82  delete m_propUtils;
83  m_propUtils = 0;
84  }
85 
86  if(m_menu)
87  {
88  delete m_menu;
89  m_menu = 0;
90  }
91 }
92 
93 void te::layout::MenuBuilder::createMenu( QList<QGraphicsItem*> items )
94 {
95  m_graphicsItems = items;
96 
97  if(m_graphicsItems.empty())
98  return;
99 
100  bool window = false;
101  m_properties = m_propUtils->intersection(items, window);
102 
103  if(!m_properties)
104  return;
105 
106  if(m_menu)
107  {
108  delete m_menu;
109  m_menu = 0;
110  }
111 
112  m_menu = new QMenu();
113  connect(m_menu, SIGNAL(triggered(QAction*)), this, SLOT(onMenuTriggered(QAction*)));
114 
116 
117  foreach(Property prop, m_properties->getProperties())
118  {
119  if(!prop.isMenu() || !prop.isVisible())
120  continue;
121 
122  std::string label = prop.getLabel();
123  if(label.compare("") == 0)
124  label = prop.getName();
125 
126  QAction* action = createAction(label, prop.getName(), prop.getIcon());
127  m_menu->addAction(action);
128  if(prop.getType() == dataType->getDataTypeBool())
129  {
130  action->setCheckable(true);
131  action->setChecked(prop.getValue().toBool());
132  }
133  }
134 }
135 
136 QAction* te::layout::MenuBuilder::createAction( std::string text, std::string objName, std::string icon, std::string tooltip )
137 {
138  QAction *actionMenu = new QAction(text.c_str(), this);
139  actionMenu->setObjectName(objName.c_str());
140 
141  actionMenu->setIcon(QIcon::fromTheme(icon.c_str()));
142  actionMenu->setToolTip(tooltip.c_str());
143 
144  return actionMenu;
145 }
146 
148 {
149  if(!m_menu)
150  return;
151 
152  m_currentPropertyClicked = findMnuProperty(action->objectName().toStdString());
153 
155 
156  if(m_currentPropertyClicked.getType() == dataType->getDataTypeColor())
157  {
158  onShowColorDlg();
159  }
160  else if(m_currentPropertyClicked.getType() == dataType->getDataTypeFont())
161  {
162  onShowFontDlg();
163  }
164  else if(m_currentPropertyClicked.getType() == dataType->getDataTypeGridSettings())
165  {
166  onShowGridSettingsDlg();
167  }
168  else if(m_currentPropertyClicked.getType() == dataType->getDataTypeImage())
169  {
170  onShowImageDlg();
171  }
172  else if(m_currentPropertyClicked.getType() == dataType->getDataTypeBool())
173  {
174  checkedBool(action->isChecked());
175  }
176  else if(m_currentPropertyClicked.getType() == dataType->getDataTypeTextGridSettings())
177  {
178  onShowTextGridSettingsDlg();
179  }
180 }
181 
183 {
184  QWidget* wdg = dynamic_cast<QWidget*>(parent());
185 
186  if(!wdg)
187  return;
188 
190 
191  Property property = m_currentPropertyClicked;
192 
193  if(property.getType() != dataType->getDataTypeFont())
194  return;
195 
196  bool ok = false;
197  Font font;
198  QFont qFont;
199 
200  font = property.getValue().toFont();
201  qFont.setFamily(font.getFamily().c_str());
202  qFont.setPointSize(font.getPointSize());
203  qFont.setBold(font.isBold());
204  qFont.setItalic(font.isItalic());
205  qFont.setUnderline(font.isUnderline());
206  qFont.setStrikeOut(font.isStrikeout());
207  qFont.setKerning(font.isKerning());
208 
209  QFont newFont = QFontDialog::getFont(&ok, qFont, wdg, tr("Select Font"));
210 
211  if (!ok || newFont == qFont)
212  return;
213 
214  font.setFamily(newFont.family().toStdString());
215  font.setPointSize(newFont.pointSize());
216  font.setBold(newFont.bold());
217  font.setItalic(newFont.italic());
218  font.setUnderline(newFont.underline());
219  font.setStrikeout(newFont.strikeOut());
220  font.setKerning(newFont.kerning());
221  property.setValue(font, dataType->getDataTypeFont());
222 
223  emit changeDlgProperty(property);
224 }
225 
227 {
228  QWidget* wdg = dynamic_cast<QWidget*>(parent());
229 
230  if(!wdg)
231  return;
232 
234 
235  Property property = m_currentPropertyClicked;
236 
237  if(property.getType() != dataType->getDataTypeColor())
238  return;
239 
240  bool ok = false;
241  QColor qcolor;
242  te::color::RGBAColor color;
243 
244  color = property.getValue().toColor();
245  qcolor.setRed(color.getRed());
246  qcolor.setGreen(color.getGreen());
247  qcolor.setBlue(color.getBlue());
248  qcolor.setAlpha(color.getAlpha());
249 
250  QRgb oldRgba = qcolor.rgba();
251 
252  QRgb newRgba = QColorDialog::getRgba(oldRgba, &ok, wdg);
253 
254  if (!ok || newRgba == oldRgba)
255  return;
256 
257  qcolor = QColor::fromRgba(newRgba);
258 
259  if(qcolor.isValid())
260  {
261  color.setColor(qcolor.red(), qcolor.green(), qcolor.blue(), qcolor.alpha());
262  property.setValue(color, dataType->getDataTypeColor());
263 
264  emit changeDlgProperty(property);
265  }
266 }
267 
269 {
271 
272  Property property = m_currentPropertyClicked;
273 
274  if(property.getType() != dataType->getDataTypeBool())
275  return;
276 
277  property.setValue(checked, dataType->getDataTypeBool());
278  changePropertyValue(property);
279 }
280 
282 {
283  changePropertyValue(property);
284 }
285 
287 {
289 
290  if(property.getType() == dataType->getDataTypeNone())
291  return;
292 
293  Scene* lScene = dynamic_cast<Scene*>(Context::getInstance().getScene());
294 
295  std::vector<QGraphicsItem*> commandItems;
296  std::vector<Properties*> commandOld;
297  std::vector<Properties*> commandNew;
298 
299  foreach(QGraphicsItem* item, m_graphicsItems)
300  {
301  if (item)
302  {
303  ItemObserver* lItem = dynamic_cast<ItemObserver*>(item);
304  if(lItem)
305  {
306  if(!lItem->getModel())
307  {
308  continue;
309  }
310 
311  Properties* props = new Properties("");
312  Properties* beforeProps = lItem->getModel()->getProperties();
313  Properties* oldCommand = new Properties(*beforeProps);
314  if(props)
315  {
316  props->setObjectName(lItem->getModel()->getProperties()->getObjectName());
317  props->setTypeObj(lItem->getModel()->getProperties()->getTypeObj());
318  props->setHashCode(lItem->getModel()->getHashCode());
319  props->addProperty(property);
320 
321  lItem->getModel()->updateProperties(props);
322  lItem->redraw();
323 
324  if(beforeProps)
325  {
326  beforeProps = lItem->getModel()->getProperties();
327  Properties* newCommand = new Properties(*beforeProps);
328  commandItems.push_back(item);
329  commandOld.push_back(oldCommand);
330  commandNew.push_back(newCommand);
331  }
332  }
333  }
334  }
335  }
336 
337  if(!m_graphicsItems.isEmpty())
338  {
339  QUndoCommand* command = new ChangePropertyCommand(commandItems, commandOld, commandNew);
340  lScene->addUndoStack(command);
341  }
342  lScene->update();
343 }
344 
346 {
347  Property prop;
348 
349  std::map<std::string, Property>::iterator it;
350 
351  foreach( Property pro, m_properties->getProperties())
352  {
353  if(pro.getType() == dataType)
354  {
355  prop = pro;
356  break;
357  }
358  }
359 
360  return prop;
361 }
362 
364 {
365  Property prop;
366 
367  std::map<std::string, Property>::iterator it;
368 
369  foreach( Property pro, m_properties->getProperties())
370  {
371  if(pro.getName().compare(name) == 0)
372  {
373  prop = pro;
374  break;
375  }
376  }
377 
378  return prop;
379 }
380 
381 void te::layout::MenuBuilder::menuExec( int x /*= 0*/, int y /*= 0*/ )
382 {
383  if(!m_menu)
384  return;
385 
386  QPoint pt(x, y);
387  m_menu->exec(pt);
388 }
389 
390 
391 
392 
393 
394 
std::string getName()
Method that returns the name of this property.
Definition: Property.cpp:57
Class specifies a font.
Definition: Font.h:46
virtual bool addProperty(Property property)
Adds the specified property to the set of properties for this object.
Definition: Properties.h:193
void changeDlgProperty(Property property)
bool isBold()
Returns true if font use bold, false otherwise.
Definition: Font.cpp:81
Variant getValue()
Returns stored value.
Definition: Property.cpp:72
virtual EnumDataType * getEnumDataType()
Returns data type enumeration.
Definition: Enums.cpp:52
void setColor(const std::string &hexColor)
It sets the color using a two hexadecimal RGB-encoded color.
Definition: RGBAColor.h:329
bool isStrikeout()
Returns true if font use strikeout, false otherwise.
Definition: Font.cpp:111
void setStrikeout(bool strikeout)
Sets font with strikeout style.
Definition: Font.cpp:106
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295
virtual EnumType * getDataTypeBool() const
Returns value that represents type bool belonging to enumeration.
void setKerning(bool kerning)
Sets font with kerning style.
Definition: Font.cpp:116
virtual void setHashCode(int hashCode)
Sets the hashcode of a MVC component.
Definition: Properties.h:295
int getPointSize()
Returns point size of the font.
Definition: Font.cpp:71
virtual void menuExec(int x=0, int y=0)
virtual EnumType * getDataTypeNone() const
Returns value that represents type none belonging to enumeration.
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
The Properties class represents a persistent set of properties. The Properties can be saved to a file...
Definition: Properties.h:52
void setPointSize(int point)
Sets point size of the font.
Definition: Font.cpp:66
Abstract class to represent an observer. "View" part of MVC component. All classes representing the g...
Definition: ItemObserver.h:52
virtual bool isMenu()
Returns true if property will be used in a menu, false otherwise.
Definition: Property.cpp:233
virtual void onChangeDlgProperty(Property property)
virtual void setObjectName(std::string name)
Sets object name that owns these properties.
Definition: Properties.h:236
virtual Property findMnuProperty(EnumType *dataType)
virtual void onMenuTriggered(QAction *action)
virtual void redraw(bool bRefresh=true)
Redraws the graphic component.
virtual bool isVisible()
Return true if visible, false otherwise.
Definition: Property.cpp:258
PropertiesUtils * m_propUtils
Definition: MenuBuilder.h:98
virtual void setTypeObj(EnumType *type)
Sets object type that owns these properties.
Definition: Properties.h:246
virtual void updateProperties(te::layout::Properties *properties)=0
Updated model state with properties. Reimplement this function in a Observable subclass to provide th...
void setBold(bool bold)
Sets font with bold style.
Definition: Font.cpp:76
static Enums & getInstance()
It returns a reference to the singleton instance.
virtual QAction * createAction(std::string text, std::string objName, std::string icon, std::string tooltip="")
virtual EnumType * getTypeObj()
Returns object type that owns these properties.
Definition: Properties.h:241
MenuBuilder(QObject *parent=0)
Definition: MenuBuilder.cpp:69
Class to represent a data type enumeration. Ex.: int, double, bool, te::color::RGBAColor (color)...
Definition: EnumDataType.h:48
void setFamily(std::string family)
Returns font family name.
Definition: Font.cpp:56
virtual std::string getObjectName()
Returns object name that owns these properties.
Definition: Properties.h:231
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
virtual int getHashCode()=0
Returns the hashcode of a MVC component. Reimplement this function in a Observable subclass to provid...
void setUnderline(bool underline)
Sets font with underline style.
Definition: Font.cpp:96
bool isItalic()
Returns true if font use italic, false otherwise.
Definition: Font.cpp:91
virtual te::layout::Properties * getProperties() const =0
Returns the model state as properties. Reimplement this function in a Observable subclass to provide ...
virtual EnumType * getDataTypeColor() const
Returns value that represents type te::color::RGBAColor** (color) belonging to enumeration.
bool isUnderline()
Returns true if font use underline, false otherwise.
Definition: Font.cpp:101
virtual void checkedBool(bool checked)
Class representing the scene. This scene is child of QGraphicsScene, part of Graphics View Framework...
Definition: Scene.h:80
virtual std::string getIcon()
Definition: Property.cpp:243
virtual void createMenu(QList< QGraphicsItem * > items)
Definition: MenuBuilder.cpp:93
Manage properties dialogs values.
Utility class with functions to facilitate handling of qt properties and properties layout module...
void setValue(ValueType value, EnumType *type)
Stores a copy of value.
Definition: Property.h:298
Class that represents the value of an enumeration. An enumeration is made of "1..n" objects EnumType...
Definition: EnumType.h:48
Utility class with functions to facilitate handling of qt properties and properties layout module...
virtual EnumType * getDataTypeFont() const
Returns value that represents type Font belonging to enumeration.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
virtual Observable * getModel()
Returns the "Model" part of the MVC.
virtual void onShowFontDlg()
void setItalic(bool italic)
Sets font with italic style.
Definition: Font.cpp:86
virtual void addUndoStack(QUndoCommand *command)
Method that insert command Undo/Redo of type AddCommand in the Undo/Redo stack.
Definition: Scene.cpp:313
std::string getFamily()
Sets font family name.
Definition: Font.cpp:61
AbstractScene * getScene()
Returns abstract scene for QGraphicsScene class, part of Graphics View Framework. ...
Definition: Context.cpp:88
Undo/Redo for changes in component properties.
EnumType * getType()
Returns the type of this property.
Definition: Property.cpp:67
A property acts like a attribute member of a object and stores the state of this attribute. A set of properties stores the state of an object. Any data type, not included in the convertValue method in the class te::layout::Variant, it will be by default "std::string" value.
Definition: Property.h:47
virtual void changePropertyValue(Property property)
virtual void onShowColorDlg()
bool isKerning()
Returns true if font use kerning, false otherwise.
Definition: Font.cpp:121
bool toBool()
Returns the value of boolean type. (The setValue method received a boolean)
Definition: Variant.cpp:336
virtual std::string getLabel()
Returns the label of this property.
Definition: Property.cpp:223