Audio plugin host https://kx.studio/carla
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.

223 lines
5.4KB

  1. /*
  2. * DISTRHO Notes Plugin
  3. * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 GPL.txt file
  16. */
  17. #include "DistrhoUINotes.hpp"
  18. #include <QtGui/QResizeEvent>
  19. START_NAMESPACE_DISTRHO
  20. // -------------------------------------------------
  21. DistrhoUINotes::DistrhoUINotes()
  22. : Qt4UI(),
  23. fCurPage(1),
  24. fSaveSizeNowChecker(-1),
  25. fSaveTextNowChecker(-1),
  26. fTextEdit(this),
  27. fButton(this),
  28. fProgressBar(this),
  29. fSpacer(this),
  30. fGridLayout(this)
  31. {
  32. fButton.setCheckable(true);
  33. fButton.setChecked(true);
  34. fButton.setText("Edit");
  35. fButton.setFixedSize(fButton.minimumSizeHint());
  36. fProgressBar.set_minimum(1);
  37. fProgressBar.set_maximum(100);
  38. fProgressBar.set_value(1);
  39. fSpacer.setText("");
  40. fSpacer.setFixedSize(5, 5);
  41. fTextEdit.setReadOnly(false);
  42. setLayout(&fGridLayout);
  43. fGridLayout.addWidget(&fTextEdit, 0, 0, 1, 3);
  44. fGridLayout.addWidget(&fButton, 1, 0, 1, 1);
  45. fGridLayout.addWidget(&fProgressBar, 1, 1, 1, 1);
  46. fGridLayout.addWidget(&fSpacer, 1, 2, 1, 1);
  47. fGridLayout.setContentsMargins(0, 0, 0, 0);
  48. connect(&fButton, SIGNAL(clicked(bool)), SLOT(buttonClicked(bool)));
  49. connect(&fProgressBar, SIGNAL(valueChangedFromBar(float)), SLOT(progressBarValueChanged(float)));
  50. connect(&fTextEdit, SIGNAL(textChanged()), SLOT(textChanged()));
  51. setFixedSize(300, 200);
  52. }
  53. DistrhoUINotes::~DistrhoUINotes()
  54. {
  55. }
  56. void DistrhoUINotes::saveCurrentTextState()
  57. {
  58. QString pageKey = QString("pageText #%1").arg(fCurPage);
  59. QString pageValue = fTextEdit.toPlainText();
  60. if (pageValue != fNotes[fCurPage-1])
  61. {
  62. fNotes[fCurPage-1] = pageValue;
  63. d_setState(pageKey.toUtf8().constData(), pageValue.toUtf8().constData());
  64. }
  65. }
  66. // -------------------------------------------------
  67. // DSP Callbacks
  68. void DistrhoUINotes::d_parameterChanged(uint32_t index, float value)
  69. {
  70. if (index != 0)
  71. return;
  72. int nextCurPage = value;
  73. if (nextCurPage != fCurPage && nextCurPage >= 1 && nextCurPage <= 100)
  74. {
  75. saveCurrentTextState();
  76. fCurPage = nextCurPage;
  77. fTextEdit.setPlainText(fNotes[fCurPage-1]);
  78. fProgressBar.set_value(fCurPage);
  79. fProgressBar.update();
  80. }
  81. }
  82. void DistrhoUINotes::d_stateChanged(const char* key, const char* value)
  83. {
  84. if (std::strcmp(key, "guiWidth") == 0)
  85. {
  86. bool ok;
  87. int width = QString(value).toInt(&ok);
  88. if (ok && width > 0)
  89. setFixedWidth(width);
  90. }
  91. else if (std::strcmp(key, "guiHeight") == 0)
  92. {
  93. bool ok;
  94. int height = QString(value).toInt(&ok);
  95. if (ok && height > 0)
  96. setFixedHeight(height);
  97. }
  98. else if (std::strncmp(key, "pageText #", 10) == 0)
  99. {
  100. bool ok;
  101. int pageIndex = QString(key+10).toInt(&ok);
  102. if (ok && pageIndex >= 1 && pageIndex <= 100)
  103. {
  104. fNotes[pageIndex-1] = QString(value);
  105. if (pageIndex == fCurPage)
  106. fTextEdit.setPlainText(fNotes[pageIndex-1]);
  107. }
  108. }
  109. else if (strcmp(key, "readOnly") == 0)
  110. {
  111. bool readOnly = !strcmp(value, "yes");
  112. fButton.setChecked(!readOnly);
  113. fTextEdit.setReadOnly(readOnly);
  114. }
  115. }
  116. // -------------------------------------------------
  117. // UI Callbacks
  118. void DistrhoUINotes::d_uiIdle()
  119. {
  120. if (fSaveSizeNowChecker == 11)
  121. {
  122. d_setState("guiWidth", QString::number(width()).toUtf8().constData());
  123. d_setState("guiHeight", QString::number(height()).toUtf8().constData());
  124. fSaveSizeNowChecker = -1;
  125. }
  126. if (fSaveTextNowChecker == 11)
  127. {
  128. saveCurrentTextState();
  129. fSaveTextNowChecker = -1;
  130. }
  131. if (fSaveSizeNowChecker >= 0)
  132. fSaveSizeNowChecker++;
  133. if (fSaveTextNowChecker >= 0)
  134. fSaveTextNowChecker++;
  135. Qt4UI::d_uiIdle();
  136. }
  137. void DistrhoUINotes::resizeEvent(QResizeEvent* event)
  138. {
  139. fSaveSizeNowChecker = 0;
  140. QWidget::resizeEvent(event);
  141. }
  142. // -------------------------------------------------
  143. void DistrhoUINotes::buttonClicked(bool click)
  144. {
  145. bool readOnly = !click;
  146. fTextEdit.setReadOnly(readOnly);
  147. d_setState("readOnly", readOnly ? "yes" : "no");
  148. }
  149. void DistrhoUINotes::progressBarValueChanged(float value)
  150. {
  151. value = rint(value);
  152. if (fCurPage == (int)value)
  153. return;
  154. // maybe save current text before changing page
  155. if (fSaveTextNowChecker >= 0 && value >= 1.0f && value <= 100.0f)
  156. {
  157. saveCurrentTextState();
  158. fSaveTextNowChecker = -1;
  159. }
  160. // change current page
  161. d_parameterChanged(0, value);
  162. // tell host about this change
  163. d_setParameterValue(0, value);
  164. }
  165. void DistrhoUINotes::textChanged()
  166. {
  167. fSaveTextNowChecker = 0;
  168. }
  169. // -------------------------------------------------
  170. UI* createUI()
  171. {
  172. return new DistrhoUINotes;
  173. }
  174. // -------------------------------------------------
  175. END_NAMESPACE_DISTRHO