27 #include <boost/foreach.hpp>
30 #include "terralib_config.h"
31 #include "../../common/Exception.h"
32 #include "../../common/PlatformUtils.h"
33 #include "../../common/Translator.h"
34 #include "../../common/TerraLib.h"
35 #include "../../common/SystemApplicationSettings.h"
36 #include "../../common/UserApplicationSettings.h"
37 #include "../../common/Logger.h"
38 #include "../../common/Version.h"
39 #include "../../dataaccess/serialization/xml/Serializer.h"
40 #include "../../plugin/PluginManager.h"
41 #include "../../plugin/PluginInfo.h"
42 #include "../../plugin/Utils.h"
43 #include "../../srs/Config.h"
44 #include "../widgets/help/AssistantHelpManagerImpl.h"
45 #include "../widgets/help/HelpManager.h"
46 #include "../widgets/Utils.h"
47 #include "../widgets/utils/ScopedCursor.h"
57 #include <QApplication>
58 #if QT_VERSION < 0x050000
59 #include <QDesktopServices>
64 #include <QMessageBox>
66 #if QT_VERSION >= 0x050000
67 #include <QStandardPaths>
72 #include <boost/filesystem.hpp>
77 #if defined(TERRALIB_APACHE_LOG4CXX_ENABLED) && defined(TERRALIB_LOGGER_ENABLED)
79 #include <log4cxx/basicconfigurator.h>
80 #include <log4cxx/consoleappender.h>
81 #include <log4cxx/fileappender.h>
82 #include <log4cxx/helpers/pool.h>
83 #include <log4cxx/helpers/transcoder.h>
84 #include <log4cxx/logger.h>
85 #include <log4cxx/logmanager.h>
86 #include <log4cxx/logstring.h>
87 #include <log4cxx/simplelayout.h>
94 m_msgBoxParentWidget(0),
96 m_selectionColor(QColor(0, 255, 0)),
110 m_appConfigFile = configFileName;
115 m_msgBoxParentWidget = w;
120 registerToolBar(
id, bar);
130 QToolBar* b = getToolBar(
id);
133 throw Exception(
TE_TR(
"There is already a tool bar registered with the same name!"));
135 m_toolbars[id] = bar;
140 std::map<QString, QToolBar*>::const_iterator it = m_toolbars.find(
id);
142 return (it != m_toolbars.end()) ? it->second : 0;
147 std::vector<QToolBar*> res;
148 std::map<QString, QToolBar*>::const_iterator it;
150 for(it = m_toolbars.begin(); it != m_toolbars.end(); ++it)
151 res.push_back(it->second);
158 std::map<QString, QToolBar*>::iterator it = m_toolbars.find(
id);
160 if(it != m_toolbars.end())
161 m_toolbars.erase(it);
166 m_menus.push_back(mnu);
171 std::vector<QMenu*>::const_iterator it;
174 for(it = m_menus.begin(); it != m_menus.end(); ++it)
183 std::vector<QMenuBar*>::const_iterator it_bar;
185 for(it_bar = m_menuBars.begin(); it_bar != m_menuBars.end(); ++it_bar)
198 QMenu* mnu = findMenu(
id);
202 if(!m_menuBars.empty())
207 m_menus.push_back(mnu);
216 m_menuBars.push_back(bar);
221 throw Exception(
"Not implemented yet.");
226 return m_menuBars[0];
231 for(
size_t i=0; i<m_menus.size(); i++)
239 for(
size_t i=0; i<m_menuBars.size(); i++)
252 for(
size_t i=0; i<m_menus.size(); i++)
260 for(
size_t i=0; i<m_menuBars.size(); i++)
273 std::set<QObject*>::const_iterator it = m_applicationItems.find(obj);
275 if(it != m_applicationItems.end())
278 m_applicationItems.insert(obj);
285 std::set<QObject*>::iterator it = m_applicationItems.find(obj);
287 if(it == m_applicationItems.end())
290 m_applicationItems.erase(it);
301 #if QT_VERSION >= 0x050000
302 m_userDataDir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
304 m_userDataDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
307 if(!boost::filesystem::exists(m_userDataDir.toStdString()))
308 boost::filesystem::create_directories(m_userDataDir.toStdString());
315 #if defined(TERRALIB_APACHE_LOG4CXX_ENABLED) && defined(TERRALIB_LOGGER_ENABLED)
316 std::string path = m_userDataDir.toStdString();
317 path +=
"/log/terralib.log";
319 log4cxx::FileAppender* fileAppender =
new log4cxx::FileAppender(log4cxx::LayoutPtr(
new log4cxx::SimpleLayout()),
322 log4cxx::helpers::Pool p;
323 fileAppender->activateOptions(p);
325 log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(fileAppender));
326 log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getAll() );
327 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(
"te");
348 if(!boost::filesystem::exists(m_appIconName.toStdString()))
353 if (!boost::filesystem::exists(m_appPluginsPath.toStdString()))
358 if(!boost::filesystem::exists(m_aboutLogo.toStdString()))
363 if(!boost::filesystem::exists(m_tLibLogo.toStdString()))
367 qApp->setApplicationName(fullAppName);
371 qApp->setOrganizationName(m_appOrganization);
378 m_appHelpFile = QString::fromStdString(help_file);
380 QFileInfo info(m_appHelpFile);
382 if(!m_appHelpFile.isEmpty() && info.exists())
393 catch(
const std::exception& e)
397 QString msgErr(tr(
"Error loading application help system: %1"));
399 msgErr = msgErr.arg(e.what());
401 QMessageBox::warning(m_msgBoxParentWidget, m_appTitle, msgErr);
405 QSettings user_settings(QSettings::IniFormat,
406 QSettings::UserScope,
407 QApplication::instance()->organizationName(),
408 QApplication::instance()->applicationName());
417 m_appIconThemeDir = QString::fromStdString(icon_dir);
419 if(!m_appIconThemeDir.isEmpty())
421 QStringList ithemes = QIcon::themeSearchPaths();
423 ithemes.push_back(m_appIconThemeDir);
425 QIcon::setThemeSearchPaths(ithemes);
430 QVariant iconTheme = user_settings.value(
"icon_theme/selected_theme", m_appDefaultIconTheme);
432 QIcon::setThemeName(iconTheme.toString());
437 QString sh = QString(
"QToolBar { qproperty-iconSize: ") + iconSize.toString() +
"px " + iconSize.toString() +
"px; }";
438 qApp->setStyleSheet(sh);
444 m_defaultSRID = srid.toInt();
449 m_selectionColor = QColor(selectionColor.toString());
453 catch(
const std::exception& e)
457 QString msgErr(tr(
"Error loading application icon theme: %1"));
459 msgErr = msgErr.arg(e.what());
461 QMessageBox::warning(m_msgBoxParentWidget, m_appTitle, msgErr);
467 m_appDatasourcesFile = user_settings.value(
"data_sources/data_file",
"").toString().toStdString();
469 if(!m_appDatasourcesFile.empty())
481 const QString& udir = getUserDataDir();
485 QFileInfo infoDataSourceFile(fileName.toString());
487 if (infoDataSourceFile.exists())
489 int reply = QMessageBox::question(0, tr(
"Data Sources XML"), tr(
"A file containing data sources already configured was found. Would you like to load it."), QMessageBox::No, QMessageBox::Yes);
491 if (reply == QMessageBox::Yes)
493 std::string dataSourcesFile = fileName.toString().toStdString();
504 catch(
const std::exception& e)
508 QString msgErr(tr(
"Error loading the registered data sources: %1"));
510 msgErr = msgErr.arg(e.what());
512 QMessageBox::warning(m_msgBoxParentWidget, m_appTitle, msgErr);
515 QFileInfo info(user_settings.fileName());
520 m_initialized =
true;
527 std::vector<std::string> plgFiles;
540 QSettings user_settings(QSettings::IniFormat,
541 QSettings::UserScope,
542 QApplication::instance()->organizationName(),
543 QApplication::instance()->applicationName());
545 user_settings.beginGroup(
"plugins");
547 std::set<std::string> user_enabled_plugins;
549 int nitems = user_settings.beginReadArray(
"enabled");
551 for(
int i = 0; i != nitems; ++i)
553 user_settings.setArrayIndex(i);
555 QString name = user_settings.value(
"name").toString();
557 user_enabled_plugins.insert(name.toStdString());
560 user_settings.endArray();
563 std::set<std::string> user_unloaded_plugins;
564 int n_itemsUnloaded = user_settings.beginReadArray(
"unloaded");
566 for (
int i = 0; i != n_itemsUnloaded; ++i)
568 user_settings.setArrayIndex(i);
570 QString name = user_settings.value(
"name").toString();
572 user_unloaded_plugins.insert(name.toStdString());
575 user_settings.endArray();
578 std::set<std::string> user_broken_plugins;
579 int n_itemsBroken = user_settings.beginReadArray(
"broken");
581 for (
int i = 0; i != n_itemsBroken; ++i)
583 user_settings.setArrayIndex(i);
585 QString name = user_settings.value(
"name").toString();
587 user_broken_plugins.insert(name.toStdString());
590 user_settings.endArray();
592 user_settings.endGroup();
599 boost::ptr_vector<te::plugin::PluginInfo> plugins;
600 boost::ptr_vector<te::plugin::PluginInfo> unloadedPlugins;
601 boost::ptr_vector<te::plugin::PluginInfo> brokenPlugins;
603 for(std::size_t i = 0; i != plgFiles.size(); ++i)
607 if (user_enabled_plugins.empty())
609 if (default_plg.size() > 0)
611 if (std::find(default_plg.begin(), default_plg.end(), pinfo->
m_name) != default_plg.end())
612 plugins.push_back(pinfo);
616 plugins.push_back(pinfo);
619 else if (user_enabled_plugins.count(pinfo->
m_name) != 0)
621 plugins.push_back(pinfo);
623 else if (user_unloaded_plugins.count(pinfo->
m_name) != 0)
625 unloadedPlugins.push_back(pinfo);
627 else if (user_broken_plugins.count(pinfo->
m_name) != 0)
629 brokenPlugins.push_back(pinfo);
636 if (user_unloaded_plugins.size() > 0)
639 if (user_broken_plugins.size() > 0)
644 catch(
const std::exception& e)
648 QString msgErr(tr(
"Error reading application's plugin list: %1"));
650 msgErr = msgErr.arg(e.what());
652 QMessageBox::warning(m_msgBoxParentWidget, m_appTitle, msgErr);
662 QSettings user_settings(QSettings::IniFormat,
663 QSettings::UserScope,
664 QApplication::instance()->organizationName(),
665 QApplication::instance()->applicationName());
667 QVariant projPath = user_settings.value(
"projects/most_recent/path",
"");
668 QVariant projTitle = user_settings.value(
"projects/most_recent/title",
"");
670 QMenu* mnu = getMenu(
"File.Recent Projects");
672 if(!projPath.toString().isEmpty())
674 QAction* act = mnu->addAction(projPath.toString());
675 act->setData(projPath);
679 m_recentProjs.append(projPath.toString());
680 m_recentProjsTitles.append(projTitle.toString());
683 user_settings.beginGroup(
"projects");
685 int nrc = user_settings.beginReadArray(
"recents");
687 for(
int i = 0; i != nrc; ++i)
689 user_settings.setArrayIndex(i);
690 QString npath = user_settings.value(
"projects/path").toString();
691 QString ntitle = user_settings.value(
"projects/title").toString();
694 QAction* act = mnu->addAction(npath);
696 m_recentProjs.append(npath);
697 m_recentProjsTitles.append(ntitle);
700 mnu->setEnabled(
true);
704 catch(
const std::exception& e)
708 QString msgErr(tr(
"Error loading the registered projects: %1"));
710 msgErr = msgErr.arg(e.what());
712 QMessageBox::warning(m_msgBoxParentWidget, m_appTitle, msgErr);
718 int pos = m_recentProjs.indexOf(prjFile);
729 if(m_recentProjs.size() > maxSaved)
731 m_recentProjs.removeLast();
732 m_recentProjsTitles.removeLast();
735 m_recentProjs.prepend(prjFile);
736 m_recentProjsTitles.prepend(prjTitle);
740 m_recentProjs.move(pos, 0);
741 m_recentProjsTitles.move(pos, 0);
744 if(m_recentProjs.isEmpty())
747 QMenu* mnu = getMenu(
"File.Recent Projects");
751 mnu->setEnabled(
true);
753 QString recPrj = m_recentProjs.at(0);
754 QAction* act = mnu->addAction(recPrj);
755 act->setData(recPrj);
759 if(m_recentProjs.size() > 1)
760 for(
int i=1; i<m_recentProjs.size(); i++)
762 recPrj = m_recentProjs.at(i);
763 act = mnu->addAction(recPrj);
764 act->setData(recPrj);
768 QAction* act = findAction(
"File.Save Project As");
771 act->setEnabled(
true);
802 m_appConfigFile.clear();
804 m_applicationItems.clear();
814 m_appDatasourcesFile.clear();
816 m_appDefaultIconTheme.clear();
818 m_appHelpFile.clear();
820 m_appIconName.clear();
822 m_appIconThemeDir.clear();
826 m_msgBoxParentWidget = 0;
828 m_appOrganization.clear();
832 m_appProjectExtension.clear();
836 m_recentProjs.clear();
838 m_recentProjsTitles.clear();
840 m_appUserSettingsFile.clear();
842 m_appPluginsPath.clear();
844 m_appToolBarDefaultIconSize.clear();
848 m_selectionColor = QColor();
852 m_initialized =
false;
857 return m_appSettings;
880 return m_appProjectExtension;
885 return m_appIconName;
890 return m_appPluginsPath;
905 return m_recentProjs.isEmpty() ? QString(
"") : m_recentProjs.front();
910 return m_defaultSRID;
915 return m_selectionColor;
920 m_selectionColor = c;
925 return m_msgBoxParentWidget;
930 m_resetTerralib = status;
935 return m_userDataDir;
TEQTAFEXPORT std::vector< std::string > GetPluginsFiles()
virtual void setConfigFile(const std::string &configFileName)
It gives access to the controller singleton.
QActionGroup * findActionGroup(const QString &id) const
Returns the action group identified by id or NULL if there's not an action group identified by id...
void registerMenu(QMenu *mnu)
Register the mnu.
std::string m_name
The plugin name: an internal value used to identify the plugin in the system. Must be a unique value...
virtual ~ApplicationController()
Destructor.
TEQTAFEXPORT void CreateDefaultSettings()
Creates a default QSettings.
virtual void initializePlugins()
Load the plugin list and initialize the plugins enabled by the user.
const QString & getAppTitle() const
Returns the application title.
TECOMMONEXPORT std::string FindInTerraLibPath(const std::string &p)
Returns the path relative to a directory or file in the context of TerraLib.
QMenu * getMenu(const QString &id)
Returns a menu registered with key id.
A base class for application events.
QWidget * getMainWindow() const
Returns main window.
void registerMenuBar(QMenuBar *bar)
Register the bar.
const QString & getAppIconName() const
Returns the application icon.
static std::string asString()
ApplicationController()
Constructor.
This class models the concept of a project for the TerraLib Application Framework.
void setResetTerraLibFlag(const bool &status)
A singleton for holding he application splash screen.
void set(te::qt::af::Project *prj)
Set the current project.
QMenuBar * getMenuBar(const QString &id) const
Returns a menu bar registered with key id.
const QString & getUserDataDir() const
#define TE_TR(message)
It marks a string in order to get translated.
TEQTAFEXPORT void UpdateUserSettings(const QStringList &prjFiles, const QStringList &prjTitles, const std::string &userConfigFile)
Updates user settings file section about information of the projects.
void showMessage(const QString &message)
This will cause the text to be drawn on the splash screen and a call to Application::processEvents() ...
TEQTAFEXPORT void SaveDataSourcesFile()
Saves data sources file.
QToolBar * getToolBar(const QString &id) const
Return the toolbar identified by id or NULL if none is found.
virtual void initialize()
Initializes the application framework.
const QString & getAppName() const
Returns the application name.
QMenu * findMenu(const QString &id) const
Returns the menu registered with key id.
QColor getSelectionColor() const
Returns the application selection color.
const QString & getAppPluginsPath() const
Returns the plugins file path of application.
void finalize()
It finalizes the TerraLib Platform.
static SplashScreenManager & getInstance()
It returns a reference to the singleton instance.
TEQTAFEXPORT std::vector< std::string > GetDefaultPluginsNames()
void removeToolBar(const QString &id)
Removes the toolbar identified by id.
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
void addListener(QObject *obj)
Insert an application item that will listen to framework events.
TEQTAFEXPORT void GetProjectInformationsFromSettings(QString &defaultAuthor, int &maxSaved)
virtual void setMsgBoxParentWidget(QWidget *w)
Tells the widget to be used as the parent of messages showned in a QMessageBox.
virtual void initializeProjectMenus()
Initializes the menus for the most recent open projects.
Utility functions for dealing with plugins.
const QString & getAppProjectExtension() const
Returns the application project extension.
void addToolBar(const QString &id, QToolBar *bar)
Register the toolbar in the list of the known toolbars and dispatch an event.
void initialize()
It initializes the TerraLib Platform.
QSettings & getSettings()
Return the QSettings of the application. This can be used to add settings from external sources...
void broadcast(te::qt::af::evt::Event *evt)
Send events in broadcast for all registered components.
bool decode(std::string &s)
Decode the pct-encoded (hex) sequences, if any, return success.
void setSelectionColor(const QColor &c)
Sets the application selection color.
te::qt::af::Project * getProject()
Get the current project.
QMenuBar * findMenuBar(const QString &id) const
Returns the menu bar registered with key id.
This class models the concept of a project for the TerraLib Application Framework.
QAction * findAction(const QString &id) const
Returns the action identified by id or NULL if there's not an action identified by id...
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
void removeListener(QObject *obj)
Remove the obj from the list of event listeners.
std::vector< QToolBar * > getToolBars() const
Return the list of registered toolbars.
const QString & getTlibLogo() const
The basic information about a plugin.
QString getMostRecentProject() const
Returns the most recent project.
#define TERRALIB_APPLICATION_DATASOURCE_FILE_NAME
The default name for the application file containing the list of data sources.
Contains the list of the application events.
TEPLUGINEXPORT PluginInfo * GetInstalledPlugin(const std::string &pluginFilePath)
It returns information about a given plugin provided its plugin configuration file name or dir...
The base API for controllers of TerraLib applications.
void registerToolBar(const QString &id, QToolBar *bar)
Register the toolbar in the list of the known toolbars.
virtual void finalize()
Finalize the application framework.
TEDATAACCESSEXPORT void ReadDataSourceInfo(const std::string &datasourcesFileName)
void updateRecentProjects(const QString &prjFile, const QString &prjTitle)
Update the list of recent projects. This is commonly used when there's a new most recent project...
int getDefaultSRID() const
Returns the application default SRID value.
const QString & getAboutLogo() const