All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DialogPropertiesBrowser.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 DialogPropertiesBrowser.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
30 #include "../../../core/property/Properties.h"
31 #include "../../../core/enum/Enums.h"
32 #include "../../../core/Font.h"
33 #include "../../../core/AbstractBuildGraphicsOutside.h"
34 #include "../../../core/pattern/singleton/Context.h"
35 #include "../BuildGraphicsOutside.h"
36 #include "../../outside/GridSettingsOutside.h"
37 #include "../../../outside/GridSettingsModel.h"
38 #include "../ItemUtils.h"
39 
40 // Qt
41 #include <QVariant>
42 #include <QFont>
43 #include <QColor>
44 #include <QImageReader>
45 #include <QFileDialog>
46 #include <QMessageBox>
47 #include <QDesktopWidget>
48 #include <QApplication>
49 #include <QByteArray>
50 
53  m_strDlgManager(0),
54  m_dlgEditorFactory(0)
55 {
56  createManager();
57 }
58 
59 te::layout::DialogPropertiesBrowser::DialogPropertiesBrowser( QtDlgEditorFactory* factory, QtStringPropertyManager* manager, QObject *parent /*= 0*/ ):
61  m_strDlgManager(manager),
62  m_dlgEditorFactory(factory)
63 {
65  {
66  connect(m_dlgEditorFactory, SIGNAL(internalDlg(QWidget *, QtProperty *)), this, SLOT(onSetDlg(QWidget *, QtProperty *)));
67  }
68 }
69 
71 {
72  closeAllWindows();
73 
74  m_dlgProps.clear();
75 
76  if(m_strDlgManager)
77  {
78  delete m_strDlgManager;
79  m_strDlgManager = 0;
80  }
81 
82  if(m_dlgEditorFactory)
83  {
84  delete m_dlgEditorFactory;
85  m_dlgEditorFactory = 0;
86  }
87 }
88 
90 {
91  m_strDlgManager = new QtStringPropertyManager;
92 
93  m_dlgEditorFactory = new QtDlgEditorFactory;
94 
95  // internalDlg is called when an item of property browser tree is clicked
96  connect(m_dlgEditorFactory, SIGNAL(internalDlg(QWidget *, QtProperty *)), this, SLOT(onSetDlg(QWidget *, QtProperty *)));
97 }
98 
99 void te::layout::DialogPropertiesBrowser::onSetDlg( QWidget *parent, QtProperty * prop )
100 {
102 
103  if(!dataType || !parent)
104  {
105  return;
106  }
107 
108  std::string name = prop->propertyName().toStdString();
109 
110  Property propt = findDlgProperty(name);
111 
112  m_currentPropertyClicked = propt;
113 
114  if(propt.getType() == dataType->getDataTypeNone())
115  return;
116 
117  if(propt.getType() == dataType->getDataTypeGridSettings())
118  {
119  connect(parent, SIGNAL(showDlg()), this, SLOT(onShowGridSettingsDlg()));
120  }
121  if(propt.getType() == dataType->getDataTypeImage())
122  {
123  connect(parent, SIGNAL(showDlg()), this, SLOT(onShowImageDlg()));
124  }
125  if(propt.getType() == dataType->getDataTypeTextGridSettings())
126  {
127  connect(parent, SIGNAL(showDlg()), this, SLOT(onShowTextGridSettingsDlg()));
128  }
129 }
130 
132 {
133  return m_strDlgManager;
134 }
135 
137 {
138  return m_dlgEditorFactory;
139 }
140 
142 {
143  QColor qcolor;
144  QFont qfont;
145  te::color::RGBAColor color;
146  Font font;
147 
148  m_changeProperty = true;
149 
150  if(!pproperty)
151  {
152  return false;
153  }
154 
155  m_changeProperty = false;
156 
157  return true;
158 }
159 
161 {
162  QtProperty* qproperty = 0;
163 
165  if(!dataType)
166  {
167  return qproperty;
168  }
169 
170  if(!m_strDlgManager)
171  {
172  return qproperty;
173  }
174 
175  qproperty = m_strDlgManager->addProperty(tr(property.getName().c_str()));
176  m_strDlgManager->setValue(qproperty, property.getValue().toString().c_str());
177  addPropertyItem(qproperty, QLatin1String(property.getName().c_str()));
178  /*The sub properties should not appear in this case,
179  because will be previewed in the dialog window will be opened.*/
180  m_dlgProps.insert(std::pair<std::string, Property>(property.getName(),property));
181 
182  return qproperty;
183 }
184 
186 {
187  bool result = false;
188 
190 
191  if(prop.getType() == dataType->getDataTypeGridSettings()
192  || prop.getType() == dataType->getDataTypeImage()
193  || prop.getType() == dataType->getDataTypeTextGridSettings())
194  {
195  result = true;
196  }
197 
198  return result;
199 }
200 
202 {
203  Property prop;
204 
205  std::string propName;
206  std::map<std::string, Property>::iterator it;
207 
208  for (it = m_dlgProps.begin(); it != m_dlgProps.end(); ++it) {
209  propName = it->first;
210  if(name.compare(propName) == 0)
211  {
212  prop = it->second;
213  break;
214  }
215  }
216 
217  return prop;
218 }
219 
221 {
222  Property prop;
223 
224  Property pro;
225  std::map<std::string, Property>::iterator it;
226 
227  for (it = m_dlgProps.begin(); it != m_dlgProps.end(); ++it) {
228  pro = it->second;
229  if(pro.getType() == dataType)
230  {
231  prop = pro;
232  break;
233  }
234  }
235 
236  return prop;
237 }
238 
239 void te::layout::DialogPropertiesBrowser::changeValueQtPropertyDlg( std::string name, QVariant variant )
240 {
241  if(!m_strDlgManager)
242  {
243  return;
244  }
245 
246  QSet<QtProperty*> list = m_strDlgManager->properties();
247  foreach( QtProperty* prop, list)
248  {
249  if(prop->propertyName().toStdString().compare(name))
250  {
251  m_strDlgManager->setValue(prop, variant.toString());
252  }
253  }
254 }
255 
257 {
259  if(!enumObj)
260  {
261  return;
262  }
263 
264  QWidget* widget = createOutside(enumObj->getGridSettings());
265  if(!widget)
266  {
267  return;
268  }
269 
270  GridSettingsOutside* gridSettings = dynamic_cast<GridSettingsOutside*>(widget);
271  if(!gridSettings)
272  {
273  return;
274  }
275 
276  connect(gridSettings, SIGNAL(updateProperty(Property)), this, SLOT(updateOutside(Property)));
277 
278  GridSettingsModel* model = dynamic_cast<GridSettingsModel*>(gridSettings->getModel());
279  if(!model)
280  {
281  return;
282  }
283 
285  if(!utils)
286  {
287  return;
288  }
289 
290  std::vector<te::layout::Properties*> props = utils->getGridMapProperties();
291 
292  model->setProperties(props);
293 
294  gridSettings->show();
295 }
296 
298 {
299  // Bulding the filter string
300  QString filter = tr("Images") + " ( ";
301  QList<QByteArray> formats = QImageReader::supportedImageFormats();
302  for(int i = 0; i < formats.size() - 1; ++i)
303  filter += "*." + formats[i] + " ";
304  filter += ")";
305 
306  QDesktopWidget* wg = QApplication::desktop();
307  QFileDialog dialog;
308 
309  //Put the dialog in the screen center
310  QRect screen = wg->screenGeometry();
311  dialog.move( screen.center() - dialog.rect().center() );
312 
313  QString path = dialog.getOpenFileName(wg, tr("Select an Image File"), "", filter);
314  if(path.isNull())
315  return;
316 
317  // Try load image
318  QImage img;
319  if(!img.load(path))
320  {
321  QMessageBox::critical(wg, tr("Error"), tr("The selected image cannot be loaded."));
322  return;
323  }
324  else
325  {
327 
328  Property prop = m_currentPropertyClicked;
329  prop.setValue(path.toStdString(), dataType->getDataTypeImage());
330 
331  QVariant v(path);
332  changeValueQtPropertyDlg(prop.getName(), v);
333 
334  emit changeDlgProperty(prop);
335  }
336 }
337 
339 {
340 
341 }
342 
344 {
345  Property prop;
346  prop.setName(name);
347 
349 
350  if(!dataType)
351  {
352  return prop;
353  }
354 
355  QVariant variant = findPropertyValue(name);
356 
357  if(variant.isNull() || !variant.isValid())
358  {
359  return prop;
360  }
361 
362  if(prop.getType() == dataType->getDataTypeGridSettings())
363  {
364  prop.setValue(variant.toString().toStdString(), prop.getType());
365  }
366  else if(prop.getType() == dataType->getDataTypeImage())
367  {
368  prop.setValue(variant.toString().toStdString(), prop.getType());
369  }
370 
371  return prop;
372 }
373 
374 te::layout::EnumType* te::layout::DialogPropertiesBrowser::getLayoutType( QVariant::Type type, std::string name /*= ""*/ )
375 {
376  Property prop;
378  EnumType* dataType = 0;
379 
380  if(!dtType)
381  {
382  return 0;
383  }
384 
385  switch(type)
386  {
387  case QVariant::String:
388  {
389  dataType = dtType->getDataTypeString();
390 
391  //Custom types: Dialog Window Type
392  if(name.compare("") != 0)
393  {
394  prop = findDlgProperty(name);
395  if(!prop.getValue().isNull())
396  {
397  if(prop.getType() == dtType->getDataTypeGridSettings())
398  {
399  dataType = dtType->getDataTypeGridSettings();
400  }
401  if(prop.getType() == dtType->getDataTypeImage())
402  {
403  dataType = dtType->getDataTypeImage();
404  }
405  }
406  }
407  }
408  break;
409  default:
410  prop.setValue(0, dtType->getDataTypeNone());
411  }
412 
413  return dataType;
414 }
415 
417 {
418  int type = QVariant::Invalid;
419 
421 
422  if(!dtType)
423  {
424  return type;
425  }
426 
427  if(dataType == dtType->getDataTypeGridSettings())
428  {
429  type = QVariant::String;
430  }
431  else if(dataType == dtType->getDataTypeImage())
432  {
433  type = QVariant::String;
434  }
435  else
436  {
437  type = QVariant::Invalid;
438  }
439 
440  return type;
441 }
442 
444 {
445  std::string name = property.getName();
446  QtProperty* qprop = findProperty(name);
447  QColor qcolor;
448  te::color::RGBAColor color;
449  Font font;
450  QFont qfont;
451 
452  if(!qprop)
453  {
454  return false;
455  }
456 
457  return changeQtPropertyValue(qprop, property);
458 }
459 
460 std::map<std::string, te::layout::Property> te::layout::DialogPropertiesBrowser::getDlgProps()
461 {
462  return m_dlgProps;
463 }
464 
466 {
467 
468 }
469 
471 {
472  QWidget* widget = 0;
473 
474  if(!enumType)
475  {
476  return widget;
477  }
478 
480  if(!abstractBuild)
481  {
482  return widget;
483  }
484 
485  BuildGraphicsOutside* build = dynamic_cast<BuildGraphicsOutside*>(abstractBuild);
486  if(!build)
487  {
488  return widget;
489  }
490 
491  widget = build->createOuside(enumType);
492  return widget;
493 }
494 
496 {
497  emit changeDlgProperty(prop);
498 }
499 
500 
501 
502 
503 
504 
505 
506 
std::string getName()
Method that returns the name of this property.
Definition: Property.cpp:57
Class to represent a graphic object (MVC component) and widget object (MVC widget) type enumeration...
Class specifies a font.
Definition: Font.h:46
SimpleData< std::string, STRING_TYPE > String
Definition: SimpleData.h:229
virtual Property findDlgProperty(std::string name)
virtual EnumType * getGridSettings() const
Returns value that represents GridSettings window (MVC dialog) type belonging to enumeration.
Variant getValue()
Returns stored value.
Definition: Property.cpp:72
virtual EnumDataType * getEnumDataType()
Returns data type enumeration.
Definition: Enums.cpp:52
virtual std::vector< Properties * > getGridMapProperties()
Get properties of all GridMapItem, children of a MapItem.
Definition: ItemUtils.cpp:412
QWidget * createOuside(te::layout::EnumType *type)
Method to create a widget from the type.
virtual EnumType * getDataTypeNone() const
Returns value that represents type none belonging to enumeration.
virtual void changeValueQtPropertyDlg(std::string name, QVariant variant)
virtual EnumObjectType * getEnumObjectType()
Returns graphic object (MVC component) and widget object (MVC widget) type enumeration.
Definition: Enums.cpp:62
Manage properties dialogs values.
Abstract class for build MVC widgets.
static Enums & getInstance()
It returns a reference to the singleton instance.
Class responsible for creating or building widgets. All objects are children of Qwidget and OutsideOb...
virtual EnumType * getDataTypeGridSettings() const
Returns value that represents type GridSettings (string) belonging to enumeration.
Class to represent a data type enumeration. Ex.: int, double, bool, te::color::RGBAColor (color)...
Definition: EnumDataType.h:48
bool isNull()
Returns true if no value has been set, false otherwise.
Definition: Variant.cpp:351
Window (QDialog) map grid setting (MapItem).
virtual bool changeQtPropertyValue(QtProperty *pproperty, Property property)
ItemUtils * getItemUtils()
Returns pointer for manipulating items in the scene and vectorization of text and legend...
Definition: Context.cpp:273
virtual void onSetDlg(QWidget *parent, QtProperty *prop)
Utility class for manipulating items in the scene and vectorization of text and legend.
Definition: ItemUtils.h:62
virtual EnumType * getDataTypeTextGridSettings() const
Returns value that represents type TextGridSettings (string) belonging to enumeration.
virtual Observable * getModel()
Returns the "Model" part of the MVC.
virtual EnumType * getLayoutType(QVariant::Type type, std::string name="")
AbstractBuildGraphicsOutside * getAbstractBuildGraphicsOutside()
Returns pointer for build graphics MVC components.
Definition: Context.cpp:298
void setValue(ValueType value, EnumType *type)
Stores a copy of value.
Definition: Property.h:298
virtual int getVariantType(EnumType *dataType)
Class that represents the value of an enumeration. An enumeration is made of "1..n" objects EnumType...
Definition: EnumType.h:48
virtual QtProperty * addProperty(Property property)
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
std::string toString()
Returns the value of string type. (The setValue method received a string)
Definition: Variant.cpp:311
virtual void setProperties(std::vector< te::layout::Properties * > properties)
virtual bool updateProperty(Property property)
void setName(std::string name)
Sets the name of this property.
Definition: Property.cpp:62
virtual EnumType * getDataTypeString() const
Returns value that represents type string belonging to enumeration.
std::map< std::string, Property > getDlgProps()
virtual Property getProperty(std::string name)
Manage properties variants values.
virtual EnumType * getDataTypeImage() const
Returns value that represents type image (string) belonging to enumeration.
QtStringPropertyManager * getStringPropertyManager()
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 QWidget * createOutside(EnumType *enumType)