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.

273 lines
6.7KB

  1. /*
  2. * Carla Bridge Toolkit, Qt version
  3. * Copyright (C) 2011-2014 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 doc/GPL.txt file.
  16. */
  17. #include "CarlaBridgeClient.hpp"
  18. #include "CarlaBridgeToolkit.hpp"
  19. #include "CarlaStyle.hpp"
  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. #else
  26. # include <QtGui/QApplication>
  27. # include <QtGui/QMainWindow>
  28. #endif
  29. CARLA_BRIDGE_START_NAMESPACE
  30. // -------------------------------------------------------------------------
  31. static int qargc = 0;
  32. static char* qargv[0] = {};
  33. // -------------------------------------------------------------------------
  34. class CarlaBridgeToolkitQt: public QObject,
  35. public CarlaBridgeToolkit
  36. {
  37. Q_OBJECT
  38. public:
  39. CarlaBridgeToolkitQt(CarlaBridgeClient* const client, const char* const windowTitle)
  40. : QObject(nullptr),
  41. CarlaBridgeToolkit(client, windowTitle),
  42. fApp(nullptr),
  43. fWindow(nullptr),
  44. fMsgTimer(0),
  45. fNeedsShow(false),
  46. leakDetector_CarlaBridgeToolkitQt()
  47. {
  48. carla_debug("CarlaBridgeToolkitQt::CarlaBridgeToolkitQt(%p, \"%s\")", client, windowTitle);
  49. connect(this, SIGNAL(setSizeSafeSignal(int,int)), SLOT(setSizeSafeSlot(int,int)));
  50. }
  51. ~CarlaBridgeToolkitQt() override
  52. {
  53. CARLA_ASSERT(fApp == nullptr);
  54. CARLA_ASSERT(fWindow == nullptr);
  55. CARLA_ASSERT(fMsgTimer == 0);
  56. carla_debug("CarlaBridgeToolkitQt::~CarlaBridgeToolkitQt()");
  57. }
  58. void init() override
  59. {
  60. CARLA_ASSERT(fApp == nullptr);
  61. CARLA_ASSERT(fWindow == nullptr);
  62. CARLA_ASSERT(fMsgTimer == 0);
  63. carla_debug("CarlaBridgeToolkitQt::init()");
  64. fApp = new QApplication(qargc, qargv);
  65. fWindow = new QMainWindow(nullptr);
  66. fWindow->resize(30, 30);
  67. fWindow->hide();
  68. }
  69. void exec(const bool showUI) override
  70. {
  71. CARLA_ASSERT(kClient != nullptr);
  72. CARLA_ASSERT(fApp != nullptr);
  73. CARLA_ASSERT(fWindow != nullptr);
  74. carla_debug("CarlaBridgeToolkitQt::exec(%s)", bool2str(showUI));
  75. QWidget* const widget((QWidget*)kClient->getWidget());
  76. fWindow->setCentralWidget(widget);
  77. fWindow->adjustSize();
  78. widget->setParent(fWindow);
  79. widget->show();
  80. if (! kClient->isResizable())
  81. {
  82. fWindow->setFixedSize(fWindow->width(), fWindow->height());
  83. #ifdef Q_OS_WIN
  84. fWindow->setWindowFlags(fWindow->windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  85. #endif
  86. }
  87. fWindow->setWindowIcon(QIcon::fromTheme("carla", QIcon(":/scalable/carla.svg")));
  88. fWindow->setWindowTitle(kWindowTitle.buffer());
  89. if (showUI || fNeedsShow)
  90. {
  91. show();
  92. fNeedsShow = false;
  93. }
  94. fMsgTimer = startTimer(30);
  95. // First idle
  96. handleTimeout();
  97. // Main loop
  98. fApp->exec();
  99. }
  100. void quit() override
  101. {
  102. CARLA_ASSERT(kClient != nullptr);
  103. CARLA_ASSERT(fApp != nullptr);
  104. CARLA_ASSERT(fWindow != nullptr);
  105. carla_debug("CarlaBridgeToolkitQt::quit()");
  106. if (fMsgTimer != 0)
  107. {
  108. killTimer(fMsgTimer);
  109. fMsgTimer = 0;
  110. }
  111. if (fWindow != nullptr)
  112. {
  113. fWindow->close();
  114. delete fWindow;
  115. fWindow = nullptr;
  116. }
  117. if (fApp != nullptr)
  118. {
  119. if (! fApp->closingDown())
  120. fApp->quit();
  121. delete fApp;
  122. fApp = nullptr;
  123. }
  124. }
  125. void show() override
  126. {
  127. carla_debug("CarlaBridgeToolkitQt::show()");
  128. fNeedsShow = true;
  129. if (fWindow != nullptr)
  130. fWindow->show();
  131. }
  132. void hide() override
  133. {
  134. carla_debug("CarlaBridgeToolkitQt::hide()");
  135. fNeedsShow = false;
  136. if (fWindow != nullptr)
  137. fWindow->hide();
  138. }
  139. void resize(const int width, const int height) override
  140. {
  141. CARLA_ASSERT_INT(width > 0, width);
  142. CARLA_ASSERT_INT(height > 0, height);
  143. carla_debug("CarlaBridgeToolkitQt::resize(%i, %i)", width, height);
  144. if (width <= 0)
  145. return;
  146. if (height <= 0)
  147. return;
  148. emit setSizeSafeSignal(width, height);
  149. }
  150. protected:
  151. QApplication* fApp;
  152. QMainWindow* fWindow;
  153. int fMsgTimer;
  154. bool fNeedsShow;
  155. void handleTimeout()
  156. {
  157. if (kClient == nullptr)
  158. return;
  159. kClient->uiIdle();
  160. if (! kClient->oscIdle())
  161. {
  162. killTimer(fMsgTimer);
  163. fMsgTimer = 0;
  164. }
  165. }
  166. private:
  167. void timerEvent(QTimerEvent* const ev)
  168. {
  169. if (ev->timerId() == fMsgTimer)
  170. handleTimeout();
  171. QObject::timerEvent(ev);
  172. }
  173. signals:
  174. void setSizeSafeSignal(int, int);
  175. private slots:
  176. void setSizeSafeSlot(int width, int height)
  177. {
  178. CARLA_ASSERT(kClient != nullptr && ! kClient->isResizable());
  179. CARLA_ASSERT(fWindow != nullptr);
  180. if (kClient == nullptr || fWindow == nullptr)
  181. return;
  182. if (kClient->isResizable())
  183. fWindow->resize(width, height);
  184. else
  185. fWindow->setFixedSize(width, height);
  186. }
  187. #ifndef MOC_PARSING
  188. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgeToolkitQt)
  189. #endif
  190. };
  191. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  192. # include "CarlaBridgeToolkitQt5.moc"
  193. #else
  194. # include "CarlaBridgeToolkitQt4.moc"
  195. #endif
  196. // -------------------------------------------------------------------------
  197. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeClient* const client, const char* const windowTitle)
  198. {
  199. return new CarlaBridgeToolkitQt(client, windowTitle);
  200. }
  201. // -------------------------------------------------------------------------
  202. CARLA_BRIDGE_END_NAMESPACE
  203. // -------------------------------------------------------------------------
  204. // missing declaration
  205. int qInitResources();
  206. int qCleanupResources();
  207. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  208. # include "resources.qt5.cpp"
  209. #else
  210. # include "resources.qt4.cpp"
  211. #endif
  212. // -------------------------------------------------------------------------