/
QcApplication.cpp
193 lines (162 loc) · 5.74 KB
/
QcApplication.cpp
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
/************************************************************************
*
* Copyright 2010 Jakob Leben (jakob.leben@gmail.com)
*
* This file is part of SuperCollider Qt GUI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
************************************************************************/
#include "QcApplication.h"
#include "widgets/QcTreeWidget.h"
#include <PyrLexer.h>
#include <VMGlobals.h>
#include <PyrKernel.h>
#include <PyrSlot.h>
#include <GC.h>
#include "SC_Filesystem.hpp"
#include <QThread>
#include <QFileOpenEvent>
#include <QKeyEvent>
#include <QIcon>
#include <QMenuBar>
#include <QSharedPointer>
#include "hacks/hacks_mac.hpp"
#ifdef Q_OS_MAC
# include "../../common/SC_Apple.hpp"
#endif
extern bool compiledOK;
QcApplication* QcApplication::_instance = 0;
QMutex QcApplication::_mutex;
#ifdef Q_WS_X11
# include <X11/Xlib.h>
#endif
/* on x11, we need to check, if we can actually connect to the X server */
static bool QtColliderUseGui(void) {
#ifdef Q_WS_X11
Display* dpy = XOpenDisplay(NULL);
if (!dpy)
return false;
XCloseDisplay(dpy);
return true;
#else
return true;
#endif
}
// undefine some interfering X11 definitions
#undef KeyPress
bool QcApplication::_systemHasMouseWheel = false;
QcApplication::QcApplication(int& argc, char** argv): QApplication(argc, argv, QtColliderUseGui()) {
_mutex.lock();
_instance = this;
_mutex.unlock();
this->setAttribute(Qt::AA_UseHighDpiPixmaps);
#ifdef Q_OS_MAC
QtCollider::Mac::DisableAutomaticWindowTabbing();
#endif
if (QtColliderUseGui()) { // avoid a crash on linux, if x is not available
QIcon icon;
icon.addFile(":/icons/sc-cube-128");
icon.addFile(":/icons/sc-cube-48");
icon.addFile(":/icons/sc-cube-32");
icon.addFile(":/icons/sc-cube-16");
setWindowIcon(icon);
createMenu();
}
#ifdef Q_OS_MAC
// On Mac, we may need to disable "App Nap", so we aren't put to sleep unexpectedly
SC::Apple::disableAppNap();
#endif
_handleCmdPeriod = SC_Filesystem::instance().getIdeName() != "scapp";
}
QcApplication::~QcApplication() {
_mutex.lock();
_instance = 0;
_mutex.unlock();
}
void QcApplication::createMenu() {
_mainMenu = QSharedPointer<QMenuBar>::create();
#ifdef Q_OS_MAC
// macOS registers cmd+q on menu bars by default. Here we register a handler for cmd+q that does nothing
// and we disable the Quit menu by default
auto* action = new QAction("");
action->setMenuRole(QAction::QuitRole);
action->setEnabled(false);
QObject::connect(action, SIGNAL(triggered()), this, SLOT(onQuit()));
auto* menu = new QMenu(tr("&File"));
menu->addAction(action);
_mainMenu->addMenu(menu);
#endif
}
void QcApplication::onQuit() {
qWarning("[QcApplication::onQuit] CMD+Q was caught by the interpreter. "
"This is weird, it should not happen. "
"Please file an issue at https://github.com/supercollider/supercollider/issues");
}
bool QcApplication::compareThread() { return gMainVMGlobals->canCallOS; }
void QcApplication::interpret(const QString& str, bool print) {
QtCollider::lockLang();
if (compiledOK) {
VMGlobals* g = gMainVMGlobals;
PyrString* strObj = newPyrString(g->gc, str.toStdString().c_str(), 0, true);
SetObject(&slotRawInterpreter(&g->process->interpreter)->cmdLine, strObj);
g->gc->GCWriteNew(slotRawObject(&g->process->interpreter),
strObj); // we know strObj is white so we can use GCWriteNew
runLibrary(print ? SC_SYM(interpretPrintCmdLine) : SC_SYM(interpretCmdLine));
}
QtCollider::unlockLang();
}
bool QcApplication::event(QEvent* event) {
switch (event->type()) {
case QEvent::FileOpen: {
// open the file dragged onto the application icon on Mac
QFileOpenEvent* fe = static_cast<QFileOpenEvent*>(event);
interpret(QStringLiteral("Document.open(\"%1\")").arg(fe->file()), false);
event->accept();
return true;
}
default:
break;
}
return QApplication::event(event);
}
bool QcApplication::notify(QObject* object, QEvent* event) {
switch (event->type()) {
case QEvent::KeyPress: {
QKeyEvent* kevent = static_cast<QKeyEvent*>(event);
if (_handleCmdPeriod && (kevent->key() == Qt::Key_Period) && (kevent->modifiers() & Qt::ControlModifier)) {
static QString cmdPeriodCommand("CmdPeriod.run");
interpret(cmdPeriodCommand, false);
}
break;
}
case QEvent::Wheel: {
_systemHasMouseWheel = true;
break;
}
default:
break;
}
bool result = QApplication::notify(object, event);
#ifdef Q_OS_MAC
// XXX Explicitly accept all handled events so they don't propagate outside the application.
// This is a hack; for a not-fully-understood reason Qt past 5.7 sends these events to the
// native window if they aren't accepted here. This caused issue #4058. Accepting them here
// seems to solve the problem, but might cause other issues since it is a heavy-handed way
// of doing this. TODO - solve more elegantly
if (result)
event->accept();
#endif
return result;
}