All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SymbolSelectorDialog.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 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 terralib/qt/widgets/se/SymbolSelectorDialog.cpp
22 
23  \brief A dialog used to select a specific symbol.
24 */
25 
26 // TerraLib
27 #include "../../../common/Exception.h"
28 #include "../../../serialization/qt/widgets/Symbol.h"
29 #include "Symbol.h"
30 #include "SymbolInfoDialog.h"
31 #include "SymbolLibrary.h"
32 #include "SymbolLibraryManager.h"
33 #include "SymbolPreviewWidget.h"
34 #include "SymbologyPreview.h"
35 #include "SymbolSelectorDialog.h"
36 #include "ui_SymbolSelectorDialogForm.h"
37 
38 // Qt
39 #include <QtGui/QFileDialog>
40 #include <QtGui/QMessageBox>
41 
42 // STL
43 #include <cassert>
44 
46  : QDialog(parent, f),
47  m_ui(new Ui::SymbolSelectorDialogForm)
48 {
49  // Setup
50  m_ui->setupUi(this);
51  m_ui->m_symbolLibraryTreeWidget->setIconSize(QSize(32, 32));
52 
53  // Preview
54  m_preview = new SymbolPreviewWidget(QSize(120, 120), this);
55 
56  // Adjusting...
57  QGridLayout* previewLayout = new QGridLayout(m_ui->m_previewGroupBox);
58  previewLayout->addWidget(m_preview);
59 
60  // Signals & slots
61  connect(m_ui->m_symbolLibraryTreeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), SLOT(onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
62  connect(m_ui->m_showSymbolInfoPushButton, SIGNAL(pressed()), SLOT(onShowSymbolInfoPushButtonPressed()));
63  connect(m_ui->m_loadSymbolLibraryPushButton, SIGNAL(pressed()), SLOT(onLoadSymbolLibraryPushButtonPressed()));
64  connect(m_ui->m_searchLineEdit, SIGNAL(textChanged(const QString&)), SLOT(onSearchLineEditTextChanged(const QString&)));
65 
66  initialize();
67 }
68 
70 {
71 }
72 
74 {
75  SymbolSelectorDialog dlg(parent);
76 
77  if(!title.isEmpty())
78  dlg.setWindowTitle(title);
79 
80  if(dlg.exec() == QDialog::Accepted)
81  return dlg.getSymbol();
82 
83  return 0;
84 }
85 
87 {
88  Symbol* symbol = getSelectedSymbol();
89 
90  if(symbol == 0)
91  return 0;
92 
93  return symbol->clone();
94 }
95 
96 void te::qt::widgets::SymbolSelectorDialog::onCurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
97 {
98  Symbol* symbol = getSymbolFromItem(current);
99 
100  if(symbol == 0)
101  return;
102 
103  m_preview->updatePreview(symbol);
104 }
105 
107 {
108  Symbol* symbol = getSelectedSymbol();
109 
110  if(symbol == 0)
111  return;
112 
113  SymbolInfoDialog info;
114  info.setSymbolInfo(symbol->getInfo());
115  info.setReadMode();
116 
117  info.exec();
118 }
119 
121 {
122  QString path = QFileDialog::getOpenFileName(this, tr("Select a TerraLib Symbol Library File"), "", "XML (.xml)");
123  if(path.isNull())
124  return;
125 
126  try
127  {
128  // te::serialize::ReadSymbolLibrary(path.toStdString());
129  // initialize();
130  }
131  catch(te::common::Exception& e)
132  {
133  QString message = tr("The selected symbol library could not be loaded.\n Details: )");
134  message += e.what();
135  QMessageBox::critical(this, tr("Error"), message);
136  }
137 }
138 
140 {
141  QList<QTreeWidgetItem*> items = m_ui->m_symbolLibraryTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 0);
142  items.append(m_ui->m_symbolLibraryTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 1));
143  items.append(m_ui->m_symbolLibraryTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 2));
144 
145  filter(items);
146 }
147 
149 {
150  // Gets the loaded libraries
151  std::pair<std::map<std::string, SymbolLibrary*>::const_iterator,
152  std::map<std::string, SymbolLibrary*>::const_iterator> iteratorsLibrary = SymbolLibraryManager::getInstance().getIterator();
153 
154  std::map<std::string, SymbolLibrary*>::const_iterator itLibrary;
155  for(itLibrary = iteratorsLibrary.first; itLibrary != iteratorsLibrary.second; ++itLibrary) // for each library
156  {
157  QString libraryName = QString::fromStdString(itLibrary->second->getName());
158 
159  QTreeWidgetItem* libraryItem = new QTreeWidgetItem(m_ui->m_symbolLibraryTreeWidget, LIBRARY);
160  libraryItem->setText(0, libraryName);
161  libraryItem->setData(0, Qt::UserRole, libraryName);
162 
163  std::pair<std::map<std::string, Symbol*>::const_iterator,
164  std::map<std::string, Symbol*>::const_iterator> iteratorsSymbol = itLibrary->second->getIterator();
165 
166  std::map<std::string, Symbol*>::const_iterator itSymbol;
167 
168  for(itSymbol = iteratorsSymbol.first; itSymbol != iteratorsSymbol.second; ++itSymbol) // for each Symbol
169  {
170  // The current symbol
171  Symbol* symbol = itSymbol->second;
172 
173  // Bulding the Qt symbol item
174  QTreeWidgetItem* symbolItem = new QTreeWidgetItem(libraryItem, SYMBOL);
175  symbolItem->setText(0, symbol->getInfo().m_name.c_str());
176  symbolItem->setIcon(0, SymbologyPreview::build(symbol->getSymbolizers(), m_ui->m_symbolLibraryTreeWidget->iconSize()));
177  symbolItem->setToolTip(0, formatSymbolInfo(symbol->getInfo()));
178  symbolItem->setData(0, Qt::UserRole, QVariant(QString(symbol->getInfo().m_id.c_str())));
179  symbolItem->setText(1, symbol->getInfo().m_author.c_str());
180  symbolItem->setText(2, symbol->getInfo().m_tags.c_str());
181  symbolItem->setText(3, symbol->getInfo().m_description.c_str());
182  }
183  }
184 
185  m_ui->m_symbolLibraryTreeWidget->sortItems(0, Qt::AscendingOrder);
186  m_ui->m_symbolLibraryTreeWidget->resizeColumnToContents(0);
187  m_ui->m_symbolLibraryTreeWidget->expandAll();
188 }
189 
190 void te::qt::widgets::SymbolSelectorDialog::filter(const QList<QTreeWidgetItem*>& items)
191 {
192  for(int i = 0; i < m_ui->m_symbolLibraryTreeWidget->topLevelItemCount(); ++i)
193  {
194  QTreeWidgetItem* library = m_ui->m_symbolLibraryTreeWidget->topLevelItem(i);
195  assert(library && library->type() == LIBRARY);
196 
197  for(int j = 0; j < library->childCount(); ++j)
198  {
199  QTreeWidgetItem* symbol = library->child(j);
200  assert(symbol && symbol->type() == SYMBOL);
201  bool hide = items.indexOf(symbol) == -1;
202  symbol->setHidden(hide);
203  }
204  }
205  update();
206 }
207 
209 {
210  QList<QTreeWidgetItem*> selected = m_ui->m_symbolLibraryTreeWidget->selectedItems();
211 
212  if(selected.empty())
213  return 0;
214 
215  return getSymbolFromItem(selected.at(0));
216 }
217 
219 {
220  assert(item);
221 
222  if(item->type() != SYMBOL)
223  return 0;
224 
225  // Gets the library
226  QTreeWidgetItem* parent = item->parent();
227  assert(parent);
228 
229  QString name = parent->data(0, Qt::UserRole).toString();
230  assert(!name.isEmpty());
231 
232  SymbolLibrary* symbolLibrary = SymbolLibraryManager::getInstance().findByName(name.toStdString());
233  assert(symbolLibrary);
234 
235  // Gets the symbol
236  QString id = item->data(0, Qt::UserRole).toString();
237  assert(!id.isEmpty());
238 
239  Symbol* symbol = symbolLibrary->findById(id.toStdString());
240  assert(symbol);
241 
242  return symbol;
243 }
244 
246 {
247  QString information("<h3>Symbol Information</h3><ul>");
248  information += "<li><b>Name: </b>" + QString(info.m_name.c_str()) + "</li>";
249  information += "<li><b>Author: </b>" + QString(info.m_author.c_str()) + "</li>";
250  information += "<li><b>Tags: </b>" + QString(info.m_tags.c_str()) + "</li>";
251  information += "<li><b>Description: </b>" + QString(info.m_description.c_str()) + "</li>";
252  information += "</ul>";
253 
254  return information;
255 }
static QPixmap build(const te::se::Symbolizer *symb, const QSize &size)
Generates the preview of given symbolizer element.
Symbol * clone() const
It creates a new copy of this object.
Definition: Symbol.cpp:120
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
void setReadMode(bool enable=true)
SymbolPreviewWidget * m_preview
Preview Widget used to visualize the symbol.
void setSymbolInfo(const SymbolInfo &info)
Sets the symbol information to this dialog.
A widget used to preview symbol elements.
void onCurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
void onSearchLineEditTextChanged(const QString &text)
Symbol * getSymbolFromItem(QTreeWidgetItem *item) const
Auxiliary internal method that returns the symbol given a list widget item.
virtual const char * what() const
It outputs the exception message.
Definition: Exception.cpp:58
The SymbolLibraryManager is a singleton that can be used to manage all loaded symbol libraries in Ter...
This class represents a symbol. TODO: More description!
Definition: Symbol.h:54
std::pair< std::map< std::string, SymbolLibrary * >::const_iterator, std::map< std::string, SymbolLibrary * >::const_iterator > getIterator() const
It returns a pair of iterators over the symbol libraries of this manager.
A dialog used to select a specific symbol.
void filter(const QList< QTreeWidgetItem * > &items)
Auxiliary internal method to filter the symbols.
This class represents a library of symbols.
Definition: SymbolLibrary.h:52
Symbol * getSymbol() const
Gets the selected symbol.
A dialog used to configure informations about a symbol.
const SymbolInfo & getInfo() const
It return the information associated to the symbol.
Definition: Symbol.cpp:50
std::auto_ptr< Ui::SymbolSelectorDialogForm > m_ui
Dialog form.
Symbol * getSelectedSymbol() const
Auxiliary internal method that returns the selected symbol.
A dialog used to select a specific symbol.
Static class used to generate preview of Symbology elements.
This class represents a symbol.
Information about a given Symbol.
Definition: SymbolInfo.h:60
SymbolLibrary * findByName(const std::string &name) const
It returns the symbol library identified by a given name or NULL if none is found.
QString formatSymbolInfo(const SymbolInfo &info) const
Auxiliary internal method to format a symbol info to be used on tool tips.
static Symbol * getSymbol(QWidget *parent, const QString &title="")
Pops up a modal symbol selector dialog with the given window title, lets the user select a symbol...
static SymbolLibraryManager & getInstance()
It returns a reference to the singleton instance.
This class represents a library of symbols.
A dialog used to configure informations about a symbol.
A widget used to preview symbol elements.
SymbolSelectorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a symbol selector dialog which is a child of parent, with widget flags set to f...
void initialize()
Initialize the dialog.
const std::vector< te::se::Symbolizer * > & getSymbolizers() const
It returns the list of Symbolizers that compose the symbol.
Definition: Symbol.cpp:77