application.cpp
1 //------------------------------------------------------------------------------
2 // application.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "application.h"
24 
25 #include "configuration/doomseekerconfig.h"
26 #include "gui/mainwindow.h"
27 #include <QPointer>
28 #include <cassert>
29 
30 const QString Application::NAME = "doomseeker";
31 
32 DClass<Application>
33 {
34 public:
35  QPointer<MainWindow> mainWindow;
36  bool running;
37  QStringList originalArgs;
38 };
39 
40 DPointered(Application)
41 
42 Application *Application::staticInstance = nullptr;
43 
44 Application::Application(int &argc, char **argv)
45  : QApplication(argc, argv)
46 {
47  d->mainWindow = nullptr;
48  d->running = true;
49  setApplicationName(NAME);
50 #if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
51  setDesktopFileName("org.drdteam.Doomseeker");
52 #endif
53 
54  // http://blog.qt.io/blog/2013/04/25/retina-display-support-for-mac-os-ios-and-x11/
55  setAttribute(Qt::AA_UseHighDpiPixmaps);
56 }
57 
58 
59 Application::~Application()
60 {
61 }
62 
64 {
65  if (staticInstance != nullptr)
66  {
67  staticInstance->destroy();
68  delete staticInstance;
69  staticInstance = nullptr;
70  }
71 }
72 
73 void Application::destroy()
74 {
75  d->running = false;
76 }
77 
78 QIcon Application::icon()
79 {
80  return QIcon(":/icon.png");
81 }
82 
83 bool Application::isInit()
84 {
85  return staticInstance != nullptr;
86 }
87 
88 void Application::init(int &argc, char **argv)
89 {
90  assert(staticInstance == nullptr && "Cannot initialize Application twice!");
91  // The argv must captured before it's passed to Application because
92  // Application calls Qt where the argc and argv may be modified.
93  QStringList args;
94  for (int i = 0; i < argc; ++i)
95  args << argv[i];
96  staticInstance = new Application(argc, argv);
97  staticInstance->d->originalArgs = args;
98 }
99 
100 Application *Application::instance()
101 {
102  assert(staticInstance != nullptr);
103  return staticInstance;
104 }
105 
107 {
108  return gConfig.doomseeker.bCheckTheIntegrityOfWads;
109 }
110 
112 {
113  return d->running;
114 }
115 
117 {
118  return d->mainWindow.data();
119 }
120 
122 {
123  return d->mainWindow.data();
124 }
125 
126 void Application::setMainWindow(MainWindow *mainWindow)
127 {
128  d->mainWindow = mainWindow;
129 }
130 
131 const QStringList &Application::originalArgs() const
132 {
133  return d->originalArgs;
134 }
135 
137 {
138  d->running = false;
139 }