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.

DistrhoUINotes.cpp 5.4KB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "moc_DistrhoUINotes.cpp"
  21. // -------------------------------------------------
  22. DistrhoUINotes::DistrhoUINotes()
  23. : QtUI(),
  24. fCurPage(1),
  25. fSaveSizeNowChecker(-1),
  26. fSaveTextNowChecker(-1),
  27. fTextEdit(this),
  28. fButton(this),
  29. fProgressBar(this),
  30. fSpacer(this),
  31. fGridLayout(this)
  32. {
  33. fButton.setCheckable(true);
  34. fButton.setChecked(true);
  35. fButton.setText("Edit");
  36. fButton.setFixedSize(fButton.minimumSizeHint());
  37. fProgressBar.set_minimum(1);
  38. fProgressBar.set_maximum(100);
  39. fProgressBar.set_value(1);
  40. fSpacer.setText("");
  41. fSpacer.setFixedSize(5, 5);
  42. fTextEdit.setReadOnly(false);
  43. setLayout(&fGridLayout);
  44. fGridLayout.addWidget(&fTextEdit, 0, 0, 1, 3);
  45. fGridLayout.addWidget(&fButton, 1, 0, 1, 1);
  46. fGridLayout.addWidget(&fProgressBar, 1, 1, 1, 1);
  47. fGridLayout.addWidget(&fSpacer, 1, 2, 1, 1);
  48. fGridLayout.setContentsMargins(0, 0, 0, 0);
  49. connect(&fButton, SIGNAL(clicked(bool)), SLOT(buttonClicked(bool)));
  50. connect(&fProgressBar, SIGNAL(valueChangedFromBar(float)), SLOT(progressBarValueChanged(float)));
  51. connect(&fTextEdit, SIGNAL(textChanged()), SLOT(textChanged()));
  52. setSize(300, 200);
  53. }
  54. DistrhoUINotes::~DistrhoUINotes()
  55. {
  56. }
  57. void DistrhoUINotes::saveCurrentTextState()
  58. {
  59. QString pageKey = QString("pageText #%1").arg(fCurPage);
  60. QString pageValue = fTextEdit.toPlainText();
  61. if (pageValue != fNotes[fCurPage-1])
  62. {
  63. fNotes[fCurPage-1] = pageValue;
  64. d_setState(pageKey.toUtf8().constData(), pageValue.toUtf8().constData());
  65. }
  66. }
  67. // -------------------------------------------------
  68. // DSP Callbacks
  69. void DistrhoUINotes::d_parameterChanged(uint32_t index, float value)
  70. {
  71. if (index != 0)
  72. return;
  73. int nextCurPage = value;
  74. if (nextCurPage != fCurPage && nextCurPage >= 1 && nextCurPage <= 100)
  75. {
  76. saveCurrentTextState();
  77. fCurPage = nextCurPage;
  78. fTextEdit.setPlainText(fNotes[fCurPage-1]);
  79. fProgressBar.set_value(fCurPage);
  80. fProgressBar.update();
  81. }
  82. }
  83. void DistrhoUINotes::d_stateChanged(const char* key, const char* value)
  84. {
  85. if (std::strcmp(key, "guiWidth") == 0)
  86. {
  87. bool ok;
  88. int width = QString(value).toInt(&ok);
  89. if (ok && width > 0)
  90. setSize(width, height());
  91. }
  92. else if (std::strcmp(key, "guiHeight") == 0)
  93. {
  94. bool ok;
  95. int height = QString(value).toInt(&ok);
  96. if (ok && height > 0)
  97. setSize(width(), height);
  98. }
  99. else if (std::strncmp(key, "pageText #", 10) == 0)
  100. {
  101. bool ok;
  102. int pageIndex = QString(key+10).toInt(&ok);
  103. if (ok && pageIndex >= 1 && pageIndex <= 100)
  104. {
  105. fNotes[pageIndex-1] = QString(value);
  106. if (pageIndex == fCurPage)
  107. fTextEdit.setPlainText(fNotes[pageIndex-1]);
  108. }
  109. }
  110. else if (strcmp(key, "readOnly") == 0)
  111. {
  112. bool readOnly = !strcmp(value, "yes");
  113. fButton.setChecked(!readOnly);
  114. fTextEdit.setReadOnly(readOnly);
  115. }
  116. }
  117. // -------------------------------------------------
  118. // UI Callbacks
  119. void DistrhoUINotes::d_uiIdle()
  120. {
  121. if (fSaveSizeNowChecker == 11)
  122. {
  123. d_setState("guiWidth", QString::number(width()).toUtf8().constData());
  124. d_setState("guiHeight", QString::number(height()).toUtf8().constData());
  125. fSaveSizeNowChecker = -1;
  126. }
  127. if (fSaveTextNowChecker == 11)
  128. {
  129. saveCurrentTextState();
  130. fSaveTextNowChecker = -1;
  131. }
  132. if (fSaveSizeNowChecker >= 0)
  133. fSaveSizeNowChecker++;
  134. if (fSaveTextNowChecker >= 0)
  135. fSaveTextNowChecker++;
  136. }
  137. void DistrhoUINotes::resizeEvent(QResizeEvent* event)
  138. {
  139. fSaveSizeNowChecker = 0;
  140. QtUI::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