All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SymbolSelectorDialog.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 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 "Symbol.h"
29 #include "SymbolInfoDialog.h"
30 #include "SymbolLibrary.h"
31 #include "SymbolLibraryManager.h"
32 #include "SymbolPreviewWidget.h"
33 #include "SymbologyPreview.h"
34 #include "SymbolSelectorDialog.h"
35 #include "ui_SymbolSelectorDialogForm.h"
36 #include "Utils.h"
37 
38 // Qt
39 #include <QFileDialog>
40 #include <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  // For while, hide symbol edit option
67  m_ui->m_editSymbolPushButton->setVisible(false);
68 
69  initialize();
70 }
71 
73 {
74 }
75 
77 {
78  SymbolSelectorDialog dlg(parent);
79 
80  if(!title.isEmpty())
81  dlg.setWindowTitle(title);
82 
83  if(dlg.exec() == QDialog::Accepted)
84  return dlg.getSymbol();
85 
86  return 0;
87 }
88 
90 {
91  Symbol* symbol = getSelectedSymbol();
92 
93  if(symbol == 0)
94  return 0;
95 
96  return symbol->clone();
97 }
98 
99 void te::qt::widgets::SymbolSelectorDialog::onCurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
100 {
101  Symbol* symbol = getSymbolFromItem(current);
102 
103  if(symbol == 0)
104  return;
105 
106  m_preview->updatePreview(symbol);
107 }
108 
110 {
111  Symbol* symbol = getSelectedSymbol();
112 
113  if(symbol == 0)
114  return;
115 
116  SymbolInfoDialog info;
117  info.setSymbolInfo(symbol->getInfo());
118  info.setReadMode();
119 
120  info.exec();
121 }
122 
124 {
125  QString path = QFileDialog::getOpenFileName(this, tr("Select a TerraLib Symbol Library File"), "", "TerraLib Symbol Library Files (*.xml)");
126  if(path.isNull())
127  return;
128 
129  try
130  {
131  ReadSymbolLibrary(path.toStdString());
132  initialize();
133  }
134  catch(te::common::Exception& e)
135  {
136  QString message = tr("The selected symbol library could not be loaded.\n Details: )");
137  message += e.what();
138  QMessageBox::critical(this, tr("Error"), message);
139  }
140 }
141 
143 {
144  QList<QTreeWidgetItem*> items = m_ui->m_symbolLibraryTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 0);
145  items.append(m_ui->m_symbolLibraryTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 1));
146  items.append(m_ui->m_symbolLibraryTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 2));
147 
148  filter(items);
149 }
150 
152 {
153  m_ui->m_symbolLibraryTreeWidget->clear();
154 
155  // Gets the loaded libraries
156  std::pair<std::map<std::string, SymbolLibrary*>::const_iterator,
157  std::map<std::string, SymbolLibrary*>::const_iterator> iteratorsLibrary = SymbolLibraryManager::getInstance().getIterator();
158 
159  std::map<std::string, SymbolLibrary*>::const_iterator itLibrary;
160  for(itLibrary = iteratorsLibrary.first; itLibrary != iteratorsLibrary.second; ++itLibrary) // for each library
161  {
162  QString libraryName = QString::fromStdString(itLibrary->second->getName());
163 
164  QTreeWidgetItem* libraryItem = new QTreeWidgetItem(m_ui->m_symbolLibraryTreeWidget, LIBRARY);
165  libraryItem->setText(0, libraryName);
166  libraryItem->setData(0, Qt::UserRole, libraryName);
167 
168  std::pair<std::map<std::string, Symbol*>::const_iterator,
169  std::map<std::string, Symbol*>::const_iterator> iteratorsSymbol = itLibrary->second->getIterator();
170 
171  std::map<std::string, Symbol*>::const_iterator itSymbol;
172 
173  for(itSymbol = iteratorsSymbol.first; itSymbol != iteratorsSymbol.second; ++itSymbol) // for each Symbol
174  {
175  // The current symbol
176  Symbol* symbol = itSymbol->second;
177 
178  // Bulding the Qt symbol item
179  QTreeWidgetItem* symbolItem = new QTreeWidgetItem(libraryItem, SYMBOL);
180  symbolItem->setText(0, symbol->getInfo().m_name.c_str());
181  symbolItem->setIcon(0, SymbologyPreview::build(symbol->getSymbolizers(), m_ui->m_symbolLibraryTreeWidget->iconSize()));
182  symbolItem->setToolTip(0, formatSymbolInfo(symbol->getInfo()));
183  symbolItem->setData(0, Qt::UserRole, QVariant(QString(symbol->getInfo().m_id.c_str())));
184  symbolItem->setText(1, symbol->getInfo().m_author.c_str());
185  symbolItem->setText(2, symbol->getInfo().m_tags.c_str());
186  symbolItem->setText(3, symbol->getInfo().m_description.c_str());
187  }
188  }
189 
190  m_ui->m_symbolLibraryTreeWidget->sortItems(0, Qt::AscendingOrder);
191  m_ui->m_symbolLibraryTreeWidget->resizeColumnToContents(0);
192  m_ui->m_symbolLibraryTreeWidget->expandAll();
193 }
194 
195 void te::qt::widgets::SymbolSelectorDialog::filter(const QList<QTreeWidgetItem*>& items)
196 {
197  for(int i = 0; i < m_ui->m_symbolLibraryTreeWidget->topLevelItemCount(); ++i)
198  {
199  QTreeWidgetItem* library = m_ui->m_symbolLibraryTreeWidget->topLevelItem(i);
200  assert(library && library->type() == LIBRARY);
201 
202  for(int j = 0; j < library->childCount(); ++j)
203  {
204  QTreeWidgetItem* symbol = library->child(j);
205  assert(symbol && symbol->type() == SYMBOL);
206  bool hide = items.indexOf(symbol) == -1;
207  symbol->setHidden(hide);
208  }
209  }
210  update();
211 }
212 
214 {
215  QList<QTreeWidgetItem*> selected = m_ui->m_symbolLibraryTreeWidget->selectedItems();
216 
217  if(selected.empty())
218  return 0;
219 
220  return getSymbolFromItem(selected.at(0));
221 }
222 
224 {
225  assert(item);
226 
227  if(item->type() != SYMBOL)
228  return 0;
229 
230  // Gets the library
231  QTreeWidgetItem* parent = item->parent();
232  assert(parent);
233 
234  QString name = parent->data(0, Qt::UserRole).toString();
235  assert(!name.isEmpty());
236 
237  SymbolLibrary* symbolLibrary = SymbolLibraryManager::getInstance().findByName(name.toStdString());
238  assert(symbolLibrary);
239 
240  // Gets the symbol
241  QString id = item->data(0, Qt::UserRole).toString();
242  assert(!id.isEmpty());
243 
244  Symbol* symbol = symbolLibrary->findById(id.toStdString());
245  assert(symbol);
246 
247  return symbol;
248 }
249 
251 {
252  QString information("<h3>Symbol Information</h3><ul>");
253  information += "<li><b>Name: </b>" + QString(info.m_name.c_str()) + "</li>";
254  information += "<li><b>Author: </b>" + QString(info.m_author.c_str()) + "</li>";
255  information += "<li><b>Tags: </b>" + QString(info.m_tags.c_str()) + "</li>";
256  information += "<li><b>Description: </b>" + QString(info.m_description.c_str()) + "</li>";
257  information += "</ul>";
258 
259  return information;
260 }
std::auto_ptr< Ui::SymbolSelectorDialogForm > m_ui
Dialog form.
A widget used to preview symbol elements.
void setReadMode(bool enable=true)
This class represents a symbol.
A dialog used to configure informations about a symbol.
virtual const char * what() const
It outputs the exception message.
Definition: Exception.cpp:58
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...
This file contains utility functions for dealing with Symbology Enconding and Qt concepts.
Static class used to generate preview of Symbology elements.
const SymbolInfo & getInfo() const
It return the information associated to the symbol.
Definition: Symbol.cpp:50
A dialog used to select a specific symbol.
Symbol * getSelectedSymbol() const
Auxiliary internal method that returns the selected symbol.
SymbolLibrary * findByName(const std::string &name) const
It returns the symbol library identified by a given name or NULL if none is found.
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.
Symbol * getSymbol() const
Gets the selected symbol.
The SymbolLibraryManager is a singleton that can be used to manage all loaded symbol libraries in Ter...
static SymbolLibraryManager & getInstance()
It returns a reference to the singleton instance.
void initialize()
Initialize the dialog.
This class represents a library of symbols.
void filter(const QList< QTreeWidgetItem * > &items)
Auxiliary internal method to filter the symbols.
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...
This class represents a library of symbols.
Definition: SymbolLibrary.h:52
A dialog used to configure informations about a symbol.
Information about a given Symbol.
Definition: SymbolInfo.h:60
This class represents a symbol. TODO: More description!
Definition: Symbol.h:54
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
Symbol * getSymbolFromItem(QTreeWidgetItem *item) const
Auxiliary internal method that returns the symbol given a list widget item.
const std::vector< te::se::Symbolizer * > & getSymbolizers() const
It returns the list of Symbolizers that compose the symbol.
Definition: Symbol.cpp:77
A dialog used to select a specific symbol.
void onSearchLineEditTextChanged(const QString &text)
void onCurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
A widget used to preview symbol elements.
QString formatSymbolInfo(const SymbolInfo &info) const
Auxiliary internal method to format a symbol info to be used on tool tips.
SymbolPreviewWidget * m_preview
Preview Widget used to visualize the symbol.
TEQTWIDGETSEXPORT void ReadSymbolLibrary(const std::string &path)
It reads a symbol library file and put the result on SymbolLibraryManager.
Definition: Utils.cpp:128
void setSymbolInfo(const SymbolInfo &info)
Sets the symbol information to this dialog.
static QPixmap build(const te::se::Symbolizer *symb, const QSize &size)
Generates the preview of given symbolizer element.