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.

332 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 title)
  66. : CarlaBridgeToolkit(client, title),
  67. QObject(nullptr),
  68. settings("Cadence", appName)
  69. {
  70. qDebug("CarlaBridgeToolkitQt::CarlaBridgeToolkitQt(\"%s\")", title);
  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. if (window)
  87. {
  88. window->close();
  89. delete window;
  90. }
  91. #ifdef BRIDGE_CONTAINER
  92. // TESTING
  93. if (embedContainer)
  94. {
  95. embedContainer->close();
  96. delete embedContainer;
  97. }
  98. #endif
  99. }
  100. void init()
  101. {
  102. qDebug("CarlaBridgeToolkitQt::init()");
  103. CARLA_ASSERT(! app);
  104. CARLA_ASSERT(! window);
  105. CARLA_ASSERT(! msgTimer);
  106. app = new QApplication(qargc, qargv);
  107. window = new QMainWindow(nullptr);
  108. window->resize(30, 30);
  109. window->hide();
  110. }
  111. void exec(const bool showGui)
  112. {
  113. qDebug("CarlaBridgeToolkitQt::exec(%s)", bool2str(showGui));
  114. CARLA_ASSERT(app);
  115. CARLA_ASSERT(window);
  116. CARLA_ASSERT(client);
  117. #if defined(BRIDGE_QT4) || defined(BRIDGE_QT5)
  118. QWidget* const widget = (QWidget*)client->getWidget();
  119. window->setCentralWidget(widget);
  120. window->adjustSize();
  121. widget->setParent(window);
  122. widget->show();
  123. #endif
  124. if (! client->isResizable())
  125. {
  126. window->setFixedSize(window->width(), window->height());
  127. #ifdef Q_OS_WIN
  128. window->setWindowFlags(window->windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  129. #endif
  130. }
  131. window->setWindowTitle(title);
  132. if (settings.contains(QString("%1/pos_x").arg(title)))
  133. {
  134. bool hasX, hasY;
  135. int posX = settings.value(QString("%1/pos_x").arg(title), window->x()).toInt(&hasX);
  136. int posY = settings.value(QString("%1/pos_y").arg(title), window->y()).toInt(&hasY);
  137. if (hasX && hasY)
  138. window->move(posX, posY);
  139. if (client->isResizable())
  140. {
  141. bool hasWidth, hasHeight;
  142. int width = settings.value(QString("%1/width").arg(title), window->width()).toInt(&hasWidth);
  143. int height = settings.value(QString("%1/height").arg(title), window->height()).toInt(&hasHeight);
  144. if (hasWidth && hasHeight)
  145. window->resize(width, height);
  146. }
  147. }
  148. if (showGui)
  149. show();
  150. else
  151. client->sendOscUpdate();
  152. // Timer
  153. msgTimer = startTimer(50);
  154. // Main loop
  155. app->exec();
  156. }
  157. void quit()
  158. {
  159. qDebug("CarlaBridgeToolkitQt::quit()");
  160. CARLA_ASSERT(app);
  161. if (msgTimer != 0)
  162. {
  163. killTimer(msgTimer);
  164. msgTimer = 0;
  165. }
  166. if (window)
  167. {
  168. if (client)
  169. {
  170. settings.setValue(QString("%1/pos_x").arg(title), window->x());
  171. settings.setValue(QString("%1/pos_y").arg(title), window->y());
  172. settings.setValue(QString("%1/width").arg(title), window->width());
  173. settings.setValue(QString("%1/height").arg(title), window->height());
  174. settings.sync();
  175. }
  176. window->close();
  177. delete window;
  178. window = nullptr;
  179. }
  180. if (app)
  181. {
  182. if (! app->closingDown())
  183. app->quit();
  184. delete app;
  185. app = nullptr;
  186. }
  187. }
  188. void show()
  189. {
  190. qDebug("CarlaBridgeToolkitQt::show()");
  191. CARLA_ASSERT(window);
  192. if (window)
  193. window->show();
  194. }
  195. void hide()
  196. {
  197. qDebug("CarlaBridgeToolkitQt::hide()");
  198. CARLA_ASSERT(window);
  199. if (window)
  200. window->hide();
  201. }
  202. void resize(const int width, const int height)
  203. {
  204. qDebug("CarlaBridgeToolkitQt::resize(%i, %i)", width, height);
  205. CARLA_ASSERT(window);
  206. if (app->thread() != QThread::currentThread())
  207. {
  208. nextWidth = width;
  209. nextHeight = height;
  210. needsResize = true;
  211. return;
  212. }
  213. if (window)
  214. window->setFixedSize(width, height);
  215. #ifdef BRIDGE_CONTAINER
  216. if (embedContainer)
  217. embedContainer->setFixedSize(width, height);
  218. #endif
  219. }
  220. #ifdef BRIDGE_CONTAINER
  221. void* getContainerId()
  222. {
  223. qDebug("CarlaBridgeToolkitQt::getContainerId()");
  224. CARLA_ASSERT(window);
  225. if (! embedContainer)
  226. {
  227. embedContainer = new QEmbedContainer(window);
  228. window->setCentralWidget(embedContainer);
  229. window->adjustSize();
  230. embedContainer->setParent(window);
  231. embedContainer->show();
  232. }
  233. return (void*)embedContainer->winId();
  234. }
  235. #endif
  236. protected:
  237. QApplication* app;
  238. QMainWindow* window;
  239. QSettings settings;
  240. int msgTimer;
  241. bool needsResize;
  242. int nextWidth, nextHeight;
  243. #ifdef BRIDGE_CONTAINER
  244. QEmbedContainer* embedContainer;
  245. #endif
  246. void timerEvent(QTimerEvent* const event)
  247. {
  248. if (event->timerId() == msgTimer && client)
  249. {
  250. if (needsResize)
  251. {
  252. client->toolkitResize(nextWidth, nextHeight);
  253. needsResize = false;
  254. }
  255. if (client->isOscControlRegistered() && ! client->oscIdle())
  256. {
  257. killTimer(msgTimer);
  258. msgTimer = 0;
  259. }
  260. }
  261. QObject::timerEvent(event);
  262. }
  263. };
  264. // -------------------------------------------------------------------------
  265. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeClient* const client, const char* const title)
  266. {
  267. return new CarlaBridgeToolkitQt(client, title);
  268. }
  269. CARLA_BRIDGE_END_NAMESPACE