NorMIT-nav  2023.01.05-dev+develop.0da12
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 #include <QDockWidget>
20 
21 #include <iostream>
22 
23 #if !defined(WIN32)
24 #include <langinfo.h>
25 #include <locale>
26 #endif
27 
28 namespace cx
29 {
30 
31 Application::Application(int& argc, char** argv) : QApplication(argc, argv)
32 {
33  this->force_C_locale_decimalseparator();
34 
35  QFile stylesheet(":/cxStyleSheet.ss");
36  stylesheet.open(QIODevice::ReadOnly);
37  this->setStyleSheet(stylesheet.readAll());
38 }
39 
40 void Application::reportException(QString text)
41 {
42  QThread* main = this->thread();
43  QThread* current = QThread::currentThread();
44  QString threadName = current->objectName();
45  if (threadName.isEmpty())
46  {
47  if (main==current)
48  threadName = "MAIN";
49  else
50  threadName = "SECONDARY";
51  }
52 
53  reportError(QString("Exception caught: [%1]\nin thread [%2]").arg(text).arg(threadName));
54 }
55 
56 bool Application::notify(QObject *rec, QEvent *ev)
57 {
58  try
59  {
60  return QApplication::notify(rec, ev);
61  }
62  catch(std::exception& e)
63  {
64  this->reportException(e.what());
65  exit(0);
66  }
67  catch( ... )
68  {
69  this->reportException("Unknown");
70  exit(0);
71  }
72  return false;
73 }
74 
75 
76 void Application::force_C_locale_decimalseparator()
77 {
78 #if !defined(WIN32)
79  QString radixChar = nl_langinfo(RADIXCHAR);
80  QString C_radixChar = ".";
81 
82  if (radixChar != C_radixChar)
83  {
84  QLocale::setDefault(QLocale::c());
85  setlocale(LC_NUMERIC,"C");
86 
87  std::cout << QString("Detected non-standard decimal separator [%1], changing to standard [%2].")
88  .arg(radixChar)
89  .arg(C_radixChar)
90  << std::endl;
91 
92  QString number = QString("%1").arg(0.5);
93  if (!number.contains(C_radixChar))
94  std::cout << "Failed to set decimal separator." << std::endl;
95  }
96 #endif
97 }
98 
99 void bringWindowToFront(QWidget* window)
100 {
101  if (!window->isVisible())
102  window->show();
103 
104 #ifdef __APPLE__ // needed on mac for bringing to front: does the opposite on linux
105  window->activateWindow();
106 #endif
107  window->raise();
108 }
109 
110 QWidget* getMainWindow()
111 {
112  QWidget *mainwindow = Q_NULLPTR;
113  foreach(mainwindow, QApplication::topLevelWidgets()) {
114  if(mainwindow->objectName() == "main_window")
115  break;
116  }
117  return mainwindow;
118 }
119 
120 template<typename T>
122 {
123  QWidget *mainwindow = getMainWindow();
124  T object = mainwindow->findChild<T>(objectName);
125  return object;
126 }
127 
129 {
130  QAction* action = findMainWindowChildWithObjectName<QAction*>(actionName);
131  if(action)
132  {
133  action->trigger();
134  }
135 }
136 
137 template
138 cxResource_EXPORT QWidget* findMainWindowChildWithObjectName(QString objectName);
139 
140 template
141 cxResource_EXPORT QDockWidget* findMainWindowChildWithObjectName(QString objectName);
142 
143 } // namespace cx
cxLogger.h
cx::Application::notify
virtual bool notify(QObject *rec, QEvent *ev)
Definition: cxApplication.cpp:56
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::triggerMainWindowActionWithObjectName
void triggerMainWindowActionWithObjectName(QString actionName)
Definition: cxApplication.cpp:128
cx::getMainWindow
QWidget * getMainWindow()
Definition: cxApplication.cpp:110
main
int main(int argc, char *argv[])
Definition: cxMain.cpp:20
cx::Application::Application
Application(int &argc, char **argv)
Definition: cxApplication.cpp:31
cx::findMainWindowChildWithObjectName
T findMainWindowChildWithObjectName(QString objectName)
Definition: cxApplication.cpp:121
cx::bringWindowToFront
void bringWindowToFront(QWidget *window)
Definition: cxApplication.cpp:99
cxApplication.h
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71