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 <QObject>
43 #include <QStringList>
44 #include <QSettings>
45 #include <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 
70  {
74  };
75 
76  /*!
77  \class ApplicationController
78 
79  \brief The base API for TerraLib applications.
80 
81  \note Temporally this class is a singleton!
82 
83  \ingroup af
84  */
85  class TEQTAFEXPORT ApplicationController : public QObject//, public te::common::Singleton<ApplicationController>
86  {
87  Q_OBJECT
88 
89  public:
90 
91  /*!
92  \brief Constructor.
93 
94  \param parent The parent object.
95  */
96  ApplicationController(/*QObject* parent = 0*/);
97 
98  /*!
99  \brief Destructor.
100  */
101  virtual ~ApplicationController();
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  \brief Returns the action group identified by \a id or NULL if there's not an action group identified by \a id.
246 
247  \param id Identifier of the action group.
248 
249  \return QActionGroup identified or NULL if could not find one.
250  */
251  QActionGroup* findActionGroup(const QString& id) const;
252 
253  //@}
254 
255  /*!
256  \brief Insert an application item that will listen to framework events.
257 
258  \param obj The application item that will listen to framework events.
259 
260  \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.
261 
262  \note If the \a obj was previously registered, nothing will be done.
263  */
264  void addListener(QObject* obj, const ListenerType& type = BOTH);
265 
266  /*!
267  \brief Remove the \a obj from the list of event listeners.
268 
269  \param obj The application item to be removed.
270 
271  \note If the \a obj was not previously registered, nothing will be done.
272  */
273  void removeListener(QObject* obj);
274 
275  /*!
276  \brief Initializes the application framework.
277 
278  The initialization will read the following configurations files (in order):
279  <ul>
280  <li>A main config file, generally called config.xml</li>
281  <li>A user settings file, generally called user_settings.xml<li>
282  </ul>
283 
284  It will start by initializing the available TerraLib modules.
285  This means that applications that uses the application framework,
286  does not need to call TerraLib initialization (TerraLib::getInstance().initialize()) and
287  finalization methods (TerraLib::getInstance().finalize()).
288 
289  \exception Exception It throws an exception if the initialization fails.
290 
291  \note Make sure that the application calls this method BEFORE it uses any TerraLib modules or functions.
292  */
293  virtual void initialize();
294 
295  /*!
296  \brief Load the plugin list and initialize the plugins enabled by the user.
297 
298  The plugin initialization will read a file containing a list of plugins with their addresses.
299  This file is generally called application_plugins.xml.
300 
301  \pre The application must assure that any resource needed by their plugins are ready for use. This
302  means that initialize should be called before initializing the plugins.
303  */
304  virtual void initializePlugins();
305 
306  /*!
307  \brief Finalize the application framework.
308 
309  \details This method will unload all TerraLib modules, plugins, and dynamic libraries that are loaded.
310 
311  \note Make sure that the application calls this method BEFORE it finishes.
312  */
313  virtual void finalize();
314 
315  virtual void finalizePlugins();
316 
317  /*!
318  \brief Return the QSettings of the application. This can be used to add settings from external sources.
319 
320  \return QSettings being used.
321  */
322  QSettings& getSettings();
323 
324  public slots:
325 
326  /*!
327  \brief Send events in broadcast for all registered components.
328 
329  \param evt Event to be sent.
330  */
331 // void broadcast(te::qt::af::evt::Event* evt);
332 
333  signals:
334 
335  /*!
336  \brief Signal emmited to the aplication framework listeners.
337  */
338  void triggered(te::qt::af::evt::Event*);
339 
340  public:
341 
342 
343  void trigger(te::qt::af::evt::Event*);
344 
345  /*!
346  \brief Returns the application name.
347 
348  \return Application name.
349  */
350  const QString& getAppName() const;
351 
352  /*!
353  \brief Returns the application title.
354 
355  \return Application title.
356  */
357  const QString& getAppTitle() const;
358 
359  /*!
360  \brief Returns the application project extension.
361 
362  \return Application project extension.
363  */
364  //const QString& getAppProjectExtension() const;
365 
366  const QString& getAboutLogo() const;
367 
368  const QString& getTlibLogo() const;
369 
370  /*!
371  \brief Returns the application icon.
372 
373  \return Application icon.
374  */
375  const QString& getAppIconName() const;
376 
377  /*!
378  \brief Returns the plugins file path of application.
379 
380  \return A path of plugins file.
381  */
382  const QString& getAppPluginsPath() const;
383 
384  /*!
385  \brief Returns the most recent project.
386 
387  \return Application most recent opened project.
388  */
389  //QString getMostRecentProject() const;
390 
391  /*!
392  \brief Returns the application default SRID value.
393 
394  \return Application default SRID value.
395  */
396  int getDefaultSRID() const;
397 
398  /*!
399  \brief Returns the application selection color.
400 
401  \return Application selection color.
402  */
403  QColor getSelectionColor() const;
404 
405  /*! \brief Sets the application selection color. */
406  void setSelectionColor(const QColor& c);
407 
408  /*!
409  \brief Returns main window.
410 
411  \return The main window pointer.
412  */
413  QWidget* getMainWindow() const;
414 
415  void setResetTerraLibFlag(const bool& status);
416 
417  const QString& getUserDataDir() const;
418 
419  protected:
420 
421  std::map<QString, QToolBar*> m_toolbars; //!< Toolbars registered.
422  std::vector<QMenu*> m_menus; //!< Menus registered.
423  std::vector<QMenuBar*> m_menuBars; //!< Menu bars registered.
424  std::set<QObject*> m_applicationItems; //!< The list of registered application items.
425  QWidget* m_msgBoxParentWidget; //!< Parent used to show message boxes.
426  std::string m_appConfigFile; //!< The application framework configuration file.
427  QString m_userDataDir; //!< The data dir used to store data files.
428  QString m_appOrganization; //!< Organization name.
429  QString m_appName; //!< Application name.
430  QString m_appTitle; //!< Application title.
431  QString m_appIconName; //!< Icon used in the application.
432  QString m_aboutLogo;
433  QString m_tLibLogo;
434  std::string m_appUserSettingsFile; //!< Name of the user settings file.
435  QString m_appPluginsPath; //!< Name of the plugins path.
436  QString m_appHelpFile; //!< Name of the help file.
437  QString m_appIconThemeDir; //!< Directory of the application icon theme.
438  QString m_appDefaultIconTheme; //!< Name of the icon theme to be used.
439  QString m_appToolBarDefaultIconSize; //!< Size of the tool buttons.
440  std::string m_appDatasourcesFile; //!< Name of the file containing datasources used.
441  int m_defaultSRID; //!< Default SRID value.
442  QColor m_selectionColor; //!< Default selection color.
443 
444  bool m_initialized; //!< A flag indicating if the controller is initialized.
445 
446  QSettings m_appSettings;
447 
449 
450  };
451 
452  class TEQTAFEXPORT AppCtrlSingleton: public te::common::Singleton<ApplicationController>
453  {
454  };
455  } // end namespace af
456  } // end namespace qt
457 } // end namespace te
458 
459 #endif // __TERRALIB_QT_AF_INTERNAL_APPLICATIONCONTROLLER_H
460 
std::string m_appUserSettingsFile
Name of the user settings file.
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.
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.
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:71
std::string m_appDatasourcesFile
Name of the file containing datasources used.
QString m_appToolBarDefaultIconSize
Size of the tool buttons.
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.
Template support for singleton pattern.
Definition: Singleton.h:100
QString m_userDataDir
The data dir used to store data files.