TerraViewController.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 terraview/TerraViewController.cpp
22 
23 \brief The API for controller of TerraView application.
24 */
25 
26 // TerraLib
27 #include <terralib_buildconfig.h>
31 #include "TerraViewController.h"
32 #include "Utils.h"
33 
34 // Qt
35 #include <QApplication>
36 #if QT_VERSION < 0x050000
37 #include <QDesktopServices>
38 #endif
39 #include <QDir>
40 #include <QIcon>
41 #include <QMenu>
42 #include <QMessageBox>
43 #include <QResource>
44 #if QT_VERSION >= 0x050000
45 #include <QStandardPaths>
46 #endif
47 #include <QWidget>
48 
49 // Boost
50 #include <boost/filesystem.hpp>
51 
52 //STL
53 #include <algorithm>
54 
56  std::string appConfigFile)
57  : m_app(app)
58 {
60 
61  m_appProjectExtension = QString::fromUtf8(te::common::SystemApplicationSettings::getInstance().getValue("Application.ProjectExtension").c_str());
62 }
63 
65 {
66  m_appProjectExtension.clear();
67 
68  m_recentProjs.clear();
69 
70  m_recentProjsTitles.clear();
71 }
72 
74 {
75  te::qt::af::SplashScreenManager::getInstance().showMessage("Loading recent projects...");
76 
77  try
78  {
79  QSettings user_settings(QSettings::IniFormat,
80  QSettings::UserScope,
81  QApplication::instance()->organizationName(),
82  QApplication::instance()->applicationName());
83 
84  QVariant projPath = user_settings.value("projects/most_recent/path", "");
85  QVariant projTitle = user_settings.value("projects/most_recent/title", "");
86 
87  QMenu* mnu = m_app->getMenu("File.Recent Projects");
88 
89  if(!projPath.toString().isEmpty())
90  {
91  QAction* act = mnu->addAction(projPath.toString());
92  act->setData(projPath);
93 
94  mnu->addSeparator();
95 
96  m_recentProjs.append(projPath.toString());
97  m_recentProjsTitles.append(projTitle.toString());
98  }
99 
100  user_settings.beginGroup("projects");
101 
102  int nrc = user_settings.beginReadArray("recents");
103 
104  for(int i = 0; i != nrc; ++i)
105  {
106  user_settings.setArrayIndex(i);
107  QString npath = user_settings.value("project/path").toString();
108  QString ntitle = user_settings.value("project/title").toString();
109 
110 
111  QAction* act = mnu->addAction(npath);
112  act->setData(npath);
113  m_recentProjs.append(npath);
114  m_recentProjsTitles.append(ntitle);
115  }
116 
117  mnu->setEnabled(true);
118 
119  te::qt::af::SplashScreenManager::getInstance().showMessage("Recent projects loaded!");
120  }
121  catch(const std::exception& e)
122  {
123  te::qt::widgets::ScopedCursor acursor(Qt::ArrowCursor);
124 
125  QString msgErr(tr("Error loading the registered projects: %1"));
126 
127  msgErr = msgErr.arg(e.what());
128 
129  QMessageBox::warning(m_app->getMainWindow(), m_app->getAppTitle(), msgErr);
130  }
131 }
132 
133 void TerraViewController::updateRecentProjects(const QString& prjFile, const QString& prjTitle)
134 {
135  int pos = m_recentProjs.indexOf(prjFile);
136 
137  QString author;
138  int maxSaved;
139 
140  GetProjectInformationsFromSettings(author, maxSaved);
141 
142  if(pos != 0)
143  {
144  if(pos < 0)
145  {
146  if(m_recentProjs.size() > maxSaved) // TODO: Size of the list must be configurable.
147  {
148  m_recentProjs.removeLast();
149  m_recentProjsTitles.removeLast();
150  }
151 
152  m_recentProjs.prepend(prjFile);
153  m_recentProjsTitles.prepend(prjTitle);
154  }
155  else
156  {
157  m_recentProjs.move(pos, 0);
158  m_recentProjsTitles.move(pos, 0);
159  }
160 
161  if(m_recentProjs.isEmpty())
162  return;
163 
164  QMenu* mnu = m_app->getMenu("File.Recent Projects");
165 
166  mnu->clear();
167 
168  mnu->setEnabled(true);
169 
170  QString recPrj = m_recentProjs.at(0);
171  QAction* act = mnu->addAction(recPrj);
172  act->setData(recPrj);
173 
174  mnu->addSeparator();
175 
176  if(m_recentProjs.size() > 1)
177  for(int i=1; i<m_recentProjs.size(); i++)
178  {
179  recPrj = m_recentProjs.at(i);
180  act = mnu->addAction(recPrj);
181  act->setData(recPrj);
182  }
183  }
184 
185  QAction* act = m_app->findAction("File.Save Project As");
186 
187  if(act != nullptr)
188  act->setEnabled(true);
189 }
190 
191 
192 
194 {
195  return m_appProjectExtension;
196 }
197 
198 
200 {
201  return m_recentProjs.isEmpty() ? QString("") : m_recentProjs.front();
202 }
203 
205 {
206  QString appName = m_app->getAppName();
207  QString appProjectExtension = m_appProjectExtension;
208  QString extensionFilter = appName;
209  extensionFilter += QString(" (*.");
210  extensionFilter += appProjectExtension + ")";
211 
212  return extensionFilter;
213 }
214 
const QString & getAppTitle() const
Returns the application title.
QMenu * getMenu(const QString &id)
Returns a menu registered with key id.
QWidget * getMainWindow() const
Returns main window.
QStringList m_recentProjsTitles
List of the titles of the recent projects.
The base API for TerraLib applications.
A singleton for holding he application splash screen.
virtual void initializeProjectMenus()
Initializes the menus for the most recent open projects.
QString m_appProjectExtension
Application project extension.
The API for controller of TerraView application.
QString getMostRecentProject() const
Returns the most recent project.
const QString & getAppProjectExtension() const
Returns the application project extension.
void GetProjectInformationsFromSettings(QString &defaultAuthor, int &maxSaved)
const QString & getAppName() const
Returns the application name.
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:126
QStringList m_recentProjs
List of the recent projects.
virtual ~TerraViewController()
Destructor.
An object that when created shows a cursor during its scope.
te::qt::af::ApplicationController * m_app
QString getExtensionFilter()
Returns the project extension filter .
void updateRecentProjects(const QString &prjFile, const QString &prjTitle)
Update the list of recent projects. This is commonly used when there&#39;s a new most recent project...
TerraViewController(te::qt::af::ApplicationController *app, std::string appConfigFile)
Constructor.
QAction * findAction(const QString &id) const
Returns the action identified by id or NULL if there&#39;s not an action identified by id...
A singleton for managing application settings applied to the whole system (all users).
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48