33 #include "ui_notifypluginoptionspage.h"
37 #include <QFileDialog>
38 #include <QtAlgorithms>
39 #include <QStringList>
40 #include <QtCore/QSettings>
41 #include <QTableWidget>
46 #include <QMediaPlayer>
56 : IOptionsPage(parent)
57 , _objManager(*ExtensionSystem::PluginManager::instance()->getObject<
UAVObjectManager>())
59 , _dynamicFieldCondition(NULL)
60 , _dynamicFieldWidget(NULL)
61 , _dynamicFieldType(-1)
64 , _selectedNotification(NULL)
78 _optionsPage.reset(
new Ui::NotifyPluginOptionsPage());
80 QWidget *optionsPageWidget =
new QWidget;
81 _dynamicFieldWidget = NULL;
82 _dynamicFieldCondition = NULL;
85 _form = optionsPageWidget;
87 _optionsPage->setupUi(optionsPageWidget);
89 _optionsPage->SoundDirectoryPathChooser->setExpectedKind(Utils::PathChooser::Directory);
90 _optionsPage->SoundDirectoryPathChooser->setPromptDialogTitle(
91 tr(
"Choose sound collection directory"));
94 &NotifyPluginOptionsPage::on_clicked_buttonSoundFolder);
95 connect(_optionsPage->SoundCollectionList, QOverload<int>::of(&QComboBox::currentIndexChanged),
96 this, &NotifyPluginOptionsPage::on_changedIndex_soundLanguage);
102 _privListNotifications = _owner->getListNotifications();
105 setSelectedNotification(_owner->getCurrentNotification());
106 addDynamicFieldLayout();
108 updateConfigView(_selectedNotification);
114 int curr_row = _privListNotifications.indexOf(_selectedNotification);
115 _notifyRulesSelection->setCurrentIndex(_notifyRulesModel->index(curr_row, 0, QModelIndex()),
116 QItemSelectionModel::ClearAndSelect
117 | QItemSelectionModel::Rows);
119 return optionsPageWidget;
124 getOptionsPageValues(_owner->getCurrentNotification());
125 _owner->setEnableSound(_optionsPage->chkEnableSound->isChecked());
131 disconnect(_optionsPage->UAVObjectField,
132 QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
this,
133 &NotifyPluginOptionsPage::on_changedIndex_UAVField);
135 disconnect(_testSound, &QMediaPlayer::stateChanged,
this,
136 &NotifyPluginOptionsPage::on_changed_playButtonText);
142 void NotifyPluginOptionsPage::initButtons()
144 _optionsPage->chkEnableSound->setChecked(_owner->getEnableSound());
146 _optionsPage->buttonModify->setEnabled(
false);
147 _optionsPage->buttonDelete->setEnabled(
false);
148 _optionsPage->buttonPlayNotification->setEnabled(
false);
149 connect(_optionsPage->buttonAdd, &QAbstractButton::pressed,
this,
150 &NotifyPluginOptionsPage::on_clicked_buttonAddNotification);
151 connect(_optionsPage->buttonDelete, &QAbstractButton::pressed,
this,
152 &NotifyPluginOptionsPage::on_clicked_buttonDeleteNotification);
153 connect(_optionsPage->buttonModify, &QAbstractButton::pressed,
this,
154 &NotifyPluginOptionsPage::on_clicked_buttonModifyNotification);
155 connect(_optionsPage->buttonPlayNotification, &QAbstractButton::clicked,
this,
156 &NotifyPluginOptionsPage::on_clicked_buttonTestSoundNotification);
159 void NotifyPluginOptionsPage::initPhononPlayer()
161 _testSound =
new QMediaPlayer;
162 connect(_testSound, &QMediaPlayer::stateChanged,
this,
163 &NotifyPluginOptionsPage::on_changed_playButtonText);
166 void NotifyPluginOptionsPage::initRulesTable()
168 qNotifyDebug_if(_notifyRulesModel.isNull()) <<
"_notifyRulesModel.isNull())";
169 qNotifyDebug_if(!_notifyRulesSelection) <<
"_notifyRulesSelection.isNull())";
171 _notifyRulesSelection =
new QItemSelectionModel(_notifyRulesModel.data());
173 connect(_notifyRulesSelection, &QItemSelectionModel::selectionChanged,
this,
174 &NotifyPluginOptionsPage::on_changedSelection_notifyTable);
178 _optionsPage->notifyRulesView->setModel(_notifyRulesModel.data());
179 _optionsPage->notifyRulesView->setSelectionModel(_notifyRulesSelection);
182 _optionsPage->notifyRulesView->resizeRowsToContents();
183 _optionsPage->notifyRulesView->setColumnWidth(
eMessageName, 200);
184 _optionsPage->notifyRulesView->setColumnWidth(
eRepeatValue, 120);
185 _optionsPage->notifyRulesView->setColumnWidth(
eExpireTimer, 100);
186 _optionsPage->notifyRulesView->setColumnWidth(
eTurnOn, 60);
187 _optionsPage->notifyRulesView->setDragEnabled(
true);
188 _optionsPage->notifyRulesView->setAcceptDrops(
true);
189 _optionsPage->notifyRulesView->setDropIndicatorShown(
true);
190 _optionsPage->notifyRulesView->setDragDropMode(QAbstractItemView::InternalMove);
193 UAVObjectField *NotifyPluginOptionsPage::getObjectFieldFromSelected()
199 void NotifyPluginOptionsPage::setSelectedNotification(
NotificationItem *ntf)
201 _selectedNotification = ntf;
204 if (!_currUAVObject) {
209 void NotifyPluginOptionsPage::addDynamicFieldLayout()
214 QSizePolicy labelSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
215 labelSizePolicy.setHorizontalStretch(0);
216 labelSizePolicy.setVerticalStretch(0);
219 QLabel *labelSayOrder =
new QLabel(
"Say order ", _form);
220 labelSayOrder->setSizePolicy(labelSizePolicy);
222 _optionsPage->dynamicValueLayout->addWidget(labelSayOrder);
223 _sayOrder =
new QComboBox(_form);
224 _optionsPage->dynamicValueLayout->addWidget(_sayOrder);
227 QLabel *labelValueIs =
new QLabel(
"Value is ", _form);
228 labelValueIs->setSizePolicy(labelSizePolicy);
229 _optionsPage->dynamicValueLayout->addWidget(labelValueIs);
231 _dynamicFieldCondition =
new QComboBox(_form);
232 _optionsPage->dynamicValueLayout->addWidget(_dynamicFieldCondition);
234 addDynamicField(field);
237 void NotifyPluginOptionsPage::addDynamicField(
UAVObjectField *objField)
240 qNotifyDebug() <<
"addDynamicField | input objField == NULL";
243 if (objField->
getType() == _dynamicFieldType) {
244 if (QComboBox *fieldValue = dynamic_cast<QComboBox *>(_dynamicFieldWidget)) {
246 QStringList enumValues(objField->
getOptions());
247 fieldValue->addItems(enumValues);
252 disconnect(_dynamicFieldCondition,
253 QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
this,
254 &NotifyPluginOptionsPage::on_changedIndex_rangeValue);
256 _dynamicFieldCondition->clear();
259 _dynamicFieldCondition->removeItem(
smaller);
260 _dynamicFieldCondition->removeItem(
bigger);
265 _dynamicFieldCondition->setCurrentIndex(
268 connect(_dynamicFieldCondition, QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
269 this, &NotifyPluginOptionsPage::on_changedIndex_rangeValue);
271 addDynamicFieldWidget(objField);
274 void NotifyPluginOptionsPage::addDynamicFieldWidget(
UAVObjectField *objField)
282 if (_dynamicFieldWidget) {
283 _optionsPage->dynamicValueLayout->removeWidget(_dynamicFieldWidget);
284 delete _dynamicFieldWidget;
285 _dynamicFieldWidget = NULL;
288 QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
289 sizePolicy.setHorizontalStretch(0);
290 sizePolicy.setVerticalStretch(0);
292 _dynamicFieldType = objField->
getType();
293 switch (_dynamicFieldType) {
295 _dynamicFieldWidget =
new QComboBox(_form);
296 QStringList enumValues(objField->
getOptions());
297 (
dynamic_cast<QComboBox *
>(_dynamicFieldWidget))->addItems(enumValues);
301 Q_ASSERT(_dynamicFieldCondition);
304 _dynamicFieldWidget =
new QLineEdit(_form);
306 (
static_cast<QLineEdit *
>(_dynamicFieldWidget))->setInputMask(
"#999.99 : #999.99;");
307 (
static_cast<QLineEdit *
>(_dynamicFieldWidget))->setText(
"0000000000");
308 (
static_cast<QLineEdit *
>(_dynamicFieldWidget))->setCursorPosition(0);
310 _dynamicFieldWidget =
new QDoubleSpinBox(_form);
311 (
dynamic_cast<QDoubleSpinBox *
>(_dynamicFieldWidget))->setRange(-999.99, 999.99);
315 enum { eDynamicFieldWidth = 100 };
316 _dynamicFieldWidget->setSizePolicy(sizePolicy);
317 _dynamicFieldWidget->setFixedWidth(eDynamicFieldWidth);
318 _optionsPage->dynamicValueLayout->addWidget(_dynamicFieldWidget);
321 void NotifyPluginOptionsPage::setDynamicFieldValue(
NotificationItem *notification)
323 if (QDoubleSpinBox *seValue = dynamic_cast<QDoubleSpinBox *>(_dynamicFieldWidget))
324 seValue->setValue(notification->
singleValue().toDouble());
326 if (QComboBox *cbValue = dynamic_cast<QComboBox *>(_dynamicFieldWidget)) {
327 int idx = cbValue->findText(notification->
singleValue().toString());
329 cbValue->setCurrentIndex(idx);
331 if (QLineEdit *rangeValue = dynamic_cast<QLineEdit *>(_dynamicFieldWidget)) {
332 QString str = QString(
"%1%2")
333 .arg(notification->
singleValue().toDouble(), 5,
'f', 2,
'0')
335 rangeValue->setText(str);
338 <<
"NotifyPluginOptionsPage::setDynamicValueField | unknown _fieldValue: "
339 << _dynamicFieldWidget;
345 void NotifyPluginOptionsPage::resetFieldType()
347 _dynamicFieldType = -1;
350 void NotifyPluginOptionsPage::getOptionsPageValues(
NotificationItem *notification)
352 Q_ASSERT(notification);
355 notification->
setDataObject(_optionsPage->UAVObject->currentText());
356 notification->
setObjectField(_optionsPage->UAVObjectField->currentText());
357 notification->setSound1(_optionsPage->Sound1->currentText());
358 notification->setSound2(_optionsPage->Sound2->currentText());
359 notification->setSound3(_optionsPage->Sound3->currentText());
360 notification->
setSayOrder(_sayOrder->currentIndex());
363 if (QDoubleSpinBox *spinValue = dynamic_cast<QDoubleSpinBox *>(_dynamicFieldWidget))
366 if (QComboBox *comboBoxValue = dynamic_cast<QComboBox *>(_dynamicFieldWidget))
369 if (QLineEdit *rangeValue = dynamic_cast<QLineEdit *>(_dynamicFieldWidget)) {
370 QString str = rangeValue->text();
371 QStringList range = str.split(
':');
379 void NotifyPluginOptionsPage::updateConfigView(
NotificationItem *notification)
381 Q_ASSERT(notification);
382 disconnect(_optionsPage->UAVObject,
383 QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
this,
384 &NotifyPluginOptionsPage::on_changedIndex_UAVObject);
385 disconnect(_optionsPage->UAVObjectField,
386 QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
this,
387 &NotifyPluginOptionsPage::on_changedIndex_UAVField);
390 if (path.isEmpty()) {
394 _optionsPage->SoundDirectoryPathChooser->setPath(path);
396 if (-1 != _optionsPage->SoundCollectionList->findText(notification->
getCurrentLanguage())) {
397 _optionsPage->SoundCollectionList->setCurrentIndex(
400 _optionsPage->SoundCollectionList->setCurrentIndex(
401 _optionsPage->SoundCollectionList->findText(
"default"));
406 foreach (QVector<UAVDataObject *> list, objList) {
408 _optionsPage->UAVObject->addItem(obj->
getName());
412 if (-1 != _optionsPage->UAVObject->findText(notification->
getDataObject())) {
413 _optionsPage->UAVObject->setCurrentIndex(
414 _optionsPage->UAVObject->findText(notification->
getDataObject()));
417 _optionsPage->UAVObjectField->clear();
418 if (_currUAVObject) {
421 _optionsPage->UAVObjectField->addItem(field->
getName());
424 if (-1 != _optionsPage->UAVObjectField->findText(notification->
getObjectField())) {
425 _optionsPage->UAVObjectField->setCurrentIndex(
426 _optionsPage->UAVObjectField->findText(notification->
getObjectField()));
429 if (-1 != _optionsPage->Sound1->findText(notification->getSound1())) {
430 _optionsPage->Sound1->setCurrentIndex(
431 _optionsPage->Sound1->findText(notification->getSound1()));
434 _optionsPage->SoundCollectionList->setCurrentIndex(
435 _optionsPage->SoundCollectionList->findText(
"default"));
436 _optionsPage->Sound1->setCurrentIndex(
437 _optionsPage->Sound1->findText(notification->getSound1()));
440 if (-1 != _optionsPage->Sound2->findText(notification->getSound2())) {
441 _optionsPage->Sound2->setCurrentIndex(
442 _optionsPage->Sound2->findText(notification->getSound2()));
445 _optionsPage->SoundCollectionList->setCurrentIndex(
446 _optionsPage->SoundCollectionList->findText(
"default"));
447 _optionsPage->Sound2->setCurrentIndex(
448 _optionsPage->Sound2->findText(notification->getSound2()));
451 if (-1 != _optionsPage->Sound3->findText(notification->getSound3())) {
452 _optionsPage->Sound3->setCurrentIndex(
453 _optionsPage->Sound3->findText(notification->getSound3()));
456 _optionsPage->SoundCollectionList->setCurrentIndex(
457 _optionsPage->SoundCollectionList->findText(
"default"));
458 _optionsPage->Sound3->setCurrentIndex(
459 _optionsPage->Sound3->findText(notification->getSound3()));
464 _dynamicFieldCondition->setCurrentIndex(
467 _sayOrder->setCurrentIndex(notification->
getSayOrder());
469 setDynamicFieldValue(notification);
471 connect(_optionsPage->UAVObject,
472 QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
this,
473 &NotifyPluginOptionsPage::on_changedIndex_UAVObject);
474 connect(_optionsPage->UAVObjectField,
475 QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
this,
476 &NotifyPluginOptionsPage::on_changedIndex_UAVField);
479 void NotifyPluginOptionsPage::on_changedIndex_rangeValue(QString )
481 Q_ASSERT(_dynamicFieldWidget);
484 addDynamicFieldWidget(field);
485 setDynamicFieldValue(_selectedNotification);
488 void NotifyPluginOptionsPage::on_changedIndex_UAVField(QString field)
491 Q_ASSERT(_currUAVObject);
492 addDynamicField(_currUAVObject->
getField(field));
495 void NotifyPluginOptionsPage::on_changedIndex_UAVObject(QString val)
499 if (!_currUAVObject) {
500 qNotifyDebug() <<
"on_UAVObject_indexChanged | no such UAVOBject";
504 disconnect(_optionsPage->UAVObjectField,
505 QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
this,
506 &NotifyPluginOptionsPage::on_changedIndex_UAVField);
507 _optionsPage->UAVObjectField->clear();
509 _optionsPage->UAVObjectField->addItem(field->
getName());
511 connect(_optionsPage->UAVObjectField,
512 QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
this,
513 &NotifyPluginOptionsPage::on_changedIndex_UAVField);
514 _selectedNotification->
setObjectField(fieldList.at(0)->getName());
515 addDynamicField(fieldList.at(0));
518 void NotifyPluginOptionsPage::on_changedIndex_soundLanguage(
int index)
520 _optionsPage->SoundCollectionList->setCurrentIndex(index);
521 QString collectionPath = _optionsPage->SoundDirectoryPathChooser->path()
522 + QDir::toNativeSeparators(
"/" + _optionsPage->SoundCollectionList->currentText());
524 QDir dirPath(collectionPath);
528 dirPath.setNameFilters(filters);
529 QStringList listSoundFiles = dirPath.entryList(filters);
530 listSoundFiles.replaceInStrings(QRegExp(
".mp3|.wav"),
"");
531 _optionsPage->Sound1->clear();
532 _optionsPage->Sound2->clear();
533 _optionsPage->Sound3->clear();
534 _optionsPage->Sound1->addItem(
"");
535 _optionsPage->Sound1->addItems(listSoundFiles);
536 _optionsPage->Sound2->addItem(
"");
537 _optionsPage->Sound2->addItems(listSoundFiles);
538 _optionsPage->Sound3->addItem(
"");
539 _optionsPage->Sound3->addItems(listSoundFiles);
542 void NotifyPluginOptionsPage::on_changed_playButtonText(QMediaPlayer::State newstate)
546 if (newstate == QMediaPlayer::PausedState || newstate == QMediaPlayer::StoppedState) {
547 _optionsPage->buttonPlayNotification->setText(
"Play");
548 _optionsPage->buttonPlayNotification->setIcon(QPixmap(
":/notify/images/play.png"));
550 if (newstate == QMediaPlayer::PlayingState) {
551 _optionsPage->buttonPlayNotification->setText(
"Stop");
552 _optionsPage->buttonPlayNotification->setIcon(QPixmap(
":/notify/images/stop.png"));
557 void NotifyPluginOptionsPage::on_changedSelection_notifyTable(
558 const QItemSelection &selected,
const QItemSelection & )
562 if (selected.indexes().size()) {
564 setSelectedNotification(_privListNotifications.at(selected.indexes().at(0).row()));
566 addDynamicField(field);
567 updateConfigView(_selectedNotification);
570 _optionsPage->buttonModify->setEnabled(select);
571 _optionsPage->buttonDelete->setEnabled(select);
572 _optionsPage->buttonPlayNotification->setEnabled(select);
575 void NotifyPluginOptionsPage::on_clicked_buttonSoundFolder(
const QString &path)
578 QStringList listDirCollections = dirPath.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
579 _optionsPage->SoundCollectionList->clear();
580 _optionsPage->SoundCollectionList->addItems(listDirCollections);
583 void NotifyPluginOptionsPage::on_clicked_buttonTestSoundNotification()
587 playlist =
new QMediaPlaylist;
588 qNotifyDebug() <<
"on_buttonTestSoundNotification_clicked";
589 Q_ASSERT(-1 != _notifyRulesSelection->currentIndex().row());
591 notification = _privListNotifications.at(_notifyRulesSelection->currentIndex().row());
592 QStringList sequence = notification->
toSoundList();
593 if (sequence.isEmpty()) {
597 foreach (QString item, sequence) {
599 playlist->addMedia(QUrl::fromLocalFile(item));
601 _testSound->setPlaylist(playlist);
605 void NotifyPluginOptionsPage::on_clicked_buttonAddNotification()
609 if (_optionsPage->SoundDirectoryPathChooser->path().isEmpty()) {
610 QPalette textPalette = _optionsPage->SoundDirectoryPathChooser->palette();
611 textPalette.setColor(QPalette::Normal, QPalette::Text, Qt::red);
612 _optionsPage->SoundDirectoryPathChooser->setPalette(textPalette);
613 _optionsPage->SoundDirectoryPathChooser->setPath(
"please select sound collection folder");
617 getOptionsPageValues(notification);
619 if (((!_optionsPage->Sound2->currentText().isEmpty())
620 && (_sayOrder->currentText() ==
"After second"))
621 || ((!_optionsPage->Sound3->currentText().isEmpty())
622 && (_sayOrder->currentText() ==
"After third"))) {
626 notification->
setSayOrder(_sayOrder->currentIndex());
629 _notifyRulesModel->entryAdded(notification);
630 _notifyRulesSelection->setCurrentIndex(
631 _notifyRulesModel->index(_privListNotifications.size() - 1, 0, QModelIndex()),
632 QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
635 void NotifyPluginOptionsPage::on_clicked_buttonDeleteNotification()
637 _notifyRulesModel->removeRow(_notifyRulesSelection->currentIndex().row());
638 if (!_notifyRulesModel->rowCount()
639 && (_notifyRulesSelection->currentIndex().row() > 0
640 && _notifyRulesSelection->currentIndex().row() < _notifyRulesModel->rowCount())) {
641 _optionsPage->buttonDelete->setEnabled(
false);
642 _optionsPage->buttonModify->setEnabled(
false);
643 _optionsPage->buttonPlayNotification->setEnabled(
false);
647 void NotifyPluginOptionsPage::on_clicked_buttonModifyNotification()
650 getOptionsPageValues(notification);
652 _privListNotifications.at(_notifyRulesSelection->currentIndex().row())->retryValue());
654 _privListNotifications.at(_notifyRulesSelection->currentIndex().row())->lifetime());
656 _privListNotifications.at(_notifyRulesSelection->currentIndex().row())->mute());
658 _privListNotifications.replace(_notifyRulesSelection->currentIndex().row(), notification);
659 entryUpdated(_notifyRulesSelection->currentIndex().row());
662 void NotifyPluginOptionsPage::on_FinishedPlaying()
QVariant singleValue() const
Uses to logging only inside notify plugin, can be convinient turned on/off.
void setObjectField(QString text)
QStringList & toSoundList()
QString getObjectField() const
void changed(const QString &text)
static QStringList conditionValues
~NotifyPluginOptionsPage()
QString getCurrentLanguage() const
Notify Plugin options page header.
void setSayOrder(int text)
QStringList getOptions() const
void setDataObject(QString text)
static QStringList sayOrderValues
void updateNotificationList(QList< NotificationItem * > list)
UAVObjectField * getField(const QString &name)
void setSoundCollectionPath(QString path)
QString getSoundCollectionPath() const
NotifyPluginOptionsPage(QObject *parent=nullptr)
double valueRange2() const
QList< UAVObjectField * > getFields()
QWidget * createPage(QWidget *parent)
QString getDataObject() const
void entryUpdated(int offset)
void setCurrentLanguage(QString text)
void setSingleValue(QVariant value)
void setCondition(int value)
UAVObject * getObject(const QString &name, quint32 instId=0)
void updateNotifications(QList< NotificationItem * > list)
QVector< QVector< UAVDataObject * > > getDataObjectsVector()
void setLifetime(int value)
void entryUpdated(int index)
static QString InsertDataPath(QString path)
void setRetryValue(int value)
void setValueRange2(double value)
FieldType getType() const