Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 
34 #include "cxApplication.h"
35 #include "cxLogger.h"
36 #include <QThread>
37 #include <QWidget>
38 #include <QFile>
39 #include <QAction>
40 
41 #include <iostream>
42 
43 #if !defined(WIN32)
44 #include <langinfo.h>
45 #include <locale>
46 #endif
47 
48 namespace cx
49 {
50 
51 Application::Application(int& argc, char** argv) : QApplication(argc, argv)
52 {
53  this->force_C_locale_decimalseparator();
54 
55  QFile stylesheet(":/cxStyleSheet.ss");
56  stylesheet.open(QIODevice::ReadOnly);
57  this->setStyleSheet(stylesheet.readAll());
58 }
59 
60 void Application::reportException(QString text)
61 {
62  QThread* main = this->thread();
63  QThread* current = QThread::currentThread();
64  QString threadName = current->objectName();
65  if (threadName.isEmpty())
66  {
67  if (main==current)
68  threadName = "MAIN";
69  else
70  threadName = "SECONDARY";
71  }
72 
73  reportError(QString("Exception caught: [%1]\nin thread [%2]").arg(text).arg(threadName));
74 }
75 
76 bool Application::notify(QObject *rec, QEvent *ev)
77 {
78  try
79  {
80  return QApplication::notify(rec, ev);
81  }
82  catch(std::exception& e)
83  {
84  this->reportException(e.what());
85  exit(0);
86  }
87  catch( ... )
88  {
89  this->reportException("Unknown");
90  exit(0);
91  }
92  return false;
93 }
94 
95 
96 void Application::force_C_locale_decimalseparator()
97 {
98 #if !defined(WIN32)
99  QString radixChar = nl_langinfo(RADIXCHAR);
100  QString C_radixChar = ".";
101 
102  if (radixChar != C_radixChar)
103  {
104  QLocale::setDefault(QLocale::c());
105  setlocale(LC_NUMERIC,"C");
106 
107  std::cout << QString("Detected non-standard decimal separator [%1], changing to standard [%2].")
108  .arg(radixChar)
109  .arg(C_radixChar)
110  << std::endl;
111 
112  QString number = QString("%1").arg(0.5);
113  if (!number.contains(C_radixChar))
114  std::cout << "Failed to set decimal separator." << std::endl;
115  }
116 #endif
117 }
118 
119 void bringWindowToFront(QWidget* window)
120 {
121  if (!window->isVisible())
122  window->show();
123 
124 #ifdef __APPLE__ // needed on mac for bringing to front: does the opposite on linux
125  window->activateWindow();
126 #endif
127  window->raise();
128 }
129 
131 {
132  QWidget *mainwindow = Q_NULLPTR;
133  foreach(mainwindow, QApplication::topLevelWidgets()) {
134  if(mainwindow->objectName() == "MainWindow")
135  break;
136  }
137  QAction* action = mainwindow->findChild<QAction*>(actionName);
138  if(action)
139  {
140  action->trigger();
141  }
142 }
143 
144 } // namespace cx
int main(int argc, char *argv[])
Definition: cxMain.cpp:40
void reportError(QString msg)
Definition: cxLogger.cpp:92
Application(int &argc, char **argv)
void bringWindowToFront(QWidget *window)
void triggerMainWindowActionWithObjectName(QString actionName)
virtual bool notify(QObject *rec, QEvent *ev)