Cleanup
[carla.git] / source / bridges-ui / CarlaBridgeToolkitQt.cpp
blob442d16c5aee1c25db1370ae6fc7d672cc9c8fe14
1 /*
2 * Carla Bridge UI
3 * Copyright (C) 2011-2019 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaBridgeFormat.hpp"
19 #include "CarlaBridgeToolkit.hpp"
21 #include "CarlaStyle.hpp"
23 #include <QtCore/QTimerEvent>
25 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
26 # ifdef BRIDGE_QT4
27 # error Wrong includes for this bridge!
28 # endif
29 # include <QtWidgets/QApplication>
30 # include <QtWidgets/QMainWindow>
31 #else
32 # ifdef BRIDGE_QT5
33 # error Wrong includes for this bridge!
34 # endif
35 # include <QtGui/QApplication>
36 # include <QtGui/QMainWindow>
37 # ifdef HAVE_X11
38 # define USE_CUSTOM_X11_METHODS
39 # include <QtGui/QX11Info>
40 # include <X11/Xlib.h>
41 # endif
42 #endif
44 CARLA_BRIDGE_UI_START_NAMESPACE
46 // -------------------------------------------------------------------------
48 static int qargc = 0;
49 static char* qargv[0] = {};
51 // -------------------------------------------------------------------------
53 class CarlaBridgeToolkitQt: public QObject,
54 public CarlaBridgeToolkit
56 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) && defined(__clang_major__) && __clang_major__ >= 4
57 # pragma clang diagnostic push
58 # pragma clang diagnostic ignored "-Winconsistent-missing-override"
59 #endif
60 Q_OBJECT
61 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) && defined(__clang_major__) && __clang_major__ >= 4
62 # pragma clang diagnostic pop
63 #endif
65 public:
66 CarlaBridgeToolkitQt(CarlaBridgeFormat* const format)
67 : QObject(nullptr),
68 CarlaBridgeToolkit(format),
69 fApp(nullptr),
70 fWindow(nullptr),
71 fMsgTimer(0),
72 fNeedsShow(false)
74 carla_debug("CarlaBridgeToolkitQt::CarlaBridgeToolkitQt(%p)", format);
77 ~CarlaBridgeToolkitQt() override
79 CARLA_SAFE_ASSERT(fApp == nullptr);
80 CARLA_SAFE_ASSERT(fWindow == nullptr);
81 CARLA_SAFE_ASSERT(fMsgTimer == 0);
82 carla_debug("CarlaBridgeToolkitQt::~CarlaBridgeToolkitQt()");
85 bool init(const int /*argc*/, const char** /*argv[]*/) override
87 CARLA_SAFE_ASSERT_RETURN(fApp == nullptr, false);
88 CARLA_SAFE_ASSERT_RETURN(fWindow == nullptr, false);
89 CARLA_SAFE_ASSERT_RETURN(fMsgTimer == 0, false);
90 carla_debug("CarlaBridgeToolkitQt::init()");
92 fApp = new QApplication(qargc, qargv);
94 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
95 fWindow = new QMainWindow(nullptr);
96 #else
97 fWindow = new QMainWindow(nullptr, nullptr);
98 #endif
99 fWindow->resize(30, 30);
100 fWindow->hide();
102 return true;
105 void exec(const bool showUI) override
107 CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
108 CARLA_SAFE_ASSERT_RETURN(fApp != nullptr,);
109 CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
110 carla_debug("CarlaBridgeToolkitQt::exec(%s)", bool2str(showUI));
112 const CarlaBridgeFormat::Options& options(fPlugin->getOptions());
114 QWidget* const widget((QWidget*)fPlugin->getWidget());
116 fWindow->setCentralWidget(widget);
117 fWindow->adjustSize();
119 widget->setParent(fWindow);
120 widget->show();
122 if (! options.isResizable)
124 fWindow->setFixedSize(fWindow->width(), fWindow->height());
125 #ifdef CARLA_OS_WIN
126 fWindow->setWindowFlags(fWindow->windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
127 #endif
130 fWindow->setWindowIcon(QIcon::fromTheme("carla", QIcon(":/scalable/carla.svg")));
131 fWindow->setWindowTitle(options.windowTitle.buffer());
133 #ifdef USE_CUSTOM_X11_METHODS
134 if (options.transientWindowId != 0)
136 XSetTransientForHint(QX11Info::display(),
137 static_cast< ::Window>(fWindow->winId()),
138 static_cast< ::Window>(options.transientWindowId));
140 #endif
142 if (showUI || fNeedsShow)
144 show();
145 fNeedsShow = false;
148 fMsgTimer = startTimer(30);
150 // First idle
151 handleTimeout();
153 // Main loop
154 fApp->exec();
157 void quit() override
159 CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
160 CARLA_SAFE_ASSERT_RETURN(fApp != nullptr,);
161 CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
162 carla_debug("CarlaBridgeToolkitQt::quit()");
164 if (fMsgTimer != 0)
166 killTimer(fMsgTimer);
167 fMsgTimer = 0;
170 if (fWindow != nullptr)
172 fWindow->close();
174 delete fWindow;
175 fWindow = nullptr;
178 if (fApp != nullptr)
180 if (! fApp->closingDown())
181 fApp->quit();
183 delete fApp;
184 fApp = nullptr;
188 void show() override
190 carla_debug("CarlaBridgeToolkitQt::show()");
192 fNeedsShow = true;
194 if (fWindow != nullptr)
195 fWindow->show();
198 void focus() override
200 carla_debug("CarlaBridgeToolkitQt::focus()");
203 void hide() override
205 carla_debug("CarlaBridgeToolkitQt::hide()");
207 fNeedsShow = false;
209 if (fWindow != nullptr)
210 fWindow->hide();
213 void setChildWindow(void* const) override {}
215 void setSize(const uint width, const uint height) override
217 CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
218 CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
219 CARLA_SAFE_ASSERT_RETURN(width > 0,);
220 CARLA_SAFE_ASSERT_RETURN(height > 0,);
221 carla_debug("CarlaBridgeToolkitQt::resize(%i, %i)", width, height);
223 if (fPlugin->getOptions().isResizable)
224 fWindow->resize(static_cast<int>(width), static_cast<int>(height));
225 else
226 fWindow->setFixedSize(static_cast<int>(width), static_cast<int>(height));
229 void setTitle(const char* const title) override
231 CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
232 CARLA_SAFE_ASSERT_RETURN(title != nullptr && title[0] != '\0',);
233 carla_debug("CarlaBridgeToolkitQt::setTitle(\"%s\")", title);
235 fWindow->setWindowTitle(title);
238 protected:
239 QApplication* fApp;
240 QMainWindow* fWindow;
242 int fMsgTimer;
243 bool fNeedsShow;
245 void handleTimeout()
247 CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
249 if (fPlugin->isPipeRunning())
250 fPlugin->idlePipe();
252 fPlugin->idleUI();
255 private:
256 void timerEvent(QTimerEvent* const ev) override
258 if (ev->timerId() == fMsgTimer)
259 handleTimeout();
261 QObject::timerEvent(ev);
264 #ifndef MOC_PARSING
265 CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgeToolkitQt)
266 #endif
269 // -------------------------------------------------------------------------
271 CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeFormat* const format)
273 return new CarlaBridgeToolkitQt(format);
276 // -------------------------------------------------------------------------
278 CARLA_BRIDGE_UI_END_NAMESPACE
280 // -------------------------------------------------------------------------
282 CARLA_BRIDGE_UI_USE_NAMESPACE
284 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
285 # pragma GCC diagnostic push
286 # pragma GCC diagnostic ignored "-Wmissing-declarations"
287 # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
288 #endif
290 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
291 # include "CarlaBridgeToolkitQt5.moc"
292 # include "resources.qt5.cpp"
293 #else
294 # include "CarlaBridgeToolkitQt4.moc"
295 # include "resources.qt4.cpp"
296 #endif
298 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
299 # pragma GCC diagnostic pop
300 #endif
302 // -------------------------------------------------------------------------