Debian Codegraph @

src/qutespinbox.cpp

raw symbols SCIP
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 "qutespinbox.h"
24 #include <QStringBuilder>
25 #include <cmath>
26
27 #define USEFONTPIXELSIZE
28
29 QuteSpinBox::QuteSpinBox(QWidget* parent) : QuteText(parent)
30 {
31 delete m_widget; //delete widget created by parent constructor
32 m_widget = new QDoubleSpinBox(this);
33 m_widget->setContextMenuPolicy(Qt::NoContextMenu);
34 // Necessary to pass mouse tracking to widget panel for _MouseX channels
35 m_widget->setMouseTracking(true);
36 static_cast<QDoubleSpinBox*>(m_widget)->setAccelerated(true);
37 static_cast<QDoubleSpinBox*>(m_widget)->setRange(-999999999999.0, 999999999999.0);
38 static_cast<QDoubleSpinBox*>(m_widget)->setKeyboardTracking (false);
39 connect(static_cast<QDoubleSpinBox *>(m_widget), SIGNAL(valueChanged(double)),
40 this, SLOT(valueChanged(double)));
41
42 m_type = "editnum";
43
44 setProperty("QCS_value", (double) 0.0);
45 setProperty("QCS_resolution", (double) 0.1);
46 setProperty("QCS_minimum",(double) -999999999999.0);
47 setProperty("QCS_maximum", (double) 999999999999.0);
48 setProperty("QCS_randomizable", false);
49 setProperty("QCS_randomizableGroup", 0);
50 // For numbers with fixed resolution it makes more sense to align to the right
51 setProperty("QCS_alignment", "right");
52
53 // Remove these properties, which are part of parent class but
54 // have no meaning here
55 setProperty("QCS_label", QVariant());
56 setProperty("QCS_color", QVariant());
57 setProperty("QCS_borderradius", QVariant());
58 setProperty("QCS_borderwidth", QVariant());
59 }
60
61 QuteSpinBox::~QuteSpinBox() {}
62
63 void QuteSpinBox::setText(QString text)
64 {
65 setValue(text.toDouble());
66 }
67
68 void QuteSpinBox::setMidiValue(int value)
69 {
70 double max = property("QCS_maximum").toDouble();
71 double min = property("QCS_minimum").toDouble();
72 if (max != 999999999999.0 && min != -999999999999.0) {
73 double newval = min + ((value / 127.0)* (max - min));
74 setValue(newval);
75 QPair<QString, double> channelValue(m_channel, newval);
76 emit newValue(channelValue);
77 }
78 else {
79 qDebug() << "QuteSpinBox::setMidiValue ranges not set.";
80 }
81 }
82
83 //void QuteSpinBox::setResolution(double resolution)
84 //{
85 // setProperty("QCS_resolution", resolution);
86 //}
87
88 QString QuteSpinBox::getWidgetLine()
89 {
90 #ifdef USE_WIDGET_MUTEX
91 widgetLock.lockForRead();
92 #endif
93 QString line = "ioText {" + QString::number(property("QCS_x").toInt()) + ", " + QString::number(property("QCS_y").toInt()) + "} ";
94 line += "{"+ QString::number(property("QCS_width").toInt()) +", "+ QString::number(property("QCS_height").toInt()) +"} ";
95 line += m_type + " ";
96 line += QString::number(static_cast<QDoubleSpinBox*>(m_widget)->value(), 'f', 6) + " ";
97 line += QString::number(property("QCS_resolution").toDouble(), 'f', 6) + " \"" + m_channel + "\" ";
98 line += property("QCS_alignment").toString() + " ";
99 line += "\"" + m_widget->property("QCS_font").toString() + "\" "
100 + QString::number(m_widget->property("QCS_fontsize").toInt()) + " ";
101 QColor color = m_widget->palette().color(QPalette::WindowText);
102 line += "{" + QString::number(color.red() * 256)
103 + ", " + QString::number(color.green() * 256)
104 + ", " + QString::number(color.blue() * 256) + "} ";
105 color = m_widget->palette().color(QPalette::Window);
106 line += "{" + QString::number(color.red() * 256)
107 + ", " + QString::number(color.green() * 256)
108 + ", " + QString::number(color.blue() * 256) + "} ";
109 line += m_widget->autoFillBackground()? "background ":"nobackground ";
110 line += "noborder ";
111 line += QString::number(static_cast<QDoubleSpinBox*>(m_widget)->value(), 'f', 6);
112 // For this type of ioText widget, value and text are redundant. CsoundQt reads mthe value coming
113 // in the text field, but writes to both.
114 // qDebug("QuteText::getWidgetLine() %s", line.toStdString().c_str());
115 #ifdef USE_WIDGET_MUTEX
116 widgetLock.unlock();
117 #endif
118 return line;
119 }
120
121 QString QuteSpinBox::getCsladspaLine()
122 {
123 #ifdef USE_WIDGET_MUTEX
124 widgetLock.lockForRead();
125 #endif
126 QString line = "ControlPort=" + m_channel + "|" + m_channel + "\n";
127 line += "Range=-9999|9999";
128 #ifdef USE_WIDGET_MUTEX
129 widgetLock.unlock();
130 #endif
131 return line;
132 }
133
134 QString QuteSpinBox::getCabbageLine()
135 {
136 #ifdef USE_WIDGET_MUTEX
137 widgetLock.lockForRead();
138 #endif
139 QString line = "numberbox channel(\"" + m_channel + "\"), ";
140 line += QString("bounds(%1,%2,%3,%4), ").arg(x()).arg(y()).arg(width()).arg(height());
141 //line += QString("text(%1), ").arg(m_channel); // Is it good idea to put channel as name?
142 line += QString( "range(%1,%2,%3,1,%4), ").arg(property("QCS_minimum").toDouble()).arg(property("QCS_maximum").toDouble()).arg(m_value).arg(property("QCS_resolution").toDouble());
143 //color does not work CsoundQt, why? Leave it out now... line += QString("colour(\"%1\")").arg( property("QCS_color").toString());
144 QColor color = property("QCS_color").value<QColor>();
145 line += "fontcolour(" + QString::number(color.red()) + "," + QString::number(color.green()) + "," + QString::number(color.blue()) + "), ";
146 color = property("QCS_bgcolor").value<QColor>();
147 line += "colour(" + QString::number(color.red()) + "," + QString::number(color.green()) + "," + QString::number(color.blue()) + ") ";
148 if (property("QCS_midicc").toInt() >= 0 && property("QCS_midichan").toInt()>0) { // insert only if midi channel is above 0
149 line += ", midiCtrl(\"" + QString::number(property("QCS_midichan").toInt()) + ",";
150 line += QString::number(property("QCS_midicc").toInt()) + "\")";
151 }
152 #ifdef USE_WIDGET_MUTEX
153 widgetLock.unlock();
154 #endif
155 return line;
156 }
157
158 QString QuteSpinBox::getQml()
159 {
160 QString qml = QString();
161 #ifdef USE_WIDGET_MUTEX
162 widgetLock.lockForWrite();
163 #endif
164 // currently QtQuick.Controls 2 SpindBox can be programmes to handle floating point
165 // see https://doc.qt.io/qt-5.9/qml-qtquick-controls2-spinbox.html accepting floating point numbers
166 // but this is not supported here yet. Use integer stepSize for your spingbox for now
167 float resolution = property("QCS_resolution").toFloat();
168 if (floorf(resolution) != resolution ) { // check if integer
169 qDebug() << "stepsize not integer";
170 return QString();
171
172 // QMessageBox::warning(nullptr, tr("SpinBox conversion"),
173 // tr("Currently only integer resolution (stepsize) is supported for QML spinbox.\nPlease change your spinbox to use it in QML"));
174
175 // messagebox freezes here...
176 //return "";
177 }
178
179 qml = "\n\tSpinBox {\n"; // NB! Does not accept floating point numbers...
180
181 //qml += QString("\t\tid: %1SpinBox\n").arg(m_channel);
182 qml += QString("\t\tx: %1 * scaleItem.scale\n").arg(x());
183 qml += QString("\t\ty: %1 * scaleItem.scale\n").arg(y());
184 qml += QString("\t\twidth: (%1 + up.indicator.width + down.indicator.width) * scaleItem.scale\n").arg(width());
185 qml += QString("\t\theight: %1 * scaleItem.scale\n").arg(height());
186 qml += QString("\t\tfrom: %1\n").arg(property("QCS_minimum").toString());
187 qml += QString("\t\tto: %1\n").arg(property("QCS_maximum").toString());
188 qml += QString("\t\tstepSize: %1\n").arg(property("QCS_resolution").toInt()); // Int for now...
189 qml += QString("\t\tvalue: %1\n").arg(int(getValue())); // TODO: use double
190 qml += "\t\teditable: true\n";
191 qml += QString("\t\tonValueChanged: csound.setControlChannel(\"%1\", value)\n").arg(m_channel);
192 //color, font and other outlook properties ignored by now
193 qml += "\t}";
194 #ifdef USE_WIDGET_MUTEX
195 widgetLock.unlock();
196 #endif
197
198 return qml;
199
200 }
201
202 QString QuteSpinBox::getWidgetXmlText()
203 {
204 xmlText = "";
205 QXmlStreamWriter s(&xmlText);
206 createXmlWriter(s);
207
208 #ifdef USE_WIDGET_MUTEX
209 widgetLock.lockForRead();
210 #endif
211 // s.writeTextElement("type", m_type);
212
213 //These are not implemented in blue
214 s.writeTextElement("alignment", property("QCS_alignment").toString());
215
216 s.writeTextElement("font", property("QCS_font").toString());
217 s.writeTextElement("fontsize", QString::number(property("QCS_fontsize").toDouble()));
218
219 QColor color = property("QCS_color").value<QColor>();
220 s.writeStartElement("color");
221 s.writeTextElement("r", QString::number(color.red()));
222 s.writeTextElement("g", QString::number(color.green()));
223 s.writeTextElement("b", QString::number(color.blue()));
224 s.writeEndElement();
225 color = property("QCS_bgcolor").value<QColor>();
226 s.writeStartElement("bgcolor");
227 s.writeAttribute("mode", property("QCS_bgcolormode").toBool()? "background":"nobackground");
228 s.writeTextElement("r", QString::number(color.red()));
229 s.writeTextElement("g", QString::number(color.green()));
230 s.writeTextElement("b", QString::number(color.blue()));
231 s.writeEndElement();
232 s.writeTextElement("resolution", QString::number(property("QCS_resolution").toDouble(), 'f', 8));
233
234 s.writeTextElement("minimum", QString::number(property("QCS_minimum").toDouble()));
235 s.writeTextElement("maximum", QString::number(property("QCS_maximum").toDouble()));
236 // s.writeTextElement("bordermode", property("QCS_bordermode").toString());
237 // s.writeTextElement("borderradius", QString::number(property("QCS_borderradius").toInt()));
238 // s.writeTextElement("borderwidth", QString::number(property("QCS_borderwidth").toInt()));
239 s.writeStartElement("randomizable");
240 s.writeAttribute("group", QString::number(property("QCS_randomizableGroup").toInt()));
241 s.writeCharacters(property("QCS_randomizable").toBool() ? "true": "false");
242 s.writeEndElement();
243 s.writeTextElement("value", QString::number(m_value));
244 s.writeEndElement();
245
246 #ifdef USE_WIDGET_MUTEX
247 widgetLock.unlock();
248 #endif
249 return xmlText;
250 }
251
252 QString QuteSpinBox::getWidgetType()
253 {
254 return QString("BSBSpinBox");
255 }
256
257 void QuteSpinBox::refreshWidget()
258 {
259 #ifdef USE_WIDGET_MUTEX
260 widgetLock.lockForRead();
261 #endif
262 // double resolution = property("QCS_resolution").toDouble();
263 // double val = resolution * (int(m_value /resolution) + 1.0); // ceil or int won't work here...
264 double val = m_value;
265 // qDebug()<< "QuteSpinBox::refreshWidget()" << val << m_value << resolution;
266 m_valueChanged = false;
267 #ifdef USE_WIDGET_MUTEX
268 widgetLock.unlock();
269 #endif
270 // int cursorPos = static_cast<QLineEdit*>(m_widget)->cursorPosition();
271 m_widget->blockSignals(true);
272 static_cast<QDoubleSpinBox*>(m_widget)->setValue(val);
273 // static_cast<QDoubleSpinBox*>(m_widget)->setCursorPosition(cursorPos);
274 m_widget->blockSignals(false);
275 }
276
277 void QuteSpinBox::applyInternalProperties()
278 {
279 QuteWidget::applyInternalProperties();
280
281 #ifdef USE_WIDGET_MUTEX
282 widgetLock.lockForWrite();
283 #endif
284 auto w = static_cast<QDoubleSpinBox*>(m_widget);
285 w->setRange(property("QCS_minimum").toDouble(),property("QCS_maximum").toDouble());
286 m_value = property("QCS_value").toDouble();
287 double resolution = property("QCS_resolution").toDouble();
288 m_valueChanged = true;
289 int i;
290 for (i=0; i < 8; i++) {// Check for used decimal places.
291 double fractpart, intpart;
292 fractpart = modf (resolution * pow(10, i), &intpart);
293 if (fractpart < 1e-200) {
294 break;
295 }
296 }
297
298 w->setDecimals(i);
299 w->setSingleStep(resolution);
300 Qt::Alignment align;
301 QString alignText = property("QCS_alignment").toString();
302 if (alignText == "left") {
303 align = Qt::AlignLeft|Qt::AlignVCenter;
304 }
305 else if (alignText == "center") {
306 align = Qt::AlignHCenter|Qt::AlignVCenter;
307 }
308 else if (alignText == "right") {
309 align = Qt::AlignRight|Qt::AlignVCenter;
310 }
311 w->setAlignment(align);
312 setTextColor(property("QCS_color").value<QColor>());
313
314 int new_fontSize = 0;
315 int totalHeight = 0;
316 double fontSize = (property("QCS_fontsize").toDouble()*m_fontScaling) + m_fontOffset;
317
318 auto bgstr = property("QCS_bgcolormode").toBool() ?
319 QString("background-color:")+property("QCS_bgcolor").value<QColor>().name()+";" :
320 QString("");
321
322 #ifdef USEFONTPIXELSIZE
323 auto sheet = QString(
324 "QDoubleSpinBox {"
325 "font-family: \"%1\";"
326 "font-size: %2px;"
327 "color: %3;"
328 "%4 }")
329 .arg(property("QCS_font").toString())
330 .arg(QString::number(fontSize))
331 .arg(property("QCS_color").value<QColor>().name())
332 .arg(bgstr);
333 #else
334 while (totalHeight < fontSize + 1) {
335 new_fontSize++;
336 QFont font(property("QCS_font").toString(), new_fontSize);
337 QFontMetricsF fm(font);
338 totalHeight = fm.ascent() + fm.descent();
339 }
340 auto sheet = QString(
341 "QDoubleSpinBox {"
342 "font-family: \"%1\";"
343 "font-size: %2pt;"
344 "color: %3;"
345 "%4 }")
346 .arg(property("QCS_font").toString())
347 .arg(QString::number(new_fontSize))
348 .arg(property("QCS_color").value<QColor>().name())
349 .arg(bgstr);
350
351 #endif
352 m_widget->setStyleSheet(sheet);
353
354 #ifdef USE_WIDGET_MUTEX
355 widgetLock.unlock();
356 #endif
357
358 }
359
360 void QuteSpinBox::createPropertiesDialog()
361 {
362 // qDebug("QuteSpinBox::createPropertiesDialog()");
363 QuteText::createPropertiesDialog();
364 dialog->setWindowTitle("SpinBox");
365 QLabel *label = new QLabel(dialog);
366 label->setText("Resolution");
367 layout->addWidget(label, 4, 0, Qt::AlignRight|Qt::AlignVCenter);
368 resolutionSpinBox = new QDoubleSpinBox(dialog);
369 resolutionSpinBox->setDecimals(6);
370 resolutionSpinBox->setRange(0,999999999999.0);
371 layout->addWidget(resolutionSpinBox, 4, 1, Qt::AlignLeft|Qt::AlignVCenter);
372
373 label = new QLabel(dialog);
374 label->setText("Min =");
375 layout->addWidget(label, 2, 0, Qt::AlignRight|Qt::AlignVCenter);
376 minSpinBox = new QDoubleSpinBox(dialog);
377 minSpinBox->setDecimals(6);
378 minSpinBox->setRange(-999999999999.0, 999999999999.0);
379 layout->addWidget(minSpinBox, 2,1, Qt::AlignLeft|Qt::AlignVCenter);
380 label = new QLabel(dialog);
381 label->setText("Max =");
382 layout->addWidget(label, 2, 2, Qt::AlignRight|Qt::AlignVCenter);
383 maxSpinBox = new QDoubleSpinBox(dialog);
384 maxSpinBox->setDecimals(6);
385 maxSpinBox->setRange(-999999999999.0, 999999999999.0);
386 layout->addWidget(maxSpinBox, 2,3, Qt::AlignLeft|Qt::AlignVCenter);
387 borderRadius->hide();
388 labelPtrs["borderRadius"]->hide();
389 borderWidth->hide();
390 labelPtrs["borderWidth"]->hide();
391 vertAlignmentComboBox->hide();
392 labelPtrs["vertAlign"]->hide();
393 #ifdef USE_WIDGET_MUTEX
394 widgetLock.lockForRead();
395 #endif
396 resolutionSpinBox->setValue(property("QCS_resolution").toDouble());
397 minSpinBox->setValue(property("QCS_minimum").toDouble());
398 maxSpinBox->setValue(property("QCS_maximum").toDouble());
399 text->setText(static_cast<QDoubleSpinBox *>(m_widget)->text());
400 #ifdef USE_WIDGET_MUTEX
401 widgetLock.unlock();
402 #endif
403 }
404
405 void QuteSpinBox::applyProperties()
406 {
407 switch (alignment->currentIndex()) {
408 case 0:
409 setProperty("QCS_alignment", "left");
410 break;
411 case 1:
412 setProperty("QCS_alignment", "center");
413 break;
414 case 2:
415 setProperty("QCS_alignment", "right");
416 break;
417 default:
418 setProperty("QCS_alignment", "");
419 }
420 setProperty("QCS_font", font->currentFont().family());
421 setProperty("QCS_fontsize", fontSize->value());
422 setProperty("QCS_bgcolor", bgColor->palette().color(QPalette::Window));
423 setProperty("QCS_bgcolormode", bg->isChecked());
424 setProperty("QCS_color", textColor->palette().color(QPalette::Window));
425 setProperty("QCS_resolution", resolutionSpinBox->value());
426 setProperty("QCS_value", text->toPlainText().toDouble());
427 setProperty("QCS_maximum", maxSpinBox->value());
428 setProperty("QCS_minimum", minSpinBox->value());
429 // setProperty("QCS_randomizable",false);
430 // setProperty("QCS_bordermode", border->isChecked() ? "border" : "noborder");
431 QuteWidget::applyProperties(); // Must be last so that the widgetChanged signal is last
432 }
433
434 void QuteSpinBox::valueChanged(double value)
435 {
436 #ifdef USE_WIDGET_MUTEX
437 widgetLock.lockForWrite();
438 #endif
439 setInternalValue(value);
440 QPair<QString, double> channelValue(m_channel, value);
441 #ifdef USE_WIDGET_MUTEX
442 widgetLock.unlock();
443 #endif
444 emit newValue(channelValue);
445 }
446
447 void QuteSpinBox::editingFinishedSlot()
448 {
449 double value = static_cast<QDoubleSpinBox*>(m_widget)->value();
450
451 #ifdef USE_WIDGET_MUTEX
452 widgetLock.lockForWrite();
453 #endif
454 setInternalValue(value);
455 QPair<QString, double> channelValue(m_channel, value);
456 #ifdef USE_WIDGET_MUTEX
457 widgetLock.unlock();
458 #endif
459 emit newValue(channelValue);
460 }
461
462 void QuteSpinBox::setInternalValue(double value)
463 {
464 m_value = value;
465 m_valueChanged = true;
466 }