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) 2011-2012 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 // TerraLib
27 #include "../../common/PlatformUtils.h"
28 #include "../../common/SystemApplicationSettings.h"
29 #include "../../common/UserApplicationSettings.h"
30 #include "../../dataaccess/serialization/xml/Serializer.h"
31 #include "../../maptools/AbstractLayer.h"
32 #include "../../plugin/PluginManager.h"
33 #include "../../plugin/PluginInfo.h"
34 #include "../../maptools/serialization/xml/Layer.h"
35 #include "../../xml/Reader.h"
36 #include "../../xml/ReaderFactory.h"
37 #include "../../xml/Writer.h"
38 #include "../../Version.h"
39 #include "ApplicationController.h"
40 #include "Exception.h"
41 #include "Project.h"
42 #include "Utils.h"
43 #include "XMLFormatter.h"
44 
45 // STL
46 #include <cassert>
47 #include <fstream>
48 #include <memory>
49 
50 // Boost
51 #include <boost/filesystem.hpp>
52 #include <boost/format.hpp>
53 #include <boost/property_tree/ptree.hpp>
54 #include <boost/algorithm/string/replace.hpp>
55 
56 // Qt
57 #include <QDir>
58 #include <QFileInfo>
59 #include <QSettings>
60 #include <QString>
61 #include <QTextStream>
62 #include <QApplication>
63 #include <QAction>
64 #include <QMainWindow>
65 #include <QMessageBox>
66 #include <QToolBar>
67 
69 {
70  boost::filesystem::path furi(uri);
71 
72  if (!boost::filesystem::exists(furi) || !boost::filesystem::is_regular_file(furi))
73  throw Exception((boost::format(TE_TR("Could not read project file: %1%.")) % uri).str());
74 
75  std::auto_ptr<te::xml::Reader> xmlReader(te::xml::ReaderFactory::make());
76  xmlReader->setValidationScheme(false);
77 
78  xmlReader->read(uri);
79 
80  if(!xmlReader->next())
81  throw Exception((boost::format(TE_TR("Could not read project information in the file: %1%.")) % uri).str());
82 
83  if(xmlReader->getNodeType() != te::xml::START_ELEMENT)
84  throw Exception((boost::format(TE_TR("Error reading the document %1%, the start element wasn't found.")) % uri).str());
85 
86  if(xmlReader->getElementLocalName() != "Project")
87  throw Exception((boost::format(TE_TR("The first tag in the document %1% is not 'Project'.")) % uri).str());
88 
89  Project* proj = ReadProject(*xmlReader);
90 
91  proj->setFileName(uri);
92 
93  XMLFormatter::format(proj, false);
94 
95  proj->setProjectAsChanged(false);
96 
97  return proj;
98 
99 }
100 
102 {
103  std::auto_ptr<Project> project(new Project);
104 
105  reader.next();
106  assert(reader.getNodeType() == te::xml::START_ELEMENT);
107  assert(reader.getElementLocalName() == "Title");
108 
109  reader.next();
110  assert(reader.getNodeType() == te::xml::VALUE);
111  project->setTitle(reader.getElementValue());
112  reader.next(); // End element
113 
114  reader.next();
115  assert(reader.getNodeType() == te::xml::START_ELEMENT);
116  assert(reader.getElementLocalName() == "Author");
117 
118  reader.next();
119 
120  if(reader.getNodeType() == te::xml::VALUE)
121  {
122  project->setAuthor(reader.getElementValue());
123  reader.next(); // End element
124  }
125 
126  reader.next();
127  assert(reader.getNodeType() == te::xml::START_ELEMENT);
128  assert(reader.getElementLocalName() == "ComponentList");
129  reader.next(); // End element
130  reader.next(); // next after </ComponentList>
131 
132  assert(reader.getNodeType() == te::xml::START_ELEMENT);
133  assert(reader.getElementLocalName() == "LayerList");
134 
135  reader.next();
136 
138 
139  // Read the layers
140  while((reader.getNodeType() != te::xml::END_ELEMENT) &&
141  (reader.getElementLocalName() != "LayerList"))
142  {
143  te::map::AbstractLayerPtr layer(lserial.read(reader));
144 
145  assert(layer.get());
146 
147  project->add(layer);
148  }
149 
150  assert(reader.getNodeType() == te::xml::END_ELEMENT);
151  assert(reader.getElementLocalName() == "LayerList");
152 
153  reader.next();
154  assert((reader.getNodeType() == te::xml::END_ELEMENT) || (reader.getNodeType() == te::xml::END_DOCUMENT));
155  assert(reader.getElementLocalName() == "Project");
156 
157  project->setProjectAsChanged(false);
158 
159  return project.release();
160 }
161 
162 void te::qt::af::Save(const te::qt::af::Project& project, const std::string& uri)
163 {
164  std::ofstream fout(uri.c_str(), std::ios_base::trunc);
165 
166  te::xml::Writer w(fout);
167 
168  Project p(project);
169 
170  XMLFormatter::format(&p, true);
171 
172  Save(p, w);
173 
174  XMLFormatter::format(&p, false);
175 
176  fout.close();
177 }
178 
180 {
181  std::string schema_loc = te::common::FindInTerraLibPath("share/terralib/schemas/terralib/qt/af/project.xsd");
182 
183  writer.writeStartDocument("UTF-8", "no");
184 
185  writer.writeStartElement("Project");
186 
187  boost::replace_all(schema_loc, " ", "%20");
188 
189  writer.writeAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema-instance");
190  writer.writeAttribute("xmlns:te_da", "http://www.terralib.org/schemas/dataaccess");
191  writer.writeAttribute("xmlns:te_map", "http://www.terralib.org/schemas/maptools");
192  writer.writeAttribute("xmlns:te_qt_af", "http://www.terralib.org/schemas/common/af");
193 
194  writer.writeAttribute("xmlns:se", "http://www.opengis.net/se");
195  writer.writeAttribute("xmlns:ogc", "http://www.opengis.net/ogc");
196  writer.writeAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
197 
198  writer.writeAttribute("xmlns", "http://www.terralib.org/schemas/qt/af");
199  writer.writeAttribute("xsd:schemaLocation", "http://www.terralib.org/schemas/qt/af " + schema_loc);
200  writer.writeAttribute("version", TERRALIB_VERSION_STRING);
201 
202  writer.writeElement("Title", project.getTitle());
203  writer.writeElement("Author", project.getAuthor());
204 
205  writer.writeStartElement("ComponentList");
206  writer.writeEndElement("ComponentList");
207 
208  writer.writeStartElement("te_map:LayerList");
209 
211 
212  for(std::list<te::map::AbstractLayerPtr>::const_iterator it = project.getTopLayers().begin();
213  it != project.getTopLayers().end();
214  ++it)
215  lserial.write(it->get(), writer);
216 
217  writer.writeEndElement("te_map:LayerList");
218 
219  writer.writeEndElement("Project");
220 }
221 
222 void te::qt::af::UpdateUserSettings(const QStringList& prjFiles, const QStringList& prjTitles, const std::string& userConfigFile)
223 {
224  QSettings user_settings(QSettings::IniFormat,
225  QSettings::UserScope,
226  QApplication::instance()->organizationName(),
227  QApplication::instance()->applicationName());
228 
229 // save recent projects
230  if(!prjFiles.empty() && !prjTitles.empty() && (prjFiles.size() == prjTitles.size()))
231  {
232  user_settings.setValue("projects/most_recent/path", prjFiles.at(0));
233  user_settings.setValue("projects/most_recent/title", prjTitles.at(0));
234 
235  if(prjFiles.size() > 1)
236  {
237  user_settings.beginGroup("projects");
238 
239  user_settings.beginWriteArray("recents");
240 
241  for(int i = 1; i != prjFiles.size(); ++i)
242  {
243  user_settings.setArrayIndex(i - 1);
244  user_settings.setValue("projects/path", prjFiles.at(i));
245  user_settings.setValue("projects/title", prjTitles.at(i));
246  }
247 
248  user_settings.endArray();
249 
250  user_settings.endGroup();
251  }
252  }
253 
254 // save enabled plugins
255  user_settings.remove("plugins/enabled");
256 
257  user_settings.beginGroup("plugins");
258 
259  user_settings.beginWriteArray("enabled");
260 
261  std::vector<std::string> plugins = te::plugin::PluginManager::getInstance().getPlugins();
262 
263  int aidx = 0;
264 
265  for(std::size_t i = 0; i != plugins.size(); ++i)
266  {
267  if(!te::plugin::PluginManager::getInstance().isLoaded(plugins[i]))
268  continue;
269 
270  user_settings.setArrayIndex(aidx++);
271 
272  user_settings.setValue("name", plugins[i].c_str());
273  }
274 
275  user_settings.endArray();
276 
277  user_settings.endGroup();
278 }
279 
281 {
282  QSettings usettings(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
283 
284  QVariant fileName = usettings.value("data_sources/data_file");
285 
286  if(fileName.isNull())
287  {
288  const QString& udir = ApplicationController::getInstance().getUserDataDir();
289 
290  fileName = udir + "/" + QString(TERRALIB_APPLICATION_DATASOURCE_FILE_NAME);
291 
292  usettings.setValue("data_sources/data_file", fileName);
293  }
294 
295  te::serialize::xml::Save(fileName.toString().toStdString());
296 }
297 
298 
299 void AddToolbarAndActions(QToolBar* bar, QSettings& sett)
300 {
301  sett.beginGroup(bar->objectName());
302 
303  sett.setValue("name", bar->objectName());
304 
305  sett.beginWriteArray("Actions");
306 
307  QList<QAction*> acts = bar->actions();
308 
309  for(int i=0; i<acts.size(); i++)
310  {
311  sett.setArrayIndex(i);
312  sett.setValue("action", acts.at(i)->objectName());
313  }
314 
315  sett.endArray();
316 
317  sett.endGroup();
318 }
319 
321 {
322  std::vector<QToolBar*> bars = te::qt::af::ApplicationController::getInstance().getToolBars();
323  std::vector<QToolBar*>::const_iterator it;
324  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
325 
326  sett.beginGroup("toolbars");
327 
328  for (it = bars.begin(); it != bars.end(); ++it)
329  {
330  QToolBar* bar = *it;
331 
332  sett.remove(bar->objectName());
333 
334  AddToolbarAndActions(bar, sett);
335  }
336 
337  sett.endGroup();
338 }
339 
341 {
342  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
343 
344  sett.beginGroup("toolbars");
345 
346  AddToolbarAndActions(bar, sett);
347 
348  sett.endGroup();
349 }
350 
352 {
353  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
354 
355  sett.beginGroup("toolbars");
356  sett.remove(bar->objectName());
357  sett.endGroup();
358 }
359 
360 std::vector<QToolBar*> te::qt::af::ReadToolBarsFromSettings(QWidget* barsParent)
361 {
362  std::vector<QToolBar*> bars;
363 
364  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
365 
366  sett.beginGroup("toolbars");
367  QStringList lst = sett.childGroups();
368 
369  QStringList::iterator it;
370 
371  for(it=lst.begin(); it != lst.end(); ++it)
372  {
373  QString gr = *it;
374 
375  sett.beginGroup(gr);
376 
377  QString grName = sett.value("name").toString();
378 
379  int size = sett.beginReadArray("Actions");
380 
381  QToolBar* toolbar = new QToolBar(barsParent);
382  toolbar->setObjectName(grName);
383  toolbar->setWindowTitle(grName);
384 
385  for(int i=0; i<size; i++)
386  {
387  sett.setArrayIndex(i);
388  QString act = sett.value("action").toString();
389 
390  if(act == "")
391  {
392  toolbar->addSeparator();
393  }
394  else
395  {
396  QAction* a = ApplicationController::getInstance().findAction(act);
397 
398  if(a != 0)
399  toolbar->addAction(a);
400  }
401  }
402 
403  sett.endArray();
404  sett.endGroup();
405 
406  bars.push_back(toolbar);
407  }
408 
409  sett.endGroup();
410 
411  return bars;
412 }
413 
414 void te::qt::af::SaveState(QMainWindow* mainWindow)
415 {
416  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
417 
418  sett.beginGroup("mainWindow");
419  sett.setValue("geometry", mainWindow->saveGeometry());
420  sett.setValue("windowState", mainWindow->saveState());
421  sett.endGroup();
422 }
423 
424 void te::qt::af::RestoreState(QMainWindow* mainWindow)
425 {
426  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
427 
428  sett.beginGroup("mainWindow");
429  mainWindow->restoreGeometry(sett.value("geometry").toByteArray());
430  mainWindow->restoreState(sett.value("windowState").toByteArray());
431  sett.endGroup();
432 }
433 
434 void te::qt::af::GetProjectInformationsFromSettings(QString& defaultAuthor, int& maxSaved)
435 {
436  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
437 
438  sett.beginGroup("projects");
439  defaultAuthor = sett.value("author_name").toString();
440  maxSaved = sett.value("recents_history_size").toInt();
441  sett.endGroup();
442 }
443 
444 void te::qt::af::SaveProjectInformationsOnSettings(const QString& defaultAuthor, const int& maxSaved)
445 {
446  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
447 
448  sett.beginGroup("projects");
449  sett.setValue("author_name", defaultAuthor);
450  sett.setValue("recents_history_size", maxSaved);
451  sett.endGroup();
452 }
453 
454 void te::qt::af::SaveLastDatasourceOnSettings(const QString& dsType)
455 {
456  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
457 
458  sett.setValue("projects/last datasource used", dsType);
459 }
460 
462 {
463  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
464 
465  sett.setValue("projects/openLastDataSource", openLast);
466 }
467 
469 {
470  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
471 
472  return sett.value("projects/last datasource used").toString();
473 }
474 
476 {
477  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
478 
479  QVariant variant = sett.value("projects/openLastDataSource");
480 
481  // If the option was never edited
482  if(variant.isNull() || !variant.isValid())
483  return true;
484 
485  return variant.toBool();
486 }
487 
489 {
490  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
491 
492  sett.beginGroup("toolbars");
493 
494  sett.beginGroup("File Tool Bar");
495  sett.setValue("name", "File Tool Bar");
496  sett.beginWriteArray("Actions");
497  sett.setArrayIndex(0);
498  sett.setValue("action", "File.New Project");
499  sett.setArrayIndex(1);
500  sett.setValue("action", "File.Open Project");
501  sett.setArrayIndex(2);
502  sett.setValue("action", "File.Save Project");
503  sett.setArrayIndex(3);
504  sett.setValue("action", "");
505  sett.setArrayIndex(4);
506  sett.setValue("action", "Project.New Folder");
507  sett.setArrayIndex(5);
508  sett.setValue("action", "Project.Add Layer.All Sources");
509  sett.endArray();
510  sett.endGroup();
511 
512  sett.beginGroup("View Tool Bar");
513  sett.setValue("name", "View Tool Bar");
514  sett.beginWriteArray("Actions");
515  sett.setArrayIndex(0);
516  sett.setValue("action", "View.Layer Explorer");
517  sett.setArrayIndex(1);
518  sett.setValue("action", "View.Map Display");
519  sett.setArrayIndex(2);
520  sett.setValue("action", "View.Data Table");
521  sett.setArrayIndex(3);
522  sett.setValue("action", "View.Style Explorer");
523  sett.endArray();
524  sett.endGroup();
525 
526  sett.beginGroup("Map Tool Bar");
527  sett.setValue("name", "Map Tool Bar");
528  sett.beginWriteArray("Actions");
529  sett.setArrayIndex(0);
530  sett.setValue("action", "Map.Draw");
531  sett.setArrayIndex(1);
532  sett.setValue("action", "Map.Previous Extent");
533  sett.setArrayIndex(2);
534  sett.setValue("action", "Map.Next Extent");
535  sett.setArrayIndex(3);
536  sett.setValue("action", "Map.Zoom Extent");
537  sett.setArrayIndex(4);
538  sett.setValue("action", "");
539  sett.setArrayIndex(5);
540  sett.setValue("action", "Map.Zoom In");
541  sett.setArrayIndex(6);
542  sett.setValue("action", "Map.Zoom Out");
543  sett.setArrayIndex(7);
544  sett.setValue("action", "Map.Pan");
545  sett.setArrayIndex(8);
546  sett.setValue("action", "");
547  sett.setArrayIndex(9);
548  sett.setValue("action", "Map.Info");
549  sett.setArrayIndex(10);
550  sett.setValue("action", "Map.Selection");
551  sett.endArray();
552  sett.endGroup();
553 
554  sett.endGroup();
555 
556  sett.beginGroup("projects");
557 
558  sett.setValue("author_name", "");
559  sett.setValue("recents_history_size", "8");
560 
561  sett.endGroup();
562 }
563 
564 QString te::qt::af::UnsavedStar(const QString windowTitle, bool isUnsaved)
565 {
566  QString result(windowTitle);
567 
568  if(isUnsaved)
569  {
570  if(result.at(result.count()-1) != '*')
571  result += "*";
572  }
573  else
574  {
575  if(result.at(result.count()-1) == '*')
576  result.remove((result.count()-1), 1);
577  }
578 
579  return result;
580 }
581 
583 {
584  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
585  QString hexColor = sett.value("display/defaultDisplayColor").toString();
586  QColor defaultColor;
587  defaultColor.setNamedColor(hexColor);
588  if(!defaultColor.isValid())
589  return Qt::white;
590 
591  return defaultColor;
592 }
593 
594 QString te::qt::af::GetStyleSheetFromColors(QColor primaryColor, QColor secondaryColor)
595 {
596  QString sty("alternate-background-color: ");
597  sty += "rgb(" + QString::number(secondaryColor.red()) + ", " + QString::number(secondaryColor.green());
598  sty += ", " + QString::number(secondaryColor.blue()) + ")";
599  sty += ";background-color: rgb(" + QString::number(primaryColor.red()) + ", " + QString::number(primaryColor.green());
600  sty += ", " + QString::number(primaryColor.blue()) + ");";
601 
602  return sty;
603 }
604 
606 {
607  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
608  //bool isChecked = sett.value("table/tableAlternateColors").toBool();
609  QColor pColor;
610  pColor.setNamedColor(sett.value("table/primaryColor").toString());
611  QColor sColor;
612  sColor.setNamedColor(sett.value("table/secondaryColor").toString());
613 
614  if(!pColor.isValid())
615  pColor = Qt::white;
616  if(!sColor.isValid())
617  sColor = Qt::white;
618 
619  return GetStyleSheetFromColors(pColor, sColor);
620 }
621 
623 {
624  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
625  bool isChecked = sett.value("table/tableAlternateColors").toBool();
626 
627  return isChecked;
628 }
629 
631 {
632  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
633 
634  sett.beginGroup("toolbars");
635  QStringList lst = sett.childGroups();
636  QStringList::iterator it;
637 
638  for(it=lst.begin(); it!=lst.end(); ++it)
639  {
640  int size = sett.beginReadArray(*it+"/Actions");
641 
642  for(int i=0; i<size; i++)
643  {
644  sett.setArrayIndex(i);
645 
646  QString v = sett.value("action").toString();
647 
648  if (v == act->objectName())
649  {
650  ApplicationController::getInstance().getToolBar(*it)->addAction(act);
651  break;
652  }
653  }
654 
655  sett.endArray();
656  }
657 
658  sett.endGroup();
659 }
660 
661 std::vector<std::string> te::qt::af::GetPluginsFiles()
662 {
663  std::vector<std::string> res;
664 
665  QStringList filters;
666 
667  filters << "*.teplg";
668 
669  QDir d(te::common::FindInTerraLibPath("share/terralib/plugins").c_str());
670 
671  QFileInfoList files = d.entryInfoList(filters, QDir::Files);
672 
673  foreach(QFileInfo file, files)
674  {
675  res.push_back(file.absoluteFilePath().toStdString());
676  }
677 
678  return res;
679 }
680 
681 std::vector<std::string> te::qt::af::GetPluginsNames(const std::vector<std::string>& plgFiles)
682 {
683  std::vector<std::string> res;
684  std::vector<std::string>::const_iterator it;
685 
686  for(it=plgFiles.begin(); it!=plgFiles.end(); ++it)
687  {
688  boost::property_tree::ptree p;
689  boost::property_tree::read_xml(*it, p, boost::property_tree::xml_parser::trim_whitespace);
690 
691  res.push_back(p.get<std::string>("PluginInfo.Name"));
692  }
693 
694  return res;
695 }
696 
698 {
699  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
700 
701  return sett.value("configuration/generation").toString();
702 }
703 
704 void te::qt::af::SetDateTime(const QString& dateTime)
705 {
706  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
707 
708  sett.setValue("configuration/generation", dateTime);
709 }
710 
712 {
713  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
714 
715  QFileInfo info(sett.fileName());
716 
717  return info.absolutePath();
718 }
719 
720 void te::qt::af::UpdateUserSettingsFile(const QString& fileName, const bool& removeOlder)
721 {
722  QFileInfo info(fileName);
725 
726  if(info.exists())
727  info.dir().remove(info.fileName());
728 
729  std::string olderFile = appSett.getValue("Application.UserSettingsFile.<xmlattr>.xlink:href");
730 
731  appSett.setValue("Application.UserSettingsFile.<xmlattr>.xlink:href", fileName.toStdString());
732 
733  if(removeOlder)
734  {
735  info.setFile(olderFile.c_str());
736  info.dir().remove(info.fileName());
737  }
738 
739  info.setFile(fileName);
740 
741  if(!info.exists())
742  {
743  boost::property_tree::xml_writer_settings<char> settings('\t', 1);
744  boost::property_tree::write_xml(fileName.toStdString(), usrSett.getAllSettings(), std::locale(), settings);
745  }
746 
747  usrSett.load(fileName.toStdString());
748 }
749 
750 void te::qt::af::WriteDefaultProjectFile(const QString& fileName)
751 {
752  boost::property_tree::ptree p;
753 
754  std::string schema_location = te::common::FindInTerraLibPath("share/terralib/schemas/terralib/qt/af/project.xsd");
755 
756  //Header
757  p.add("Project.<xmlattr>.xmlns:xsd", "http://www.w3.org/2001/XMLSchema-instance");
758  p.add("Project.<xmlattr>.xmlns:te_map", "http://www.terralib.org/schemas/maptools");
759  p.add("Project.<xmlattr>.xmlns:te_qt_af", "http://www.terralib.org/schemas/qt/af");
760  p.add("Project.<xmlattr>.xmlns", "http://www.terralib.org/schemas/qt/af");
761  p.add("Project.<xmlattr>.xsd:schemaLocation", "http://www.terralib.org/schemas/qt/af " + schema_location);
762  p.add("Project.<xmlattr>.version", TERRALIB_VERSION_STRING);
763 
764  //Contents
765  p.add("Project.Title", "Default project");
766  p.add("Project.Author", "");
767  p.add("Project.ComponentList", "");
768  p.add("Project.te_map:LayerList", "");
769 
770  //Store file
771  boost::property_tree::xml_writer_settings<char> settings('\t', 1);
772  boost::property_tree::write_xml(fileName.toStdString(), p, std::locale(), settings);
773 }
774 
776 {
777  QString fileName = qApp->applicationDirPath() + "/../.generated";
778 
779  QFile f(fileName);
780  if (!f.open(QFile::ReadOnly | QFile::Text))
781  return "";
782 
783  QTextStream in(&f);
784  QString s = in.readAll();
785 
786  f.close();
787 
788  return s;
789 }
TEQTAFEXPORT void RemoveToolBarFromSettings(QToolBar *bar)
Removes a tool bar from the settings.
Definition: Utils.cpp:351
TEQTAFEXPORT void UpdateToolBarsInTheSettings()
Update plugins file.
Definition: Utils.cpp:320
TEQTAFEXPORT QColor GetDefaultDisplayColorFromSettings()
Definition: Utils.cpp:582
TEQTAFEXPORT std::vector< std::string > GetPluginsFiles()
Definition: Utils.cpp:661
const std::string & getTitle() const
It gets the title of the project.
Definition: Project.cpp:51
virtual void writeStartElement(const std::string &qName)
Definition: Writer.cpp:44
void write(const te::map::AbstractLayer *alayer, te::xml::Writer &writer) const
Definition: Layer.cpp:695
This class models a XML reader object.
Definition: Reader.h:55
virtual void writeStartDocument(const std::string &encoding, const std::string &standalone)
Definition: Writer.cpp:39
TEQTAFEXPORT void CreateDefaultSettings()
Creates a default QSettings.
Definition: Utils.cpp:488
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:461
This class models a XML writer object.
Definition: Writer.h:52
A singleton for managing application settings applied to a single user.
TEQTAFEXPORT void SetDateTime(const QString &dateTime)
Definition: Utils.cpp:704
TEQTAFEXPORT std::vector< QToolBar * > ReadToolBarsFromSettings(QWidget *barsParent=0)
Returns a vector of tool bars registered in the QSettings.
Definition: Utils.cpp:360
This class models the concept of a project for the TerraLib Application Framework.
virtual void writeAttribute(const std::string &attName, const std::string &value)
Definition: Writer.cpp:90
TEQTAFEXPORT QString UnsavedStar(const QString windowTitle, bool isUnsaved)
Unsaved star.
Definition: Utils.cpp:564
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:454
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:345
TEQTAFEXPORT void SaveState(QMainWindow *mainWindow)
Definition: Utils.cpp:414
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:222
TEQTAFEXPORT bool GetAlternateRowColorsFromSettings()
Definition: Utils.cpp:622
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:280
TEQTAFEXPORT void AddToolBarToSettings(QToolBar *bar)
Update settings with a new tool bar.
Definition: Utils.cpp:340
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
te::map::AbstractLayer * read(te::xml::Reader &reader) const
Definition: Layer.cpp:681
TEQTAFEXPORT void UpdateUserSettingsFile(const QString &fileName, const bool &removeOlder=true)
Changes the user settings file location.
Definition: Utils.cpp:720
TEQTAFEXPORT void AddActionToCustomToolbars(QAction *act)
Check QSettings for existance of act and adds it if necessary.
Definition: Utils.cpp:630
TEQTAFEXPORT void SaveProjectInformationsOnSettings(const QString &defaultAuthor, const int &maxSaved)
Definition: Utils.cpp:444
static Layer & getInstance()
It returns a reference to the singleton instance.
virtual void writeElement(const std::string &qName, const std::string &value)
Definition: Writer.cpp:54
TEQTAFEXPORT QString GetDefaultConfigFileOutputDir()
Returns the default path for output of configuration file.
Definition: Utils.cpp:711
TEQTAFEXPORT QString GetStyleSheetFromColors(QColor primaryColor, QColor secondaryColor)
Definition: Utils.cpp:594
void setProjectAsChanged(const bool &changed)
It sets the project status as changed or not.
Definition: Project.cpp:218
A singleton for managing application settings applied to the whole system (all users).
TEDATAACCESSEXPORT void Save(const std::string &fileName)
Definition: Serializer.cpp:197
TEQTAFEXPORT void GetProjectInformationsFromSettings(QString &defaultAuthor, int &maxSaved)
Definition: Utils.cpp:434
An exception class for the TerraLib Application Framework.
TEQTAFEXPORT void RestoreState(QMainWindow *mainWindow)
Definition: Utils.cpp:424
TEQTAFEXPORT QString GetGenerationDate()
Returns the date and time of generated binary.
Definition: Utils.cpp:775
virtual void writeEndElement(const std::string &qName)
Definition: Writer.cpp:156
TEQTAFEXPORT QString GetDateTime()
Definition: Utils.cpp:697
TEQTAFEXPORT void WriteDefaultProjectFile(const QString &fileName)
Writes the configuration file. It updates the application settings.
Definition: Utils.cpp:750
void setFileName(const std::string &fName)
It sets the filename where the project will be saved.
Definition: Project.cpp:207
TEQTAFEXPORT QString GetStyleSheetFromSettings()
Definition: Utils.cpp:605
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:68
This class models the concept of a project for the TerraLib Application Framework.
Definition: Project.h:50
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:299
virtual std::string getElementValue() const =0
It returns the element data value in the case of VALUE node.
Utility routines for the TerraLib Application Framework module.
TEQTAFEXPORT bool GetOpenLastProjectFromSettings()
Definition: Utils.cpp:475
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.
#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:162
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:681
The base API for controllers of TerraLib applications.
TEQTAFEXPORT QString GetLastDatasourceFromSettings()
Definition: Utils.cpp:468