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.

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