1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Sonic Visualiser
5     An audio file viewer and annotation editor.
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2006 Chris Cannam and QMUL.
8 
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14 */
15 
16 #include "PropertyBox.h"
17 #include "PluginParameterDialog.h"
18 
19 #include "base/PropertyContainer.h"
20 #include "base/PlayParameters.h"
21 #include "base/PlayParameterRepository.h"
22 #include "layer/Layer.h"
23 #include "base/UnitDatabase.h"
24 #include "base/RangeMapper.h"
25 
26 #include "AudioDial.h"
27 #include "LEDButton.h"
28 #include "IconLoader.h"
29 #include "LevelPanWidget.h"
30 #include "LevelPanToolButton.h"
31 #include "WidgetScale.h"
32 
33 #include "NotifyingCheckBox.h"
34 #include "NotifyingComboBox.h"
35 #include "NotifyingPushButton.h"
36 #include "NotifyingToolButton.h"
37 #include "ColourComboBox.h"
38 #include "ColourMapComboBox.h"
39 
40 #include <QGridLayout>
41 #include <QHBoxLayout>
42 #include <QVBoxLayout>
43 #include <QPushButton>
44 #include <QToolButton>
45 #include <QLabel>
46 #include <QFrame>
47 #include <QApplication>
48 #include <QColorDialog>
49 #include <QInputDialog>
50 #include <QDir>
51 
52 #include <cassert>
53 #include <iostream>
54 #include <cmath>
55 
56 //#define DEBUG_PROPERTY_BOX 1
57 
PropertyBox(PropertyContainer * container)58 PropertyBox::PropertyBox(PropertyContainer *container) :
59     m_container(container),
60     m_showButton(nullptr),
61     m_playButton(nullptr)
62 {
63 #ifdef DEBUG_PROPERTY_BOX
64     SVDEBUG << "PropertyBox[" << this << "(\"" <<
65         container->getPropertyContainerName() << "\" at " << container << ")]::PropertyBox" << endl;
66 #endif
67 
68     m_mainBox = new QVBoxLayout;
69     setLayout(m_mainBox);
70 
71 #ifdef Q_OS_MAC
72     QMargins mm = m_mainBox->contentsMargins();
73     QMargins mmhalf(mm.left()/2, mm.top()/3, mm.right()/2, mm.bottom()/3);
74     m_mainBox->setContentsMargins(mmhalf);
75 #endif
76 
77 //    m_nameWidget = new QLabel;
78 //    m_mainBox->addWidget(m_nameWidget);
79 //    m_nameWidget->setText(container->objectName());
80 
81     m_mainWidget = new QWidget;
82     m_mainBox->addWidget(m_mainWidget);
83     m_mainBox->insertStretch(2, 10);
84 
85     m_viewPlayFrame = nullptr;
86     populateViewPlayFrame();
87 
88     m_layout = new QGridLayout;
89     m_layout->setMargin(0);
90     m_layout->setHorizontalSpacing(2);
91     m_layout->setVerticalSpacing(1);
92     m_mainWidget->setLayout(m_layout);
93 
94     PropertyContainer::PropertyList properties = m_container->getProperties();
95 
96     blockSignals(true);
97 
98     size_t i;
99 
100     for (i = 0; i < properties.size(); ++i) {
101         updatePropertyEditor(properties[i]);
102     }
103 
104     blockSignals(false);
105 
106     m_layout->setRowStretch(m_layout->rowCount(), 10);
107 
108     connect(UnitDatabase::getInstance(), SIGNAL(unitDatabaseChanged()),
109             this, SLOT(unitDatabaseChanged()));
110 
111 #ifdef DEBUG_PROPERTY_BOX
112     SVDEBUG << "PropertyBox[" << this << "]::PropertyBox returning" << endl;
113 #endif
114 }
115 
~PropertyBox()116 PropertyBox::~PropertyBox()
117 {
118 #ifdef DEBUG_PROPERTY_BOX
119     SVDEBUG << "PropertyBox[" << this << "]::~PropertyBox" << endl;
120 #endif
121 }
122 
123 void
populateViewPlayFrame()124 PropertyBox::populateViewPlayFrame()
125 {
126 #ifdef DEBUG_PROPERTY_BOX
127     SVDEBUG << "PropertyBox[" << this << ":" << m_container << "]::populateViewPlayFrame" << endl;
128 #endif
129 
130     if (m_viewPlayFrame) {
131         delete m_viewPlayFrame;
132         m_viewPlayFrame = nullptr;
133     }
134 
135     if (!m_container) return;
136 
137     Layer *layer = dynamic_cast<Layer *>(m_container);
138     if (layer) {
139         disconnect(layer, SIGNAL(modelReplaced()),
140                    this, SLOT(populateViewPlayFrame()));
141         connect(layer, SIGNAL(modelReplaced()),
142                 this, SLOT(populateViewPlayFrame()));
143     }
144 
145     auto params = m_container->getPlayParameters();
146     if (!params && !layer) return;
147 
148     m_viewPlayFrame = new QFrame;
149     m_viewPlayFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
150     m_mainBox->addWidget(m_viewPlayFrame);
151 
152     QGridLayout *layout = new QGridLayout;
153     m_viewPlayFrame->setLayout(layout);
154 
155     layout->setMargin(layout->margin() / 2);
156 
157 #ifdef DEBUG_PROPERTY_BOX
158     SVDEBUG << "PropertyBox::populateViewPlayFrame: container " << m_container << " (name " << m_container->getPropertyContainerName() << ") params " << params << endl;
159 #endif
160 
161     QSize buttonSize = WidgetScale::scaleQSize(QSize(26, 26));
162     int col = 0;
163 
164     if (params) {
165 
166         m_playButton = new NotifyingToolButton;
167         m_playButton->setCheckable(true);
168         m_playButton->setIcon(IconLoader().load("speaker"));
169         m_playButton->setToolTip(tr("Click to toggle playback"));
170         m_playButton->setChecked(!params->isPlayMuted());
171         m_playButton->setFixedSize(buttonSize);
172         connect(m_playButton, SIGNAL(toggled(bool)),
173                 this, SLOT(playAudibleButtonChanged(bool)));
174         connect(m_playButton, SIGNAL(mouseEntered()),
175                 this, SLOT(mouseEnteredWidget()));
176         connect(m_playButton, SIGNAL(mouseLeft()),
177                 this, SLOT(mouseLeftWidget()));
178         connect(params.get(), SIGNAL(playAudibleChanged(bool)),
179                 this, SLOT(playAudibleChanged(bool)));
180 
181         LevelPanToolButton *levelPan = new LevelPanToolButton;
182         levelPan->setFixedSize(buttonSize);
183         levelPan->setImageSize((buttonSize.height() * 3) / 4);
184         layout->addWidget(levelPan, 0, col++, Qt::AlignCenter);
185         connect(levelPan, SIGNAL(levelChanged(float)),
186                 this, SLOT(playGainControlChanged(float)));
187         connect(levelPan, SIGNAL(panChanged(float)),
188                 this, SLOT(playPanControlChanged(float)));
189         connect(params.get(), SIGNAL(playGainChanged(float)),
190                 levelPan, SLOT(setLevel(float)));
191         connect(params.get(), SIGNAL(playPanChanged(float)),
192                 levelPan, SLOT(setPan(float)));
193         connect(levelPan, SIGNAL(mouseEntered()),
194                 this, SLOT(mouseEnteredWidget()));
195         connect(levelPan, SIGNAL(mouseLeft()),
196                 this, SLOT(mouseLeftWidget()));
197 
198         layout->addWidget(m_playButton, 0, col++, Qt::AlignCenter);
199 
200         if (params->getPlayClipId() != "") {
201             NotifyingToolButton *playParamButton = new NotifyingToolButton;
202             playParamButton->setObjectName("playParamButton");
203             playParamButton->setIcon(IconLoader().load("faders"));
204             playParamButton->setFixedSize(buttonSize);
205             layout->addWidget(playParamButton, 0, col++, Qt::AlignCenter);
206             connect(playParamButton, SIGNAL(clicked()),
207                     this, SLOT(editPlayParameters()));
208             connect(playParamButton, SIGNAL(mouseEntered()),
209                     this, SLOT(mouseEnteredWidget()));
210             connect(playParamButton, SIGNAL(mouseLeft()),
211                     this, SLOT(mouseLeftWidget()));
212         }
213     }
214 
215     layout->setColumnStretch(col++, 10);
216 
217     if (layer) {
218 
219         QLabel *showLabel = new QLabel(tr("Show"));
220         layout->addWidget(showLabel, 0, col++, Qt::AlignVCenter | Qt::AlignRight);
221 
222         m_showButton = new LEDButton(palette().highlight().color());
223         layout->addWidget(m_showButton, 0, col++, Qt::AlignVCenter | Qt::AlignLeft);
224         connect(m_showButton, SIGNAL(stateChanged(bool)),
225                 this, SIGNAL(showLayer(bool)));
226         connect(m_showButton, SIGNAL(mouseEntered()),
227                 this, SLOT(mouseEnteredWidget()));
228         connect(m_showButton, SIGNAL(mouseLeft()),
229                 this, SLOT(mouseLeftWidget()));
230     }
231 }
232 
233 void
updatePropertyEditor(PropertyContainer::PropertyName name,bool rangeChanged)234 PropertyBox::updatePropertyEditor(PropertyContainer::PropertyName name,
235                                   bool rangeChanged)
236 {
237     PropertyContainer::PropertyType type = m_container->getPropertyType(name);
238     int row = m_layout->rowCount();
239 
240     int min = 0, max = 0, value = 0, deflt = 0;
241     value = m_container->getPropertyRangeAndValue(name, &min, &max, &deflt);
242 
243     bool have = (m_propertyControllers.find(name) !=
244                  m_propertyControllers.end());
245 
246     QString groupName = m_container->getPropertyGroupName(name);
247     QString propertyLabel = m_container->getPropertyLabel(name);
248     QString iconName = m_container->getPropertyIconName(name);
249 
250 #ifdef DEBUG_PROPERTY_BOX
251     SVDEBUG << "PropertyBox[" << this
252               << "(\"" << m_container->getPropertyContainerName()
253               << "\")]";
254     SVDEBUG << "::updatePropertyEditor(\"" << name << "\", "
255          << rangeChanged << "):";
256     SVDEBUG << " type " << type << ", value " << value
257          << ", have " << have << ", group \"" << groupName << "\"" << endl;
258 #endif
259 
260     QString groupLabel = groupName;
261     if (groupName == QString()) {
262         groupName = "ungrouped: " + name; // not tr(), this is internal id
263         groupLabel = propertyLabel;
264     }
265 
266     if (!have) {
267         if (m_groupLayouts.find(groupName) == m_groupLayouts.end()) {
268             QWidget *labelWidget = new QLabel(groupLabel, m_mainWidget);
269             m_layout->addWidget(labelWidget, row, 0);
270             QWidget *frame = new QWidget(m_mainWidget);
271             frame->setMinimumSize(WidgetScale::scaleQSize(QSize(1, 24)));
272             m_groupLayouts[groupName] = new QGridLayout;
273 #ifdef Q_OS_MAC
274             // Seems to be plenty of whitespace already
275             m_groupLayouts[groupName]->setContentsMargins(0, 0, 0, 0);
276 #else
277             // Need a bit of padding on the left
278             m_groupLayouts[groupName]->setContentsMargins
279                 (WidgetScale::scalePixelSize(10), 0, 0, 0);
280 #endif
281             frame->setLayout(m_groupLayouts[groupName]);
282             m_layout->addWidget(frame, row, 1, 1, 2);
283             m_layout->setColumnStretch(1, 10);
284         }
285     }
286 
287     QGridLayout *groupLayout = m_groupLayouts[groupName];
288 
289 #ifdef DEBUG_PROPERTY_BOX
290     SVDEBUG << "groupName becomes \"" << groupName << "\", groupLabel = \""
291          << groupLabel << "\", groupLayout = " << groupLayout << endl;
292 #endif
293 
294     assert(groupLayout);
295 
296     QWidget *existing = m_propertyControllers[name];
297 
298     switch (type) {
299 
300     case PropertyContainer::ToggleProperty:
301     {
302         QAbstractButton *button;
303 
304         if (!(button = qobject_cast<QAbstractButton *>(existing))) {
305 #ifdef DEBUG_PROPERTY_BOX
306             SVDEBUG << "PropertyBox: creating new checkbox" << endl;
307 #endif
308             if (iconName != "") {
309 #ifdef Q_OS_MAC
310                 button = new NotifyingToolButton();
311 #else
312                 button = new NotifyingPushButton();
313 #endif
314                 button->setCheckable(true);
315                 QIcon icon(IconLoader().load(iconName));
316                 button->setIcon(icon);
317                 button->setObjectName(name);
318                 button->setFixedSize(WidgetScale::scaleQSize(QSize(18, 18)));
319             } else {
320                 button = new NotifyingCheckBox();
321                 button->setObjectName(name);
322             }
323             connect(button, SIGNAL(toggled(bool)),
324                     this, SLOT(propertyControllerChanged(bool)));
325             connect(button, SIGNAL(mouseEntered()),
326                     this, SLOT(mouseEnteredWidget()));
327             connect(button, SIGNAL(mouseLeft()),
328                     this, SLOT(mouseLeftWidget()));
329             button->setToolTip(propertyLabel);
330 
331             if (existing) {
332                 groupLayout->replaceWidget(existing, button);
333                 delete existing;
334             } else {
335                 groupLayout->addWidget(button, 0, groupLayout->columnCount());
336             }
337 
338             m_propertyControllers[name] = button;
339         }
340 
341         if (button->isChecked() != (value > 0)) {
342             button->blockSignals(true);
343             button->setChecked(value > 0);
344             button->blockSignals(false);
345         }
346         break;
347     }
348 
349     case PropertyContainer::RangeProperty:
350     {
351         AudioDial *dial;
352 
353         if ((dial = qobject_cast<AudioDial *>(existing))) {
354             if (rangeChanged) {
355                 dial->blockSignals(true);
356                 dial->setMinimum(min);
357                 dial->setMaximum(max);
358                 dial->setRangeMapper(m_container->getNewPropertyRangeMapper(name));
359                 dial->blockSignals(false);
360             }
361         } else {
362 #ifdef DEBUG_PROPERTY_BOX
363             SVDEBUG << "PropertyBox: creating new dial" << endl;
364 #endif
365             dial = new AudioDial();
366             dial->setObjectName(name);
367             dial->setMinimum(min);
368             dial->setMaximum(max);
369             dial->setPageStep(1);
370             dial->setNotchesVisible((max - min) <= 12);
371             // important to set the range mapper before the default,
372             // because the range mapper is used to map the default
373             dial->setRangeMapper(m_container->getNewPropertyRangeMapper(name));
374             dial->setDefaultValue(deflt);
375             dial->setShowToolTip(true);
376             connect(dial, SIGNAL(valueChanged(int)),
377                     this, SLOT(propertyControllerChanged(int)));
378             connect(dial, SIGNAL(mouseEntered()),
379                     this, SLOT(mouseEnteredWidget()));
380             connect(dial, SIGNAL(mouseLeft()),
381                     this, SLOT(mouseLeftWidget()));
382 
383             dial->setFixedWidth(WidgetScale::scalePixelSize(24));
384             dial->setFixedHeight(WidgetScale::scalePixelSize(24));
385 
386             if (existing) {
387                 groupLayout->replaceWidget(existing, dial);
388                 delete existing;
389             } else {
390                 groupLayout->addWidget(dial, 0, groupLayout->columnCount());
391             }
392 
393             m_propertyControllers[name] = dial;
394         }
395 
396         if (dial->value() != value) {
397             dial->blockSignals(true);
398             dial->setValue(value);
399             dial->blockSignals(false);
400         }
401         break;
402     }
403 
404     case PropertyContainer::ColourProperty:
405     {
406         ColourComboBox *cb;
407 
408         if (!(cb = qobject_cast<ColourComboBox *>(existing))) {
409 
410 #ifdef DEBUG_PROPERTY_BOX
411             SVDEBUG << "PropertyBox: creating new colour combobox" << endl;
412 #endif
413             cb = new ColourComboBox(true);
414             cb->setObjectName(name);
415 
416             connect(cb, SIGNAL(colourChanged(int)),
417                     this, SLOT(propertyControllerChanged(int)));
418             connect(cb, SIGNAL(mouseEntered()),
419                     this, SLOT(mouseEnteredWidget()));
420             connect(cb, SIGNAL(mouseLeft()),
421                     this, SLOT(mouseLeftWidget()));
422 
423             cb->setToolTip(propertyLabel);
424 
425             if (existing) {
426                 groupLayout->replaceWidget(existing, cb);
427                 delete existing;
428             } else {
429                 groupLayout->addWidget(cb, 0, groupLayout->columnCount());
430             }
431 
432             m_propertyControllers[name] = cb;
433         }
434 
435         if (cb->currentIndex() != value) {
436             cb->blockSignals(true);
437             cb->setCurrentIndex(value);
438             cb->blockSignals(false);
439         }
440 
441         break;
442     }
443 
444     case PropertyContainer::ColourMapProperty:
445     {
446         ColourMapComboBox *cb;
447 
448         if (!(cb = qobject_cast<ColourMapComboBox *>(existing))) {
449 #ifdef DEBUG_PROPERTY_BOX
450             SVDEBUG << "PropertyBox: creating new colourmap combobox" << endl;
451 #endif
452             cb = new ColourMapComboBox(false);
453             cb->setObjectName(name);
454 
455             connect(cb, SIGNAL(colourMapChanged(int)),
456                     this, SLOT(propertyControllerChanged(int)));
457             connect(cb, SIGNAL(mouseEntered()),
458                     this, SLOT(mouseEnteredWidget()));
459             connect(cb, SIGNAL(mouseLeft()),
460                     this, SLOT(mouseLeftWidget()));
461 
462             cb->setToolTip(propertyLabel);
463 
464             if (existing) {
465                 groupLayout->replaceWidget(existing, cb);
466                 delete existing;
467             } else {
468                 groupLayout->addWidget(cb, 0, groupLayout->columnCount());
469             }
470 
471             m_propertyControllers[name] = cb;
472         }
473 
474         if (cb->currentIndex() != value) {
475             cb->blockSignals(true);
476             cb->setCurrentIndex(value);
477             cb->blockSignals(false);
478         }
479 
480         break;
481     }
482 
483     case PropertyContainer::ValueProperty:
484     case PropertyContainer::UnitsProperty:
485     {
486         NotifyingComboBox *cb;
487 
488         if (!(cb = qobject_cast<NotifyingComboBox *>(existing))) {
489 #ifdef DEBUG_PROPERTY_BOX
490             SVDEBUG << "PropertyBox: creating new combobox" << endl;
491 #endif
492             cb = new NotifyingComboBox();
493             cb->setObjectName(name);
494             cb->setDuplicatesEnabled(false);
495         }
496 
497         if (!have || rangeChanged) {
498 
499             cb->blockSignals(true);
500             cb->clear();
501             cb->setEditable(false);
502 
503             if (type == PropertyContainer::ValueProperty) {
504 
505                 for (int i = min; i <= max; ++i) {
506 
507                     QString label = m_container->getPropertyValueLabel(name, i);
508                     QString iname = m_container->getPropertyValueIconName(name, i);
509 
510                     if (iname != "") {
511                         QIcon icon(IconLoader().load(iname));
512                         cb->addItem(icon, label);
513                     } else {
514                         cb->addItem(label);
515                     }
516                 }
517 
518             } else { // PropertyContainer::UnitsProperty
519 
520                 QStringList units = UnitDatabase::getInstance()->getKnownUnits();
521                 for (int i = 0; i < units.size(); ++i) {
522                     cb->addItem(units[i]);
523                 }
524 
525                 cb->setEditable(true);
526             }
527         }
528 
529         if (!have) {
530             connect(cb, SIGNAL(activated(int)),
531                     this, SLOT(propertyControllerChanged(int)));
532             connect(cb, SIGNAL(mouseEntered()),
533                     this, SLOT(mouseEnteredWidget()));
534             connect(cb, SIGNAL(mouseLeft()),
535                     this, SLOT(mouseLeftWidget()));
536 
537             cb->setToolTip(propertyLabel);
538             groupLayout->addWidget(cb, 0, groupLayout->columnCount());
539             m_propertyControllers[name] = cb;
540         } else if (existing != cb) {
541             groupLayout->replaceWidget(existing, cb);
542             delete existing;
543         }
544 
545         cb->blockSignals(true);
546         if (type == PropertyContainer::ValueProperty) {
547             if (cb->currentIndex() != value) {
548                 cb->setCurrentIndex(value);
549             }
550         } else {
551             QString unit = UnitDatabase::getInstance()->getUnitById(value);
552             if (cb->currentText() != unit) {
553                 for (int i = 0; i < cb->count(); ++i) {
554                     if (cb->itemText(i) == unit) {
555                         cb->setCurrentIndex(i);
556                         break;
557                     }
558                 }
559             }
560         }
561         cb->blockSignals(false);
562 
563         break;
564     }
565 
566     case PropertyContainer::InvalidProperty:
567     default:
568         break;
569     }
570 }
571 
572 void
propertyContainerPropertyChanged(PropertyContainer * pc)573 PropertyBox::propertyContainerPropertyChanged(PropertyContainer *pc)
574 {
575     if (pc != m_container) return;
576 
577 #ifdef DEBUG_PROPERTY_BOX
578     SVDEBUG << "PropertyBox::propertyContainerPropertyChanged" << endl;
579 #endif
580 
581     PropertyContainer::PropertyList properties = m_container->getProperties();
582     size_t i;
583 
584     blockSignals(true);
585 
586     for (i = 0; i < properties.size(); ++i) {
587         updatePropertyEditor(properties[i]);
588     }
589 
590     blockSignals(false);
591 }
592 
593 void
propertyContainerPropertyRangeChanged(PropertyContainer *)594 PropertyBox::propertyContainerPropertyRangeChanged(PropertyContainer *)
595 {
596     blockSignals(true);
597 
598     PropertyContainer::PropertyList properties = m_container->getProperties();
599     for (size_t i = 0; i < properties.size(); ++i) {
600         updatePropertyEditor(properties[i], true);
601     }
602 
603     blockSignals(false);
604 }
605 
606 void
unitDatabaseChanged()607 PropertyBox::unitDatabaseChanged()
608 {
609 #ifdef DEBUG_PROPERTY_BOX
610     SVDEBUG << "PropertyBox[" << this << "]: unitDatabaseChanged" << endl;
611 #endif
612     blockSignals(true);
613 
614 //    SVDEBUG << "my container is " << m_container << endl;
615 //    SVDEBUG << "my container's name is... " << endl;
616 //    SVDEBUG << m_container->objectName() << endl;
617 
618     PropertyContainer::PropertyList properties = m_container->getProperties();
619     for (size_t i = 0; i < properties.size(); ++i) {
620         if (m_container->getPropertyType(properties[i]) ==
621             PropertyContainer::UnitsProperty) {
622             updatePropertyEditor(properties[i]);
623         }
624     }
625 
626     blockSignals(false);
627 }
628 
629 void
propertyControllerChanged(bool on)630 PropertyBox::propertyControllerChanged(bool on)
631 {
632     propertyControllerChanged(on ? 1 : 0);
633 }
634 
635 void
propertyControllerChanged(int value)636 PropertyBox::propertyControllerChanged(int value)
637 {
638     QObject *obj = sender();
639     QString name = obj->objectName();
640 
641 #ifdef DEBUG_PROPERTY_BOX
642     SVDEBUG << "PropertyBox::propertyControllerChanged(" << name
643             << ", " << value << ")" << endl;
644 #endif
645 
646     PropertyContainer::PropertyType type = m_container->getPropertyType(name);
647 
648     Command *c = nullptr;
649 
650     if (type == PropertyContainer::UnitsProperty) {
651 
652         NotifyingComboBox *cb = qobject_cast<NotifyingComboBox *>(obj);
653         if (cb) {
654             QString unit = cb->currentText();
655             c = m_container->getSetPropertyCommand
656                 (name, UnitDatabase::getInstance()->getUnitId(unit));
657         }
658 
659     } else if (type != PropertyContainer::InvalidProperty) {
660 
661         c = m_container->getSetPropertyCommand(name, value);
662     }
663 
664     if (c) CommandHistory::getInstance()->addCommand(c, true, true);
665 
666     updateContextHelp(obj);
667 }
668 
669 void
playAudibleChanged(bool audible)670 PropertyBox::playAudibleChanged(bool audible)
671 {
672     m_playButton->setChecked(audible);
673 }
674 
675 void
playAudibleButtonChanged(bool audible)676 PropertyBox::playAudibleButtonChanged(bool audible)
677 {
678     auto params = m_container->getPlayParameters();
679     if (!params) return;
680 
681     if (params->isPlayAudible() != audible) {
682         PlayParameterRepository::EditCommand *command =
683             new PlayParameterRepository::EditCommand(params);
684         command->setPlayAudible(audible);
685         CommandHistory::getInstance()->addCommand(command, true, true);
686     }
687 }
688 
689 void
playGainControlChanged(float gain)690 PropertyBox::playGainControlChanged(float gain)
691 {
692     QObject *obj = sender();
693 
694     auto params = m_container->getPlayParameters();
695     if (!params) return;
696 
697     if (params->getPlayGain() != gain) {
698         PlayParameterRepository::EditCommand *command =
699             new PlayParameterRepository::EditCommand(params);
700         command->setPlayGain(gain);
701         CommandHistory::getInstance()->addCommand(command, true, true);
702     }
703 
704     updateContextHelp(obj);
705 }
706 
707 void
playPanControlChanged(float pan)708 PropertyBox::playPanControlChanged(float pan)
709 {
710     QObject *obj = sender();
711 
712     auto params = m_container->getPlayParameters();
713     if (!params) return;
714 
715     if (params->getPlayPan() != pan) {
716         PlayParameterRepository::EditCommand *command =
717             new PlayParameterRepository::EditCommand(params);
718         command->setPlayPan(pan);
719         CommandHistory::getInstance()->addCommand(command, true, true);
720     }
721 
722     updateContextHelp(obj);
723 }
724 
725 void
editPlayParameters()726 PropertyBox::editPlayParameters()
727 {
728     auto params = m_container->getPlayParameters();
729     if (!params) return;
730 
731     QString clip = params->getPlayClipId();
732 
733     PlayParameterRepository::EditCommand *command =
734         new PlayParameterRepository::EditCommand(params);
735 
736     QInputDialog *dialog = new QInputDialog(this);
737 
738     QDir dir(":/samples");
739     QStringList clipFiles = dir.entryList(QStringList() << "*.wav", QDir::Files);
740 
741     QStringList clips;
742     foreach (QString str, clipFiles) {
743         clips.push_back(str.replace(".wav", ""));
744     }
745     dialog->setComboBoxItems(clips);
746 
747     dialog->setLabelText(tr("Set playback clip:"));
748 
749     QComboBox *cb = dialog->findChild<QComboBox *>();
750     if (cb) {
751         for (int i = 0; i < cb->count(); ++i) {
752             if (cb->itemText(i) == clip) {
753                 cb->setCurrentIndex(i);
754             }
755         }
756     }
757 
758     connect(dialog, SIGNAL(textValueChanged(QString)),
759             this, SLOT(playClipChanged(QString)));
760 
761     if (dialog->exec() == QDialog::Accepted) {
762         QString newClip = dialog->textValue();
763         command->setPlayClipId(newClip);
764         CommandHistory::getInstance()->addCommand(command, true);
765     } else {
766         delete command;
767         // restore in case we mucked about with the configuration
768         // as a consequence of signals from the dialog
769         params->setPlayClipId(clip);
770     }
771 
772     delete dialog;
773 }
774 
775 void
playClipChanged(QString id)776 PropertyBox::playClipChanged(QString id)
777 {
778     auto params = m_container->getPlayParameters();
779     if (!params) return;
780 
781     params->setPlayClipId(id);
782 }
783 
784 void
layerVisibilityChanged(bool visible)785 PropertyBox::layerVisibilityChanged(bool visible)
786 {
787     if (m_showButton) m_showButton->setState(visible);
788 }
789 
790 void
mouseEnteredWidget()791 PropertyBox::mouseEnteredWidget()
792 {
793     updateContextHelp(sender());
794 }
795 
796 void
updateContextHelp(QObject * o)797 PropertyBox::updateContextHelp(QObject *o)
798 {
799     QWidget *w = qobject_cast<QWidget *>(o);
800     if (!w) return;
801 
802     if (!m_container) return;
803     QString cname = m_container->getPropertyContainerName();
804     if (cname == "") return;
805 
806     QString help;
807     QString mainText;
808     QString extraText;
809     QString editText;
810 
811     QString wname = w->objectName();
812     QString propertyLabel;
813     if (wname != "") {
814         propertyLabel = m_container->getPropertyLabel(wname);
815     }
816 
817     LevelPanToolButton *lp = qobject_cast<LevelPanToolButton *>(w);
818     AudioDial *dial = qobject_cast<AudioDial *>(w);
819 
820     if (lp) {
821 
822         mainText = tr("Adjust playback level and pan of %1").arg(cname);
823         editText = tr("click then drag to adjust, ctrl+click to reset");
824 
825     } else if (wname == "playParamButton") {
826 
827         auto params = m_container->getPlayParameters();
828         if (params) {
829             help = tr("Change sound used for playback (currently \"%1\")")
830                 .arg(params->getPlayClipId());
831         }
832 
833     } else if (dial) {
834 
835         double mv = dial->mappedValue();
836         QString unit = "";
837         if (dial->rangeMapper()) unit = dial->rangeMapper()->getUnit();
838         if (unit != "") {
839             extraText = tr(" (current value: %1%2)").arg(mv).arg(unit);
840         } else {
841             extraText = tr(" (current value: %1)").arg(mv);
842         }
843         editText = tr("drag up/down to adjust, ctrl+click to reset");
844 
845     } else if (w == m_showButton) {
846         help = tr("Toggle Visibility of %1").arg(cname);
847 
848     } else if (w == m_playButton) {
849         help = tr("Toggle Playback of %1").arg(cname);
850 
851     }
852 
853     if (help == "" && wname != "") {
854 
855         if (qobject_cast<QAbstractButton *>(w)) {
856             mainText = tr("Toggle %1 property of %2")
857                 .arg(propertyLabel).arg(cname);
858 
859         } else {
860 
861             // Last param empty for historical reasons, to avoid
862             // changing tr() string
863             mainText = tr("Adjust %1 property of %2%3")
864                 .arg(propertyLabel).arg(cname).arg("");
865         }
866     }
867 
868     if (help == "") {
869         if (mainText != "") {
870             if (editText != "") {
871                 help = tr("%1%2: %3")
872                     .arg(mainText).arg(extraText).arg(editText);
873             } else {
874                 help = tr("%1%2")
875                     .arg(mainText).arg(extraText);
876             }
877         }
878     }
879 
880     if (help != "") {
881         emit contextHelpChanged(help);
882     }
883 }
884 
885 void
mouseLeftWidget()886 PropertyBox::mouseLeftWidget()
887 {
888     if (!(QApplication::mouseButtons() & Qt::LeftButton)) {
889         emit contextHelpChanged("");
890     }
891 }
892 
893 
894