Debian Codegraph @

src/qutegraph.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 "qutegraph.h"
24 #include "curve.h"
25 #include <cmath>
26 #include <QPalette>
27
28
29 #include <QColorDialog>
30
31
32 enum CsoundEngineStatus {
33 Running=0,
34 UserDataNotSet,
35 CsoundInstanceNotSet,
36 EngineNotSet,
37 NotRunning
38 };
39
40
41 inline CsoundEngineStatus csoundEngineStatus(CsoundUserData *ud) {
42 if(ud == nullptr) {
43 return CsoundEngineStatus::UserDataNotSet;
44 }
45 if(ud->csound == nullptr) {
46 return CsoundEngineStatus::CsoundInstanceNotSet;
47 }
48 if(ud->csEngine == nullptr) {
49 return CsoundEngineStatus::EngineNotSet;
50 }
51 if(!ud->csEngine->isRunning()) {
52 return CsoundEngineStatus::NotRunning;
53 }
54 return CsoundEngineStatus::Running;
55 }
56
57
58 // -----------------------------------------------------------------------------------------
59
60 QuteGraph::QuteGraph(QWidget *parent) : QuteWidget(parent)
61 {
62 m_widget = new StackedLayoutWidget(this);
63 // m_widget->setAutoFillBackground(true);
64 // m_widget->show();
65
66 m_numticksY = 6;
67 m_label = new QLabel(this);
68 QPalette palette = m_widget->palette();
69 palette.setColor(QPalette::WindowText, QColor(150, 150, 150));
70 m_label->setPalette(palette);
71 // m_label->setText("");
72 m_label->setFont(QFont({"Helvetica", 7}));
73 m_label->move(120, -4);
74 m_label->resize(500, 25);
75 m_label->setFocusPolicy(Qt::NoFocus);
76
77 m_pageComboBox = new QComboBox(this);
78 m_pageComboBox->setMinimumWidth(120);
79 m_pageComboBox->setMaximumHeight(14);
80 m_pageComboBox->setFont(QFont({"Sans", 7}));
81 m_pageComboBox->setFocusPolicy(Qt::NoFocus);
82 m_pageComboBox->setStyleSheet("QComboBox QAbstractItemView"
83 "{ min-width: 150px; }");
84 // m_pageComboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
85 m_pageComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
86 m_drawGrid = true;
87 m_drawTableInfo = true;
88 m_showScrollbars = true;
89 canFocus(false);
90 connect(m_pageComboBox, SIGNAL(currentIndexChanged(int)),
91 this, SLOT(indexChanged(int)));
92 polygons.clear();
93
94 QPalette Pal(this->palette());
95 // set black background
96 Pal.setColor(QPalette::Background, Qt::black);
97 this->setAutoFillBackground(true);
98 this->setPalette(Pal);
99 this->setStyleSheet("QFrame { border: 0px; }");
100
101 // Default properties
102 setProperty("QCS_zoomx", 1.0);
103 setProperty("QCS_zoomy", 1.0);
104 setProperty("QCS_dispx", 1.0);
105 setProperty("QCS_dispy", 1.0);
106 setProperty("QCS_modex", "auto");
107 setProperty("QCS_modey", "auto");
108 setProperty("QCS_showSelector", true);
109 setProperty("QCS_showGrid", true);
110 setProperty("QCS_showTableInfo", true);
111 setProperty("QCS_showScrollbars", true);
112 setProperty("QCS_enableTables", true);
113 setProperty("QCS_enableDisplays", true);
114 m_enableTables = true;
115 m_enableDisplays = true;
116 setProperty("QCS_all", true);
117
118 m_showPeak = false;
119 m_showPeakCenterFrequency = 1000.0;
120 m_showPeakRelativeBandwidth = 0.25;
121 m_lastPeakFreq = 0;
122 m_frozen = false;
123 m_getPeakChannel = "";
124 m_peakChannelPtr = nullptr;
125
126 m_widget->setMouseTracking(true); // Necessary to pass mouse tracking to widget panel for _MouseX channels
127 m_widget->setContextMenuPolicy(Qt::NoContextMenu);
128 m_widget->show();
129
130 }
131
132 QuteGraph::~QuteGraph()
133 {
134 }
135
136 QString QuteGraph::getWidgetLine()
137 {
138 // Extension to MacCsound: type of graph (table, ftt, scope), value (which hold the index of the
139 // table displayed) zoom and channel name
140 // channel number is unused in QuteGraph, but selects channel for scope
141 #ifdef USE_WIDGET_MUTEX
142 widgetLock.lockForRead();
143 #endif
144 QString line = "ioGraph {" + QString::number(x()) + ", " + QString::number(y()) + "} ";
145 line += "{"+ QString::number(width()) +", "+ QString::number(height()) +"} table ";
146 line += QString::number(m_value, 'f', 6) + " ";
147 line += QString::number(property("QCS_zoomx").toDouble(), 'f', 6) + " ";
148 line += m_channel;
149 // qDebug("QuteGraph::getWidgetLine(): %s", line.toStdString().c_str());
150 #ifdef USE_WIDGET_MUTEX
151 widgetLock.unlock();
152 #endif
153 return line;
154 }
155
156 QString QuteGraph::getWidgetXmlText()
157 {
158 // Graphs are not implemented in blue
159 xmlText = "";
160 QXmlStreamWriter s(&xmlText);
161 createXmlWriter(s);
162 #ifdef USE_WIDGET_MUTEX
163 widgetLock.lockForRead();
164 #endif
165
166 s.writeTextElement("value", QString::number((int)m_value));
167 s.writeTextElement("objectName2", m_channel2);
168 s.writeTextElement("zoomx", QString::number(property("QCS_zoomx").toDouble(), 'f', 8));
169 s.writeTextElement("zoomy", QString::number(property("QCS_zoomy").toDouble(), 'f', 8));
170 s.writeTextElement("dispx", QString::number(property("QCS_dispx").toDouble(), 'f', 8));
171 s.writeTextElement("dispy", QString::number(property("QCS_dispy").toDouble(), 'f', 8));
172 s.writeTextElement("modex", property("QCS_modex").toString());
173 s.writeTextElement("modey", property("QCS_modey").toString());
174 s.writeTextElement("showSelector",
175 property("QCS_showSelector").toBool() ? "true" : "false");
176 s.writeTextElement("showGrid", property("QCS_showGrid").toBool() ? "true" : "false");
177 s.writeTextElement("showTableInfo",
178 property("QCS_showTableInfo").toBool() ? "true" : "false");
179 s.writeTextElement("showScrollbars",
180 property("QCS_showScrollbars").toBool() ? "true" : "false");
181 s.writeTextElement("enableTables",
182 property("QCS_enableTables").toBool() ? "true" : "false");
183 s.writeTextElement("enableDisplays", m_enableDisplays ? "true" : "false");
184 s.writeTextElement("all", property("QCS_all").toBool() ? "true" : "false");
185 s.writeEndElement();
186 #ifdef USE_WIDGET_MUTEX
187 widgetLock.unlock();
188 #endif
189 return xmlText;
190 }
191
192
193 QString QuteGraph::getWidgetType() {
194 return QString("BSBGraph");
195 }
196
197 void QuteGraph::setWidgetGeometry(int x,int y,int width,int height)
198 {
199 QuteWidget::setWidgetGeometry(x,y,width, height);
200 static_cast<StackedLayoutWidget *>(m_widget)->setWidgetGeometry(0,0,width, height);
201 int index = static_cast<StackedLayoutWidget *>(m_widget)->currentIndex();
202 changeCurve(-2);
203
204 if (index < 0)
205 return;
206 // changeCurve(index);
207 }
208
209 void QuteGraph::mousePressEvent(QMouseEvent *event) {
210 int index = (int)m_value;
211 switch(graphtypes[index]) {
212 case GraphType::GRAPH_SPECTRUM: {
213 if(event->button() & Qt::LeftButton) {
214 // turn on peak detection
215 m_showPeakTemp = true;
216 m_spectrumPeakMarkers[index]->setVisible(true);
217 m_spectrumPeakTexts[index]->setVisible(true);
218 m_mouseDragging = true;
219 auto view = getView(index);
220 auto peakIndex = view->mapToScene(event->x(), 0).x();
221 auto curve = curves[index];
222 auto freq = peakIndex / curve->get_size() * this->m_ud->sampleRate*0.5;
223 m_showPeakTempFrequency = freq;
224 return;
225 }
226 break;
227 }
228 default:
229 break;
230 }
231 }
232
233 void QuteGraph::mouseReleased() {
234 m_mouseDragging = false;
235 if(!m_showPeak ) {
236 m_lastPeakFreq = 0;
237 m_lastTextMarkerY = 0;
238 m_lastTextMarkerX = 0;
239 }
240 m_showPeakTemp = false;
241 int index = (int)m_value;
242 m_spectrumPeakTexts[index]->setVisible(m_showPeak);
243 auto scene = getView(index)->scene();
244 m_spectrumPeakMarkers[index]->setVisible(m_showPeak);
245 }
246
247 /*
248 void QuteGraph::mouseMoveEvent(QMouseEvent *event) {
249 int index = (int)m_value;
250 switch(graphtypes[index]) {
251 case GraphType::GRAPH_SPECTRUM: {
252 if(!m_mouseDragging)
253 return;
254 auto peakIndex = getView(index)->mapToScene(event->x(), 0).x();
255 auto curveSize = curves[index]->get_size();
256 m_showPeakTempFrequency = peakIndex / curveSize * this->m_ud->sampleRate*0.5;
257 return;
258 }
259 default:
260 break;
261 }
262 }
263 */
264
265
266 void QuteGraph::keyPressEvent(QKeyEvent *event) {
267 bool flag;
268 int index = m_value;
269 auto graphtype = graphtypes[index];
270 switch(event->key()) {
271 case Qt::Key_F:
272 if(graphtype != GraphType::GRAPH_SPECTRUM)
273 return;
274 freezeSpectrum(!m_frozen);
275 event->accept();
276 break;
277 case Qt::Key_S:
278 flag = !property("QCS_showSelector").toBool();
279 setProperty("QCS_showSelector", flag?"true":"false");
280 if(flag)
281 m_pageComboBox->show();
282 else
283 m_pageComboBox->hide();
284 event->accept();
285 break;
286 case Qt::Key_G:
287 flag = !property("QCS_showGrid").toBool();
288 setProperty("QCS_showGrid", flag?"true":"false");
289 m_drawGrid = flag;
290 event->accept();
291 break;
292 case Qt::Key_C:
293 flag = !property("QCS_showTableInfo").toBool();
294 setProperty("QCS_showTableInfo", flag?"true":"false");
295 m_drawTableInfo = flag;
296 if(flag)
297 m_label->show();
298 else
299 m_label->hide();
300 event->accept();
301 break;
302 case Qt::Key_Plus:
303 setProperty("QCS_zoomx", property("QCS_zoomx").toDouble()*2);
304 applyInternalProperties();
305 event->accept();
306 break;
307 case Qt::Key_Minus:
308 setProperty("QCS_zoomx", qMax(1.0, property("QCS_zoomx").toDouble()*0.5));
309 applyInternalProperties();
310 event->accept();
311 break;
312 case Qt::Key_Z:
313 flag = !property("QCS_showScrollbars").toBool();
314 setProperty("QCS_showScrollbars", flag);
315 showScrollbars(flag);
316 event->accept();
317 break;
318 case Qt::Key_H:
319 QMessageBox mb;
320 mb.setText(tr("<span style=\"font-size : 11pt\">"
321 "<ul>"
322 "<li><code>+</code> : zoom in"
323 "<li><code>-</code> : zoom out"
324 "<li><code>F</code> : toggle freeze (spectrum)"
325 "<li><code>S</code> : toggle Selector"
326 "<li><code>G</code> : toggle Grid"
327 "<li><code>C</code> : toggle table information"
328 "<li><code>Z</code> : toggle Scrollbars"
329 "</ul>"
330 "</span>"
331 ));
332 mb.setWindowTitle("Graph Shortcuts");
333 mb.exec();
334 event->accept();
335 break;
336 }
337 }
338
339
340 void QuteGraph::setValue(double value)
341 {
342 QuteWidget::setValue(value);
343 // m_value2 = this->getTableNumForIndex(value);
344 /*
345 if(value < 0) {
346 int tabnum = -((int)value);
347 int index = this->getIndexForTableNum(tabnum);
348 QuteWidget::setValue(index);
349 } else {
350 QuteWidget::setValue(value);
351 }
352 */
353
354 }
355
356 void QuteGraph::setValue(QString text)
357 {
358 bool ok;
359 auto parts = text.splitRef(' ', SKIP_EMPTY_PARTS);
360 if(parts[0] == "@set") {
361 bool ok;
362 int index = parts[1].toInt(&ok);
363 if(parts.size() != 2 || !ok) {
364 qDebug() << "@set: value error";
365 qDebug() << "@set syntax: @set <curveindex:int>";
366 return;
367 }
368 if(index >= 0 && index < curves.size())
369 this->setValue(index);
370 }
371 else if(parts[0] == "@find") {
372 // @find type text
373 // type: one of fft, audio, ftable
374 // Example: @find fft asignal
375 qDebug() << "@find " << parts;
376 if(parts.size() != 3) {
377 qDebug() << "@find: Wrong number of arguments";
378 qDebug() << "syntax: @find <kind:str> <text:str>";
379 qDebug() << " * kind: one of fft, audio, ftable";
380 qDebug() << " * text: a text to match against the caption";
381 return;
382 }
383 int index;
384 if(parts[1] == "fft") {
385 index = findCurve(CURVE_SPECTRUM, parts[2].toString());
386 }
387 else if (parts[1] == "audio") {
388 index = findCurve(CURVE_AUDIOSIGNAL, parts[2].toString());
389 }
390 else if (parts[1] == "table") {
391 int tabnum = parts[2].toInt(&ok);
392 if(!ok) {
393 qDebug()<<"@find table syntax: @find table <tablenumber:int>";
394 return;
395 }
396 index = this->getIndexForTableNum(tabnum);
397 }
398 else {
399 qDebug() << "@find: graph type should be one of 'table', 'fft' or 'audio', got"
400 << parts[1];
401 return;
402 }
403 if(index >= 0 && index < curves.size())
404 this->setValue(index);
405 else {
406 qDebug() << "@find: graph not found";
407 return;
408 }
409 }
410 else if(parts[0] == "@freeze") {
411 int index = m_value;
412 if(graphtypes[index] != GraphType::GRAPH_SPECTRUM)
413 return;
414 bool ok;
415 int status = parts[1].toInt(&ok);
416 if(status != 0 && status != 1) {
417 qDebug() << "Syntax: @freeze <status:int> (0 or 1)";
418 return;
419 }
420 freezeSpectrum(status);
421 }
422 else if (parts[0] == "@showPeak") {
423 if(parts.size() == 2) {
424 if(parts[1] == "true" || parts[1] == "1") {
425 m_showPeak = true;
426 }
427 else if (parts[1] == "false" || parts[1] == "0") {
428 m_showPeak = false;
429 } else {
430 bool ok;
431 double freq = parts[1].toDouble(&ok);
432 if(!ok) {
433 qDebug() << "@showPeak: expected a frequency, got" << parts[1];
434 return;
435 }
436 m_showPeak = true;
437 m_showPeakCenterFrequency = freq;
438 }
439 }
440 else if (parts.size() == 3) {
441 // @showPeak freq bw
442 bool ok;
443 double freq = parts[1].toDouble(&ok);
444 if(!ok) {
445 qDebug() << "@showPeak: expected a frequency, got" << parts[1];
446 return;
447 }
448 double bw = parts[2].toDouble(&ok);
449 if(!ok) {
450 qDebug() << "@showPeak: expected a bandwidth as third arg, got" << parts[2];
451 return;
452 }
453 m_showPeak = true;
454 if(freq > 0)
455 m_showPeakCenterFrequency = freq;
456 m_showPeakRelativeBandwidth = bw;
457 }
458 else {
459 qDebug() << "@showPeak syntax:";
460 qDebug() << " @showPeak false : turn off peak display";
461 qDebug() << " @showPeak 0 : turn off peak display";
462 qDebug() << " @showPeak true : turn on peak display";
463 qDebug() << " @showPeak 1 : turn on peak display";
464 qDebug() << " @showPeak <freq:double> : turn on peak display, set freq";
465 qDebug() << " @showPeak <freq:double> <bandwidth:double>";
466 qDebug() << " (use freq=0 to only set bandwidth)";
467 return;
468 }
469 }
470 else if(parts[0] == "@getPeak") {
471 m_getPeakChannel = parts[1].toString();
472 qDebug() << "@getPeak channel: " << m_getPeakChannel;
473 MYFLT *ptr;
474 csoundGetChannelPtr(m_ud->csound, &ptr,
475 m_getPeakChannel.toLocal8Bit().constData(),
476 CSOUND_INPUT_CHANNEL | CSOUND_CONTROL_CHANNEL);
477 m_peakChannelPtr = ptr;
478 }
479 else {
480 qDebug() << "Command" << text << "not found";
481 }
482 }
483
484
485 void QuteGraph::refreshWidget()
486 {
487 bool needsUpdate = false;
488 #ifdef USE_WIDGET_MUTEX
489 widgetLock.lockForRead();
490 #endif
491 int index = 0;
492 if (m_valueChanged) {
493 index = (int) m_value;
494 if(index < 0) {
495 m_value2 = -index;
496 } else {
497 m_value2 = getTableNumForIndex(index);
498 }
499 m_value2Changed = false;
500 m_valueChanged = false;
501 needsUpdate = true;
502 }
503 else if (m_value2Changed) {
504 index = getIndexForTableNum((int)m_value2);
505 if (index >= 0) {
506 m_value = index;
507 // m_valueChanged = false;
508 }
509 m_value2Changed = false;
510 needsUpdate = true;
511 }
512 #ifdef USE_WIDGET_MUTEX
513 widgetLock.unlock(); // unlock
514 #endif
515 if (needsUpdate) {
516 if (index < 0) {
517 index = getIndexForTableNum(-index);
518 }
519 if (index < 0 ||
520 index >= curves.size() ||
521 curves[index]->get_caption().isEmpty()) {
522 // Don't show if curve has no name. Is this likely?
523 return;
524 }
525 // m_pageComboBox->blockSignals(true);
526 // m_pageComboBox->setCurrentIndex(index);
527 // m_pageComboBox->blockSignals(false);
528 changeCurve(index);
529
530 }
531 // QComboBox *cb = this->m_pageComboBox;
532 // cb->move(this->width() - cb->width(), this->height()-cb->height());
533 }
534
535 void QuteGraph::createPropertiesDialog()
536 {
537 QLabel *label;
538 QuteWidget::createPropertiesDialog();
539 dialog->setWindowTitle("Graph");
540
541 channelLabel->setText("Index Channel name =");
542 channelLabel->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
543 // nameLineEdit->setText(getChannelName());
544
545 label = new QLabel("F-table Channel name =", dialog);
546 layout->addWidget(label, 4, 0, Qt::AlignRight|Qt::AlignVCenter);
547 name2LineEdit = new QLineEdit(dialog);
548 name2LineEdit->setText(getChannel2Name());
549 name2LineEdit->setMinimumWidth(320);
550 layout->addWidget(name2LineEdit, 4,1,1,3, Qt::AlignLeft|Qt::AlignVCenter);
551
552 label = new QLabel("Zoom X", dialog);
553 layout->addWidget(label, 8, 0, Qt::AlignRight|Qt::AlignVCenter);
554 zoomxBox = new QDoubleSpinBox(dialog);
555 zoomxBox->setRange(0.1, 10.0);
556 zoomxBox->setDecimals(1);
557 zoomxBox->setSingleStep(0.1);
558 layout->addWidget(zoomxBox, 8, 1, Qt::AlignLeft|Qt::AlignVCenter);
559
560 label = new QLabel("Zoom Y", dialog);
561 layout->addWidget(label, 8, 2, Qt::AlignRight|Qt::AlignVCenter);
562 zoomyBox = new QDoubleSpinBox(dialog);
563 zoomyBox->setRange(0.1, 10.0);
564 zoomyBox->setDecimals(1);
565 zoomyBox->setSingleStep(0.1);
566 layout->addWidget(zoomyBox, 8, 3, Qt::AlignLeft|Qt::AlignVCenter);
567
568 acceptTablesCheckBox = new QCheckBox(dialog);
569 acceptTablesCheckBox->setText("Enable tables");
570 acceptTablesCheckBox->setToolTip("Enable the display of tables. Each time a table is "
571 "creted it will be made available to be selected.\n"
572 "NB: modifications to a table will not be shown in the "
573 "graph, use a TablePlot widget for that.\n"
574 "NB2: If you plan to use the Graph widget to display"
575 "spectra or signals, uncheck this option");
576 acceptTablesCheckBox->setChecked(property("QCS_enableTables").toBool());
577 layout->addWidget(acceptTablesCheckBox, 9, 0, Qt::AlignLeft|Qt::AlignVCenter);
578
579 acceptDisplaysCheckBox = new QCheckBox(dialog);
580 acceptDisplaysCheckBox->setText("Enable Displays");
581 acceptDisplaysCheckBox->setToolTip("Enable displaying audio signals/spectra. Check this"
582 "if you plan to use the graph widget to display audio"
583 "signals and spectra using the opcodes display/dispfft");
584 acceptDisplaysCheckBox->setChecked(property("QCS_enableDisplays").toBool());
585 layout->addWidget(acceptDisplaysCheckBox, 9, 1, Qt::AlignLeft|Qt::AlignVCenter);
586
587 showSelectorCheckBox = new QCheckBox("Show Selector", dialog);
588 showSelectorCheckBox->setChecked(property("QCS_showSelector").toBool());
589 layout->addWidget(showSelectorCheckBox, 10, 0, Qt::AlignLeft|Qt::AlignVCenter);
590
591 showGridCheckBox = new QCheckBox("Show Grid", dialog);
592 showGridCheckBox->setChecked(property("QCS_showGrid").toBool());
593 showGridCheckBox->setToolTip("Show the grid. Has effect only for spectral graphs");
594 layout->addWidget(showGridCheckBox, 10, 1, Qt::AlignLeft|Qt::AlignVCenter);
595
596 showTableInfoCheckBox = new QCheckBox("Show Table Information", dialog);
597 showTableInfoCheckBox->setCheckState(
598 property("QCS_showTableInfo").toBool()?Qt::Checked:Qt::Unchecked);
599 showTableInfoCheckBox->setToolTip("Show the grid. Has effect only for spectral graphs");
600 layout->addWidget(showTableInfoCheckBox, 11, 0, Qt::AlignLeft|Qt::AlignVCenter);
601
602 showScrollbarsCheckBox = new QCheckBox("Show Scrollbars", dialog);
603 showScrollbarsCheckBox->setChecked(property("QCS_showScrollbars").toBool());
604 layout->addWidget(showScrollbarsCheckBox, 11, 1, Qt::AlignLeft|Qt::AlignVCenter);
605
606 #ifdef USE_WIDGET_MUTEX
607 widgetLock.lockForRead();
608 #endif
609 zoomxBox->setValue(property("QCS_zoomx").toDouble());
610 zoomyBox->setValue(property("QCS_zoomy").toDouble());
611 #ifdef USE_WIDGET_MUTEX
612 widgetLock.unlock();
613 #endif
614 // channelLabel->hide();
615 // nameLineEdit->hide();
616 }
617
618 void QuteGraph::applyProperties()
619 {
620 #ifdef USE_WIDGET_MUTEX
621 widgetLock.lockForWrite();
622 #endif
623 setProperty("QCS_objectName2", name2LineEdit->text());
624 setProperty("QCS_zoomx", zoomxBox->value());
625 setProperty("QCS_zoomy", zoomyBox->value());
626 setProperty("QCS_dispx", 1);
627 setProperty("QCS_dispy", 1);
628 setProperty("QCS_modex", "lin");
629 setProperty("QCS_modey", "lin");
630 setProperty("QCS_all", true);
631 setProperty("QCS_showSelector", showSelectorCheckBox->checkState());
632 setProperty("QCS_showGrid", showGridCheckBox->checkState());
633 setProperty("QCS_showScrollbars", showScrollbarsCheckBox->isChecked());
634 setProperty("QCS_enableTables", acceptTablesCheckBox->isChecked());
635 setProperty("QCS_enableDisplays", acceptDisplaysCheckBox->isChecked());
636
637 m_enableTables = acceptTablesCheckBox->isChecked();
638 m_enableDisplays = acceptDisplaysCheckBox->isChecked();
639
640 showTableInfo(showTableInfoCheckBox->checkState());
641
642 #ifdef USE_WIDGET_MUTEX
643 widgetLock.unlock();
644 #endif
645 QuteWidget::applyProperties();
646 }
647
648
649 int QuteGraph::findCurve(CurveType type, QString text) {
650 // returns the index or -1 if not found
651 for (int i = 0; i < curves.size(); i++) {
652 if(curves[i]->get_type() != type)
653 continue;
654 QString caption = curves[i]->get_caption();
655 if (caption.contains(text)) {
656 return i;
657 }
658 }
659 return -1;
660 }
661
662 void QuteGraph::changeCurve(int index)
663 {
664 if(curves.size() <= 0)
665 return;
666
667 int origRequest = index;
668 StackedLayoutWidget *stacked = static_cast<StackedLayoutWidget *>(m_widget);
669 if (index == -1) { // goto last curve
670 index = stacked->count() - 1;
671 }
672 else if (index == -2) { // update curve but don't change which
673 if (m_value < 0)
674 index = getIndexForTableNum(-m_value);
675 else
676 index = (int) m_value;
677 } else if (index >= stacked->count()) {
678 qDebug() << "changeCurve: index out of range. Num indices:"<<stacked->size();
679 return;
680 }
681 else if (stacked->currentIndex() == index) {
682 return;
683 } else {
684 // change curve
685 auto view = stacked->currentWidget();
686 view->hide();
687 stacked->blockSignals(true);
688 stacked->setCurrentIndex(index);
689 stacked->blockSignals(false);
690 m_pageComboBox->blockSignals(true);
691 m_pageComboBox->setCurrentIndex(index);
692 m_pageComboBox->blockSignals(false);
693 }
694
695 if (index < 0 || index >= curves.size()) { // Invalid index
696 QDEBUG << "Invalid index" << index;
697 return;
698 }
699 m_value = index;
700 switch(graphtypes[index]) {
701 case GraphType::GRAPH_FTABLE: {
702 int ftable = getTableNumForIndex(index);
703 if (m_value2 != ftable) {
704 m_value2 = ftable;
705 m_value2Changed = true;
706 }
707 if(m_drawTableInfo) {
708 auto curve = curves[index];
709 auto text = QString("%1 pts (%2, %3)")
710 .arg(curve->get_size())
711 .arg(curve->get_max(), 0, 'f', 3)
712 .arg(curve->get_min(), 0, 'f', 3);
713 m_label->setText(text);
714 m_label->show();
715 }
716 if(origRequest != -2) {
717 drawGraph(curves[index], index);
718 }
719 break;
720 }
721 case GraphType::GRAPH_SPECTRUM:
722 m_value2 = -1;
723 m_label->hide();
724 break;
725 case GraphType::GRAPH_AUDIOSIGNAL:
726 m_value2 = -1;
727 m_label->hide();
728 break;
729 }
730 scaleGraph(index);
731
732 }
733
734 void QuteGraph::indexChanged(int index)
735 {
736 #ifdef USE_WIDGET_MUTEX
737 widgetLock.lockForRead();
738 #endif
739 if (m_channel == "") {
740 setInternalValue(index);
741 }
742 else {
743 QPair<QString, double> channelValue(m_channel, index);
744 emit newValue(channelValue);
745 }
746 if (m_channel2 == "") {
747 setValue2(getTableNumForIndex(index));
748 }
749 else {
750 QPair<QString, double> channel2Value(m_channel2, getTableNumForIndex(index));
751 emit newValue(channel2Value);
752 }
753
754 #ifdef USE_WIDGET_MUTEX
755 widgetLock.unlock();
756 #endif
757
758 }
759
760 void QuteGraph::clearCurves()
761 {
762 // curveLock.lock();
763 m_widget->blockSignals(true);
764 static_cast<StackedLayoutWidget *>(m_widget)->clearCurves();
765 m_widget->blockSignals(false);
766 m_pageComboBox->blockSignals(true);
767 m_pageComboBox->clear();
768 m_pageComboBox->blockSignals(false);
769 curves.clear();
770 graphtypes.clear();
771 lines.clear();
772 polygons.clear();
773 m_gridlines.clear();
774 m_gridTextsX.clear();
775 m_gridTextsY.clear();
776 m_spectrumPeakTexts.clear();
777 m_spectrumPeakMarkers.clear();
778 m_showPeak = false;
779 m_showPeakTemp = false;
780 m_showPeakCenterFrequency = 1000;
781 // curveLock.unlock();
782 }
783
784 void QuteGraph::addCurve(Curve * curve)
785 {
786 QMutexLocker locker(&curveLock);
787 Q_ASSERT(curve != nullptr);
788 GraphType graphType;
789 QGraphicsView *view;
790 CurveType type = curve->get_type();
791 if(type == CURVE_SPECTRUM) {
792 if(!m_enableDisplays)
793 return;
794 graphType = GraphType::GRAPH_SPECTRUM;
795 auto spectralView = new SpectralView(m_widget);
796 view = static_cast<QGraphicsView*>(spectralView);
797 connect(spectralView, SIGNAL(mouseReleased()), this, SLOT(mouseReleased()));
798 view->setRenderHint(QPainter::Antialiasing);
799 } else if(type == CURVE_FTABLE) {
800 if(!m_enableTables)
801 return;
802 graphType = GraphType::GRAPH_FTABLE;
803 view = new QGraphicsView(m_widget);
804 view->setRenderHint(QPainter::Antialiasing);
805 } else {
806 if(!m_enableDisplays)
807 return;
808 graphType = GraphType::GRAPH_AUDIOSIGNAL;
809 view = new QGraphicsView(m_widget);
810 }
811 QGraphicsScene *scene = new QGraphicsScene(view);
812 view->setContextMenuPolicy(Qt::NoContextMenu);
813 view->setScene(scene);
814 view->setObjectName(curve->get_caption());
815 auto scrollbarPolicy = property("QCS_showScrollbars").toBool() ? Qt::ScrollBarAsNeeded
816 : Qt::ScrollBarAlwaysOff;
817 view->setHorizontalScrollBarPolicy(scrollbarPolicy);
818 view->show();
819 scene->setBackgroundBrush(QBrush(Qt::black));
820 lines.append(QVector<QGraphicsLineItem *>());
821 QVector<QGraphicsLineItem *> gridLinesVector;
822 QVector<QGraphicsTextItem *> gridTextVectorX;
823 QVector<QGraphicsTextItem *> gridTextVectorY;
824 qreal freqStep = 1000.0;
825 // Max. samplerate. We generate a grid for this and can make lines/labels visible or
826 // not depending on the actual sample rate
827 qreal sr = 96000;
828 int numTicksX = (int)(sr*0.5 / freqStep);
829 int numTicksY = m_numticksY;
830 auto gridpen = QPen(QColor(90, 90, 90));
831 auto gridpen2 = QPen(QColor(60, 60, 60));
832 gridpen.setCosmetic(true);
833 gridpen2.setCosmetic(true);
834
835 auto marker = new QGraphicsRectItem();
836 marker->setVisible(false);
837 auto markerpen = QPen(QColor(250, 250, 250));
838 markerpen.setCosmetic(true);
839 marker->setPen(markerpen);
840 marker->setBrush(QBrush(QColor(255, 255, 255, 100)));
841 m_spectrumPeakMarkers.append(marker);
842 scene->addItem(marker);
843 auto markerText = new QGraphicsTextItem();
844 markerText->setDefaultTextColor(markerpen.color());
845 markerText->setVisible(false);
846 markerText->setFlags(QGraphicsItem::ItemIgnoresTransformations);
847 markerText->setFont(QFont("Sans", 8));
848 scene->addItem(markerText);
849 m_spectrumPeakTexts.append(markerText);
850 m_dbRange = 100;
851 auto gridTextFont = QFont("Sans", 6);
852
853 if(graphType == GraphType::GRAPH_SPECTRUM) {
854 for (int i = 0 ; i < numTicksY; i++) {
855 // 0 - 0db, 5 -> 100 dB (6:120
856 QGraphicsLineItem *gridLine = new QGraphicsLineItem();
857 gridLine->setPen(gridpen);
858 scene->addItem(gridLine);
859 gridLinesVector.append(gridLine);
860 QGraphicsTextItem *gridText = new QGraphicsTextItem();
861 gridText->setDefaultTextColor(Qt::gray);
862 gridText->setFlags(QGraphicsItem::ItemIgnoresTransformations);
863 int dbs = round(float(i)/numTicksY * 120.0);
864 gridText->setHtml(QString("<div style=\"background:#000000;\">-%1 </p>"
865 ).arg(dbs));
866 gridText->setFont(gridTextFont);
867 gridText->setVisible(false);
868 scene->addItem(gridText);
869 gridTextVectorY.append(gridText);
870
871 }
872
873 for (int i = 0; i < numTicksX; i++) {
874 QGraphicsLineItem *gridLine = new QGraphicsLineItem();
875 gridLine->setPen(i%2 == 0 ? gridpen : gridpen2);
876 scene->addItem(gridLine);
877 gridLinesVector.append(gridLine);
878 QGraphicsTextItem *gridText = new QGraphicsTextItem();
879 gridText->setDefaultTextColor(Qt::gray);
880 gridText->setFlags(QGraphicsItem::ItemIgnoresTransformations);
881 if (i > 0 && i%2==0) {
882 // double kHz = i*((numTicksX-1.0)/numTicksX) * 2.0;
883 double kHz = i * freqStep / 1000.0 ;
884 if(i%10==0)
885 gridText->setHtml(
886 QString("<div style=\"background:#000000;\">%1k</p>")
887 .arg(kHz, 2, 'f', 1));
888 else
889 gridText->setHtml(
890 QString("<div style=\"background:#000000;\">%1</p>")
891 .arg(kHz, 2, 'f', 1));
892
893 }
894 gridText->setFont(gridTextFont);
895 gridText->setVisible(false);
896 scene->addItem(gridText);
897 gridTextVectorX.append(gridText);
898 }
899 }
900
901 m_gridlines.append(gridLinesVector);
902 m_gridTextsX.append(gridTextVectorX);
903 m_gridTextsY.append(gridTextVectorY);
904
905 graphtypes.append(graphType);
906
907 QGraphicsPolygonItem * item = new QGraphicsPolygonItem();
908 auto graphPen = QPen(Qt::yellow);
909 graphPen.setCosmetic(true);
910 item->setPen(graphPen);
911 item->show();
912 polygons.append(item);
913 scene->addItem(item);
914 view->setResizeAnchor (QGraphicsView::NoAnchor);
915 // view->setFocusPolicy(Qt::NoFocus);
916 m_pageComboBox->blockSignals(true);
917 QString curveTitle = curve->get_caption().trimmed();
918 if(curveTitle.endsWith(":"))
919 curveTitle = curveTitle.mid(0, curveTitle.size()-1);
920 m_pageComboBox->addItem(curveTitle);
921 // m_pageComboBox->addItem(curve->get_title());
922 m_pageComboBox->blockSignals(false);
923 // curveLock.lock();
924 static_cast<StackedLayoutWidget *>(m_widget)->addWidget(view);
925 curves.append(curve);
926 if (m_value == curves.size() - 1) {
927 // If new curve created corresponds to current stored value
928 changeCurve(m_value);
929 }
930 }
931
932 int QuteGraph::getCurveIndex(Curve * curve)
933 {
934 Q_ASSERT(curve != nullptr);
935 int index = -1;
936 for (int i = 0; i < curves.size(); i++) {
937 if (curves[i] == curve) {
938 index = i;
939 break;
940 }
941 }
942 return index;
943 }
944
945 QGraphicsView * QuteGraph::getView(int index) {
946 StackedLayoutWidget *widget_ = static_cast<StackedLayoutWidget *>(m_widget);
947 QGraphicsView *view = static_cast<QGraphicsView *>(widget_->widget(index));
948 return view;
949 }
950
951 void QuteGraph::drawGraph(Curve *curve, int index) {
952 // QString caption = curve->get_caption();
953 // auto view = getView(index);
954 // switch(graphtypes[index]) {
955 switch(curve->get_type()) {
956 // case GraphType::GRAPH_FTABLE:
957 case CurveType::CURVE_FTABLE:
958 // drawFtable(curve, index);
959 // view->setRenderHint(QPainter::Antialiasing);
960 drawFtablePath(curve, index);
961 scaleGraph(index);
962 break;
963 // case GraphType::GRAPH_SPECTRUM:
964 case CurveType::CURVE_SPECTRUM:
965 // view->setRenderHint(QPainter::Antialiasing);
966 drawSpectrum(curve, index);
967 // drawSpectrumPath(curve, index);
968 changeCurve(-2); //update curve
969 break;
970 case CurveType::CURVE_AUDIOSIGNAL:
971 // case GraphType::GRAPH_AUDIOSIGNAL:
972 // drawSignal(curve, index);
973 drawSignalPath(curve, index);
974 changeCurve(-2); //update curve
975 break;
976 }
977 }
978
979 void QuteGraph::setCurveData(Curve * curve)
980 {
981 Q_ASSERT(curve != nullptr);
982 int index = getCurveIndex(curve);
983
984 if (index >= curves.size() ||
985 index < 0 ||
986 index != m_value) {
987 return;
988 }
989
990 /*
991 if(!m_enableTables && graphtypes[index] == GraphType::GRAPH_FTABLE)
992 return;
993 */
994
995 auto view = getView(index);
996
997 // Refitting curves in view resets the scrollbar so we need the previous value
998 int viewPosx = view->horizontalScrollBar()->value();
999 int viewPosy = view->verticalScrollBar()->value();
1000
1001 drawGraph(curve, index);
1002 view->horizontalScrollBar()->setValue(viewPosx);
1003 view->verticalScrollBar()->setValue(viewPosy);
1004 }
1005
1006 void QuteGraph::applyInternalProperties()
1007 {
1008 QuteWidget::applyInternalProperties();
1009 if(property("QCS_showSelector").toBool()) {
1010 m_pageComboBox->show();
1011 } else {
1012 m_pageComboBox->hide();
1013 }
1014 changeCurve(-2); // Redraw
1015 m_drawGrid = property("QCS_showGrid").toBool();
1016 m_drawTableInfo = property("QCS_showTableInfo").toBool();
1017
1018 showScrollbars(property("QCS_showScrollbars").toBool());
1019 }
1020
1021 void QuteGraph::drawFtablePath(Curve *curve, int index) {
1022 Q_ASSERT(index >= 0);
1023 QGraphicsScene *scene = this->getView(index)->scene();
1024 // QGraphicsScene *scene = static_cast<QGraphicsView *>(static_cast<StackedLayoutWidget *>(m_widget)->widget(index))->scene();
1025
1026 double max = curve->get_max();
1027 max = max == 0 ? 1: max;
1028 int curveSize = curve->get_size();
1029
1030 int decimate = curveSize /1024;
1031 if (decimate == 0) {
1032 decimate = 1;
1033 }
1034 auto pen = QPen(QColor(255, 255, 50), 0);
1035 // auto pen = QPen(QColor(255, 45, 7), 0);
1036
1037 auto rect = this->rect();
1038 int width = rect.width();
1039 int step = curveSize / width;
1040 if(step == 0)
1041 step = 1;
1042
1043 QPainterPath path;
1044 for (int i = 0; i < curveSize; i+=step) {
1045 double value = curve->get_data(i);
1046 path.lineTo(QPointF(i, -value));
1047 }
1048 scene->clear();
1049 if(step > 1) {
1050 pen.setWidth(0);
1051 }
1052 scene->addPath(path, pen);
1053 }
1054
1055
1056 void QuteGraph::drawFtable(Curve * curve, int index)
1057 {
1058 // bool live = curve->getOriginal() != 0;
1059 Q_ASSERT(index >= 0);
1060 QString caption = curve->get_caption();
1061 if (caption.isEmpty()) {
1062 return;
1063 }
1064 QGraphicsScene *scene = static_cast<QGraphicsView *>(static_cast<StackedLayoutWidget *>(m_widget)->widget(index))->scene();
1065 double max = curve->get_max();
1066 max = max == 0 ? 1: max;
1067 int size = (int) curve->get_size();
1068 int decimate = size /1024;
1069 if (decimate == 0) {
1070 decimate = 1;
1071 }
1072 auto pen = QPen(QColor(255, 45, 7));
1073 pen.setCosmetic(true);
1074 if (lines[index].size() != size) {
1075 foreach (QGraphicsLineItem *line, lines[index]) {
1076 scene->removeItem(line);
1077 delete line;
1078 }
1079 lines[index].clear();
1080 for (int i = 0; i < size; i++) {
1081 if (decimate == 0 || i%decimate == 0) {
1082 QGraphicsLineItem *line = new QGraphicsLineItem(i, 0, i, 0);
1083 line->setPen(pen);
1084 lines[index].append(line);
1085 scene->addItem(line);
1086 }
1087 }
1088 }
1089 for (int i = 0; i < lines[index].size(); i++) { //skip first item, which is base line
1090 QGraphicsLineItem *line = static_cast<QGraphicsLineItem *>(lines[index][i]);
1091 MYFLT value = curve->get_data((i * decimate));
1092 line->setLine((i * decimate), 0, (i * decimate), -value );
1093 line->show();
1094 }
1095 scaleGraph(index);
1096 }
1097
1098 void QuteGraph::drawSpectrumPath(Curve *curve, int index) {
1099 int curveSize = curve->get_size();
1100 QGraphicsScene *scene = static_cast<QGraphicsView *>(static_cast<StackedLayoutWidget *>(m_widget)->widget(index))->scene();
1101 QPainterPath path;
1102 double db0 = m_ud->zerodBFS;
1103 path.moveTo(0, -20.0*log10(fabs(curve->get_data(0))/db0));
1104 for(int i=1; i < curveSize; i++) {
1105 double value = 20.0*log10(fabs(curve->get_data(i))/db0);
1106 path.lineTo(QPointF(i, -value));
1107 }
1108 scene->clear();
1109 auto pen = QPen(Qt::yellow);
1110 pen.setCosmetic(true);
1111
1112 QPainterPath gridPath;
1113 QPainter painter;
1114
1115 if(m_drawGrid) {
1116 int sr = (int)this->getSr();
1117 int nyquist = sr / 2;
1118 int step = 1000;
1119 int numTicksX = nyquist / step;
1120 int numTicksY = 7;
1121 // printf("grid: nyquist: %d, numticks: %d\n", nyquist, numTicksX);
1122
1123 auto gridPen = QPen(QColor(40, 40, 40));
1124 gridPen.setCosmetic(true);
1125 auto textColor = QColor(128, 128, 128);
1126 qreal curveSizeF = (qreal)curveSize;
1127 auto font = QFont("Sans");
1128 font.setPixelSize(9);
1129 const int maxy = 110;
1130 for (int i = 1; i < numTicksX; i++) {
1131 qreal freq = i * step;
1132 qreal x = freq/nyquist * curveSizeF;
1133 gridPath.moveTo(x, 0);
1134 gridPath.lineTo(x, maxy);
1135 if(i%2 == 0) {
1136 auto item = scene->addText(QString::number(freq/1000.0, 'f', 1), font);
1137 item->setDefaultTextColor(textColor);
1138 item->setPos(x, 0);
1139 item->setFlag(item->ItemIgnoresTransformations, true);
1140 }
1141 }
1142 for (int i=1; i < numTicksY-1; i++) {
1143 qreal y = (qreal)i/numTicksY * maxy;
1144 gridPath.moveTo(0, y);
1145 gridPath.lineTo(curveSizeF, y);
1146 }
1147 scene->addPath(gridPath, gridPen);
1148 }
1149 scene->addPath(path, pen);
1150 }
1151
1152 size_t QuteGraph::spectrumGetPeak(Curve *curve, double freq, double bandwidth) {
1153 qreal sr = this->getSr(44100.);
1154 qreal nyquist = sr * 0.5;
1155 size_t curveSize = curve->get_size();
1156 qreal minfreq = freq - bandwidth*0.5;
1157 qreal maxfreq = freq + bandwidth*0.5;
1158 minfreq = qMax(1.0, minfreq);
1159 maxfreq = qMax(minfreq, maxfreq);
1160 size_t index0 = (size_t)(minfreq / nyquist * curveSize);
1161 index0 = index0 < curveSize - 1 ? index0 : curveSize - 1;
1162 size_t index1 = (size_t)(maxfreq / nyquist * curveSize);
1163 index1 = index1 < curveSize - 1 ? index1 : curveSize - 1;
1164 qreal maxvalue = 0;
1165 size_t maxindex = 0;
1166 if(!m_frozen) {
1167 for(int i=index0; i<index1; i++) {
1168 auto data = curve->get_data(i);
1169 if (data > maxvalue) {
1170 maxvalue = data;
1171 maxindex = i;
1172 }
1173 }
1174 }
1175 else {
1176 for(int i=index0; i<index1; i++) {
1177 auto data = frozenCurve[i];
1178 if (data > maxvalue) {
1179 maxvalue = data;
1180 maxindex = i;
1181 }
1182 }
1183 }
1184 if(maxindex > curveSize - 1 || maxindex < 0) {
1185 qDebug() << "spectrum peak: wrong index " << maxindex;
1186 return 0;
1187 }
1188 return maxindex;
1189 }
1190
1191 QString mton(double midinote) {
1192 // C C# D D# E F F# G G# A Bb B
1193 static const int _pc2idx[] = {2, 2, 3, 3, 4, 5, 5, 6, 6, 0, 1, 1};
1194 static const int _pc2alt[] = {0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 2, 0};
1195 static const char _alts[] = " #b";
1196
1197 QString dst = "";
1198 double m = midinote;
1199
1200 int octave = (int) (m / 12 - 1);
1201 int pc = (int)m % 12;
1202 int cents = round((m - floor(m)) * 100.0);
1203 int sign;
1204
1205 if (cents == 0) {
1206 sign = 0;
1207 } else if (cents <= 50) {
1208 sign = 1;
1209 } else {
1210 cents = 100 - cents;
1211 sign = -1;
1212 pc += 1;
1213 if (pc == 12) {
1214 pc = 0;
1215 octave += 1;
1216 }
1217 }
1218 if(octave >= 0) {
1219 dst.append('0' + octave);
1220 } else {
1221 dst.append('-');
1222 dst.append('0' - octave);
1223 }
1224 dst.append('A' + _pc2idx[pc]);
1225 int32_t alt = _pc2alt[pc];
1226 if(alt > 0) {
1227 dst.append(_alts[alt]);
1228 }
1229 if(sign == 1) {
1230 dst.append('+');
1231 if(cents != 50) {
1232 dst += QString::number(cents);
1233 }
1234 } else if(sign == -1) {
1235 dst.append('-');
1236 if(cents != 50) {
1237 dst += QString::number(cents);
1238 }
1239 }
1240 return dst;
1241 }
1242
1243
1244 QPoint quantizePoint(int x, int y, int resx, int resy) {
1245 x = static_cast<int>(round((double)x / resx) * resx);
1246 y = static_cast<int>(round((double)y / resy) * resy);
1247 return QPoint(x, y);
1248 }
1249
1250 void QuteGraph::freezeSpectrum(bool status) {
1251 int index = m_value;
1252 auto curve = curves[index];
1253 if(status) {
1254 m_frozen = true;
1255 frozenCurve.resize(curve->get_size());
1256 for(int i=0; i < curve->get_size(); i++) {
1257 frozenCurve[i] = curve->get_data(i);
1258 }
1259 } else {
1260 m_frozen = false;
1261 }
1262 }
1263
1264 void QuteGraph::drawSpectrum(Curve *curve, int index) {
1265 if(!curveLock.tryLock())
1266 return;
1267 int curveSize = curve->get_size();
1268 if(curveSize != frozenCurve.size() && graphtypes[index] == GraphType::GRAPH_SPECTRUM)
1269 freezeSpectrum(false);
1270 // auto view = getView(index);
1271 // QGraphicsScene *scene = static_cast<QGraphicsView *>(static_cast<StackedLayoutWidget *>(m_widget)->widget(index))->scene();
1272 QVector<QPointF> polygonPoints;
1273 polygonPoints.resize(curveSize + 2);
1274 double dbRange = m_dbRange;
1275 polygonPoints[0] = QPointF(0, dbRange);
1276 double db0 = m_ud->zerodBFS;
1277
1278 if(!m_frozen) {
1279 for (int i = 0; i < (int) curveSize; i++) {
1280 auto data = curve->get_data(i);
1281 double db = data > 0.000001 ? 20.0*log10(data/db0) : -dbRange;
1282 // double value = 20.0*log10(fabs(curve->get_data(i))/db0);
1283 polygonPoints[i+1] = QPointF(i, -db); //skip first item, which is base line
1284 }
1285 } else {
1286 for (int i = 0; i < frozenCurve.size(); i++) {
1287 auto data = frozenCurve[i];
1288 double db = data > 0.000001 ? 20.0*log10(data/db0) : -dbRange;
1289 // double value = 20.0*log10(fabs(curve->get_data(i))/db0);
1290 polygonPoints[i+1] = QPointF(i, -db); //skip first item, which is base line
1291 }
1292 }
1293
1294 polygonPoints.back() = QPointF(curveSize - 1, dbRange);
1295 polygons[index]->setPolygon(QPolygonF(polygonPoints));
1296
1297 // m_pageComboBox->setItemText(index, curve->get_caption());
1298 // draw Grid
1299 int numTicksY = m_numticksY;
1300 auto gridlinesvec = m_gridlines[index];
1301 auto gridtextvecx = m_gridTextsX[index];
1302 auto gridtextvecy = m_gridTextsY[index];
1303 qreal sr = this->getSr(44100.0);
1304 qreal nyquist = sr * 0.5;
1305 qreal freqStep = 1000.0; // TODO: allow to configure this
1306 int numTicksX = (int)(nyquist / freqStep);
1307 // TODO: fix y axis
1308 if(m_drawGrid) {
1309 gridtextvecy[0]->setVisible(true);
1310 for (int i = 1; i < numTicksY; i++) {
1311 int y = float(i)/(numTicksY) * dbRange;
1312 gridlinesvec[i]->setLine(0, y, curveSize, y);
1313 gridlinesvec[i]->setVisible(true);
1314 gridtextvecy[i]->setPos(0, y);
1315 gridtextvecy[i]->setVisible(true);
1316 }
1317
1318 for (int i = 1; i < numTicksX; i++) {
1319 // qreal x = i * qreal(curveSize)/numTicksX;
1320 qreal freq = i * freqStep;
1321 qreal x = freq/nyquist * curveSize;
1322 int idx = i + m_numticksY;
1323 gridlinesvec[idx]->setLine(x, 0, x, dbRange);
1324 gridlinesvec[idx]->setVisible(true);
1325 gridtextvecx[i]->setPos(x, 0);
1326 gridtextvecx[i]->setVisible(true);
1327 }
1328 } else {
1329 for(int i=0; i < numTicksY; i++) {
1330 gridlinesvec[i]->setVisible(false);
1331 gridtextvecy[i]->setVisible(false);
1332 }
1333 for (int i = 0; i < numTicksX; i++) {
1334 gridlinesvec[i+m_numticksY]->setVisible(false);
1335 gridtextvecx[i]->setVisible(false);
1336 }
1337 }
1338
1339 if(m_showPeak || m_showPeakTemp) {
1340 auto freq = m_showPeakTemp ? m_showPeakTempFrequency : m_showPeakCenterFrequency;
1341 double bandwidth;
1342 if(m_showPeakTemp) {
1343 // if using the mouth to point at a near peak, we take zoom into account and
1344 // the bandwidth is a fraction of the displayed frequency range.
1345 auto zoomx = property("QCS_zoomx").toDouble();
1346 bandwidth = nyquist / zoomx / 8.0;
1347 }
1348 else
1349 bandwidth = freq * m_showPeakRelativeBandwidth;
1350 size_t peakIndex = this->spectrumGetPeak(curve, freq, bandwidth);
1351 auto data = m_frozen ? frozenCurve[peakIndex] : curve->get_data(peakIndex);
1352 double db = data > 0.000001 ? 20.0*log10(data/db0) : -dbRange;
1353 auto marker = m_spectrumPeakMarkers[index];
1354 auto view = this->getView(index);
1355 auto vcenter = view->mapFromScene(QPointF(peakIndex, -db));
1356 auto markerLeftTop = view->mapToScene(QPoint(vcenter.x() - 4, vcenter.y()-4));
1357 auto markerRightBottom = view->mapToScene(vcenter.x() + 4, vcenter.y()+4);
1358 marker->setRect(markerLeftTop.x(), markerLeftTop.y(),
1359 markerRightBottom.x()-markerLeftTop.x(),
1360 markerRightBottom.y() - markerLeftTop.y());
1361 auto markerText = m_spectrumPeakTexts[index];
1362 auto textPos = view->mapToScene(vcenter.x() + 10, vcenter.y() - 12);
1363 auto markerY = m_lastTextMarkerY <= 0 ? textPos.y() :
1364 textPos.y() * 0.2 + m_lastTextMarkerY * 0.8;
1365 auto markerX = m_lastTextMarkerX <= 0 ? textPos.x() :
1366 textPos.x() * 0.3 + m_lastTextMarkerX * 0.7;
1367 m_lastTextMarkerY = markerY;
1368 m_lastTextMarkerX = markerX;
1369 markerText->setPos(markerX, markerY);
1370 double factor = nyquist / curveSize;
1371 double peakFreq;
1372 if(peakIndex <= 1 || peakIndex >= curveSize - 2) {
1373 peakFreq = factor * peakIndex;
1374 }
1375 else {
1376 double freq0 = qreal(peakIndex-1)*factor;
1377 double freq1 = qreal(peakIndex)*factor;
1378 double freq2 = qreal(peakIndex+1)*factor;
1379 double amp1 = data;
1380 double amp0, amp2;
1381 if(!m_frozen) {
1382 amp0 = curve->get_data(peakIndex-1);
1383 amp2 = curve->get_data(peakIndex+1);
1384 } else {
1385 amp0 = frozenCurve[peakIndex-1];
1386 amp2 = frozenCurve[peakIndex+1];
1387 }
1388 amp1 *= amp1;
1389 amp0 *= amp0;
1390 amp2 *= amp2;
1391 peakFreq = (freq0*amp0 + freq1*amp1 + freq2*amp2) / (amp0+amp1+amp2);
1392 }
1393 peakFreq = m_lastPeakFreq <= 0 ? peakFreq : peakFreq * 0.2 + m_lastPeakFreq * 0.8;
1394 m_lastPeakFreq = peakFreq;
1395 double a4 = csoundGetA4(this->m_ud->csound);
1396 double midinote;
1397 QString notename;
1398 if(peakFreq > 10) {
1399 midinote = 12.0 * log2(peakFreq / a4) + 69.0;
1400 notename = mton(midinote);
1401 }
1402 else {
1403 // midinote = 0;
1404 notename = "LOW";
1405 }
1406 markerText->setPlainText(QString("%1 Hz (%2)").arg((int)(peakFreq+0.5)).arg(notename));
1407
1408 marker->setVisible(true);
1409 markerText->setVisible(true);
1410 if(m_peakChannelPtr != nullptr) {
1411 *m_peakChannelPtr = peakFreq;
1412 }
1413 }
1414 else {
1415 if(m_getPeakChannel != nullptr) {
1416 *m_peakChannelPtr = 0;
1417 }
1418 }
1419 curveLock.unlock();
1420 }
1421
1422 void QuteGraph::drawSignalPath(Curve *curve, int index) {
1423 int curveSize = curve->get_size();
1424 QPainterPath path;
1425 auto zerodbfs = m_ud->zerodBFS;
1426 for(int i=0; i<curveSize; i++) {
1427 auto value = curve->get_data(i)/zerodbfs;
1428 path.lineTo(i, value);
1429 }
1430 QPainterPath grid;
1431 grid.moveTo(0, 0);
1432 grid.lineTo(curveSize, 0);
1433
1434 QGraphicsScene *scene = static_cast<QGraphicsView *>(static_cast<StackedLayoutWidget *>(m_widget)->widget(index))->scene();
1435 auto pen = QPen(QColor(255, 193, 7), 0);
1436 scene->clear();
1437 scene->addPath(grid, QPen(QColor(40, 40, 40), 0));
1438 scene->addPath(path, pen);
1439 }
1440
1441 void QuteGraph::drawSignal(Curve *curve, int index)
1442 {
1443 int curveSize = curve->get_size();
1444 QVector<QPointF> polygonPoints;
1445 polygonPoints.resize(curveSize + 2);
1446 polygonPoints[0] = QPointF(0,0);
1447 for (int i = 0; i < (int) curveSize; i++) {
1448 double value = curve->get_data(i)/m_ud->zerodBFS;
1449 polygonPoints[i + 1] = QPointF(i, value); //skip first item, which is base line
1450 }
1451 polygonPoints.back() = QPointF(curveSize - 1,0);
1452 polygons[index]->setPolygon(QPolygonF(polygonPoints));
1453 auto pen = QPen(QColor(255, 193, 7));
1454 pen.setCosmetic(true);
1455 polygons[index]->setPen(pen);
1456 polygons[index]->setBrush(Qt::NoBrush);
1457 m_pageComboBox->setItemText(index, curve->get_caption());
1458 }
1459
1460 void QuteGraph::scaleGraph(int index)
1461 {
1462 auto curve = curves[index];
1463
1464 double max = curves[index]->get_max();
1465 double min = curves[index]->get_min();
1466 double zoomx = property("QCS_zoomx").toDouble();
1467 double zoomy = property("QCS_zoomy").toDouble();
1468 // double span = max - min;
1469 // FIXME implement dispx, dispy and modex, modey
1470 int size = curve->get_size();
1471 double sizef = (double)size;
1472 auto view = getView(index);
1473 // view->setResizeAnchor(QGraphicsView::NoAnchor);
1474 auto graphType = graphtypes[index];
1475 if(graphType == GraphType::GRAPH_FTABLE && max != min) {
1476 double yrange = max - min;
1477 double factor = 1.17;
1478 view->setSceneRect(0, -max*factor - 0.05, sizef, yrange*factor);
1479 view->fitInView(0, -max*factor/zoomy, sizef/zoomx, yrange*factor/zoomy);
1480 } else if(graphType == GraphType::GRAPH_SPECTRUM) {
1481 double dbRange = m_dbRange;
1482 view->setSceneRect (0, -3, size, dbRange);
1483 view->fitInView(0, -3/zoomy, sizef/zoomx, dbRange/zoomy);
1484 } else { //from display opcode
1485 view->setSceneRect (0, -1, size, 2);
1486 // view->fitInView(0, -10./zoomy, (double) size/zoomx, 10./zoomy);
1487 view->fitInView(0, -2./zoomy, sizef/zoomx, 2./zoomy);
1488 }
1489
1490 }
1491
1492 int QuteGraph::getTableNumForIndex(int index) {
1493 if (index < 0 || index >= curves.size() || curves.size() <= 0) {
1494 // Invalid index
1495 return -1;
1496 }
1497 int ftable = -1;
1498 if(graphtypes[index] == GraphType::GRAPH_FTABLE) {
1499 QString caption = curves[index]->get_caption();
1500 ftable= caption.mid(caption.indexOf(" ") + 1,
1501 caption.indexOf(":") - caption.indexOf(" ") - 1).toInt();
1502 }
1503 return ftable;
1504 }
1505
1506 int QuteGraph::getIndexForTableNum(int ftable)
1507 {
1508 int index = -1;
1509 for (int i = 0; i < curves.size(); i++) {
1510 QString text = curves[i]->get_caption();
1511 if (text.contains("ftable")) {
1512 QStringList parts = text.split(QRegExp("[ :]"), QString::SkipEmptyParts);
1513 if (parts.size() > 1) {
1514 int num = parts.last().toInt();
1515 if (ftable == num) {
1516 index = i;
1517 break;
1518 }
1519 }
1520 }
1521 }
1522 return index;
1523 }
1524
1525 void QuteGraph::setInternalValue(double value)
1526 {
1527 m_value = value;
1528 m_valueChanged = true;
1529 }
1530
1531 void QuteGraph::showScrollbars(bool show) {
1532 auto policy = show ? Qt::ScrollBarAsNeeded : Qt::ScrollBarAlwaysOff;
1533 for(int i=0; i < this->curves.size(); i++) {
1534 auto view = getView(i);
1535 view->setHorizontalScrollBarPolicy(policy);
1536 }
1537 m_showScrollbars = show;
1538 }
1539
1540 // ----------------------
1541 QuteTableWidget::~QuteTableWidget() {
1542 };
1543
1544 void QuteTableWidget::reset() {
1545 // This needs to be called with the lock
1546 m_tabnum = 0;
1547 m_running = false;
1548 m_data = nullptr;
1549 m_tabsize = 0;
1550 if(m_autorange) {
1551 m_maxy = 1.0;
1552 m_miny = -1.0;
1553 }
1554 #if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
1555 m_path.clear();
1556 #else
1557 m_path = QPainterPath(); // not sure if it works
1558 #endif
1559 }
1560
1561
1562 void QuteTableWidget::paintGrid(QPainter *painter) {
1563 int margin = m_margin;
1564 QString maxystr, minystr;
1565 auto rect = this->rect();
1566
1567 if(m_maxy >= 10)
1568 maxystr = QString::number((int)m_maxy);
1569 else if(m_maxy >= 1)
1570 maxystr = QString::number(m_maxy, 'f', 1);
1571 else
1572 maxystr = QString::number(m_maxy, 'f', 2);
1573 double absminy = abs(m_miny);
1574 if(absminy >= 10)
1575 minystr = QString::number((int)m_miny);
1576 else if(absminy >= 1)
1577 minystr = QString::number(m_miny, 'f', 1);
1578 else
1579 minystr = QString::number(m_miny, 'f', 2);
1580
1581 const int yoffset = 0;
1582 auto x0 = rect.x() + margin;
1583 auto x1 = rect.x() + rect.width() - margin + 1;
1584 auto y0 = rect.y() + margin + yoffset;
1585 auto y1 = rect.y() + rect.height() - margin + yoffset ;
1586 auto height = rect.height() - margin*2;
1587 double yscale = -height / (m_maxy - m_miny);
1588
1589 auto tabsizestr = QString::number(m_tabsize);
1590 const int textMargin = 4;
1591 painter->setBrush(Qt::NoBrush);
1592 painter->setPen(QPen(QColor(96, 96, 96), 0));
1593
1594 // upper line
1595 painter->drawLine(rect.x() + gridFontMetrics.boundingRect(maxystr).width()+textMargin*2, y0, x1, y0);
1596 // lower line
1597 painter->drawLine(x0, y1, x1 - gridFontMetrics.boundingRect(tabsizestr).width() - 2, y1);
1598
1599 // 0 line
1600 if (m_maxy > 0 && m_miny < 0) {
1601 int yzero = static_cast<int>(-m_miny * yscale + (y0+height));
1602 painter->drawLine(x0, yzero, x1, yzero);
1603 painter->setPen(QColor(200, 200, 200));
1604 painter->drawText(rect.x(), yzero, "0");
1605 }
1606
1607 painter->setPen(QColor(200, 200, 200));
1608 painter->setFont(gridFont);
1609 painter->drawText(rect.x()+textMargin, y0+textMargin, maxystr);
1610 painter->drawText(rect, Qt::AlignLeft|Qt::AlignBottom, minystr);
1611
1612 // right padding
1613 rect.setWidth(rect.width() - textMargin);
1614 painter->drawText(rect, Qt::AlignRight|Qt::AlignBottom, tabsizestr);
1615
1616 }
1617
1618
1619 void QuteTableWidget::paintEvent(QPaintEvent *event) {
1620 QPainter painter(this);
1621 painter.setRenderHint(QPainter::Antialiasing);
1622 painter.setPen(Qt::NoPen);
1623 if(!m_running) {
1624 painter.setBrush(QColor(176, 0, 32));
1625 painter.drawRect(this->rect());
1626 painter.setPen(Qt::white);
1627 painter.drawText(this->rect(), Qt::AlignCenter, "Stopped");
1628 return;
1629 }
1630 if(m_tabnum <= 0) {
1631 // table not set
1632 painter.setBrush(QColor(0, 176, 32));
1633 painter.drawRect(this->rect());
1634 painter.setPen(Qt::white);
1635 painter.drawText(this->rect(), Qt::AlignCenter, "Table not set");
1636 return;
1637 }
1638 // running OK
1639 painter.setBrush(QColor(24, 24, 24));
1640 painter.drawRect(this->rect());
1641 painter.setBrush(Qt::NoBrush);
1642 blockSignals(true);
1643 mutex.lock();
1644 if(m_showGrid) {
1645 this->paintGrid(&painter);
1646 }
1647 painter.setPen(QPen(m_color, 0));
1648 painter.drawPath(m_path);
1649 mutex.unlock();
1650 blockSignals(false);
1651 }
1652
1653 void QuteTableWidget::setRange(double maxy) {
1654 if(maxy == 0.0) {
1655 m_autorange = true;
1656 } else {
1657 m_autorange = false;
1658 m_maxy = maxy;
1659 m_miny = -maxy;
1660 }
1661 }
1662
1663 void QuteTableWidget::updatePath() {
1664 if(!m_running || m_tabnum <= 0) {
1665 return;
1666 }
1667
1668 MYFLT *data;
1669 int tabsize = csoundGetTable(m_ud->csound, &data, m_tabnum);
1670 if(tabsize == 0 || data == nullptr) {
1671 QDEBUG << "Table not found" << m_tabnum;
1672 return;
1673 }
1674
1675 m_tabsize = tabsize;
1676 int margin = m_margin;
1677
1678 auto rect = this->rect();
1679 auto width = rect.width() - margin*2;
1680 auto height = rect.height() - margin*2;
1681
1682
1683 double xscale = width / (double)tabsize;
1684 double y0 = rect.y() + margin;
1685 double x0 = rect.x() + margin;
1686
1687 int step = tabsize / width;
1688 if(step == 0)
1689 step = 1;
1690
1691 double maxy = m_maxy;
1692 double miny = m_miny;
1693
1694 if(m_autorange) {
1695 double newmaxy = m_maxy;
1696 double newminy = m_miny;
1697 for(int i=0; i < tabsize; i += step) {
1698 double y = data[i];
1699 if(y > newmaxy)
1700 newmaxy = y;
1701 else if (y < newminy)
1702 newminy = y;
1703 }
1704 maxy = m_maxy = ceil(newmaxy);
1705 miny = m_miny = floor(newminy);
1706 }
1707
1708 #if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
1709 m_path.clear();
1710 #else
1711 m_path = QPainterPath(); // not sure if it works
1712 #endif
1713 QPolygonF poly;
1714 double yscale = -height / (maxy-miny);
1715
1716 double ydata = data[0];
1717 poly.append(QPoint(x0, (ydata-miny)*yscale+y0+height));
1718
1719 for(int i=1; i < tabsize; i+=step) {
1720 ydata = data[i];
1721 double x2 = i*xscale + x0;
1722 double y2 = (ydata - miny) * yscale + (y0+height);
1723 // path->lineTo(x2, y2);
1724 poly.append(QPointF(x2, y2));
1725 }
1726 m_path.addPolygon(poly);
1727 }
1728
1729 void QuteTableWidget::updateData(int tabnum) {
1730 QMutexLocker locker(&mutex);
1731 if(!m_running) {
1732 return;
1733 }
1734 else if(tabnum >= 0 && tabnum != m_tabnum) {
1735 if(m_autorange) {
1736 m_maxy = 1.0;
1737 m_miny = 0;
1738 }
1739 m_tabnum = tabnum;
1740 }
1741 this->updatePath();
1742 this->update();
1743 }
1744
1745
1746 // -------------------------
1747
1748 QuteTable::~QuteTable() {};
1749
1750 // void QuteTable::mousePressEvent(QMouseEvent *event) {};
1751 // void QuteTable::mouseReleaseEvent(QMouseEvent *event) {};
1752
1753 QuteTable::QuteTable(QWidget *parent) : QuteWidget(parent) {
1754 m_widget = new QuteTableWidget(this);
1755 m_value = 0;
1756 m_tabnum = 0;
1757 // auto w = static_cast<QuteTableWidget*>(m_widget);
1758 setProperty("QCS_randomizable", false);
1759 m_widget->setContextMenuPolicy(Qt::NoContextMenu);
1760 m_widget->setMouseTracking(true);
1761 setProperty("QCS_range", 0.0);
1762 setColor(QColor(255, 193, 3));
1763 setProperty("QCS_showGrid", true);
1764 }
1765
1766 void QuteTable::setColor(QColor color) {
1767 setProperty("QCS_color", color);
1768 static_cast<QuteTableWidget*>(m_widget)->setColor(color);
1769 }
1770
1771 void QuteTable::applyInternalProperties() {
1772 QuteWidget::applyInternalProperties();
1773 auto w = static_cast<QuteTableWidget*>(m_widget);
1774 auto color = property("QCS_color").value<QColor>();
1775 w->setColor(color);
1776 w->setRange(property("QCS_range").toDouble());
1777 w->showGrid(property("QCS_showGrid").toBool());
1778 }
1779
1780 void QuteTable::createPropertiesDialog() {
1781 QuteWidget::createPropertiesDialog();
1782 dialog->setWindowTitle("Table Plot");
1783 auto label = new QLabel(dialog);
1784 label->setText("Color");
1785 layout->addWidget(label, 4, 0, Qt::AlignRight|Qt::AlignVCenter);
1786
1787 colorButton = new SelectColorButton(dialog);
1788 colorButton->setColor(property("QCS_color").value<QColor>());
1789 layout->addWidget(colorButton, 4, 1, Qt::AlignLeft|Qt::AlignVCenter);
1790
1791 double range = property("QCS_range").toDouble();
1792 rangeCheckBox = new QCheckBox("Fixed Range", dialog);
1793 rangeCheckBox->setToolTip("If checked the range is fixed to the value set on the right."
1794 "If the table has values outside this range these will not be"
1795 "displayed");
1796 rangeCheckBox->setChecked(range > 0.0);
1797 layout->addWidget(rangeCheckBox, 5, 1, Qt::AlignRight|Qt::AlignVCenter);
1798
1799 rangeSpinBox = new QDoubleSpinBox(dialog);
1800 rangeSpinBox->setRange(0.1, 999999.0);
1801 rangeSpinBox->setSingleStep(0.1);
1802 rangeSpinBox->setToolTip("Sets the max. range to plot. Disable this to use autorange"
1803 "With a fixed range, values outside the range will not be "
1804 "visible");
1805 rangeSpinBox->setValue(range > 0 ? range : 1.0);
1806 layout->addWidget(rangeSpinBox, 5, 2, Qt::AlignLeft|Qt::AlignVCenter);
1807 connect(rangeCheckBox, SIGNAL(toggled(bool)), rangeSpinBox, SLOT(setEnabled(bool)));
1808
1809 gridCheckBox = new QCheckBox("Show Grid", dialog);
1810 gridCheckBox->setChecked(property("QCS_showGrid").toBool());
1811 layout->addWidget(gridCheckBox, 6, 1, Qt::AlignLeft|Qt::AlignVCenter);
1812
1813 }
1814
1815 void QuteTable::applyProperties() {
1816 #ifdef USE_WIDGET_MUTEX
1817 widgetLock.lockForWrite();
1818 #endif
1819 setProperty("QCS_color", colorButton->getColor());
1820 bool fixedrange = rangeCheckBox->isChecked();
1821 double range = rangeSpinBox->value();
1822 if(fixedrange) {
1823 Q_ASSERT(range > 0.0);
1824 setProperty("QCS_range", range);
1825 } else {
1826 setProperty("QCS_range", 0.0);
1827 }
1828 setProperty("QCS_showGrid", gridCheckBox->isChecked());
1829
1830 #ifdef USE_WIDGET_MUTEX
1831 widgetLock.unlock();
1832 #endif
1833 QuteWidget::applyProperties();
1834 }
1835
1836 void QuteTable::refreshWidget() {
1837 // Q_ASSERT(m_valueChanged);
1838 QMutexLocker locker(&mutex);
1839 m_valueChanged = false;
1840 auto status = csoundEngineStatus(m_csoundUserData);
1841 m_csoundRunning = status == CsoundEngineStatus::Running;
1842 auto w = static_cast<QuteTableWidget*>(m_widget);
1843 w->setRunningStatus(m_csoundRunning);
1844 if(status != CsoundEngineStatus::Running) {
1845 return;
1846 }
1847 w->updateData(m_tabnum);
1848 }
1849
1850 QString QuteTable::getWidgetXmlText() {
1851 xmlText = "";
1852 QXmlStreamWriter s(&xmlText);
1853 createXmlWriter(s); // --------- start
1854
1855 QColor color = property("QCS_color").value<QColor>();
1856 s.writeStartElement("color");
1857 s.writeTextElement("r", QString::number(color.red()));
1858 s.writeTextElement("g", QString::number(color.green()));
1859 s.writeTextElement("b", QString::number(color.blue()));
1860 s.writeEndElement();
1861
1862 auto range = property("QCS_range").toDouble();
1863 s.writeTextElement("range", QString::number(range, 'f', 2));
1864
1865 s.writeEndElement(); // --------- end
1866 return xmlText;
1867 }
1868
1869 void QuteTable::onStop() {
1870 // this->blockSignals(true);
1871 mutex.lock();
1872 m_tabnum = 0;
1873 m_value = 0;
1874 m_valueChanged = true;
1875
1876 static_cast<QuteTableWidget*>(m_widget)->stop(m_csoundUserData);
1877 mutex.unlock();
1878 // this->applyInternalProperties();
1879 // this->blockSignals(false);
1880 }
1881
1882 void QuteTable::setCsoundUserData(CsoundUserData *ud) {
1883 if(ud == nullptr) {
1884 qDebug()<<"CsoundUserData is null";
1885 return;
1886 }
1887 m_csoundUserData = ud;
1888 auto w = static_cast<QuteTableWidget*>(m_widget);
1889 if(w == nullptr) {
1890 qDebug() << "widget is null";
1891 return;
1892 }
1893 w->setUserData(ud);
1894 connect(ud->csEngine, SIGNAL(stopSignal()), this, SLOT(onStop()));
1895 }
1896
1897 /*
1898 void QuteTable::setWidgetGeometry(int x, int y, int width, int height) {
1899
1900 if(csoundEngineStatus(m_csoundUserData)==CsoundEngineStatus::Running) {
1901 return;
1902 }
1903 QuteWidget::setWidgetGeometry(x,y,width, height);
1904 }
1905 */
1906
1907 void QuteTable::setTableNumber(int tabnum) {
1908 if(tabnum == m_tabnum)
1909 return;
1910 m_valueChanged = true;
1911 m_value = tabnum;
1912 m_tabnum = tabnum;
1913 auto w = static_cast<QuteTableWidget*>(m_widget);
1914 w->updateData(m_tabnum);
1915 }
1916
1917 void QuteTable::setValue(double value) {
1918 if(value == 0) {
1919 m_value = m_tabnum;
1920 return;
1921 }
1922 else if(value == -1) {
1923 if(m_tabnum <= 0) {
1924 QDEBUG << "Table number not set, can't update";
1925 return;
1926 }
1927 // update data, don't change table number
1928 m_valueChanged = true;
1929 // auto w = static_cast<QuteTableWidget*>(m_widget);
1930 // w->updateData(m_tabnum);
1931 return;
1932 }
1933 else if(m_value == value) {
1934 return;
1935 }
1936 else if(value > 0) {
1937 setTableNumber(static_cast<int>(value));
1938 }
1939 else {
1940 qDebug() << "Invalid value for TablePlot:" << value;
1941 return;
1942 }
1943 };
1944
1945 void QuteTable::setValue(QString s) {
1946 auto parts = s.splitRef(' ', SKIP_EMPTY_PARTS);
1947 if(parts.size() == 0) {
1948 qWarning() << "TablePLot: Message not understood, expected @set <tabnum> "
1949 "or @update";
1950 return;
1951 }
1952 if(parts[0] == "@set") {
1953 if(parts.size() != 2) {
1954 qWarning() << "@set message expects a table number (example: @set 103)";
1955 return;
1956 }
1957 int tabnum = parts[1].toInt();
1958 setTableNumber(tabnum);
1959 } else if (parts[0] == "@update" && m_tabnum > 0) {
1960 setValue(-1);
1961 } else
1962 qWarning() << "Message not supported:" << s;
1963 }
1964
1965 SpectralView::~SpectralView() {};
1966
1967 void SpectralView::mouseReleaseEvent(QMouseEvent *ev) {
1968 emit mouseReleased();
1969 QGraphicsView::mouseReleaseEvent(ev);
1970 }