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.

238 lines
5.9KB

  1. /*
  2. * Carla UI bridge code
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.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.h"
  18. #include <QtCore/QSettings>
  19. #include <QtCore/QTimer>
  20. #include <QtCore/QTimerEvent>
  21. #include <QtGui/QApplication>
  22. #include <QtGui/QDialog>
  23. #include <QtGui/QVBoxLayout>
  24. CARLA_BRIDGE_START_NAMESPACE
  25. static int qargc = 0;
  26. static char* qargv[] = { nullptr };
  27. class BridgeApplication : public QApplication
  28. {
  29. public:
  30. BridgeApplication()
  31. : QApplication(qargc, qargv, true)
  32. {
  33. msgTimer = 0;
  34. m_client = nullptr;
  35. }
  36. void exec(CarlaClient* const client)
  37. {
  38. m_client = client;
  39. msgTimer = startTimer(50);
  40. QApplication::exec();
  41. }
  42. protected:
  43. void timerEvent(QTimerEvent* const event)
  44. {
  45. if (event->timerId() == msgTimer)
  46. {
  47. if (m_client && ! m_client->runMessages())
  48. killTimer(msgTimer);
  49. }
  50. QApplication::timerEvent(event);
  51. }
  52. private:
  53. int msgTimer;
  54. CarlaClient* m_client;
  55. };
  56. // -------------------------------------------------------------------------
  57. class CarlaToolkitQt4: public CarlaToolkit
  58. {
  59. public:
  60. CarlaToolkitQt4(const char* const title)
  61. : CarlaToolkit(title),
  62. #if defined(BRIDGE_LV2_QT4)
  63. settings("Cadence", "Carla-Qt4UIs")
  64. #elif defined(BRIDGE_LV2_X11) || defined(BRIDGE_VST_X11)
  65. settings("Cadence", "Carla-X11UIs")
  66. #elif defined(BRIDGE_LV2_HWND) || defined(BRIDGE_VST_HWND)
  67. settings("Cadence", "Carla-HWNDUIs")
  68. #else
  69. settings("Cadence", "Carla-UIs")
  70. #endif
  71. {
  72. qDebug("CarlaToolkitQt4::CarlaToolkitQt4(%s)", title);
  73. app = nullptr;
  74. window = nullptr;
  75. }
  76. ~CarlaToolkitQt4()
  77. {
  78. qDebug("CarlaToolkitQt4::~CarlaToolkitQt4()");
  79. Q_ASSERT(! app);
  80. }
  81. void init()
  82. {
  83. qDebug("CarlaToolkitQt4::init()");
  84. Q_ASSERT(! app);
  85. app = new BridgeApplication;
  86. }
  87. void exec(CarlaClient* const client, const bool showGui)
  88. {
  89. qDebug("CarlaToolkitQt4::exec(%p)", client);
  90. Q_ASSERT(app);
  91. Q_ASSERT(client);
  92. m_client = client;
  93. if (client->needsReparent())
  94. {
  95. window = (QDialog*)client->getWidget();
  96. window->resize(10, 10);
  97. }
  98. else
  99. {
  100. // TODO - window->setCentralWidget(widget); or other simpler method
  101. window = new QDialog(nullptr);
  102. window->resize(10, 10);
  103. window->setLayout(new QVBoxLayout(window));
  104. QWidget* const widget = (QWidget*)client->getWidget();
  105. window->layout()->addWidget(widget);
  106. window->layout()->setContentsMargins(0, 0, 0, 0);
  107. window->adjustSize();
  108. widget->setParent(window);
  109. widget->show();
  110. }
  111. if (! client->isResizable())
  112. window->setFixedSize(window->width(), window->height());
  113. window->setWindowTitle(m_title);
  114. if (settings.contains(QString("%1/pos_x").arg(m_title)))
  115. {
  116. int posX = settings.value(QString("%1/pos_x").arg(m_title), window->x()).toInt();
  117. int posY = settings.value(QString("%1/pos_y").arg(m_title), window->y()).toInt();
  118. window->move(posX, posY);
  119. if (client->isResizable())
  120. {
  121. int width = settings.value(QString("%1/width").arg(m_title), window->width()).toInt();
  122. int height = settings.value(QString("%1/height").arg(m_title), window->height()).toInt();
  123. window->resize(width, height);
  124. }
  125. }
  126. app->connect(window, SIGNAL(finished(int)), app, SLOT(quit()));
  127. m_client->sendOscUpdate();
  128. if (showGui)
  129. show();
  130. // Main loop
  131. app->exec(client);
  132. }
  133. void quit()
  134. {
  135. qDebug("CarlaToolkitQt4::quit()");
  136. Q_ASSERT(app);
  137. if (window)
  138. {
  139. if (m_client)
  140. {
  141. settings.setValue(QString("%1/pos_x").arg(m_title), window->x());
  142. settings.setValue(QString("%1/pos_y").arg(m_title), window->y());
  143. settings.setValue(QString("%1/width").arg(m_title), window->width());
  144. settings.setValue(QString("%1/height").arg(m_title), window->height());
  145. settings.sync();
  146. }
  147. window->close();
  148. delete window;
  149. window = nullptr;
  150. }
  151. m_client = nullptr;
  152. if (app)
  153. {
  154. if (! app->closingDown())
  155. app->quit();
  156. delete app;
  157. app = nullptr;
  158. }
  159. }
  160. void show()
  161. {
  162. qDebug("CarlaToolkitQt4::show()");
  163. Q_ASSERT(window);
  164. if (window)
  165. window->show();
  166. }
  167. void hide()
  168. {
  169. qDebug("CarlaToolkitQt4::hide()");
  170. Q_ASSERT(window);
  171. if (window)
  172. window->hide();
  173. }
  174. void resize(int width, int height)
  175. {
  176. qDebug("CarlaToolkitQt4::resize(%i, %i)", width, height);
  177. Q_ASSERT(window);
  178. if (window)
  179. window->setFixedSize(width, height);
  180. }
  181. private:
  182. BridgeApplication* app;
  183. QDialog* window;
  184. QSettings settings;
  185. };
  186. // -------------------------------------------------------------------------
  187. CarlaToolkit* CarlaToolkit::createNew(const char* const title)
  188. {
  189. return new CarlaToolkitQt4(title);
  190. }
  191. CARLA_BRIDGE_END_NAMESPACE