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.

228 lines
5.5KB

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