NorMIT-nav  18.04
An IGT application
cxApplication.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 
13 #include "cxApplication.h"
14 #include "cxLogger.h"
15 #include <QThread>
16 #include <QWidget>
17 #include <QFile>
18 #include <QAction>
19 
20 #include <iostream>
21 
22 #if !defined(WIN32)
23 #include <langinfo.h>
24 #include <locale>
25 #endif
26 
27 namespace cx
28 {
29 
30 Application::Application(int& argc, char** argv) : QApplication(argc, argv)
31 {
32  this->force_C_locale_decimalseparator();
33 
34  QFile stylesheet(":/cxStyleSheet.ss");
35  stylesheet.open(QIODevice::ReadOnly);
36  this->setStyleSheet(stylesheet.readAll());
37 }
38 
39 void Application::reportException(QString text)
40 {
41  QThread* main = this->thread();
42  QThread* current = QThread::currentThread();
43  QString threadName = current->objectName();
44  if (threadName.isEmpty())
45  {
46  if (main==current)
47  threadName = "MAIN";
48  else
49  threadName = "SECONDARY";
50  }
51 
52  reportError(QString("Exception caught: [%1]\nin thread [%2]").arg(text).arg(threadName));
53 }
54 
55 bool Application::notify(QObject *rec, QEvent *ev)
56 {
57  try
58  {
59  return QApplication::notify(rec, ev);
60  }
61  catch(std::exception& e)
62  {
63  this->reportException(e.what());
64  exit(0);
65  }
66  catch( ... )
67  {
68  this->reportException("Unknown");
69  exit(0);
70  }
71  return false;
72 }
73 
74 
75 void Application::force_C_locale_decimalseparator()
76 {
77 #if !defined(WIN32)
78  QString radixChar = nl_langinfo(RADIXCHAR);
79  QString C_radixChar = ".";
80 
81  if (radixChar != C_radixChar)
82  {
83  QLocale::setDefault(QLocale::c());
84  setlocale(LC_NUMERIC,"C");
85 
86  std::cout << QString("Detected non-standard decimal separator [%1], changing to standard [%2].")
87  .arg(radixChar)
88  .arg(C_radixChar)
89  << std::endl;
90 
91  QString number = QString("%1").arg(0.5);
92  if (!number.contains(C_radixChar))
93  std::cout << "Failed to set decimal separator." << std::endl;
94  }
95 #endif
96 }
97 
98 void bringWindowToFront(QWidget* window)
99 {
100  if (!window->isVisible())
101  window->show();
102 
103 #ifdef __APPLE__ // needed on mac for bringing to front: does the opposite on linux
104  window->activateWindow();
105 #endif
106  window->raise();
107 }
108 
109 QWidget* getMainWindow()
110 {
111  QWidget *mainwindow = Q_NULLPTR;
112  foreach(mainwindow, QApplication::topLevelWidgets()) {
113  if(mainwindow->objectName() == "main_window")
114  break;
115  }
116  return mainwindow;
117 }
118 
119 template<typename T>
121 {
122  QWidget *mainwindow = getMainWindow();
123  T object = mainwindow->findChild<T>(objectName);
124  return object;
125 }
126 
128 {
129  QAction* action = findMainWindowChildWithObjectName<QAction*>(actionName);
130  if(action)
131  {
132  action->trigger();
133  }
134 }
135 
136 template
137 cxResource_EXPORT QWidget* findMainWindowChildWithObjectName(QString objectName);
138 
139 } // namespace cx
T findMainWindowChildWithObjectName(QString objectName)
int main(int argc, char *argv[])
Definition: cxMain.cpp:20
void reportError(QString msg)
Definition: cxLogger.cpp:71
Application(int &argc, char **argv)
void bringWindowToFront(QWidget *window)
void triggerMainWindowActionWithObjectName(QString actionName)
QWidget * getMainWindow()
virtual bool notify(QObject *rec, QEvent *ev)
Namespace for all CustusX production code.