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.

214 lines
5.4KB

  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. 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. 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. assert(app);
  73. 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. client->oscSendUpdate();
  112. // Main loop
  113. app->exec();
  114. }
  115. void quit()
  116. {
  117. qDebug("CarlaBridgeToolkitQt4::quit()");
  118. assert(app);
  119. if (window)
  120. {
  121. if (m_client)
  122. {
  123. settings.setValue(QString("%1/pos_x").arg(m_title), window->x());
  124. settings.setValue(QString("%1/pos_y").arg(m_title), window->y());
  125. settings.setValue(QString("%1/width").arg(m_title), window->width());
  126. settings.setValue(QString("%1/height").arg(m_title), window->height());
  127. settings.sync();
  128. }
  129. window->close();
  130. delete window;
  131. window = nullptr;
  132. }
  133. m_client = nullptr;
  134. if (app)
  135. {
  136. if (! app->closingDown())
  137. app->quit();
  138. delete app;
  139. }
  140. }
  141. void show()
  142. {
  143. qDebug("CarlaBridgeToolkitQt4::show()");
  144. assert(window);
  145. if (window)
  146. window->show();
  147. }
  148. void hide()
  149. {
  150. qDebug("CarlaBridgeToolkitQt4::hide()");
  151. assert(window);
  152. if (window)
  153. window->hide();
  154. }
  155. void resize(int width, int height)
  156. {
  157. qDebug("CarlaBridgeToolkitQt4::resize(%i, %i)", width, height);
  158. assert(window);
  159. if (window)
  160. window->setFixedSize(width, height);
  161. }
  162. private:
  163. QApplication* app;
  164. QDialog* window;
  165. QSettings settings;
  166. };
  167. // -------------------------------------------------------------------------
  168. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(const char* const title)
  169. {
  170. return new CarlaBridgeToolkitQt4(title);
  171. }
  172. CARLA_BRIDGE_END_NAMESPACE