PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Base/QTGUI/qSlicerApplication.cxx

https://github.com/pieper/Slicer4
C++ | 354 lines | 192 code | 45 blank | 117 comment | 7 complexity | 78af13c2703a095919d4be6bbb0b82fc MD5 | raw file
  1. /*==============================================================================
  2. Program: 3D Slicer
  3. Copyright (c) 2010 Kitware Inc.
  4. See Doc/copyright/copyright.txt
  5. or http://www.slicer.org/copyright/copyright.txt for details.
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
  12. and was partially funded by NIH grant 3P41RR013218-12S1
  13. ==============================================================================*/
  14. // Qt includes
  15. #include <QCleanlooksStyle>
  16. #include <QColor>
  17. #include <QDebug>
  18. #include <QFont>
  19. #include <QFontDatabase>
  20. #include <QFontInfo>
  21. #include <QMap>
  22. #include <QPalette>
  23. #include <QPluginLoader>
  24. #include <QRect>
  25. #include <QStyle>
  26. #include <QWidget>
  27. // CTK includes
  28. #include <ctkIconEnginePlugin.h>
  29. #include <ctkLogger.h>
  30. #include <ctkSettings.h>
  31. // QTGUI includes
  32. #include "qSlicerApplication.h"
  33. #include "qSlicerWidget.h"
  34. #include "qSlicerIOManager.h"
  35. #include "qSlicerCommandOptions.h"
  36. #include "qSlicerLayoutManager.h"
  37. #include "qSlicerStyle.h"
  38. #ifdef Slicer_USE_PYTHONQT
  39. # include "qSlicerPythonManager.h"
  40. #endif
  41. // MRMLLogic includes
  42. #include <vtkMRMLApplicationLogic.h>
  43. //--------------------------------------------------------------------------
  44. static ctkLogger logger("org.slicer.base.qtgui.qSlicerApplication");
  45. //--------------------------------------------------------------------------
  46. //-----------------------------------------------------------------------------
  47. void qSlicerApplyPalette(QPalette& palette)
  48. {
  49. /* Old palette that makes a high contrast in Windows.
  50. palette.setColor(QPalette::Window, Qt::white);
  51. palette.setColor(QPalette::Base, Qt::white);
  52. palette.setColor(QPalette::AlternateBase, QColor("#e4e4fe"));
  53. palette.setColor(QPalette::Button, Qt::white);
  54. */
  55. palette.setColor(QPalette::Button, "#fcfcfc");
  56. palette.setColor(QPalette::Light, "#c8c8c8");
  57. palette.setColor(QPalette::Midlight, "#e6e6e6");
  58. palette.setColor(QPalette::Dark, "#aaaaaa");
  59. palette.setColor(QPalette::Mid, "#c8c8c8");
  60. palette.setColor(QPalette::Base, Qt::white);
  61. palette.setColor(QPalette::Window, Qt::white);
  62. palette.setColor(QPalette::Shadow, "#5a5a5a");
  63. palette.setColor(QPalette::AlternateBase, QColor("#e4e4fe"));
  64. }
  65. //-----------------------------------------------------------------------------
  66. class qSlicerApplicationPrivate
  67. {
  68. Q_DECLARE_PUBLIC(qSlicerApplication);
  69. protected:
  70. qSlicerApplication* const q_ptr;
  71. public:
  72. qSlicerApplicationPrivate(qSlicerApplication& object);
  73. ///
  74. /// Convenient method regrouping all initialization code
  75. void init();
  76. ///
  77. /// Initialize application style
  78. void initStyle();
  79. ///
  80. /// Initialize application palette
  81. /// Note: the palette is reset to its default values by initStyle
  82. void initPalette();
  83. ///
  84. /// Initialize application font
  85. void initFont();
  86. // Description:
  87. // Load application styleSheet
  88. void loadStyleSheet();
  89. QMap<QWidget*,bool> TopLevelWidgetsSavedVisibilityState;
  90. Qt::WindowFlags DefaultWindowFlags;
  91. qSlicerLayoutManager* LayoutManager;
  92. };
  93. //-----------------------------------------------------------------------------
  94. // qSlicerApplicationPrivate methods
  95. //-----------------------------------------------------------------------------
  96. qSlicerApplicationPrivate::qSlicerApplicationPrivate(qSlicerApplication& object)
  97. : q_ptr(&object)
  98. {
  99. this->LayoutManager = 0;
  100. }
  101. //-----------------------------------------------------------------------------
  102. void qSlicerApplicationPrivate::init()
  103. {
  104. Q_Q(qSlicerApplication);
  105. this->initStyle();
  106. this->initFont();
  107. this->initPalette();
  108. this->loadStyleSheet();
  109. // Note: qSlicerCoreApplication class takes ownership of the ioManager and
  110. // will be responsible to delete it
  111. q->setCoreIOManager(new qSlicerIOManager);
  112. #ifdef Slicer_USE_PYTHONQT
  113. // Note: qSlicerCoreApplication class takes ownership of the pythonManager and
  114. // will be responsible to delete it
  115. q->setCorePythonManager(new qSlicerPythonManager());
  116. #endif
  117. }
  118. /*
  119. #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
  120. Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loaderV2,
  121. (QIconEngineFactoryInterfaceV2_iid, QLatin1String("/iconengines"), Qt::CaseInsensitive))
  122. #endif
  123. */
  124. //-----------------------------------------------------------------------------
  125. void qSlicerApplicationPrivate::initStyle()
  126. {
  127. Q_Q(qSlicerApplication);
  128. q->setStyle(new qSlicerStyle(new QCleanlooksStyle));
  129. // Force showing the icons in the menus despite the native OS style
  130. // discourages it
  131. q->setAttribute(Qt::AA_DontShowIconsInMenus, false);
  132. // Init the style of the icons
  133. // The plugin qSlicerIconEnginePlugin is located in the iconengines
  134. // subdirectory of Slicer lib dir (typically lib/Slicer3).
  135. // By adding the path to the lib dir, Qt automatically loads the icon engine
  136. // plugin
  137. q->addLibraryPath(q->slicerHome() + "/" + Slicer_INSTALL_LIB_DIR);
  138. }
  139. //-----------------------------------------------------------------------------
  140. void qSlicerApplicationPrivate::initPalette()
  141. {
  142. Q_Q(qSlicerApplication);
  143. QPalette myPalette = q->palette();
  144. qSlicerApplyPalette(myPalette);
  145. q->setPalette(myPalette);
  146. }
  147. //-----------------------------------------------------------------------------
  148. void qSlicerApplicationPrivate::initFont()
  149. {
  150. /*
  151. Q_Q(qSlicerApplication);
  152. QFont f("Verdana", 9);
  153. QFontInfo ff(f);
  154. QFontDatabase database;
  155. foreach (QString family, database.families())
  156. {
  157. cout << family.toStdString() << endl;
  158. }
  159. cout << "Family: " << ff.family().toStdString() << endl;
  160. cout << "Size: " << ff.pointSize() << endl;
  161. q->setFont(f);
  162. */
  163. }
  164. //-----------------------------------------------------------------------------
  165. void qSlicerApplicationPrivate::loadStyleSheet()
  166. {
  167. // Q_Q(qSlicerApplication);
  168. // QString styleSheet =
  169. // "background-color: white;"
  170. // "alternate-background-color: #e4e4fe;";
  171. //
  172. // q->setStyleSheet(styleSheet);
  173. }
  174. //-----------------------------------------------------------------------------
  175. // qSlicerApplication methods
  176. //-----------------------------------------------------------------------------
  177. qSlicerApplication::qSlicerApplication(int &_argc, char **_argv):Superclass(_argc, _argv)
  178. , d_ptr(new qSlicerApplicationPrivate(*this))
  179. {
  180. Q_D(qSlicerApplication);
  181. d->init();
  182. }
  183. //-----------------------------------------------------------------------------
  184. qSlicerApplication::~qSlicerApplication()
  185. {
  186. }
  187. //-----------------------------------------------------------------------------
  188. qSlicerApplication* qSlicerApplication::application()
  189. {
  190. qSlicerApplication* app = qobject_cast<qSlicerApplication*>(QApplication::instance());
  191. return app;
  192. }
  193. //-----------------------------------------------------------------------------
  194. void qSlicerApplication::initialize(bool& exitWhenDone)
  195. {
  196. // If specific command line option are required for the different Slicer apps
  197. // (SlicerQT, SlicerBatch, SlicerDaemon, ...).
  198. // The class qSlicerCommandOptions could be subclassed into, for example,
  199. // qSlicerGUICommandOptions, qSlicerDaemonCommandOptions, ...
  200. // Each subclass should be added in their respective Applications/Slicer{Batch, Daemon}
  201. // directory.
  202. // The following line should also be moved into the 'Main.cxx' specific to each app.
  203. // This comment should also be deleted !
  204. this->setCoreCommandOptions(new qSlicerCommandOptions(this->settings()));
  205. // Proceed to initialization of the Core
  206. this->Superclass::initialize(exitWhenDone);
  207. }
  208. //-----------------------------------------------------------------------------
  209. qSlicerCommandOptions* qSlicerApplication::commandOptions()
  210. {
  211. qSlicerCommandOptions* _commandOptions =
  212. dynamic_cast<qSlicerCommandOptions*>(this->coreCommandOptions());
  213. Q_ASSERT(_commandOptions);
  214. return _commandOptions;
  215. }
  216. //-----------------------------------------------------------------------------
  217. qSlicerIOManager* qSlicerApplication::ioManager()
  218. {
  219. qSlicerIOManager* _ioManager = dynamic_cast<qSlicerIOManager*>(this->coreIOManager());
  220. Q_ASSERT(_ioManager);
  221. return _ioManager;
  222. }
  223. #ifdef Slicer_USE_PYTHONQT
  224. //-----------------------------------------------------------------------------
  225. qSlicerPythonManager* qSlicerApplication::pythonManager()
  226. {
  227. qSlicerPythonManager* _pythonManager =
  228. qobject_cast<qSlicerPythonManager*>(this->corePythonManager());
  229. Q_ASSERT(_pythonManager);
  230. return _pythonManager;
  231. }
  232. #endif
  233. //-----------------------------------------------------------------------------
  234. void qSlicerApplication::setLayoutManager(qSlicerLayoutManager* layoutManager)
  235. {
  236. Q_D(qSlicerApplication);
  237. d->LayoutManager = layoutManager;
  238. this->mrmlApplicationLogic()->SetSliceLogics(
  239. layoutManager? layoutManager->mrmlSliceLogics() : 0);
  240. }
  241. //-----------------------------------------------------------------------------
  242. qSlicerLayoutManager* qSlicerApplication::layoutManager()const
  243. {
  244. Q_D(const qSlicerApplication);
  245. return d->LayoutManager;
  246. }
  247. //-----------------------------------------------------------------------------
  248. CTK_SET_CPP(qSlicerApplication, Qt::WindowFlags, setDefaultWindowFlags, DefaultWindowFlags);
  249. CTK_GET_CPP(qSlicerApplication, Qt::WindowFlags, defaultWindowFlags, DefaultWindowFlags);
  250. //-----------------------------------------------------------------------------
  251. void qSlicerApplication::setTopLevelWidgetsVisible(bool visible)
  252. {
  253. Q_D(qSlicerApplication);
  254. foreach(QWidget * widget, this->topLevelWidgets())
  255. {
  256. // Store current visibility state
  257. if (!visible)
  258. {
  259. if (!d->TopLevelWidgetsSavedVisibilityState.contains(widget))
  260. {
  261. d->TopLevelWidgetsSavedVisibilityState[widget] = widget->isVisible();
  262. }
  263. widget->hide();
  264. }
  265. else
  266. {
  267. QMap<QWidget*,bool>::const_iterator it = d->TopLevelWidgetsSavedVisibilityState.find(widget);
  268. // If widget state was saved, restore it. Otherwise skip.
  269. if (it != d->TopLevelWidgetsSavedVisibilityState.end())
  270. {
  271. widget->setVisible(it.value());
  272. }
  273. }
  274. }
  275. // Each time widget are set visible. Internal Map can be cleared.
  276. if (visible)
  277. {
  278. d->TopLevelWidgetsSavedVisibilityState.clear();
  279. }
  280. }
  281. //-----------------------------------------------------------------------------
  282. void qSlicerApplication::setTopLevelWidgetVisible(qSlicerWidget* widget, bool visible)
  283. {
  284. if (!widget) { return; }
  285. Q_ASSERT(!widget->parent());
  286. Q_D(qSlicerApplication);
  287. // When internal Map is empty, it means top widget are visible
  288. if (d->TopLevelWidgetsSavedVisibilityState.empty())
  289. {
  290. widget->setVisible(visible);
  291. }
  292. else
  293. {
  294. d->TopLevelWidgetsSavedVisibilityState[widget] = visible;
  295. }
  296. }
  297. //-----------------------------------------------------------------------------
  298. QSettings* qSlicerApplication::newSettings(const QString& organization,
  299. const QString& application)
  300. {
  301. return new ctkSettings(organization, application, this);
  302. }