examples/qt/charts/main.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 main.cpp
22 
23  \brief Examples about chart widgets
24  */
25 
26 // TerraLib
27 #include "../../Config.h"
29 #include <terralib/core/plugin.h>
30 #include <terralib/common.h>
31 #include <terralib/dataaccess.h>
34 
35 // Qt Chart
43 
44 //Utils
46 
47 // Qt
48 #include <QApplication>
49 
50 // STL
51 #include <exception>
52 #include <iostream>
53 
55 {
56  try
57  {
59 
60  std::string plugin_path = te::core::FindInTerraLibPath("share/terralib/plugins");
61 
62 #ifdef TERRALIB_MOD_OGR_ENABLED
63  info = te::core::JSONPluginInfoSerializer(plugin_path + "/te.da.ogr.teplg.json");
66 
67 #endif
68  }
69  catch(...)
70  {
71  std::cout << std::endl << "Failed to load the data source OGR driver: unknown exception!" << std::endl;
72  }
73 }
74 
75 void generateHistogram(te::da::DataSet* dataset, std::string datasourceId, te::da::DataSourceTransactor* transactor)
76 {
77  //Getting the Column that will be used to populate the chart
78  std::string renda = "densidade";
79 
80  std::size_t rendaIdx= te::da::GetPropertyPos(dataset, renda);
81 
82  std::unique_ptr<te::da::DataSetType> dt = transactor->getDataSetType("sp_cities");
83 
84  //Creating the histogram and it's chart with the given dataset
85  te::qt::widgets::DataSet2Layer converter(datasourceId);
86  te::da::DataSetTypePtr dsType(dt.release());
87  te::map::AbstractLayerPtr layer = converter(dsType);
88  te::qt::widgets::Histogram* histogram = te::qt::widgets::createHistogram(layer, static_cast<int>(rendaIdx), 50, -1);
90 
91  //Creating and adjusting the chart Display's style.
93  chartStyle->setTitle(QString::fromUtf8("Histogram"));
94  chartStyle->setAxisX(QString::fromUtf8(renda.c_str()));
95  chartStyle->setAxisY(QString::fromUtf8("Frequency"));
96 
97  //Creating and adjusting the chart Display
98  te::qt::widgets::ChartDisplay* chartDisplay = new te::qt::widgets::ChartDisplay(0, QString::fromUtf8("Histogram"), chartStyle);
99  chartDisplay->adjustDisplay();
100  chart->attach(chartDisplay);
101 
102  //Adjusting the chart widget, once it's closed all the other pointers will be deleted. Check the charts documentation for further notes on pointer ownership
104  displayWidget->show();
105  displayWidget->setWindowTitle("Histogram");
106  displayWidget->setAttribute(Qt::WA_DeleteOnClose, true);
107 }
108 
110 {
111  //Getting the Column that will be used to populate the chart
112  std::string renda = "densidade";
113  std::string anosest = "codmicro";
114 
115  std::size_t rendaIdx= te::da::GetPropertyPos(dataset, renda);
116  std::size_t anosestIdx= te::da::GetPropertyPos(dataset, anosest);
117 
118  std::unique_ptr<te::da::DataSetType> dt = transactor->getDataSetType("sp_cities");
119 
120  //Creating the scatter and it's chart with the given dataset
121  te::qt::widgets::Scatter* scatter = te::qt::widgets::createScatter(dataset, dt.get(), static_cast<int>(rendaIdx), static_cast<int>(anosestIdx), -1);
123 
124  //Creating and adjusting the chart Display's style.
126  chartStyle->setTitle(QString::fromUtf8("Scatter"));
127  chartStyle->setAxisX(QString::fromUtf8(renda.c_str()));
128  chartStyle->setAxisY(QString::fromUtf8(anosest.c_str()));
129 
130  //Creating and adjusting the chart Display
131  te::qt::widgets::ChartDisplay* chartDisplay = new te::qt::widgets::ChartDisplay(0, QString::fromUtf8("Scatter"), chartStyle);
132  chartDisplay->adjustDisplay();
133  chart->attach(chartDisplay);
134 
135  //Adjusting the chart widget, once it's closed all the other pointers will be deleted. Check the charts documentation for further notes on pointer ownership
137  displayWidget->show();
138  displayWidget->setWindowTitle("Scatter");
139  displayWidget->setAttribute(Qt::WA_DeleteOnClose, true);
140 }
141 
142 int main(int /*argc*/, char** /*argv*/)
143 {
144 // initialize Terralib support
146 
147  try
148  {
149  LoadOGRModule();
150 
151  // Get a dataset
152  std::string connInfo("file://");
153 
154  std::string data_dir = TERRALIB_DATA_DIR;
155  connInfo += data_dir + "/shape";
156 
157  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR", connInfo);
158  ds->open();
159 
160  std::unique_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
161 
162  std::unique_ptr<te::da::DataSet> dataset = transactor->getDataSet("sp_cities");
163  if(dataset.get()==0)
164  {
165  return 0;
166  }
167 
168  int argc = 1;
169  QApplication app(argc, 0);
170  QString title("Testing Chart Widgets");
171 
172  generateHistogram(dataset.get(), ds->getId(), transactor.get());
173  generateScatter(dataset.get(), transactor.get());
174 
175  int ret;
176  ret = app.exec();
177  }
178  catch(const std::exception& e)
179  {
180  std::cout << std::endl << "An exception has occurred: " << e.what() << std::endl;
181 
182  return EXIT_FAILURE;
183  }
184  catch(...)
185  {
186  std::cout << std::endl << "An unexpected exception has occurred!" << std::endl;
187 
188  return EXIT_FAILURE;
189  }
190 
191 // finalize Terralib support
193 
195 
196  return EXIT_SUCCESS;
197 }
TEQTWIDGETSEXPORT Scatter * createScatter(te::da::DataSet *dataset, te::da::DataSetType *dataType, int propX, int propY, int stat, bool readall=true)
Scatter Creator.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
void generateHistogram(te::da::DataSet *dataset, std::string datasourceId, te::da::DataSourceTransactor *transactor)
Include files for Core Plugin Library.
void adjustDisplay()
Updates the general display settings according to the ChartStyle. The adjusted properties are: Title;...
int main(int, char **)
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
void generateScatter(te::da::DataSet *dataset, te::da::DataSourceTransactor *transactor)
void insert(const PluginInfo &pinfo)
Adds plugin with its plugin information to the list of unloaded plugins.
void attach(QwtPlot *plot)
It atttaches a QwtPlot to this Cahrt.
TEQTWIDGETSEXPORT Histogram * createHistogram(te::map::AbstractLayerPtr layer, int propId, int slices, int stat, bool readall=true)
Histogram Creator.
This file is a wrapper around platform specific include files.
This is the base class for Layers.
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
Basic information about a plugin.
static te::dt::Date ds(2010, 01, 01)
A class to represent a Histogram.
Definition: Histogram.h:56
void load(const std::string &plugin_name, const bool start=true)
It tries to load the informed plugin.
void setTitle(QString newTitle)
It sets the style&#39;s title.
Definition: ChartStyle.cpp:71
A class to represent a histogram.
std::string name
The plugin name: an internal value used to identify the plugin in the system. Must be a unique value...
void LoadOGRModule()
static PluginManager & instance()
Access the singleton.
A class to represent a scatter.
Definition: Scatter.h:51
void finalize()
It finalizes the TerraLib Platform.
static TerraLib & getInstance()
It returns a reference to the singleton instance.
void setAxisY(QString newAxisY)
It sets the style&#39;s y axis label.
Definition: ChartStyle.cpp:101
static te::dt::TimeDuration dt(20, 30, 50, 11)
A class to represent a chart display.
Definition: ChartDisplay.h:65
void setAxisX(QString newAxisX)
It sets the style&#39;s x axis label.
Definition: ChartStyle.cpp:91
A class to represent a histogram chart.
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
A class to represent a scatter chart.
Definition: ScatterChart.h:55
A class to represent a scatter&#39;s chart.
A class to represent a scatter.
void initialize()
It initializes the TerraLib Platform.
A dataset is the unit of information manipulated by the data access module of TerraLib.
A wdiget used to display a chart.
TECOREEXPORT PluginInfo JSONPluginInfoSerializer(const std::string &file_name)
A plugin finder that search for plugins in some special directories defined by compile time macros...
Definition: Serializers.cpp:44
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
A widget used to display a set of charts.
This file contains include headers for the TerraLib Common Runtime module.
A class used to define a chartDisplay&#39;s style.
void clear()
Stop and unload all plugins, then clear the internal list of plugins.
virtual std::unique_ptr< te::da::DataSetType > getDataSetType(const std::string &name)=0
It gets information about the given dataset.
This file contains include headers for the Data Access module of TerraLib.
A class to represent a chart display.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
This file contains a set of utility chart functions.