Debian Codegraph @

src/qutemeter.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 "qutemeter.h"
24
25 #include <math.h> //for isnan
26
27 // This class is called meter for historical reasons (MacCsound's widget was called meter)
28 // But it is a meter and a controller widget
29
30 QuteMeter::QuteMeter(QWidget *parent) : QuteWidget(parent)
31 {
32 setGeometry(0,0, parent->width(), parent->height());
33 m_widget = new MeterWidget(this);
34 m_widget->setAutoFillBackground(true);
35 // m_widget->setWindowFlags(Qt::WindowStaysOnTopHint);
36 canFocus(false);
37 // static_cast<MeterWidget *>(m_widget)->setRenderHints(QPainter::Antialiasing);
38 // connect(static_cast<MeterWidget *>(m_widget), SIGNAL(popUpMenu(QPoint)), this, SLOT(popUpMenu(QPoint)));
39 // connect(static_cast<MeterWidget *>(m_widget), SIGNAL(newValues(double, double)), this, SLOT(setValuesFromWidget(double,double)));
40
41 setProperty("QCS_xMin", 0.0);
42 setProperty("QCS_xMax", 1.0);
43 setProperty("QCS_yMin", 0.0);
44 setProperty("QCS_yMax", 1.0);
45
46 setProperty("QCS_type", "fill");
47 setProperty("QCS_pointsize", 1);
48 setProperty("QCS_fadeSpeed", 1.0);
49 setProperty("QCS_mouseControl", "jump");
50 setProperty("QCS_mouseControlAct", "press");
51
52 setProperty("QCS_color", QColor(Qt::green));
53 setProperty("QCS_bgcolor", QColor(30, 30, 30));
54 setProperty("QCS_bgcolormode", true);
55 setProperty("QCS_randomizable", false);
56 setProperty("QCS_randomizableGroup", 0);
57 setProperty("QCS_randomizableMode", "both");
58
59 setProperty("QCS_bordermode", "noborder");
60 setProperty("QCS_borderColor", "#00FF00");
61
62 connect(static_cast<MeterWidget *>(m_widget), SIGNAL(newValue1(double)),
63 this, SLOT(valueChanged(double)));
64 connect(static_cast<MeterWidget *>(m_widget), SIGNAL(newValue2(double)),
65 this, SLOT(value2Changed(double)));
66 m_widget->setMouseTracking(true); // Necessary to pass mouse tracking to widget panel for _MouseX channels
67
68 }
69
70 QuteMeter::~QuteMeter()
71 {
72 }
73
74
75 void QuteMeter::setMidiValue(int value)
76 {
77 double max, min;
78 bool vertical;
79 if (m_widget->width() > m_widget->height()) {
80 vertical = false;
81 }
82 else {
83 vertical = true;
84 }
85 if (vertical) {
86 max = property("QCS_yMax").toDouble();
87 min = property("QCS_yMin").toDouble();
88 }
89 else {
90 max = property("QCS_xMax").toDouble();
91 min = property("QCS_xMin").toDouble();
92 }
93 double newval = min + ((value / 127.0)* (max - min));
94 setValue(newval);
95 QPair<QString, double> channelValue(m_channel, newval);
96 emit newValue(channelValue);
97 }
98
99 QString QuteMeter::getWidgetLine()
100 {
101 #ifdef USE_WIDGET_MUTEX
102 widgetLock.lockForRead();
103 #endif
104 QString line = "ioMeter {" + QString::number(x()) + ", " + QString::number(y()) + "} ";
105 line += "{"+ QString::number(width()) +", "+ QString::number(height()) +"} ";
106 QColor color = property("QCS_color").value<QColor>();
107 line += "{" + QString::number(color.red() * 256)
108 + ", " + QString::number(color.green() * 256)
109 + ", " + QString::number(color.blue() * 256) + "} ";
110 line += "\"" + m_channel + "\" " + QString::number(m_value, 'f', 6) + " ";
111 line += "\"" + m_channel2 + "\" " + QString::number(m_value2, 'f', 6) + " ";
112 line += property("QCS_type").toString() + " ";
113 line += QString::number(property("QCS_pointsize").toInt()) + " ";
114 line += QString::number(property("QCS_fadeSpeed").toInt()) + " ";
115 line += "mouse";
116 // qDebug("QuteMeter::getWidgetLine() %s", line.toStdString().c_str());
117 #ifdef USE_WIDGET_MUTEX
118 widgetLock.unlock();
119 #endif
120 return line;
121 }
122
123 QString QuteMeter::getWidgetXmlText()
124 {
125 xmlText = "";
126 QXmlStreamWriter s(&xmlText);
127 createXmlWriter(s);
128
129 #ifdef USE_WIDGET_MUTEX
130 widgetLock.lockForRead();
131 #endif
132 s.writeTextElement("objectName2", m_channel2);
133 s.writeTextElement("xMin", QString::number(property("QCS_xMin").toDouble(), 'f', 8));
134 s.writeTextElement("xMax", QString::number(property("QCS_xMax").toDouble(), 'f', 8));
135 s.writeTextElement("yMin", QString::number(property("QCS_yMin").toDouble(), 'f', 8));
136 s.writeTextElement("yMax", QString::number(property("QCS_yMax").toDouble(), 'f', 8));
137 s.writeTextElement("xValue", QString::number(m_value, 'f', 8));
138 s.writeTextElement("yValue", QString::number(m_value2, 'f', 8));
139
140 s.writeTextElement("type", property("QCS_type").toString());
141 s.writeTextElement("pointsize", QString::number(property("QCS_pointsize").toInt()));
142 s.writeTextElement("fadeSpeed",QString::number(property("QCS_fadeSpeed").toDouble(),'f',8));
143 s.writeStartElement("mouseControl");
144 s.writeAttribute("act", property("QCS_mouseControlAct").toString());
145 s.writeCharacters(property("QCS_mouseControl").toString());
146 s.writeEndElement();
147
148 s.writeTextElement("bordermode", property("QCS_bordermode").toString());
149 s.writeTextElement("borderColor", property("QCS_borderColor").toString());
150
151 QColor color = property("QCS_color").value<QColor>();
152 s.writeStartElement("color");
153 s.writeTextElement("r", QString::number(color.red()));
154 s.writeTextElement("g", QString::number(color.green()));
155 s.writeTextElement("b", QString::number(color.blue()));
156 s.writeEndElement();
157 s.writeStartElement("randomizable");
158 s.writeAttribute("mode", property("QCS_randomizableMode").toString());
159 s.writeAttribute("group", QString::number(property("QCS_randomizableGroup").toInt()));
160 s.writeCharacters(property("QCS_randomizable").toBool() ? "true":"false");
161 s.writeEndElement();
162
163 QColor bgcolor = property("QCS_bgcolor").value<QColor>();
164 s.writeStartElement("bgcolor");
165 s.writeTextElement("r", QString::number(bgcolor.red()));
166 s.writeTextElement("g", QString::number(bgcolor.green()));
167 s.writeTextElement("b", QString::number(bgcolor.blue()));
168 s.writeEndElement();
169
170 s.writeTextElement("bgcolormode", property("QCS_bgcolormode").toBool()?"true":"false");
171
172 s.writeEndElement();
173 #ifdef USE_WIDGET_MUTEX
174 widgetLock.unlock();
175 #endif
176 return xmlText;
177 }
178
179 QString QuteMeter::getWidgetType()
180 {
181 // QString type = static_cast<MeterWidget *>(m_widget)->getType();
182 auto type = static_cast<MeterWidget*>(m_widget)->getMeterType();
183 if (type == MeterWidgetType::Fill ||
184 type == MeterWidgetType::Line ||
185 type == MeterWidgetType::Llif) {
186 return QString("BSBController");
187 }
188 // else if (type == "crosshair" or type == "point") {
189 // }
190 return QString("BSBController");
191 }
192
193 QString QuteMeter::getCabbageLine()
194 {
195 // QString type = static_cast<MeterWidget *>(m_widget)->getType();
196 auto type = static_cast<MeterWidget*>(m_widget)->getMeterType();
197 if (!(type == MeterWidgetType::Crosshair || type == MeterWidgetType::Point)) {
198 qDebug()<<"Meter can be converted to XYpad only if the type is crosshair or point";
199 return QString();
200 }
201 #ifdef USE_WIDGET_MUTEX
202 widgetLock.lockForWrite();
203 #endif
204 QString line = "xypad ";
205 line += QString("bounds(%1,%2,%3,%4), ").arg(x()).arg(y()).arg(width()).arg(height());
206 line += QString("channel(\"%1\",\"%2\"), ").arg(m_channel).arg(m_channel2);
207 line += QString("rangex(%1, %2, %3), ").arg(property("QCS_xMin").toDouble()).arg(property("QCS_xMax").toDouble()).arg(m_value);
208 line += QString("rangey(%1, %2, %3)").arg(property("QCS_yMin").toDouble()).arg(property("QCS_yMax").toDouble()).arg(m_value2);
209
210 //NB! meter does not have midi2 yet!
211 if (property("QCS_midicc").toInt() >= 0 && property("QCS_midichan").toInt()>0) { // insert only if midi channel is above 0
212 line += ", midiCtrl(\"" + QString::number(property("QCS_midichan").toInt()) + ",";
213 line += QString::number(property("QCS_midicc").toInt()) + "\")";
214 }
215 #ifdef USE_WIDGET_MUTEX
216 widgetLock.unlock();
217 #endif
218 return line;
219 }
220
221 QString QuteMeter::getQml()
222 {
223 QString qml = QString();
224 QString type = property("QCS_type").toString();
225 if ( type != "fill") {
226 qDebug() << "Currently exporting only meter controllers (type \"fill\") is supported";
227 return qml;
228 }
229 #ifdef USE_WIDGET_MUTEX
230 widgetLock.lockForWrite();
231 #endif
232 // todo: crosshair jm
233
234 bool vertical;
235 if (m_widget->width() > m_widget->height()) {
236 vertical = false;
237 } else {
238 vertical = true;
239 }
240 double max, min, value;
241 QString channel;
242 if (vertical) {
243 max = property("QCS_yMax").toDouble();
244 min = property("QCS_yMin").toDouble();
245 channel = m_channel2;
246 value = m_value2;
247 }
248 else {
249 max = property("QCS_xMax").toDouble();
250 min = property("QCS_xMin").toDouble();
251 channel = m_channel;
252 value = m_value;
253 }
254
255 qml=QString(R"(
256 Rectangle {
257 x: %1 * scaleItem.scale
258 y: %2 * scaleItem.scale
259 width: %3 * scaleItem.scale
260 height: %4 * scaleItem.scale
261 color: "black"
262 border.color: "black"
263 border.width: 2
264 property double min: %5
265 property double max: %6
266 property double value: %7
267 property string channel: "%8"
268 property bool isVertical: height>width
269
270 MouseArea {
271 anchors.fill: parent
272
273 function setValue() { // value from min..max
274 var value;
275 if (parent.isVertical) {
276 value = parent.min + (1 - (mouseY/parent.height)) * (parent.max-parent.min);
277 } else {
278 value = parent.min + (mouseX/parent.width) * (parent.max-parent.min);
279 }
280
281 if (value>parent.max) value=parent.max;
282 if (value<parent.min) value=parent.min;
283 if (typeof(csound) !== 'undefined' ) csound.setControlChannel(parent.channel, value);
284
285 parent.value = value;
286 }
287
288 onClicked: setValue();
289 onMouseYChanged: setValue();
290 }
291
292 Rectangle {
293 property double relativeValue: (parent.value-parent.min)/(parent.max-parent.min)
294 width: parent.isVertical ? parent.width - 2*parent.border.width :
295 (parent.width-2*parent.border.width) *relativeValue
296 height: parent.isVertical ? (parent.height-2*parent.border.width) *relativeValue :
297 parent.height - 2*parent.border.width
298
299 anchors.margins: parent.border.width
300 anchors.bottom: parent.bottom
301 anchors.left: parent.left
302 color: "%9"
303 }
304 }
305 )").arg(x()).arg(y()).arg(width()).arg(height()).arg(min).arg(max).arg(value).
306 arg(channel).arg(property("QCS_color").toString());
307
308
309 #ifdef USE_WIDGET_MUTEX
310 widgetLock.unlock();
311 #endif
312
313 return qml;
314
315 }
316
317 void QuteMeter::createPropertiesDialog()
318 {
319 QuteWidget::createPropertiesDialog();
320
321 #ifdef USE_WIDGET_MUTEX
322 widgetLock.lockForRead();
323 #endif
324 dialog->setWindowTitle("Controller");
325 channelLabel->setText(tr("Horizontal Channel ="));
326 channelLabel->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
327
328 QLabel *label = new QLabel(tr("Vertical Channel ="), dialog);
329 layout->addWidget(label, 4, 0, Qt::AlignRight|Qt::AlignVCenter);
330
331 name2LineEdit = new QLineEdit(dialog);
332 name2LineEdit->setText(getChannel2Name());
333 // name2LineEdit->setMinimumWidth(320);
334 layout->addWidget(name2LineEdit, 4, 1, 1, 3, Qt::AlignLeft|Qt::AlignVCenter);
335 // if (static_cast<MeterWidget *>(m_widget)->getType() != "point" and ((MeterWidget *)m_widget)->getType() != "crosshair") {
336 // if (((MeterWidget *)m_widget)->m_vertical) {
337 // channelLabel->setEnabled(false);
338 // nameLineEdit->setEnabled(false);
339 // }
340 // else {
341 // label->setEnabled(false);
342 // name2LineEdit->setEnabled(false);
343 // }
344 // }
345 auto w = static_cast<MeterWidget *>(m_widget);
346 label = new QLabel("Color", dialog);
347 label->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
348 layout->addWidget(label, 5, 0, Qt::AlignRight|Qt::AlignVCenter);
349
350 colorButton = new SelectColorButton(dialog);
351 colorButton->setColor(static_cast<MeterWidget *>(m_widget)->getColor());
352 layout->addWidget(colorButton, 5,1, Qt::AlignLeft|Qt::AlignVCenter);
353
354 bgColorCheckBox = new QCheckBox("Background", dialog);
355 bgColorCheckBox->setChecked(property("QCS_bgcolormode").toBool());
356 layout->addWidget(bgColorCheckBox, 5, 2, Qt::AlignVCenter);
357
358 bgColorButton = new SelectColorButton(dialog);
359 bgColorButton->setColor(static_cast<MeterWidget *>(m_widget)->getBgColor());
360 layout->addWidget(bgColorButton, 5, 3, Qt::AlignLeft|Qt::AlignVCenter);
361
362 connect(bgColorCheckBox, SIGNAL(toggled(bool)), bgColorButton, SLOT(setEnabled(bool)));
363
364 borderCheckBox = new QCheckBox(tr("Border"), dialog);
365 borderCheckBox->setCheckState(
366 property("QCS_bordermode").toString()=="border" ? Qt::Checked : Qt::Unchecked);
367 layout->addWidget(borderCheckBox, 5, 4, Qt::AlignLeft|Qt::AlignVCenter);
368
369 borderColorButton = new SelectColorButton(dialog);
370 borderColorButton->setColor(QColor(property("QCS_borderColor").toString()));
371 layout->addWidget(borderColorButton, 5, 5, Qt::AlignLeft|Qt::AlignVCenter);
372
373 label = new QLabel(dialog);
374 label->setText("Type");
375 label->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
376 layout->addWidget(label, 6, 0, Qt::AlignRight|Qt::AlignVCenter);
377
378 typeComboBox = new QComboBox(dialog);
379 typeComboBox->addItem("fill");
380 typeComboBox->addItem("llif");
381 typeComboBox->addItem("line");
382 typeComboBox->addItem("crosshair");
383 typeComboBox->addItem("point");
384 typeComboBox->setCurrentIndex(typeComboBox->findText(w->getType()));
385 layout->addWidget(typeComboBox, 6, 1, Qt::AlignLeft|Qt::AlignVCenter);
386
387 label = new QLabel(dialog);
388 label->setText("Point/Line Width:");
389 label->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
390 layout->addWidget(label, 7, 0, Qt::AlignRight|Qt::AlignVCenter);
391
392 pointSizeSpinBox = new QSpinBox(dialog);
393 pointSizeSpinBox->unsetLocale();
394 pointSizeSpinBox->setValue(w->getPointSize());
395 pointSizeSpinBox->setToolTip("Size of the point / line if applicable");
396 layout->addWidget(pointSizeSpinBox, 7,1, Qt::AlignLeft|Qt::AlignVCenter);
397
398 if(w->getMeterType() != MeterWidgetType::Point &&
399 w->getMeterType() != MeterWidgetType::Line) {
400 // label->setEnabled(false);
401 pointSizeSpinBox->setEnabled(false);
402 }
403
404 connect(typeComboBox, SIGNAL(currentIndexChanged(int)),
405 this, SLOT(checkTypeComboBox()));
406
407 label = new QLabel(dialog);
408 label->setText("Fade speed:");
409 label->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
410 layout->addWidget(label, 7, 2, Qt::AlignRight|Qt::AlignVCenter);
411 fadeSpeedSpinBox = new QSpinBox(dialog);
412 fadeSpeedSpinBox->unsetLocale();
413 fadeSpeedSpinBox->setValue(property("QCS_fadeSpeed").toDouble());
414 fadeSpeedSpinBox->setRange(0, 1000);
415 layout->addWidget(fadeSpeedSpinBox, 7,3, Qt::AlignLeft|Qt::AlignVCenter);
416 label->setEnabled(false);
417 fadeSpeedSpinBox->setEnabled(false);
418
419 label = new QLabel(dialog);
420 label->setText("Behavior");
421 label->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
422 label->setEnabled(false);
423 layout->addWidget(label, 8, 0, Qt::AlignRight|Qt::AlignVCenter);
424
425 behaviorComboBox = new QComboBox(dialog);
426 behaviorComboBox->addItem("jump");
427 behaviorComboBox->addItem("relative");
428 behaviorComboBox->addItem("none");
429 behaviorComboBox->setCurrentIndex(
430 behaviorComboBox->findText(property("QCS_mouseControl").toString()));
431 behaviorComboBox->setEnabled(false);
432 layout->addWidget(behaviorComboBox,8, 1, Qt::AlignLeft|Qt::AlignVCenter);
433
434 label = new QLabel(dialog);
435 label->setText("Min X value =");
436 layout->addWidget(label,9, 0, Qt::AlignRight|Qt::AlignVCenter);
437
438 m_xMinBox = new QDoubleSpinBox(dialog);
439 m_xMinBox->setRange(-999999999.0, 999999999.0);
440 m_xMinBox->setValue(property("QCS_xMin").toDouble());
441 layout->addWidget(m_xMinBox, 9,1, Qt::AlignLeft|Qt::AlignVCenter);
442
443 label = new QLabel(dialog);
444 label->setText("Max X value =");
445 layout->addWidget(label, 9, 2, Qt::AlignRight|Qt::AlignVCenter);
446
447 m_xMaxBox = new QDoubleSpinBox(dialog);
448 m_xMaxBox->setRange(-999999999.0, 999999999.0);
449 m_xMaxBox->setValue(property("QCS_xMax").toDouble());
450 layout->addWidget(m_xMaxBox, 9,3, Qt::AlignLeft|Qt::AlignVCenter);
451
452 label = new QLabel(dialog);
453 label->setText("Min Y value =");
454 layout->addWidget(label, 10, 0, Qt::AlignRight|Qt::AlignVCenter);
455
456 m_yMinBox = new QDoubleSpinBox(dialog);
457 m_yMinBox->setRange(-999999999.0, 999999999.0);
458 m_yMinBox->setValue(property("QCS_yMin").toDouble());
459 layout->addWidget(m_yMinBox, 10, 1, Qt::AlignLeft|Qt::AlignVCenter);
460
461 label = new QLabel(dialog);
462 label->setText("Max Y value =");
463 layout->addWidget(label, 10, 2, Qt::AlignRight|Qt::AlignVCenter);
464
465 m_yMaxBox = new QDoubleSpinBox(dialog);
466 m_yMaxBox->setRange(-999999999.0, 999999999.0);
467 m_yMaxBox->setValue(property("QCS_yMax").toDouble());
468 layout->addWidget(m_yMaxBox, 10,3, Qt::AlignLeft|Qt::AlignVCenter);
469
470 setProperty("QCS_xValue", m_value);
471 setProperty("QCS_yValue", m_value2);
472 #ifdef USE_WIDGET_MUTEX
473 widgetLock.unlock();
474 #endif
475
476 }
477
478 void QuteMeter::checkTypeComboBox() {
479 qDebug() << "checkTypeComboBox";
480 QString metertype = typeComboBox->currentText();
481 if(metertype == "line" || metertype == "point" )
482 pointSizeSpinBox->setEnabled(true);
483 else {
484 pointSizeSpinBox->setEnabled(false);
485 }
486 }
487
488 void QuteMeter::refreshWidget()
489 {
490 #ifdef USE_WIDGET_MUTEX
491 widgetLock.lockForRead();
492 #endif
493 double val1 = m_value;
494 double val2 = m_value2;
495 if (val1 < property("QCS_xMin").toDouble()) { // Must check this in case number is -inf
496 val1 = property("QCS_xMin").toDouble();
497 }
498 if (val2 < property("QCS_yMin").toDouble()) {
499 val2 = property("QCS_yMin").toDouble();
500 }
501 m_widget->blockSignals(true);
502 static_cast<MeterWidget *>(m_widget)->setValues(val1, val2);
503 m_widget->blockSignals(false);
504 m_valueChanged = false;
505 m_value2Changed = false;
506 #ifdef USE_WIDGET_MUTEX
507 widgetLock.unlock();
508 #endif
509 }
510
511 void QuteMeter::applyProperties()
512 {
513 // setProperty("QCS_objectName", nameLineEdit->text());
514 #ifdef USE_WIDGET_MUTEX
515 widgetLock.lockForRead();
516 #endif
517 // Only properties should be changed here as applyInternalProperties takes care of the rest
518 setProperty("QCS_objectName2", name2LineEdit->text());
519 // setProperty("QCS_color", colorButton->palette().color(QPalette::Window));
520 setProperty("QCS_color", colorButton->getColor());
521 // setProperty("QCS_bgcolor", bgColorButton->palette().color(QPalette::Window));
522 setProperty("QCS_bgcolor", bgColorButton->getColor());
523 setProperty("QCS_bgcolormode", bgColorCheckBox->isChecked());
524 setProperty("QCS_type", typeComboBox->currentText());
525 setProperty("QCS_pointsize", pointSizeSpinBox->value());
526 setProperty("QCS_fadeSpeed", fadeSpeedSpinBox->value());
527 setProperty("QCS_mouseControl", behaviorComboBox->currentText());
528 setProperty("QCS_xMin", m_xMinBox->value());
529 setProperty("QCS_xMax", m_xMaxBox->value());
530 setProperty("QCS_yMin", m_yMinBox->value());
531 setProperty("QCS_yMax", m_yMaxBox->value());
532 setProperty("QCS_bordermode", borderCheckBox->checkState()?"border":"noborder");
533 setProperty("QCS_borderColor", borderColorButton->getColor().name());
534 #ifdef USE_WIDGET_MUTEX
535 widgetLock.unlock();
536 #endif
537 QuteWidget::applyProperties(); //Must be last to make sure the widgetChanged signal is last
538 }
539
540 void QuteMeter::setWidgetGeometry(int x,int y,int width,int height)
541 {
542 QuteWidget::setWidgetGeometry(x,y,width, height);
543 /* In MacCsound, meter widgets have an offset of about five pixels in every border
544 This has proven problematic in Qt, as the graphicsScene*/
545 m_widget->blockSignals(true);
546 static_cast<MeterWidget *>(m_widget)->setWidgetGeometry(0,0,width, height);
547 m_widget->blockSignals(false);
548 }
549
550 void QuteMeter::applyInternalProperties()
551 {
552 QuteWidget::applyInternalProperties();
553 auto meter = static_cast<MeterWidget *>(m_widget);
554 meter->setColor(property("QCS_color").value<QColor>());
555 meter->setBgColor(property("QCS_bgcolor").value<QColor>());
556 meter->showBackground(property("QCS_bgcolormode").toBool());
557 meter->setType(property("QCS_type").toString());
558 meter->setPointSize(property("QCS_pointsize").toInt());
559 meter->setRanges(property("QCS_xMin").toDouble(),
560 property("QCS_xMax").toDouble(),
561 property("QCS_yMin").toDouble(),
562 property("QCS_yMax").toDouble());
563 meter->showBorder(property("QCS_bordermode").toString()=="border");
564 meter->setBorderColor(QColor(property("QCS_borderColor").toString()));
565
566 m_value = property("QCS_xValue").toDouble();
567 m_value2 = property("QCS_yValue").toDouble();
568 // if (m_value2 == m_value) {
569 // qDebug() << "Warning! Controller Widget can't have the same name for both channels!";
570 // m_value2 = "";
571 // }
572 setValue(m_value);
573 setValue2(m_value2);
574 }
575
576
577 void QuteMeter::valueChanged(double value1)
578 {
579 // QString type = property("QCS_type").toString();
580 // qDebug() << "QuteMeter::valueChanged " << value1;
581
582 #ifdef USE_WIDGET_MUTEX
583 widgetLock.lockForRead();
584 #endif
585 m_value = value1;
586 m_valueChanged = true;
587 #ifdef USE_WIDGET_MUTEX
588 widgetLock.unlock();
589 #endif
590 QPair<QString, double> channelValue(m_channel, m_value);
591 emit newValue(channelValue);
592 }
593
594 void QuteMeter::value2Changed(double value2)
595 {
596 // QString type = property("QCS_type").toString();
597 // qDebug() << "QuteMeter::value2Changed "<< value2;
598
599 #ifdef USE_WIDGET_MUTEX
600 widgetLock.lockForRead();
601 #endif
602 m_value2 = value2;
603 m_value2Changed = true;
604 #ifdef USE_WIDGET_MUTEX
605 widgetLock.unlock();
606 #endif
607 QPair<QString, double> channelValue(m_channel2, m_value2);
608 emit newValue(channelValue);
609 }
610
611 /* Meter Widget ----------------------------------------*/
612
613 MeterWidget::MeterWidget(QWidget *parent) : QGraphicsView(parent)
614 {
615 auto t0 = std::chrono::high_resolution_clock::now();
616 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
617 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
618 setAlignment(Qt::AlignLeft | Qt::AlignTop);
619 setTransformationAnchor(QGraphicsView::NoAnchor);
620 setInteractive (true);
621 // m_scene = new QGraphicsScene(5,5, 20, 20, this);
622 m_scene = new QGraphicsScene(0, 0, 20, 20, this);
623 m_scene->setBackgroundBrush(Qt::black);
624 m_scene->setSceneRect(0, 0, 20, 20);
625 m_showBackground = true;
626 m_bgcolor = Qt::black;
627 setFrameStyle(QFrame::NoFrame);
628
629 setScene(m_scene);
630 m_mouseDown = false;
631 auto borderPen = QPen(QColor(Qt::green).darker(150), 2);
632 auto blockPen = Qt::NoPen;
633 m_block = m_scene->addRect(0, 0, 0, 0, blockPen, QBrush(Qt::green));
634 m_point = m_scene->addEllipse(0, 0, 0, 0, QPen(Qt::green) , QBrush(Qt::green));
635 m_vline = m_scene->addLine(0, 0, 0, 0, QPen(Qt::green));
636 m_hline = m_scene->addLine(0, 0, 0, 0, QPen(Qt::green));
637 m_border = m_scene->addRect(m_scene->sceneRect(), borderPen, Qt::NoBrush);
638 m_border->hide();
639 // auto t1 = std::chrono::high_resolution_clock::now();
640 // QDEBUG << "... MeterWidget constructor " << std::chrono::duration<double, std::milli>(t1-t0).count() << "ms";
641
642 }
643
644 MeterWidget::~MeterWidget()
645 {
646 }
647
648 QColor MeterWidget::getColor()
649 {
650 return m_block->brush().color();
651 }
652
653 QColor MeterWidget::getBgColor()
654 {
655 return m_scene->backgroundBrush().color();
656 }
657
658 QColor MeterWidget::getBorderColor()
659 {
660 return m_border->pen().color();
661 }
662
663
664 void MeterWidget::setValue(double value)
665 {
666 // qDebug() << "MeterWidget::setValue " <<value;
667 // if (isnan(value) != 0)
668 // return;
669 // if (value > m_xmax) {
670 // value = m_xmax;
671 // }
672 // if (value < m_xmin) {
673 // value = m_xmin;
674 // }
675 if (m_value == value) {
676 return;
677 }
678 mutex.lock();
679 m_value = value;
680
681 double portionx = (m_value - m_xmin) / (m_xmax - m_xmin);
682 double portiony = (m_value2 - m_ymin) / (m_ymax - m_ymin);
683
684 if (m_metertype == MeterWidgetType::Fill && !m_vertical) {
685 m_block->setRect(0, 0, portionx*width(), height());
686 }
687 else if (m_metertype == MeterWidgetType::Llif && !m_vertical) {
688 m_block->setRect(portionx*width(), 0, width(), height());
689 }
690 else if (m_metertype == MeterWidgetType::Line && !m_vertical) {
691 m_vline->setLine(portionx*width(), 0, portionx*width(), height());
692 }
693 else {
694 m_vline->setLine(portionx*width(), 0, portionx*width(), height());
695 m_point->setRect(portionx*width() - m_pointSize/2.0,
696 height()*(1 - portiony) - m_pointSize/2.0,
697 m_pointSize,
698 m_pointSize);
699 }
700 mutex.unlock();
701 // emit valueChanged(m_value);
702 }
703
704 void MeterWidget::setValue2(double value)
705 {
706 // qDebug() << "MeterWidget::setValue2 " << value;
707 // if (value > m_ymax) {
708 // value = m_ymax;
709 // }
710 // if (value < m_ymin) {
711 // value = m_ymin;
712 // }
713 if (m_value2 == value) {
714 return;
715 }
716 mutex.lock();
717 m_value2 = value;
718 double portionx = (m_value - m_xmin) / (m_xmax - m_xmin);
719 double portiony = (m_value2 - m_ymin) / (m_ymax - m_ymin);
720 if (m_metertype == MeterWidgetType::Fill && m_vertical) {
721 m_block->setRect(0, (1-portiony)*height(), width(), height());
722 }
723 else if (m_metertype == MeterWidgetType::Llif && m_vertical) {
724 m_block->setRect(0, 0, width(), (1-portiony)*height());
725 }
726 else if (m_metertype == MeterWidgetType::Line && m_vertical) {
727 m_hline->setLine(0, (1-portiony)*height(), width(), (1-portiony)*height());
728 // m_hline->setLine(portiony*width(), 0 ,portiony*width(), height());
729 }
730 else {
731 m_hline->setLine(0, (1-portiony)*height(), width(), (1-portiony)*height());
732 m_point->setRect(portionx*width() - m_pointSize/2.0,
733 (1-portiony)*height() - m_pointSize/2.0,
734 m_pointSize,
735 m_pointSize);
736 }
737 mutex.unlock();
738 // emit value2Changed(m_value2);
739 }
740
741 void MeterWidget::setValues(double value1, double value2)
742 {
743 if (m_value2 == value2 && m_value == value1) {
744 return;
745 }
746 mutex.lock();
747 m_value = value1;
748 m_value2 = value2;
749 double portionx = (m_value - m_xmin) / (m_xmax - m_xmin);
750 double portiony = (m_value2 - m_ymin) / (m_ymax - m_ymin);
751 switch(m_metertype) {
752 case MeterWidgetType::Fill:
753 if (!m_vertical)
754 m_block->setRect(0, 0, portionx*width(), height());
755 else
756 m_block->setRect(0, (1-portiony)*height(), width(), portiony * height());
757 break;
758 case MeterWidgetType::Llif:
759 if (!m_vertical)
760 m_block->setRect(portionx*width(),0, width(), height());
761 else
762 m_block->setRect(0, 0, width(), (1-portiony)*height());
763 break;
764 case MeterWidgetType::Line:
765 if (!m_vertical)
766 m_vline->setLine(portionx*width(), 0 ,portionx*width(), height());
767 else
768 m_hline->setLine(0, (1-portiony)*height(), width(), (1-portiony)*height());
769 break;
770 default:
771 m_vline->setLine(portionx*width(), 0 ,portionx*width(), height());
772 m_hline->setLine(0, (1-portiony)*height(), width(), (1-portiony)*height());
773 m_point->setRect(portionx*width() - (m_pointSize/2.0),
774 (1-portiony)*height() - (m_pointSize/2.0),
775 m_pointSize,
776 m_pointSize);
777 }
778 mutex.unlock();
779
780 }
781
782 void MeterWidget::setType(QString type)
783 {
784 // qDebug() << "MeterWidget::setType " << type << m_vertical;
785 if (type == "fill") {
786 m_type = type;
787 m_metertype = MeterWidgetType::Fill;
788 m_block->show();
789 m_point->hide();
790 m_vline->hide();
791 m_hline->hide();
792 setRenderHint(QPainter::Antialiasing, false);
793 }
794 else if (type == "llif") {
795 m_metertype = MeterWidgetType::Llif;
796 m_type = type;
797 m_block->show();
798 m_point->hide();
799 m_vline->hide();
800 m_hline->hide();
801 }
802 else if (type == "line") {
803 m_metertype = MeterWidgetType::Line;
804 m_type = type;
805 m_block->hide();
806 m_point->hide();
807 if (m_vertical) {
808 m_vline->hide();
809 m_hline->show();
810 }
811 else {
812 m_vline->show();
813 m_hline->hide();
814 }
815 }
816 else if (type == "crosshair") {
817 m_metertype = MeterWidgetType::Crosshair;
818 m_type = type;
819 m_block->hide();
820 m_point->hide();
821 m_vline->show();
822 m_hline->show();
823 }
824 else if (type == "point") {
825 m_metertype = MeterWidgetType::Point;
826 m_type = type;
827 m_block->hide();
828 m_point->show();
829 m_vline->hide();
830 m_hline->hide();
831 setRenderHints(QPainter::Antialiasing);
832 }
833 }
834
835 void MeterWidget::setRanges(double xmin, double xmax, double ymin, double ymax)
836 {
837 m_xmin = xmin;
838 m_xmax = xmax;
839 m_ymin = ymin;
840 m_ymax = ymax;
841 }
842
843 void MeterWidget::setPointSize(int size)
844 {
845 m_pointSize = size;
846 m_point->setRect(m_value2*width()- (m_pointSize/2.0),
847 (1-m_value)*height()- (m_pointSize/2.0),
848 m_pointSize,
849 m_pointSize);
850 auto pen = m_hline->pen();
851 pen.setWidth(size);
852 m_hline->setPen(pen);
853 m_vline->setPen(pen);
854 }
855
856 void MeterWidget::setColor(QColor color)
857 {
858 m_block->setBrush(QBrush(color));
859 // auto pen = m_border->pen();
860 // pen.setColor(color);
861 // m_border->setPen(pen);
862 m_point->setPen(QPen(Qt::NoPen));
863 m_point->setBrush(QBrush(color));
864 m_vline->setPen(QPen(color));
865 m_hline->setPen(QPen(color));
866 }
867
868 void MeterWidget::setBgColor(QColor color) {
869 m_scene->setBackgroundBrush(color);
870 m_bgcolor = color;
871 }
872
873 void MeterWidget::setBorderColor(QColor color) {
874 auto pen = m_border->pen();
875 pen.setColor(color);
876 m_border->setPen(pen);
877 }
878
879 void MeterWidget::showBackground(bool show) {
880 if(!show) {
881 m_scene->setBackgroundBrush(Qt::NoBrush);
882 this->setStyleSheet("background: transparent");
883 } else {
884 // m_scene->setBackgroundBrush(m_bgcolor);
885 this->setStyleSheet("background: " + m_bgcolor.name());
886 }
887 m_showBackground = show;
888 }
889
890 void MeterWidget::setWidgetGeometry(int x,int y,int width,int height)
891 {
892 m_scene->setSceneRect(0,0, width, height);
893 // m_border->setRect(0, 0, width-1, height-1);
894 m_border->setRect(0, 0, width, height);
895 setSceneRect(0,0, width, height);
896 setGeometry(x,y,width, height);
897 setMaximumSize(width, height);
898 m_vertical = width <= height;
899 setType(m_type); // update widgets which depend on verticality
900 }
901
902 void MeterWidget::mouseMoveEvent(QMouseEvent* event)
903 {
904 if (event->buttons() & Qt::LeftButton) {
905 // if (event->x() > 0 and event->x()< width() and
906 // event->y() > 0 and event->y()< height())
907 double newhor = m_xmin + (m_xmax - m_xmin) * (double)event->x()/width();
908 double newvert = m_ymin + (m_ymax - m_ymin) * (1-((double)event->y()/height()));
909 if (newhor > m_xmax) {
910 newhor = m_xmax;
911 }
912 else if (newhor < m_xmin) {
913 newhor = m_xmin;
914 }
915 if (newvert > m_ymax) {
916 newvert = m_ymax;
917 }
918 else if (newvert < m_ymin) {
919 newvert = m_ymin;
920 }
921 switch(m_metertype) {
922 case MeterWidgetType::Fill:
923 case MeterWidgetType::Line:
924 case MeterWidgetType::Llif:
925 if (m_vertical)
926 emit newValue2(newvert);
927 else
928 emit newValue1(newhor);
929 break;
930 case MeterWidgetType::Crosshair:
931 case MeterWidgetType::Point:
932 emit newValue1(newhor);
933 emit newValue2(newvert);
934 break;
935 }
936 }
937 }
938
939 void MeterWidget::mousePressEvent(QMouseEvent* event)
940 {
941 if (event->buttons() & Qt::LeftButton) {
942 // if (event->x() > 0 and event->x()< width() and
943 // event->y() > 0 and event->y()< height())
944 double newhor = m_xmin + (m_xmax - m_xmin) * (double)event->x()/width();
945 double newvert = m_ymin + (m_ymax - m_ymin) * (1-((double)event->y()/height()));
946 switch(m_metertype) {
947 case MeterWidgetType::Fill:
948 case MeterWidgetType::Line:
949 case MeterWidgetType::Llif:
950 if (m_vertical)
951 emit newValue2(newvert);
952 else
953 emit newValue1(newhor);
954 break;
955 case MeterWidgetType::Crosshair:
956 case MeterWidgetType::Point:
957 emit newValue1(newhor);
958 emit newValue2(newvert);
959 }
960 }
961 }
962