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 
429 
430  te::serialize::xml::Save(fileName.toString().toStdString());
431 
433 }
434 
435 
436 void AddToolbarAndActions(QToolBar* bar, QSettings& sett)
437 {
438  sett.beginGroup(bar->objectName());
439 
440  sett.setValue("name", bar->objectName());
441 
442  sett.beginWriteArray("Actions");
443 
444  QList<QAction*> acts = bar->actions();
445 
446  for(int i=0; i<acts.size(); i++)
447  {
448  sett.setArrayIndex(i);
449  sett.setValue("action", acts.at(i)->objectName());
450  }
451 
452  sett.endArray();
453 
454  sett.endGroup();
455 }
456 
458 {
459  std::vector<QToolBar*> bars = te::qt::af::ApplicationController::getInstance().getToolBars();
460  std::vector<QToolBar*>::const_iterator it;
461  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
462 
463  sett.beginGroup("toolbars");
464 
465  for (it = bars.begin(); it != bars.end(); ++it)
466  {
467  QToolBar* bar = *it;
468 
469  sett.remove(bar->objectName());
470 
471  AddToolbarAndActions(bar, sett);
472  }
473 
474  sett.endGroup();
475 }
476 
478 {
479  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
480 
481  sett.beginGroup("toolbars");
482 
483  AddToolbarAndActions(bar, sett);
484 
485  sett.endGroup();
486 }
487 
489 {
490  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
491 
492  sett.beginGroup("toolbars");
493  sett.remove(bar->objectName());
494  sett.endGroup();
495 }
496 
497 std::vector<QToolBar*> te::qt::af::ReadToolBarsFromSettings(QWidget* barsParent)
498 {
499  std::vector<QToolBar*> bars;
500 
501  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
502 
503  sett.beginGroup("toolbars");
504  QStringList lst = sett.childGroups();
505 
506  QStringList::iterator it;
507 
508  for(it=lst.begin(); it != lst.end(); ++it)
509  {
510  QString gr = *it;
511 
512  sett.beginGroup(gr);
513 
514  QString grName = sett.value("name").toString();
515 
516  int size = sett.beginReadArray("Actions");
517 
518  QToolBar* toolbar = new QToolBar(barsParent);
519  toolbar->setObjectName(grName);
520  toolbar->setWindowTitle(grName);
521 
522  for(int i=0; i<size; i++)
523  {
524  sett.setArrayIndex(i);
525  QString act = sett.value("action").toString();
526 
527  if(act == "")
528  {
529  toolbar->addSeparator();
530  }
531  else
532  {
533  QAction* a = ApplicationController::getInstance().findAction(act);
534 
535  if(a != 0)
536  toolbar->addAction(a);
537  }
538  }
539 
540  sett.endArray();
541  sett.endGroup();
542 
543  bars.push_back(toolbar);
544  }
545 
546  sett.endGroup();
547 
548  return bars;
549 }
550 
551 void te::qt::af::SaveState(QMainWindow* mainWindow)
552 {
553  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
554 
555  sett.beginGroup("mainWindow");
556  sett.setValue("geometry", mainWindow->saveGeometry());
557  sett.setValue("windowState", mainWindow->saveState());
558  sett.endGroup();
559 }
560 
561 void te::qt::af::RestoreState(QMainWindow* mainWindow)
562 {
563  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
564 
565  sett.beginGroup("mainWindow");
566  mainWindow->restoreGeometry(sett.value("geometry").toByteArray());
567  mainWindow->restoreState(sett.value("windowState").toByteArray());
568  sett.endGroup();
569 }
570 
571 void te::qt::af::GetProjectInformationsFromSettings(QString& defaultAuthor, int& maxSaved)
572 {
573  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
574 
575  sett.beginGroup("projects");
576  defaultAuthor = sett.value("author_name").toString();
577  maxSaved = sett.value("recents_history_size").toInt();
578  sett.endGroup();
579 }
580 
581 void te::qt::af::SaveProjectInformationsOnSettings(const QString& defaultAuthor, const int& maxSaved)
582 {
583  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
584 
585  sett.beginGroup("projects");
586  sett.setValue("author_name", defaultAuthor);
587  sett.setValue("recents_history_size", maxSaved);
588  sett.endGroup();
589 }
590 
591 void te::qt::af::SaveLastDatasourceOnSettings(const QString& dsType)
592 {
593  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
594 
595  sett.setValue("projects/last datasource used", dsType);
596 }
597 
599 {
600  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
601 
602  sett.setValue("projects/openLastDataSource", openLast);
603 }
604 
606 {
607  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
608 
609  return sett.value("projects/last datasource used").toString();
610 }
611 
613 {
614  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
615 
616  QVariant variant = sett.value("projects/openLastDataSource");
617 
618  // If the option was never edited
619  if(variant.isNull() || !variant.isValid())
620  return true;
621 
622  return variant.toBool();
623 }
624 
626 {
627  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
628 
629  sett.beginGroup("toolbars");
630 
631  sett.beginGroup("File Tool Bar");
632  sett.setValue("name", "File Tool Bar");
633  sett.beginWriteArray("Actions");
634  sett.setArrayIndex(0);
635  sett.setValue("action", "File.New Project");
636  sett.setArrayIndex(1);
637  sett.setValue("action", "File.Open Project");
638  sett.setArrayIndex(2);
639  sett.setValue("action", "File.Save Project");
640  sett.setArrayIndex(3);
641  sett.setValue("action", "");
642  sett.setArrayIndex(4);
643  sett.setValue("action", "Project.New Folder");
644  sett.setArrayIndex(5);
645  sett.setValue("action", "Project.Add Layer.All Sources");
646  sett.endArray();
647  sett.endGroup();
648 
649  sett.beginGroup("View Tool Bar");
650  sett.setValue("name", "View Tool Bar");
651  sett.beginWriteArray("Actions");
652  sett.setArrayIndex(0);
653  sett.setValue("action", "View.Layer Explorer");
654  sett.setArrayIndex(1);
655  sett.setValue("action", "View.Map Display");
656  sett.setArrayIndex(2);
657  sett.setValue("action", "View.Data Table");
658  sett.setArrayIndex(3);
659  sett.setValue("action", "View.Style Explorer");
660  sett.endArray();
661  sett.endGroup();
662 
663  sett.beginGroup("Map Tool Bar");
664  sett.setValue("name", "Map Tool Bar");
665  sett.beginWriteArray("Actions");
666  sett.setArrayIndex(0);
667  sett.setValue("action", "Map.Draw");
668  sett.setArrayIndex(1);
669  sett.setValue("action", "Map.Previous Extent");
670  sett.setArrayIndex(2);
671  sett.setValue("action", "Map.Next Extent");
672  sett.setArrayIndex(3);
673  sett.setValue("action", "Map.Zoom Extent");
674  sett.setArrayIndex(4);
675  sett.setValue("action", "");
676  sett.setArrayIndex(5);
677  sett.setValue("action", "Map.Zoom In");
678  sett.setArrayIndex(6);
679  sett.setValue("action", "Map.Zoom Out");
680  sett.setArrayIndex(7);
681  sett.setValue("action", "Map.Pan");
682  sett.setArrayIndex(8);
683  sett.setValue("action", "");
684  sett.setArrayIndex(9);
685  sett.setValue("action", "Map.Info");
686  sett.setArrayIndex(10);
687  sett.setValue("action", "Map.Selection");
688  sett.endArray();
689  sett.endGroup();
690 
691  sett.endGroup();
692 
693  sett.beginGroup("projects");
694 
695  sett.setValue("author_name", "");
696  sett.setValue("recents_history_size", "8");
697 
698  sett.endGroup();
699 }
700 
701 QString te::qt::af::UnsavedStar(const QString windowTitle, bool isUnsaved)
702 {
703  QString result(windowTitle);
704 
705  if(isUnsaved)
706  {
707  if(result.at(result.count()-1) != '*')
708  result += "*";
709  }
710  else
711  {
712  if(result.at(result.count()-1) == '*')
713  result.remove((result.count()-1), 1);
714  }
715 
716  return result;
717 }
718 
720 {
721  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
722  QString hexColor = sett.value("display/defaultDisplayColor").toString();
723  QColor defaultColor;
724  defaultColor.setNamedColor(hexColor);
725  if(!defaultColor.isValid())
726  return Qt::white;
727 
728  return defaultColor;
729 }
730 
731 QString te::qt::af::GetStyleSheetFromColors(QColor primaryColor, QColor secondaryColor)
732 {
733  QString sty("alternate-background-color: ");
734  sty += "rgb(" + QString::number(secondaryColor.red()) + ", " + QString::number(secondaryColor.green());
735  sty += ", " + QString::number(secondaryColor.blue()) + ")";
736  sty += ";background-color: rgb(" + QString::number(primaryColor.red()) + ", " + QString::number(primaryColor.green());
737  sty += ", " + QString::number(primaryColor.blue()) + ");";
738 
739  return sty;
740 }
741 
743 {
744  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
745  //bool isChecked = sett.value("table/tableAlternateColors").toBool();
746  QColor pColor;
747  pColor.setNamedColor(sett.value("table/primaryColor").toString());
748  QColor sColor;
749  sColor.setNamedColor(sett.value("table/secondaryColor").toString());
750 
751  if(!pColor.isValid())
752  pColor = Qt::white;
753  if(!sColor.isValid())
754  sColor = Qt::white;
755 
756  return GetStyleSheetFromColors(pColor, sColor);
757 }
758 
760 {
761  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
762  bool isChecked = sett.value("table/tableAlternateColors").toBool();
763 
764  return isChecked;
765 }
766 
768 {
769  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
770 
771  sett.beginGroup("toolbars");
772  QStringList lst = sett.childGroups();
773  QStringList::iterator it;
774 
775  for(it=lst.begin(); it!=lst.end(); ++it)
776  {
777  int size = sett.beginReadArray(*it+"/Actions");
778 
779  for(int i=0; i<size; i++)
780  {
781  sett.setArrayIndex(i);
782 
783  QString v = sett.value("action").toString();
784 
785  if (v == act->objectName())
786  {
787  ApplicationController::getInstance().getToolBar(*it)->addAction(act);
788  break;
789  }
790  }
791 
792  sett.endArray();
793  }
794 
795  sett.endGroup();
796 }
797 
798 std::vector<std::string> te::qt::af::GetPluginsFiles()
799 {
800  std::vector<std::string> res;
801 
802  QStringList filters;
803 
804  filters << "*.teplg";
805 
806  QDir d(te::common::FindInTerraLibPath("share/terralib/plugins").c_str());
807 
808  QFileInfoList files = d.entryInfoList(filters, QDir::Files);
809 
810  foreach(QFileInfo file, files)
811  {
812  res.push_back(file.absoluteFilePath().toStdString());
813  }
814 
815  return res;
816 }
817 
818 std::vector<std::string> te::qt::af::GetDefaultPluginsNames()
819 {
820  std::vector<std::string> res;
821 
822 // Finding the Default plugins file.
823  std::string pluginsPath = te::qt::af::ApplicationController::getInstance().getAppPluginsPath().toStdString();
824 
825  if (pluginsPath == "")
826  return res;
827 
828 // Reading JSON
829  boost::property_tree::ptree pt;
830  boost::property_tree::json_parser::read_json(pluginsPath, pt);
831 
832  BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("plugins"))
833  {
834  res.push_back(v.second.get<std::string>("plugin"));
835  }
836 
837  return res;
838 }
839 
840 std::vector<std::string> te::qt::af::GetPluginsNames(const std::vector<std::string>& plgFiles)
841 {
842  std::vector<std::string> res;
843  std::vector<std::string>::const_iterator it;
844 
845  for(it=plgFiles.begin(); it!=plgFiles.end(); ++it)
846  {
847  boost::property_tree::ptree p;
848  boost::property_tree::read_xml(*it, p, boost::property_tree::xml_parser::trim_whitespace);
849 
850  res.push_back(p.get<std::string>("PluginInfo.Name"));
851  }
852 
853  return res;
854 }
855 
857 {
858  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
859 
860  return sett.value("configuration/generation").toString();
861 }
862 
863 void te::qt::af::SetDateTime(const QString& dateTime)
864 {
865  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
866 
867  sett.setValue("configuration/generation", dateTime);
868 }
869 
871 {
872  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
873 
874  QFileInfo info(sett.fileName());
875 
876  return info.absolutePath();
877 }
878 
879 void te::qt::af::UpdateUserSettingsFile(const QString& fileName, const bool& removeOlder)
880 {
881  QFileInfo info(fileName);
884 
885  if(info.exists())
886  info.dir().remove(info.fileName());
887 
888  std::string olderFile = appSett.getValue("Application.UserSettingsFile.<xmlattr>.xlink:href");
889 
890  appSett.setValue("Application.UserSettingsFile.<xmlattr>.xlink:href", fileName.toStdString());
891 
892  if(removeOlder)
893  {
894  info.setFile(olderFile.c_str());
895  info.dir().remove(info.fileName());
896  }
897 
898  info.setFile(fileName);
899 
900  if(!info.exists())
901  {
902 #if BOOST_VERSION > 105600
903  boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
904 #else
905  boost::property_tree::xml_writer_settings<char> settings('\t', 1);
906 #endif
907  boost::property_tree::write_xml(fileName.toStdString(), usrSett.getAllSettings(), std::locale(), settings);
908  }
909 
910  usrSett.load(fileName.toStdString());
911 }
912 
913 void te::qt::af::WriteDefaultProjectFile(const QString& fileName)
914 {
915  boost::property_tree::ptree p;
916 
917  std::string schema_location = te::common::FindInTerraLibPath("share/terralib/schemas/terralib/qt/af/project.xsd");
918 
919  //Header
920  p.add("Project.<xmlattr>.xmlns:xsd", "http://www.w3.org/2001/XMLSchema-instance");
921  p.add("Project.<xmlattr>.xmlns:te_map", "http://www.terralib.org/schemas/maptools");
922  p.add("Project.<xmlattr>.xmlns:te_qt_af", "http://www.terralib.org/schemas/qt/af");
923  p.add("Project.<xmlattr>.xmlns", "http://www.terralib.org/schemas/qt/af");
924  p.add("Project.<xmlattr>.xsd:schemaLocation", "http://www.terralib.org/schemas/qt/af " + schema_location);
925  p.add("Project.<xmlattr>.version", TERRALIB_VERSION_STRING);
926 
927  //Contents
928  p.add("Project.Title", "Default project");
929  p.add("Project.Author", "");
930  p.add("Project.ComponentList", "");
931  p.add("Project.te_map:LayerList", "");
932 
933  //Store file
934 #if BOOST_VERSION > 105600
935  boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
936 #else
937  boost::property_tree::xml_writer_settings<char> settings('\t', 1);
938 #endif
939  boost::property_tree::write_xml(fileName.toStdString(), p, std::locale(), settings);
940 }
941 
943 {
944  QString fileName = qApp->applicationDirPath() + "/../.generated";
945 
946  QFile f(fileName);
947  if (!f.open(QFile::ReadOnly | QFile::Text))
948  return "";
949 
950  QTextStream in(&f);
951  QString s = in.readAll();
952 
953  f.close();
954 
955  return s;
956 }
957 
959 {
960  QString title = te::qt::af::ApplicationController::getInstance().getAppTitle() + " - ";
961  title += TE_TR("Project:");
962  title += " ";
963  title += project.getTitle().c_str();
964  title += " - ";
965 
966  boost::filesystem::path p(project.getFileName());
967 
968  std::string filename = p.filename().string();
969 
970  title += filename.c_str();
971 
972  return title;
973 }
974 
976 {
977  QString appName = te::qt::af::ApplicationController::getInstance().getAppName();
978  QString appProjectExtension = te::qt::af::ApplicationController::getInstance().getAppProjectExtension();
979  QString extensionFilter = appName;
980  extensionFilter += QString(" (*.");
981  extensionFilter += appProjectExtension + ")";
982 
983  return extensionFilter;
984 }
TEQTAFEXPORT void RemoveToolBarFromSettings(QToolBar *bar)
Removes a tool bar from the settings.
Definition: Utils.cpp:488
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:457
std::map< std::string, DataSourceInfoPtr >::iterator iterator
TEQTAFEXPORT QColor GetDefaultDisplayColorFromSettings()
Definition: Utils.cpp:719
TEQTAFEXPORT std::vector< std::string > GetPluginsFiles()
Definition: Utils.cpp:798
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:625
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:598
TEQTAFEXPORT QString GetWindowTitle(const Project &project)
Return a QString with the new window title based on the project informations.
Definition: Utils.cpp:958
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:863
TEQTAFEXPORT std::vector< QToolBar * > ReadToolBarsFromSettings(QWidget *barsParent=0)
Returns a vector of tool bars registered in the QSettings.
Definition: Utils.cpp:497
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:701
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:591
This class models a XML writer object.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
TEQTAFEXPORT void SaveState(QMainWindow *mainWindow)
Definition: Utils.cpp:551
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:759
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:477
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:879
TEQTAFEXPORT void AddActionToCustomToolbars(QAction *act)
Check QSettings for existance of act and adds it if necessary.
Definition: Utils.cpp:767
TEQTAFEXPORT void SaveProjectInformationsOnSettings(const QString &defaultAuthor, const int &maxSaved)
Definition: Utils.cpp:581
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
TEQTAFEXPORT std::vector< std::string > GetDefaultPluginsNames()
Definition: Utils.cpp:818
virtual void writeToFile()=0
TEQTAFEXPORT QString GetDefaultConfigFileOutputDir()
Returns the default path for output of configuration file.
Definition: Utils.cpp:870
TEQTAFEXPORT QString GetStyleSheetFromColors(QColor primaryColor, QColor secondaryColor)
Definition: Utils.cpp:731
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:205
TEQTAFEXPORT void GetProjectInformationsFromSettings(QString &defaultAuthor, int &maxSaved)
Definition: Utils.cpp:571
An exception class for the TerraLib Application Framework.
TEQTAFEXPORT void RestoreState(QMainWindow *mainWindow)
Definition: Utils.cpp:561
TEQTAFEXPORT QString GetGenerationDate()
Returns the date and time of generated binary.
Definition: Utils.cpp:942
TEQTAFEXPORT QString GetDateTime()
Definition: Utils.cpp:856
TEQTAFEXPORT QString GetExtensionFilter()
Return extension filter string.
Definition: Utils.cpp:975
TEQTAFEXPORT void WriteDefaultProjectFile(const QString &fileName)
Writes the configuration file. It updates the application settings.
Definition: Utils.cpp:913
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:742
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:436
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:612
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:41
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:840
static void formatDataSourceInfos(const bool &encode)
Formats all data source informations registered in the te::da::DataSourceInfoManager object...
The base API for controllers of TerraLib applications.
TEQTAFEXPORT QString GetLastDatasourceFromSettings()
Definition: Utils.cpp:605
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
TEDATAACCESSEXPORT void ReadDataSourceInfo(const std::string &datasourcesFileName)
Definition: Serializer.cpp:90