1 /*
2 Copyright (C) 2008, 2009 Andres Cabrera
3 mantaraya36@gmail.com
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
QuteCheckBox(QWidget * parent)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
~QuteCheckBox()51 QuteCheckBox::~QuteCheckBox()
52 {
53 }
54
setValue(double value)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
setLabel(QString label)74 void QuteCheckBox::setLabel(QString label)
75 {
76 static_cast<QCheckBox *>(m_widget)->setText(label);
77 }
78
getValue()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
getWidgetLine()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
getCabbageLine()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
getWidgetType()132 QString QuteCheckBox::getWidgetType()
133 {
134 return QString("BSBCheckBox");
135 }
136
getQml()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
getWidgetXmlText()160 QString QuteCheckBox::getWidgetXmlText()
161 {
162 xmlText = "";
163 QXmlStreamWriter s(&xmlText);
164 createXmlWriter(s);
165
166 #ifdef USE_WIDGET_MUTEX
167 widgetLock.lockForRead();
168 #endif
169
170 s.writeTextElement("selected",
171 m_currentValue != 0 ? QString("true"):QString("false"));
172 s.writeTextElement("label", property("QCS_label").toString());
173 s.writeTextElement("pressedValue",
174 QString::number(property("QCS_pressedValue").toDouble()));
175 s.writeStartElement("randomizable");
176 s.writeAttribute("group", QString::number(property("QCS_randomizableGroup").toInt()));
177 s.writeCharacters(property("QCS_randomizable").toBool() ? "true": "false");
178 s.writeEndElement();
179 s.writeEndElement();
180
181 #ifdef USE_WIDGET_MUTEX
182 widgetLock.unlock();
183 #endif
184 return xmlText;
185 }
186
refreshWidget()187 void QuteCheckBox::refreshWidget()
188 {
189 #ifdef USE_WIDGET_MUTEX
190 widgetLock.lockForRead();
191 #endif
192 // qDebug() << "QuteCheckBox::refreshWidget " << "--" << m_currentValue;
193 m_widget->blockSignals(true);
194 static_cast<QCheckBox *>(m_widget)->setChecked(m_currentValue != 0);
195 m_widget->blockSignals(false);
196 // setProperty("QCS_pressedValue", m_value);
197 setProperty("QCS_selected", m_currentValue != 0);
198
199 m_valueChanged = false;
200 #ifdef USE_WIDGET_MUTEX
201 widgetLock.unlock();
202 #endif
203 }
204
resizeEvent(QResizeEvent * event)205 void QuteCheckBox::resizeEvent(QResizeEvent *event) {
206 int size = qMin(this->height(), this->width());
207 static_cast<QCheckBox*>(m_widget)->setStyleSheet(QString("QCheckBox::indicator { "
208 "width:%1px; "
209 "height: %1px; }").arg(size));
210 QuteWidget::resizeEvent(event);
211 }
212
213
applyInternalProperties()214 void QuteCheckBox::applyInternalProperties()
215 {
216 QuteWidget::applyInternalProperties();
217 // qDebug() << "QuteSlider::applyInternalProperties()";
218 // Pressed value must go before selected to make sure it has the right value...
219 m_value = property("QCS_pressedValue").toDouble();
220 setValue(property("QCS_selected").toBool() ? 1:0);
221 setLabel(property("QCS_label").toString());
222
223 static_cast<QCheckBox*>(m_widget)->setStyleSheet(QString("QCheckBox::indicator { "
224 "width:%1px; "
225 "height: %1px; }").arg(this->height()));
226
227 }
228
stateChanged(int state)229 void QuteCheckBox::stateChanged(int state)
230 {
231 #ifdef USE_WIDGET_MUTEX
232 widgetLock.lockForRead();
233 #endif
234 m_currentValue = (state != Qt::Unchecked ? property("QCS_pressedValue").toDouble() : 0);
235 QPair<QString, double> channelValue(m_channel, m_currentValue);
236 #ifdef USE_WIDGET_MUTEX
237 widgetLock.unlock();
238 #endif
239 emit newValue(channelValue);
240 }
241
applyProperties()242 void QuteCheckBox::applyProperties()
243 {
244 #ifdef USE_WIDGET_MUTEX
245 widgetLock.lockForWrite();
246 #endif
247 setProperty("QCS_pressedValue", valueBox->value());
248 #ifdef USE_WIDGET_MUTEX
249 widgetLock.unlock();
250 #endif
251 //Must be last to make sure the widgetChanged signal is last
252 QuteWidget::applyProperties();
253 }
254
createPropertiesDialog()255 void QuteCheckBox::createPropertiesDialog()
256 {
257 QuteWidget::createPropertiesDialog();
258 dialog->setWindowTitle("Check Box");
259
260 QLabel * label = new QLabel(dialog);
261 label->setText("Pressed Value");
262 layout->addWidget(label, 4, 2, Qt::AlignRight|Qt::AlignVCenter);
263
264 valueBox = new QDoubleSpinBox(dialog);
265 valueBox->setDecimals(6);
266 valueBox->setRange(-9999999.0, 9999999.0);
267 layout->addWidget(valueBox, 4, 3, Qt::AlignLeft|Qt::AlignVCenter);
268
269 #ifdef USE_WIDGET_MUTEX
270 widgetLock.lockForRead();
271 #endif
272 valueBox->setValue(property("QCS_pressedValue").toDouble());
273 #ifdef USE_WIDGET_MUTEX
274 widgetLock.unlock();
275 #endif
276 }
277