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 
75 {
76  SymbolSelectorDialog dlg(parent);
77 
78  if(!title.isEmpty())
79  dlg.setWindowTitle(title);
80 
81  if(dlg.exec() == QDialog::Accepted)
82  return dlg.getSymbol();
83 
84  return nullptr;
85 }
86 
88 {
89  Symbol* symbol = getSelectedSymbol();
90 
91  if(symbol == nullptr)
92  return nullptr;
93 
94  return symbol->clone();
95 }
96 
98  QTreeWidgetItem* current, QTreeWidgetItem* /*previous*/)
99 {
100  Symbol* symbol = getSymbolFromItem(current);
101 
102  if(symbol == nullptr)
103  return;
104 
105  m_preview->updatePreview(symbol);
106 }
107 
109 {
110  Symbol* symbol = getSelectedSymbol();
111 
112  if(symbol == nullptr)
113  return;
114 
115  SymbolInfoDialog info;
116  info.setSymbolInfo(symbol->getInfo());
117  info.setReadMode();
118 
119  info.exec();
120 }
121 
123 {
124  QString path = QFileDialog::getOpenFileName(this, tr("Select a TerraLib Symbol Library File"), "", "TerraLib Symbol Library Files (*.xml)");
125  if(path.isNull())
126  return;
127 
128  try
129  {
130  ReadSymbolLibrary(path.toUtf8().data());
131  initialize();
132  }
133  catch(te::common::Exception& e)
134  {
135  QString message = tr("The selected symbol library could not be loaded.\n Details: )");
136  message += e.what();
137  QMessageBox::critical(this, tr("Error"), message);
138  }
139 }
140 
142 {
143  QList<QTreeWidgetItem*> items = m_ui->m_symbolLibraryTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 0);
144  items.append(m_ui->m_symbolLibraryTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 1));
145  items.append(m_ui->m_symbolLibraryTreeWidget->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 2));
146 
147  filter(items);
148 }
149 
151 {
152  m_ui->m_symbolLibraryTreeWidget->clear();
153 
154  // Gets the loaded libraries
155  std::pair<std::map<std::string, SymbolLibrary*>::const_iterator,
156  std::map<std::string, SymbolLibrary*>::const_iterator> iteratorsLibrary = SymbolLibraryManager::getInstance().getIterator();
157 
158  std::map<std::string, SymbolLibrary*>::const_iterator itLibrary;
159  for(itLibrary = iteratorsLibrary.first; itLibrary != iteratorsLibrary.second; ++itLibrary) // for each library
160  {
161  QString libraryName = QString::fromUtf8(itLibrary->second->getName().c_str());
162 
163  QTreeWidgetItem* libraryItem = new QTreeWidgetItem(m_ui->m_symbolLibraryTreeWidget, LIBRARY);
164  libraryItem->setText(0, libraryName);
165  libraryItem->setData(0, Qt::UserRole, libraryName);
166 
167  std::pair<std::map<std::string, Symbol*>::const_iterator,
168  std::map<std::string, Symbol*>::const_iterator> iteratorsSymbol = itLibrary->second->getIterator();
169 
170  std::map<std::string, Symbol*>::const_iterator itSymbol;
171 
172  for(itSymbol = iteratorsSymbol.first; itSymbol != iteratorsSymbol.second; ++itSymbol) // for each Symbol
173  {
174  // The current symbol
175  Symbol* symbol = itSymbol->second;
176 
177  // Bulding the Qt symbol item
178  QTreeWidgetItem* symbolItem = new QTreeWidgetItem(libraryItem, SYMBOL);
179  symbolItem->setText(0, symbol->getInfo().m_name.c_str());
180  symbolItem->setIcon(0, SymbologyPreview::build(symbol->getSymbolizers(), m_ui->m_symbolLibraryTreeWidget->iconSize()));
181  symbolItem->setToolTip(0, formatSymbolInfo(symbol->getInfo()));
182  symbolItem->setData(0, Qt::UserRole, QVariant(QString(symbol->getInfo().m_id.c_str())));
183  symbolItem->setText(1, symbol->getInfo().m_author.c_str());
184  symbolItem->setText(2, symbol->getInfo().m_tags.c_str());
185  symbolItem->setText(3, symbol->getInfo().m_description.c_str());
186  }
187  }
188 
189  m_ui->m_symbolLibraryTreeWidget->sortItems(0, Qt::AscendingOrder);
190  m_ui->m_symbolLibraryTreeWidget->resizeColumnToContents(0);
191  m_ui->m_symbolLibraryTreeWidget->expandAll();
192 }
193 
194 void te::qt::widgets::SymbolSelectorDialog::filter(const QList<QTreeWidgetItem*>& items)
195 {
196  for(int i = 0; i < m_ui->m_symbolLibraryTreeWidget->topLevelItemCount(); ++i)
197  {
198  QTreeWidgetItem* library = m_ui->m_symbolLibraryTreeWidget->topLevelItem(i);
199  assert(library && library->type() == LIBRARY);
200 
201  for(int j = 0; j < library->childCount(); ++j)
202  {
203  QTreeWidgetItem* symbol = library->child(j);
204  assert(symbol && symbol->type() == SYMBOL);
205  bool hide = items.indexOf(symbol) == -1;
206  symbol->setHidden(hide);
207  }
208  }
209  update();
210 }
211 
213 {
214  QList<QTreeWidgetItem*> selected = m_ui->m_symbolLibraryTreeWidget->selectedItems();
215 
216  if(selected.empty())
217  return nullptr;
218 
219  return getSymbolFromItem(selected.at(0));
220 }
221 
223 {
224  assert(item);
225 
226  if(item->type() != SYMBOL)
227  return nullptr;
228 
229  // Gets the library
230  QTreeWidgetItem* parent = item->parent();
231  assert(parent);
232 
233  QString name = parent->data(0, Qt::UserRole).toString();
234  assert(!name.isEmpty());
235 
236  SymbolLibrary* symbolLibrary = SymbolLibraryManager::getInstance().findByName(name.toUtf8().data());
237  assert(symbolLibrary);
238 
239  // Gets the symbol
240  QString id = item->data(0, Qt::UserRole).toString();
241  assert(!id.isEmpty());
242 
243  Symbol* symbol = symbolLibrary->findById(id.toUtf8().data());
244  assert(symbol);
245 
246  return symbol;
247 }
248 
250 {
251  QString information("<h3>Symbol Information</h3><ul>");
252  information += "<li><b>Name: </b>" + QString(info.m_name.c_str()) + "</li>";
253  information += "<li><b>Author: </b>" + QString(info.m_author.c_str()) + "</li>";
254  information += "<li><b>Tags: </b>" + QString(info.m_tags.c_str()) + "</li>";
255  information += "<li><b>Description: </b>" + QString(info.m_description.c_str()) + "</li>";
256  information += "</ul>";
257 
258  return information;
259 }
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.
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...
Static class used to generate preview of Symbology elements.
const SymbolInfo & getInfo() const
It return the information associated to the symbol.
Definition: Symbol.cpp:48
A dialog used to select a specific symbol.
Symbol * getSelectedSymbol() const
Auxiliary internal method that returns the selected symbol.
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:118
This file contains utility functions for dealing with Symbology Enconding and Qt concepts.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
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:75
A dialog used to select a specific symbol.
void onSearchLineEditTextChanged(const QString &text)
void updatePreview(Symbol *symbol)
Preview a symbol element.
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.
std::unique_ptr< Ui::SymbolSelectorDialogForm > m_ui
Dialog form.
TEQTWIDGETSEXPORT void ReadSymbolLibrary(const std::string &path)
It reads a symbol library file and put the result on SymbolLibraryManager.
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.