Debian Codegraph @

src/widgetlayout.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2010 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 <cstdlib>
24
25 #include <QThread>
26
27 #include "widgetlayout.h"
28 #include "qutewidget.h"
29 #include "quteslider.h"
30 #include "qutetext.h"
31 #include "qutespinbox.h"
32 #include "qutebutton.h"
33 #include "quteknob.h"
34 #include "qutecheckbox.h"
35 #include "qutecombobox.h"
36 #include "qutemeter.h"
37 #include "quteconsole.h"
38 #include "qutegraph.h"
39 #include "qutescope.h"
40 #include "qutedummy.h"
41 #include "framewidget.h"
42
43 #include "qutecsound.h" // For passing the actions from button reserved channels
44
45
46 WidgetLayout::WidgetLayout(QWidget* parent) : QWidget(parent)
47 {
48 selectionFrame = new QRubberBand(QRubberBand::Rectangle, this);
49 selectionFrame->hide();
50 m_trackMouse = true;
51 m_editMode = false;
52 m_enableEdit = true;
53 m_xmlFormat = true;
54 m_currentPreset = -1;
55 m_activeWidgets = 0;
56 m_updateRate = 30;
57
58 auto palette = qApp->palette();
59 auto isLightTheme = palette.text().color().lightness() < palette.window().color().lightness();
60
61 m_modified = false;
62 closing = 0;
63 mouseX = mouseY = mouseRelX = mouseRelY = mouseBut1 = mouseBut2 = 0;
64 m_posx = m_posy = m_w = m_h = 0;
65 xOffset = yOffset = 0;
66 mouseRelX = mouseRelY = 0;
67 m_contained = false;
68
69 midiWriteCounter = 0;
70 midiReadCounter = 0;
71 midiQueue.resize(QCS_MAX_MIDI_QUEUE);
72 for (int i = 0; i <QCS_MAX_MIDI_QUEUE; i++ ) {
73 midiQueue[i].resize(3);
74 }
75
76 curveUpdateBuffer.resize(QCS_CURVE_BUFFER_SIZE);
77 curveUpdateBufferCount = 0;
78
79 createSliderAct = new QAction(tr("Slider"),this);
80 connect(createSliderAct, SIGNAL(triggered()), this, SLOT(createNewSlider()));
81 createLabelAct = new QAction(tr("Label"),this);
82 connect(createLabelAct, SIGNAL(triggered()), this, SLOT(createNewLabel()));
83 createDisplayAct = new QAction(tr("Display"),this);
84 connect(createDisplayAct, SIGNAL(triggered()), this, SLOT(createNewDisplay()));
85 createScrollNumberAct = new QAction(tr("ScrollNumber"),this);
86 connect(createScrollNumberAct, SIGNAL(triggered()),
87 this, SLOT(createNewScrollNumber()));
88 createLineEditAct = new QAction(tr("LineEdit"),this);
89
90 connect(createLineEditAct, SIGNAL(triggered()), this, SLOT(createNewLineEdit()));
91
92 createSpinBoxAct = new QAction(tr("SpinBox"),this);
93 connect(createSpinBoxAct, SIGNAL(triggered()), this, SLOT(createNewSpinBox()));
94
95 createButtonAct = new QAction(tr("Button"),this);
96 connect(createButtonAct, SIGNAL(triggered()), this, SLOT(createNewButton()));
97
98 createKnobAct = new QAction(tr("Knob"),this);
99 connect(createKnobAct, SIGNAL(triggered()), this, SLOT(createNewKnob()));
100
101 createCheckBoxAct = new QAction(tr("Checkbox"),this);
102 connect(createCheckBoxAct, SIGNAL(triggered()), this, SLOT(createNewCheckBox()));
103
104 createMenuAct = new QAction(tr("Menu"),this);
105 connect(createMenuAct, SIGNAL(triggered()), this, SLOT(createNewMenu()));
106
107 createMeterAct = new QAction(tr("Controller"),this);
108 connect(createMeterAct, SIGNAL(triggered()), this, SLOT(createNewMeter()));
109
110 createConsoleAct = new QAction(tr("Console"),this);
111 connect(createConsoleAct, SIGNAL(triggered()), this, SLOT(createNewConsole()));
112
113 createGraphAct = new QAction(tr("Graph"),this);
114 connect(createGraphAct, SIGNAL(triggered()), this, SLOT(createNewGraph()));
115
116 createScopeAct = new QAction(tr("Scope"),this);
117 connect(createScopeAct, SIGNAL(triggered()), this, SLOT(createNewScope()));
118
119 createTableDisplayAct = new QAction(tr("Table Plot"), this);
120 connect(createTableDisplayAct, SIGNAL(triggered()), this, SLOT(createNewTableDisplay()));
121
122 propertiesAct = new QAction(tr("Properties"),this);
123 connect(propertiesAct, SIGNAL(triggered()), this, SLOT(propertiesDialog()));
124
125 reloadWidgetsAct = new QAction(tr("Reload Widgets"), this);
126 connect(reloadWidgetsAct, SIGNAL(triggered()), this, SLOT(reloadWidgets()));
127
128 cutAct = new QAction(tr("Cut"), this);
129 // cutAct->setShortcut(tr("Ctrl+X"));
130 connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
131 copyAct = new QAction(tr("Copy"), this);
132 // copyAct->setShortcut(tr("Ctrl+C"));
133 connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
134 pasteAct = new QAction(tr("Paste"), this);
135 // pasteAct->setShortcut(tr("Ctrl+V"));
136 connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
137 duplicateAct = new QAction(tr("Duplicate Selected"), this);
138 connect(duplicateAct, SIGNAL(triggered()), this, SLOT(duplicate()));
139 deleteAct = new QAction(tr("Delete Selected"), this);
140 connect(deleteAct, SIGNAL(triggered()), this, SLOT(deleteSelected()));
141 clearAct = new QAction(tr("Clear all widgets"), this);
142 connect(clearAct, SIGNAL(triggered()), this, SLOT(clearWidgets()));
143 selectAllAct = new QAction(tr("Select all widgets"), this);
144 connect(selectAllAct, SIGNAL(triggered()), this, SLOT(selectAll()));
145
146 QString iconPrefix = isLightTheme ? ":/themes/breeze/" : ":/themes/breeze-dark/";
147
148 alignLeftAct = new QAction(QIcon(iconPrefix + "align-horizontal-left.svg"),
149 tr("Align Left"), this);
150 connect(alignLeftAct, SIGNAL(triggered()), this, SLOT(alignLeft()));
151
152 alignRightAct = new QAction(QIcon(iconPrefix + "align-horizontal-right.svg"),
153 tr("Align Right"), this);
154 connect(alignRightAct, SIGNAL(triggered()), this, SLOT(alignRight()));
155
156 alignTopAct = new QAction(QIcon(iconPrefix + "align-vertical-top.svg"),
157 tr("Align Top"), this);
158 connect(alignTopAct, SIGNAL(triggered()), this, SLOT(alignTop()));
159
160 alignBottomAct = new QAction(QIcon(iconPrefix + "align-vertical-bottom.svg"),
161 tr("Align Bottom"), this);
162 connect(alignBottomAct, SIGNAL(triggered()), this, SLOT(alignBottom()));
163
164 sendToBackAct = new QAction(QIcon(iconPrefix + "object-order-back.svg"),
165 tr("Send to back"), this);
166 connect(sendToBackAct, SIGNAL(triggered()), this, SLOT(sendToBack()));
167
168 sendToFrontAct = new QAction(QIcon(iconPrefix + "object-order-front.svg"),
169 tr("Send to Front"), this);
170 connect(sendToFrontAct, SIGNAL(triggered()), this, SLOT(sendToFront()));
171
172 distributeHorizontalAct = new QAction(tr("Distribute Horizontally"), this);
173 connect(distributeHorizontalAct, SIGNAL(triggered()),
174 this, SLOT(distributeHorizontal()));
175
176 distributeVerticalAct = new QAction(tr("Distribute Vertically"), this);
177 connect(distributeVerticalAct, SIGNAL(triggered()),
178 this, SLOT(distributeVertical()));
179
180 alignCenterHorizontalAct = new QAction(QIcon(iconPrefix + "align-horizontal-center.svg"),
181 tr("Center Horizontally"), this);
182 connect(alignCenterHorizontalAct, SIGNAL(triggered()),
183 this, SLOT(alignCenterHorizontal()));
184
185 alignCenterVerticalAct = new QAction(QIcon(iconPrefix + "align-vertical-center.svg"),
186 tr("Center Vertically"), this);
187 connect(alignCenterVerticalAct, SIGNAL(triggered()),
188 this, SLOT(alignCenterVertical()));
189
190 storePresetAct = new QAction(tr("Store Preset"), this);
191 connect(storePresetAct, SIGNAL(triggered()), this, SLOT(savePreset()));
192 newPresetAct = new QAction(tr("New Preset"), this);
193 connect(newPresetAct, SIGNAL(triggered()), this, SLOT(newPreset()));
194 recallPresetAct = new QAction(tr("Recall Preset"), this);
195 connect(recallPresetAct, SIGNAL(triggered()), this, SLOT(loadPreset()));
196
197 setFocusPolicy(Qt::StrongFocus);
198
199 setMouseTracking(true);
200
201 // Default values for properties
202 m_objectName = "";
203 setWindowTitle("Widgets");
204 m_uuid = "";
205 m_visible = true;
206
207 // Set background ON by default, since any change in the app's palette (light/dark
208 // mode) will change this background and make many existing scripts/examples
209 // unreadable.
210 setBackground(true, QColor(240, 240, 240));
211 m_updating = true;
212 updateData(); // Starts updateData timer
213
214 m_widgetNameToType["BSBSpinBox"] = QuteWidgetType::SPINBOX;
215 m_widgetNameToType["BSBLineEdit"] = QuteWidgetType::LINEEDIT;
216 m_widgetNameToType["BSBCheckBox"] = QuteWidgetType::CHECKBOX;
217 m_widgetNameToType["BSBSlider"] = QuteWidgetType::SLIDER;
218 m_widgetNameToType["BSBKnob"] = QuteWidgetType::KNOB;
219 m_widgetNameToType["BSBScrollNumber"] = QuteWidgetType::SCROLLNUMBER;
220 m_widgetNameToType["BSBButton"] = QuteWidgetType::BUTTON;
221 m_widgetNameToType["BSBDropDown"] = QuteWidgetType::DROPDOWN;
222 m_widgetNameToType["BSBController"] = QuteWidgetType::CONTROLLER;
223 m_widgetNameToType["BSBGraph"] = QuteWidgetType::GRAPH;
224 m_widgetNameToType["BSBScope"] = QuteWidgetType::SCOPE;
225 m_widgetNameToType["BSBConsole"] = QuteWidgetType::CONSOLE;
226 m_widgetNameToType["BSBTableDisplay"] = QuteWidgetType::TABLEDISPLAY;
227 }
228
229 WidgetLayout::~WidgetLayout()
230 {
231 disconnect(this, 0,0,0);
232 layoutMutex.lock();
233 closing = 1;
234 layoutMutex.unlock();
235 while (closing == 1) {
236 qApp->processEvents();
237 QThread::usleep(10000);
238 }
239 clearGraphs(); // To free memory from curves.
240 }
241
242 //unsigned int WidgetLayout::widgetCount()
243 //{
244 // widgetsMutex.lock();
245 // unsigned int number = m_widgets.size();
246 // widgetsMutex.unlock();
247 // return number;
248 //}
249
250 QuteWidgetType WidgetLayout::widgetNameToType(QString widgetName) {
251 if(!m_widgetNameToType.contains(widgetName))
252 return QuteWidgetType::UNKNOWN;
253 return m_widgetNameToType[widgetName];
254 }
255
256 void WidgetLayout::loadXmlWidgets(QString xmlWidgets)
257 {
258 m_xmlFormat = true;
259 clearWidgetLayout();
260 QDomDocument doc;
261 if (!doc.setContent(xmlWidgets)) {
262 QMessageBox::warning(this, tr("Widget Error"),
263 tr("Widgets can't be read! No widgets created."));
264 qDebug() << "WidgetLayout::loadXmlWidgets Error parsing xml text! Aborting.";
265 return;
266 }
267 QDomNodeList panel = doc.elementsByTagName("bsbPanel");
268 if (panel.size() > 1) {
269 QMessageBox::warning(this, tr("More than one panel"),
270 tr("The csd file contains more than one widget panel!\n"
271 "This is not supported by the current version,\n"
272 "Additional widget panels will be lost if the file is saved!"));
273 }
274 QDomNode p = panel.item(0);
275 if (p.isNull()) {
276 qDebug() << "WidgetLayout::loadXmlWidgets no bsbPanel element! Aborting.";
277 return;
278 }
279 QDomNodeList c = p.childNodes();
280 int version = 0;
281 for (int i = 0; i < c.size(); i++) {
282 // auto t0 = std::chrono::high_resolution_clock::now();
283 auto item = c.item(i);
284 int ret = parseXmlNode(item);
285 // auto t1 = std::chrono::high_resolution_clock::now();
286 // auto diff = std::chrono::duration<double, std::milli>(t1-t0).count();
287 // QDEBUG << "parseXmlNode" << item.toElement().attribute("type") << diff << "ms";
288 if (ret == -1) {
289 qDebug() << "WidgetLayout::loadXmlWidgets Error in Xml node parsing";
290 QMessageBox::warning(this, tr("Unrecognized wigdet format"),
291 tr("There is unrecognized widget information in the file\n"
292 "It may be saved with errors."));
293 }
294 if (ret > version) {
295 version = ret;
296 }
297 }
298 if (version > QString(QCS_CURRENT_XML_VERSION).toInt()) {
299 qDebug() << "WidgetLayout::loadXmlWidgets Newer Widget Format version";
300 QMessageBox::warning(this, tr("Newer Widget Format"),
301 tr("The file was saved by a more recent version of CsoundQt.\n"
302 "Some features may not be available and will not be saved!"));
303 }
304 else if (version < QString(QCS_CURRENT_XML_VERSION).toInt()) { // Just print a silent warning
305 qDebug() << "Older widget format.";
306 }
307 if (m_editMode) {
308 setEditMode(true);
309 }
310
311 // if (m_contained) {
312 // adjustLayoutSize();
313 // }
314 // else {
315 // this->move(m_posx, m_posy);
316 // this->resize(m_w, m_h);
317 // // setOuterGeometry();
318 // }
319 qDebug() << "Finished loading xml widgets";
320 }
321
322 void WidgetLayout::loadXmlPresets(QString xmlPresets)
323 {
324 QDomDocument doc;
325 if (!doc.setContent(xmlPresets)) {
326 qDebug() << "WidgetLayout::loadXmlPresets Error parsing preset text.";
327 }
328 QDomElement pl = doc.firstChildElement("bsbPresets");
329 QDomNodeList presetElements = pl.elementsByTagName("preset");
330 QList<int> usedNumbers;
331 for (int i = 0; i < presetElements.size(); i++) {
332 QDomElement presetElement = presetElements.item(i).toElement();
333 int newNumber = presetElement.attribute("number", "0").toInt();
334 if (!usedNumbers.contains(newNumber)) {
335 WidgetPreset newPreset;
336 newPreset.setName(presetElement.attribute("name", ""));
337 newPreset.setNumber(newNumber);
338 QDomNodeList presetValues = presetElement.elementsByTagName("value");
339 for (int i = 0; i < presetValues.size(); i++) {
340 QDomElement valueElement = presetValues.item(i).toElement();
341 QString id = valueElement.attribute("id", "");
342 int mode = valueElement.attribute("mode", "0").toInt();
343 if (mode & 1) {
344 double val = valueElement.text().toDouble();
345 newPreset.addValue(id, val);
346 }
347 if (mode & 2) {
348 double val = valueElement.text().toDouble();
349 newPreset.addValue2(id, val);
350 }
351 if (mode & 4) {
352 QString val = valueElement.text();
353 newPreset.addStringValue(id, val);
354 }
355 }
356 presets.append(newPreset);
357 usedNumbers.append(newNumber);
358 }
359 else {
360 qDebug() << "WidgetLayout::loadXmlPresets duplicate number. Ignored. " << newNumber;
361 }
362 }
363 }
364
365 void WidgetLayout::loadMacWidgets(QString macWidgets)
366 {
367 // m_xmlFormat = false;
368 clearWidgetLayout();
369 QStringList widgetLines = macWidgets.split(QRegExp("[\n\r]"), SKIP_EMPTY_PARTS);
370 foreach (QString line, widgetLines) {
371 if (line.startsWith("i")) {
372 if (newMacWidget(line) == "") {
373 qDebug() << "WidgetPanel::loadMacWidgets error processing line: " << line;
374 }
375 }
376 else {
377 if (!line.contains("<MacGUI>") && !line.contains("</MacGUI>"))
378 qDebug() << "WidgetPanel::loadMacWidgets error processing line: " << line;
379 }
380 }
381 if (m_editMode) {
382 setEditMode(true);
383 }
384 widgetChanged();
385 }
386
387 QString WidgetLayout::getWidgetsText()
388 {
389 // This function must be used with care as it accesses the widgets, which
390 // may cause crashing since widgets are not reentrant
391 QStringList txts = {"<bsbPanel>\n"};
392 layoutMutex.lock();
393 txts << "<label>" << windowTitle() << "</label>\n"
394 << "<objectName>" << m_objectName << "</objectName>\n"
395 << "<x>" << QString::number(m_posx) << "</x>\n"
396 << "<y>" << QString::number(m_posy) << "</y>\n"
397 << "<width>" << QString::number(m_w) << "</width>\n"
398 << "<height>" << QString::number(m_h) << "</height>\n"
399 << "<visible>" << (m_visible ? QString("true"):QString("false")) << "</visible>\n"
400 << "<uuid>" << m_uuid << "</uuid>\n";
401
402 QColor bgColor = this->property("QCS_bgcolor").value<QColor>();
403 auto bg = this->property("QCS_bg").toBool()? QString("background"):QString("nobackground");
404 txts << "<bgcolor mode=\"" << bg << "\">\n";
405 txts << "<r>"<<QString::number(bgColor.red())<<"</r>\n"
406 << "<g>"<<QString::number(bgColor.green())<<"</g>\n"
407 << "<b>"<<QString::number(bgColor.blue())<<"</b>\n";
408 txts << "</bgcolor>\n";
409
410 layoutMutex.unlock();
411 widgetsMutex.lock();
412 for (int i = 0; i < m_widgets.size(); i++) {
413 txts << m_widgets[i]->getWidgetXmlText() << "\n";
414 }
415 widgetsMutex.unlock();
416 txts << "</bsbPanel>";
417 return txts.join("");
418 }
419
420 QString WidgetLayout::getPresetsText()
421 {
422 QStringList txts = {"<bsbPresets>\n"};
423 for (int i = 0; i < presets.size(); i++) {
424 txts << presets[i].getXmlText();
425 }
426 txts << "</bsbPresets>";
427 return txts.join("");
428 }
429
430 QString WidgetLayout::getSelectedWidgetsText()
431 {
432 QString l;
433 l += "<bsbPanel>\n";
434 widgetsMutex.lock();
435 for (int i = 0; i < editWidgets.size(); i++) {
436 if (editWidgets[i]->isSelected()) {
437 l += m_widgets[i]->getWidgetXmlText() + "\n";
438 }
439 }
440 widgetsMutex.unlock();
441 l += "</bsbPanel>";
442 return l;
443 }
444
445 QString WidgetLayout::getMacWidgetsText()
446 {
447 // This function must be used with care as it accesses the widgets, which
448 // may cause crashing since widgets are not reentrant
449 QString text = "";
450 text = "<MacGUI>\n";
451 QString bg, color;
452 layoutMutex.lock();
453 QColor bgColor = this->property("QCS_bgcolor").value<QColor>();
454 bg = this->property("QCS_bg").toBool()? QString("background"):QString("nobackground");
455 color = QString::number((int) (bgColor.redF()*65535.)) + ", ";
456 color += QString::number((int) (bgColor.greenF()*65535.)) + ", ";
457 color += QString::number((int) (bgColor.blueF()*65535.));
458 layoutMutex.unlock();
459 text += "ioView " + bg + " {" + color +"}\n";
460
461 widgetsMutex.lock();
462 for (int i = 0; i < m_widgets.size(); i++) {
463 text += m_widgets[i]->getWidgetLine() + "\n";
464 // qDebug() << m_widgets[i]->getWidgetXmlText();
465 }
466 widgetsMutex.unlock();
467 text += "</MacGUI>";
468 return text;
469 }
470
471 QStringList WidgetLayout::getSelectedMacWidgetsText()
472 {
473 QStringList l;
474 widgetsMutex.lock();
475 for (int i = 0; i < editWidgets.size(); i++) {
476 if (editWidgets[i]->isSelected()) {
477 l << m_widgets[i]->getWidgetLine();
478 }
479 }
480 widgetsMutex.unlock();
481 return l;
482 }
483
484 QRect WidgetLayout::getOuterGeometry()
485 {
486 return QRect(m_posx, m_posy, m_w, m_h);
487 }
488
489 void WidgetLayout::setKeyRepeatMode(bool repeat)
490 {
491 m_repeatKeys = repeat;
492 }
493
494 void WidgetLayout::setValue(QString channelName, double value)
495 {
496 // qDebug() << "Setting channel" << channelName << value;
497 widgetsMutex.lock();
498 for (int i = 0; i < m_widgets.size(); i++) {
499 if (m_widgets[i]->getUuid() == channelName) {
500 m_widgets[i]->setValue(value);
501 qDebug() << "Setting channel via UUID" << channelName;
502 break;
503 }
504 else if (m_widgets[i]->getChannelName() == channelName) {
505 m_widgets[i]->setValue(value);
506 }
507 if (m_widgets[i]->getChannel2Name() == channelName) {
508 m_widgets[i]->setValue2(value);
509 }
510
511 }
512 widgetsMutex.unlock();
513 }
514
515
516 void WidgetLayout::setOuterGeometry(QRect r)
517 {
518 if (!r.isValid()) {
519 r = this->geometry();
520 m_posx = r.x();
521 m_posy = r.y();
522 m_w = r.width();
523 m_h = r.height();
524 return;
525 }
526 int newx = r.x();
527 int newy = r.y();
528 int neww = r.width();
529 int newh = r.height();
530 if (newx > 0 && newy > 0 ) {
531 m_posx = newx >= 0 && newx < 4096? newx : m_posx;
532 m_posy = newy >= 0 && newy < 4096? newy : m_posy;
533 m_w = neww >= 0 && neww < 4096? neww : m_w;
534 m_h = newh >= 0 && newh < 4096? newh : m_h;
535 }
536 }
537
538
539 void WidgetLayout::setValue(QString channelName, QString value)
540 {
541 widgetsMutex.lock();
542 for (int i = 0; i < m_widgets.size(); i++) {
543 if (m_widgets[i]->getChannelName() == channelName) {
544 m_widgets[i]->setValue(value);
545 }
546 if (m_widgets[i]->getUuid() == channelName) {
547 m_widgets[i]->setValue(value);
548 break;
549 }
550 // if (m_widgets[i]->getChannel2Name() == channelName) {
551 // m_widgets[i]->setValue2(value);
552 // }
553 }
554 widgetsMutex.unlock();
555 }
556
557 void WidgetLayout::setValue(int index, double value)
558 {
559 // there are two values for each widget
560 widgetsMutex.lock();
561 if (index < m_widgets.size() * 2) {
562 m_widgets[index/2]->setValue(value);
563 }
564 widgetsMutex.unlock();
565 }
566
567 void WidgetLayout::setValue(int index, QString value)
568 {
569 // there are two values for each widget
570 widgetsMutex.lock();
571 if (index < m_widgets.size() * 2) {
572 m_widgets[index/2]->setValue(value);
573 }
574 widgetsMutex.unlock();
575 }
576
577 QString WidgetLayout::getStringForChannel(QString channelName, bool *modified)
578 {
579 (void) modified;
580 for (int i = 0; i < m_activeWidgets ; i++) {
581 if (m_widgets[i]->getChannelName() == channelName) {
582 QString value = m_widgets[i]->getStringValue();
583 return value;
584 } else if (m_widgets[i]->getUuid() == channelName) {
585 QString value = m_widgets[i]->getStringValue();
586 return value;
587 }
588 }
589 return QString();
590 }
591
592 double WidgetLayout::getValueForChannel(QString channelName, bool *modified, double notfound)
593 {
594 (void) modified;
595 for (int i = 0; i < m_activeWidgets ; i++) {
596 if (m_widgets[i]->getChannelName() == channelName) {
597 double value = m_widgets[i]->getValue();
598 return value;
599 } else if (m_widgets[i]->getChannel2Name() == channelName) {
600 double value = m_widgets[i]->getValue2();
601 return value;
602 } else if (m_widgets[i]->getUuid() == channelName) {
603 double value = m_widgets[i]->getValue();
604 return value;
605 }
606 }
607 return notfound;
608 }
609
610 void WidgetLayout::getMouseValues(QVector<double> *values)
611 {
612 // values must have size of 6 for _MouseX _MouseY _MouseRelX _MouseRelY MouseBut1 and MouseBut2
613 if (this->isEnabled()) {
614 mouseLock.lockForRead();
615 (*values)[0] = getMouseX();
616 (*values)[1] = getMouseY();
617 (*values)[2] = getMouseRelX();
618 (*values)[3] = getMouseRelY();
619 (*values)[4] = getMouseBut1();
620 (*values)[5] = getMouseBut2();
621 mouseLock.unlock();
622 }
623 }
624
625 int WidgetLayout::getMouseX()
626 {
627 Q_ASSERT(mouseX >= 0 && mouseX < 4096);
628 return mouseX;
629 }
630
631 int WidgetLayout::getMouseY()
632 {
633 Q_ASSERT(mouseY >= 0 && mouseY < 4096);
634 return mouseY;
635 }
636 int WidgetLayout::getMouseRelX()
637 {
638 if (mouseRelX >= 0 && mouseRelX < 4096)
639 return mouseRelX;
640 else return 0;
641 }
642
643 int WidgetLayout::getMouseRelY()
644 {
645 if (mouseRelY >= 0 && mouseRelY < 4096)
646 return mouseRelY;
647 else return 0;
648 }
649
650 int WidgetLayout::getMouseBut1()
651 {
652 return mouseBut1;
653 }
654
655 int WidgetLayout::getMouseBut2()
656 {
657 return mouseBut2;
658 }
659
660 void WidgetLayout::setWidgetProperty(QString widgetid, QString property, QVariant value)
661 {
662 for (int i = 0; i < m_widgets.size(); i++) {
663 if ( (m_widgets[i]->getUuid() == widgetid) || (m_widgets[i]->getChannelName() == widgetid) ) {
664 m_widgets[i]->setProperty(property.toLocal8Bit(), value);
665 m_widgets[i]->applyInternalProperties();
666 widgetChanged();
667 }
668 }
669 }
670
671 QVariant WidgetLayout::getWidgetProperty(QString widgetid, QString property)
672 {
673 for (int i = 0; i < m_widgets.size(); i++) {
674 if ( (m_widgets[i]->getUuid() == widgetid) || (m_widgets[i]->getChannelName() == widgetid) ) {
675 return m_widgets[i]->property(property.toLocal8Bit());
676 }
677 }
678 return QVariant();
679 }
680
681 int WidgetLayout::newXmlWidget(QDomNode mainnode, bool offset, bool newId)
682 {
683 if (mainnode.isNull()) {
684 qDebug() << "WidgetLayout::newXmlWidget null element! Aborting.";
685 return -1;
686 }
687 auto t0 = std::chrono::high_resolution_clock::now();
688
689 int ret = 0;
690 auto element = mainnode.toElement();
691 QuteWidget *widget = nullptr;
692 QDomNodeList childNodes = mainnode.childNodes();
693 QString type = element.attribute("type");
694 bool forceBackground = false;
695 ret = element.attribute("version").toInt();
696 if (type == "BSBLabel" || type == "BSBDisplay") {
697
698 QuteText *w = new QuteText(this);
699 w->setTransparentForMouse(true);
700 w->setFontOffset(m_fontOffset);
701 w->setFontScaling(m_fontScaling);
702 w->setType(type == "BSBLabel" ? "label" : "display");
703 QDomElement ebg = element.firstChildElement("bgcolor");
704 if (!ebg.isNull()) {
705 if (element.attribute("mode")== "background") {
706 w->setProperty("QCS_bgcolormode", true);
707 }
708 w->setProperty("QCS_bgcolor", QVariant(getColorFromElement(ebg)));
709 }
710 widget = static_cast<QuteWidget *>(w);
711 }
712 else if (type == "BSBSpinBox") {
713 widget = static_cast<QuteSpinBox *>(new QuteSpinBox(this));
714 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
715 this, SLOT(newValue(QPair<QString,double>)));
716 }
717 else if (type == "BSBLineEdit") {
718 widget = static_cast<QuteLineEdit *>(new QuteLineEdit(this));
719 forceBackground = true;
720 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
721 this, SLOT(newValue(QPair<QString,double>)));
722 connect(widget, SIGNAL(newValue(QPair<QString,QString>)),
723 this, SLOT(newValue(QPair<QString,QString>)));
724 }
725 else if (type == "BSBCheckBox") {
726 widget = static_cast<QuteWidget *>(new QuteCheckBox(this));
727 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
728 this, SLOT(newValue(QPair<QString,double>)));
729 }
730 else if (type == "BSBSlider" || type == "BSBHSlider" || type == "BSBVSlider") {
731 widget = static_cast<QuteWidget *>( new QuteSlider(this));
732 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
733 this, SLOT(newValue(QPair<QString,double>)));
734 }
735 else if (type == "BSBKnob") {
736 widget = static_cast<QuteWidget *>(new QuteKnob(this));
737 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
738 this, SLOT(newValue(QPair<QString,double>)));
739 }
740 else if (type == "BSBScrollNumber") {
741 QuteScrollNumber *w = new QuteScrollNumber(this);
742 w->setFontOffset(m_fontOffset);
743 w->setFontScaling(m_fontScaling);
744 widget = static_cast<QuteWidget *>(w);
745 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
746 this, SLOT(newValue(QPair<QString,double>)));
747 }
748 else if (type == "BSBButton") {
749 QuteButton *w = new QuteButton(this);
750 widget = static_cast<QuteWidget *>(w);
751 connect(widget, SIGNAL(queueEventSignal(QString)),
752 this, SLOT(queueEvent(QString)));
753 connect(widget, SIGNAL(newValue(QPair<QString,QString>)),
754 this, SLOT(newValue(QPair<QString,QString>)));
755 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
756 this, SLOT(newValue(QPair<QString,double>)));
757 emit registerButton(w);
758 }
759 else if (type == "BSBDropdown") {
760 widget = static_cast<QuteWidget *>(new QuteComboBox(this));
761 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
762 this, SLOT(newValue(QPair<QString,double>)));
763 }
764 else if (type == "BSBController") {
765 auto t0 = std::chrono::high_resolution_clock::now();
766 auto w = new QuteMeter(this);
767 widget = static_cast<QuteWidget *>(w);
768 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
769 this, SLOT(newValue(QPair<QString,double>)));
770 // QDEBUG << "... BSBController new" << std::chrono::duration<double, std::milli>(std::chrono::high_resolution_clock::now()-t0).count() << "ms";
771
772 }
773 else if (type == "BSBGraph") {
774 QuteGraph *w = new QuteGraph(this);
775 widget = static_cast<QuteWidget *>(w);
776 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
777 this, SLOT(newValue(QPair<QString,double>)));
778 connect(w, SIGNAL(requestUpdateCurve(Curve*)),
779 this, SLOT(processUpdateCurve(Curve*)));
780 for (int i = 0; i < curves.size(); i++) {
781 w->addCurve(curves[i]);
782 }
783 graphWidgets.append(w);
784 emit requestCsoundUserData(widget);
785 emit registerGraph(w);
786 }
787 else if (type == "BSBScope") {
788 QuteScope *w = new QuteScope(this);
789 widget = static_cast<QuteWidget *>(w);
790 scopeWidgets.append(w);
791 emit registerScope(w);
792 }
793 else if (type == "BSBConsole") {
794 QuteConsole *w = new QuteConsole(this);
795 widget = static_cast<QuteWidget *>(w);
796 consoleWidgets.append(w);
797 // connect(widget, SIGNAL(newValue(QPair<QString,double>)), this, SLOT(newValue(QPair<QString,double>)));
798 }
799 else if (type == "BSBTableDisplay") {
800 auto w = new QuteTable(this);
801 widget = static_cast<QuteWidget *>(w);
802 emit requestCsoundUserData(w);
803 }
804 else {
805 qDebug() << type << " not implemented";
806 // QuteDummy *w = new QuteDummy(this);
807 }
808 if (widget == nullptr) {
809 qDebug() << "WidgetLayout::newXmlWidget ERROR widget has not been created!";
810 return -2;
811 }
812 if(forceBackground) {
813 widget->setProperty("QCS_bgcolormode", true);
814 widget->setProperty("QCS_bgcolor", QColor(240, 240, 240));
815 }
816
817 for (int i = 0; i < childNodes.size() ; i++) {
818 QDomElement node = childNodes.item(i).toElement();
819 QString nodeName = node.nodeName();
820 if (nodeName == "color" || nodeName == "bgcolor") { // COLOR type
821 if (node.attribute("mode") == "background") {
822 widget->setProperty("QCS_bgcolormode", true);
823 }
824 nodeName.prepend("QCS_");
825 widget->setProperty(nodeName.toLocal8Bit(), getColorFromElement(node));
826 }
827 // It's necessary to do a type conversion here rather than storing
828 // the values as QVariant strings and then converting
829 // because the property will be rightly typed, and that can be queried
830 else if (nodeName == "value" || nodeName == "resolution"
831 || nodeName == "minimum" || nodeName == "maximum"
832 || nodeName == "pressedValue") { // DOUBLE type
833 nodeName.prepend("QCS_");
834 widget->setProperty(nodeName.toLocal8Bit(), node.firstChild().nodeValue().toDouble());
835 }
836 else if (nodeName == "x") {
837 auto value = node.firstChild().nodeValue().toInt();
838 if(offset)
839 value += 20;
840 widget->setProperty("QCS_x", value);
841 }
842 else if (nodeName == "y") { // INT type (with offset)
843 auto value = node.firstChild().nodeValue().toInt();
844 if(offset)
845 value += 20;
846 widget->setProperty("QCS_y", value);
847 }
848 else if (nodeName == "width" || nodeName == "height"
849 || nodeName == "fontsize"
850 || nodeName == "precision"
851 || nodeName == "borderwidth"
852 || nodeName == "borderradius"
853 || nodeName == "selectedIndex" ) { // INT type
854 QDomNode n = node.firstChild();
855 nodeName.prepend("QCS_");
856 widget->setProperty(nodeName.toLocal8Bit(), n.nodeValue().toInt());
857 }
858 else if (nodeName == "midichan") {
859 auto value = node.firstChild().nodeValue().toInt();
860 widget->setProperty("QCS_midichan", value);
861 registerWidgetChannel(widget, value);
862 }
863 else if (nodeName == "midicc") {
864 auto value = node.firstChild().nodeValue().toInt();
865 widget->setProperty("QCS_midicc", value);
866 registerWidgetController(widget, value);
867 }
868 else if (nodeName == "randomizable" || nodeName == "selected"
869 || nodeName == "visible" ) { // BOOL type
870 QDomNode n = node.firstChild();
871 if (nodeName == "randomizable") {
872 if (node.attribute("group") != "") {
873 widget->setProperty("QCS_randomizableGroup", node.attribute("group").toInt() );
874 }
875 }
876 nodeName.prepend("QCS_");
877 widget->setProperty(nodeName.toLocal8Bit(), n.nodeValue() == "true");
878
879 }
880 else if (nodeName == "bsbDropdownItemList") { // MENU ITEM type
881 if (node.attribute("mode") == "value") {
882 qDebug() << "bsbDropdownItem modes not implemented!";
883 }
884 else if (node.attribute("mode") == "string") {
885 qDebug() << "bsbDropdownItem modes not implemented!";
886 }
887 QDomElement item = node.firstChildElement("bsbDropdownItem");
888 while (!item.isNull()) {
889 QDomElement ename = item.firstChildElement("name");
890 QDomElement evalue = item.firstChildElement("value");
891 QDomElement estringvalue = item.firstChildElement("stringvalue");
892 static_cast<QuteComboBox *>(widget)->addItem(ename.firstChild().nodeValue(),
893 evalue.firstChild().nodeValue().toDouble(),
894 estringvalue.firstChild().nodeValue());
895 item = item.nextSiblingElement("bsbDropdownItem");
896 }
897 }
898 else if (nodeName == "uuid") { // STRING type
899 QDomNode n = node.firstChild();
900 QString uuid = n.nodeValue();
901 if (newId || uuid.isEmpty()) {
902 uuid = QUuid::createUuid().toString();
903 }
904 while (!uuidFree(uuid)) {
905 uuid = QUuid::createUuid().toString();
906 }
907 widget->setProperty("QCS_uuid", uuid);
908 }
909 else { // STRING type (all the rest)
910 QDomNode n = node.firstChild();
911 nodeName.prepend("QCS_");
912 widget->setProperty(nodeName.toLocal8Bit(), n.nodeValue());
913 }
914 }
915
916 widget->applyInternalProperties();
917 registerWidget(widget);
918
919 // auto t1 = std::chrono::high_resolution_clock::now();
920 // auto diff = std::chrono::duration<double, std::milli>(t1-t0).count();
921 // QDEBUG << "newXmlWidget type" << type << "took " << diff << "ms";
922
923 return ret;
924 }
925
926 bool WidgetLayout::uuidFree(QString uuid)
927 {
928 bool isFree = true; // TODO need to check against uuid for widget panels and widget groups
929 widgetsMutex.lock();
930 for (int i = 0; i < m_widgets.size(); i++) {
931 if (m_widgets[i]->getUuid() == uuid) {
932 isFree = false;
933 break;
934 }
935 }
936 widgetsMutex.unlock();
937 return isFree;
938 }
939
940 QString WidgetLayout::newMacWidget(QString widgetLine, bool offset)
941 {
942 // This function returns -1 on error, 0 when no widget was created and 1 if widget was created
943 QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
944 QStringList quoteParts = widgetLine.split('"');
945 if (parts.size()<5)
946 return "";
947 if (parts[0]=="ioView") {
948 // Colors in MacCsound have a range of 0-65535
949 setBackground(parts[1]=="background",
950 QColor(parts[2].toInt()/256,
951 parts[3].toInt()/256,
952 parts[4].toInt()/256
953 )
954 );
955 return "";
956 }
957 else {
958 int x,y,width,height;
959 x = parts[1].toInt();
960 y = parts[2].toInt();
961 if (offset) {
962 x += 20;
963 y += 20;
964 }
965 width = parts[3].toInt();
966 height = parts[4].toInt();
967 if (parts[0]=="ioSlider") {
968 return createSlider(x,y,width,height, widgetLine);
969 }
970 else if (parts[0]=="ioText") {
971 if (parts[5]=="label" || parts[5]=="display") {
972 return createText(x,y,width,height, widgetLine);
973 }
974 else if (parts[5]=="edit") {
975 return createLineEdit(x,y,width, height, widgetLine);
976 }
977 else if (parts[5]=="scroll") {
978 return createScrollNumber(x,y,width, height, widgetLine);
979 }
980 else if (parts[5]=="editnum") {
981 return createSpinBox(x,y,width, height, widgetLine);
982 }
983 }
984 else if (parts[0]=="ioButton") {
985 return createButton(x,y,width,height, widgetLine);
986 }
987 else if (parts[0]=="ioKnob") {
988 return createKnob(x,y,width, height, widgetLine);
989 }
990 else if (parts[0]=="ioCheckbox") {
991 return createCheckBox(x,y,width, height, widgetLine);
992 }
993 else if (parts[0]=="ioMenu") {
994 return createMenu(x,y,width, height, widgetLine);
995 }
996 else if (parts[0]=="ioListing") {
997 return createConsole(x,y,width, height, widgetLine);
998 }
999 else if (parts[0]=="ioMeter") {
1000 return createMeter(x,y,width, height, widgetLine);
1001 }
1002 else if (parts[0]=="ioGraph") {
1003 if (parts.size() < 6 || parts[5]=="table")
1004 return createGraph(x,y,width, height, widgetLine);
1005 else if (parts[5]=="fft" || parts[5]=="scope" || parts[5]=="lissajou" || parts[5]=="poincare")
1006 return createScope(x,y,width, height, widgetLine);
1007 }
1008 else {
1009 // Unknown widget...
1010 qDebug("WidgetPanel::newMacWidget Warning: unknown widget!");
1011 return createDummy(x,y,width, height, widgetLine);
1012 }
1013 }
1014 return "";
1015 }
1016
1017 void WidgetLayout::registerWidget(QuteWidget * widget)
1018 {
1019 widgetsMutex.lock();
1020 connect(widget, SIGNAL(widgetChanged(QuteWidget *)),
1021 this, SLOT(widgetChanged(QuteWidget *)));
1022 connect(widget, SIGNAL(deleteThisWidget(QuteWidget *)),
1023 this, SLOT(deleteWidget(QuteWidget *)));
1024 connect(widget, SIGNAL(propertiesAccepted()),
1025 this, SLOT(markHistory()));
1026 connect(widget, SIGNAL(showMidiLearn(QuteWidget *)),
1027 this, SIGNAL(showMidiLearn(QuteWidget *)));
1028 connect(widget, SIGNAL(addChn_kSignal(QString)),
1029 this, SIGNAL(addChn_kSignal(QString)) );
1030 m_widgets.append(widget);
1031 // qDebug() << "WidgetLayout::registerWidget " << m_widgets.size() << widget;
1032 if (m_editMode) {
1033 createEditFrame(widget);
1034 editWidgets.last()->select();
1035 }
1036 setWidgetToolTip(widget, m_tooltips);
1037 m_activeWidgets++;
1038 widgetsMutex.unlock();
1039 adjustLayoutSize();
1040 widget->show();
1041 }
1042
1043 QString WidgetLayout::getMidiControllerInstrument()
1044 {
1045 // returns full Csound instrument to pass CC values to channels
1046 // and/or start events (if eventButton)
1047 if (registeredControllers.isEmpty()) {
1048 qDebug() << "There are no registered controllers!";
1049 }
1050
1051 QString instrLines ;
1052 for (int i = 0; i < registeredControllers.size(); i++) {
1053 if (registeredControllers[i].cc >= 0 && registeredControllers[i].chan>0) {
1054 QuteWidget * widget = registeredControllers[i].widget;
1055
1056 bool isEventButton = (widget->getWidgetType()=="BSBButton" && widget->property("QCS_type").toString().contains("event") ); // type "event" or "pictevent"
1057 if ( isEventButton) {
1058 QString eventLine = widget->property("QCS_eventLine").toString();
1059
1060 // find out if event has negative p3
1061 QStringList lineElements = eventLine.split(QRegExp("\\s"),SKIP_EMPTY_PARTS);
1062 if (lineElements.size() > 0 && lineElements[0] == "i") {
1063 // Remove first element if it is "i"
1064 lineElements.removeAt(0);
1065 }
1066 else if (lineElements.size() > 0 && lineElements[0][0] == 'i') {
1067 lineElements[0] = lineElements[0].mid(1); // Remove "i" character
1068 }
1069
1070 QString instrNo = lineElements[0].startsWith("\"") ?
1071 QString("nstrnum(%1)").arg(lineElements[0]) : lineElements[0];
1072
1073 bool negative_p3;
1074 if (lineElements.size() > 2 && lineElements[2].toDouble() < 0 ) {
1075 negative_p3 = true;
1076 } else {
1077 negative_p3 = false;
1078 }
1079
1080 if (!negative_p3) {
1081 instrLines += QString(R"(
1082 if (trigger:k(ctrl7:k(%1,%2,0,1),0.9, 0) == 1) then
1083 scoreline {{ %3 }}, 1 ; start
1084 endif)")
1085 .arg(registeredControllers[i].chan)
1086 .arg(registeredControllers[i].cc)
1087 .arg(eventLine) + "\n";
1088 } else {
1089 if (widget->property("QCS_latch").toBool()) {
1090 // toggle button - turn off on second press
1091 instrLines += QString(R"(
1092 if (trigger:k(ctrl7:k(%1,%2,0,1),0.9, 0)==1) then
1093 if (active:k(%3)>0) then
1094 turnoff2 %3, 4, 1 ; stop
1095 else
1096 scoreline {{ %4 }}, 1 ; start
1097 endif
1098 endif)")
1099 .arg(registeredControllers[i].chan)
1100 .arg(registeredControllers[i].cc)
1101 .arg(lineElements[0]) // lineElements[0] holds p1 (instrument name/number)
1102 .arg( eventLine ) + "\n";
1103 } else { // pushbutton - turn off when released
1104 instrLines +=
1105 QString(R"(
1106 if (trigger:k(ctrl7:k(%1,%2,0,1),0.9, 0) == 1) then
1107 scoreline {{ %4 }}, 1 ; start
1108 endif
1109
1110 if (trigger:k(ctrl7:k(%1,%2,0,1),0.1, 1) == 1) then
1111 turnoff2 %3, 4, 1 ; stop
1112 endif)")
1113 .arg(registeredControllers[i].chan)
1114 .arg(registeredControllers[i].cc)
1115 .arg( instrNo )
1116 .arg( eventLine ) + "\n";
1117 }
1118 }
1119 } else {
1120 if (widget->getWidgetType()=="BSBButton" &&
1121 widget->property("QCS_type").toString().contains("value")) {
1122 instrLines += QString ("\tchnset ctrl7:k(%1, %2, 0, %3), \"%4\"\n")
1123 .arg(registeredControllers[i].chan)
1124 .arg(registeredControllers[i].cc)
1125 .arg( widget->property("QCS_pressedValue").toDouble() )
1126 .arg(widget->getChannelName() );
1127 } else {
1128 instrLines += QString ("\tchnset ctrl7:k(%1, %2, %3, %4), \"%5\"\n")
1129 .arg(registeredControllers[i].chan)
1130 .arg(registeredControllers[i].cc)
1131 .arg( widget->property("QCS_minimum").toDouble() )
1132 .arg( widget->property("QCS_maximum").toDouble() )
1133 .arg(widget->getChannelName() );
1134 }
1135 }
1136 }
1137 }
1138
1139 QString instrString = instrLines.isEmpty() ? "" :
1140 "alwayson \"MidiControl\"\ninstr MidiControl\n" + instrLines + "endin\n";
1141 return instrString;
1142 }
1143
1144 void WidgetLayout::appendMessage(QString message)
1145 {
1146
1147 for (int i=0; i < consoleWidgets.size(); i++) {
1148 consoleWidgets[i]->appendMessage(message);
1149 consoleWidgets[i]->scrollToEnd();
1150 }
1151 }
1152
1153
1154 void WidgetLayout::flush()
1155 {
1156 // Called when running Csound to flush queues
1157 newValues.clear();
1158 }
1159
1160 void WidgetLayout::engineStopped()
1161 {
1162 flushGraphBuffer();
1163 }
1164
1165 void WidgetLayout::showWidgetTooltips(bool show)
1166 {
1167 m_tooltips = show;
1168 widgetsMutex.lock();
1169 for (int i=0; i < m_widgets.size(); i++) {
1170 setWidgetToolTip(m_widgets[i], show);
1171 }
1172 widgetsMutex.unlock();
1173 }
1174
1175 void WidgetLayout::setWidgetToolTip(QuteWidget *widget, bool show)
1176 {
1177 // This function must be protected/locked by the caller
1178 if(!show) {
1179 widget->setToolTip("");
1180 if (m_editMode && getEditWidget(widget) != nullptr) {
1181 getEditWidget(widget)->setToolTip("");
1182 }
1183 return;
1184 }
1185
1186 QStringList lines;
1187
1188 auto description = widget->getDescription();
1189 if(!description.isEmpty()) {
1190 lines << description;
1191 lines << " ";
1192 }
1193
1194 auto channelName = widget->getChannelName();
1195 auto channel2Name = widget->getChannel2Name();
1196 if(!channelName.isEmpty() && channel2Name.isEmpty()) {
1197 lines << tr("Channel: ") + channelName;
1198 }
1199 else if(!channel2Name.isEmpty()){
1200 lines << tr("ChannelH: ") + channelName + ", "
1201 + tr("ChannelV: ")+ channel2Name;
1202 }
1203
1204 int midichan = widget->property("QCS_midichan").toInt();
1205 if(midichan > 0) {
1206 int midicc = widget->property("QCS_midicc").toInt();
1207 lines << QString(tr("MIDI chan: %1 CC: %2")).arg(midichan).arg(midicc);
1208 }
1209
1210 auto tooltip = lines.join("\n");
1211 widget->setToolTip(tooltip);
1212
1213 if (getEditWidget(widget) != nullptr) {
1214 getEditWidget(widget)->setToolTip(tooltip);
1215 }
1216
1217 }
1218
1219 void WidgetLayout::setContained(bool contained)
1220 {
1221 if (m_contained == contained) {
1222 return;
1223 }
1224 // qDebug() << "WidgetLayout::setContained " << contained;
1225 m_contained = contained;
1226 bool bg = this->property("QCS_bg").toBool();
1227 QColor bgColor = this->property("QCS_bgcolor").value<QColor>();
1228 setBackground(bg, bgColor);
1229 if (m_contained) {
1230 this->setAutoFillBackground(false);
1231 // qDebug() << "WidgetLayout::setContained" << this->selectionFrame;
1232 this->selectionFrame->setParent(this->parentWidget());
1233 }
1234 else {
1235 this->selectionFrame->setParent(this); // To Avoid having it destroyed by parent
1236 }
1237 }
1238
1239 void WidgetLayout::setCurrentPosition(QPoint pos)
1240 {
1241 currentPosition = pos;
1242 }
1243
1244 void WidgetLayout::setFontOffset(double offset)
1245 {
1246 m_fontOffset = offset;
1247 widgetsMutex.lock();
1248 for (int i=0; i < m_widgets.size(); i++) {
1249 if (m_widgets[i]->getWidgetType() == "BSBLabel" ||
1250 m_widgets[i]->getWidgetType() == "BSBScrollNumber") {
1251 static_cast<QuteText *>(m_widgets[i])->setFontOffset(offset);
1252 m_widgets[i]->applyInternalProperties();
1253 }
1254 }
1255 widgetsMutex.unlock();
1256 }
1257
1258 void WidgetLayout::setFontScaling(double scaling)
1259 {
1260 m_fontScaling = scaling;
1261 widgetsMutex.lock();
1262 for (int i=0; i < m_widgets.size(); i++) {
1263 if (m_widgets[i]->getWidgetType() == "BSBLabel" ||
1264 m_widgets[i]->getWidgetType() == "BSBScrollNumber") {
1265 static_cast<QuteText *>(m_widgets[i])->setFontScaling(scaling);
1266 m_widgets[i]->applyInternalProperties();
1267 }
1268 }
1269 widgetsMutex.unlock();
1270 }
1271
1272 void WidgetLayout::setWidgetsLocked(bool lock)
1273 {
1274 widgetsMutex.lock();
1275 for (int i=0; i < m_widgets.size(); i++) {
1276 m_widgets[i]->setLocked(lock);
1277 }
1278 widgetsMutex.unlock();
1279 }
1280
1281 void WidgetLayout::appendCurve(WINDAT *windat)
1282 {
1283 // Called from the Csound callback, creates a curve and queues it for processing
1284 // Csound itself deletes the WINDAT structures, that's why we retain a copy of
1285 // the data for when Csound stops
1286 // It would be nice if Csound used a single windat for every f-table, but it reuses them...
1287 // for (int i = 0; i < curves.size(); i++) {
1288 // // Check if windat is already handled by one of the existing curves
1289 // if (windat == curves[i]->getOriginal()) {
1290 // return;
1291 // }
1292 // }
1293 for (int i = 0; i < curves.size(); i++) {
1294 // Check if caption is already present to replace curve rather than create a new one.
1295 if (curves[i]->get_caption() == windat->caption) {
1296 windat->windid = (uintptr_t) curves[i];
1297 return;
1298 }
1299 }
1300 if (curves.size() > QCS_CURVE_BUFFER_MAX) {
1301 qDebug() << "WidgetLayout::appendCurve curve size exceeded. Curve discarded!";
1302 return;
1303 }
1304 windat->caption[CAPSIZE - 1] = 0; // Just in case...
1305 Polarity polarity;
1306 switch (windat->polarity) {
1307 case NEGPOL:
1308 polarity = POLARITY_NEGPOL;
1309 break;
1310 case POSPOL:
1311 polarity = POLARITY_POSPOL;
1312 break;
1313 case BIPOL:
1314 polarity = POLARITY_BIPOL;
1315 break;
1316 default:
1317 polarity = POLARITY_NOPOL;
1318 }
1319 int indexInBuffer = -1;
1320 //This is not working... makes fft graphs ge ignored...
1321 // for (int i = 0; i < newCurveBuffer.size(); i++) {
1322 // if (strcmp(newCurveBuffer[i]->getOriginal()->caption,windat->caption) && strlen(newCurveBuffer[i]->getOriginal()->caption) > 0) {
1323 // indexInBuffer = i;
1324 // break;
1325 // }
1326 // }
1327 if (indexInBuffer < 0) {
1328 auto curve = new Curve(windat->fdata,
1329 (size_t)windat->npts,
1330 windat->caption,
1331 polarity,
1332 windat->max,
1333 windat->min,
1334 windat->absmax,
1335 windat->oabsmax,
1336 windat->danflag,
1337 windat); //FIXME delete these when starting a new run
1338 windat->windid = (uintptr_t) curve;
1339 newCurveBuffer.append(curve);
1340 // qDebug() << "WidgetLayout::appendCurve " << curve << "__--__" << windat;
1341 }
1342 else {
1343 qDebug() << "WidgetLayout::appendCurve reusing curve buffer " << indexInBuffer;
1344 newCurveBuffer[indexInBuffer]->set_data(windat->fdata);
1345 newCurveBuffer[indexInBuffer]->setOriginal(windat);
1346 }
1347 }
1348
1349 void WidgetLayout::killCurve(WINDAT *windat)
1350 {
1351 qDebug() << "WidgetLayout::killCurve()";
1352 Curve *curve = (Curve *) getCurveById(windat->windid);
1353 curve->setOriginal(nullptr);
1354 }
1355
1356 void WidgetLayout::newCurve(Curve* curve)
1357 {
1358 qDebug() << "WidgetPanel::newCurve" << curve << curve->get_caption();
1359 // FIXME check if curve is a new curve for a old ftable
1360 // for (int i = 0; i < curves.size(); i++) {
1361 // if (curves[i]->get_caption() == curve->get_caption()) {
1362 // return;
1363 // }
1364 // }
1365
1366 curves.append(curve);
1367 for (int i = 0; i < graphWidgets.size(); i++) {
1368 graphWidgets[i]->addCurve(curve);
1369 }
1370 }
1371
1372 void WidgetLayout::setCurveData(Curve *curve)
1373 {
1374 // qDebug() << "WidgetPanel::setCurveData" <<curve;
1375 for (int i = 0; i < graphWidgets.size(); i++) {
1376 graphWidgets[i]->setCurveData(curve);
1377 }
1378 }
1379
1380 //void WidgetLayout::passWidgetClipboard(QString text)
1381 //{
1382 // m_clipboard = text;
1383 //}
1384
1385 uintptr_t WidgetLayout::getCurveById(uintptr_t id)
1386 {
1387 // qDebug() << "WidgetLayout::getCurveById ";
1388 foreach (Curve *thisCurve, curves) {
1389 // qDebug() << "WidgetLayout::getCurveById " << (uintptr_t)thisCurve << "id" << id;
1390 if ((uintptr_t) thisCurve == id)
1391 return (uintptr_t) thisCurve;
1392 }
1393 return 0;
1394 }
1395
1396 void WidgetLayout::updateCurve(WINDAT *windat)
1397 {
1398 // qDebug() << "WidgetLayout::updateCurve(WINDAT *windat) " << windat->windid;
1399 if (curveUpdateBufferCount+1 < QCS_CURVE_BUFFER_SIZE) {
1400 curveUpdateBufferCount++;
1401 curveUpdateBuffer[curveUpdateBufferCount] = *windat;
1402 }
1403 }
1404
1405 void WidgetLayout::processUpdateCurve(Curve *curve) {
1406 WINDAT *orig = curve->getOriginal();
1407 qDebug() << "processUpdateCurve" << orig->npts << orig->caption;
1408 this->updateCurve(orig);
1409 }
1410
1411
1412 int WidgetLayout::killCurves(CSOUND * /*csound*/)
1413 {
1414 // qDebug() << "qutecsound::killCurves";
1415 // TODO this is a great idea, to copy data from the tables at the end of run,
1416 // but the API is not working as expected
1417 // for (int i = 0; i < curves.size(); i++) {
1418 // WINDAT * windat = curves[i]->getOriginal();
1419 // if (windat->npts > 0 && windat->windid == (uintptr_t)curves[i]) { // Check for sanity of pointer
1420 // curves[i]->set_size(windat->npts); // number of points
1421 // curves[i]->set_data(windat->fdata);
1422 // curves[i]->set_caption(QString(windat->caption)); // title of curve
1423 // // curves[i]->set_polarity(windat->polarity); // polarity
1424 // curves[i]->set_max(windat->max); // curve max
1425 // curves[i]->set_min(windat->min); // curve min
1426 // curves[i]->set_absmax(windat->absmax); // abs max of above
1427 // // curves[i]->set_y_scale(windat->y_scale); // Y axis scaling factor
1428 // curves[i]->setOriginal(0);
1429 // }
1430 // }
1431
1432 // Curves are freed not when Csound finishes, but when Csound is run again,
1433 // to have them available even if Csound is stopped.
1434 return 0;
1435 }
1436
1437 void WidgetLayout::clearGraphs()
1438 {
1439 flushGraphBuffer();
1440 bool updating = m_updating;
1441 m_updating = false;
1442 QMutexLocker locker(&layoutMutex);
1443 for (int i = 0; i < graphWidgets.size(); i++) {
1444 graphWidgets[i]->clearCurves();
1445 }
1446 while (curves.size() > 0) {
1447 Curve * c = curves.takeFirst();
1448 delete c;
1449 }
1450 m_updating = updating;
1451 }
1452
1453 void WidgetLayout::flushGraphBuffer()
1454 {
1455 bool updating = m_updating;
1456 m_updating = false;
1457 if(!layoutMutex.tryLock(200)) {
1458 QDEBUG << "Could not acquire lock, not flushing...";
1459 m_updating = updating;
1460 return;
1461 }
1462 while (newCurveBuffer.size() > 0) {
1463 Curve * c = newCurveBuffer.takeFirst();
1464 delete c;
1465 }
1466 curveUpdateBufferCount = 0;
1467 layoutMutex.unlock();
1468 m_updating = updating;
1469 }
1470
1471 void WidgetLayout::refreshWidgets()
1472 {
1473 while (midiReadCounter != midiWriteCounter) {
1474 // TODO it is inefficient to have this per layout (when more than one
1475 // layout is available)
1476 int index =midiReadCounter;
1477 int status = midiQueue[index][0];
1478 if (status & 176) { // MIDI control change
1479 int channel = (status ^ 176) + 1;
1480 for (int i = 0; i < registeredControllers.size(); i++) {
1481 if (registeredControllers[i].cc == midiQueue[index][1]) {
1482 if ( channel == registeredControllers[i].chan) {
1483 registeredControllers[i].widget->setMidiValue(midiQueue[index][2]);
1484 }
1485 }
1486 }
1487 }
1488 midiReadCounter++;
1489 midiReadCounter = midiReadCounter%QCS_MAX_MIDI_QUEUE;
1490 }
1491 QMutexLocker locker(&widgetsMutex);
1492 for (int i=0; i < m_widgets.size(); i++) {
1493 if (m_widgets[i]->m_valueChanged || m_widgets[i]->m_value2Changed) {
1494 m_widgets[i]->refreshWidget();
1495 }
1496 if (m_trackMouse) {
1497 QString ch1name = m_widgets[i]->getChannelName();
1498 if (ch1name.startsWith("_Mouse")) {
1499 if (ch1name == "_MouseX") {
1500 m_widgets[i]->setValue(getMouseX());
1501 }
1502 else if (ch1name == "_MouseY") {
1503 m_widgets[i]->setValue(getMouseY());
1504 }
1505 else if (ch1name == "_MouseRelX") {
1506 m_widgets[i]->setValue(getMouseRelX());
1507 }
1508 else if (ch1name == "_MouseRelY") {
1509 m_widgets[i]->setValue(getMouseRelY());
1510 }
1511 else if (ch1name == "_MouseBut1") {
1512 m_widgets[i]->setValue(getMouseBut1());
1513 }
1514 else if (ch1name == "_MouseBut2") {
1515 m_widgets[i]->setValue(getMouseBut2());
1516 }
1517 }
1518 QString ch2name = m_widgets[i]->getChannel2Name();
1519 if (ch2name.startsWith("_Mouse")) {
1520 if (ch2name == "_MouseX") {
1521 m_widgets[i]->setValue2(getMouseX());
1522 }
1523 else if (ch2name == "_MouseY") {
1524 m_widgets[i]->setValue2(getMouseY());
1525 }
1526 else if (ch2name == "_MouseRelX") {
1527 m_widgets[i]->setValue2(getMouseRelX());
1528 }
1529 else if (ch2name == "_MouseRelY") {
1530 m_widgets[i]->setValue2(getMouseRelY());
1531 }
1532 else if (ch2name == "_MouseBut1") {
1533 m_widgets[i]->setValue2(getMouseBut1());
1534 }
1535 else if (ch2name == "_MouseBut2") {
1536 m_widgets[i]->setValue2(getMouseBut2());
1537 }
1538 }
1539 }
1540 }
1541 }
1542
1543 QString WidgetLayout::getCsladspaLines()
1544 {
1545 QString text = "";
1546 int unsupported = 0;
1547 widgetsMutex.lock();
1548 foreach(QuteWidget *widget, m_widgets) {
1549 QString line = widget->getCsladspaLine();
1550 if (line != "") {
1551 text += line + "\n";
1552 }
1553 else {
1554 unsupported++;
1555 }
1556 }
1557 widgetsMutex.unlock();
1558 qDebug() << "WidgetPanel:getCsladspaLines() " << unsupported << " Unsupported widgets";
1559 return text;
1560 }
1561
1562 QString WidgetLayout::getQml()
1563 {
1564 QString qml = "import QtQuick 2.0\nimport QtQuick.Controls 2.0 // NB! Requires Qt 5.7 or later. Rewrite with QtQuick.Controls 1.X if using older Qt versions \n\n";
1565 QString s2 = QString(R"()");
1566 qml += "Rectangle {\n";
1567 QColor bgcolor = this->palette().color(QWidget::backgroundRole());
1568 qml+=QString("\tcolor: \"%1\"\n").arg(bgcolor.name());
1569
1570 // get width and hight currently from panel size, maybe not best method -
1571 // test how if works if some of the window is hidden
1572 int w = this->geometry().width() + 20;
1573 int h = this->geometry().height() +20;
1574
1575 qml += QString("\twidth: %1\n").arg(w);
1576 qml += QString("\theight: %1\n").arg(h);
1577
1578 qml += "\tanchors.fill: parent\n";
1579 qml += "\n\n\tFlickable {\n\tanchors.fill: parent\n";
1580 qml += QString("\tcontentWidth: %1\n").arg(w);
1581 qml += QString("\tcontentHeight: %1\n").arg(h);
1582
1583 qml += "\n";
1584
1585 qml += QString(
1586 R"(
1587 Item {
1588 id: scaleItem
1589 anchors.fill: parent
1590 }
1591
1592 PinchArea {
1593 anchors.fill: parent
1594 pinch.maximumScale: 2.5
1595 pinch.minimumScale: 0.6
1596 pinch.target: scaleItem
1597
1598 MouseArea { // necessary for co-existense of flickable an pincharea
1599 anchors.fill: parent
1600 onClicked: console.log("Clicked tiny")
1601 propagateComposedEvents: true
1602 }
1603 }
1604 )");
1605
1606
1607 int unsupported = 0;
1608 widgetsMutex.lock();
1609 foreach(QuteWidget *widget, m_widgets) {
1610 QString line = widget->getQml();
1611 if (line != "") {
1612 qml += line + "\n";
1613 }
1614 else {
1615 unsupported++;
1616 }
1617 }
1618 widgetsMutex.unlock();
1619 qDebug() << "WidgetPanel:getQml() " << unsupported << " Unsupported widgets";
1620 qml += "\t} // end Flickable\n";
1621 qml += "} // end Rectangle\n"; // end Rectangle
1622 //qDebug() << qml;
1623 return qml;
1624 }
1625
1626 QString WidgetLayout::getCabbageWidgets()
1627 {
1628 QString title = windowTitle();
1629 QString text = "form caption(\"" + title + "\"),";
1630 //text += "size(" + QString::number(m_w+20) + "," + QString::number(m_h+20) +")\n"; // m_w and m_h not returning correct results always
1631 int w = this->geometry().width();
1632 int h = this->geometry().height();
1633 text += QString(" size(%1,%2)\n").arg(w+20).arg(h+20);
1634
1635 int unsupported = 0;
1636 widgetsMutex.lock();
1637 QString unsupportedWidgets;
1638 foreach(QuteWidget *widget, m_widgets) {
1639 QString line = widget->getCabbageLine();
1640 if (line != "") {
1641 text += line + "\n";
1642 }
1643 else {
1644 unsupported++;
1645 unsupportedWidgets += widget->getWidgetType() + " ";
1646 }
1647 }
1648 widgetsMutex.unlock();
1649 qDebug() << "WidgetPanel:getCabbageWidgets()"<<unsupported<<"Unsupported widgets";
1650 if (unsupported) {
1651 QMessageBox::warning(this, tr("CsoundQt"), tr("Could not convert %1 widget(s):\n%2")
1652 .arg(unsupported).arg(unsupportedWidgets));
1653 }
1654 return text;
1655 }
1656
1657 bool WidgetLayout::isModified()
1658 {
1659 return m_modified;
1660 }
1661
1662 void WidgetLayout::addCreateWidgetActionsToMenu(QMenu &menu) {
1663 menu.addAction(createSliderAct);
1664 menu.addAction(createLabelAct);
1665 menu.addAction(createDisplayAct);
1666 menu.addAction(createScrollNumberAct);
1667 menu.addAction(createLineEditAct);
1668 menu.addAction(createSpinBoxAct);
1669 menu.addAction(createButtonAct);
1670 menu.addAction(createKnobAct);
1671 menu.addAction(createCheckBoxAct);
1672 menu.addAction(createMenuAct);
1673 menu.addAction(createMeterAct);
1674 menu.addAction(createConsoleAct);
1675 menu.addAction(createGraphAct);
1676 menu.addAction(createScopeAct);
1677 menu.addAction(createTableDisplayAct);
1678 }
1679
1680 void WidgetLayout::createContextMenu(QContextMenuEvent *event)
1681 {
1682 QMenu menu;
1683 QMenu newMenu(tr("New Widget", "New Widget submenu in widget layout popup"), &menu);
1684 this->addCreateWidgetActionsToMenu(newMenu);
1685 menu.addMenu(&newMenu);
1686 menu.addSeparator(); // ------------------------------------------------------------
1687
1688 QMenu editMenu(tr("Edit", "Edit menu in widget layout right-click menu"), &menu);
1689 editMenu.addAction(cutAct);
1690 editMenu.addAction(copyAct);
1691 editMenu.addAction(pasteAct);
1692 editMenu.addAction(selectAllAct);
1693 // Shortcut from the duplicate action is not working from some reason...
1694 // menu.addAction(duplicateAct);
1695 editMenu.addAction(deleteAct);
1696 editMenu.addAction(clearAct);
1697 menu.addMenu(&editMenu);
1698
1699 menu.addSeparator(); // ------------------------------------------------------------
1700
1701 QMenu presetMenu(tr("Presets", "Preset submenu in widget layout popup"), &menu);
1702 presetMenu.addAction(storePresetAct);
1703 presetMenu.addAction(recallPresetAct);
1704 presetMenu.addAction(newPresetAct);
1705 menu.addMenu(&presetMenu);
1706
1707 menu.addSeparator(); // ------------------------------------------------------------
1708
1709 menu.addAction(propertiesAct);
1710 menu.addAction(reloadWidgetsAct);
1711
1712 currentPosition = event->pos();
1713 // if (m_sbActive) {
1714 // currentPosition.setX(currentPosition.x() + scrollArea->horizontalScrollBar()->value());
1715 // currentPosition.setY(currentPosition.y() + scrollArea->verticalScrollBar()->value() - 20);
1716 // }
1717 menu.exec(event->globalPos());
1718 // menu.exec(event->pos());
1719 }
1720
1721 void WidgetLayout::deselectAll()
1722 {
1723 for (int i = 0; i< editWidgets.size(); i++) {
1724 editWidgets[i]->deselect();
1725 }
1726 }
1727
1728 void WidgetLayout::selectAll()
1729 {
1730 for (int i = 0; i< editWidgets.size(); i++) {
1731 editWidgets[i]->select();
1732 }
1733 }
1734
1735 void WidgetLayout::widgetMoved(QPair<int, int> delta)
1736 {
1737 widgetsMutex.lock();
1738 for (int i = 0; i < m_widgets.size(); i++) {
1739 if (editWidgets[i]->isSelected()) {
1740 int newx = m_widgets[i]->x() + delta.first;
1741 int newy = m_widgets[i]->y() + delta.second;
1742 m_widgets[i]->move(newx, newy);
1743 editWidgets[i]->move(newx, newy);
1744 // set the position to properties otherwis MidiLearn -> applyInetrnalProperties sets it to wrong (old) position
1745 m_widgets[i]->setProperty("QCS_x", newx);
1746 m_widgets[i]->setProperty("QCS_y", newy);
1747
1748 }
1749 }
1750 widgetsMutex.unlock();
1751 adjustLayoutSize();
1752 }
1753
1754 void WidgetLayout::widgetResized(QPair<int, int> delta)
1755 {
1756 // qDebug("WidgetPanel::widgetResized %i %i", delta.first, delta.second);
1757 widgetsMutex.lock();
1758 for (int i = 0; i< editWidgets.size(); i++) {
1759 if (editWidgets[i]->isSelected()) {
1760 int neww = m_widgets[i]->width() + delta.first;
1761 int newh = m_widgets[i]->height() + delta.second;
1762 neww = neww > 5 ? neww : 5;
1763 newh = newh > 5 ? newh : 5;
1764 m_widgets[i]->setWidgetGeometry(m_widgets[i]->x(), m_widgets[i]->y(), neww, newh);
1765 editWidgets[i]->resize(neww, newh);
1766 // set the position to properties otherwis MidiLearn -> applyInetrnalProperties sets it to wrong (old) position
1767 m_widgets[i]->setProperty("QCS_width", neww);
1768 m_widgets[i]->setProperty("QCS_height", newh);
1769 }
1770 }
1771 widgetsMutex.unlock();
1772 adjustLayoutSize();
1773 }
1774
1775 void WidgetLayout::mousePressEventParent(QMouseEvent *event)
1776 {
1777 WidgetLayout::mousePressEvent(event);
1778 }
1779
1780 void WidgetLayout::mouseReleaseEventParent(QMouseEvent *event)
1781 {
1782 WidgetLayout::mouseReleaseEvent(event);
1783 }
1784
1785 void WidgetLayout::mouseMoveEventParent(QMouseEvent *event)
1786 {
1787 WidgetLayout::mouseMoveEvent(event);
1788 }
1789
1790 QSize WidgetLayout::getUsedSize()
1791 {
1792 int width = 30, height = 30;
1793 for (int i = 0; i< m_widgets.size(); i++) {
1794 if (m_widgets[i]->x() + m_widgets[i]->width() > width) {
1795 width = m_widgets[i]->x() + m_widgets[i]->width();
1796 }
1797 if (m_widgets[i]->y() + m_widgets[i]->height() > height) {
1798 height = m_widgets[i]->y() + m_widgets[i]->height();
1799 }
1800 }
1801 return QSize(width, height);
1802 }
1803
1804 void WidgetLayout::adjustLayoutSize()
1805 {
1806 if (m_contained) {
1807 QSize s = this->childrenRect().size();
1808 QPoint pos = this->childrenRect().topLeft();
1809 s.rwidth() += pos.x();
1810 s.rheight() += pos.y();
1811 if (this->size() != s) {
1812 this->resize(s);
1813 }
1814 }
1815 }
1816
1817 void WidgetLayout::adjustWidgetSize()
1818 {
1819 if (!m_contained) {
1820 this->resize(m_w, m_h);
1821 this->move(m_posx, m_posy);
1822 }
1823 }
1824
1825 void WidgetLayout::selectionChanged(QRect selection)
1826 {
1827 // qDebug("WidgetLayout::selectionChanged %i %i %i %i", selection.x(), selection.y(), selection.width(), selection.height());
1828 if (editWidgets.isEmpty())
1829 return; //not in edit mode
1830 deselectAll();
1831 widgetsMutex.lock();
1832 for (int i = 0; i< m_widgets.size(); i++) {
1833 int x = m_widgets[i]->x();
1834 int y = m_widgets[i]->y();
1835 int w = m_widgets[i]->width();
1836 int h = m_widgets[i]->height();
1837 if (x > selection.x() - w && x < selection.x() + selection.width() &&
1838 y > selection.y() - h && y < selection.y() + selection.height() ) {
1839 editWidgets[i]->select();
1840 }
1841 }
1842 widgetsMutex.unlock();
1843 }
1844
1845 QString WidgetLayout::createNewSlider(int x, int y, QString channel)
1846 {
1847 QString uuid;
1848 bool dialog;
1849 int posx = x >= 0 ? x : currentPosition.x();
1850 int posy = y >= 0 ? y : currentPosition.y();
1851 deselectAll();
1852 if (channel.isEmpty()) {
1853 channel = "slider" + QString::number(m_widgets.size());
1854 dialog = true;
1855 } else {
1856 dialog = false;
1857 }
1858 uuid = createSlider(posx, posy, 20, 100,
1859 QString("ioSlider {"+ QString::number(posx) +", "+
1860 QString::number(posy) +
1861 "} {20, 100} 0.000000 1.000000 0.000000 " + channel));
1862 widgetChanged();
1863 if (dialog && getOpenProperties()) {
1864 m_widgets.last()->openProperties();
1865 }
1866 markHistory();
1867 return uuid;
1868 }
1869
1870
1871 QString WidgetLayout::createNewLabel(int x, int y, QString channel)
1872 {
1873 QString uuid;
1874 bool dialog;
1875 int posx = x >= 0 ? x : currentPosition.x();
1876 int posy = y >= 0 ? y : currentPosition.y();
1877 deselectAll();
1878 if (channel.isEmpty()) {
1879 channel = "label" + QString::number(m_widgets.size());
1880 dialog = true;
1881 } else {
1882 dialog = false;
1883 }
1884 QString line = "ioText {"+ QString::number(posx) +", "+ QString::number(posy) +"} {80, 25} label 0.000000 0.001000 \"\" left \"Arial\" 8 {0, 0, 0} {65535, 65535, 65535} nobackground noborder "+ channel;
1885 uuid = createText(posx, posy, 80, 25, line);
1886 widgetChanged();
1887 if (dialog && getOpenProperties()) {
1888 m_widgets.last()->openProperties();
1889 }
1890 markHistory();
1891 return uuid;
1892 }
1893
1894 QString WidgetLayout::createNewDisplay(int x, int y, QString channel)
1895 {
1896 QString uuid;
1897 bool dialog;
1898 int posx = x >= 0 ? x : currentPosition.x();
1899 int posy = y >= 0 ? y : currentPosition.y();
1900 deselectAll();
1901 if (channel.isEmpty()) {
1902 channel = "display" + QString::number(m_widgets.size());
1903 dialog = true;
1904 } else {
1905 dialog = false;
1906 }
1907 QString line = "ioText {"+ QString::number(posx) +", "+ QString::number(posy) +"} {80, 25} display 0.000000 0.001000 \"" + channel + "\" left \"Arial\" 8 {0, 0, 0} {65535, 65535, 65535} nobackground border Display";
1908 uuid = createText(posx, posy, 80, 25, line);
1909 widgetChanged();
1910 if (dialog && getOpenProperties()) {
1911 m_widgets.last()->openProperties();
1912 }
1913 markHistory();
1914 return uuid;
1915 }
1916
1917 QString WidgetLayout::createNewScrollNumber(int x, int y, QString channel)
1918 {
1919 QString uuid;
1920 bool dialog;
1921 int posx = x >= 0 ? x : currentPosition.x();
1922 int posy = y >= 0 ? y : currentPosition.y();
1923 deselectAll();
1924 if (channel.isEmpty()) {
1925 channel = "scroll" + QString::number(m_widgets.size());
1926 dialog = true;
1927 } else {
1928 dialog = false;
1929 }
1930 QString line = "ioText {"+ QString::number(posx) +", "+ QString::number(posy) +"} {80, 25} scroll 0.000000 0.001000 \"" + channel + "\" left \"Arial\" 8 {0, 0, 0} {65535, 65535, 65535} background border 0.000000";
1931 uuid = createScrollNumber(posx, posy, 80, 25, line);
1932 widgetChanged();
1933 if (dialog && getOpenProperties()) {
1934 m_widgets.last()->openProperties();
1935 }
1936 markHistory();
1937 return uuid;
1938 }
1939
1940 QString WidgetLayout::createNewLineEdit(int x, int y, QString channel)
1941 {
1942 QString uuid;
1943 bool dialog;
1944 int posx = x >= 0 ? x : currentPosition.x();
1945 int posy = y >= 0 ? y : currentPosition.y();
1946 deselectAll();
1947 if (channel.isEmpty()) {
1948 channel = "line" + QString::number(m_widgets.size());
1949 dialog = true;
1950 } else {
1951 dialog = false;
1952 }
1953 auto line = QString("ioText {%1, %2} {100, 25} edit 0.000000 0.001000 "
1954 "\"%3\" left \"Arial\" 8 {0, 0, 0} {65535, 65535, 65535} "
1955 "background border Type here")
1956 .arg(QString::number(posx), QString::number(posy), channel);
1957
1958 // QString line = "ioText {" + QString::number(posx) +", "+ QString::number(posy) +"} {100, 25} edit 0.000000 0.001000 \"" + channel + "\" left \"Arial\" 8 {0, 0, 0} {65535, 65535, 65535} nobackground noborder Type here";
1959 qDebug() << "createNewLineEdit";
1960 uuid = createLineEdit(posx, posy, 100, 25, line);
1961 widgetChanged();
1962 if (dialog && getOpenProperties()) {
1963 m_widgets.last()->openProperties();
1964 }
1965 markHistory();
1966 return uuid;
1967 }
1968
1969 QString WidgetLayout::createNewSpinBox(int x, int y, QString channel)
1970 {
1971 QString uuid;
1972 bool dialog;
1973 int posx = x >= 0 ? x : currentPosition.x();
1974 int posy = y >= 0 ? y : currentPosition.y();
1975 deselectAll();
1976 if (channel.isEmpty()) {
1977 channel = "spinbox" + QString::number(m_widgets.size());
1978 dialog = true;
1979 } else {
1980 dialog = false;
1981 }
1982 QString line = "ioText {"+ QString::number(posx) +", "+ QString::number(posy) +"} {80, 25} editnum 0.000000 0.001000 \"" + channel + "\" left \"Arial\" 8 {0, 0, 0} {65535, 65535, 65535} nobackground noborder Type here";
1983 uuid = createSpinBox(posx, posy, 80, 25, line);
1984 widgetChanged();
1985 if (dialog && getOpenProperties()) {
1986 m_widgets.last()->openProperties();
1987 }
1988 markHistory();
1989 return uuid;
1990 }
1991
1992 QString WidgetLayout::createNewButton(int x, int y, QString channel)
1993 {
1994 qDebug() << "WidgetLayout::createNewButton";
1995 QString uuid;
1996 bool dialog;
1997 int posx = x >= 0 ? x : currentPosition.x();
1998 int posy = y >= 0 ? y : currentPosition.y();
1999 deselectAll();
2000 if (channel.isEmpty()) {
2001 channel = "button" + QString::number(m_widgets.size());
2002 dialog = true;
2003 } else {
2004 dialog = false;
2005 }
2006 //IS IT OK if the channel name is alsoset as the text of the button?
2007 QString line = "ioButton {"+ QString::number(posx) +", "+ QString::number(posy) +"} {100, 30} event 1.000000 \"" + channel + "\" \"" + channel + "\" \"/\" i1 0 10";
2008 uuid = createButton(posx, posy, 100, 30, line);
2009 widgetChanged();
2010 if (dialog && getOpenProperties()) {
2011 m_widgets.last()->openProperties();
2012 }
2013 markHistory();
2014 return uuid;
2015 }
2016
2017 QString WidgetLayout::createNewKnob(int x, int y, QString channel)
2018 {
2019 QString uuid;
2020 bool dialog;
2021 int posx = x >= 0 ? x : currentPosition.x();
2022 int posy = y >= 0 ? y : currentPosition.y();
2023 deselectAll();
2024 if (channel.isEmpty()) {
2025 channel = "knob" + QString::number(m_widgets.size());
2026 dialog = true;
2027 } else {
2028 dialog = false;
2029 }
2030 uuid = createKnob(posx, posy, 80, 80, QString("ioKnob {"+ QString::number(posx) +", "+ QString::number(posy) + "} {80, 80} 0.000000 1.000000 0.010000 0.000000 " + channel));
2031 widgetChanged();
2032 if (dialog && getOpenProperties()) {
2033 m_widgets.last()->openProperties();
2034 }
2035 markHistory();
2036 return uuid;
2037 }
2038
2039 QString WidgetLayout::createNewCheckBox(int x, int y, QString channel)
2040 {
2041 QString uuid;
2042 bool dialog;
2043 int posx = x >= 0 ? x : currentPosition.x();
2044 int posy = y >= 0 ? y : currentPosition.y();
2045 deselectAll();
2046 if (channel.isEmpty()) {
2047 channel = "checkbox" + QString::number(m_widgets.size());
2048 dialog = true;
2049 } else {
2050 dialog = false;
2051 }
2052 uuid = createCheckBox(posx, posy, 20, 20, QString("ioCheckbox {"+ QString::number(posx) +", "+ QString::number(posy) + "} {20, 20} off " + channel));
2053 widgetChanged();
2054 if (dialog && getOpenProperties()) {
2055 m_widgets.last()->openProperties();
2056 }
2057 markHistory();
2058 return uuid;
2059 }
2060
2061 QString WidgetLayout::createNewMenu(int x, int y, QString channel)
2062 {
2063 QString uuid;
2064 bool dialog;
2065 int posx = x >= 0 ? x : currentPosition.x();
2066 int posy = y >= 0 ? y : currentPosition.y();
2067 deselectAll();
2068 if (channel.isEmpty()) {
2069 channel = "menu" + QString::number(m_widgets.size());
2070 dialog = true;
2071 } else {
2072 dialog = false;
2073 }
2074 uuid = createMenu(posx, posy, 80, 30, QString("ioMenu {"+ QString::number(posx) +", "+ QString::number(posy) + "} {80, 25} 1 303 \"item1,item2,item3\" " + channel));
2075 widgetChanged();
2076 if (dialog && getOpenProperties()) {
2077 m_widgets.last()->openProperties();
2078 }
2079 markHistory();
2080 return uuid;
2081 }
2082
2083 QString WidgetLayout::createNewMeter(int x, int y, QString channel)
2084 {
2085 QString uuid;
2086 bool dialog;
2087 int posx = x >= 0 ? x : currentPosition.x();
2088 int posy = y >= 0 ? y : currentPosition.y();
2089 deselectAll();
2090 if (channel.isEmpty()) {
2091 channel = "meter" + QString::number(m_widgets.size());
2092 dialog = true;
2093 } else {
2094 dialog = false;
2095 }
2096 uuid = createMeter(posx, posy, 30, 80, QString("ioMeter {"+ QString::number(posx) +", "+ QString::number(posy) + "} {30, 80} {0, 60000, 0} \"" + channel + "\" 0.000000 \"hor" + QString::number(m_widgets.size()) + "\" 0.000000 fill 1 0 mouse"));
2097 widgetChanged();
2098 if (dialog && getOpenProperties()) {
2099 m_widgets.last()->openProperties();
2100 }
2101 markHistory();
2102 return uuid;
2103 }
2104
2105
2106 QString WidgetLayout::createNewConsole(int x, int y, QString channel)
2107 {
2108 QString uuid;
2109 bool dialog;
2110 int posx = x >= 0 ? x : currentPosition.x();
2111 int posy = y >= 0 ? y : currentPosition.y();
2112 deselectAll();
2113 if (channel.isEmpty()) {
2114 channel = "console" + QString::number(m_widgets.size());
2115 dialog = true;
2116 } else {
2117 dialog = false;
2118 }
2119 uuid = createConsole(posx, posy, 320, 400, QString("ioListing {"+ QString::number(posx) +", "+ QString::number(posy) + "} {320, 400}"));
2120 widgetChanged();
2121 if (dialog && getOpenProperties()) {
2122 m_widgets.last()->openProperties();
2123 }
2124 markHistory();
2125 return uuid;
2126 }
2127
2128 QString WidgetLayout::createNewGraph(int x, int y, QString channel)
2129 {
2130 QString uuid;
2131 bool dialog;
2132 int posx = x >= 0 ? x : currentPosition.x();
2133 int posy = y >= 0 ? y : currentPosition.y();
2134 deselectAll();
2135 if (channel.isEmpty()) {
2136 channel = "graph" + QString::number(m_widgets.size());
2137 dialog = true;
2138 } else {
2139 dialog = false;
2140 }
2141 uuid = createGraph(posx, posy, 350, 150, QString("ioGraph {"+ QString::number(posx) +", "+ QString::number(posy) + "} {350, 150}"));
2142 widgetChanged();
2143 if (dialog && getOpenProperties()) {
2144 m_widgets.last()->openProperties();
2145 }
2146 markHistory();
2147 return uuid;
2148 }
2149
2150 QString WidgetLayout::createNewTableDisplay(int x, int y, QString channel)
2151 {
2152 QString uuid;
2153 bool dialog;
2154 int posx = x >= 0 ? x : currentPosition.x();
2155 int posy = y >= 0 ? y : currentPosition.y();
2156 deselectAll();
2157 if (channel.isEmpty()) {
2158 channel = "graph" + QString::number(m_widgets.size());
2159 dialog = true;
2160 } else {
2161 dialog = false;
2162 }
2163 uuid = createTableDisplay(posx, posy, 350, 150,
2164 QString("ioGraph {%1, %2} {%1, %2}").arg(posx, posy));
2165 widgetChanged();
2166 if (dialog && getOpenProperties()) {
2167 m_widgets.last()->openProperties();
2168 }
2169 markHistory();
2170 return uuid;
2171 }
2172
2173 QString WidgetLayout::createNewScope(int x, int y, QString channel)
2174 {
2175 QString uuid;
2176 bool dialog;
2177 int posx = x >= 0 ? x : currentPosition.x();
2178 int posy = y >= 0 ? y : currentPosition.y();
2179 deselectAll();
2180 if (channel.isEmpty()) {
2181 channel = "scope" + QString::number(m_widgets.size());
2182 dialog = true;
2183 } else {
2184 dialog = false;
2185 }
2186 uuid = createScope(posx, posy, 350, 150,
2187 QString("ioGraph {"+ QString::number(posx) +", "+
2188 QString::number(posy) +
2189 "} {350, 150} scope 2.000000 -1.000000"));
2190 widgetChanged();
2191 if (dialog && getOpenProperties()) {
2192 m_widgets.last()->openProperties();
2193 }
2194 markHistory();
2195 return uuid;
2196 }
2197
2198 void WidgetLayout::clearWidgets()
2199 {
2200 clearWidgetLayout();
2201 widgetChanged();
2202 }
2203
2204 void WidgetLayout::clearWidgetLayout()
2205 {
2206 // qDebug("WidgetLayout::clearWidgetLayout()");
2207 widgetsMutex.lock();
2208 m_activeWidgets = 0;
2209 foreach (QuteWidget *widget, m_widgets) {
2210 delete widget;
2211 }
2212 m_widgets.clear();
2213 foreach (FrameWidget *widget, editWidgets) {
2214 // qDebug("WidgetLayout::clearWidgetLayout() removed editWidget");
2215 delete widget;
2216 }
2217 editWidgets.clear();
2218 consoleWidgets.clear();
2219 graphWidgets.clear();
2220 scopeWidgets.clear();
2221 widgetsMutex.unlock();
2222 }
2223
2224
2225 QStringList WidgetLayout::getUuids()
2226 { QStringList uuids = QStringList();
2227 for (int i=0; i<m_widgets.size(); i++) {
2228 uuids.append( m_widgets[i]->getUuid() );
2229 }
2230 return uuids;
2231 }
2232
2233 QStringList WidgetLayout::listProperties(QString widgetid)
2234 {
2235
2236 QStringList prop_names = QStringList();
2237 for (int i = 0; i < m_widgets.size(); i++) {
2238 if ( (m_widgets[i]->getUuid() == widgetid) || (m_widgets[i]->getChannelName() == widgetid) ) {
2239 QList<QByteArray> props= m_widgets[i]->dynamicPropertyNames();
2240
2241 foreach (QByteArray prop, props) {
2242 prop_names << QString(prop);
2243 }
2244 return prop_names;
2245
2246 }
2247 }
2248 return QStringList();
2249
2250 }
2251
2252
2253 bool WidgetLayout::destroyWidget(QString widgetid)
2254 {
2255 for (int i = 0; i < m_widgets.size(); i++) {
2256 if ( (m_widgets[i]->getUuid() == widgetid) || (m_widgets[i]->getChannelName() == widgetid) ) {
2257 // is it necessary to use widgetsMutex.lock(); / unlock?
2258 deleteWidget(m_widgets[i]);
2259 markHistory(); // is it necessary here? probably yes, possible to undo
2260 return true;
2261 }
2262 }
2263
2264 return false;
2265 }
2266
2267 void WidgetLayout::propertiesDialog()
2268 {
2269 QDialog *dialog = new QDialog(this);
2270 dialog->resize(300, 120);
2271 dialog->setModal(true);
2272 dialog->setWindowTitle("Widget Panel");
2273 QGridLayout *layout = new QGridLayout(dialog);
2274 bgCheckBox = new QCheckBox(dialog);
2275 bgCheckBox->setText("Enable Background");
2276 if (m_contained) {
2277 bgCheckBox->setChecked(parentWidget()->autoFillBackground());
2278 }
2279 else {
2280 bgCheckBox->setChecked(this->autoFillBackground());
2281 }
2282 layout->addWidget(bgCheckBox, 0, 0, Qt::AlignRight|Qt::AlignVCenter);
2283 QLabel *label = new QLabel(dialog);
2284 label->setText("Color");
2285 label->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
2286 layout->addWidget(label, 1, 0, Qt::AlignRight|Qt::AlignVCenter);
2287 bgButton = new QPushButton(dialog);
2288 QPixmap pixmap = QPixmap(64,64);
2289 QColor color;
2290 if (m_contained) {
2291 color = parentWidget()->palette().button().color();
2292 }
2293 else {
2294 color = this->palette().button().color();
2295 }
2296 bgButton->setProperty("QCS_color",QVariant(color));
2297 pixmap.fill(color);
2298 bgButton->setIcon(pixmap);
2299 layout->addWidget(bgButton, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
2300 QPushButton *applyButton = new QPushButton(tr("Apply"));
2301 layout->addWidget(applyButton, 9, 1, Qt::AlignCenter|Qt::AlignVCenter);
2302 QPushButton *cancelButton = new QPushButton(tr("Cancel"));
2303 layout->addWidget(cancelButton, 9, 2, Qt::AlignCenter|Qt::AlignVCenter);
2304 QPushButton *acceptButton = new QPushButton(tr("Ok"));
2305 layout->addWidget(acceptButton, 9, 3, Qt::AlignCenter|Qt::AlignVCenter);
2306 connect(acceptButton, SIGNAL(released()), dialog, SLOT(accept()));
2307 connect(dialog, SIGNAL(accepted()), this, SLOT(applyProperties()));
2308 connect(applyButton, SIGNAL(released()), this, SLOT(applyProperties()));
2309 connect(cancelButton, SIGNAL(released()), dialog, SLOT(close()));
2310 connect(bgButton, SIGNAL(released()), this, SLOT(selectBgColor()));
2311 dialog->exec();
2312 }
2313
2314 void WidgetLayout::applyProperties()
2315 {
2316 QColor color = bgButton->property("QCS_color").value<QColor>();
2317 setBackground(bgCheckBox->isChecked(), color);
2318 widgetChanged();
2319 mouseBut2 = 0; // Button un clicked is not propagated after opening the edit dialog. Do it artificially here
2320 }
2321
2322 void WidgetLayout::selectBgColor()
2323 {
2324 QColor color;
2325 if (m_contained) {
2326 color = QColorDialog::getColor(parentWidget()->palette().button().color(), this);
2327 }
2328 else {
2329 color = QColorDialog::getColor(this->palette().button().color(), this);
2330 }
2331 if (color.isValid()) {
2332 bgButton->setProperty("QCS_color",QVariant(color));
2333 QPixmap pixmap(64,64);
2334 pixmap.fill(color);
2335 bgButton->setIcon(pixmap);
2336 }
2337 }
2338
2339 void WidgetLayout::alignLeft()
2340 {
2341 int leftmost = 99999;
2342 if (m_editMode) {
2343 widgetsMutex.lock();
2344 int size = editWidgets.size();
2345 for (int i = 0; i < size ; i++) { // First find leftmost
2346 if (editWidgets[i]->isSelected())
2347 leftmost = qMin(leftmost, editWidgets[i]->x());
2348 }
2349 if(leftmost == 99999) {
2350 // no widgets selected
2351 widgetsMutex.unlock();
2352 return;
2353 }
2354 for (int i = 0; i < size ; i++) { // Then put all x values to that
2355 if (editWidgets[i]->isSelected()) {
2356 editWidgets[i]->move(leftmost, editWidgets[i]->y());
2357 m_widgets[i]->move(leftmost, editWidgets[i]->y());
2358 }
2359 }
2360 widgetsMutex.unlock();
2361 markHistory();
2362 setModified(true);
2363 }
2364 }
2365
2366 void WidgetLayout::alignRight()
2367 {
2368 int rightmost = -1;
2369 if (m_editMode) {
2370 widgetsMutex.lock();
2371 int size = editWidgets.size();
2372 for (int i = 0; i < size ; i++) { // First find leftmost
2373 if (editWidgets[i]->isSelected())
2374 rightmost = qMax(rightmost, editWidgets[i]->x() + editWidgets[i]->width());
2375 }
2376 if(rightmost < 0) {
2377 // no widgets selected
2378 widgetsMutex.unlock();
2379 return;
2380 }
2381 for (int i = 0; i < size ; i++) { // Then put all x values to that
2382 if (editWidgets[i]->isSelected()) {
2383 int x = rightmost - editWidgets[i]->width();
2384 editWidgets[i]->move(x, editWidgets[i]->y());
2385 m_widgets[i]->move(x, editWidgets[i]->y());
2386 }
2387 }
2388 widgetsMutex.unlock();
2389 markHistory();
2390 setModified(true);
2391 }
2392 }
2393
2394 void WidgetLayout::alignTop()
2395 {
2396 int top = 99999;
2397 if (m_editMode) {
2398 widgetsMutex.lock();
2399 int size = editWidgets.size();
2400 for (int i = 0; i < size ; i++) { // First find uppermost
2401 if (editWidgets[i]->isSelected())
2402 top = editWidgets[i]->y() < top ? editWidgets[i]->y(): top;
2403 }
2404 if(top == 99999) {
2405 // no widgets selected
2406 widgetsMutex.unlock();
2407 return;
2408 }
2409 for (int i = 0; i < size ; i++) { // Then put all y values to that
2410 if (editWidgets[i]->isSelected()) {
2411 editWidgets[i]->move(editWidgets[i]->x(), top);
2412 m_widgets[i]->move(editWidgets[i]->x(), top);
2413 }
2414 }
2415 widgetsMutex.unlock();
2416 markHistory();
2417 setModified(true);
2418 }
2419 }
2420
2421 void WidgetLayout::alignBottom()
2422 {
2423 int bottom = -1;
2424 if (m_editMode) {
2425 widgetsMutex.lock();
2426 int size = editWidgets.size();
2427 for (int i = 0; i < size ; i++) {
2428 if (editWidgets[i]->isSelected())
2429 bottom = qMax(bottom, editWidgets[i]->y()+editWidgets[i]->height());
2430 }
2431 if(bottom < 0) {
2432 // no widgets selected
2433 widgetsMutex.unlock();
2434 return;
2435 }
2436 for (int i = 0; i < size ; i++) {
2437 if (editWidgets[i]->isSelected()) {
2438 int y = bottom - editWidgets[i]->height();
2439 editWidgets[i]->move(editWidgets[i]->x(), y);
2440 m_widgets[i]->move(editWidgets[i]->x(), y);
2441 }
2442 }
2443 widgetsMutex.unlock();
2444 markHistory();
2445 setModified(true);
2446 }
2447 }
2448
2449 void WidgetLayout::sendToBack()
2450 {
2451 if (!m_editMode)
2452 return;
2453
2454 widgetsMutex.lock();
2455 for (int i = 0; i < editWidgets.size() ; i++) { // First invert selection
2456 if (editWidgets[i]->isSelected()) {
2457 editWidgets[i]->deselect();
2458 }
2459 else {
2460 editWidgets[i]->select();
2461 }
2462 }
2463 widgetsMutex.unlock();
2464 cut();
2465 paste();
2466 deselectAll();
2467 markHistory();
2468 setModified(true);
2469 // widgetsMutex.lock();
2470 // for (int i = 0; i < editWidgets.size() ; i++) { // Now invert selection again
2471 // if (editWidgets[i]->isSelected()) {
2472 // editWidgets[i]->deselect();
2473 // }
2474 // else {
2475 // editWidgets[i]->select();
2476 // }
2477 // }
2478 // widgetsMutex.unlock();
2479 }
2480
2481 void WidgetLayout::sendToFront()
2482 {
2483 if(!m_editMode)
2484 return;
2485 if (m_editMode) {
2486 cut();
2487 paste();
2488 deselectAll();
2489 markHistory();
2490 setModified(true);
2491 }
2492 }
2493
2494
2495 void WidgetLayout::distributeHorizontal()
2496 {
2497 if (m_editMode) {
2498 int spacing, emptySpace, max = -9999, min = 9999, widgetWidth = 0;
2499 int num = 0;
2500 QVector<int> order;
2501 widgetsMutex.lock();
2502 for (int i = 0; i < editWidgets.size() ; i++) { // First check free space
2503 if (editWidgets[i]->isSelected()) {
2504 widgetWidth += editWidgets[i]->width();
2505 num++;
2506 if (min > editWidgets[i]->x()) { // Left most widget
2507 min = editWidgets[i]->x();
2508 order.prepend(i);
2509 }
2510 if (max < editWidgets[i]->x() + editWidgets[i]->width()) { // Right most widget
2511 max = editWidgets[i]->x() + editWidgets[i]->width();
2512 if (!order.contains(i))
2513 order.append(i);
2514 }
2515 if (!order.contains(i)) {
2516 int j = 0;
2517 while (j < order.size()) {
2518 if (editWidgets[order[j]]->x() > editWidgets[i]->x()) {
2519 break;
2520 }
2521 j++;
2522 }
2523 order.insert(j++, i);
2524 }
2525 }
2526 }
2527 if (num < 3)
2528 return; // do nothing for less than three selected
2529 emptySpace = max - min - widgetWidth;
2530 int accum = min;
2531 for (int i = 1; i < order.size() - 1 ; i++) { // Don't touch first and last
2532 spacing = emptySpace / (num- i);
2533 emptySpace -= spacing;
2534 accum += spacing + editWidgets[order[i-1]]->width();
2535 editWidgets[order[i]]->move(accum, editWidgets[order[i]]->y());
2536 m_widgets[order[i]]->move(accum, editWidgets[order[i]]->y());
2537 }
2538 widgetsMutex.unlock();
2539 markHistory();
2540 setModified(true);
2541 }
2542 }
2543
2544 void WidgetLayout::distributeVertical()
2545 {
2546 if (m_editMode) {
2547 int spacing, emptySpace, max = -9999, min = 9999, widgetHeight = 0;
2548 int num = 0;
2549 QVector<int> order;
2550 widgetsMutex.lock();
2551 for (int i = 0; i < editWidgets.size() ; i++) { // First check free space
2552 if (editWidgets[i]->isSelected()) {
2553 num++;
2554 widgetHeight += editWidgets[i]->height();
2555 if (min > editWidgets[i]->y()) { // Bottom widget
2556 min = editWidgets[i]->y();
2557 order.prepend(i);
2558 }
2559 if (max < editWidgets[i]->y() + editWidgets[i]->height()) { // Topmost widget
2560 max = editWidgets[i]->y() + editWidgets[i]->height();
2561 if (!order.contains(i))
2562 order.append(i);
2563 }
2564 if (!order.contains(i)) {
2565 int j = 0;
2566 while (j < order.size()) {
2567 if (editWidgets[order[j]]->y() > editWidgets[i]->y()) {
2568 break;
2569 }
2570 j++;
2571 }
2572 order.insert(j++, i);
2573 }
2574 }
2575 }
2576 if (num < 3)
2577 return; // do nothing for less than three selected
2578 emptySpace = max - min - widgetHeight;
2579 // qDebug() << "WidgetLayout::distributeHorizontal " << emptySpace << "---" << order;
2580 int accum = min;
2581 for (int i = 1; i < order.size() - 1 ; i++) { // Don't touch first and last
2582 spacing = emptySpace / (num - i);
2583 emptySpace -= spacing;
2584 accum += spacing + editWidgets[order[i-1]]->height();
2585 editWidgets[order[i]]->move(editWidgets[order[i]]->x(), accum);
2586 m_widgets[order[i]]->move(editWidgets[order[i]]->x(), accum);
2587 }
2588 widgetsMutex.unlock();
2589 markHistory();
2590 setModified(true);
2591 }
2592 }
2593
2594 void WidgetLayout::alignCenterHorizontal()
2595 {
2596 if (m_editMode) {
2597 int center = 0, min = 9999;
2598 widgetsMutex.lock();
2599 for (int i = 0; i < editWidgets.size() ; i++) { // First find center
2600 if (editWidgets[i]->isSelected()) {
2601 if (min > editWidgets[i]->y()) { // Upper widget
2602 min = editWidgets[i]->y();
2603 center = editWidgets[i]->x() + (editWidgets[i]->width()/2);
2604 }
2605 }
2606 }
2607 for (int i = 0; i < editWidgets.size() ; i++) {
2608 if (editWidgets[i]->isSelected()) {
2609 int newx = center - (editWidgets[i]->width()/2);
2610 editWidgets[i]->move(newx, editWidgets[i]->y());
2611 m_widgets[i]->move(newx, m_widgets[i]->y());
2612 }
2613 }
2614 widgetsMutex.unlock();
2615 markHistory();
2616 setModified(true);
2617 }
2618 }
2619
2620 void WidgetLayout::alignCenterVertical()
2621 {
2622 if (m_editMode) {
2623 int center = 0, min = 9999;
2624 widgetsMutex.lock();
2625 for (int i = 0; i < editWidgets.size() ; i++) { // First find center
2626 if (editWidgets[i]->isSelected()) {
2627 if (min > editWidgets[i]->x()) { // Leftmost widget
2628 min = editWidgets[i]->x();
2629 center = editWidgets[i]->y() + (editWidgets[i]->height()/2);
2630 }
2631 }
2632 }
2633 for (int i = 0; i < editWidgets.size() ; i++) {
2634 if (editWidgets[i]->isSelected()) {
2635 int newy = center - (editWidgets[i]->height()/2);
2636 editWidgets[i]->move(editWidgets[i]->x(), newy);
2637 m_widgets[i]->move(m_widgets[i]->x(), newy);
2638 }
2639 }
2640 widgetsMutex.unlock();
2641 markHistory();
2642 setModified(true);
2643 }
2644 }
2645
2646 void WidgetLayout::keyPressEvent(QKeyEvent *event)
2647 {
2648 int key = event->key();
2649 if(m_editMode) {
2650 int quant = (event->modifiers() & Qt::AltModifier) ? 1 : 5;
2651 switch(key) {
2652 case Qt::Key_Left:
2653 this->moveSelected(-quant, 0, quant);
2654 event->accept();
2655 return;
2656 case Qt::Key_Right:
2657 this->moveSelected(quant, 0, quant);
2658 event->accept();
2659 return;
2660 case Qt::Key_Up:
2661 this->moveSelected(0, -quant, quant);
2662 event->accept();
2663 return;
2664 case Qt::Key_Down:
2665 this->moveSelected(0, quant, quant);
2666 event->accept();
2667 return;
2668 case Qt::Key_S:
2669 this->createNewSlider();
2670 event->accept();
2671 return;
2672 case Qt::Key_K:
2673 this->createNewKnob();
2674 event->accept();
2675 return;
2676 case Qt::Key_B:
2677 this->createNewButton();
2678 event->accept();
2679 return;
2680 case Qt::Key_A:
2681 if(event->modifiers() & Qt::ControlModifier) {
2682 this->selectAll();
2683 event->accept();
2684 return;
2685 }
2686 }
2687
2688 }
2689
2690 if (!event->isAutoRepeat() || m_repeatKeys) {
2691 QString keyText = event->text();
2692 if (key == Qt::Key_D && (event->modifiers() & Qt::ControlModifier )) {
2693 // TODO why is this necessary? The shortcut from the duplicate
2694 // action in the main app is not working!
2695 this->duplicate();
2696 event->accept();
2697 return;
2698 }
2699
2700 // if (event->key() == Qt::Key_X && (event->modifiers() & Qt::ControlModifier )) {
2701 // this->cut();
2702 // event->accept();
2703 // return;
2704 // }
2705 // else if (event->key() == Qt::Key_C && (event->modifiers() & Qt::ControlModifier )) {
2706 // this->copy();
2707 // event->accept();
2708 // return;
2709 // }
2710 // else if (event->key() == Qt::Key_V && (event->modifiers() & Qt::ControlModifier )) {
2711 // this->paste();
2712 // event->accept();
2713 // return;
2714 // }
2715 if (key == Qt::Key_Delete || key == Qt::Key_Backspace) {
2716 this->deleteSelected();
2717 event->accept();
2718 return;
2719 }
2720 // else if (event->matches(QKeySequence::Undo)) {
2721 // this->undo();
2722 // event->accept();
2723 // }
2724 // else if (event->matches(QKeySequence::Redo)) {
2725 // this->redo();
2726 // event->accept();
2727 // }
2728 else { //else if (key != "") {
2729 // qDebug() << "WidgetLayout::keyPressEvent" <convolve< key;
2730 // appendMessage(key);
2731 QWidget::keyPressEvent(event); // Propagate event if not used
2732 if (!keyText.isEmpty()) {
2733 int key = static_cast<int>( keyText[0].toLatin1() ); // if simple character send ASCII code
2734 emit keyPressed(key);
2735 } else {
2736 emit keyPressed(event->key()); // else send keycode and let CsoundEngine process it
2737 }
2738 // event->accept();
2739 }
2740 }
2741 }
2742
2743 void WidgetLayout::keyReleaseEvent(QKeyEvent *event)
2744 {
2745 if (!event->isAutoRepeat() || m_repeatKeys) {
2746 QString keyText = event->text();
2747 int key = -1;
2748 if (!keyText.isEmpty()) {
2749 key = (int) keyText[0].toLatin1(); // if simple character send ASCII code
2750 } else {
2751 key = event->key();
2752 }
2753 emit keyReleased(key);
2754 }
2755 QWidget::keyReleaseEvent(event); // Propagate event
2756 }
2757
2758 void WidgetLayout::widgetChanged(QuteWidget* widget)
2759 {
2760 if (widget != nullptr) {
2761 // widgetsMutex.lock();
2762 int index = m_widgets.indexOf(widget);
2763 if (index >= 0 && editWidgets.size() > index) {
2764 int newx = widget->x();
2765 int newy = widget->y();
2766 int neww = widget->width();
2767 int newh = widget->height();
2768 editWidgets[index]->move(newx, newy);
2769 editWidgets[index]->resize(neww, newh);
2770 }
2771 setWidgetToolTip(widget, m_tooltips);
2772 // widgetsMutex.unlock();
2773 int cc = widget->property("QCS_midicc").toInt(); // Is it safe to query these here?
2774 int chan = widget->property("QCS_midichan").toInt();
2775 registerWidgetController(widget, cc);
2776 registerWidgetChannel(widget, chan);
2777 setModified(true);
2778 }
2779 adjustLayoutSize();
2780 }
2781
2782 void WidgetLayout::mousePressEvent(QMouseEvent *event)
2783 {
2784 if (m_editMode && (event->button() & Qt::LeftButton)) {
2785 this->setFocus(Qt::MouseFocusReason);
2786 selectionFrame->show();
2787 startx = event->x() + xOffset;
2788 starty = event->y() + yOffset;
2789 selectionFrame->setGeometry(startx, starty, 0,0);
2790 if (event->button() & Qt::LeftButton) {
2791 deselectAll();
2792 }
2793 }
2794 mouseLock.lockForWrite();
2795 if (event->button() == Qt::LeftButton)
2796 mouseBut1 = 1;
2797 else if (event->button() == Qt::RightButton)
2798 mouseBut2 = 1;
2799 mouseLock.unlock();
2800 // QWidget::mousePressEvent(event);
2801 }
2802
2803 void WidgetLayout::mouseMoveEvent(QMouseEvent *event)
2804 {
2805 // QWidget::mouseMoveEvent(event);
2806 int x = startx;
2807 int y = starty;
2808 int width = abs(event->x() - startx + xOffset);
2809 int height = abs(event->y() - starty + yOffset);
2810 if (event->buttons() & Qt::LeftButton) {
2811 // Currently dragging selection
2812 if (event->x() < (startx - xOffset)) {
2813 x = event->x() + xOffset;
2814 }
2815 if (event->y() < (starty - yOffset)) {
2816 y = event->y() + yOffset;
2817 }
2818 selectionFrame->setGeometry(x, y, width, height);
2819 selectionChanged(QRect(x - xOffset, y - yOffset, width, height));
2820 }
2821 mouseLock.lockForWrite();
2822 mouseX = event->globalX();
2823 mouseY = event->globalY();
2824 mouseRelX = event->x() + xOffset;
2825 mouseRelY = event->y() + yOffset;
2826 mouseLock.unlock();
2827 }
2828
2829 void WidgetLayout::mouseReleaseEvent(QMouseEvent *event)
2830 {
2831 if (event->button() & Qt::LeftButton) {
2832 selectionFrame->hide();
2833 }
2834 mouseLock.lockForWrite();
2835 if (event->button() == Qt::LeftButton)
2836 mouseBut1 = 0;
2837 else if (event->button() == Qt::RightButton) {
2838 emit deselectAll();
2839 mouseBut2 = 0;
2840 }
2841 mouseLock.unlock();
2842 markHistory();
2843 // QWidget::mouseReleaseEvent(event);
2844 }
2845
2846 void WidgetLayout::contextMenuEvent(QContextMenuEvent *event)
2847 {
2848 if (m_enableEdit) {
2849 createContextMenu(event);
2850 event->accept();
2851 }
2852 }
2853
2854 void WidgetLayout::closeEvent(QCloseEvent *event) {
2855 emit this->windowStatus(false);
2856 }
2857
2858 int WidgetLayout::parseXmlNode(QDomNode node)
2859 {
2860 int ret = 0;
2861 QString name = node.nodeName();
2862 if (name == "objectName") {
2863 m_objectName = node.firstChild().nodeValue();
2864 // this->setProperty("QCS_objectName", node.firstChild().nodeValue());
2865 }
2866 else if (name == "label") {
2867 this->setWindowTitle(node.firstChild().nodeValue());
2868 }
2869 else if (name == "x") {
2870 int newx = node.firstChild().nodeValue().toInt();
2871 m_posx = newx >= 0 && newx < 4096? newx : m_posx;
2872 }
2873 else if (name == "y") {
2874 int newy = node.firstChild().nodeValue().toInt();
2875 m_posy = newy >= 0 && newy < 4096? newy : m_posy;
2876 }
2877 else if (name == "width") {
2878 int neww = node.firstChild().nodeValue().toInt();
2879 m_w = neww >= 0 && neww < 4096? neww : m_w;
2880 }
2881 else if (name == "height") {
2882 int newh = node.firstChild().nodeValue().toInt();
2883 m_h = newh >= 0 && newh < 4096? newh : m_h;
2884 }
2885 else if (name == "visible") {
2886 m_visible = node.firstChild().nodeValue() == "true";
2887 }
2888 else if (name == "uuid") {
2889 m_uuid = node.firstChild().nodeValue();
2890 }
2891 else if (name == "bgcolor") {
2892 bool bg = false;
2893 auto nodeElement = node.toElement();
2894
2895 if (nodeElement.attribute("mode")== "background") {
2896 //qDebug() << "background true";
2897 bg = true;
2898 }
2899 QDomElement er = nodeElement.firstChildElement("r");
2900 QDomElement eg = nodeElement.firstChildElement("g");
2901 QDomElement eb = nodeElement.firstChildElement("b");
2902 auto bgcolor = QColor(er.firstChild().nodeValue().toInt(),
2903 eg.firstChild().nodeValue().toInt(),
2904 eb.firstChild().nodeValue().toInt());
2905 // auto parent = node.parentNode().nodeName();
2906 //qDebug() << "setting background" << parent << bgcolor << node.toElement().text();
2907 setBackground(bg, bgcolor);
2908 }
2909 else if (name == "bsbObject") {
2910 ret = newXmlWidget(node);
2911 }
2912 else if (name == "bsbGroup") {
2913 qDebug() << "bsbGroup not implemented";
2914 }
2915 else {
2916 qDebug() << "WidgetLayout::parseXmlNode unknown node name: "<< name;
2917 return -1;
2918 }
2919 return ret;
2920 }
2921
2922 QString WidgetLayout::createSlider(int x, int y, int width, int height, QString widgetLine)
2923 {
2924 // qDebug("ioSlider x=%i y=%i w=%i h=%i", x,y, width, height);
2925 QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
2926 QuteSlider *widget= new QuteSlider(this);
2927 widget->setProperty("QCS_x",x);
2928 widget->setProperty("QCS_y",y);
2929 widget->setProperty("QCS_width",width);
2930 widget->setProperty("QCS_height",height);
2931 widget->setProperty("QCS_minimum",parts[5].toDouble());
2932 widget->setProperty("QCS_maximum",parts[6].toDouble());
2933 widget->setProperty("QCS_value",parts[7].toDouble());
2934
2935 if (parts.size()>8) {
2936 int i=8;
2937 QString channelName = "";
2938 while (parts.size()>i) {
2939 channelName += parts[i] + " ";
2940 i++;
2941 }
2942 channelName.chop(1); //remove last space
2943 widget->setProperty("QCS_objectName", channelName);
2944 }
2945 widget->applyInternalProperties();
2946 connect(widget, SIGNAL(newValue(QPair<QString,double>)), this,
2947 SLOT(newValue(QPair<QString,double>)));
2948 registerWidget(widget);
2949 return widget->getUuid();
2950 }
2951
2952 QString WidgetLayout::createText(int x, int y, int width, int height, QString widgetLine)
2953 {
2954 QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
2955 QStringList quoteParts = widgetLine.split('"');
2956 if (parts.size()<20 || quoteParts.size()<5)
2957 return "";
2958 QStringList lastParts = quoteParts[4].split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
2959 if (lastParts.size() < 9)
2960 return "";
2961 QuteText *widget= new QuteText(this);
2962 widget->setProperty("QCS_x",x);
2963 widget->setProperty("QCS_y",y);
2964 widget->setProperty("QCS_width",width);
2965 widget->setProperty("QCS_height",height);
2966 widget->setType(parts[5]);
2967 widget->setProperty("QCS_objectName",quoteParts[1]);
2968 widget->setProperty("QCS_alignment",quoteParts[2].simplified());
2969 widget->setProperty("QCS_font",quoteParts[3].simplified());
2970 widget->setProperty("QCS_fontsize",lastParts[0].toInt() + 2);
2971 widget->setProperty("QCS_color", QColor(lastParts[1].toDouble()/256.0,
2972 lastParts[2].toDouble()/256.0,
2973 lastParts[3].toDouble()/256.0));
2974 widget->setProperty("QCS_bgcolor", QColor(lastParts[4].toDouble()/256.0,
2975 lastParts[5].toDouble()/256.0,
2976 lastParts[6].toDouble()/256.0));
2977 widget->setProperty("QCS_bgcolormode", lastParts[7] == "background");
2978 widget->setProperty("QCS_bordermode", lastParts[8]);
2979 QString labelText = widgetLine.mid(widgetLine.indexOf("border") + 7);
2980 if (parts[5] == "label") {
2981 widget->setProperty("QCS_precision", 3);
2982 widget->setTransparentForMouse(true);
2983 }
2984 else if (parts[5] == "display") {
2985 widget->setProperty("QCS_precision", 3);
2986 }
2987 else {
2988 widget->setProperty("QCS_resolution", parts[7].toDouble());
2989 }
2990 labelText.replace("\u00AC", "\n");
2991 widget->setProperty("QCS_label", labelText);
2992 widget->setFontOffset(m_fontOffset);
2993 widget->setFontScaling(m_fontScaling);
2994 widget->applyInternalProperties();
2995 registerWidget(widget);
2996 return widget->getUuid();
2997 }
2998
2999 QString WidgetLayout::createScrollNumber(int x, int y, int width, int height, QString widgetLine)
3000 {
3001 QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3002 QStringList quoteParts = widgetLine.split('"');
3003 if (parts.size()<20 || quoteParts.size()<5)
3004 return "";
3005 QStringList lastParts = quoteParts[4].split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3006 if (lastParts.size() < 9)
3007 return "";
3008 QuteScrollNumber *widget= new QuteScrollNumber(this);
3009
3010 widget->setProperty("QCS_x",x);
3011 widget->setProperty("QCS_y",y);
3012 widget->setProperty("QCS_width",width);
3013 widget->setProperty("QCS_height",height);
3014 widget->setType(parts[5]);
3015 widget->setProperty("QCS_resolution", parts[7].toDouble());
3016 widget->setProperty("QCS_objectName",quoteParts[1]);
3017 widget->setProperty("QCS_alignment",quoteParts[2].simplified());
3018 widget->setProperty("QCS_font",quoteParts[3].simplified());
3019 widget->setProperty("QCS_fontsize",lastParts[0].toInt() + 2);
3020 widget->setProperty("QCS_color", QColor(lastParts[1].toDouble()/256.0,
3021 lastParts[2].toDouble()/256.0,
3022 lastParts[3].toDouble()/256.0));
3023 widget->setProperty("QCS_bgcolor", QColor(lastParts[4].toDouble()/256.0,
3024 lastParts[5].toDouble()/256.0,
3025 lastParts[6].toDouble()/256.0));
3026 widget->setProperty("QCS_bgcolormode", lastParts[7] == "background");
3027 widget->setProperty("QCS_bordermode", lastParts[8]);
3028
3029 widget->setFontOffset(m_fontOffset);
3030 widget->setFontScaling(m_fontScaling);
3031 QString labelText = "";
3032 int i = 9;
3033 while (lastParts.size() > i) {
3034 labelText += lastParts[i] + " ";
3035 i++;
3036 }
3037 labelText.chop(1);
3038 widget->setProperty("QCS_value", labelText.toDouble());
3039 // widget->setValue(labelText.toDouble());
3040 widget->applyInternalProperties();
3041 connect(widget, SIGNAL(newValue(QPair<QString,double>)),
3042 this, SLOT(newValue(QPair<QString,double>)));
3043 registerWidget(widget);
3044 return widget->getUuid();
3045 }
3046
3047 QString WidgetLayout::createLineEdit(int x, int y, int width, int height, QString widgetLine)
3048 {
3049 qDebug() << "createLineEdit";
3050 auto parts = widgetLine.splitRef(QRegularExpression("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3051 QStringList quoteParts = widgetLine.split('"');
3052 if (parts.size()<20 || quoteParts.size()<5)
3053 return "";
3054 QStringList lastParts = quoteParts[4].split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3055 if (lastParts.size() < 9)
3056 return "";
3057 QuteLineEdit *widget= new QuteLineEdit(this);
3058
3059 widget->setProperty("QCS_x",x);
3060 widget->setProperty("QCS_y",y);
3061 widget->setProperty("QCS_width",width);
3062 widget->setProperty("QCS_height",height);
3063 widget->setType(parts[5].toString());
3064 widget->setProperty("QCS_objectName", quoteParts[1]);
3065 widget->setProperty("QCS_alignment",quoteParts[2].simplified());
3066 widget->setProperty("QCS_font",quoteParts[3].simplified());
3067 widget->setProperty("QCS_fontsize",lastParts[0].toInt() + 2);
3068 widget->setProperty("QCS_color", QColor(lastParts[1].toDouble()/256.0,
3069 lastParts[2].toDouble()/256.0,
3070 lastParts[3].toDouble()/256.0));
3071 widget->setProperty("QCS_bgcolor", QColor(lastParts[4].toDouble()/256.0,
3072 lastParts[5].toDouble()/256.0,
3073 lastParts[6].toDouble()/256.0));
3074 widget->setProperty("QCS_bgcolormode", lastParts[7] == "background");
3075 widget->setProperty("QCS_bordermode", lastParts[8]);
3076 QString labelText = "";
3077 labelText = widgetLine.mid(widgetLine.indexOf("border") + 7);
3078 widget->setProperty("QCS_label", labelText);
3079 widget->applyInternalProperties();
3080 QDEBUG << ">>>>> name" << quoteParts[1] << ", bgcolormode" << lastParts[7];
3081
3082 registerWidget(widget);
3083 return widget->getUuid();
3084 }
3085
3086 QString WidgetLayout::createSpinBox(int x, int y, int width, int height, QString widgetLine)
3087 {
3088 QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3089 QStringList quoteParts = widgetLine.split('"');
3090 if (parts.size()<20 || quoteParts.size()<5)
3091 return "";
3092 QStringList lastParts = quoteParts[4].split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3093 if (lastParts.size() < 9)
3094 return "";
3095 QuteSpinBox *widget= new QuteSpinBox(this);
3096
3097 widget->setProperty("QCS_x",x);
3098 widget->setProperty("QCS_y",y);
3099 widget->setProperty("QCS_width",width);
3100 widget->setProperty("QCS_height",height);
3101 widget->setType(parts[5]);
3102 widget->setProperty("QCS_resolution",parts[7].toDouble());
3103 widget->setProperty("QCS_objectName",quoteParts[1]);
3104 widget->setProperty("QCS_alignment",quoteParts[2].simplified());
3105 widget->setProperty("QCS_font",quoteParts[3].simplified());
3106 widget->setProperty("QCS_fontsize",lastParts[0].toInt() + 2);
3107 widget->setProperty("QCS_color", QColor(lastParts[1].toDouble()/256.0,
3108 lastParts[2].toDouble()/256.0,
3109 lastParts[3].toDouble()/256.0));
3110 widget->setProperty("QCS_bgcolor", QColor(lastParts[4].toDouble()/256.0,
3111 lastParts[5].toDouble()/256.0,
3112 lastParts[6].toDouble()/256.0));
3113 widget->setProperty("QCS_bgcolormode", lastParts[7] == "background");
3114 widget->setProperty("QCS_bordermode", lastParts[8]);
3115
3116 QString labelText = "";
3117 int i = 9;
3118 while (lastParts.size() > i) {
3119 labelText += lastParts[i] + " ";
3120 i++;
3121 }
3122 labelText.chop(1);
3123 widget->setProperty("QCS_value", labelText.toDouble());
3124 connect(widget, SIGNAL(newValue(QPair<QString,double>)), this, SLOT(newValue(QPair<QString,double>)));
3125 widget->applyInternalProperties();
3126 registerWidget(widget);
3127 return widget->getUuid();
3128 }
3129
3130 QString WidgetLayout::createButton(int x, int y, int width, int height, QString widgetLine)
3131 {
3132 qDebug("WidgetPanel::createButton");
3133 QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3134 QStringList quoteParts = widgetLine.split('"');
3135 // if (parts.size()<20 || quoteParts.size()>5)
3136 // return -1;
3137 QStringList lastParts = quoteParts[4].split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3138 // if (lastParts.size() < 9)
3139 // return -1;
3140 QuteButton *widget= new QuteButton(this);
3141 widget->setProperty("QCS_x",x);
3142 widget->setProperty("QCS_y",y);
3143 widget->setProperty("QCS_width",width);
3144 widget->setProperty("QCS_height",height);
3145 widget->setProperty("QCS_objectName",quoteParts[1]);
3146 widget->setProperty("QCS_type",parts[5]);
3147 widget->setProperty("QCS_pressedValue",parts[6].toDouble()); //value produced by button when pushed
3148 // widget->setProperty("QCS_stringvalue",parts[7].toDouble()); // Not available in old format
3149 widget->setText(quoteParts[3]);
3150 widget->setProperty("QCS_image",quoteParts[5]);
3151 if (quoteParts.size()>6) {
3152 quoteParts[6].remove(0,1); //remove initial space
3153 widget->setProperty("QCS_eventLine", quoteParts[6]);
3154 }
3155 connect(widget, SIGNAL(queueEventSignal(QString)), this, SLOT(queueEvent(QString)));
3156 connect(widget, SIGNAL(newValue(QPair<QString,QString>)),
3157 this, SLOT(newValue(QPair<QString,QString>)));
3158 connect(widget, SIGNAL(newValue(QPair<QString,double>)), this, SLOT(newValue(QPair<QString,double>)));
3159 emit registerButton(widget);
3160 widget->applyInternalProperties();
3161 registerWidget(widget);
3162 return widget->getUuid();
3163 }
3164
3165 QString WidgetLayout::createKnob(int x, int y, int width, int height, QString widgetLine)
3166 {
3167 auto parts = widgetLine.splitRef(QRegularExpression("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3168 // QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3169 QuteKnob *widget= new QuteKnob(this);
3170 widget->setProperty("QCS_x",x);
3171 widget->setProperty("QCS_y",y);
3172 widget->setProperty("QCS_width",width);
3173 widget->setProperty("QCS_height",height);
3174 widget->setProperty("QCS_minimum",parts[5].toDouble());
3175 widget->setProperty("QCS_maximum",parts[6].toDouble());
3176 widget->setProperty("QCS_resolution",parts[7].toDouble());
3177 widget->setProperty("QCS_value",parts[8].toDouble());
3178 if (parts.size()>9) {
3179 int i=9;
3180 QString channelName = "";
3181 while (parts.size()>i) {
3182 channelName += parts[i] + " ";
3183 i++;
3184 }
3185 channelName.chop(1); //remove last space
3186 widget->setProperty("QCS_objectName", channelName);
3187 }
3188 connect(widget, SIGNAL(newValue(QPair<QString,double>)), this, SLOT(newValue(QPair<QString,double>)));
3189 widget->applyInternalProperties();
3190 registerWidget(widget);
3191 return widget->getUuid();
3192 }
3193
3194 QString WidgetLayout::createCheckBox(int x, int y, int width, int height, QString widgetLine)
3195 {
3196 auto parts = widgetLine.splitRef(QRegularExpression("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3197 QuteCheckBox *widget= new QuteCheckBox(this);
3198 widget->setProperty("QCS_x",x);
3199 widget->setProperty("QCS_y",y);
3200 widget->setProperty("QCS_width",width);
3201 widget->setProperty("QCS_height",height);
3202 widget->setValue(parts[5]=="on");
3203 if (parts.size()>6) {
3204 int i=6;
3205 QString channelName = "";
3206 while (parts.size()>i) {
3207 channelName += parts[i] + " ";
3208 i++;
3209 }
3210 channelName.chop(1); //remove last space
3211 widget->setProperty("QCS_objectName", channelName);
3212 }
3213 connect(widget, SIGNAL(newValue(QPair<QString,double>)), this, SLOT(newValue(QPair<QString,double>)));
3214 widget->applyInternalProperties();
3215 registerWidget(widget);
3216 return widget->getUuid();
3217 }
3218
3219 QString WidgetLayout::createMenu(int x, int y, int width, int height, QString widgetLine)
3220 {
3221 auto parts = widgetLine.splitRef(QRegularExpression("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3222 auto quoteParts = widgetLine.splitRef('"');
3223 QuteComboBox *widget= new QuteComboBox(this);
3224 widget->setProperty("QCS_x",x);
3225 widget->setProperty("QCS_y",y);
3226 widget->setProperty("QCS_width",width);
3227 widget->setProperty("QCS_height",height);
3228 if (quoteParts.size() > 2) {
3229 widget->setProperty("QCS_objectName", quoteParts[2].trimmed().toString()); //remove initial space from channel name
3230 }
3231 widget->setProperty("QCS_selectedIndex", parts[5].toInt());
3232
3233 widget->setText(quoteParts[1].toString());
3234 connect(widget, SIGNAL(newValue(QPair<QString,double>)), this, SLOT(newValue(QPair<QString,double>)));
3235 widget->applyInternalProperties();
3236 registerWidget(widget);
3237 return widget->getUuid();
3238 }
3239
3240 QString WidgetLayout::createMeter(int x, int y, int width, int height, QString widgetLine)
3241 {
3242 QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3243 QStringList quoteParts = widgetLine.split('"');
3244 if (quoteParts.size() < 5) {
3245 qDebug("WidgetPanel::createMeter ERROR parsing widget line!");
3246 return 0;
3247 }
3248 QStringList parts2 = quoteParts[4].split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3249 if (parts2.size() < 5) {
3250 qDebug("WidgetPanel::createMeter ERROR parsing widget line!");
3251 return 0;
3252 }
3253 QuteMeter *widget= new QuteMeter(this);
3254 widget->setProperty("QCS_x",x);
3255 widget->setProperty("QCS_y",y);
3256 widget->setProperty("QCS_width",width);
3257 widget->setProperty("QCS_height",height);
3258 widget->setProperty("QCS_color" ,QColor(parts[5].toDouble()/256.0,
3259 parts[6].toDouble()/256.0,
3260 parts[7].toDouble()/256.0));
3261 widget->setProperty("QCS_type", parts2[1]);
3262 widget->setProperty("QCS_objectName2", quoteParts[1]);
3263 widget->setProperty("QCS_yValue", quoteParts[2].toDouble());
3264 widget->setProperty("QCS_xValue", parts2[0].toDouble());
3265 widget->setProperty("QCS_objectName", quoteParts[3]);
3266 widget->setProperty("QCS_pointsize", parts2[2].toInt());
3267 widget->setProperty("QCS_fadeSpeed", parts2[3].toInt());
3268 widget->setProperty("QCS_xMin", 0.0);
3269 widget->setProperty("QCS_xMax", 1.0);
3270 widget->setProperty("QCS_yMin", 0.0);
3271 widget->setProperty("QCS_yMax", 1.0);
3272 // widget->setBehavior(parts2[4]);
3273
3274 connect(widget, SIGNAL(newValue(QPair<QString,double>)), this, SLOT(newValue(QPair<QString,double>)));
3275 widget->applyInternalProperties();
3276 registerWidget(widget);
3277 return widget->getUuid();
3278 }
3279
3280 QString WidgetLayout::createConsole(int x, int y, int width, int height, QString widgetLine)
3281 {
3282 // qDebug("ioListing x=%i y=%i w=%i h=%i", x,y, width, height);
3283 QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3284 QuteConsole *widget= new QuteConsole(this);
3285 widget->setProperty("QCS_x",x);
3286 widget->setProperty("QCS_y",y);
3287 widget->setProperty("QCS_width",width);
3288 widget->setProperty("QCS_height",height);
3289
3290 widget->applyInternalProperties();
3291 consoleWidgets.append(widget);
3292 registerWidget(widget);
3293 return widget->getUuid();
3294 }
3295
3296 QString WidgetLayout::createGraph(int x, int y, int width, int height, QString widgetLine)
3297 {
3298 QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3299 QuteGraph *widget= new QuteGraph(this);
3300 widget->setProperty("QCS_x",x);
3301 widget->setProperty("QCS_y",y);
3302 widget->setProperty("QCS_width",width);
3303 widget->setProperty("QCS_height",height);
3304 //Graph widget is always of type "graph" part 5 is discarded
3305 if (parts.size() > 6)
3306 widget->setValue(parts[6].toDouble());
3307 if (parts.size() > 7) {
3308 widget->setProperty("QCS_zoomx",parts[7].toDouble());
3309 }
3310 else
3311 widget->setProperty("QCS_zoomx", 1.0);
3312 if (parts.size()>8) {
3313 int i = 8;
3314 QString channelName = "";
3315 while (parts.size()>i) {
3316 channelName += parts[i] + " ";
3317 i++;
3318 }
3319 channelName.chop(1); //remove last space
3320 widget->setProperty("QCS_objectName", channelName);
3321 }
3322 for (int i = 0; i < curves.size(); i++) {
3323 widget->addCurve(curves[i]);
3324 }
3325 graphWidgets.append(widget);
3326 emit requestCsoundUserData(widget);
3327 emit registerGraph(widget);
3328 registerWidget(widget);
3329 widget->applyInternalProperties();
3330 return widget->getUuid();
3331 }
3332
3333 QString WidgetLayout::createScope(int x, int y, int width, int height, QString widgetLine)
3334 {
3335 QuteScope *widget= new QuteScope(this);
3336 widget->setProperty("QCS_x",x);
3337 widget->setProperty("QCS_y",y);
3338 widget->setProperty("QCS_width",width);
3339 widget->setProperty("QCS_height",height);
3340 QStringList parts = widgetLine.split(QRegExp("[\\{\\}, ]"), SKIP_EMPTY_PARTS);
3341 if (parts.size() > 5) {
3342 widget->setProperty("QCS_type",parts[5]);
3343 }
3344 if (parts.size() > 6) {
3345 widget->setProperty("QCS_zoomx",parts[6].toDouble());
3346 }
3347 if (parts.size() > 7) {
3348 int chans = (int) parts[7].toDouble();
3349 if (chans < 0) {
3350 chans = -255; // Force all 8 channels when loading old format
3351 }
3352 widget->setProperty("QCS_value", chans);
3353 }
3354 if (parts.size() > 8) {
3355 int i=8;
3356 QString channelName = "";
3357 while (parts.size()>i) {
3358 channelName += parts[i] + " ";
3359 i++;
3360 }
3361 channelName.chop(1); //remove last space
3362 widget->setProperty("QCS_objectName", channelName);
3363 }
3364 emit registerScope(widget);
3365 scopeWidgets.append(widget);
3366 registerWidget(widget);
3367 widget->applyInternalProperties();
3368 return widget->getUuid();
3369 }
3370
3371 QString WidgetLayout::createDummy(int x, int y, int width, int height, QString widgetLine)
3372 {
3373 (void) widgetLine;
3374 QuteWidget *widget= new QuteDummy(this);
3375 widget->setProperty("QCS_x",x);
3376 widget->setProperty("QCS_y",y);
3377 widget->setProperty("QCS_width",width);
3378 widget->setProperty("QCS_height",height);
3379 connect(widget, SIGNAL(propertiesAccepted()), this, SLOT(markHistory()));
3380 widget->show();
3381 widgetsMutex.lock();
3382 m_widgets.append(widget);
3383 if (m_editMode) {
3384 createEditFrame(widget);
3385 editWidgets.last()->select();
3386 }
3387 setWidgetToolTip(widget, m_tooltips);
3388 widgetsMutex.unlock();
3389 return widget->getUuid();
3390 }
3391
3392 QString WidgetLayout::createTableDisplay(int x, int y, int width, int height, QString widgetLine) {
3393
3394 QuteTable *widget = new QuteTable(this);
3395 widget->setProperty("QCS_x", x);
3396 widget->setProperty("QCS_y", y);
3397 widget->setProperty("QCS_width", width);
3398 widget->setProperty("QCS_height", height);
3399 widget->setProperty("QCS_tableNumber", 0);
3400
3401 emit requestCsoundUserData(widget);
3402 registerWidget(widget);
3403 widget->applyInternalProperties();
3404 return widget->getUuid();
3405 }
3406
3407 void WidgetLayout::setBackground(bool bg, QColor bgColor)
3408 {
3409 QWidget *w;
3410 layoutMutex.lock();
3411 w = m_contained ? this->parentWidget() : this; // If contained, set background of parent widget
3412
3413 if (bg) // to get rid of pink backgrounds
3414 w->setPalette(QPalette(bgColor));
3415 else
3416 w->setPalette(QPalette());
3417 w->setBackgroundRole(QPalette::Window);
3418 w->setAutoFillBackground(bg);
3419 layoutMutex.unlock();
3420 this->setProperty("QCS_bg", QVariant(bg));
3421 this->setProperty("QCS_bgcolor", QVariant(bgColor));
3422 }
3423
3424 FrameWidget *WidgetLayout::getEditWidget(QuteWidget *widget)
3425 {
3426 // This function is only called from setWidgetTooltip, so it should not be locked,
3427 // because it already is
3428 FrameWidget *out = 0;
3429 for (int i = 0 ; i < m_widgets.size(); i++) {
3430 if (m_widgets[i] == widget) {
3431 if (editWidgets.size() > i) {
3432 return editWidgets[i];
3433 }
3434 }
3435 }
3436 return out;
3437 }
3438
3439 void WidgetLayout::registerWidgetController(QuteWidget *widget, int cc)
3440 {
3441 for (int i = 0; i < registeredControllers.size(); i++) {
3442 if (registeredControllers[i].widget == widget) {
3443 registeredControllers[i].cc = cc;
3444 return;
3445 }
3446 }
3447 // right order of parameters: RegisteredController(QuteWidget * _widget, int _chan, int _cc)
3448 registeredControllers << RegisteredController(widget, 0, cc);
3449 }
3450
3451 void WidgetLayout::registerWidgetChannel(QuteWidget *widget, int chan)
3452 {
3453 if (chan < 0) { // No control
3454 return;
3455 }
3456 for (int i = 0; i < registeredControllers.size(); i++) {
3457 if (registeredControllers[i].widget == widget) {
3458 registeredControllers[i].chan = chan;
3459 return;
3460 }
3461 }
3462 // RegisteredController(QuteWidget * _widget, int _chan,int _cc)
3463 registeredControllers << RegisteredController(widget, chan, 1);
3464 }
3465
3466 void WidgetLayout::unregisterWidgetController(QuteWidget *widget)
3467 {
3468 for (int i = 0; i < registeredControllers.size(); i++) {
3469 if (registeredControllers[i].widget == widget) {
3470 registeredControllers.removeAt(i);
3471 return;
3472 }
3473 }
3474 }
3475
3476 void WidgetLayout::clearWidgetControllers()
3477 {
3478 registeredControllers.clear();
3479 }
3480
3481 void WidgetLayout::setModified(bool mod)
3482 {
3483 m_modified = mod;
3484 if (mod) {
3485 emit changed();
3486 }
3487 }
3488
3489 void WidgetLayout::setMouseOffset(int x, int y)
3490 {
3491 xOffset = -x;
3492 yOffset = -y;
3493 }
3494
3495 void WidgetLayout::clearHistory()
3496 {
3497 m_history.clear();
3498 m_history << "";
3499 m_historyIndex = 0;
3500 }
3501
3502 int WidgetLayout::getPresetIndex(int number)
3503 {
3504 for (int i = 0; i < presets.size(); i++) {
3505 if (presets[i].getNumber() == number) {
3506 return i;
3507 }
3508 }
3509 return -1;
3510 }
3511
3512 void WidgetLayout::loadPreset()
3513 {
3514 QDialog d(this);
3515 QVBoxLayout *l = new QVBoxLayout(&d);
3516 QPushButton *okButton = new QPushButton(tr("Close"),&d);
3517 QPushButton *newButton = new QPushButton(tr("New Preset"),&d);
3518
3519 QTreeWidget *treeWidget = new QTreeWidget(&d);
3520
3521 treeWidget->setHeaderLabel(tr("Double-click Preset to Load"));
3522 l->addWidget(treeWidget);
3523 l->addWidget(okButton);
3524 l->addWidget(newButton);
3525
3526 connect(okButton, SIGNAL(released()), &d, SLOT(accept()));
3527 connect(newButton, SIGNAL(released()), this, SLOT(newPreset()));
3528 connect(newButton, SIGNAL(released()), &d, SLOT(reject())); // For now just close the load preset window (instead of refreshing...
3529
3530 treeWidget->setColumnCount(1);
3531 QList<QTreeWidgetItem *> items;
3532 for (int i = 0; i < presets.size(); i++) {
3533 QString itemText = QString::number(presets[i].getNumber()) + " " + presets[i].getName();
3534 QTreeWidgetItem *item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(itemText));
3535 item->setData(0,Qt::UserRole,presets[i].getNumber());
3536 items.append(item);
3537 }
3538 QList<QTreeWidgetItem *> sortedItems;
3539 for (int i = 0; i < items.size(); i++) {
3540 int j = 0;
3541 for (j = 0; j < sortedItems.size(); j++) {
3542 if (items[i]->data(0,Qt::UserRole).toInt() < sortedItems[j]->data(0,Qt::UserRole).toInt()) {
3543 break;
3544 }
3545 }
3546 sortedItems.insert(j, items[i]);
3547 }
3548 treeWidget->insertTopLevelItems(0, sortedItems);
3549 connect(treeWidget,SIGNAL(itemDoubleClicked (QTreeWidgetItem * , int)),
3550 this, SLOT(loadPresetFromItem(QTreeWidgetItem * , int)) );
3551
3552 d.setModal(false);
3553 d.exec();
3554 }
3555
3556 void WidgetLayout::loadPresetFromAction()
3557 {
3558 QAction *s = static_cast<QAction *>(sender());
3559 loadPreset(s->data().toInt());
3560 }
3561
3562 void WidgetLayout::loadPresetFromItem(QTreeWidgetItem * item, int column)
3563 {
3564 loadPreset(item->data(column,Qt::UserRole).toInt());
3565 }
3566
3567 void WidgetLayout::loadPreset(int num)
3568 {
3569 int index = getPresetIndex(num);
3570 loadPresetFromIndex(index);
3571 }
3572
3573 void WidgetLayout::loadPresetFromIndex(int index)
3574 {
3575 if (index < 0 || index >= presets.size()) {
3576 qDebug() << "WidgetLayout::loadPreset num invalid.";
3577 return;
3578 }
3579 m_currentPreset = index;
3580 WidgetPreset p = presets[index];
3581 QStringList ids = p.getWidgetIds();
3582 widgetsMutex.lock();
3583 for (int i = 0; i < ids.size(); i++) {
3584 QString savedId = ids[i];
3585 for (int j = 0; j < m_widgets.size(); j++) {
3586 QString id = m_widgets[j]->getUuid();
3587 if (savedId != id)
3588 continue;
3589 int mode = p.getMode(i);
3590 if (mode & 1) {
3591 m_widgets[j]->setValue(p.getValue(i));
3592 QString channel = m_widgets[j]->getChannelName();
3593 // Store the value in the changes buffer to read from chnget
3594 if (!channel.isEmpty()) {
3595 valueMutex.lock();
3596 if(newValues.contains(channel))
3597 newValues[channel] = p.getValue(i);
3598 else
3599 newValues.insert(channel, p.getValue(i));
3600 valueMutex.unlock();
3601 }
3602 }
3603 if (mode & 2) {
3604 m_widgets[j]->setValue2(p.getValue2(i));
3605 QString channel = m_widgets[j]->getChannelName();
3606 // store the value in the changes buffer to read from chnget
3607 if (!channel.isEmpty()) {
3608 valueMutex.lock();
3609 if(newValues.contains(channel))
3610 newValues[channel] = p.getValue2(i);
3611 else
3612 newValues.insert(channel, p.getValue2(i));
3613 valueMutex.unlock();
3614 }
3615 }
3616 if (mode & 4) {
3617 m_widgets[j]->setValue(p.getStringValue(i));
3618 QString channel = m_widgets[j]->getChannelName();
3619 // Store the value in the changes buffer to read from chnget
3620 if (!channel.isEmpty()) {
3621 stringValueMutex.lock();
3622 if(newStringValues.contains(channel))
3623 newStringValues[channel] = p.getStringValue(i);
3624 else
3625 newStringValues.insert(channel, p.getStringValue(i));
3626 stringValueMutex.unlock();
3627 }
3628 }
3629 }
3630 }
3631 widgetsMutex.unlock();
3632 QPair<QString, QString> channelValue;
3633 channelValue.first = "_GetPresetName";
3634 channelValue.second = p.getName();
3635 newValue(channelValue);
3636 QPair<QString, double> channelValue2;
3637 channelValue2.first = "_GetPresetNumber";
3638 channelValue2.second = p.getNumber();
3639 newValue(channelValue2);
3640 }
3641
3642 void WidgetLayout::newPreset()
3643 {
3644 QDialog d(this);
3645 QGridLayout *l = new QGridLayout(&d);
3646 QLabel *nameLabel = new QLabel(tr("New Preset Name:"), &d);
3647 QLineEdit *nameLineEdit = new QLineEdit(&d);
3648 QLabel *numberLabel = new QLabel(tr("Number:"), &d);
3649 QSpinBox *numberSpinBox = new QSpinBox(&d);
3650 numberSpinBox->unsetLocale();
3651 QPushButton *okButton = new QPushButton(tr("Ok"),&d);
3652 QPushButton *cancelButton = new QPushButton(tr("Cancel"),&d);
3653 l->addWidget(nameLabel, 0,0, Qt::AlignRight);
3654 l->addWidget(nameLineEdit, 0,1, Qt::AlignLeft);
3655 l->addWidget(numberLabel, 1,0, Qt::AlignRight);
3656 l->addWidget(numberSpinBox, 1,1, Qt::AlignLeft);
3657 l->addWidget(cancelButton, 2,0, Qt::AlignCenter);
3658 l->addWidget(okButton, 2,1, Qt::AlignCenter);
3659 connect(okButton, SIGNAL(released()), &d, SLOT(accept()));
3660 connect(cancelButton, SIGNAL(released()), &d, SLOT(reject()));
3661
3662 int ret = d.exec();
3663 if (ret == QDialog::Accepted) {
3664 if (presetExists(numberSpinBox->value())) {
3665 int ret = QMessageBox::question(this, tr("Preset Already Exists"),
3666 tr("Preset %i already exists. Overwrite?").arg(numberSpinBox->value()),
3667 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
3668 if (ret == QMessageBox::Yes) {
3669 savePreset(numberSpinBox->value(), nameLineEdit->text() );
3670 }
3671 }
3672 else {
3673 savePreset(numberSpinBox->value(), nameLineEdit->text() );
3674 }
3675 }
3676 }
3677
3678
3679 void WidgetLayout::savePreset()
3680 {
3681 QDialog d(this);
3682 QVBoxLayout *l = new QVBoxLayout(&d);
3683 QLabel *lab= new QLabel(&d);
3684 QComboBox *box = new QComboBox(&d);
3685 QPushButton *okButton = new QPushButton(tr("Ok"),&d);
3686 QPushButton *cancelButton = new QPushButton(tr("Cancel"),&d);
3687 QPushButton *newButton = new QPushButton(tr("New Preset"),&d);
3688
3689 lab->setText(tr("Select Preset to save"));
3690 l->addWidget(lab);
3691 l->addWidget(box);
3692 l->addWidget(cancelButton);
3693 l->addWidget(newButton);
3694 l->addWidget(okButton);
3695 connect(okButton, SIGNAL(released()), &d, SLOT(accept()));
3696 connect(cancelButton, SIGNAL(released()), &d, SLOT(reject()));
3697 connect(newButton, SIGNAL(released()), this, SLOT(newPreset()));
3698 connect(newButton, SIGNAL(released()), &d, SLOT(reject())); // For now just close the load preset window (instead of refreshing...
3699 for (int i = 0; i < presets.size(); i++) {
3700 QString itemText = QString::number(presets[i].getNumber()) + " " + presets[i].getName();
3701 box->addItem(itemText, presets[i].getNumber() );
3702 }
3703 box->setCurrentIndex(m_currentPreset);
3704
3705 int ret = d.exec();
3706 if (ret == QDialog::Accepted) {
3707 if (box->currentIndex() < 0) {
3708 newPreset();
3709 }
3710 else {
3711 savePreset(presets[box->currentIndex()].getNumber(), presets[box->currentIndex()].getName());
3712 }
3713 }
3714 }
3715
3716 void WidgetLayout::savePreset(int num, QString name)
3717 {
3718 int index = getPresetIndex(num);
3719 WidgetPreset p;
3720 p.setName(name);
3721 p.setNumber(num);
3722 if (index == -1) {
3723 qDebug() << "WidgetLayout::savePreset new preset";
3724 presets.append(p);
3725 index = presets.size() - 1;
3726 }
3727 widgetsMutex.lock();
3728 for (int i = 0; i < m_widgets.size(); i++) {
3729 QString id = m_widgets[i]->getUuid();
3730 if (!(m_widgets[i]->getWidgetType() == "BSBLabel")
3731 && !(m_widgets[i]->getWidgetType() == "BSBLineEdit")
3732 && !(m_widgets[i]->getWidgetType() == "BSBButton")
3733 && !(m_widgets[i]->getWidgetType() == "BSBConsole")) {
3734 p.addValue(id, m_widgets[i]->getValue());
3735 }
3736 if (m_widgets[i]->getWidgetType() == "BSBButton") {
3737 if (static_cast<QuteButton *>(m_widgets[i])->property("QCS_latch").toBool()) {
3738 p.addValue(id, m_widgets[i]->getValue());
3739 }
3740 }
3741 if (m_widgets[i]->getWidgetType() == "BSBController"
3742 || m_widgets[i]->getWidgetType() == "BSBXYController") {
3743 p.addValue2(id, m_widgets[i]->getValue2());
3744 }
3745 if (m_widgets[i]->getWidgetType() == "BSBButton"
3746 || m_widgets[i]->getWidgetType() == "BSBLineEdit"
3747 || m_widgets[i]->getWidgetType() == "BSBDisplay") {
3748 p.addStringValue(id, m_widgets[i]->getStringValue());
3749 }
3750 // Note that BSBLabel is left out from presets
3751 }
3752 widgetsMutex.unlock();
3753 presets[index] = p;
3754 m_currentPreset = index;
3755 }
3756
3757 void WidgetLayout::setPresetName(int num, QString name)
3758 {
3759 if (num >= 0 && num < presets.size()) {
3760 int index = getPresetIndex(num);
3761 if (index >= 0) {
3762 presets[getPresetIndex(num)].setName(name);
3763 }
3764 else {
3765 qDebug() << "WidgetLayout::setPresetName invalid number.";
3766 }
3767 }
3768 }
3769
3770 QList<int> WidgetLayout::getPresetNums()
3771 {
3772 QList<int> list;
3773 for (int i = 0; i < presets.size(); i++) {
3774 list.append(presets[i].getNumber());
3775 }
3776 return list;
3777 }
3778
3779 QString WidgetLayout::getPresetName(int num)
3780 {
3781 int index = getPresetIndex(num);
3782 if (index >= 0) {
3783 return presets[getPresetIndex(num)].getName();
3784 }
3785 else {
3786 return QString();
3787 qDebug() << "WidgetLayout::getPresetName invalid number.";
3788 }
3789 }
3790
3791 bool WidgetLayout::presetExists(int num)
3792 {
3793 return getPresetNums().contains(num);
3794 }
3795
3796 QColor WidgetLayout::getColorFromElement(QDomElement elem)
3797 {
3798 QDomElement er = elem.firstChildElement("r");
3799 QDomElement eg = elem.firstChildElement("g");
3800 QDomElement eb = elem.firstChildElement("b");
3801 return QColor(er.firstChild().nodeValue().toInt(),
3802 eg.firstChild().nodeValue().toInt(),
3803 eb.firstChild().nodeValue().toInt());
3804 }
3805
3806 void WidgetLayout::copy()
3807 {
3808 qDebug() << "WidgetLayout::copy()";
3809 QString text;
3810 if (m_editMode) {
3811 widgetsMutex.lock();
3812 if (m_xmlFormat) {
3813 for (int i = 0; i < editWidgets.size() ; i++) {
3814 if (editWidgets[i]->isSelected()) {
3815 text += m_widgets[i]->getWidgetXmlText()+ "\n";
3816 }
3817 }
3818 }
3819 else {
3820 for (int i = 0; i < editWidgets.size() ; i++) {
3821 if (editWidgets[i]->isSelected()) {
3822 text += m_widgets[i]->getWidgetLine() + "\n";
3823 }
3824 }
3825 }
3826 widgetsMutex.unlock();
3827 QClipboard *clipboard = qApp->clipboard();
3828 clipboard->setText(text);
3829 }
3830 }
3831
3832 void WidgetLayout::cut()
3833 {
3834 qDebug() << "WidgetLayout::cut()";
3835 if (m_editMode) {
3836 WidgetLayout::copy();
3837 widgetsMutex.lock();
3838 for (int i = editWidgets.size() - 1; i >= 0 ; i--) {
3839 if (editWidgets[i]->isSelected()) {
3840 widgetsMutex.unlock();
3841 deleteWidget(m_widgets[i]);
3842 widgetsMutex.lock();
3843 }
3844 }
3845 widgetsMutex.unlock();
3846 markHistory();
3847 }
3848 }
3849
3850 void WidgetLayout::paste()
3851 {
3852 qDebug() << "WidgetLayout::paste()";
3853 if (m_editMode) {
3854 deselectAll();
3855 QClipboard *clipboard = qApp->clipboard();
3856 QString clipboardText = clipboard->text();
3857 if (m_xmlFormat) {
3858 QDomDocument doc;
3859 QString errorText;
3860 int lineNumber, column;
3861 if (!doc.setContent("<doc>" + clipboardText + "</doc>",
3862 &errorText, &lineNumber, &column)) {
3863 qDebug() <<"WidgetLayout::paste Parsing Error: " << errorText << lineNumber
3864 << column << clipboardText;
3865 }
3866 QDomElement docElement = doc.firstChildElement("doc");
3867 QDomElement pl = docElement.firstChildElement("bsbObject");
3868 while (!pl.isNull()) {
3869 newXmlWidget(pl);
3870 editWidgets.last()->select();
3871 pl = pl.nextSiblingElement("bsbObject");
3872 }
3873 }
3874 else {
3875 QStringList lines = clipboardText.split("\n");
3876 foreach (QString line, lines) {
3877 newMacWidget(line);
3878 editWidgets.last()->select();
3879 }
3880 }
3881 }
3882 markHistory();
3883 }
3884
3885 void WidgetLayout::setEditEnabled(bool enabled)
3886 {
3887 setEditMode(enabled);
3888 m_enableEdit = enabled;
3889 }
3890
3891 void WidgetLayout::setEditMode(bool active)
3892 {
3893 if (!m_enableEdit) {
3894 return;
3895 }
3896 if (active) {
3897 widgetsMutex.lock();
3898 foreach (FrameWidget *widget, editWidgets) {
3899 delete widget;
3900 }
3901 editWidgets.clear();
3902 foreach (QuteWidget * widget, m_widgets) {
3903 createEditFrame(widget);
3904 setWidgetToolTip(widget,m_tooltips);
3905 }
3906 widgetsMutex.unlock();
3907 }
3908 else {
3909 foreach (QFrame* frame, editWidgets) {
3910 delete(frame);
3911 }
3912 editWidgets.clear();
3913 }
3914 m_editMode = active;
3915 }
3916
3917 void WidgetLayout::createEditFrame(QuteWidget* widget)
3918 {
3919 // This function should be locked from outside always
3920 FrameWidget * frame = new FrameWidget(this);
3921 QPalette palette = QPalette(QColor(Qt::red),QColor(Qt::red));
3922 palette.setColor(QPalette::WindowText, QColor(Qt::red));
3923 frame->setWidget(widget);
3924 frame->setPalette(palette);
3925 frame->setFocusProxy(this);
3926 frame->setGeometry(widget->x(), widget->y(), widget->width(), widget->height());
3927 frame->setFrameShape(QFrame::Box);
3928 // frame->setMouseTracking(false); //Only track mouse when buttons are pressed
3929 frame->show();
3930 editWidgets.append(frame);
3931 connect(frame, SIGNAL(popUpMenu(QPoint)), widget, SLOT(popUpMenu(QPoint)));
3932 connect(frame, SIGNAL(deselectAllSignal()), this, SLOT(deselectAll()));
3933 connect(frame, SIGNAL(moved( QPair<int, int> )),
3934 this, SLOT(widgetMoved( QPair<int, int> )));
3935 connect(frame, SIGNAL(resized( QPair<int, int> )),
3936 this, SLOT(widgetResized( QPair<int, int> )));
3937 connect(frame, SIGNAL(mouseReleased()), this, SLOT(markHistory()));
3938 connect(frame, SIGNAL(editWidget()), widget, SLOT(openProperties()));
3939 connect(frame, SIGNAL(widgetSelected(QuteWidget*)),
3940 this, SLOT(widgetSelected(QuteWidget*)));
3941 connect(frame, SIGNAL(widgetUnselected(QuteWidget*)),
3942 this, SLOT(widgetUnselected(QuteWidget*)));
3943
3944 }
3945
3946 void WidgetLayout::markHistory()
3947 {
3948 QString text = m_xmlFormat ? getWidgetsText() : getMacWidgetsText();
3949 if (m_history.isEmpty()) {
3950 m_history << "";
3951 m_historyIndex = 0;
3952 }
3953 if (m_history[m_historyIndex] != text) {
3954 if (! m_history[m_historyIndex].isEmpty())
3955 m_historyIndex++;
3956 if (m_historyIndex >= QCS_MAX_UNDO) {
3957 m_history.pop_front();
3958 (m_historyIndex)--;
3959 }
3960 if (m_history.size() != m_historyIndex + 1)
3961 m_history.resize(m_historyIndex + 1);
3962 m_history[m_historyIndex] = text;
3963 }
3964 }
3965
3966 void WidgetLayout::deleteWidget(QuteWidget *widget)
3967 {
3968 widgetsMutex.lock();
3969 int index = m_widgets.indexOf(widget);
3970 m_activeWidgets = index; // Allow all widgets before this one to be active
3971 widget->close();
3972 m_widgets.remove(index);
3973 if (!editWidgets.isEmpty()) {
3974 delete(editWidgets[index]);
3975 editWidgets.remove(index);
3976 }
3977 index = consoleWidgets.indexOf(dynamic_cast<QuteConsole *>(widget));
3978 if (index >= 0) {
3979 consoleWidgets.remove(index);
3980 }
3981 index = graphWidgets.indexOf(dynamic_cast<QuteGraph *>(widget));
3982 if (index >= 0)
3983 graphWidgets.remove(index);
3984 index = scopeWidgets.indexOf(dynamic_cast<QuteScope *>(widget));
3985 if (index >= 0)
3986 scopeWidgets.remove(index);
3987 m_activeWidgets = m_widgets.size(); // Allow all widgets again
3988 widgetsMutex.unlock();
3989 widgetChanged(widget);
3990 }
3991
3992 void WidgetLayout::newValue(QPair<QString, double> channelValue)
3993 {
3994 if (channelValue.first == "_SetPreset") {
3995 loadPreset((int)channelValue.second);
3996 }
3997 if (channelValue.first == "_SetPresetIndex") {
3998 loadPresetFromIndex((int)channelValue.second);
3999 }
4000 QString path, channelName = channelValue.first;
4001 auto idx = channelName.indexOf("/");
4002 if (idx >= 0) {
4003 path = channelName.mid(idx+1);
4004 channelName = channelName.left(idx);
4005 }
4006 widgetsMutex.lock();
4007 if (!channelName.isEmpty()) {
4008 // Pass the value on to the other widgets
4009 for (int i = 0; i < m_widgets.size(); i++){
4010 if (m_widgets[i]->getChannelName() == channelName) {
4011 if (path.isEmpty()) {
4012 m_widgets[i]->setValue(channelValue.second);
4013 }
4014 else
4015 m_widgets[i]->widgetMessage(path,channelValue.second);
4016 }
4017 if (m_widgets[i]->getChannel2Name() == channelValue.first) {
4018 m_widgets[i]->setValue2(channelValue.second);
4019 }
4020 }
4021 }
4022 widgetsMutex.unlock();
4023 // Now store the value in the changes buffer to read from chnget
4024 if (!channelValue.first.isEmpty()) {
4025 valueMutex.lock();
4026 if(newValues.contains(channelValue.first))
4027 newValues[channelValue.first] = channelValue.second;
4028 else
4029 newValues.insert(channelValue.first, channelValue.second);
4030 valueMutex.unlock();
4031 }
4032 }
4033
4034 //FIXME there's no need to go through here coming from the widgets...
4035 // at least not to set the widget's value...
4036 void WidgetLayout::newValue(QPair<QString, QString> channelValue)
4037 {
4038 QString channelName = channelValue.first;
4039 if (channelValue.first.contains("/")) {
4040 channelName = channelValue.first.left(channelValue.first.indexOf("/"));
4041 }
4042 QString path = channelValue.first.mid(channelValue.first.indexOf("/") + 1);
4043 // Send value to a widget if channel matches
4044 widgetsMutex.lock();
4045 if (!channelName.isEmpty()) {
4046 for (int i = 0; i < m_widgets.size(); i++){
4047 if (m_widgets[i]->getChannelName() != channelName)
4048 continue;
4049 if (path == channelName)
4050 m_widgets[i]->setValue(channelValue.second);
4051 else
4052 m_widgets[i]->widgetMessage(path,channelValue.second);
4053 }
4054 }
4055 widgetsMutex.unlock();
4056 // Now store the value in the changes buffer to read from chnget
4057 if (!channelValue.first.isEmpty()) {
4058 stringValueMutex.lock();
4059 if(newStringValues.contains(channelValue.first))
4060 newStringValues[channelValue.first] = channelValue.second;
4061 else
4062 newStringValues.insert(channelValue.first, channelValue.second);
4063 stringValueMutex.unlock();
4064 }
4065 }
4066
4067 void WidgetLayout::processNewValues()
4068 {
4069 // Apply values received
4070 // qDebug("WidgetPanel::processNewValues");
4071
4072 // if (closing != 0)
4073 // return;
4074 // QList<QString> channelNames;
4075 // valueMutex.lock();
4076 // channelNames = newValues.keys();
4077 // valueMutex.unlock();
4078 // widgetsMutex.lock();
4079 // foreach(QString name, channelNames) {
4080 // for (int i = 0; i < m_widgets.size(); i++){
4081 // if (m_widgets[i]->getChannelName() == name) {
4082 // m_widgets[i]->setValue(newValues.value(name));
4083 // }
4084 // if (m_widgets[i]->getChannel2Name() == name) {
4085 // m_widgets[i]->setValue2(newValues.value(name));
4086 // }
4087 // if (m_trackMouse) {
4088 // QString ch1name = m_widgets[i]->getChannelName();
4089 // if (ch1name == "_MouseX") {
4090 // m_widgets[i]->setValue(getMouseX());
4091 // }
4092 // else if (ch1name == "_MouseY") {
4093 // m_widgets[i]->setValue(getMouseY());
4094 // }
4095 // else if (ch1name == "_MouseRelX") {
4096 // m_widgets[i]->setValue(getMouseRelX());
4097 // }
4098 // else if (ch1name == "_MouseRelY") {
4099 // m_widgets[i]->setValue(getMouseRelY());
4100 // }
4101 // else if (ch1name == "_MouseBut1") {
4102 // m_widgets[i]->setValue(getMouseBut1());
4103 // }
4104 // else if (ch1name == "_MouseBut2") {
4105 // m_widgets[i]->setValue(getMouseBut2());
4106 // }
4107 // QString ch2name = m_widgets[i]->getChannel2Name();
4108 // if (ch2name == "_MouseX") {
4109 // m_widgets[i]->setValue2(getMouseX());
4110 // }
4111 // else if (ch2name == "_MouseY") {
4112 // m_widgets[i]->setValue2(getMouseY());
4113 // }
4114 // else if (ch2name == "_MouseRelX") {
4115 // m_widgets[i]->setValue2(getMouseRelX());
4116 // }
4117 // else if (ch2name == "_MouseRelY") {
4118 // m_widgets[i]->setValue2(getMouseRelY());
4119 // }
4120 // else if (ch2name == "_MouseBut1") {
4121 // m_widgets[i]->setValue2(getMouseBut1());
4122 // }
4123 // else if (ch2name == "_MouseBut2") {
4124 // m_widgets[i]->setValue2(getMouseBut2());
4125 // }
4126 // }
4127 // }
4128 // }
4129 // widgetsMutex.unlock();
4130 // valueMutex.lock();
4131 // newValues.clear();
4132 // valueMutex.unlock();
4133 // stringValueMutex.lock();
4134 // channelNames = newStringValues.keys();
4135 // // Now set string values
4136 // stringValueMutex.unlock();
4137 // widgetsMutex.lock();
4138 // foreach(QString name, channelNames) {
4139 // for (int i = 0; i < m_widgets.size(); i++){
4140 // if (m_widgets[i]->getChannelName() == name) {
4141 // m_widgets[i]->setValue(newStringValues.value(name));
4142 // }
4143 // }
4144 // }
4145 // widgetsMutex.unlock();
4146 // stringValueMutex.lock();
4147 // newStringValues.clear();
4148 // stringValueMutex.unlock();
4149 }
4150
4151 void WidgetLayout::queueEvent(QString eventLine)
4152 {
4153 emit queueEventSignal(eventLine);
4154 }
4155
4156 void WidgetLayout::duplicate()
4157 {
4158 if(!m_editMode)
4159 return;
4160 widgetsMutex.lock();
4161 QList<int> selectedWidgets;
4162 for (int i = 0; i < editWidgets.size() ; i++) {
4163 if (editWidgets[i]->isSelected()) {
4164 selectedWidgets << i;
4165 }
4166 }
4167 for (int i = 0; i < selectedWidgets.size() ; i++) {
4168 int index = selectedWidgets[i];
4169 editWidgets[index]->deselect();
4170 if (m_xmlFormat) {
4171 QDomDocument doc;
4172 doc.setContent(m_widgets[index]->getWidgetXmlText());
4173 widgetsMutex.unlock();
4174 newXmlWidget(doc.firstChildElement("bsbObject"), true, true);
4175 widgetsMutex.lock();
4176 }
4177 else {
4178 widgetsMutex.unlock();
4179 newMacWidget(m_widgets[index]->getWidgetLine(), true);
4180 widgetsMutex.lock();
4181 }
4182 // editWidgets.last()->select();
4183 }
4184 widgetsMutex.unlock();
4185 markHistory();
4186 setModified(true);
4187 }
4188
4189 void WidgetLayout::deleteSelected()
4190 {
4191 widgetsMutex.lock();
4192 for (int i = editWidgets.size() - 1; i >= 0 ; i--) {
4193 if (!editWidgets[i]->isSelected())
4194 continue;
4195 widgetsMutex.unlock();
4196 deleteWidget(m_widgets[i]);
4197 widgetsMutex.lock();
4198 }
4199 widgetsMutex.unlock();
4200 markHistory();
4201 setModified(true);
4202 }
4203
4204 void WidgetLayout::moveSelected(int horiz, int vert, int grid) {
4205 widgetsMutex.lock();
4206 for (int i = editWidgets.size() - 1; i >= 0 ; i--) {
4207 if(!editWidgets[i]->isSelected())
4208 continue;
4209 widgetsMutex.unlock();
4210 QPoint pos = m_widgets[i]->pos();
4211 int x = pos.x() + horiz;
4212 int y = pos.y() + vert;
4213 if(grid > 1) {
4214 double xq = round(x / (double)grid) * grid;
4215 x = (int)xq;
4216 double yq = round(y / (double)grid) * grid;
4217 y = (int)yq;
4218
4219 }
4220 m_widgets[i]->move(x, y);
4221 editWidgets[i]->move(x, y);
4222 widgetsMutex.lock();
4223 }
4224 widgetsMutex.unlock();
4225 markHistory();
4226 setModified(true);
4227 }
4228
4229 void WidgetLayout::undo()
4230 {
4231 if(m_historyIndex <= 0)
4232 return;
4233 m_historyIndex--;
4234 if (m_xmlFormat)
4235 loadXmlWidgets(m_history[m_historyIndex]);
4236 else
4237 loadMacWidgets(m_history[m_historyIndex]);
4238 }
4239
4240 void WidgetLayout::reloadWidgets() {
4241 if(m_historyIndex <= 0)
4242 return;
4243 loadXmlWidgets(m_history[m_historyIndex]);
4244 }
4245
4246 void WidgetLayout::redo()
4247 {
4248 if (m_historyIndex >= m_history.size() - 1)
4249 return;
4250 m_historyIndex++;
4251 if (m_xmlFormat)
4252 loadXmlWidgets(m_history[m_historyIndex]);
4253 else
4254 loadMacWidgets(m_history[m_historyIndex]);
4255 }
4256
4257
4258 void WidgetLayout::updateData()
4259 {
4260 if (closing == 1) {
4261 closing = 0;
4262 return;
4263 }
4264
4265 if(!m_updating)
4266 return;
4267
4268 refreshWidgets();
4269 int const refresh_rate = m_updateRate;
4270 int const msec = 1000 / refresh_rate;
4271 if (!layoutMutex.tryLock(1)) {
4272 updateTimer.singleShot(msec, this, SLOT(updateData()));
4273 return;
4274 }
4275 while (!newCurveBuffer.isEmpty()) {
4276 Curve * curve = newCurveBuffer.takeFirst();
4277 newCurve(curve); // Register new curve
4278 }
4279 // Check for graph updates after creating new curves
4280 while (curveUpdateBufferCount > 0) {
4281 WINDAT * curveData = &curveUpdateBuffer[curveUpdateBufferCount--];
4282 Curve *curve = (Curve *) getCurveById(curveData->windid);
4283 if (curve != nullptr && curveData != nullptr) {
4284 curve->set_size(curveData->npts); // number of points
4285 curve->set_data(curveData->fdata);
4286 curve->set_caption(curveData->caption);
4287 curve->set_max(curveData->max);
4288 curve->set_min(curveData->min);
4289 curve->set_absmax(curveData->absmax);
4290 // Y axis scaling factor
4291 // curve->set_y_scale(windat->y_scale);
4292 setCurveData(curve);
4293 }
4294 // delete curveData; //FIXME don't do this deleting here...
4295 }
4296 for (int i = 0; i < scopeWidgets.size(); i++) {
4297 scopeWidgets[i]->updateData();
4298 }
4299 layoutMutex.unlock();
4300 closing = 0;
4301 updateTimer.singleShot(msec, this, SLOT(updateData()));
4302 }
4303
4304 void WidgetLayout::widgetSelected(QuteWidget *widget)
4305 {
4306 emit widgetSelectedSignal(widget);
4307 }
4308
4309 void WidgetLayout::widgetUnselected(QuteWidget *widget)
4310 {
4311 emit widgetUnselectedSignal(widget);
4312 }