CustusX  16.5.0-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 
40 #include <iostream>
41 
42 #if !defined(WIN32)
43 #include <langinfo.h>
44 #include <locale>
45 #endif
46 
47 namespace cx
48 {
49 
50 Application::Application(int& argc, char** argv) : QApplication(argc, argv)
51 {
52  this->force_C_locale_decimalseparator();
53 
54  QFile stylesheet(":/cxStyleSheet.ss");
55  stylesheet.open(QIODevice::ReadOnly);
56  this->setStyleSheet(stylesheet.readAll());
57 }
58 
59 void Application::reportException(QString text)
60 {
61  QThread* main = this->thread();
62  QThread* current = QThread::currentThread();
63  QString threadName = current->objectName();
64  if (threadName.isEmpty())
65  {
66  if (main==current)
67  threadName = "MAIN";
68  else
69  threadName = "SECONDARY";
70  }
71 
72  reportError(QString("Exception caught: [%1]\nin thread [%2]").arg(text).arg(threadName));
73 }
74 
75 bool Application::notify(QObject *rec, QEvent *ev)
76 {
77  try
78  {
79  return QApplication::notify(rec, ev);
80  }
81  catch(std::exception& e)
82  {
83  this->reportException(e.what());
84  exit(0);
85  }
86  catch( ... )
87  {
88  this->reportException("Unknown");
89  exit(0);
90  }
91  return false;
92 }
93 
94 
95 void Application::force_C_locale_decimalseparator()
96 {
97 #if !defined(WIN32)
98  QString radixChar = nl_langinfo(RADIXCHAR);
99  QString C_radixChar = ".";
100 
101  if (radixChar != C_radixChar)
102  {
103  QLocale::setDefault(QLocale::c());
104  setlocale(LC_NUMERIC,"C");
105 
106  std::cout << QString("Detected non-standard decimal separator [%1], changing to standard [%2].")
107  .arg(radixChar)
108  .arg(C_radixChar)
109  << std::endl;
110 
111  QString number = QString("%1").arg(0.5);
112  if (!number.contains(C_radixChar))
113  std::cout << "Failed to set decimal separator." << std::endl;
114  }
115 #endif
116 }
117 
118 void bringWindowToFront(QWidget* window)
119 {
120  if (!window->isVisible())
121  window->show();
122 
123 #ifdef __APPLE__ // needed on mac for bringing to front: does the opposite on linux
124  window->activateWindow();
125 #endif
126  window->raise();
127 }
128 
129 } // 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)
virtual bool notify(QObject *rec, QEvent *ev)