| 1 | /* |
| 2 | Copyright (C) 2008, 2009 Andres Cabrera |
| 3 | [email protected] |
| 4 | |
| 5 | This file is part of CsoundQt. |
| 6 | |
| 7 | CsoundQt is free software; you can redistribute it |
| 8 | and/or modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | CsoundQt is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with Csound; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include "qutebutton.h" |
| 24 | |
| 25 | QuteButton::QuteButton(QWidget *parent) : QuteWidget(parent) |
| 26 | { |
| 27 | m_widget = new QPushButton(this); |
| 28 | m_widget->setContextMenuPolicy(Qt::NoContextMenu); |
| 29 | m_currentValue = 0; |
| 30 | // Necessary to pass mouse tracking to widget panel for _MouseX channels |
| 31 | m_widget->setMouseTracking(true); |
| 32 | setMouseTracking(true); |
| 33 | canFocus(false); |
| 34 | // m_imageFilename = "/"; |
| 35 | connect(static_cast<QPushButton *>(m_widget), SIGNAL(pressed()), |
| 36 | this, SLOT(buttonPressed())); |
| 37 | connect(static_cast<QPushButton *>(m_widget), SIGNAL(released()), |
| 38 | this, SLOT(buttonReleased())); |
| 39 | |
| 40 | setProperty("QCS_type", "event"); |
| 41 | setProperty("QCS_pressedValue", 1.0); |
| 42 | setProperty("QCS_stringvalue", ""); |
| 43 | setProperty("QCS_text", ""); |
| 44 | setProperty("QCS_image", ""); |
| 45 | setProperty("QCS_eventLine", ""); |
| 46 | setProperty("QCS_latch", false); |
| 47 | setProperty("QCS_momentaryMidiButton", false); // used for latched button if bound to MIDI controller |
| 48 | setProperty("QCS_latched", false); |
| 49 | setProperty("QCS_fontsize", 10); |
| 50 | |
| 51 | QPixmap p = QPixmap(8, 8); |
| 52 | p.fill(QColor(Qt::green)); |
| 53 | onIcon.addPixmap(p, QIcon::Normal, QIcon::On); |
| 54 | p.fill(QColor(Qt::black)); |
| 55 | onIcon.addPixmap(p, QIcon::Normal, QIcon::Off); |
| 56 | |
| 57 | m_isPlaying = false; // used for non-latched eventButton to turn the instrument off on second press |
| 58 | } |
| 59 | |
| 60 | QuteButton::~QuteButton() |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | void QuteButton::setValue(double value) |
| 65 | { |
| 66 | #ifdef USE_WIDGET_MUTEX |
| 67 | widgetLock.lockForWrite(); |
| 68 | #endif |
| 69 | if (value < 0) { |
| 70 | m_currentValue = -value; |
| 71 | m_value = -value; |
| 72 | } else { |
| 73 | m_currentValue = value != 0 ? m_value : 0.0; |
| 74 | if (property("QCS_latch").toBool()) { |
| 75 | static_cast<QPushButton *>(m_widget)->setChecked(m_currentValue != 0); |
| 76 | } |
| 77 | } |
| 78 | m_valueChanged = true; |
| 79 | #ifdef USE_WIDGET_MUTEX |
| 80 | widgetLock.unlock(); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | void QuteButton::setValue(QString text) |
| 85 | { |
| 86 | // qDebug() << "QuteButton::setValue" << text; |
| 87 | #ifdef USE_WIDGET_MUTEX |
| 88 | widgetLock.lockForWrite(); |
| 89 | #endif |
| 90 | if (m_channel.startsWith("_Browse") || m_channel.startsWith("_MBrowse") ) { |
| 91 | m_stringValue = text; |
| 92 | m_valueChanged = true; |
| 93 | } |
| 94 | #ifdef USE_WIDGET_MUTEX |
| 95 | widgetLock.unlock(); |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | double QuteButton::getValue() |
| 100 | { |
| 101 | // Returns the value for any button type. |
| 102 | double value = 0.0; |
| 103 | #ifdef USE_WIDGET_MUTEX |
| 104 | widgetLock.lockForRead(); |
| 105 | #endif |
| 106 | if (m_currentValue != 0) { |
| 107 | value = m_value; |
| 108 | } |
| 109 | #ifdef USE_WIDGET_MUTEX |
| 110 | widgetLock.unlock(); |
| 111 | #endif |
| 112 | return value; |
| 113 | } |
| 114 | |
| 115 | QString QuteButton::getStringValue() |
| 116 | { |
| 117 | #ifdef USE_WIDGET_MUTEX |
| 118 | widgetLock.lockForRead(); |
| 119 | #endif |
| 120 | QString stringValue; |
| 121 | QString name = m_channel; |
| 122 | if (name.startsWith("_Browse") || name.startsWith("_MBrowse") ) { |
| 123 | stringValue = m_stringValue; |
| 124 | #ifdef USE_WIDGET_MUTEX |
| 125 | widgetLock.unlock(); |
| 126 | #endif |
| 127 | } |
| 128 | else { |
| 129 | #ifdef USE_WIDGET_MUTEX |
| 130 | widgetLock.unlock(); |
| 131 | #endif |
| 132 | stringValue = QString::number(getValue()); |
| 133 | } |
| 134 | return stringValue; |
| 135 | } |
| 136 | |
| 137 | QString QuteButton::getWidgetLine() |
| 138 | { |
| 139 | #ifdef USE_WIDGET_MUTEX |
| 140 | widgetLock.lockForRead(); |
| 141 | #endif |
| 142 | QString line = "ioButton {" + QString::number(x()) + ", " + QString::number(y()) + "} "; |
| 143 | line += "{"+ QString::number(width()) +", "+ QString::number(height()) +"} "; |
| 144 | line += property("QCS_type").toString() + " "; |
| 145 | line += QString::number(m_value,'f', 6) + " "; |
| 146 | line += "\"" + m_channel + "\" "; |
| 147 | line += "\"" + static_cast<QPushButton *>(m_widget)->text().replace(QRegExp("[\n\r]"), "\u00AC") + "\" "; |
| 148 | line += "\"" + property("QCS_image").toString() + "\" "; |
| 149 | line += property("QCS_eventLine").toString(); |
| 150 | // qDebug("QuteButton::getWidgetLine() %s", line.toStdString().c_str()); |
| 151 | #ifdef USE_WIDGET_MUTEX |
| 152 | widgetLock.unlock(); |
| 153 | #endif |
| 154 | return line; |
| 155 | } |
| 156 | |
| 157 | QString QuteButton::getCabbageLine() |
| 158 | { |
| 159 | #ifdef USE_WIDGET_MUTEX |
| 160 | widgetLock.lockForRead(); |
| 161 | #endif |
| 162 | QString line = "button channel(\"" + m_channel + "\"), "; |
| 163 | line += QString("bounds(%1,%2,%3,%4), ").arg(x()).arg(y()).arg(width()).arg(height()); |
| 164 | if (property("QCS_latch").toBool()) { |
| 165 | line += QString("text(\"%1\", \"%2\"), ").arg(property("QCS_text").toString() + " OFF").arg(property("QCS_text").toString() + " ON"); // set different texts for ON/OFF if latced |
| 166 | } else { |
| 167 | line += "text(\"" + property("QCS_text").toString()+ " \"), "; // otherwise just the button text |
| 168 | } |
| 169 | line += QString("latched(%1)").arg((int)property("QCS_latch").toBool()); |
| 170 | if (property("QCS_midicc").toInt() >= 0 && property("QCS_midichan").toInt()>0) { // insert only if midi channel is above 0 |
| 171 | line += ", midiCtrl(\"" + QString::number(property("QCS_midichan").toInt()) + ","; |
| 172 | line += QString::number(property("QCS_midicc").toInt()) + "\")"; |
| 173 | } |
| 174 | #ifdef USE_WIDGET_MUTEX |
| 175 | widgetLock.unlock(); |
| 176 | #endif |
| 177 | // qDebug() << "Warning: Cabbage does not support button values different than 1, images or event buttons"; |
| 178 | return line; |
| 179 | } |
| 180 | |
| 181 | QString QuteButton::getQml() |
| 182 | { |
| 183 | QString qml = QString(); |
| 184 | #ifdef USE_WIDGET_MUTEX |
| 185 | widgetLock.lockForWrite(); |
| 186 | #endif |
| 187 | qml = "\tButton { \n"; |
| 188 | //qml += QString("\t\tid: %1Button\n").arg(m_channel); |
| 189 | qml += QString("\t\tx: %1 * scaleItem.scale\n").arg(x()); |
| 190 | qml += QString("\t\ty: %1 * scaleItem.scale\n").arg(y()); |
| 191 | qml += QString("\t\twidth: %1 * scaleItem.scale\n").arg(width()); |
| 192 | qml += QString("\t\theight: %1 * scaleItem.scale\n").arg(height()); |
| 193 | qml += QString("\t\ttext: \"%1\"\n").arg( property("QCS_text").toString()); |
| 194 | bool checkable = property("QCS_latch").toBool(); |
| 195 | if (checkable) { |
| 196 | qml += "\t\tcheckable: true\n"; |
| 197 | } |
| 198 | |
| 199 | qml += QString("\t\tproperty double pressedValue: %1\n").arg(property("QCS_pressedValue").toDouble()); // to be used for pressing the button. |
| 200 | |
| 201 | QString type = property("QCS_type").toString(); |
| 202 | qml += QString("\t\tproperty bool isEnventButton: %1\n").arg( (type=="value" ? "false" : "true" ) ); |
| 203 | if (type == "value") { |
| 204 | qml += QString(R"( |
| 205 | onPressedChanged: { |
| 206 | if (pressed) { |
| 207 | csound.setControlChannel("%1", pressedValue ); |
| 208 | } else { |
| 209 | csound.setControlChannel("%1", 0 ); |
| 210 | } |
| 211 | } |
| 212 | )").arg(m_channel); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | if (type == "event" || type == "pictevent") { |
| 217 | QString eventLine = property("QCS_eventLine").toString(); |
| 218 | QString turnOffLine = QString(); |
| 219 | if (property("QCS_latch").toBool() && eventLine.size() > 0) { |
| 220 | QStringList lineElements = eventLine.split(QRegExp("\\s"),QString::SkipEmptyParts); |
| 221 | if (lineElements.size() > 0 && lineElements[0] == "i") { |
| 222 | lineElements.removeAt(0); // Remove first element if it is "i" |
| 223 | } |
| 224 | else if (lineElements.size() > 0 && lineElements[0][0] == 'i') { |
| 225 | lineElements[0] = lineElements[0].mid(1); // Remove "i" character |
| 226 | } |
| 227 | |
| 228 | // this code is necessary to let instruments with line like "i 1 0 -1" to be switched on and off by latched button |
| 229 | if (lineElements.size() > 2 && lineElements[2].toDouble() < 0) { // If duration is negative, use button to turn note on and off |
| 230 | if ( lineElements[0].startsWith("\"") || lineElements[0].startsWith("\'") ) { |
| 231 | //qDebug()<<"Stopping named instrument: " << lineElements[0]; |
| 232 | lineElements[0].insert(1,"-"); |
| 233 | } else { |
| 234 | lineElements[0].prepend("-"); |
| 235 | } |
| 236 | lineElements.prepend("i"); |
| 237 | |
| 238 | turnOffLine = lineElements.join(" "); |
| 239 | |
| 240 | qml += QString("onCheckedChanged: (checked) ? csound.readScore(\'%1\') : csound.readScore(\'%2\')") |
| 241 | .arg(eventLine, turnOffLine); // if unchecked, turnOffLine should consist line to turn off the instrument |
| 242 | } |
| 243 | } else { // if not latched, use onClicked event |
| 244 | qml += QString("\t\tonClicked: csound.readScore(\'%1\') \n").arg(eventLine); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | |
| 249 | qml += "\t}\n"; |
| 250 | #ifdef USE_WIDGET_MUTEX |
| 251 | widgetLock.unlock(); |
| 252 | #endif |
| 253 | |
| 254 | return qml; |
| 255 | |
| 256 | } |
| 257 | |
| 258 | #define propDouble(prop, decimals) (QString::number(property(prop).toDouble(),'f', decimals)) |
| 259 | |
| 260 | QString QuteButton::getWidgetXmlText() |
| 261 | { |
| 262 | // Buttons are not implemented in blue |
| 263 | xmlText = ""; |
| 264 | QXmlStreamWriter s(&xmlText); |
| 265 | createXmlWriter(s); |
| 266 | |
| 267 | #ifdef USE_WIDGET_MUTEX |
| 268 | widgetLock.lockForRead(); |
| 269 | #endif |
| 270 | |
| 271 | s.writeTextElement("type", property("QCS_type").toString()); |
| 272 | s.writeTextElement("pressedValue", QString::number(m_value,'f', 8)); |
| 273 | s.writeTextElement("stringvalue", m_stringValue); |
| 274 | s.writeTextElement("text", property("QCS_text").toString()); |
| 275 | s.writeTextElement("image", property("QCS_image").toString()); |
| 276 | s.writeTextElement("eventLine", property("QCS_eventLine").toString()); |
| 277 | s.writeTextElement("latch", property("QCS_latch").toString()); |
| 278 | s.writeTextElement("momentaryMidiButton", property("QCS_momentaryMidiButton").toString()); |
| 279 | s.writeTextElement("latched", property("QCS_latched").toString()); |
| 280 | s.writeTextElement("fontsize", QString::number(property("QCS_fontsize").toInt())); |
| 281 | s.writeEndElement(); |
| 282 | #ifdef USE_WIDGET_MUTEX |
| 283 | widgetLock.unlock(); |
| 284 | #endif |
| 285 | return xmlText; |
| 286 | } |
| 287 | |
| 288 | QString QuteButton::getWidgetType() |
| 289 | { |
| 290 | return QString("BSBButton"); |
| 291 | } |
| 292 | |
| 293 | void QuteButton::applyProperties() |
| 294 | { |
| 295 | #ifdef USE_WIDGET_MUTEX |
| 296 | widgetLock.lockForWrite(); |
| 297 | #endif |
| 298 | QString eventLine = line->text(); |
| 299 | while (eventLine.size() > 0 && eventLine[0] == ' ') { |
| 300 | // remove all spaces at the beginning. This is needed for event queue lines |
| 301 | eventLine.remove(0,1); |
| 302 | } |
| 303 | setProperty("QCS_eventLine", eventLine); |
| 304 | setProperty("QCS_text", text->toPlainText()); |
| 305 | setProperty("QCS_image", filenameLineEdit->text()); |
| 306 | setProperty("QCS_type", typeComboBox->currentText()); |
| 307 | setProperty("QCS_pressedValue", valueBox->value()); |
| 308 | setProperty("QCS_latch", latchCheckBox->isChecked()); |
| 309 | setProperty("QCS_momentaryMidiButton", useMomentaryMidiButtonCheckBox->isChecked()); |
| 310 | setProperty("QCS_fontsize", fontSizeSpinBox->value()); |
| 311 | |
| 312 | #ifdef USE_WIDGET_MUTEX |
| 313 | widgetLock.unlock(); |
| 314 | #endif |
| 315 | //Must be last to make sure the widgetChanged signal is last |
| 316 | QuteWidget::applyProperties(); |
| 317 | // qDebug() << "QuteButton::applyProperties()" << m_value; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | void QuteButton::createPropertiesDialog() |
| 322 | { |
| 323 | QuteWidget::createPropertiesDialog(); |
| 324 | #ifdef USE_WIDGET_MUTEX |
| 325 | widgetLock.lockForRead(); |
| 326 | #endif |
| 327 | dialog->setWindowTitle("Button"); |
| 328 | |
| 329 | QLabel *label = new QLabel(dialog); |
| 330 | label->setText("Type"); |
| 331 | layout->addWidget(label, 4, 0, Qt::AlignRight|Qt::AlignVCenter); |
| 332 | typeComboBox = new QComboBox(dialog); |
| 333 | typeComboBox->addItem("event"); |
| 334 | typeComboBox->addItem("value"); |
| 335 | typeComboBox->addItem("pictevent"); |
| 336 | typeComboBox->addItem("pictvalue"); |
| 337 | typeComboBox->addItem("pict"); |
| 338 | typeComboBox->setCurrentIndex(typeComboBox->findText(property("QCS_type").toString())); |
| 339 | layout->addWidget(typeComboBox, 4, 1, Qt::AlignLeft|Qt::AlignVCenter); |
| 340 | |
| 341 | latchCheckBox = new QCheckBox(dialog); |
| 342 | latchCheckBox->setText(tr("Latch")); |
| 343 | layout->addWidget(latchCheckBox, 5, 1, 1,2, Qt::AlignLeft|Qt::AlignVCenter); |
| 344 | latchCheckBox->setChecked(property("QCS_latch").toBool()); |
| 345 | label = new QLabel(dialog); |
| 346 | label->setText("Value"); |
| 347 | layout->addWidget(label, 4, 2, Qt::AlignRight|Qt::AlignVCenter); |
| 348 | |
| 349 | valueBox = new QDoubleSpinBox(dialog); |
| 350 | valueBox->setDecimals(6); |
| 351 | valueBox->setRange(-9999999.0, 9999999.0); |
| 352 | valueBox->setValue(m_value); |
| 353 | valueBox->setMaximumWidth(100); |
| 354 | valueBox->setDecimals(4); |
| 355 | layout->addWidget(valueBox, 4, 3, Qt::AlignLeft|Qt::AlignVCenter); |
| 356 | |
| 357 | |
| 358 | label = new QLabel(dialog); |
| 359 | label->setText("Text:"); |
| 360 | layout->addWidget(label, 6, 0, Qt::AlignRight|Qt::AlignVCenter); |
| 361 | |
| 362 | text = new QTextEdit(dialog); |
| 363 | text->setMinimumWidth(320); |
| 364 | text->setText(property("QCS_text").toString()); |
| 365 | layout->addWidget(text, 6, 1, 1, 3, Qt::AlignLeft|Qt::AlignVCenter); |
| 366 | |
| 367 | label = new QLabel(dialog); |
| 368 | label->setText(tr("Font Size")); |
| 369 | layout->addWidget(label, 7, 0, Qt::AlignRight|Qt::AlignVCenter); |
| 370 | |
| 371 | fontSizeSpinBox = new QSpinBox(dialog); |
| 372 | fontSizeSpinBox->unsetLocale(); |
| 373 | fontSizeSpinBox->setMinimum(6); |
| 374 | fontSizeSpinBox->setMaximum(999); |
| 375 | fontSizeSpinBox->setValue(property("QCS_fontsize").toInt()); |
| 376 | layout->addWidget(fontSizeSpinBox, 7, 1, Qt::AlignLeft|Qt::AlignVCenter); |
| 377 | |
| 378 | label = new QLabel(dialog); |
| 379 | label->setText("Image:"); |
| 380 | layout->addWidget(label, 8, 0, Qt::AlignRight|Qt::AlignVCenter); |
| 381 | filenameLineEdit = new QLineEdit(dialog); |
| 382 | filenameLineEdit->setMinimumWidth(320); |
| 383 | filenameLineEdit->setText(property("QCS_image").toString()); |
| 384 | layout->addWidget(filenameLineEdit, 8, 1, 1, 3, Qt::AlignLeft|Qt::AlignVCenter); |
| 385 | |
| 386 | QPushButton *browseButton = new QPushButton(dialog); |
| 387 | browseButton->setText("Browse"); |
| 388 | layout->addWidget(browseButton, 8, 4, Qt::AlignLeft|Qt::AlignVCenter); |
| 389 | connect(browseButton, SIGNAL(released()), this, SLOT(browseFile())); |
| 390 | |
| 391 | label = new QLabel(dialog); |
| 392 | label->setText("Event:"); |
| 393 | layout->addWidget(label, 9, 0, Qt::AlignRight|Qt::AlignVCenter); |
| 394 | line = new QLineEdit(dialog); |
| 395 | // text->setText(((QuteLabel *)m_widget)->toPlainText()); |
| 396 | layout->addWidget(line, 9,1,1,3, Qt::AlignLeft|Qt::AlignVCenter); |
| 397 | line->setMinimumWidth(320); |
| 398 | line->setText(property("QCS_eventLine").toString()); |
| 399 | |
| 400 | useMomentaryMidiButtonCheckBox = new QCheckBox(dialog); |
| 401 | useMomentaryMidiButtonCheckBox->setText(tr("Momentary")); |
| 402 | useMomentaryMidiButtonCheckBox->setWhatsThis(tr("Check if you use MIDI push button (momentary button) to toggle the latch - \nFirst push switches on, second off.")); |
| 403 | // TODO: enabling/disabling needs some signal->slot connection |
| 404 | //useMomentaryMidiButtonCheckBox->setEnabled( latchCheckBox->isChecked() ); |
| 405 | useMomentaryMidiButtonCheckBox->setChecked(property("QCS_momentaryMidiButton").toBool()); |
| 406 | |
| 407 | const int midiRow = layout->rowCount()-2; |
| 408 | layout->removeWidget(midiLearnButton); |
| 409 | layout->addWidget(useMomentaryMidiButtonCheckBox, midiRow, 4, Qt::AlignLeft|Qt::AlignVCenter); |
| 410 | layout->addWidget(midiLearnButton, midiRow, 5, Qt::AlignLeft|Qt::AlignVCenter); |
| 411 | |
| 412 | |
| 413 | |
| 414 | #ifdef USE_WIDGET_MUTEX |
| 415 | widgetLock.unlock(); |
| 416 | #endif |
| 417 | } |
| 418 | |
| 419 | void QuteButton::setText(QString text) |
| 420 | { |
| 421 | // For old widget format conversion of line endings |
| 422 | setProperty("QCS_text", text); |
| 423 | static_cast<QPushButton *>(m_widget)->setText(text); |
| 424 | } |
| 425 | |
| 426 | void QuteButton::popUpMenu(QPoint pos) |
| 427 | { |
| 428 | QuteWidget::popUpMenu(pos); |
| 429 | } |
| 430 | |
| 431 | void QuteButton::setMidiValue(int value) |
| 432 | { |
| 433 | double pressedValue = property("QCS_pressedValue").toDouble(); |
| 434 | double newValue = 0; |
| 435 | |
| 436 | bool isLatch = property("QCS_latch").toBool(); |
| 437 | bool useMomentaryMidiButton = property("QCS_momentaryMidiButton").toBool(); |
| 438 | QString type = property("QCS_type").toString(); |
| 439 | |
| 440 | qDebug () << "Playing: " << m_isPlaying << type; |
| 441 | |
| 442 | if (isLatch && useMomentaryMidiButton) { |
| 443 | if (value >0 ) { |
| 444 | if ( (type.contains("event") && m_isPlaying) || (type.contains("value") && m_currentValue > 0) ) { |
| 445 | qDebug() << "Toggle value to 0 / playing off from MIDI"; |
| 446 | newValue = 0; |
| 447 | } else { |
| 448 | newValue = pressedValue; |
| 449 | qDebug() << "Toggle value 1 / playing on from MIDI"; |
| 450 | } |
| 451 | } else { |
| 452 | qDebug() << "Ignore button release of momentary button"; |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | } else { |
| 457 | newValue = (value > 0) ? pressedValue : 0; |
| 458 | } |
| 459 | |
| 460 | setValue(newValue); |
| 461 | performAction(); |
| 462 | |
| 463 | } |
| 464 | |
| 465 | void QuteButton::refreshWidget() |
| 466 | { |
| 467 | // setValue sets the value the widget outputs while it is pressed |
| 468 | // static_cast<QPushButton *>(m_widget)->setChecked(m_value); |
| 469 | |
| 470 | #ifdef USE_WIDGET_MUTEX |
| 471 | widgetLock.lockForRead(); |
| 472 | #endif |
| 473 | |
| 474 | // setProperty("QCS_value", m_value); |
| 475 | // setProperty("QCS_stringvalue", m_stringValue); |
| 476 | |
| 477 | setProperty("QCS_latched", m_currentValue != 0); |
| 478 | m_valueChanged = false; |
| 479 | #ifdef USE_WIDGET_MUTEX |
| 480 | widgetLock.unlock(); |
| 481 | #endif |
| 482 | } |
| 483 | |
| 484 | |
| 485 | void QuteButton::applyInternalProperties() |
| 486 | { |
| 487 | QuteWidget::applyInternalProperties(); |
| 488 | //qDebug() << "QuteButton::applyInternalProperties"; |
| 489 | m_value = property("QCS_pressedValue").toDouble(); |
| 490 | // m_value2 = property("QCS_value2").toDouble(); |
| 491 | m_stringValue = property("QCS_stringvalue").toString(); |
| 492 | QString type = property("QCS_type").toString(); |
| 493 | auto w = static_cast<QPushButton*>(m_widget); |
| 494 | w->setCheckable(property("QCS_latch").toBool()); |
| 495 | // Set icon here, because it can be overwritten if button is "pict" |
| 496 | if (property("QCS_latch").toBool()) { |
| 497 | w->setIcon(onIcon); |
| 498 | } else { |
| 499 | w->setIcon(QIcon()); |
| 500 | } |
| 501 | |
| 502 | if (type == "event" || type == "value") { |
| 503 | icon = QIcon(); |
| 504 | static_cast<QPushButton *>(m_widget)->setIcon(icon); |
| 505 | auto fontsizeProperty = property("QCS_fontsize"); |
| 506 | if(!fontsizeProperty.isValid()) { |
| 507 | qDebug() << "Button: fontsize invalid / not present. Setting to default"; |
| 508 | } else { |
| 509 | int fontsize = fontsizeProperty.toInt(); |
| 510 | if(fontsize <= 0) |
| 511 | qDebug() << "Invalid font size for button, skipping"; |
| 512 | else { |
| 513 | |
| 514 | auto sheet = QString("QPushButton {font-size: %1pt; }").arg(fontsize); |
| 515 | w->setStyleSheet(sheet); |
| 516 | } |
| 517 | } |
| 518 | w->setText(property("QCS_text").toString()); |
| 519 | |
| 520 | } else if (type == "pictevent" || type == "pictvalue" || type == "pict") { |
| 521 | qDebug() << "///////////////////////////"; |
| 522 | w->setStyleSheet(nullptr); |
| 523 | w->setText(""); |
| 524 | icon = QIcon(QPixmap(property("QCS_image").toString())); |
| 525 | static_cast<QPushButton *>(m_widget)->setIcon(icon); |
| 526 | static_cast<QPushButton *>(m_widget)->setIconSize(QSize(width(),height())); |
| 527 | } else { |
| 528 | qDebug() << "Warning! QuteButton::applyInternalProperties() unrecognized type:" |
| 529 | << type; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | |
| 534 | void QuteButton::performAction() { |
| 535 | QString type = property("QCS_type").toString(); |
| 536 | QString eventLine = property("QCS_eventLine").toString(); |
| 537 | QString name = m_channel; |
| 538 | bool isLatch = property("QCS_latch").toBool(); |
| 539 | //bool useMomentaryMidiButton = property("QCS_momentaryMidiButton").toBool(); |
| 540 | |
| 541 | if (type.contains("event") && !eventLine.isEmpty()) { |
| 542 | if ( hasIndefiniteDuration() ) { |
| 543 | if ( m_currentValue == 0 ) { // turn off |
| 544 | QStringList lineElements = eventLine.split(QRegExp("\\s"),SKIP_EMPTY_PARTS); |
| 545 | if (lineElements.size() > 0 && lineElements[0] == "i") { |
| 546 | lineElements.removeAt(0); // Remove first element if it is "i" |
| 547 | } |
| 548 | else if (lineElements.size() > 0 && lineElements[0][0] == 'i') { |
| 549 | lineElements[0] = lineElements[0].mid(1); // Remove "i" character |
| 550 | } |
| 551 | if ( lineElements[0].startsWith("\"") || lineElements[0].startsWith("\'") ) { |
| 552 | //qDebug()<<"Stopping named instrument: " << lineElements[0]; |
| 553 | lineElements[0].insert(1,"-"); |
| 554 | } |
| 555 | else { |
| 556 | lineElements[0].prepend("-"); |
| 557 | } |
| 558 | lineElements.prepend("i"); |
| 559 | setValue(0); |
| 560 | m_isPlaying = false; |
| 561 | emit(queueEventSignal(lineElements.join(" "))); |
| 562 | } else { |
| 563 | setValue( property("QCS_pressedValue").toDouble() ); // was 1 |
| 564 | m_isPlaying = true; |
| 565 | emit(queueEventSignal(eventLine)); |
| 566 | } |
| 567 | } else { // if not negative p3 then just fire the event |
| 568 | if (!isLatch && m_currentValue>0) { //do not fire the event if latched && m_value==0 && is positive p3 |
| 569 | emit(queueEventSignal(eventLine)); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | else if (type == "value" || type == "pictvalue") { |
| 574 | if(!name.startsWith("_")) { |
| 575 | emit newValue(QPair<QString, double>(name, m_currentValue)); |
| 576 | } |
| 577 | else if (name == "_Play") { |
| 578 | if(m_value == 0) |
| 579 | emit stop(); |
| 580 | else |
| 581 | emit play(); |
| 582 | } |
| 583 | else if (name == "_Stop") |
| 584 | emit stop(); |
| 585 | else if (name == "_Pause") |
| 586 | emit pause(); |
| 587 | else if (name == "_Render") |
| 588 | emit render(); |
| 589 | else if (name.startsWith("_Browse")) { |
| 590 | QString fileName = QFileDialog::getOpenFileName(this, tr("Select File")); |
| 591 | if (fileName != "") { |
| 592 | setProperty("QCS_stringvalue", fileName); |
| 593 | emit newValue(QPair<QString, QString>(name, fileName)); |
| 594 | } |
| 595 | } |
| 596 | else if (name.startsWith("_MBrowse")) { |
| 597 | // Browse multiple files |
| 598 | QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Select File(s)")); |
| 599 | if (!fileNames.isEmpty()) { |
| 600 | QString joinedNames = fileNames.join("|"); |
| 601 | setProperty("QCS_stringvalue", joinedNames); |
| 602 | emit newValue(QPair<QString, QString>(name, joinedNames)); |
| 603 | } |
| 604 | } |
| 605 | else { |
| 606 | qDebug() << "Warning: Channel names starting with _ are reserved. This will " |
| 607 | "be an error in a next release"; |
| 608 | emit newValue(QPair<QString, double>(name, m_currentValue)); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | bool QuteButton::hasIndefiniteDuration() |
| 614 | { |
| 615 | QString eventLine = property("QCS_eventLine").toString(); |
| 616 | if ( !eventLine.isEmpty()) { |
| 617 | QStringList lineElements = eventLine.split(QRegExp("\\s"),QString::SkipEmptyParts); |
| 618 | if (lineElements.size() > 0 && lineElements[0] == "i") { |
| 619 | lineElements.removeAt(0); // Remove first element if it is "i" |
| 620 | } |
| 621 | else if (lineElements.size() > 0 && lineElements[0][0] == 'i') { |
| 622 | lineElements[0] = lineElements[0].mid(1); // Remove "i" character |
| 623 | } |
| 624 | // If duration is negative, use button to turn note on and off |
| 625 | if (lineElements.size() > 2 && lineElements[2].toDouble() < 0) { |
| 626 | return true; |
| 627 | } |
| 628 | } |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | void QuteButton::buttonPressed() |
| 633 | { |
| 634 | #ifdef USE_WIDGET_MUTEX |
| 635 | widgetLock.lockForRead(); |
| 636 | #endif |
| 637 | // open file browser on release |
| 638 | if (m_channel.startsWith("_Browse") || m_channel.startsWith("_MBrowse")) { |
| 639 | return; |
| 640 | } |
| 641 | auto w = static_cast<QPushButton *>(m_widget); |
| 642 | if (property("QCS_latch").toBool()) { |
| 643 | m_currentValue = !w->isChecked() ? m_value : 0; |
| 644 | } else { |
| 645 | m_currentValue = m_value; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | |
| 650 | #ifdef USE_WIDGET_MUTEX |
| 651 | widgetLock.unlock(); |
| 652 | #endif |
| 653 | performAction(); |
| 654 | } |
| 655 | |
| 656 | void QuteButton::buttonReleased() |
| 657 | { |
| 658 | // Only produce events for event types |
| 659 | #ifdef USE_WIDGET_MUTEX |
| 660 | widgetLock.lockForRead(); |
| 661 | #endif |
| 662 | if (m_channel.startsWith("_Browse") || m_channel.startsWith("_MBrowse")) { |
| 663 | performAction(); |
| 664 | return; |
| 665 | } |
| 666 | bool isLatch = property("QCS_latch").toBool(); |
| 667 | |
| 668 | if (!isLatch ) { |
| 669 | m_currentValue = 0; |
| 670 | if ( property("QCS_type").toString().contains("event") && hasIndefiniteDuration() ) { |
| 671 | performAction(); // to stop the playing instrument |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | |
| 676 | #ifdef USE_WIDGET_MUTEX |
| 677 | widgetLock.unlock(); |
| 678 | #endif |
| 679 | emit newValue(QPair<QString, double>(m_channel, m_currentValue)); |
| 680 | |
| 681 | } |
| 682 | |
| 683 | void QuteButton::browseFile() |
| 684 | { |
| 685 | QString file = QFileDialog::getOpenFileName(this,tr("Select File")); |
| 686 | if (file!="") { |
| 687 | filenameLineEdit->setText(file); |
| 688 | } |
| 689 | } |