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.

217 lines
5.3KB

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