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.

357 lines
9.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. # ifdef Q_WS_X11
  39. typedef QX11EmbedContainer QEmbedContainer;
  40. # else
  41. typedef QWidget QEmbedContainer;
  42. # endif
  43. #endif
  44. CARLA_BRIDGE_START_NAMESPACE
  45. // -------------------------------------------------------------------------
  46. #if defined(BRIDGE_QT4)
  47. static const char* const appName = "Carla-Qt4UIs";
  48. #elif defined(BRIDGE_QT5)
  49. static const char* const appName = "Carla-Qt5UIs";
  50. #elif defined(BRIDGE_COCOA)
  51. static const char* const appName = "Carla-CocoaUIs";
  52. #elif defined(BRIDGE_HWND)
  53. static const char* const appName = "Carla-HWNDUIs";
  54. #elif defined(BRIDGE_X11)
  55. static const char* const appName = "Carla-X11UIs";
  56. #else
  57. static const char* const appName = "Carla-UIs";
  58. #endif
  59. static int qargc = 0;
  60. static char* qargv[0] = {};
  61. // -------------------------------------------------------------------------
  62. class CarlaBridgeToolkitQt: public CarlaBridgeToolkit,
  63. public QObject
  64. {
  65. public:
  66. CarlaBridgeToolkitQt(CarlaBridgeClient* const client, const char* const uiTitle)
  67. : CarlaBridgeToolkit(client, uiTitle),
  68. QObject(nullptr),
  69. settings("falkTX", appName)
  70. {
  71. carla_debug("CarlaBridgeToolkitQt::CarlaBridgeToolkitQt(%p, \"%s\")", client, uiTitle);
  72. app = nullptr;
  73. window = nullptr;
  74. msgTimer = 0;
  75. needsResize = false;
  76. nextWidth = 0;
  77. nextHeight = 0;
  78. #ifdef BRIDGE_CONTAINER
  79. embedContainer = nullptr;
  80. #endif
  81. }
  82. ~CarlaBridgeToolkitQt() override
  83. {
  84. carla_debug("CarlaBridgeToolkitQt::~CarlaBridgeToolkitQt()");
  85. CARLA_ASSERT(! app);
  86. CARLA_ASSERT(! window);
  87. CARLA_ASSERT(! msgTimer);
  88. }
  89. void init() override
  90. {
  91. carla_debug("CarlaBridgeToolkitQt::init()");
  92. CARLA_ASSERT(! app);
  93. CARLA_ASSERT(! window);
  94. CARLA_ASSERT(! msgTimer);
  95. app = new QApplication(qargc, qargv);
  96. {
  97. QSettings settings("falkTX", "Carla");
  98. if (settings.value("Main/UseProTheme", true).toBool())
  99. {
  100. CarlaStyle* const style(new CarlaStyle());
  101. app->setStyle(style);
  102. style->ready(app);
  103. QString color(settings.value("Main/ProThemeColor", "Black").toString());
  104. if (color == "System")
  105. pass(); //style->setColorScheme(CarlaStyle::COLOR_SYSTEM);
  106. else
  107. style->setColorScheme(CarlaStyle::COLOR_BLACK);
  108. }
  109. }
  110. window = new QMainWindow(nullptr);
  111. window->resize(30, 30);
  112. window->hide();
  113. }
  114. void exec(const bool showGui) override
  115. {
  116. carla_debug("CarlaBridgeToolkitQt::exec(%s)", bool2str(showGui));
  117. CARLA_ASSERT(app);
  118. CARLA_ASSERT(window);
  119. CARLA_ASSERT(kClient);
  120. #if defined(BRIDGE_QT4) || defined(BRIDGE_QT5)
  121. QWidget* const widget = (QWidget*)kClient->getWidget();
  122. window->setCentralWidget(widget);
  123. window->adjustSize();
  124. widget->setParent(window);
  125. widget->show();
  126. #endif
  127. if (! kClient->isResizable())
  128. {
  129. window->setFixedSize(window->width(), window->height());
  130. #ifdef Q_OS_WIN
  131. window->setWindowFlags(window->windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  132. #endif
  133. }
  134. window->setWindowTitle(kUiTitle);
  135. if (settings.contains(QString("%1/pos_x").arg(kUiTitle)))
  136. {
  137. bool hasX, hasY;
  138. int posX = settings.value(QString("%1/pos_x").arg(kUiTitle), window->x()).toInt(&hasX);
  139. int posY = settings.value(QString("%1/pos_y").arg(kUiTitle), window->y()).toInt(&hasY);
  140. if (hasX && hasY)
  141. window->move(posX, posY);
  142. if (kClient->isResizable())
  143. {
  144. bool hasWidth, hasHeight;
  145. int width = settings.value(QString("%1/width").arg(kUiTitle), window->width()).toInt(&hasWidth);
  146. int height = settings.value(QString("%1/height").arg(kUiTitle), window->height()).toInt(&hasHeight);
  147. if (hasWidth && hasHeight)
  148. window->resize(width, height);
  149. }
  150. }
  151. if (showGui)
  152. show();
  153. else
  154. kClient->sendOscUpdate();
  155. // Timer
  156. msgTimer = startTimer(50);
  157. // First idle
  158. handleTimeout();
  159. // Main loop
  160. app->exec();
  161. }
  162. void quit() override
  163. {
  164. carla_debug("CarlaBridgeToolkitQt::quit()");
  165. CARLA_ASSERT(app);
  166. if (msgTimer != 0)
  167. {
  168. killTimer(msgTimer);
  169. msgTimer = 0;
  170. }
  171. if (window != nullptr)
  172. {
  173. settings.setValue(QString("%1/pos_x").arg(kUiTitle), window->x());
  174. settings.setValue(QString("%1/pos_y").arg(kUiTitle), window->y());
  175. settings.setValue(QString("%1/width").arg(kUiTitle), window->width());
  176. settings.setValue(QString("%1/height").arg(kUiTitle), window->height());
  177. settings.sync();
  178. window->close();
  179. }
  180. #ifdef BRIDGE_CONTAINER
  181. if (embedContainer != nullptr)
  182. {
  183. embedContainer->close();
  184. delete embedContainer;
  185. embedContainer = nullptr;
  186. }
  187. #endif
  188. if (window != nullptr)
  189. {
  190. delete window;
  191. window = nullptr;
  192. }
  193. if (app)
  194. {
  195. if (! app->closingDown())
  196. app->quit();
  197. delete app;
  198. app = nullptr;
  199. }
  200. }
  201. void show() override
  202. {
  203. carla_debug("CarlaBridgeToolkitQt::show()");
  204. CARLA_ASSERT(window);
  205. if (window)
  206. window->show();
  207. }
  208. void hide() override
  209. {
  210. carla_debug("CarlaBridgeToolkitQt::hide()");
  211. CARLA_ASSERT(window);
  212. if (window)
  213. window->hide();
  214. }
  215. void resize(const int width, const int height) override
  216. {
  217. carla_debug("CarlaBridgeToolkitQt::resize(%i, %i)", width, height);
  218. CARLA_ASSERT(window);
  219. if (app->thread() != QThread::currentThread())
  220. {
  221. nextWidth = width;
  222. nextHeight = height;
  223. needsResize = true;
  224. return;
  225. }
  226. if (window)
  227. window->setFixedSize(width, height);
  228. #ifdef BRIDGE_CONTAINER
  229. if (embedContainer)
  230. embedContainer->setFixedSize(width, height);
  231. #endif
  232. }
  233. #ifdef BRIDGE_CONTAINER
  234. void* getContainerId()
  235. {
  236. carla_debug("CarlaBridgeToolkitQt::getContainerId()");
  237. CARLA_ASSERT(window != nullptr);
  238. if (embedContainer == nullptr)
  239. {
  240. embedContainer = new QEmbedContainer(window);
  241. window->setCentralWidget(embedContainer);
  242. window->adjustSize();
  243. embedContainer->setParent(window);
  244. embedContainer->show();
  245. }
  246. return (void*)embedContainer->winId();
  247. }
  248. #endif
  249. protected:
  250. QApplication* app;
  251. QMainWindow* window;
  252. QSettings settings;
  253. int msgTimer;
  254. bool needsResize;
  255. int nextWidth, nextHeight;
  256. #ifdef BRIDGE_CONTAINER
  257. QEmbedContainer* embedContainer;
  258. #endif
  259. void handleTimeout()
  260. {
  261. if (! kClient)
  262. return;
  263. if (needsResize)
  264. {
  265. kClient->toolkitResize(nextWidth, nextHeight);
  266. needsResize = false;
  267. }
  268. if (kClient->isOscControlRegistered() && ! kClient->oscIdle())
  269. {
  270. killTimer(msgTimer);
  271. msgTimer = 0;
  272. }
  273. }
  274. private:
  275. void timerEvent(QTimerEvent* const event)
  276. {
  277. if (event->timerId() == msgTimer)
  278. handleTimeout();
  279. QObject::timerEvent(event);
  280. }
  281. };
  282. // -------------------------------------------------------------------------
  283. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeClient* const client, const char* const uiTitle)
  284. {
  285. return new CarlaBridgeToolkitQt(client, uiTitle);
  286. }
  287. CARLA_BRIDGE_END_NAMESPACE