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. if (showGui)
  128. show();
  129. else
  130. m_client->sendOscUpdate();
  131. // Main loop
  132. app->exec(client);
  133. }
  134. void quit()
  135. {
  136. qDebug("CarlaToolkitQt4::quit()");
  137. Q_ASSERT(app);
  138. if (window)
  139. {
  140. if (m_client)
  141. {
  142. settings.setValue(QString("%1/pos_x").arg(m_title), window->x());
  143. settings.setValue(QString("%1/pos_y").arg(m_title), window->y());
  144. settings.setValue(QString("%1/width").arg(m_title), window->width());
  145. settings.setValue(QString("%1/height").arg(m_title), window->height());
  146. settings.sync();
  147. }
  148. window->close();
  149. delete window;
  150. window = nullptr;
  151. }
  152. m_client = nullptr;
  153. if (app)
  154. {
  155. if (! app->closingDown())
  156. app->quit();
  157. delete app;
  158. app = nullptr;
  159. }
  160. }
  161. void show()
  162. {
  163. qDebug("CarlaToolkitQt4::show()");
  164. Q_ASSERT(window);
  165. if (window)
  166. window->show();
  167. }
  168. void hide()
  169. {
  170. qDebug("CarlaToolkitQt4::hide()");
  171. Q_ASSERT(window);
  172. if (window)
  173. window->hide();
  174. }
  175. void resize(int width, int height)
  176. {
  177. qDebug("CarlaToolkitQt4::resize(%i, %i)", width, height);
  178. Q_ASSERT(window);
  179. if (window)
  180. window->setFixedSize(width, height);
  181. }
  182. private:
  183. BridgeApplication* app;
  184. QDialog* window;
  185. QSettings settings;
  186. };
  187. // -------------------------------------------------------------------------
  188. CarlaToolkit* CarlaToolkit::createNew(const char* const title)
  189. {
  190. return new CarlaToolkitQt4(title);
  191. }
  192. CARLA_BRIDGE_END_NAMESPACE