diff --git a/src/core/model/searchchannelmodel.h b/src/core/model/searchchannelmodel.h index b7c9bcd5..360a112c 100644 --- a/src/core/model/searchchannelmodel.h +++ b/src/core/model/searchchannelmodel.h @@ -1,62 +1,62 @@ /* Copyright (c) 2018-2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef SEARCHCHANNELMODEL_H #define SEARCHCHANNELMODEL_H #include #include #include "libruqola_private_export.h" class LIBRUQOLACORE_TESTS_EXPORT SearchChannelModel : public QAbstractListModel { Q_OBJECT public: enum ChannelRoles { ChannelName = Qt::UserRole + 1, ChannelId, IconName, ChannelType, }; Q_ENUM(ChannelRoles) explicit SearchChannelModel(QObject *parent = nullptr); ~SearchChannelModel() override; Q_REQUIRED_RESULT int rowCount(const QModelIndex &parent = QModelIndex()) const override; Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; Q_REQUIRED_RESULT QHash roleNames() const override; void setChannels(const QVector &channels); void parseChannels(const QJsonObject &obj); void clear(); void parseAllChannels(const QJsonObject &obj); private: - QIcon channelIconName(const Channel &channel) const; - QString channelId(const Channel &channel) const; - QString channelName(const Channel &channel) const; + Q_REQUIRED_RESULT QIcon channelIconName(const Channel &channel) const; + Q_REQUIRED_RESULT QString channelId(const Channel &channel) const; + Q_REQUIRED_RESULT QString channelName(const Channel &channel) const; QVector mChannel; }; #endif // SEARCHCHANNELMODEL_H diff --git a/src/widgets/dialogs/searchchannel/searchchanneldelegate.cpp b/src/widgets/dialogs/searchchannel/searchchanneldelegate.cpp index a7776c09..5a6f3841 100644 --- a/src/widgets/dialogs/searchchannel/searchchanneldelegate.cpp +++ b/src/widgets/dialogs/searchchannel/searchchanneldelegate.cpp @@ -1,84 +1,86 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "searchchanneldelegate.h" +#include "common/delegatepaintutil.h" #include #include #include #include "model/searchchannelmodel.h" SearchChannelDelegate::SearchChannelDelegate(QObject *parent) : QItemDelegate(parent) , mAddChannel(QIcon::fromTheme(QStringLiteral("list-add"))) { } SearchChannelDelegate::~SearchChannelDelegate() { } void SearchChannelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { // [M] [M] [M] [M] ([M] = margin) //TODO add channel type icon too painter->save(); const Layout layout = doLayout(option, index); - - const int iconSize = option.widget->style()->pixelMetric(QStyle::PM_ButtonIconSize); - const int margin = 8; - - const QRect decorationRect(option.rect.x() + margin, option.rect.y(), iconSize, option.rect.height()); - - const QString text = index.data(SearchChannelModel::ChannelName).toString(); - - const int xText = option.rect.x() + iconSize + 2 * margin; - const QRect displayRect(xText, option.rect.y(), - option.rect.width() - xText - 2 * margin - iconSize, - option.rect.height()); - QStyleOptionViewItem optionCopy = option; optionCopy.showDecorationSelected = true; drawBackground(painter, optionCopy, index); + const QIcon icon = index.data(SearchChannelModel::IconName).value(); - icon.paint(painter, decorationRect, Qt::AlignCenter); + icon.paint(painter, layout.iconChannelRect, Qt::AlignCenter); - const QRect addChannelIconRect(option.rect.width() - iconSize - 2 * margin, option.rect.y(), iconSize, option.rect.height()); - mAddChannel.paint(painter, addChannelIconRect, Qt::AlignCenter); + painter->drawText(layout.usableRect, layout.channelName); + + mAddChannel.paint(painter, layout.selectChannelRect, Qt::AlignCenter); - drawDisplay(painter, optionCopy, displayRect, text); // this takes care of eliding if the text is too long painter->restore(); } bool SearchChannelDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { const QEvent::Type eventType = event->type(); if (eventType == QEvent::MouseButtonRelease) { auto *mev = static_cast(event); const Layout layout = doLayout(option, index); + if (layout.selectChannelRect.contains(mev->pos())) { + return true; + } //TODO } return QItemDelegate::editorEvent(event, model, option, index); } SearchChannelDelegate::Layout SearchChannelDelegate::doLayout(const QStyleOptionViewItem &option, const QModelIndex &index) const { - //TODO - return {}; + Layout layout; + QRect usableRect = option.rect; + const int iconSize = option.widget->style()->pixelMetric(QStyle::PM_ButtonIconSize); + layout.usableRect = usableRect; // Just for the top, for now. The left will move later on. + + layout.iconChannelRect = QRect(option.rect.x() + DelegatePaintUtil::margin(), option.rect.y(), iconSize, option.rect.height()); + + layout.channelName = index.data(SearchChannelModel::ChannelName).toString(); + + layout.selectChannelRect = QRect(option.rect.width() - iconSize - 2 * DelegatePaintUtil::margin(), option.rect.y(), iconSize, option.rect.height()); + + return layout; } diff --git a/src/widgets/dialogs/searchchannel/searchchanneldelegate.h b/src/widgets/dialogs/searchchannel/searchchanneldelegate.h index 1d67a443..039ec488 100644 --- a/src/widgets/dialogs/searchchannel/searchchanneldelegate.h +++ b/src/widgets/dialogs/searchchannel/searchchanneldelegate.h @@ -1,46 +1,50 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef SEARCHCHANNELDELEGATE_H #define SEARCHCHANNELDELEGATE_H #include "libruqolawidgets_private_export.h" #include class LIBRUQOLAWIDGETS_TESTS_EXPORT SearchChannelDelegate : public QItemDelegate { Q_OBJECT public: explicit SearchChannelDelegate(QObject *parent = nullptr); ~SearchChannelDelegate() override; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override; private: struct Layout { + QRect iconChannelRect; + QRect usableRect; + QString channelName; + //Select Channel Rect QRect selectChannelRect; }; SearchChannelDelegate::Layout doLayout(const QStyleOptionViewItem &option, const QModelIndex &index) const; QIcon mAddChannel; }; #endif