| 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 "qutecheckbox.h" |
| 24 | |
| 25 | QuteCheckBox::QuteCheckBox(QWidget *parent) : QuteWidget(parent) |
| 26 | { |
| 27 | m_widget = new QCheckBox(this); |
| 28 | auto w = static_cast<QCheckBox *>(m_widget); |
| 29 | // w->setStyleSheet("QCheckBox::indicator { width:50px; height: 50px; }"); |
| 30 | |
| 31 | // Necessary to pass mouse tracking to widget panel for _MouseX channels |
| 32 | m_widget->setMouseTracking(true); |
| 33 | m_widget->setContextMenuPolicy(Qt::NoContextMenu); |
| 34 | canFocus(false); |
| 35 | |
| 36 | setProperty("QCS_selected", false); |
| 37 | setProperty("QCS_label", ""); |
| 38 | setProperty("QCS_pressedValue", 1.0); |
| 39 | setProperty("QCS_randomizable", false); |
| 40 | setProperty("QCS_randomizableGroup", 0); |
| 41 | |
| 42 | m_currentValue = 0; |
| 43 | |
| 44 | w->setStyleSheet(QString("QCheckBox::indicator { " |
| 45 | "width:%1px; " |
| 46 | "height: %1px; }").arg(this->height())); |
| 47 | |
| 48 | connect(w, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int))); |
| 49 | } |
| 50 | |
| 51 | QuteCheckBox::~QuteCheckBox() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void QuteCheckBox::setValue(double value) |
| 56 | { |
| 57 | #ifdef USE_WIDGET_MUTEX |
| 58 | widgetLock.lockForWrite(); |
| 59 | #endif |
| 60 | if (value >=0) { |
| 61 | m_currentValue = value != 0 ? m_value : 0.0; |
| 62 | } |
| 63 | else { |
| 64 | m_value = -value; |
| 65 | m_currentValue = -value; |
| 66 | } |
| 67 | m_valueChanged = true; |
| 68 | // qDebug( ) << "QuteCheckBox::setValue " << value << "---" << m_currentValue; |
| 69 | #ifdef USE_WIDGET_MUTEX |
| 70 | widgetLock.unlock(); |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | void QuteCheckBox::setLabel(QString label) |
| 75 | { |
| 76 | static_cast<QCheckBox *>(m_widget)->setText(label); |
| 77 | } |
| 78 | |
| 79 | double QuteCheckBox::getValue() |
| 80 | { |
| 81 | #ifdef USE_WIDGET_MUTEX |
| 82 | widgetLock.lockForRead(); |
| 83 | #endif |
| 84 | double value = m_currentValue; |
| 85 | #ifdef USE_WIDGET_MUTEX |
| 86 | widgetLock.unlock(); |
| 87 | #endif |
| 88 | return value; |
| 89 | } |
| 90 | |
| 91 | //QString QuteCheckBox::getLabel() |
| 92 | //{ |
| 93 | // return static_cast<QCheckBox *>(m_widget)->text(); |
| 94 | //} |
| 95 | |
| 96 | QString QuteCheckBox::getWidgetLine() |
| 97 | { |
| 98 | #ifdef USE_WIDGET_MUTEX |
| 99 | widgetLock.lockForRead(); |
| 100 | #endif |
| 101 | QString line = "ioCheckbox {" + QString::number(x()) + ", " + QString::number(y()) + "} "; |
| 102 | line += "{"+ QString::number(width()) +", "+ QString::number(height()) +"} "; |
| 103 | line += (static_cast<QCheckBox *>(m_widget)->isChecked()? QString("on "):QString("off ")); |
| 104 | line += m_channel; |
| 105 | // qDebug("QuteText::getWidgetLine() %s", line.toStdString().c_str()); |
| 106 | #ifdef USE_WIDGET_MUTEX |
| 107 | widgetLock.unlock(); |
| 108 | #endif |
| 109 | return line; |
| 110 | } |
| 111 | |
| 112 | QString QuteCheckBox::getCabbageLine() |
| 113 | { |
| 114 | #ifdef USE_WIDGET_MUTEX |
| 115 | widgetLock.lockForRead(); |
| 116 | #endif |
| 117 | QString line = "checkbox channel(\"" + m_channel + "\"), "; |
| 118 | line += QString("bounds(%1,%2,%3,%4), ").arg(x()).arg(y()).arg(width()).arg(height()); |
| 119 | line += "value(" + (static_cast<QCheckBox *>(m_widget)->isChecked()? |
| 120 | QString("1"):QString("0")) + ")"; |
| 121 | //line += QString(", caption(%1)").arg(m_channel); |
| 122 | if (property("QCS_midicc").toInt() >= 0 && property("QCS_midichan").toInt()>0) { // insert only if midi channel is above 0 |
| 123 | line += ", midiCtrl(\"" + QString::number(property("QCS_midichan").toInt()) + ","; |
| 124 | line += QString::number(property("QCS_midicc").toInt()) + "\")"; |
| 125 | } |
| 126 | #ifdef USE_WIDGET_MUTEX |
| 127 | widgetLock.unlock(); |
| 128 | #endif |
| 129 | return line; |
| 130 | } |
| 131 | |
| 132 | QString QuteCheckBox::getWidgetType() |
| 133 | { |
| 134 | return QString("BSBCheckBox"); |
| 135 | } |
| 136 | |
| 137 | QString QuteCheckBox::getQml() |
| 138 | { |
| 139 | QString qml = QString(); |
| 140 | #ifdef USE_WIDGET_MUTEX |
| 141 | widgetLock.lockForWrite(); |
| 142 | #endif |
| 143 | |
| 144 | qml = "\n\tCheckBox {\n"; |
| 145 | qml += QString("\t\tx: %1 * scaleItem.scale\n").arg(x()); |
| 146 | qml += QString("\t\ty: %1 * scaleItem.scale\n").arg(y()); |
| 147 | qml += QString("\t\twidth: %1 * scaleItem.scale\n").arg(width()); |
| 148 | qml += QString("\t\theight: %1 * scaleItem.scale\n").arg(height()); |
| 149 | qml += QString("\t\tchecked: %1\n").arg(getValue()==0 ? "false" : "true" ); |
| 150 | qml += QString("\t\tonCheckedChanged: csound.setControlChannel(\"%1\", checked ? %2 : 0)\n").arg(m_channel).arg(property("QCS_pressedValue").toDouble()); |
| 151 | //color, font and other outlook properties ignored by now |
| 152 | qml += "\t}"; |
| 153 | #ifdef USE_WIDGET_MUTEX |
| 154 | widgetLock.unlock(); |
| 155 | #endif |
| 156 | return qml; |
| 157 | |
| 158 | } |
| 159 | |
| 160 | void QuteCheckBox::setMidiValue(int value) |
| 161 | { |
| 162 | double pressedVal = property("QCS_pressedValue").toDouble(); |
| 163 | double newval= value == 0 ? 0 : pressedVal; |
| 164 | setValue(newval); |
| 165 | QPair<QString, double> channelValue(m_channel, newval); |
| 166 | emit newValue(channelValue); |
| 167 | } |
| 168 | |
| 169 | QString QuteCheckBox::getWidgetXmlText() |
| 170 | { |
| 171 | xmlText = ""; |
| 172 | QXmlStreamWriter s(&xmlText); |
| 173 | createXmlWriter(s); |
| 174 | |
| 175 | #ifdef USE_WIDGET_MUTEX |
| 176 | widgetLock.lockForRead(); |
| 177 | #endif |
| 178 | |
| 179 | s.writeTextElement("selected", |
| 180 | m_currentValue != 0 ? QString("true"):QString("false")); |
| 181 | s.writeTextElement("label", property("QCS_label").toString()); |
| 182 | s.writeTextElement("pressedValue", |
| 183 | QString::number(property("QCS_pressedValue").toDouble())); |
| 184 | s.writeStartElement("randomizable"); |
| 185 | s.writeAttribute("group", QString::number(property("QCS_randomizableGroup").toInt())); |
| 186 | s.writeCharacters(property("QCS_randomizable").toBool() ? "true": "false"); |
| 187 | s.writeEndElement(); |
| 188 | s.writeEndElement(); |
| 189 | |
| 190 | #ifdef USE_WIDGET_MUTEX |
| 191 | widgetLock.unlock(); |
| 192 | #endif |
| 193 | return xmlText; |
| 194 | } |
| 195 | |
| 196 | void QuteCheckBox::refreshWidget() |
| 197 | { |
| 198 | #ifdef USE_WIDGET_MUTEX |
| 199 | widgetLock.lockForRead(); |
| 200 | #endif |
| 201 | // qDebug() << "QuteCheckBox::refreshWidget " << "--" << m_currentValue; |
| 202 | m_widget->blockSignals(true); |
| 203 | static_cast<QCheckBox *>(m_widget)->setChecked(m_currentValue != 0); |
| 204 | m_widget->blockSignals(false); |
| 205 | // setProperty("QCS_pressedValue", m_value); |
| 206 | setProperty("QCS_selected", m_currentValue != 0); |
| 207 | |
| 208 | m_valueChanged = false; |
| 209 | #ifdef USE_WIDGET_MUTEX |
| 210 | widgetLock.unlock(); |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | void QuteCheckBox::resizeEvent(QResizeEvent *event) { |
| 215 | int size = qMin(this->height(), this->width()); |
| 216 | static_cast<QCheckBox*>(m_widget)->setStyleSheet(QString("QCheckBox::indicator { " |
| 217 | "width:%1px; " |
| 218 | "height: %1px; }").arg(size)); |
| 219 | QuteWidget::resizeEvent(event); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | void QuteCheckBox::applyInternalProperties() |
| 224 | { |
| 225 | QuteWidget::applyInternalProperties(); |
| 226 | // qDebug() << "QuteSlider::applyInternalProperties()"; |
| 227 | // Pressed value must go before selected to make sure it has the right value... |
| 228 | m_value = property("QCS_pressedValue").toDouble(); |
| 229 | setValue(property("QCS_selected").toBool() ? 1:0); |
| 230 | setLabel(property("QCS_label").toString()); |
| 231 | |
| 232 | static_cast<QCheckBox*>(m_widget)->setStyleSheet(QString("QCheckBox::indicator { " |
| 233 | "width:%1px; " |
| 234 | "height: %1px; }").arg(this->height())); |
| 235 | |
| 236 | } |
| 237 | |
| 238 | void QuteCheckBox::stateChanged(int state) |
| 239 | { |
| 240 | #ifdef USE_WIDGET_MUTEX |
| 241 | widgetLock.lockForRead(); |
| 242 | #endif |
| 243 | m_currentValue = (state != Qt::Unchecked ? property("QCS_pressedValue").toDouble() : 0); |
| 244 | QPair<QString, double> channelValue(m_channel, m_currentValue); |
| 245 | #ifdef USE_WIDGET_MUTEX |
| 246 | widgetLock.unlock(); |
| 247 | #endif |
| 248 | emit newValue(channelValue); |
| 249 | } |
| 250 | |
| 251 | void QuteCheckBox::applyProperties() |
| 252 | { |
| 253 | #ifdef USE_WIDGET_MUTEX |
| 254 | widgetLock.lockForWrite(); |
| 255 | #endif |
| 256 | setProperty("QCS_pressedValue", valueBox->value()); |
| 257 | #ifdef USE_WIDGET_MUTEX |
| 258 | widgetLock.unlock(); |
| 259 | #endif |
| 260 | //Must be last to make sure the widgetChanged signal is last |
| 261 | QuteWidget::applyProperties(); |
| 262 | } |
| 263 | |
| 264 | void QuteCheckBox::createPropertiesDialog() |
| 265 | { |
| 266 | QuteWidget::createPropertiesDialog(); |
| 267 | dialog->setWindowTitle("Check Box"); |
| 268 | |
| 269 | QLabel * label = new QLabel(dialog); |
| 270 | label->setText("Pressed Value"); |
| 271 | layout->addWidget(label, 4, 2, Qt::AlignRight|Qt::AlignVCenter); |
| 272 | |
| 273 | valueBox = new QDoubleSpinBox(dialog); |
| 274 | valueBox->setDecimals(6); |
| 275 | valueBox->setRange(-9999999.0, 9999999.0); |
| 276 | layout->addWidget(valueBox, 4, 3, Qt::AlignLeft|Qt::AlignVCenter); |
| 277 | |
| 278 | #ifdef USE_WIDGET_MUTEX |
| 279 | widgetLock.lockForRead(); |
| 280 | #endif |
| 281 | valueBox->setValue(property("QCS_pressedValue").toDouble()); |
| 282 | #ifdef USE_WIDGET_MUTEX |
| 283 | widgetLock.unlock(); |
| 284 | #endif |
| 285 | } |