Debian Codegraph @

src/qutewidget.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2008, 2009 Andres Cabrera
3 [email protected]
4
5 This file is part of CsoundQt.
6
7 CsoundQt is free software; you can redistribute it
8 and/or modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 CsoundQt is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with Csound; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA
21 */
22
23 #include "qutewidget.h"
24 #include "widgetlayout.h"
25
26 QuteWidget::QuteWidget(QWidget *parent):
27 QWidget(parent), dialog(NULL)
28 {
29 propertiesAct = new QAction(tr("&Properties"), this);
30 propertiesAct->setStatusTip(tr("Open widget properties"));
31 connect(propertiesAct, SIGNAL(triggered()), this, SLOT(openProperties()));
32
33 addChn_kAct = new QAction(tr("Add chn_k to csd"),this);
34 addChn_kAct->setStatusTip(tr("Add chn_k definitionto ;;channels section in editor"));
35 connect(addChn_kAct, SIGNAL(triggered()), this, SLOT(addChn_k()));
36
37 m_value = 0.0;
38 m_value2 = 0.0;
39 m_stringValue = "";
40 m_valueChanged = false;
41 m_value2Changed = false;
42 m_locked = false;
43 m_description = "";
44 // used by all widgets which need access to the api (TableDisplay)
45 // TODO: adapt Scope and Graph to use this instead of implementing their own
46 m_csoundUserData = nullptr;
47
48 this->setMinimumSize(2,2);
49 this->setMouseTracking(true); // Necessary to pass mouse tracking to widget panel for _MouseX channels
50
51 setProperty("QCS_x", 0);
52 setProperty("QCS_y", 0);
53 //setProperty("width", 20);
54 //setProperty("height", 20);
55 setProperty("QCS_width", 20);
56 setProperty("QCS_height", 20);
57 setProperty("QCS_uuid", QUuid::createUuid().toString());
58 setProperty("QCS_visible", true);
59 setProperty("QCS_midichan", 0);
60 setProperty("QCS_midicc", -3);
61 setProperty("QCS_description", "");
62 }
63
64 QuteWidget::~QuteWidget()
65 {
66 }
67
68 void QuteWidget::setWidgetGeometry(int x, int y, int w, int h)
69 {
70 // qDebug() << "QuteWidget::setWidgetGeometry" <<x<<y<<w<<h;
71 Q_ASSERT(w > 0 && h > 0);
72 // Q_ASSERT(x > 0 && y > 0 and w > 0 && h > 0);
73 this->setGeometry(QRect(x,y,w,h));
74 m_widget->blockSignals(true);
75 m_widget->setGeometry(QRect(0,0,w,h));
76 m_widget->blockSignals(false);
77 // this->markChanged(); // It's better not to have geometry changes trigger markChanged as geometry changes can occur for various reasons (e.g. when calling applyInternalProperties)
78 }
79
80 void QuteWidget::setValue(double value)
81 {
82 #ifdef USE_WIDGET_MUTEX
83 widgetLock.lockForWrite();
84 #endif
85 m_value = value;
86 m_valueChanged = true;
87 #ifdef USE_WIDGET_MUTEX
88 widgetLock.unlock();
89 #endif
90 }
91
92 void QuteWidget::setValue2(double value)
93 {
94 #ifdef USE_WIDGET_MUTEX
95 widgetLock.lockForWrite();
96 #endif
97 m_value2 = value;
98 m_value2Changed = true;
99 #ifdef USE_WIDGET_MUTEX
100 widgetLock.unlock();
101 #endif
102 }
103
104 void QuteWidget::setValue(QString value)
105 {
106 #ifdef USE_WIDGET_MUTEX
107 widgetLock.lockForWrite();
108 #endif
109 m_stringValue = value;
110 m_valueChanged = true;
111 #ifdef USE_WIDGET_MUTEX
112 widgetLock.unlock();
113 #endif
114 }
115
116 void QuteWidget::setMidiValue(int /* value */)
117 {
118 qDebug() << "Not available for this widget." << this;
119 }
120
121 void QuteWidget::setMidiValue2(int /* value */)
122 {
123 qDebug() << "Not available for this widget." << this;
124 }
125
126 void QuteWidget::widgetMessage(QString path, QString text)
127 {
128 qDebug() << text;
129 if (property(path.toLocal8Bit()).isValid()) {
130 setProperty(path.toLocal8Bit(), text);
131 // applyInternalProperties();
132 }
133 }
134
135 void QuteWidget::widgetMessage(QString path, double value)
136 {
137 qDebug() << value;
138 if (property(path.toLocal8Bit()).isValid()) {
139 setProperty(path.toLocal8Bit(), value);
140 // applyInternalProperties();
141 }
142 }
143
144 QString QuteWidget::getChannelName()
145 {
146 return m_channel;
147 }
148
149 QString QuteWidget::getChannel2Name()
150 {
151 // widgetLock.lockForRead();
152 QString name = m_channel2;
153 // widgetLock.unlock();
154 return name;
155 }
156
157 QString QuteWidget::getCabbageLine()
158 {
159 //Widgets return empty strings when not supported
160 return QString("");
161 }
162
163 void QuteWidget::createXmlWriter(QXmlStreamWriter &s)
164 {
165 s.setAutoFormatting(true);
166 s.writeStartElement("bsbObject");
167 s.writeAttribute("type", getWidgetType());
168
169 s.writeAttribute("version", QCS_CURRENT_XML_VERSION); // Only for compatibility with blue (absolute values)
170
171 s.writeTextElement("objectName", m_channel);
172 s.writeTextElement("x", QString::number(x()));
173 s.writeTextElement("y", QString::number(y()));
174 s.writeTextElement("width", QString::number(width()));
175 s.writeTextElement("height", QString::number(height()));
176 s.writeTextElement("uuid", property("QCS_uuid").toString());
177 s.writeTextElement("visible", property("QCS_visible").toBool() ? "true":"false");
178 s.writeTextElement("midichan", QString::number(property("QCS_midichan").toInt()));
179 s.writeTextElement("midicc", QString::number(property("QCS_midicc").toInt()));
180 s.writeTextElement("description", m_description);
181 }
182
183 double QuteWidget::getValue()
184 {
185 // When reimplementing this, remember to use the widget mutex to protect data,
186 // as this can be called from many different places
187 #ifdef USE_WIDGET_MUTEX
188 widgetLock.lockForRead();
189 #endif
190 double value = m_value;
191 #ifdef USE_WIDGET_MUTEX
192 widgetLock.unlock();
193 #endif
194 return value;
195 }
196
197 double QuteWidget::getValue2()
198 {
199 // When reimplementing this, remember to use the widget mutex to protect data, as this can be called from many different places
200 #ifdef USE_WIDGET_MUTEX
201 widgetLock.lockForRead();
202 #endif
203 double value = m_value2;
204 #ifdef USE_WIDGET_MUTEX
205 widgetLock.unlock();
206 #endif
207 return value;
208 }
209
210 QString QuteWidget::getStringValue()
211 {
212 // When reimplementing this, remember to use the widget mutex to protect data, as this can be called from many different places
213 #ifdef USE_WIDGET_MUTEX
214 widgetLock.lockForRead();
215 #endif
216 QString value = m_stringValue;
217 #ifdef USE_WIDGET_MUTEX
218 widgetLock.unlock();
219 #endif
220 return value;
221 }
222
223 QString QuteWidget::getDescription()
224 {
225 return m_description;
226 }
227
228 QString QuteWidget::getCsladspaLine()
229 {
230 //Widgets return empty strings when not supported
231 return QString("");
232 }
233
234 QString QuteWidget::getQml()
235 {
236 //Widgets return empty strings when not supported
237 return QString();
238 }
239
240 QString QuteWidget::getUuid()
241 {
242 if (property("QCS_uuid").isValid())
243 return property("QCS_uuid").toString();
244 else
245 return QString();
246 }
247
248 void QuteWidget::applyInternalProperties()
249 {
250 // qDebug() << "QuteWidget::applyInternalProperties()";
251 #ifdef USE_WIDGET_MUTEX
252 widgetLock.lockForRead();
253 #endif
254 int x,y,width, height;
255 x = property("QCS_x").toInt();
256 y = property("QCS_y").toInt();
257 width = property("QCS_width").toInt();
258 height = property("QCS_height").toInt();
259 setWidgetGeometry(x,y,width, height);
260 m_channel = property("QCS_objectName").toString();
261 m_channel2 = property("QCS_objectName2").toString();
262 m_midicc = property("QCS_midicc").toInt();
263 m_midichan = property("QCS_midichan").toInt();
264 setVisible(property("QCS_visible").toBool());
265 m_valueChanged = true;
266 m_description = property("QCS_description").toString();
267 #ifdef USE_WIDGET_MUTEX
268 widgetLock.unlock();
269 #endif
270 }
271
272 void QuteWidget::markChanged()
273 {
274 emit widgetChanged(this);
275 }
276
277 void QuteWidget::canFocus(bool can)
278 {
279 if (can) {
280 this->setFocusPolicy(Qt::StrongFocus);
281 m_widget->setFocusPolicy(Qt::StrongFocus);
282 }
283 else {
284 this->setFocusPolicy(Qt::NoFocus);
285 m_widget->setFocusPolicy(Qt::NoFocus);
286 }
287 }
288
289 void QuteWidget::updateDialogWindow(int cc, int channel) // to update values from midi Learn window to widget properties' dialog
290 {
291
292 if (!dialog) {
293 qDebug() << "Dialog window not careated";
294 return;
295 }
296
297 if (dialog->isVisible() && acceptsMidi()) {
298 midiccSpinBox->setValue(cc);
299 midichanSpinBox->setValue(channel);
300 }
301 }
302
303 void QuteWidget::contextMenuEvent(QContextMenuEvent *event)
304 {
305 popUpMenu(event->globalPos());
306 }
307
308 void QuteWidget::popUpMenu(QPoint pos)
309 {
310 if (m_locked) {
311 return;
312 }
313 QMenu menu(this);
314 menu.addAction(propertiesAct);
315 menu.addSeparator();
316
317 if (!m_channel.isEmpty() || !m_channel2.isEmpty()) {
318 menu.addAction(addChn_kAct);
319 menu.addSeparator();
320 }
321
322 if (acceptsMidi()) {
323 menu.addAction(tr("MIDI learn"), this, SLOT(openMidiDialog()) );
324 menu.addSeparator();
325 }
326
327 QList<QAction *> actionList = getParentActionList();
328
329 for (int i = 0; i < actionList.size(); i++) {
330 auto action = actionList[i];
331 if(action == nullptr)
332 menu.addSeparator();
333 else
334 menu.addAction(action);
335 }
336
337 menu.addSeparator();
338
339 WidgetLayout *layout = static_cast<WidgetLayout *>(this->parentWidget());
340 layout->setCurrentPosition(layout->mapFromGlobal(pos));
341
342 menu.addAction(layout->storePresetAct);
343 menu.addAction(layout->newPresetAct);
344 menu.addAction(layout->recallPresetAct);
345
346 menu.addSeparator();
347
348 QMenu presetMenu(tr("Presets"), &menu);
349
350 QList<int> list = layout->getPresetNums();
351 for (int i = 0; i < list.size(); i++) {
352 QAction *act = new QAction(layout->getPresetName(list[i]), &menu);
353 act->setData(i);
354 connect(act, SIGNAL(triggered()), layout, SLOT(loadPresetFromAction()));
355 presetMenu.addAction(act);
356 }
357
358 /*
359 menu.addSeparator();
360
361 QMenu createMenu(tr("Create New", "Menu name in widget right-click menu"),&menu);
362 createMenu.addAction(layout->createSliderAct);
363 createMenu.addAction(layout->createLabelAct);
364 createMenu.addAction(layout->createDisplayAct);
365 createMenu.addAction(layout->createScrollNumberAct);
366 createMenu.addAction(layout->createLineEditAct);
367 createMenu.addAction(layout->createSpinBoxAct);
368 createMenu.addAction(layout->createButtonAct);
369 createMenu.addAction(layout->createKnobAct);
370 createMenu.addAction(layout->createCheckBoxAct);
371 createMenu.addAction(layout->createMenuAct);
372 createMenu.addAction(layout->createMeterAct);
373 createMenu.addAction(layout->createConsoleAct);
374 createMenu.addAction(layout->createGraphAct);
375 createMenu.addAction(layout->createScopeAct);
376
377 menu.addMenu(&createMenu);
378 */
379 menu.exec(pos);
380 }
381
382 void QuteWidget::openProperties()
383 {
384 createPropertiesDialog();
385
386 connect(acceptButton, SIGNAL(released()), dialog, SLOT(accept()));
387 connect(dialog, SIGNAL(accepted()), this, SLOT(apply()));
388 connect(applyButton, SIGNAL(released()), this, SLOT(apply()));
389 connect(cancelButton, SIGNAL(released()), dialog, SLOT(close()));
390 if (acceptsMidi()) {
391 connect(midiLearnButton, SIGNAL(released()),this, SLOT(openMidiDialog()));
392 }
393 dialog->exec();
394 if (dialog->result() != QDialog::Accepted) {
395 qDebug() << "QuteWidget::openProperties() dialog not accepted";
396 }
397 // dialog->deleteLater();
398 parentWidget()->setFocus(Qt::OtherFocusReason); // For some reason focus is grabbed away from the layout, but this doesn't solve the problem...
399 }
400
401
402 void QuteWidget::deleteWidget()
403 {
404 // qDebug("QuteWidget::deleteWidget()");
405 emit(deleteThisWidget(this));
406 }
407
408 void QuteWidget::openMidiDialog()
409 {
410 //createPropertiesDialog(); <- tryout for midi learn from context menu
411 emit showMidiLearn(this);
412 }
413
414 void QuteWidget::addChn_k()
415 {
416 //qDebug()<<Q_FUNC_INFO << m_channel << m_channel2;
417 if (!m_channel.isEmpty()) {
418 emit addChn_kSignal(m_channel);
419 }
420 if (!m_channel2.isEmpty()) {
421 emit addChn_kSignal(m_channel2);
422 }
423 }
424
425 void QuteWidget::createPropertiesDialog()
426 {
427 // qDebug() << "QuteWidget::createPropertiesDialog()---Dynamic Properties:\n"
428 // << dynamicPropertyNames ();
429 int footerRow = 20;
430 dialog = new QDialog(this);
431 dialog->resize(360, 300);
432 // dialog->setModal(true);
433 layout = new QGridLayout(dialog);
434 QLabel *label;
435
436 label = new QLabel("X =", dialog);
437 layout->addWidget(label, 0, 0, Qt::AlignRight|Qt::AlignVCenter);
438
439 xSpinBox = new QSpinBox(dialog);
440 xSpinBox->unsetLocale();
441 xSpinBox->setMaximum(9999);
442 layout->addWidget(xSpinBox, 0, 1, Qt::AlignLeft|Qt::AlignVCenter);
443
444 label = new QLabel("Y =", dialog);
445 layout->addWidget(label, 0, 2, Qt::AlignRight|Qt::AlignVCenter);
446
447 ySpinBox = new QSpinBox(dialog);
448 ySpinBox->unsetLocale();
449 ySpinBox->setMaximum(9999);
450 layout->addWidget(ySpinBox, 0, 3, Qt::AlignLeft|Qt::AlignVCenter);
451
452 label = new QLabel(tr("Width ="), dialog);
453 layout->addWidget(label, 1, 0, Qt::AlignRight|Qt::AlignVCenter);
454
455 wSpinBox = new QSpinBox(dialog);
456 wSpinBox->unsetLocale();
457 wSpinBox->setMaximum(9999);
458 layout->addWidget(wSpinBox, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
459
460 label = new QLabel(tr("Height ="), dialog);
461 layout->addWidget(label, 1, 2, Qt::AlignRight|Qt::AlignVCenter);
462
463 hSpinBox = new QSpinBox(dialog);
464 hSpinBox->unsetLocale();
465 hSpinBox->setMaximum(9999);
466 layout->addWidget(hSpinBox, 1, 3, Qt::AlignLeft|Qt::AlignVCenter);
467
468 channelLabel = new QLabel(tr("Channel ="), dialog);
469 layout->addWidget(channelLabel, 3, 0, Qt::AlignRight|Qt::AlignVCenter);
470
471 nameLineEdit = new QLineEdit(dialog);
472 nameLineEdit->setFocus(Qt::OtherFocusReason);
473 nameLineEdit->selectAll();
474 layout->addWidget(nameLineEdit, 3, 1, 1, 3, Qt::AlignLeft|Qt::AlignVCenter);
475
476 label = new QLabel(tr("Description ="), dialog);
477 layout->addWidget(label, footerRow-2, 0, Qt::AlignRight|Qt::AlignVCenter);
478
479 descriptionLineEdit = new QLineEdit(dialog);
480 descriptionLineEdit->setMinimumWidth(300);
481 layout->addWidget(descriptionLineEdit, footerRow-2, 1, 1, 4, Qt::AlignLeft|Qt::AlignVCenter);
482
483
484 if (acceptsMidi()) { // only when MIDI-enabled widgets
485 int midiRow = footerRow - 1;
486 label = new QLabel("MIDI CC =", dialog);
487 layout->addWidget(label, midiRow, 0, Qt::AlignRight|Qt::AlignVCenter);
488
489 midiccSpinBox = new QSpinBox(dialog);
490 midiccSpinBox->unsetLocale();
491 midiccSpinBox->setRange(0,119);
492 layout->addWidget(midiccSpinBox, midiRow, 1, Qt::AlignLeft|Qt::AlignVCenter);
493
494 label = new QLabel("MIDI Channel =", dialog);
495 layout->addWidget(label, midiRow, 2, Qt::AlignRight|Qt::AlignVCenter);
496
497 midichanSpinBox = new QSpinBox(dialog);
498 midichanSpinBox->unsetLocale();
499 midichanSpinBox->setRange(0,127);
500 layout->addWidget(midichanSpinBox, midiRow,3, Qt::AlignLeft|Qt::AlignVCenter);
501
502 midiLearnButton = new QPushButton(tr("MIDI learn"));
503 layout->addWidget(midiLearnButton, midiRow, 4, Qt::AlignLeft|Qt::AlignVCenter);
504 }
505 acceptButton = new QPushButton(tr("Ok"));
506 acceptButton->setDefault(true);
507 layout->addWidget(acceptButton, footerRow, 3, Qt::AlignCenter|Qt::AlignVCenter);
508
509 applyButton = new QPushButton(tr("Apply"));
510 layout->addWidget(applyButton, footerRow, 1, Qt::AlignCenter|Qt::AlignVCenter);
511 cancelButton = new QPushButton(tr("Cancel"));
512 layout->addWidget(cancelButton, footerRow, 2, Qt::AlignCenter|Qt::AlignVCenter);
513
514 #ifdef USE_WIDGET_MUTEX
515 widgetLock.lockForRead();
516 #endif
517 xSpinBox->setValue(this->x());
518 ySpinBox->setValue(this->y());
519 wSpinBox->setValue(this->width());
520 hSpinBox->setValue(this->height());
521 nameLineEdit->setText(getChannelName());
522 descriptionLineEdit->setText(getDescription());
523 if (acceptsMidi()) {
524 midiccSpinBox->setValue(this->m_midicc);
525 midichanSpinBox->setValue(this->m_midichan);
526 }
527 #ifdef USE_WIDGET_MUTEX
528 widgetLock.unlock();
529 #endif
530 }
531
532 void QuteWidget::applyProperties()
533 {
534 // qDebug();
535 #ifdef USE_WIDGET_MUTEX
536 widgetLock.lockForRead();
537 #endif
538 setProperty("QCS_objectName", nameLineEdit->text());
539 setProperty("QCS_x", xSpinBox->value());
540 setProperty("QCS_y",ySpinBox->value());
541 setProperty("QCS_width", wSpinBox->value());
542 setProperty("QCS_height", hSpinBox->value());
543 if (acceptsMidi()) {
544 setProperty("QCS_midicc", midiccSpinBox->value());
545 setProperty("QCS_midichan", midichanSpinBox->value());
546 }
547 setProperty("QCS_description", descriptionLineEdit->text());
548 #ifdef USE_WIDGET_MUTEX
549 widgetLock.unlock();
550 #endif
551 applyInternalProperties();
552 // setChannelName(nameLineEdit->text());
553 // setWidgetGeometry(xSpinBox->value(), ySpinBox->value(), wSpinBox->value(), hSpinBox->value());
554
555 // this->setMouseTracking(true); // Necessary to pass mouse tracking to widget panel for _MouseX channels
556 emit(widgetChanged(this));
557 emit propertiesAccepted();
558 parentWidget()->setFocus(Qt::PopupFocusReason); // For some reason focus is grabbed away from the layout
559 m_valueChanged = true;
560 }
561
562 QList<QAction *> QuteWidget::getParentActionList()
563 {
564 QList<QAction *> actionList;
565 // A bit of a kludge... Must get the Widget Panel, which is the parent to the widget which
566 // holds the actual QuteWidgets
567 WidgetLayout *layout = static_cast<WidgetLayout *>(this->parentWidget());
568 actionList.append(layout->alignLeftAct);
569 actionList.append(layout->alignRightAct);
570 actionList.append(layout->alignTopAct);
571 actionList.append(layout->alignBottomAct);
572 actionList.append(nullptr);
573 actionList.append(layout->alignCenterHorizontalAct);
574 actionList.append(layout->alignCenterVerticalAct);
575 actionList.append(nullptr);
576 actionList.append(layout->sendToBackAct);
577 actionList.append(layout->sendToFrontAct);
578 actionList.append(nullptr);
579 actionList.append(layout->distributeHorizontalAct);
580 actionList.append(layout->distributeVerticalAct);
581 actionList.append(nullptr);
582 actionList.append(layout->copyAct);
583 actionList.append(layout->pasteAct);
584 actionList.append(layout->cutAct);
585 actionList.append(layout->deleteAct);
586 actionList.append(layout->duplicateAct);
587
588 // FIXME put edit action in menu
589 // actionList.append(layout->editAct);
590 return actionList;
591 }
592
593 void QuteWidget::apply()
594 {
595 applyProperties();
596 }