TabularViewerEx.cpp
Go to the documentation of this file.
1 #include "TabularViewerEx.h"
2 #include "SelectPKey.h"
3 #include "HighlightedInfo.h"
4 #include "StarDelegate.h"
5 
6 //! TerraLib include files
8 #include <terralib/qt/widgets/dataview/TabularViewer.h>
9 #include <terralib/Config.h>
10 
13 #include <terralib/dataaccess/datasource/DataSourceCatalogLoader.h>
16 
17 //! Qt include files
18 #include <QGridLayout>
19 #include <QVBoxLayout>
20 #include <QPushButton>
21 #include <QMessageBox>
22 #include <QTabWidget>
23 #include <QLabel>
24 #include <QGroupBox>
25 #include <QInputDialog>
26 #include <QMenu>
27 #include <QMenuBar>
28 #include <QAction>
29 #include <QDir>
30 
31 te::da::DataSource* getDataSource(const std::string& src)
32 {
34 
35  bool res = ds->isOpened(); // expect false;
36 
37  std::map<std::string, std::string> connInfo;
38  connInfo["SOURCE"] = src ;
39  ds->open(connInfo);
40  res = ds->isOpened(); // expect true
41  res = ds->isValid(); // expect true
42 
43  return (res) ? ds : 0;
44 }
45 
47 {
48 // get a transactor to interact to the data source
49  te::da::DataSourceTransactor* transactor = ds->getTransactor();
50 
51 // from transactor, take a catalog loader to find out the datasets stored in the data source
52  te::da::DataSourceCatalogLoader* cloader = transactor->getCatalogLoader();
53 
54 // now retrieve the name of the datasets
55  boost::ptr_vector<std::string> datasets;
56 
57  cloader->getDataSets(datasets);
58 
59 // retrieve the dataset by its name
60  te::da::DataSet* dataset = transactor->getDataSet(datasets[0]);
61 
62  delete cloader;
63 
64 // release the transactor: you are the owner
65  delete transactor;
66 
67  return dataset;
68 }
69 
70 //! Function for initialize the TabularViewerEx dialog.
71 void initDialog(TabularViewerEx* wd, QTabWidget* tab, te::qt::widgets::FileChooser*& fc, te::qt::widgets::TabularViewer*& tv, SelectPKey*& pkeySel)
72 {
73  QMenuBar* mbar= new QMenuBar(wd);
74  QMenu* file_mnu = new QMenu(QObject::tr("File"), mbar);
75  QAction* act = new QAction(QObject::tr("Change icons theme..."), file_mnu);
76 
77  file_mnu->addAction(act);
78  mbar->addMenu(file_mnu);
79  wd->setMenuBar(mbar);
80 
81  QWidget* wid = new QWidget(wd);
82  pkeySel = new SelectPKey(wd);
83 
84  fc = new te::qt::widgets::FileChooser(wd);
85  tv = new te::qt::widgets::TabularViewer(wd);
86  QPushButton* btn = new QPushButton(QObject::tr("Update viewer"), wd);
87  QVBoxLayout* vlay = new QVBoxLayout;
88  QGridLayout* grdLay = new QGridLayout(wid);
89 
90  //! [Adding custom delegate decorator]
92 
93  // Decorate the clone of delegate
94  StarDelegate* st = new StarDelegate(del->clone());
95 
96  // Setting up color
97  st->setHighlightColor(QColor(255, 255, 0));
98 
99  //Setting up the new delegate
100  st->setParent(tv);
101  tv->setItemDelegate(st);
102 
103  // Update menus
104  tv->updatePopupMenus();
105 
106  delete del;
107  //! [Adding custom delegate decorator]
108 
109  vlay->addWidget(fc);
110  vlay->addWidget(btn);
111  vlay->addWidget(tv);
112 
113  grdLay->addLayout(vlay, 0, 0, 1, 1);
114 
115  tab->addTab(wid, QObject::tr("Data viewer"));
116  tab->addTab(pkeySel, QObject::tr("Primary keys"));
117  tab->addTab(new HighlightedInfo(tv, wd), QObject::tr("Query by primary key"));
118 
119  tab->setTabEnabled(1, false);
120  tab->setTabEnabled(2, false);
121 
122  wd->setCentralWidget(tab);
123 
124  //! Connecting slots
125  wd->connect(btn, SIGNAL(pressed()), SLOT(updateViewer()));
126  tv->connect(pkeySel, SIGNAL(pkeysChanged(const std::vector<size_t>&)), SLOT(setPrimaryKeys(const std::vector<size_t>&)));
127  wd->connect(pkeySel, SIGNAL(pkeysChanged(const std::vector<size_t>&)), SLOT(pkeysChanged(const std::vector<size_t>&)));
128  wd->connect(act, SIGNAL(triggered()), SLOT(changeThemeName()));
129 }
130 
132 QMainWindow(parent),
133 m_dsrc(0),
134 m_tab(new QTabWidget(this))
135 {
137 
138  QMainWindow::setWindowTitle(tr("Shapefile tabular viewer"));
139 
140  QString filter(tr("Shape Files (*.shp *.SHP)"));
141 
142  //! Adding filter for shape files
143  m_fchooser->setFilterPattern(filter);
144 }
145 
147 {
148  delete m_dsrc;
149 }
150 
152 {
153  QString fname = m_fchooser->getSelectedResource();
154 
155  if(fname.isEmpty())
156  {
157  QMessageBox::warning(this, tr("Example warning"), tr("There's no file selected."));
158  return;
159  }
160 
161  if(m_dsrc != 0)
162  {
163  m_dsrc->close();
164  delete m_dsrc;
165  }
166 
167  m_dsrc = getDataSource(fname.toUtf8().data());
168 
169  if(m_dsrc != 0)
170  {
172  m_viewer->showData(ds);
174 
175  m_tab->setTabEnabled(1, true);
176  m_tab->setTabEnabled(2, false);
177  }
178 }
179 
180 void TabularViewerEx::pkeysChanged(const std::vector<size_t>& ids)
181 {
182  if(!ids.empty())
183  m_tab->setTabEnabled(2, true);
184 }
185 
186 void TabularViewerEx::setThemeName(const QString& tName)
187 {
188  QIcon::setThemeName(tName);
189  QMainWindow::update();
190 }
191 
193 {
194  bool ok;
195  QString text = QInputDialog::getText(this, tr("Enter the name of the new icon theme."), tr("Icon theme name:"), QLineEdit::Normal, QDir::home().dirName(), &ok);
196 
197  if (ok && !text.isEmpty())
198  setThemeName(text);
199 }
te::da::DataSource * m_dsrc
An specialization of QItemDelegate to be used with te::map::AbstractTable objects.
virtual bool isValid() const =0
It checks if the data source is valid (available for using).
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
te::qt::widgets::FileChooser * m_fchooser
virtual std::unique_ptr< DataSourceTransactor > getTransactor()=0
It returns the set of parameters used to set up the access channel to the underlying repository...
te::da::DataSource * getDataSource(const std::string &src)
TerraLib include files.
virtual void open()=0
It opens the data source and makes it ready for using.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
SelectPKey * m_pkey_sel
void initDialog(TabularViewerEx *wd, QTabWidget *tab, te::qt::widgets::FileChooser *&fc, te::qt::widgets::TabularViewer *&tv, SelectPKey *&pkeySel)
Function for initialize the TabularViewerEx dialog.
Defines a component for choose a file.
virtual bool isOpened() const =0
It returns true if the data source is opened, otherwise it returns false.
static te::dt::Date ds(2010, 01, 01)
TerraLib include files.
Definition: StarDelegate.h:11
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
void updateColumns(te::da::DataSet *dset)
Definition: SelectPKey.cpp:57
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
TabularViewerEx(QWidget *parent=0)
Defines a component for choose a file.
Definition: FileChooser.h:60
A factory for data sources.
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
A dataset is the unit of information manipulated by the data access module of TerraLib.
virtual void close()=0
It closes the data source and clears all the resources used by its internal communication channel...
virtual std::unique_ptr< DataSet > getDataSet(const std::string &name, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)=0
It gets the dataset identified by the given name. A dataset can be connected or disconnected. A connected dataset, after its creation through the data source transactor, continues to depend on the connection given by its associated data source. Differently, a disconnected dataset, after its creation, no more depends of the connection given by the data source, and it continues to live after the connection has been released to the data source.
QTabWidget * m_tab
void setHighlightColor(const QColor &value)
te::da::DataSet * getDataSet(te::da::DataSource *ds)
void pkeysChanged(const std::vector< size_t > &ids)
te::qt::widgets::TabularViewer * m_viewer
QString getSelectedResource() const
Returns the text presented in line edit.
Definition: FileChooser.cpp:76
This file contains several utility functions for dealing with STL containers.
void setFilterPattern(const QString &filter)
Defines the filter for files of interest.
Definition: FileChooser.cpp:70
void setThemeName(const QString &tName)