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 "gui/mainwindow.h"
26 #include <QPointer>
27 #include <cassert>
28 
29 const QString Application::NAME = "doomseeker";
30 
31 DClass<Application>
32 {
33 public:
34  QPointer<MainWindow> mainWindow;
35  bool running;
36  QStringList originalArgs;
37 };
38 
39 DPointered(Application)
40 
41 Application *Application::staticInstance = nullptr;
42 
43 Application::Application(int &argc, char **argv)
44  : QApplication(argc, argv)
45 {
46  d->mainWindow = nullptr;
47  d->running = true;
48  setApplicationName(NAME);
49 
50  // http://blog.qt.io/blog/2013/04/25/retina-display-support-for-mac-os-ios-and-x11/
51  setAttribute(Qt::AA_UseHighDpiPixmaps);
52 }
53 
54 
55 Application::~Application()
56 {
57 }
58 
60 {
61  if (staticInstance != nullptr)
62  {
63  staticInstance->destroy();
64  delete staticInstance;
65  staticInstance = nullptr;
66  }
67 }
68 
69 void Application::destroy()
70 {
71  d->running = false;
72 }
73 
74 QIcon Application::icon()
75 {
76  return QIcon(":/icon.png");
77 }
78 
79 bool Application::isInit()
80 {
81  return staticInstance != nullptr;
82 }
83 
84 void Application::init(int &argc, char **argv)
85 {
86  assert(staticInstance == nullptr && "Cannot initialize Application twice!");
87  // The argv must captured before it's passed to Application because
88  // Application calls Qt where the argc and argv may be modified.
89  QStringList args;
90  for (int i = 0; i < argc; ++i)
91  args << argv[i];
92  staticInstance = new Application(argc, argv);
93  staticInstance->d->originalArgs = args;
94 }
95 
96 Application *Application::instance()
97 {
98  assert(staticInstance != nullptr);
99  return staticInstance;
100 }
101 
103 {
104  return d->running;
105 }
106 
108 {
109  return d->mainWindow.data();
110 }
111 
113 {
114  return d->mainWindow.data();
115 }
116 
117 void Application::setMainWindow(MainWindow *mainWindow)
118 {
119  d->mainWindow = mainWindow;
120 }
121 
122 const QStringList &Application::originalArgs() const
123 {
124  return d->originalArgs;
125 }
126 
128 {
129  d->running = false;
130 }