Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

374 lines
10.0KB

  1. /*
  2. * Carla Bridge Toolkit, Qt version
  3. * Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  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.
  9. *
  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.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #include "CarlaBridgeClient.hpp"
  18. #include "CarlaBridgeToolkit.hpp"
  19. #include "CarlaStyle.hpp"
  20. #include <QtCore/QSettings>
  21. #include <QtCore/QThread>
  22. #include <QtCore/QTimerEvent>
  23. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  24. # include <QtWidgets/QApplication>
  25. # include <QtWidgets/QMainWindow>
  26. # ifdef Q_WS_X11
  27. # undef Q_WS_X11
  28. # endif
  29. #else
  30. # include <QtGui/QApplication>
  31. # include <QtGui/QMainWindow>
  32. # ifdef Q_WS_X11
  33. # include <QtGui/QX11EmbedContainer>
  34. # endif
  35. #endif
  36. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  37. # define BRIDGE_CONTAINER
  38. # ifndef BRIDGE_X11
  39. typedef QWidget QEmbedContainer;
  40. # else
  41. # ifdef Q_WS_X11
  42. typedef QX11EmbedContainer QEmbedContainer;
  43. # else
  44. # warning Using X11 UI bridge without QX11EmbedContainer
  45. typedef QWidget QEmbedContainer;
  46. # endif
  47. # endif
  48. #endif
  49. CARLA_BRIDGE_START_NAMESPACE
  50. // -------------------------------------------------------------------------
  51. #if defined(BRIDGE_QT4)
  52. static const char* const appName = "Carla-Qt4UIs";
  53. #elif defined(BRIDGE_QT5)
  54. static const char* const appName = "Carla-Qt5UIs";
  55. #elif defined(BRIDGE_COCOA)
  56. static const char* const appName = "Carla-CocoaUIs";
  57. #elif defined(BRIDGE_HWND)
  58. static const char* const appName = "Carla-HWNDUIs";
  59. #elif defined(BRIDGE_X11)
  60. static const char* const appName = "Carla-X11UIs";
  61. #else
  62. static const char* const appName = "Carla-UIs";
  63. #endif
  64. static int qargc = 0;
  65. static char* qargv[0] = {};
  66. // -------------------------------------------------------------------------
  67. class CarlaBridgeToolkitQt: public QObject,
  68. public CarlaBridgeToolkit
  69. {
  70. Q_OBJECT
  71. public:
  72. CarlaBridgeToolkitQt(CarlaBridgeClient* const client, const char* const uiTitle)
  73. : QObject(nullptr),
  74. CarlaBridgeToolkit(client, uiTitle),
  75. fApp(nullptr),
  76. fWindow(nullptr),
  77. #ifdef BRIDGE_CONTAINER
  78. fEmbedContainer(nullptr),
  79. #endif
  80. fMsgTimer(0)
  81. {
  82. carla_debug("CarlaBridgeToolkitQt::CarlaBridgeToolkitQt(%p, \"%s\")", client, uiTitle);
  83. connect(this, SIGNAL(setSizeSafeSignal(int,int)), SLOT(setSizeSafeSlot(int,int)));
  84. }
  85. ~CarlaBridgeToolkitQt() override
  86. {
  87. CARLA_ASSERT(fApp == nullptr);
  88. CARLA_ASSERT(fWindow == nullptr);
  89. CARLA_ASSERT(fMsgTimer == 0);
  90. carla_debug("CarlaBridgeToolkitQt::~CarlaBridgeToolkitQt()");
  91. }
  92. void init() override
  93. {
  94. CARLA_ASSERT(fApp == nullptr);
  95. CARLA_ASSERT(fWindow == nullptr);
  96. CARLA_ASSERT(fMsgTimer == 0);
  97. carla_debug("CarlaBridgeToolkitQt::init()");
  98. fApp = new QApplication(qargc, qargv);
  99. {
  100. QSettings settings("falkTX", "Carla");
  101. if (settings.value("Main/UseProTheme", true).toBool())
  102. {
  103. CarlaStyle* const style(new CarlaStyle());
  104. fApp->setStyle(style);
  105. style->ready(fApp);
  106. QString color(settings.value("Main/ProThemeColor", "Black").toString());
  107. if (color == "System")
  108. pass(); //style->setColorScheme(CarlaStyle::COLOR_SYSTEM);
  109. else
  110. style->setColorScheme(CarlaStyle::COLOR_BLACK);
  111. }
  112. }
  113. fWindow = new QMainWindow(nullptr);
  114. fWindow->resize(30, 30);
  115. fWindow->hide();
  116. }
  117. void exec(const bool showGui) override
  118. {
  119. CARLA_ASSERT(kClient != nullptr);
  120. CARLA_ASSERT(fApp != nullptr);
  121. CARLA_ASSERT(fWindow != nullptr);
  122. carla_debug("CarlaBridgeToolkitQt::exec(%s)", bool2str(showGui));
  123. #if defined(BRIDGE_QT4) || defined(BRIDGE_QT5)
  124. QWidget* const widget((QWidget*)kClient->getWidget());
  125. fWindow->setCentralWidget(widget);
  126. fWindow->adjustSize();
  127. widget->setParent(fWindow);
  128. widget->show();
  129. #endif
  130. if (! kClient->isResizable())
  131. {
  132. fWindow->setFixedSize(fWindow->width(), fWindow->height());
  133. #ifdef Q_OS_WIN
  134. fWindow->setWindowFlags(fWindow->windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  135. #endif
  136. }
  137. fWindow->setWindowTitle(kUiTitle);
  138. {
  139. QSettings settings("falkTX", appName);
  140. if (settings.contains(QString("%1/pos_x").arg(kUiTitle)))
  141. {
  142. bool hasX, hasY;
  143. const int posX(settings.value(QString("%1/pos_x").arg(kUiTitle), fWindow->x()).toInt(&hasX));
  144. const int posY(settings.value(QString("%1/pos_y").arg(kUiTitle), fWindow->y()).toInt(&hasY));
  145. if (hasX && hasY)
  146. fWindow->move(posX, posY);
  147. if (kClient->isResizable())
  148. {
  149. bool hasWidth, hasHeight;
  150. const int width(settings.value(QString("%1/width").arg(kUiTitle), fWindow->width()).toInt(&hasWidth));
  151. const int height(settings.value(QString("%1/height").arg(kUiTitle), fWindow->height()).toInt(&hasHeight));
  152. if (hasWidth && hasHeight)
  153. fWindow->resize(width, height);
  154. }
  155. }
  156. if (settings.value("Engine/UIsAlwaysOnTop", true).toBool())
  157. fWindow->setWindowFlags(fWindow->windowFlags() | Qt::WindowStaysOnTopHint);
  158. }
  159. if (showGui)
  160. show();
  161. else
  162. kClient->sendOscUpdate();
  163. fMsgTimer = startTimer(50);
  164. // First idle
  165. handleTimeout();
  166. // Main loop
  167. fApp->exec();
  168. }
  169. void quit() override
  170. {
  171. CARLA_ASSERT(kClient != nullptr);
  172. CARLA_ASSERT(fApp != nullptr);
  173. CARLA_ASSERT(fWindow != nullptr);
  174. carla_debug("CarlaBridgeToolkitQt::quit()");
  175. if (fMsgTimer != 0)
  176. {
  177. killTimer(fMsgTimer);
  178. fMsgTimer = 0;
  179. }
  180. if (fWindow != nullptr)
  181. {
  182. QSettings settings("falkTX", appName);
  183. settings.setValue(QString("%1/pos_x").arg(kUiTitle), fWindow->x());
  184. settings.setValue(QString("%1/pos_y").arg(kUiTitle), fWindow->y());
  185. settings.setValue(QString("%1/width").arg(kUiTitle), fWindow->width());
  186. settings.setValue(QString("%1/height").arg(kUiTitle), fWindow->height());
  187. settings.sync();
  188. fWindow->close();
  189. #ifdef BRIDGE_CONTAINER
  190. if (fEmbedContainer != nullptr)
  191. {
  192. fEmbedContainer->close();
  193. delete fEmbedContainer;
  194. fEmbedContainer = nullptr;
  195. }
  196. #endif
  197. delete fWindow;
  198. fWindow = nullptr;
  199. }
  200. if (fApp != nullptr)
  201. {
  202. if (! fApp->closingDown())
  203. fApp->quit();
  204. delete fApp;
  205. fApp = nullptr;
  206. }
  207. }
  208. void show() override
  209. {
  210. CARLA_ASSERT(fWindow != nullptr);
  211. carla_debug("CarlaBridgeToolkitQt::show()");
  212. if (fWindow != nullptr)
  213. fWindow->show();
  214. }
  215. void hide() override
  216. {
  217. CARLA_ASSERT(fWindow != nullptr);
  218. carla_debug("CarlaBridgeToolkitQt::hide()");
  219. if (fWindow != nullptr)
  220. fWindow->hide();
  221. }
  222. void resize(const int width, const int height) override
  223. {
  224. CARLA_ASSERT_INT(width > 0, width);
  225. CARLA_ASSERT_INT(height > 0, height);
  226. carla_debug("CarlaBridgeToolkitQt::resize(%i, %i)", width, height);
  227. if (width <= 0)
  228. return;
  229. if (height <= 0)
  230. return;
  231. emit setSizeSafeSignal(width, height);
  232. }
  233. #ifdef BRIDGE_CONTAINER
  234. void* getContainerId()
  235. {
  236. CARLA_ASSERT(fWindow != nullptr);
  237. carla_debug("CarlaBridgeToolkitQt::getContainerId()");
  238. if (fEmbedContainer == nullptr)
  239. {
  240. fEmbedContainer = new QEmbedContainer(fWindow);
  241. fWindow->setCentralWidget(fEmbedContainer);
  242. fWindow->adjustSize();
  243. fEmbedContainer->setParent(fWindow);
  244. fEmbedContainer->show();
  245. }
  246. return (void*)fEmbedContainer->winId();
  247. }
  248. #endif
  249. protected:
  250. QApplication* fApp;
  251. QMainWindow* fWindow;
  252. #ifdef BRIDGE_CONTAINER
  253. QEmbedContainer* fEmbedContainer;
  254. #endif
  255. int fMsgTimer;
  256. void handleTimeout()
  257. {
  258. if (kClient == nullptr)
  259. return;
  260. kClient->uiIdle();
  261. if (! kClient->oscIdle())
  262. {
  263. killTimer(fMsgTimer);
  264. fMsgTimer = 0;
  265. }
  266. }
  267. private:
  268. void timerEvent(QTimerEvent* const event)
  269. {
  270. if (event->timerId() == fMsgTimer)
  271. handleTimeout();
  272. QObject::timerEvent(event);
  273. }
  274. signals:
  275. void setSizeSafeSignal(int, int);
  276. private slots:
  277. void setSizeSafeSlot(int width, int height)
  278. {
  279. CARLA_ASSERT(kClient != nullptr && ! kClient->isResizable());
  280. CARLA_ASSERT(fWindow != nullptr);
  281. if (kClient == nullptr || fWindow == nullptr)
  282. return;
  283. if (kClient->isResizable())
  284. fWindow->resize(width, height);
  285. else
  286. fWindow->setFixedSize(width, height);
  287. #ifdef BRIDGE_CONTAINER
  288. if (fEmbedContainer != nullptr)
  289. fEmbedContainer->setFixedSize(width, height);
  290. #endif
  291. }
  292. };
  293. #include "CarlaBridgeToolkitQt.moc"
  294. // -------------------------------------------------------------------------
  295. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeClient* const client, const char* const uiTitle)
  296. {
  297. return new CarlaBridgeToolkitQt(client, uiTitle);
  298. }
  299. CARLA_BRIDGE_END_NAMESPACE