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