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.

264 lines
6.5KB

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