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.

218 lines
5.5KB

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