From 29a39b4e7f60073ba46e019217ca455dd88df67a Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Wed, 28 Nov 2018 17:45:50 +0100 Subject: [PATCH] Attempt to fix devices issue --- src/device/devicemanager.h | 4 +-- src/device/devicestatefiltermodel.cpp | 14 +++++---- src/device/devicestatefiltermodel.h | 1 + src/device/deviceview.cpp | 43 ++++++++++++++------------- src/device/deviceviewcontainer.ui | 4 +-- 5 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/device/devicemanager.h b/src/device/devicemanager.h index d2fdb3474..ef26700a4 100644 --- a/src/device/devicemanager.h +++ b/src/device/devicemanager.h @@ -102,8 +102,8 @@ class DeviceManager : public QAbstractListModel { void SetDeviceOptions(int row, const QString &friendly_name, const QString &icon_name, MusicStorage::TranscodeMode mode, Song::FileType format); // QAbstractListModel - int rowCount(const QModelIndex &parent) const; - QVariant data(const QModelIndex &index, int role) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; public slots: void Unmount(int row); diff --git a/src/device/devicestatefiltermodel.cpp b/src/device/devicestatefiltermodel.cpp index bb50bd942..382e237db 100644 --- a/src/device/devicestatefiltermodel.cpp +++ b/src/device/devicestatefiltermodel.cpp @@ -30,11 +30,11 @@ DeviceStateFilterModel::DeviceStateFilterModel(QObject *parent, DeviceManager::State state) : QSortFilterProxyModel(parent), - state_(state) -{ - connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(ProxyRowCountChanged())); - connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(ProxyRowCountChanged())); - connect(this, SIGNAL(modelReset()), SLOT(ProxyRowCountChanged())); + state_(state) { + + connect(this, SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(ProxyRowCountChanged(QModelIndex, int, int))); + connect(this, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(ProxyRowCountChanged(QModelIndex, int, int))); + connect(this, SIGNAL(modelReset()), this, SLOT(ProxyRowCountChanged())); } bool DeviceStateFilterModel::filterAcceptsRow(int row, const QModelIndex&) const { @@ -45,6 +45,10 @@ void DeviceStateFilterModel::ProxyRowCountChanged() { emit IsEmptyChanged(rowCount() == 0); } +void DeviceStateFilterModel::ProxyRowCountChanged(QModelIndex index, int first, int last) { + emit IsEmptyChanged(rowCount() == 0); +} + void DeviceStateFilterModel::setSourceModel(QAbstractItemModel *sourceModel) { QSortFilterProxyModel::setSourceModel(sourceModel); setDynamicSortFilter(true); diff --git a/src/device/devicestatefiltermodel.h b/src/device/devicestatefiltermodel.h index 4919d4121..20edfc8af 100644 --- a/src/device/devicestatefiltermodel.h +++ b/src/device/devicestatefiltermodel.h @@ -49,6 +49,7 @@ protected: private slots: void ProxyRowCountChanged(); + void ProxyRowCountChanged(QModelIndex index, int first, int last); private: DeviceManager::State state_; diff --git a/src/device/deviceview.cpp b/src/device/deviceview.cpp index 895f9aaa1..a038a7385 100644 --- a/src/device/deviceview.cpp +++ b/src/device/deviceview.cpp @@ -71,23 +71,23 @@ const int DeviceItemDelegate::kIconPadding = 6; DeviceItemDelegate::DeviceItemDelegate(QObject *parent) : CollectionItemDelegate(parent) {} -void DeviceItemDelegate::paint(QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &index) const { +void DeviceItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { // Is it a device or a collection item? if (index.data(DeviceManager::Role::Role_State).isNull()) { - CollectionItemDelegate::paint(p, opt, index); + CollectionItemDelegate::paint(painter, option, index); return; } // Draw the background - const QWidget *widget = opt.widget; + const QWidget *widget = option.widget; QStyle *style = widget->style() ? widget->style() : QApplication::style(); - style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, p, widget); + style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, widget); - p->save(); + painter->save(); // Font for the status line - QFont status_font(opt.font); + QFont status_font(option.font); #ifdef Q_OS_WIN32 status_font.setPointSize(status_font.pointSize() - 1); @@ -95,25 +95,25 @@ void DeviceItemDelegate::paint(QPainter *p, const QStyleOptionViewItem &opt, con status_font.setPointSize(status_font.pointSize() - 2); #endif - const int text_height = QFontMetrics(opt.font).height() + QFontMetrics(status_font).height(); + const int text_height = QFontMetrics(option.font).height() + QFontMetrics(status_font).height(); - QRect line1(opt.rect); - QRect line2(opt.rect); - line1.setTop(line1.top() + (opt.rect.height() - text_height) / 2); - line2.setTop(line1.top() + QFontMetrics(opt.font).height()); + QRect line1(option.rect); + QRect line2(option.rect); + line1.setTop(line1.top() + (option.rect.height() - text_height) / 2); + line2.setTop(line1.top() + QFontMetrics(option.font).height()); line1.setLeft(line1.left() + DeviceManager::kDeviceIconSize + kIconPadding); line2.setLeft(line2.left() + DeviceManager::kDeviceIconSize + kIconPadding); // Change the color for selected items - if (opt.state & QStyle::State_Selected) { - p->setPen(opt.palette.color(QPalette::HighlightedText)); + if (option.state & QStyle::State_Selected) { + painter->setPen(option.palette.color(QPalette::HighlightedText)); } // Draw the icon - p->drawPixmap(opt.rect.topLeft(), index.data(Qt::DecorationRole).value()); + painter->drawPixmap(option.rect.topLeft(), index.data(Qt::DecorationRole).value()); // Draw the first line (device name) - p->drawText(line1, Qt::AlignLeft | Qt::AlignTop, index.data().toString()); + painter->drawText(line1, Qt::AlignLeft | Qt::AlignTop, index.data().toString()); // Draw the second line (status) DeviceManager::State state = static_cast(index.data(DeviceManager::Role_State).toInt()); @@ -154,14 +154,15 @@ void DeviceItemDelegate::paint(QPainter *p, const QStyleOptionViewItem &opt, con } } - if (opt.state & QStyle::State_Selected) - p->setPen(opt.palette.color(QPalette::HighlightedText)); + if (option.state & QStyle::State_Selected) + painter->setPen(option.palette.color(QPalette::HighlightedText)); else - p->setPen(opt.palette.color(QPalette::Dark)); - p->setFont(status_font); - p->drawText(line2, Qt::AlignLeft | Qt::AlignTop, status_text); + painter->setPen(option.palette.color(QPalette::Dark)); - p->restore(); + painter->setFont(status_font); + painter->drawText(line2, Qt::AlignLeft | Qt::AlignTop, status_text); + + painter->restore(); } diff --git a/src/device/deviceviewcontainer.ui b/src/device/deviceviewcontainer.ui index 4d6460056..fd0a23ad4 100644 --- a/src/device/deviceviewcontainer.ui +++ b/src/device/deviceviewcontainer.ui @@ -6,8 +6,8 @@ 0 0 - 300 - 300 + 400 + 600