ApplicationController.h
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/ApplicationController.h
22 
23  \brief The base API for controllers of TerraLib applications.
24 */
25 
26 #ifndef __TERRALIB_QT_AF_INTERNAL_APPLICATIONCONTROLLER_H
27 #define __TERRALIB_QT_AF_INTERNAL_APPLICATIONCONTROLLER_H
28 
29 // Terralib
30 #include "../../common/Singleton.h"
31 #include "Config.h"
32 
33 // STL
34 #include <map>
35 #include <set>
36 #include <vector>
37 
38 // Boost
39 //#include <boost/noncopyable.hpp>
40 
41 // Qt
42 #include <QtCore/QObject>
43 #include <QtCore/QStringList>
44 #include <QtCore/QSettings>
45 #include <QtGui/QColor>
46 
47 // Forward declarations
48 class QAction;
49 class QActionGroup;
50 class QMenu;
51 class QMenuBar;
52 class QToolBar;
53 class QWidget;
54 
55 namespace te
56 {
57  namespace qt
58  {
59  namespace af
60  {
61  class Project;
62 
63  namespace evt
64  {
65  // Forward declarations
66  struct Event;
67  }
68 
69  /*!
70  \class ApplicationController
71 
72  \brief The base API for TerraLib applications.
73 
74  \note Temporally this class is a singleton!
75 
76  \ingroup af
77  */
78  class TEQTAFEXPORT ApplicationController : public QObject, public te::common::Singleton<ApplicationController>
79  {
80  Q_OBJECT
81 
82  public:
83 
84  /*!
85  \brief Constructor.
86 
87  \param parent The parent object.
88  */
89  ApplicationController(/*QObject* parent = 0*/);
90 
91  /*!
92  \brief Destructor.
93  */
94  virtual ~ApplicationController();
95 
96  /*!
97  \brief It gives access to the controller singleton.
98 
99  \return A pointer to the application singleton.
100 
101  \pre The application must have been initialized before calling this method.
102  */
103 // static ApplicationController& getInstance();
104 
105  /*!
106  \brief Tells wich configuration file to be used by the controller during its initialization.
107 
108  \param configFileName The configuration file name with full path.
109  */
110  virtual void setConfigFile(const std::string& configFileName);
111 
112  /*!
113  \brief Tells the widget to be used as the parent of messages showned in a QMessageBox.
114 
115  \param w The widget to be used as the parent of messages showned in a QMessageBox.
116  */
117  virtual void setMsgBoxParentWidget(QWidget* w);
118 
119  /*!
120  \name Menus and Menubars Management.
121 
122  \brief Register and recover menus and menu bars.
123 
124  The methods \a find will returns a valid object only if it exists in some menu, otherwise the return will be a NULL pointer.
125  The methods \a get ALWAYS returns a valid pointer. If the requested menu or menubar does not exists these funtions will create,
126  register and return the object.
127 
128  \note The identifiers of the menus are the texts presented by menus.
129  \note The search for menus will occurs in ALL OF THE REGISTERED menus and menu bars, including submenus. The correct sintaxe
130  for searchies submenus may be found in the documentation of the findMenu(QString mnuText, QMenu* mnu) method.
131  */
132  //@{
133 
134  /*!
135  \brief Register the toolbar in the list of the known toolbars and dispatch an event.
136 
137  \param id The toolbar identification string.
138  \param bar The toolbar to be registered. The controller will not take the \a bar ownership.
139 
140  \pos An event will be broadcasted to indicate that the toolbar was appended.
141 
142  \exception Exception It throws an exception if a toolbar with the given \a id already exists.
143  */
144  void addToolBar(const QString& id, QToolBar* bar);
145 
146  /*!
147  \brief Register the toolbar in the list of the known toolbars.
148 
149  \param id The toolbar identification string.
150  \param bar The toolbar to be registered. The controller will not take the \a bar ownership.
151 
152  \exception Exception It throws an exception if a toolbar with the given \a id already exists.
153 
154  \todo Rever se este metodo eh necessario ou nao! Talvez possamos viver somente com o addToolBar!
155  */
156  void registerToolBar(const QString& id, QToolBar* bar);
157 
158  /*!
159  \brief Return the toolbar identified by \a id or \a NULL if none is found.
160 
161  \param id The toolbar identification string.
162 
163  \return A toolbar identified by \a id or \a NULL if none is found.
164  */
165  QToolBar* getToolBar(const QString& id) const;
166 
167  /*!
168  \brief Return the list of registered toolbars.
169 
170  \return List of registered ToolBars.
171  */
172  std::vector<QToolBar*> getToolBars() const;
173 
174  /*!
175  \brief Removes the toolbar identified by \a id.
176 
177  \param id Identifier of the tool bar.
178  */
179  void removeToolBar(const QString& id);
180 
181  /*!
182  \brief Register the \a mnu.
183 
184  \param mnu Pointer to the menu to be registered.
185  */
186  void registerMenu(QMenu* mnu);
187 
188  /*!
189  \brief Returns the menu registered with key \a id
190 
191  \param id Indentifier of the menu.
192 
193  \return Menu registered or NULL with there is not one registered with the key \a id.
194  */
195  QMenu* findMenu(const QString& id) const;
196 
197  /*!
198  \brief Returns a menu registered with key \a id.
199 
200  \param id Identifier of menu to be searched.
201 
202  \return The menu registered with the \a id identifier.
203 
204  \note If the menu does not exists, then this function will create it.
205  */
206  QMenu* getMenu(const QString& id);
207 
208  /*!
209  \brief Register the \a bar.
210 
211  \param bar Pointer to the menu bar to be registered.
212  */
213  void registerMenuBar(QMenuBar* bar);
214 
215  /*!
216  \brief Returns the menu bar registered with key \a id
217 
218  \param id Indentifier of the menu bar.
219 
220  \return Menu bar registered or NULL with there is not one registered with the key \a id.
221  */
222  QMenuBar* findMenuBar(const QString& id) const;
223 
224  /*!
225  \brief Returns a menu bar registered with key \a id.
226 
227  \param id Identifier of menu bar to be searched.
228 
229  \return The menu bar registered with the \a id identifier.
230 
231  \note If the menu bar does not exists, then this function will create it.
232  */
233  QMenuBar* getMenuBar(const QString& id) const;
234 
235  /*!
236  \brief Returns the action identified by \a id or NULL if there's not an action identified by \a id.
237 
238  \param id Identifier if the action.
239 
240  \return QAction identified or NULL if could not find one.
241 
242  \note There's a rule to determine \a id. The text of menus and submenus separated by '.' character.
243  */
244  QAction* findAction(const QString& id) const;
245 
246  /*!
247  \brief Returns the action group identified by \a id or NULL if there's not an action group identified by \a id.
248 
249  \param id Identifier of the action group.
250 
251  \return QActionGroup identified or NULL if could not find one.
252  */
253  QActionGroup* findActionGroup(const QString& id) const;
254 
255  //@}
256 
257  /*!
258  \brief Insert an application item that will listen to framework events.
259 
260  \param obj The application item that will listen to framework events.
261 
262  \note The \a obj MUST HAVE AN IMPLEMENTATION of the slot \a onApplicationTriggered(Event*). If it hasn't, a warning will be sent at run-time.
263 
264  \note If the \a obj was previously registered, nothing will be done.
265  */
266  void addListener(QObject* obj);
267 
268  /*!
269  \brief Remove the \a obj from the list of event listeners.
270 
271  \param obj The application item to be removed.
272 
273  \note If the \a obj was not previously registered, nothing will be done.
274  */
275  void removeListener(QObject* obj);
276 
277  /*!
278  \brief Initializes the application framework.
279 
280  The initialization will read the following configurations files (in order):
281  <ul>
282  <li>A main config file, generally called config.xml</li>
283  <li>A user settings file, generally called user_settings.xml<li>
284  </ul>
285 
286  It will start by initializing the available TerraLib modules.
287  This means that applications that uses the application framework,
288  does not need to call TerraLib initialization (TerraLib::getInstance().initialize()) and
289  finalization methods (TerraLib::getInstance().finalize()).
290 
291  \exception Exception It throws an exception if the initialization fails.
292 
293  \note Make sure that the application calls this method BEFORE it uses any TerraLib modules or functions.
294  */
295  virtual void initialize();
296 
297  /*!
298  \brief Load the plugin list and initialize the plugins enabled by the user.
299 
300  The plugin initialization will read a file containing a list of plugins with their addresses.
301  This file is generally called application_plugins.xml.
302 
303  \pre The application must assure that any resource needed by their plugins are ready for use. This
304  means that initialize should be called before initializing the plugins.
305  */
306  virtual void initializePlugins();
307 
308  /*!
309  \brief Initializes the menus for the most recent open projects.
310  */
311  virtual void initializeProjectMenus();
312 
313  /*!
314  \brief Update the list of recent projects. This is commonly used when there's a new most recent project.
315 
316  \param prj_file Complete file name for the project file.
317 
318  \param prj_title Title of the project.
319  */
320  void updateRecentProjects(const QString& prjFile, const QString& prjTitle);
321 
322  /*!
323  \brief Set the current project.
324 
325  \param prj Pointer to a current project.
326 
327  */
328  void set(te::qt::af::Project* prj);
329 
330  /*!
331  \brief Get the current project.
332 
333  \return Pointer to a current project if defined or a null pointer in other case.
334 
335  */
336  te::qt::af::Project* getProject();
337 
338  /*!
339  \brief Finalize the application framework.
340 
341  \details This method will unload all TerraLib modules, plugins, and dynamic libraries that are loaded.
342 
343  \note Make sure that the application calls this method BEFORE it finishes.
344  */
345  virtual void finalize();
346 
347  /*!
348  \brief Return the QSettings of the application. This can be used to add settings from external sources.
349 
350  \return QSettings being used.
351  */
352  QSettings& getSettings();
353 
354  public slots:
355 
356  /*!
357  \brief Send events in broadcast for all registered components.
358 
359  \param evt Event to be sent.
360  */
361  void broadcast(te::qt::af::evt::Event* evt);
362 
363  signals:
364 
365  /*!
366  \brief Signal emmited to the aplication framework listeners.
367  */
368  void triggered(te::qt::af::evt::Event*);
369 
370  public:
371 
372  /*!
373  \brief Returns the application name.
374 
375  \return Application name.
376  */
377  const QString& getAppName() const;
378 
379  /*!
380  \brief Returns the application title.
381 
382  \return Application title.
383  */
384  const QString& getAppTitle() const;
385 
386  /*!
387  \brief Returns the application project extension.
388 
389  \return Application project extension.
390  */
391  const QString& getAppProjectExtension() const;
392 
393  const QString& getAboutLogo() const;
394 
395  const QString& getTlibLogo() const;
396 
397  /*!
398  \brief Returns the application icon.
399 
400  \return Application icon.
401  */
402  const QString& getAppIconName() const;
403 
404  /*!
405  \brief Returns the plugins file path of application.
406 
407  \return A path of plugins file.
408  */
409  const QString& getAppPluginsPath() const;
410 
411  /*!
412  \brief Returns the most recent project.
413 
414  \return Application most recent opened project.
415  */
416  QString getMostRecentProject() const;
417 
418  /*!
419  \brief Returns the application default SRID value.
420 
421  \return Application default SRID value.
422  */
423  int getDefaultSRID() const;
424 
425  /*!
426  \brief Returns the application selection color.
427 
428  \return Application selection color.
429  */
430  QColor getSelectionColor() const;
431 
432  /*! \brief Sets the application selection color. */
433  void setSelectionColor(const QColor& c);
434 
435  /*!
436  \brief Returns main window.
437 
438  \return The main window pointer.
439  */
440  QWidget* getMainWindow() const;
441 
442  void setResetTerraLibFlag(const bool& status);
443 
444  const QString& getUserDataDir() const;
445 
446  protected:
447 
448  std::map<QString, QToolBar*> m_toolbars; //!< Toolbars registered.
449  std::vector<QMenu*> m_menus; //!< Menus registered.
450  std::vector<QMenuBar*> m_menuBars; //!< Menu bars registered.
451  std::set<QObject*> m_applicationItems; //!< The list of registered application items.
452  QWidget* m_msgBoxParentWidget; //!< Parent used to show message boxes.
453  std::string m_appConfigFile; //!< The application framework configuration file.
454  QString m_userDataDir; //!< The data dir used to store data files.
455  QString m_appOrganization; //!< Organization name.
456  QString m_appName; //!< Application name.
457  QString m_appTitle; //!< Application title.
458  QString m_appProjectExtension; //!< Application project extension.
459  QString m_appIconName; //!< Icon used in the application.
460  QString m_aboutLogo;
461  QString m_tLibLogo;
462  QStringList m_recentProjs; //!< List of the recent projects.
463  QStringList m_recentProjsTitles; //!< List of the titles of the recent projects.
464  std::string m_appUserSettingsFile; //!< Name of the user settings file.
465  QString m_appPluginsPath; //!< Name of the plugins path.
466  QString m_appHelpFile; //!< Name of the help file.
467  QString m_appIconThemeDir; //!< Directory of the application icon theme.
468  QString m_appDefaultIconTheme; //!< Name of the icon theme to be used.
469  QString m_appToolBarDefaultIconSize; //!< Size of the tool buttons.
470  std::string m_appDatasourcesFile; //!< Name of the file containing datasources used.
471  int m_defaultSRID; //!< Default SRID value.
472  QColor m_selectionColor; //!< Default selection color.
473 
474  bool m_initialized; //!< A flag indicating if the controller is initialized.
475 
476  Project* m_project; //!< Pointer to current project.
477 
478  QSettings m_appSettings;
479 
481 
482 // Singleton instance
483 // static ApplicationController* sm_instance; //!< There can be only one object of class Application.
484  };
485 
486  } // end namespace af
487  } // end namespace qt
488 } // end namespace te
489 
490 #endif // __TERRALIB_QT_AF_INTERNAL_APPLICATIONCONTROLLER_H
491 
std::string m_appUserSettingsFile
Name of the user settings file.
QStringList m_recentProjs
List of the recent projects.
std::set< QObject * > m_applicationItems
The list of registered application items.
QString m_appIconName
Icon used in the application.
A base class for application events.
Definition: Event.h:59
QColor m_selectionColor
Default selection color.
The base API for TerraLib applications.
QStringList m_recentProjsTitles
List of the titles of the recent projects.
std::map< QString, QToolBar * > m_toolbars
Toolbars registered.
Configuration flags for the TerraLib Application Framework.
QString m_appPluginsPath
Name of the plugins path.
QString m_appTitle
Application title.
std::vector< QMenu * > m_menus
Menus registered.
Project * m_project
Pointer to current project.
QWidget * m_msgBoxParentWidget
Parent used to show message boxes.
URI C++ Library.
std::string m_appConfigFile
The application framework configuration file.
QString m_appHelpFile
Name of the help file.
#define TEQTAFEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:78
std::string m_appDatasourcesFile
Name of the file containing datasources used.
QString m_appToolBarDefaultIconSize
Size of the tool buttons.
This class models the concept of a project for the TerraLib Application Framework.
Definition: Project.h:50
QString m_appIconThemeDir
Directory of the application icon theme.
QString m_appDefaultIconTheme
Name of the icon theme to be used.
std::vector< QMenuBar * > m_menuBars
Menu bars registered.
QString m_appOrganization
Organization name.
bool m_initialized
A flag indicating if the controller is initialized.
QString m_appProjectExtension
Application project extension.
Template support for singleton pattern.
Definition: Singleton.h:100
QString m_userDataDir
The data dir used to store data files.