videodownloader/itemdelegates/comboboxitemdelegate.cpp

76 lines
2.5 KiB
C++
Raw Normal View History

2015-09-08 17:05:59 +02:00
#include "./comboboxitemdelegate.h"
2015-04-22 19:32:04 +02:00
#include "../model/downloadmodel.h"
#include <QApplication>
2017-05-01 03:22:50 +02:00
#include <QComboBox>
2015-04-22 19:32:04 +02:00
#include <QMessageBox>
2017-05-01 03:22:50 +02:00
#include <QStandardItemModel>
2015-04-22 19:32:04 +02:00
namespace QtGui {
2017-05-01 03:22:50 +02:00
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
2015-04-22 19:32:04 +02:00
ComboBoxItemDelegate::~ComboBoxItemDelegate()
2017-05-01 03:22:50 +02:00
{
}
2015-04-22 19:32:04 +02:00
2017-05-01 03:22:50 +02:00
bool ComboBoxItemDelegate::ComboBoxItemDelegate::editorEvent(
QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
2015-04-22 19:32:04 +02:00
{
2017-05-01 03:22:50 +02:00
switch (event->type()) {
2015-04-22 19:32:04 +02:00
case QEvent::MouseButtonPress:
return false;
case QEvent::GraphicsSceneWheel:
case QEvent::Wheel:
return false;
default:
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
}
QWidget *ComboBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
{
2017-05-01 03:22:50 +02:00
if (const DownloadModel *model = qobject_cast<const DownloadModel *>(index.model())) {
2015-04-22 19:32:04 +02:00
QStringList availableOptions = model->data(index, DownloadModel::AvailableOptionsRole).toStringList();
2017-05-01 03:22:50 +02:00
if (availableOptions.size() > 0) {
2015-04-22 19:32:04 +02:00
QComboBox *editor = new QComboBox(parent);
editor->addItems(availableOptions);
editor->setCurrentIndex(model->data(index, DownloadModel::ChosenOptionRole).toInt());
return editor;
}
}
return nullptr;
}
void ComboBoxItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
2017-05-01 03:22:50 +02:00
if (const DownloadModel *model = qobject_cast<const DownloadModel *>(index.model())) {
if (QComboBox *comboBox = qobject_cast<QComboBox *>(editor)) {
2015-04-22 19:32:04 +02:00
comboBox->setCurrentIndex(model->data(index, DownloadModel::ChosenOptionRole).toInt());
}
}
}
void ComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
2017-05-01 03:22:50 +02:00
if (QComboBox *comboBox = qobject_cast<QComboBox *>(editor)) {
2015-04-22 19:32:04 +02:00
model->setData(index, comboBox->currentIndex(), DownloadModel::ChosenOptionRole);
}
}
void ComboBoxItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
{
editor->setGeometry(option.rect);
}
2017-05-01 03:22:50 +02:00
void ComboBoxItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const //(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
2015-04-22 19:32:04 +02:00
{
QStyledItemDelegate::paint(painter, option, index);
}
2019-07-20 20:20:58 +02:00
} // namespace QtGui