summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swift/QtUI/QtSoundSelectionStyledItemDelegate.cpp')
-rw-r--r--Swift/QtUI/QtSoundSelectionStyledItemDelegate.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/Swift/QtUI/QtSoundSelectionStyledItemDelegate.cpp b/Swift/QtUI/QtSoundSelectionStyledItemDelegate.cpp
new file mode 100644
index 0000000..3811004
--- /dev/null
+++ b/Swift/QtUI/QtSoundSelectionStyledItemDelegate.cpp
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2017 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#include <Swift/QtUI/QtSoundSelectionStyledItemDelegate.h>
+
+#include <QApplication>
+#include <QComboBox>
+#include <QEvent>
+#include <QFileDialog>
+#include <QMenu>
+#include <QMouseEvent>
+#include <QPainter>
+#include <QStyle>
+#include <QStyleOptionComboBox>
+
+#include <Swiften/Base/Log.h>
+
+#include <Swift/QtUI/QtSwiftUtil.h>
+
+namespace Swift {
+
+QtSoundSelectionStyledItemDelegate::QtSoundSelectionStyledItemDelegate(QObject* parent) : QStyledItemDelegate(parent) {
+
+}
+
+void QtSoundSelectionStyledItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
+ // draw item selected background
+ painter->fillRect(option.rect, option.state & QStyle::State_Selected ? option.palette.highlight() : option.palette.base());
+
+ auto editRoleString = index.data(Qt::EditRole).toString();
+
+ // draw combo box
+ QStyleOptionComboBox opt;
+ opt.rect = option.rect;
+ opt.rect.setHeight(opt.rect.height() + 2);
+ opt.state = QStyle::State_Active | QStyle::State_Enabled;
+ if (editRoleString.isEmpty()) {
+ opt.currentText = tr("No sound");
+ }
+ else if (editRoleString == "defaultSound") {
+ opt.currentText = tr("Default sound");
+ }
+ else {
+ opt.currentText = editRoleString;
+ }
+
+ painter->save();
+ QFont smallFont;
+ smallFont.setPointSize(smallFont.pointSize() - 3);
+ painter->setFont(smallFont);
+
+ QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &opt, painter);
+ QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &opt, painter);
+ painter->restore();
+}
+
+bool QtSoundSelectionStyledItemDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& /*option*/, const QModelIndex& index) {
+ if (event->type() == QEvent::MouseButtonRelease) {
+ auto mouseEvent = dynamic_cast<QMouseEvent*>(event);
+ assert(mouseEvent);
+ auto editRoleString = index.data(Qt::EditRole).toString();
+
+ auto popUpMenu = new QMenu();
+
+ auto noSound = popUpMenu->addAction(tr("No sound"));
+ auto defaultSound = popUpMenu->addAction(tr("Default sound"));
+ QAction* customSoundFile = nullptr;
+ QAction* selectedAction = nullptr;
+ if (editRoleString.isEmpty()) {
+ selectedAction = noSound;
+ }
+ else if (editRoleString == "defaultSound") {
+ selectedAction = defaultSound;
+ }
+ else {
+ customSoundFile = popUpMenu->addAction(editRoleString);
+ selectedAction = customSoundFile;
+ }
+ if (selectedAction) {
+ selectedAction->setCheckable(true);
+ selectedAction->setChecked(true);
+ }
+ auto chooseSoundFile = popUpMenu->addAction(tr("Choose sound fileā€¦"));
+
+ selectedAction = popUpMenu->exec(mouseEvent->globalPos(), selectedAction);
+
+ if (selectedAction == defaultSound) {
+ model->setData(index, "defaultSound", Qt::EditRole);
+ }
+ else if (customSoundFile && (selectedAction == customSoundFile)) {
+ model->setData(index, customSoundFile->text(), Qt::EditRole);
+ }
+ else if (selectedAction == noSound) {
+ model->setData(index, "", Qt::EditRole);
+ }
+ else if (selectedAction == chooseSoundFile) {
+ auto newPath = QFileDialog::getOpenFileName(0, tr("Choose notification sound file"), "", tr("WAV Files (*.wav)"));
+ if (!newPath.isEmpty()) {
+ model->setData(index, newPath, Qt::EditRole);
+ }
+ }
+
+ delete popUpMenu;
+ }
+ return true;
+}
+
+};