All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.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/af/Utils.cpp
22 
23  \brief Utility routines for the TerraLib Application Framework module.
24 */
25 
26 // Boost
27 #include <boost/foreach.hpp> // Boost => don't change this include order, otherwise you may have compiling problems!
28 
29 // TerraLib
30 #include "../../common/BoostUtils.h"
31 #include "../../common/PlatformUtils.h"
32 #include "../../common/SystemApplicationSettings.h"
33 #include "../../common/UserApplicationSettings.h"
34 #include "../../dataaccess/datasource/DataSourceInfo.h"
35 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
36 #include "../../dataaccess/serialization/xml/Serializer.h"
37 #include "../../maptools/AbstractLayer.h"
38 #include "../../plugin/PluginManager.h"
39 #include "../../plugin/PluginInfo.h"
40 #include "../../maptools/serialization/xml/Layer.h"
41 #include "../../xml/AbstractWriter.h"
42 #include "../../xml/AbstractWriterFactory.h"
43 #include "../../xml/Reader.h"
44 #include "../../xml/ReaderFactory.h"
45 #include "../../Version.h"
46 #include "ApplicationController.h"
47 #include "Exception.h"
48 #include "Project.h"
49 #include "Utils.h"
50 #include "XMLFormatter.h"
51 
52 // STL
53 #include <cassert>
54 #include <fstream>
55 #include <memory>
56 
57 // Boost
58 #include <boost/property_tree/ptree.hpp>
59 #include <boost/property_tree/json_parser.hpp>
60 #include <boost/algorithm/string/replace.hpp>
61 #include <boost/filesystem.hpp>
62 #include <boost/format.hpp>
63 
64 // Qt
65 #include <QDir>
66 #include <QFileInfo>
67 #include <QSettings>
68 #include <QString>
69 #include <QTextStream>
70 #include <QApplication>
71 #include <QAction>
72 #include <QMainWindow>
73 #include <QMessageBox>
74 #include <QToolBar>
75 
76 
78 {
79  boost::filesystem::path furi(uri);
80 
81  if (!boost::filesystem::exists(furi) || !boost::filesystem::is_regular_file(furi))
82  throw Exception((boost::format(TE_TR("Could not read project file: %1%.")) % uri).str());
83 
84  std::auto_ptr<te::xml::Reader> xmlReader(te::xml::ReaderFactory::make());
85  xmlReader->setValidationScheme(false);
86 
87  xmlReader->read(uri);
88 
89  if(!xmlReader->next())
90  throw Exception((boost::format(TE_TR("Could not read project information in the file: %1%.")) % uri).str());
91 
92  if(xmlReader->getNodeType() != te::xml::START_ELEMENT)
93  throw Exception((boost::format(TE_TR("Error reading the document %1%, the start element wasn't found.")) % uri).str());
94 
95  if(xmlReader->getElementLocalName() != "Project")
96  throw Exception((boost::format(TE_TR("The first tag in the document %1% is not 'Project'.")) % uri).str());
97 
98  Project* proj = ReadProject(*xmlReader);
99 
100  proj->setFileName(uri);
101 
102  XMLFormatter::format(proj, false);
103 
104  proj->setProjectAsChanged(false);
105 
106  return proj;
107 
108 }
109 
111 {
112  std::auto_ptr<Project> project(new Project);
113 
114  reader.next();
115  assert(reader.getNodeType() == te::xml::START_ELEMENT);
116  assert(reader.getElementLocalName() == "Title");
117 
118  reader.next();
119  assert(reader.getNodeType() == te::xml::VALUE);
120  project->setTitle(reader.getElementValue());
121  reader.next(); // End element
122 
123  reader.next();
124  assert(reader.getNodeType() == te::xml::START_ELEMENT);
125  assert(reader.getElementLocalName() == "Author");
126 
127  reader.next();
128 
129  if(reader.getNodeType() == te::xml::VALUE)
130  {
131  project->setAuthor(reader.getElementValue());
132  reader.next(); // End element
133  }
134 
135  //read data source list from this project
136  reader.next();
137 
138  assert(reader.getNodeType() == te::xml::START_ELEMENT);
139  assert(reader.getElementLocalName() == "DataSourceList");
140 
141  reader.next();
142 
143  // DataSourceList contract form
144  if(reader.getNodeType() == te::xml::END_ELEMENT &&
145  reader.getElementLocalName() == "DataSourceList")
146  {
147  reader.next();
148  }
149 
150  while((reader.getNodeType() == te::xml::START_ELEMENT) &&
151  (reader.getElementLocalName() == "DataSource"))
152  {
155  }
156 
157  //end read data source list
158 
159  assert(reader.getNodeType() == te::xml::START_ELEMENT);
160  assert(reader.getElementLocalName() == "ComponentList");
161  reader.next(); // End element
162  reader.next(); // next after </ComponentList>
163 
164  assert(reader.getNodeType() == te::xml::START_ELEMENT);
165  assert(reader.getElementLocalName() == "LayerList");
166 
167  reader.next();
168 
170 
171  // Read the layers
172  while((reader.getNodeType() != te::xml::END_ELEMENT) &&
173  (reader.getElementLocalName() != "LayerList"))
174  {
175  te::map::AbstractLayerPtr layer(lserial.read(reader));
176 
177  assert(layer.get());
178 
179  project->add(layer);
180  }
181 
182  assert(reader.getNodeType() == te::xml::END_ELEMENT);
183  assert(reader.getElementLocalName() == "LayerList");
184 
185  reader.next();
186  assert((reader.getNodeType() == te::xml::END_ELEMENT) || (reader.getNodeType() == te::xml::END_DOCUMENT));
187  assert(reader.getElementLocalName() == "Project");
188 
189  project->setProjectAsChanged(false);
190 
191  return project.release();
192 }
193 
194 void te::qt::af::Save(const te::qt::af::Project& project, const std::string& uri)
195 {
196 
197  std::auto_ptr<te::xml::AbstractWriter> writer(te::xml::AbstractWriterFactory::make());
198 
199  writer->setURI(uri);
200 
201  Project p(project);
202 
203  Save(p, *writer.get());
204 
205  /* old way
206  std::ofstream fout(uri.c_str(), std::ios_base::trunc);
207 
208  te::xml::Writer w(fout);
209 
210  Project p(project);
211 
212  XMLFormatter::format(&p, true);
213 
214  Save(p, w);
215 
216  XMLFormatter::format(&p, false);
217 
218  fout.close();*/
219 }
220 
222 {
223  std::string schema_loc = te::common::FindInTerraLibPath("share/terralib/schemas/terralib/qt/af/project.xsd");
224 
225  writer.writeStartDocument("UTF-8", "no");
226 
227  writer.writeStartElement("Project");
228 
229  boost::replace_all(schema_loc, " ", "%20");
230 
231  writer.writeAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema-instance");
232  writer.writeAttribute("xmlns:te_da", "http://www.terralib.org/schemas/dataaccess");
233  writer.writeAttribute("xmlns:te_map", "http://www.terralib.org/schemas/maptools");
234  writer.writeAttribute("xmlns:te_qt_af", "http://www.terralib.org/schemas/common/af");
235 
236  writer.writeAttribute("xmlns:se", "http://www.opengis.net/se");
237  writer.writeAttribute("xmlns:ogc", "http://www.opengis.net/ogc");
238  writer.writeAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
239 
240  writer.writeAttribute("xmlns", "http://www.terralib.org/schemas/qt/af");
241  writer.writeAttribute("xsd:schemaLocation", "http://www.terralib.org/schemas/qt/af " + schema_loc);
242  writer.writeAttribute("version", TERRALIB_VERSION_STRING);
243 
244  writer.writeElement("Title", project.getTitle());
245  writer.writeElement("Author", project.getAuthor());
246 
247  //write data source list
248  writer.writeStartElement("te_da:DataSourceList");
249 
250  writer.writeAttribute("xmlns:te_common", "http://www.terralib.org/schemas/common");
251 
255 
256  for(it=itBegin; it!=itEnd; ++it)
257  {
258  bool ogrDsrc = it->second->getAccessDriver() == "OGR";
259 
260  writer.writeStartElement("te_da:DataSource");
261 
262  writer.writeAttribute("id", it->second->getId());
263  writer.writeAttribute("type", it->second->getType());
264  writer.writeAttribute("access_driver", it->second->getAccessDriver());
265 
266  writer.writeStartElement("te_da:Title");
267  writer.writeValue((!ogrDsrc) ? it->second->getTitle() : te::common::ConvertLatin1UTFString(it->second->getTitle()));
268  writer.writeEndElement("te_da:Title");
269 
270  writer.writeStartElement("te_da:Description");
271  writer.writeValue((!ogrDsrc) ? it->second->getDescription() : te::common::ConvertLatin1UTFString(it->second->getDescription()));
272  writer.writeEndElement("te_da:Description");
273 
274  writer.writeStartElement("te_da:ConnectionInfo");
275  std::map<std::string, std::string> info = it->second->getConnInfo();
276  std::map<std::string, std::string>::iterator conIt;
277 
278  for(conIt=info.begin(); conIt!=info.end(); ++conIt)
279  {
280  writer.writeStartElement("te_common:Parameter");
281 
282  writer.writeStartElement("te_common:Name");
283  writer.writeValue(conIt->first);
284  writer.writeEndElement("te_common:Name");
285 
286  writer.writeStartElement("te_common:Value");
287  writer.writeValue((ogrDsrc && (conIt->first == "URI" || conIt->first == "SOURCE")) ? te::common::ConvertLatin1UTFString(conIt->second) : conIt->second);
288  writer.writeEndElement("te_common:Value");
289 
290  writer.writeEndElement("te_common:Parameter");
291  }
292  writer.writeEndElement("te_da:ConnectionInfo");
293 
294  writer.writeEndElement("te_da:DataSource");
295  }
296 
297  writer.writeEndElement("te_da:DataSourceList");
298  //end write
299 
300  writer.writeStartElement("ComponentList");
301  writer.writeEndElement("ComponentList");
302 
303  writer.writeStartElement("te_map:LayerList");
304 
306 
307  for(std::list<te::map::AbstractLayerPtr>::const_iterator it = project.getTopLayers().begin();
308  it != project.getTopLayers().end();
309  ++it)
310  lserial.write(it->get(), writer);
311 
312  writer.writeEndElement("te_map:LayerList");
313 
314  writer.writeEndElement("Project");
315 
316  writer.writeToFile();
317 
318 }
319 
320 void te::qt::af::UpdateUserSettings(const QStringList& prjFiles, const QStringList& prjTitles, const std::string& userConfigFile)
321 {
322  QSettings user_settings(QSettings::IniFormat,
323  QSettings::UserScope,
324  QApplication::instance()->organizationName(),
325  QApplication::instance()->applicationName());
326 
327 // save recent projects
328  if(!prjFiles.empty() && !prjTitles.empty() && (prjFiles.size() == prjTitles.size()))
329  {
330  user_settings.setValue("projects/most_recent/path", prjFiles.at(0));
331  user_settings.setValue("projects/most_recent/title", prjTitles.at(0));
332 
333  if(prjFiles.size() > 1)
334  {
335  user_settings.beginGroup("projects");
336 
337  user_settings.beginWriteArray("recents");
338 
339  for(int i = 1; i != prjFiles.size(); ++i)
340  {
341  user_settings.setArrayIndex(i - 1);
342  user_settings.setValue("projects/path", prjFiles.at(i));
343  user_settings.setValue("projects/title", prjTitles.at(i));
344  }
345 
346  user_settings.endArray();
347 
348  user_settings.endGroup();
349  }
350  }
351 
352 // save enabled plugins
353  user_settings.remove("plugins/enabled");
354 
355  user_settings.beginGroup("plugins");
356  user_settings.remove("enabled");
357  user_settings.remove("unloaded");
358  user_settings.remove("broken");
359 
360  user_settings.beginWriteArray("enabled");
361 
362  std::vector<std::string> plugins = te::plugin::PluginManager::getInstance().getPlugins();
363 
364  int aidx = 0;
365 
366  for(std::size_t i = 0; i != plugins.size(); ++i)
367  {
368  if(!te::plugin::PluginManager::getInstance().isLoaded(plugins[i]))
369  continue;
370 
371  user_settings.setArrayIndex(aidx++);
372 
373  user_settings.setValue("name", plugins[i].c_str());
374  }
375 
376  user_settings.endArray();
377 
378  // save unloaded plugins
379  user_settings.beginWriteArray("unloaded");
380 
381  int unloadedidx = 0;
382 
383  for (std::size_t i = 0; i != plugins.size(); ++i)
384  {
385  if (!te::plugin::PluginManager::getInstance().isUnloadedPlugin(plugins[i]))
386  continue;
387 
388  user_settings.setArrayIndex(unloadedidx++);
389  user_settings.setValue("name", plugins[i].c_str());
390  }
391 
392  user_settings.endArray();
393 
394  // save broken plugins
395  user_settings.beginWriteArray("broken");
396 
397  int brokenidx = 0;
398 
399  for (std::size_t i = 0; i != plugins.size(); ++i)
400  {
401  if (!te::plugin::PluginManager::getInstance().isBrokenPlugin(plugins[i]))
402  continue;
403 
404  user_settings.setArrayIndex(brokenidx++);
405  user_settings.setValue("name", plugins[i].c_str());
406  }
407 
408  user_settings.endArray();
409 
410  user_settings.endGroup();
411 }
412 
414 {
415  QSettings usettings(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
416 
417  QVariant fileName = usettings.value("data_sources/data_file");
418 
419  if(fileName.isNull())
420  {
421  const QString& udir = ApplicationController::getInstance().getUserDataDir();
422 
423  fileName = udir + "/" + QString(TERRALIB_APPLICATION_DATASOURCE_FILE_NAME);
424 
425  usettings.setValue("data_sources/data_file", fileName);
426  }
427 
428  te::serialize::xml::Save(fileName.toString().toStdString());
429 }
430 
431 
432 void AddToolbarAndActions(QToolBar* bar, QSettings& sett)
433 {
434  sett.beginGroup(bar->objectName());
435 
436  sett.setValue("name", bar->objectName());
437 
438  sett.beginWriteArray("Actions");
439 
440  QList<QAction*> acts = bar->actions();
441 
442  for(int i=0; i<acts.size(); i++)
443  {
444  sett.setArrayIndex(i);
445  sett.setValue("action", acts.at(i)->objectName());
446  }
447 
448  sett.endArray();
449 
450  sett.endGroup();
451 }
452 
454 {
455  std::vector<QToolBar*> bars = te::qt::af::ApplicationController::getInstance().getToolBars();
456  std::vector<QToolBar*>::const_iterator it;
457  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
458 
459  sett.beginGroup("toolbars");
460 
461  for (it = bars.begin(); it != bars.end(); ++it)
462  {
463  QToolBar* bar = *it;
464 
465  sett.remove(bar->objectName());
466 
467  AddToolbarAndActions(bar, sett);
468  }
469 
470  sett.endGroup();
471 }
472 
474 {
475  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
476 
477  sett.beginGroup("toolbars");
478 
479  AddToolbarAndActions(bar, sett);
480 
481  sett.endGroup();
482 }
483 
485 {
486  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
487 
488  sett.beginGroup("toolbars");
489  sett.remove(bar->objectName());
490  sett.endGroup();
491 }
492 
493 std::vector<QToolBar*> te::qt::af::ReadToolBarsFromSettings(QWidget* barsParent)
494 {
495  std::vector<QToolBar*> bars;
496 
497  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
498 
499  sett.beginGroup("toolbars");
500  QStringList lst = sett.childGroups();
501 
502  QStringList::iterator it;
503 
504  for(it=lst.begin(); it != lst.end(); ++it)
505  {
506  QString gr = *it;
507 
508  sett.beginGroup(gr);
509 
510  QString grName = sett.value("name").toString();
511 
512  int size = sett.beginReadArray("Actions");
513 
514  QToolBar* toolbar = new QToolBar(barsParent);
515  toolbar->setObjectName(grName);
516  toolbar->setWindowTitle(grName);
517 
518  for(int i=0; i<size; i++)
519  {
520  sett.setArrayIndex(i);
521  QString act = sett.value("action").toString();
522 
523  if(act == "")
524  {
525  toolbar->addSeparator();
526  }
527  else
528  {
529  QAction* a = ApplicationController::getInstance().findAction(act);
530 
531  if(a != 0)
532  toolbar->addAction(a);
533  }
534  }
535 
536  sett.endArray();
537  sett.endGroup();
538 
539  bars.push_back(toolbar);
540  }
541 
542  sett.endGroup();
543 
544  return bars;
545 }
546 
547 void te::qt::af::SaveState(QMainWindow* mainWindow)
548 {
549  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
550 
551  sett.beginGroup("mainWindow");
552  sett.setValue("geometry", mainWindow->saveGeometry());
553  sett.setValue("windowState", mainWindow->saveState());
554  sett.endGroup();
555 }
556 
557 void te::qt::af::RestoreState(QMainWindow* mainWindow)
558 {
559  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
560 
561  sett.beginGroup("mainWindow");
562  mainWindow->restoreGeometry(sett.value("geometry").toByteArray());
563  mainWindow->restoreState(sett.value("windowState").toByteArray());
564  sett.endGroup();
565 }
566 
567 void te::qt::af::GetProjectInformationsFromSettings(QString& defaultAuthor, int& maxSaved)
568 {
569  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
570 
571  sett.beginGroup("projects");
572  defaultAuthor = sett.value("author_name").toString();
573  maxSaved = sett.value("recents_history_size").toInt();
574  sett.endGroup();
575 }
576 
577 void te::qt::af::SaveProjectInformationsOnSettings(const QString& defaultAuthor, const int& maxSaved)
578 {
579  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
580 
581  sett.beginGroup("projects");
582  sett.setValue("author_name", defaultAuthor);
583  sett.setValue("recents_history_size", maxSaved);
584  sett.endGroup();
585 }
586 
587 void te::qt::af::SaveLastDatasourceOnSettings(const QString& dsType)
588 {
589  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
590 
591  sett.setValue("projects/last datasource used", dsType);
592 }
593 
595 {
596  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
597 
598  sett.setValue("projects/openLastDataSource", openLast);
599 }
600 
602 {
603  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
604 
605  return sett.value("projects/last datasource used").toString();
606 }
607 
609 {
610  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
611 
612  QVariant variant = sett.value("projects/openLastDataSource");
613 
614  // If the option was never edited
615  if(variant.isNull() || !variant.isValid())
616  return true;
617 
618  return variant.toBool();
619 }
620 
622 {
623  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
624 
625  sett.beginGroup("toolbars");
626 
627  sett.beginGroup("File Tool Bar");
628  sett.setValue("name", "File Tool Bar");
629  sett.beginWriteArray("Actions");
630  sett.setArrayIndex(0);
631  sett.setValue("action", "File.New Project");
632  sett.setArrayIndex(1);
633  sett.setValue("action", "File.Open Project");
634  sett.setArrayIndex(2);
635  sett.setValue("action", "File.Save Project");
636  sett.setArrayIndex(3);
637  sett.setValue("action", "");
638  sett.setArrayIndex(4);
639  sett.setValue("action", "Project.New Folder");
640  sett.setArrayIndex(5);
641  sett.setValue("action", "Project.Add Layer.All Sources");
642  sett.endArray();
643  sett.endGroup();
644 
645  sett.beginGroup("View Tool Bar");
646  sett.setValue("name", "View Tool Bar");
647  sett.beginWriteArray("Actions");
648  sett.setArrayIndex(0);
649  sett.setValue("action", "View.Layer Explorer");
650  sett.setArrayIndex(1);
651  sett.setValue("action", "View.Map Display");
652  sett.setArrayIndex(2);
653  sett.setValue("action", "View.Data Table");
654  sett.setArrayIndex(3);
655  sett.setValue("action", "View.Style Explorer");
656  sett.endArray();
657  sett.endGroup();
658 
659  sett.beginGroup("Map Tool Bar");
660  sett.setValue("name", "Map Tool Bar");
661  sett.beginWriteArray("Actions");
662  sett.setArrayIndex(0);
663  sett.setValue("action", "Map.Draw");
664  sett.setArrayIndex(1);
665  sett.setValue("action", "Map.Previous Extent");
666  sett.setArrayIndex(2);
667  sett.setValue("action", "Map.Next Extent");
668  sett.setArrayIndex(3);
669  sett.setValue("action", "Map.Zoom Extent");
670  sett.setArrayIndex(4);
671  sett.setValue("action", "");
672  sett.setArrayIndex(5);
673  sett.setValue("action", "Map.Zoom In");
674  sett.setArrayIndex(6);
675  sett.setValue("action", "Map.Zoom Out");
676  sett.setArrayIndex(7);
677  sett.setValue("action", "Map.Pan");
678  sett.setArrayIndex(8);
679  sett.setValue("action", "");
680  sett.setArrayIndex(9);
681  sett.setValue("action", "Map.Info");
682  sett.setArrayIndex(10);
683  sett.setValue("action", "Map.Selection");
684  sett.endArray();
685  sett.endGroup();
686 
687  sett.endGroup();
688 
689  sett.beginGroup("projects");
690 
691  sett.setValue("author_name", "");
692  sett.setValue("recents_history_size", "8");
693 
694  sett.endGroup();
695 }
696 
697 QString te::qt::af::UnsavedStar(const QString windowTitle, bool isUnsaved)
698 {
699  QString result(windowTitle);
700 
701  if(isUnsaved)
702  {
703  if(result.at(result.count()-1) != '*')
704  result += "*";
705  }
706  else
707  {
708  if(result.at(result.count()-1) == '*')
709  result.remove((result.count()-1), 1);
710  }
711 
712  return result;
713 }
714 
716 {
717  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
718  QString hexColor = sett.value("display/defaultDisplayColor").toString();
719  QColor defaultColor;
720  defaultColor.setNamedColor(hexColor);
721  if(!defaultColor.isValid())
722  return Qt::white;
723 
724  return defaultColor;
725 }
726 
727 QString te::qt::af::GetStyleSheetFromColors(QColor primaryColor, QColor secondaryColor)
728 {
729  QString sty("alternate-background-color: ");
730  sty += "rgb(" + QString::number(secondaryColor.red()) + ", " + QString::number(secondaryColor.green());
731  sty += ", " + QString::number(secondaryColor.blue()) + ")";
732  sty += ";background-color: rgb(" + QString::number(primaryColor.red()) + ", " + QString::number(primaryColor.green());
733  sty += ", " + QString::number(primaryColor.blue()) + ");";
734 
735  return sty;
736 }
737 
739 {
740  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
741  //bool isChecked = sett.value("table/tableAlternateColors").toBool();
742  QColor pColor;
743  pColor.setNamedColor(sett.value("table/primaryColor").toString());
744  QColor sColor;
745  sColor.setNamedColor(sett.value("table/secondaryColor").toString());
746 
747  if(!pColor.isValid())
748  pColor = Qt::white;
749  if(!sColor.isValid())
750  sColor = Qt::white;
751 
752  return GetStyleSheetFromColors(pColor, sColor);
753 }
754 
756 {
757  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
758  bool isChecked = sett.value("table/tableAlternateColors").toBool();
759 
760  return isChecked;
761 }
762 
764 {
765  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
766 
767  sett.beginGroup("toolbars");
768  QStringList lst = sett.childGroups();
769  QStringList::iterator it;
770 
771  for(it=lst.begin(); it!=lst.end(); ++it)
772  {
773  int size = sett.beginReadArray(*it+"/Actions");
774 
775  for(int i=0; i<size; i++)
776  {
777  sett.setArrayIndex(i);
778 
779  QString v = sett.value("action").toString();
780 
781  if (v == act->objectName())
782  {
783  ApplicationController::getInstance().getToolBar(*it)->addAction(act);
784  break;
785  }
786  }
787 
788  sett.endArray();
789  }
790 
791  sett.endGroup();
792 }
793 
794 std::vector<std::string> te::qt::af::GetPluginsFiles()
795 {
796  std::vector<std::string> res;
797 
798  QStringList filters;
799 
800  filters << "*.teplg";
801 
802  QDir d(te::common::FindInTerraLibPath("share/terralib/plugins").c_str());
803 
804  QFileInfoList files = d.entryInfoList(filters, QDir::Files);
805 
806  foreach(QFileInfo file, files)
807  {
808  res.push_back(file.absoluteFilePath().toStdString());
809  }
810 
811  return res;
812 }
813 
814 std::vector<std::string> te::qt::af::GetDefaultPluginsNames()
815 {
816  std::vector<std::string> res;
817 
818 // Finding the Default plugins file.
819  std::string pluginsPath = te::qt::af::ApplicationController::getInstance().getAppPluginsPath().toStdString();
820 
821  if (pluginsPath == "")
822  return res;
823 
824 // Reading JSON
825  boost::property_tree::ptree pt;
826  boost::property_tree::json_parser::read_json(pluginsPath, pt);
827 
828  BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("plugins"))
829  {
830  res.push_back(v.second.get<std::string>("plugin"));
831  }
832 
833  return res;
834 }
835 
836 std::vector<std::string> te::qt::af::GetPluginsNames(const std::vector<std::string>& plgFiles)
837 {
838  std::vector<std::string> res;
839  std::vector<std::string>::const_iterator it;
840 
841  for(it=plgFiles.begin(); it!=plgFiles.end(); ++it)
842  {
843  boost::property_tree::ptree p;
844  boost::property_tree::read_xml(*it, p, boost::property_tree::xml_parser::trim_whitespace);
845 
846  res.push_back(p.get<std::string>("PluginInfo.Name"));
847  }
848 
849  return res;
850 }
851 
853 {
854  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
855 
856  return sett.value("configuration/generation").toString();
857 }
858 
859 void te::qt::af::SetDateTime(const QString& dateTime)
860 {
861  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
862 
863  sett.setValue("configuration/generation", dateTime);
864 }
865 
867 {
868  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
869 
870  QFileInfo info(sett.fileName());
871 
872  return info.absolutePath();
873 }
874 
875 void te::qt::af::UpdateUserSettingsFile(const QString& fileName, const bool& removeOlder)
876 {
877  QFileInfo info(fileName);
880 
881  if(info.exists())
882  info.dir().remove(info.fileName());
883 
884  std::string olderFile = appSett.getValue("Application.UserSettingsFile.<xmlattr>.xlink:href");
885 
886  appSett.setValue("Application.UserSettingsFile.<xmlattr>.xlink:href", fileName.toStdString());
887 
888  if(removeOlder)
889  {
890  info.setFile(olderFile.c_str());
891  info.dir().remove(info.fileName());
892  }
893 
894  info.setFile(fileName);
895 
896  if(!info.exists())
897  {
898 #if BOOST_VERSION > 105600
899  boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
900 #else
901  boost::property_tree::xml_writer_settings<char> settings('\t', 1);
902 #endif
903  boost::property_tree::write_xml(fileName.toStdString(), usrSett.getAllSettings(), std::locale(), settings);
904  }
905 
906  usrSett.load(fileName.toStdString());
907 }
908 
909 void te::qt::af::WriteDefaultProjectFile(const QString& fileName)
910 {
911  boost::property_tree::ptree p;
912 
913  std::string schema_location = te::common::FindInTerraLibPath("share/terralib/schemas/terralib/qt/af/project.xsd");
914 
915  //Header
916  p.add("Project.<xmlattr>.xmlns:xsd", "http://www.w3.org/2001/XMLSchema-instance");
917  p.add("Project.<xmlattr>.xmlns:te_map", "http://www.terralib.org/schemas/maptools");
918  p.add("Project.<xmlattr>.xmlns:te_qt_af", "http://www.terralib.org/schemas/qt/af");
919  p.add("Project.<xmlattr>.xmlns", "http://www.terralib.org/schemas/qt/af");
920  p.add("Project.<xmlattr>.xsd:schemaLocation", "http://www.terralib.org/schemas/qt/af " + schema_location);
921  p.add("Project.<xmlattr>.version", TERRALIB_VERSION_STRING);
922 
923  //Contents
924  p.add("Project.Title", "Default project");
925  p.add("Project.Author", "");
926  p.add("Project.ComponentList", "");
927  p.add("Project.te_map:LayerList", "");
928 
929  //Store file
930 #if BOOST_VERSION > 105600
931  boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
932 #else
933  boost::property_tree::xml_writer_settings<char> settings('\t', 1);
934 #endif
935  boost::property_tree::write_xml(fileName.toStdString(), p, std::locale(), settings);
936 }
937 
939 {
940  QString fileName = qApp->applicationDirPath() + "/../.generated";
941 
942  QFile f(fileName);
943  if (!f.open(QFile::ReadOnly | QFile::Text))
944  return "";
945 
946  QTextStream in(&f);
947  QString s = in.readAll();
948 
949  f.close();
950 
951  return s;
952 }
953 
955 {
956  QString title = te::qt::af::ApplicationController::getInstance().getAppTitle() + " - ";
957  title += TE_TR("Project:");
958  title += " ";
959  title += project.getTitle().c_str();
960  title += " - ";
961 
962  boost::filesystem::path p(project.getFileName());
963 
964  std::string filename = p.filename().string();
965 
966  title += filename.c_str();
967 
968  return title;
969 }
970 
972 {
973  QString appName = te::qt::af::ApplicationController::getInstance().getAppName();
974  QString appProjectExtension = te::qt::af::ApplicationController::getInstance().getAppProjectExtension();
975  QString extensionFilter = appName;
976  extensionFilter += QString(" (*.");
977  extensionFilter += appProjectExtension + ")";
978 
979  return extensionFilter;
980 }
TEQTAFEXPORT void RemoveToolBarFromSettings(QToolBar *bar)
Removes a tool bar from the settings.
Definition: Utils.cpp:484
TECOMMONEXPORT std::string ConvertLatin1UTFString(const std::string &data, const bool &toUtf=true)
Converts a string from latin1 to utf8 and vice-versa.
Definition: BoostUtils.cpp:169
TEQTAFEXPORT void UpdateToolBarsInTheSettings()
Update plugins file.
Definition: Utils.cpp:453
std::map< std::string, DataSourceInfoPtr >::iterator iterator
TEQTAFEXPORT QColor GetDefaultDisplayColorFromSettings()
Definition: Utils.cpp:715
TEQTAFEXPORT std::vector< std::string > GetPluginsFiles()
Definition: Utils.cpp:794
const std::string & getTitle() const
It gets the title of the project.
Definition: Project.cpp:51
This class models a XML reader object.
Definition: Reader.h:55
TEQTAFEXPORT void CreateDefaultSettings()
Creates a default QSettings.
Definition: Utils.cpp:621
virtual void writeStartElement(const std::string &qName)=0
TECOMMONEXPORT std::string FindInTerraLibPath(const std::string &p)
Returns the path relative to a directory or file in the context of TerraLib.
TEQTAFEXPORT void SaveOpenLastProjectOnSettings(bool openLast)
Definition: Utils.cpp:594
TEQTAFEXPORT QString GetWindowTitle(const Project &project)
Return a QString with the new window title based on the project informations.
Definition: Utils.cpp:954
virtual void writeValue(const std::string &value)=0
A singleton for managing application settings applied to a single user.
TEQTAFEXPORT void SetDateTime(const QString &dateTime)
Definition: Utils.cpp:859
TEQTAFEXPORT std::vector< QToolBar * > ReadToolBarsFromSettings(QWidget *barsParent=0)
Returns a vector of tool bars registered in the QSettings.
Definition: Utils.cpp:493
This class models the concept of a project for the TerraLib Application Framework.
static te::xml::AbstractWriter * make()
It creates a new XML writer using the dafault implementation.
TEQTAFEXPORT QString UnsavedStar(const QString windowTitle, bool isUnsaved)
Unsaved star.
Definition: Utils.cpp:697
void setValue(const std::string &key, const std::string &value)
It stores the value according to the key.
static te::xml::Reader * make()
It creates a new XML reader using the dafault implementation.
TEQTAFEXPORT void SaveLastDatasourceOnSettings(const QString &dsType)
Definition: Utils.cpp:587
This class models a XML writer object.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
TEQTAFEXPORT void SaveState(QMainWindow *mainWindow)
Definition: Utils.cpp:547
TEQTAFEXPORT void UpdateUserSettings(const QStringList &prjFiles, const QStringList &prjTitles, const std::string &userConfigFile)
Updates user settings file section about information of the projects.
Definition: Utils.cpp:320
TEQTAFEXPORT bool GetAlternateRowColorsFromSettings()
Definition: Utils.cpp:755
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
TEQTAFEXPORT void SaveDataSourcesFile()
Saves data sources file.
Definition: Utils.cpp:413
TEQTAFEXPORT void AddToolBarToSettings(QToolBar *bar)
Update settings with a new tool bar.
Definition: Utils.cpp:473
const std::list< te::map::AbstractLayerPtr > & getTopLayers() const
It gets all the top layers of the project (folder and single layers).
Definition: Project.cpp:67
virtual void writeElement(const std::string &qName, const std::string &value)=0
te::map::AbstractLayer * read(te::xml::Reader &reader) const
Definition: Layer.cpp:144
TEQTAFEXPORT void UpdateUserSettingsFile(const QString &fileName, const bool &removeOlder=true)
Changes the user settings file location.
Definition: Utils.cpp:875
TEQTAFEXPORT void AddActionToCustomToolbars(QAction *act)
Check QSettings for existance of act and adds it if necessary.
Definition: Utils.cpp:763
TEQTAFEXPORT void SaveProjectInformationsOnSettings(const QString &defaultAuthor, const int &maxSaved)
Definition: Utils.cpp:577
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
TEQTAFEXPORT std::vector< std::string > GetDefaultPluginsNames()
Definition: Utils.cpp:814
virtual void writeToFile()=0
TEQTAFEXPORT QString GetDefaultConfigFileOutputDir()
Returns the default path for output of configuration file.
Definition: Utils.cpp:866
TEQTAFEXPORT QString GetStyleSheetFromColors(QColor primaryColor, QColor secondaryColor)
Definition: Utils.cpp:727
void setProjectAsChanged(const bool &changed)
It sets the project status as changed or not.
Definition: Project.cpp:226
A singleton for managing application settings applied to the whole system (all users).
TEDATAACCESSEXPORT void Save(const std::string &fileName)
Definition: Serializer.cpp:201
TEQTAFEXPORT void GetProjectInformationsFromSettings(QString &defaultAuthor, int &maxSaved)
Definition: Utils.cpp:567
An exception class for the TerraLib Application Framework.
TEQTAFEXPORT void RestoreState(QMainWindow *mainWindow)
Definition: Utils.cpp:557
TEQTAFEXPORT QString GetGenerationDate()
Returns the date and time of generated binary.
Definition: Utils.cpp:938
TEQTAFEXPORT QString GetDateTime()
Definition: Utils.cpp:852
TEQTAFEXPORT QString GetExtensionFilter()
Return extension filter string.
Definition: Utils.cpp:971
TEQTAFEXPORT void WriteDefaultProjectFile(const QString &fileName)
Writes the configuration file. It updates the application settings.
Definition: Utils.cpp:909
void setFileName(const std::string &fName)
It sets the filename where the project will be saved.
Definition: Project.cpp:215
TEQTAFEXPORT QString GetStyleSheetFromSettings()
Definition: Utils.cpp:738
virtual void writeAttribute(const std::string &attName, const std::string &value)=0
QString GetStyleSheetFromColors(QColor primaryColor, QColor secondaryColor)
Definition: TableWidget.cpp:23
const std::string & getAuthor() const
It gets the author of the project.
Definition: Project.cpp:62
TEQTAFEXPORT Project * ReadProject(const std::string &uri)
Reads and return a te::qt::af::Project from the file.
Definition: Utils.cpp:77
This class models the concept of a project for the TerraLib Application Framework.
Definition: Project.h:50
void write(const te::map::AbstractLayer *alayer, te::xml::AbstractWriter &writer) const
Definition: Layer.cpp:158
virtual NodeType getNodeType() const =0
It return the type of node read.
void load(const std::string &fileName)
It tries to find a default config file based on system macros and default condigurations.
void AddToolbarAndActions(QToolBar *bar, QSettings &sett)
Definition: Utils.cpp:432
virtual std::string getElementValue() const =0
It returns the element data value in the case of VALUE node.
virtual void writeEndElement(const std::string &qName)=0
virtual void writeStartDocument(const std::string &encoding, const std::string &standalone)=0
Utility routines for the TerraLib Application Framework module.
TEQTAFEXPORT bool GetOpenLastProjectFromSettings()
Definition: Utils.cpp:608
const boost::property_tree::ptree & getAllSettings() const
It return a reading reference to the internal settings.
std::string getValue(const std::string &key)
It returns the value for a given key or empty.
A class for xml serialization formatting strings.
const std::string & getFileName() const
It gets the filename where the project is saved.
Definition: Project.cpp:221
#define TERRALIB_APPLICATION_DATASOURCE_FILE_NAME
The default name for the application file containing the list of data sources.
Definition: Config.h:44
TEQTAFEXPORT void Save(const Project &project, const std::string &uri)
Saves the informations of the project in the uri file.
Definition: Utils.cpp:194
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
virtual bool next()=0
It gets the next event to be read.
TEQTAFEXPORT std::vector< std::string > GetPluginsNames(const std::vector< std::string > &plgFiles)
Definition: Utils.cpp:836
The base API for controllers of TerraLib applications.
TEQTAFEXPORT QString GetLastDatasourceFromSettings()
Definition: Utils.cpp:601
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
TEDATAACCESSEXPORT void ReadDataSourceInfo(const std::string &datasourcesFileName)
Definition: Serializer.cpp:90