-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwordsearchapplication.cpp
More file actions
233 lines (213 loc) · 7.41 KB
/
wordsearchapplication.cpp
File metadata and controls
233 lines (213 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/***************************************************************************
* Copyright (C) 2006-2023 M Wellings *
* info@openforeveryone.co.uk *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2 as *
* published by the Free Software Foundation *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QMessageBox>
#include <QSplashScreen>
#include <QFileOpenEvent>
#include <QMenu>
#ifdef Q_OS_WIN32
#include <QFontDatabase>
#endif
#include "wordsearchdoc.h"
#include "mainwindow.h"
#include "updatechecker.h"
#include "wordsearchapplication.h"
WordSearchApplication::WordSearchApplication( int & argc, char **argv ) :
QApplication(argc,argv)
{
QCoreApplication::setOrganizationName("OpenForEveryone");
QCoreApplication::setOrganizationDomain("openforeveryone.co.uk");
QCoreApplication::setApplicationName("Word Search Creator");
Splash = new QSplashScreen(QPixmap(":/images/about.png"));
Splash->show();
processEvents();
#ifdef Q_OS_MAC
setupDockMenu();
#endif
int fileloaded = false;
WordSearchDoc *newwsd;
//Are we being asked to open a file?
if (QCoreApplication::arguments().size() > 1)
{
newwsd = new WordSearchDoc;
QString fileName=QCoreApplication::arguments().last();
//Open the file
fileloaded = !newwsd->Open(fileName);
if (!fileloaded)
QMessageBox::warning(Splash,"Word Search Creator 1.2", QString("Cannot open file: %1").arg(fileName));
}
updateChecker = new UpdateChecker(this);
updateChecker->checkForUpdate(false);
if (!fileloaded)
{
//No file has been loaded in this function.
//Bring up a blank window only if there are no other windows (that may have been created by a Mac asking us to open a file).
if (windows.count()==0)
window = new MainWindow();
window->showMaximized();
Splash->finish(window);
}
else
{
//A file was loaded in this function, show it in a new MainWindow.
window = new MainWindow(newwsd);
newwsd->setEditedState(false);
window->showMaximized();
Splash->finish(window);
}
#ifdef Q_OS_WIN32
//Make the fonts avalible to the application
QFontDatabase::addApplicationFont(QCoreApplication::applicationDirPath().append("/URWGothicL-Book.ttf"));
#endif
}
void WordSearchApplication::loadFile(const QString &fileName)
{
//This function is called when a Mac asks us to open a file.
#ifdef Q_OS_MAC
setupDockMenu();
#endif
WordSearchDoc *newwsd;
int fileloaded = false;
newwsd = new WordSearchDoc;
fileloaded = !newwsd->Open(fileName);
if (!fileloaded)
QMessageBox::warning(Splash,"Word Search Creator 1.2", QString("Cannot open file: %1").arg(fileName));
window = new MainWindow(newwsd);
newwsd->setEditedState(false);
window->show();
}
bool WordSearchApplication::event(QEvent *event)
{
//Catch any QEvent::FileOpen events and call loadFile(const QString &fileName) otherwise use the default behaviour.
switch (event->type()) {
case QEvent::FileOpen:
loadFile(static_cast<QFileOpenEvent *>(event)->file());
return true;
default:
return QApplication::event(event);
}
}
void WordSearchApplication::registerWindow(MainWindow *window)
{
windows.append(window);
updateWindowLists();
}
void WordSearchApplication::deRegisterWindow(MainWindow *window)
{
windows.removeAll(window);
updateWindowLists();
}
void WordSearchApplication::updateWindowLists()
{
//Emitting this signal will cause all MainWindows to update their window menus
emit windowListChanged();
#ifdef Q_OS_MAC
//On macOS we also want and the dock menu to be updated
updateDockMenu();
#endif
}
void WordSearchApplication::quitApplication()
{
foreach (MainWindow *window, windows)
{
window->close();
}
}
#ifdef Q_OS_MAC
void WordSearchApplication::setupDockMenu()
{
if (dockMenu)
return;
dockMenu = new QMenu("DockMenu");
dockMenu->setAsDockMenu();
dockSerprator = dockMenu->addSeparator();
QAction *newAct = new QAction(tr("New Wordsearch"), this);
dockMenu->addAction(newAct);
connect(newAct, SIGNAL(triggered()), this, SLOT(New()));
windowselectorGroup = new QActionGroup(this);
connect(windowselectorGroup, SIGNAL(triggered(QAction*)), this, SLOT(windowSelected(QAction*)));
}
void WordSearchApplication::updateDockMenu()
{
foreach (QAction *action, windowActions)
{
dockMenu->removeAction(action);
delete action;
}
windowActions.clear();
int i=0;
foreach (MainWindow *window, windows)
{
QString title = window->windowTitle();
//Remove the edited placeholder. Shoud be done better than this.
title.remove("[*]");
QAction *windowAction = new QAction(title, this);
dockMenu->insertAction(dockSerprator, windowAction);
windowAction->setCheckable(true);
windowAction->setData(i);
windowAction->setActionGroup(windowselectorGroup);
if (window->isActiveWindow())
{
windowAction->setChecked(true);
}
windowActions.append(windowAction);
i++;
}
}
#endif
void WordSearchApplication::setWindowCheck()
{
#ifdef Q_OS_MAC
int i=0;
foreach (MainWindow *window, windows)
{
windowActions[i]->setChecked(window->isActiveWindow());
i++;
}
#endif
//Emitting this signal will cause all MainWindows to update their window menu
//check mark ensuuing it is against the currently focused window
emit currentWindowChanged();
}
void WordSearchApplication::windowSelected(QAction *action)
{
//One of the windows was selected off the dock menu
QMainWindow *selctedWindow = windows.at(action->data().toInt());
if (selctedWindow->windowState().testFlag(Qt::WindowState::WindowMinimized))
{
//Seems the only way to restore a minimised window on macOS is to use showMaximized() or showNormal() but we must know which to use.
if(selctedWindow->windowState().testFlag(Qt::WindowState::WindowMaximized))
{
selctedWindow->showMaximized();
}
else
{
selctedWindow->showNormal();
}
}
else
{
selctedWindow->raise();
selctedWindow->activateWindow();
}
}
void WordSearchApplication::New()
{
MainWindow *window = new MainWindow();
window->show();
}