diff --git a/src/widgets/dialogs/searchchannel/searchchanneldelegate.cpp b/src/widgets/dialogs/searchchannel/searchchanneldelegate.cpp index c61199dd..e411ee10 100644 --- a/src/widgets/dialogs/searchchannel/searchchanneldelegate.cpp +++ b/src/widgets/dialogs/searchchannel/searchchanneldelegate.cpp @@ -1,88 +1,88 @@ /* 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); QStyleOptionViewItem optionCopy = option; optionCopy.showDecorationSelected = true; drawBackground(painter, optionCopy, index); const QIcon icon = index.data(SearchChannelModel::IconName).value(); icon.paint(painter, layout.iconChannelRect, Qt::AlignCenter); painter->drawText(layout.usableRect, layout.channelName); mAddChannel.paint(painter, layout.selectChannelRect, Qt::AlignCenter); 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())) { + Q_EMIT channelSelected(index); return true; } - //TODO } return QItemDelegate::editorEvent(event, model, option, index); } SearchChannelDelegate::Layout SearchChannelDelegate::doLayout(const QStyleOptionViewItem &option, const QModelIndex &index) const { 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 - DelegatePaintUtil::margin(), option.rect.y(), iconSize, option.rect.height()); layout.usableRect.setLeft(layout.iconChannelRect.width() + DelegatePaintUtil::margin()); return layout; } diff --git a/src/widgets/dialogs/searchchannel/searchchanneldelegate.h b/src/widgets/dialogs/searchchannel/searchchanneldelegate.h index 039ec488..58d1f96f 100644 --- a/src/widgets/dialogs/searchchannel/searchchanneldelegate.h +++ b/src/widgets/dialogs/searchchannel/searchchanneldelegate.h @@ -1,50 +1,54 @@ /* 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; + +Q_SIGNALS: + void channelSelected(const QModelIndex &index); + 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 diff --git a/src/widgets/dialogs/searchchannelwidget.cpp b/src/widgets/dialogs/searchchannelwidget.cpp index 0eb5172c..5f42d103 100644 --- a/src/widgets/dialogs/searchchannelwidget.cpp +++ b/src/widgets/dialogs/searchchannelwidget.cpp @@ -1,78 +1,79 @@ /* 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 "searchchannelwidget.h" #include "searchchannel/searchchanneldelegate.h" #include "model/searchchannelfilterproxymodel.h" #include "model/searchchannelmodel.h" #include "misc/lineeditcatchreturnkey.h" #include "ruqolawidgets_debug.h" #include "ruqola.h" #include "rocketchataccount.h" #include #include #include #include SearchChannelWidget::SearchChannelWidget(QWidget *parent) : QWidget(parent) { auto *mainLayout = new QVBoxLayout(this); mainLayout->setObjectName(QStringLiteral("mainLayout")); mainLayout->setContentsMargins(0, 0, 0, 0); mSearchLineEdit = new QLineEdit(this); mSearchLineEdit->setObjectName(QStringLiteral("mSearchLineEdit")); mSearchLineEdit->setClearButtonEnabled(true); new LineEditCatchReturnKey(mSearchLineEdit, this); connect(mSearchLineEdit, &QLineEdit::textChanged, this, &SearchChannelWidget::slotTextChanged); mainLayout->addWidget(mSearchLineEdit); mResultListWidget = new QListView(this); mResultListWidget->setModel(Ruqola::self()->rocketChatAccount()->searchChannelFilterProxyModel()); mResultListWidget->setObjectName(QStringLiteral("mResultListWidget")); mainLayout->addWidget(mResultListWidget); - mResultListWidget->setItemDelegate(new SearchChannelDelegate(this)); - connect(mResultListWidget, &QListView::doubleClicked, this, &SearchChannelWidget::slotOpenChannel); + SearchChannelDelegate *searchDeletegate = new SearchChannelDelegate(this); + mResultListWidget->setItemDelegate(searchDeletegate); + connect(searchDeletegate, &SearchChannelDelegate::channelSelected, this, &SearchChannelWidget::slotOpenChannel); } SearchChannelWidget::~SearchChannelWidget() { } void SearchChannelWidget::slotTextChanged(const QString &str) { Ruqola::self()->rocketChatAccount()->channelAndPrivateAutocomplete(str); } void SearchChannelWidget::slotOpenChannel(const QModelIndex &index) { if (index.isValid()) { const auto channelType = index.data(SearchChannelModel::ChannelType).value(); const QString channelId = index.data(SearchChannelModel::ChannelId).toString(); if (channelType == Channel::ChannelType::Room) { Ruqola::self()->rocketChatAccount()->openChannel(channelId, RocketChatAccount::ChannelTypeInfo::RoomId); } else if (channelType == Channel::ChannelType::PrivateChannel) { Ruqola::self()->rocketChatAccount()->openDirectChannel(channelId); } else { qCWarning(RUQOLAWIDGETS_LOG) << "Unknown open channel type : " << channelType << " channelid : " << channelId; } } }