Collection of tools useful for audio production
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.

334 lines
8.2KB

  1. /*
  2. * Carla Bridge Toolkit, Qt version
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #include "carla_bridge_client.hpp"
  18. #include "carla_bridge_toolkit.hpp"
  19. #include <QtCore/QSettings>
  20. #include <QtCore/QThread>
  21. #include <QtCore/QTimerEvent>
  22. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  23. # include <QtWidgets/QApplication>
  24. # include <QtWidgets/QMainWindow>
  25. # ifdef Q_WS_X11
  26. # undef Q_WS_X11
  27. # endif
  28. #else
  29. # include <QtGui/QApplication>
  30. # include <QtGui/QMainWindow>
  31. # ifdef Q_WS_X11
  32. # include <QtGui/QX11EmbedContainer>
  33. # endif
  34. #endif
  35. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  36. # define BRIDGE_CONTAINER
  37. # ifdef Q_WS_X11
  38. typedef QX11EmbedContainer QEmbedContainer;
  39. # else
  40. typedef QWidget QEmbedContainer;
  41. # endif
  42. #endif
  43. CARLA_BRIDGE_START_NAMESPACE
  44. // -------------------------------------------------------------------------
  45. #if defined(BRIDGE_QT4)
  46. static const char* const appName = "Carla-Qt4UIs";
  47. #elif defined(BRIDGE_QT5)
  48. static const char* const appName = "Carla-Qt5UIs";
  49. #elif defined(BRIDGE_COCOA)
  50. static const char* const appName = "Carla-CocoaUIs";
  51. #elif defined(BRIDGE_HWND)
  52. static const char* const appName = "Carla-HWNDUIs";
  53. #elif defined(BRIDGE_X11)
  54. static const char* const appName = "Carla-X11UIs";
  55. #else
  56. static const char* const appName = "Carla-UIs";
  57. #endif
  58. static int qargc = 0;
  59. static char* qargv[0] = {};
  60. // -------------------------------------------------------------------------
  61. class CarlaBridgeToolkitQt: public CarlaBridgeToolkit,
  62. public QObject
  63. {
  64. public:
  65. CarlaBridgeToolkitQt(CarlaBridgeClient* const client, const char* const uiTitle)
  66. : CarlaBridgeToolkit(client, uiTitle),
  67. QObject(nullptr),
  68. settings("Cadence", appName)
  69. {
  70. qDebug("CarlaBridgeToolkitQt::CarlaBridgeToolkitQt(%p, \"%s\")", client, uiTitle);
  71. app = nullptr;
  72. window = nullptr;
  73. msgTimer = 0;
  74. needsResize = false;
  75. nextWidth = 0;
  76. nextHeight = 0;
  77. #ifdef BRIDGE_CONTAINER
  78. embedContainer = nullptr;
  79. #endif
  80. }
  81. ~CarlaBridgeToolkitQt()
  82. {
  83. qDebug("CarlaBridgeToolkitQt::~CarlaBridgeToolkitQt()");
  84. CARLA_ASSERT(! app);
  85. CARLA_ASSERT(! msgTimer);
  86. }
  87. void init()
  88. {
  89. qDebug("CarlaBridgeToolkitQt::init()");
  90. CARLA_ASSERT(! app);
  91. CARLA_ASSERT(! window);
  92. CARLA_ASSERT(! msgTimer);
  93. app = new QApplication(qargc, qargv);
  94. window = new QMainWindow(nullptr);
  95. window->resize(30, 30);
  96. window->hide();
  97. }
  98. void exec(const bool showGui)
  99. {
  100. qDebug("CarlaBridgeToolkitQt::exec(%s)", bool2str(showGui));
  101. CARLA_ASSERT(app);
  102. CARLA_ASSERT(window);
  103. CARLA_ASSERT(client);
  104. #if defined(BRIDGE_QT4) || defined(BRIDGE_QT5)
  105. QWidget* const widget = (QWidget*)client->getWidget();
  106. window->setCentralWidget(widget);
  107. window->adjustSize();
  108. widget->setParent(window);
  109. widget->show();
  110. #endif
  111. if (! client->isResizable())
  112. {
  113. window->setFixedSize(window->width(), window->height());
  114. #ifdef Q_OS_WIN
  115. window->setWindowFlags(window->windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  116. #endif
  117. }
  118. window->setWindowTitle(uiTitle);
  119. if (settings.contains(QString("%1/pos_x").arg(uiTitle)))
  120. {
  121. bool hasX, hasY;
  122. int posX = settings.value(QString("%1/pos_x").arg(uiTitle), window->x()).toInt(&hasX);
  123. int posY = settings.value(QString("%1/pos_y").arg(uiTitle), window->y()).toInt(&hasY);
  124. if (hasX && hasY)
  125. window->move(posX, posY);
  126. if (client->isResizable())
  127. {
  128. bool hasWidth, hasHeight;
  129. int width = settings.value(QString("%1/width").arg(uiTitle), window->width()).toInt(&hasWidth);
  130. int height = settings.value(QString("%1/height").arg(uiTitle), window->height()).toInt(&hasHeight);
  131. if (hasWidth && hasHeight)
  132. window->resize(width, height);
  133. }
  134. }
  135. if (showGui)
  136. show();
  137. else
  138. client->sendOscUpdate();
  139. // Timer
  140. msgTimer = startTimer(50);
  141. // First idle
  142. handleTimeout();
  143. // Main loop
  144. app->exec();
  145. }
  146. void quit()
  147. {
  148. qDebug("CarlaBridgeToolkitQt::quit()");
  149. CARLA_ASSERT(app);
  150. if (msgTimer != 0)
  151. {
  152. killTimer(msgTimer);
  153. msgTimer = 0;
  154. }
  155. if (window)
  156. {
  157. settings.setValue(QString("%1/pos_x").arg(uiTitle), window->x());
  158. settings.setValue(QString("%1/pos_y").arg(uiTitle), window->y());
  159. settings.setValue(QString("%1/width").arg(uiTitle), window->width());
  160. settings.setValue(QString("%1/height").arg(uiTitle), window->height());
  161. settings.sync();
  162. window->close();
  163. delete window;
  164. window = nullptr;
  165. }
  166. #ifdef BRIDGE_CONTAINER
  167. if (embedContainer)
  168. {
  169. embedContainer->close();
  170. delete embedContainer;
  171. embedContainer = nullptr;
  172. }
  173. #endif
  174. if (app)
  175. {
  176. if (! app->closingDown())
  177. app->quit();
  178. delete app;
  179. app = nullptr;
  180. }
  181. }
  182. void show()
  183. {
  184. qDebug("CarlaBridgeToolkitQt::show()");
  185. CARLA_ASSERT(window);
  186. if (window)
  187. window->show();
  188. }
  189. void hide()
  190. {
  191. qDebug("CarlaBridgeToolkitQt::hide()");
  192. CARLA_ASSERT(window);
  193. if (window)
  194. window->hide();
  195. }
  196. void resize(const int width, const int height)
  197. {
  198. qDebug("CarlaBridgeToolkitQt::resize(%i, %i)", width, height);
  199. CARLA_ASSERT(window);
  200. if (app->thread() != QThread::currentThread())
  201. {
  202. nextWidth = width;
  203. nextHeight = height;
  204. needsResize = true;
  205. return;
  206. }
  207. if (window)
  208. window->setFixedSize(width, height);
  209. #ifdef BRIDGE_CONTAINER
  210. if (embedContainer)
  211. embedContainer->setFixedSize(width, height);
  212. #endif
  213. }
  214. #ifdef BRIDGE_CONTAINER
  215. void* getContainerId()
  216. {
  217. qDebug("CarlaBridgeToolkitQt::getContainerId()");
  218. CARLA_ASSERT(window);
  219. if (! embedContainer)
  220. {
  221. embedContainer = new QEmbedContainer(window);
  222. window->setCentralWidget(embedContainer);
  223. window->adjustSize();
  224. embedContainer->setParent(window);
  225. embedContainer->show();
  226. }
  227. return (void*)embedContainer->winId();
  228. }
  229. #endif
  230. protected:
  231. QApplication* app;
  232. QMainWindow* window;
  233. QSettings settings;
  234. int msgTimer;
  235. bool needsResize;
  236. int nextWidth, nextHeight;
  237. #ifdef BRIDGE_CONTAINER
  238. QEmbedContainer* embedContainer;
  239. #endif
  240. void handleTimeout()
  241. {
  242. if (! client)
  243. return;
  244. if (needsResize)
  245. {
  246. client->toolkitResize(nextWidth, nextHeight);
  247. needsResize = false;
  248. }
  249. if (client->isOscControlRegistered() && ! client->oscIdle())
  250. {
  251. killTimer(msgTimer);
  252. msgTimer = 0;
  253. }
  254. }
  255. private:
  256. void timerEvent(QTimerEvent* const event)
  257. {
  258. if (event->timerId() == msgTimer)
  259. handleTimeout();
  260. QObject::timerEvent(event);
  261. }
  262. };
  263. // -------------------------------------------------------------------------
  264. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeClient* const client, const char* const uiTitle)
  265. {
  266. return new CarlaBridgeToolkitQt(client, uiTitle);
  267. }
  268. CARLA_BRIDGE_END_NAMESPACE