File indexing completed on 2024-12-08 04:34:36

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "switchchanneltreeview.h"
0007 #include "model/switchchannelhistorymodel.h"
0008 #include "rocketchataccount.h"
0009 #include "switchchanneldelegate.h"
0010 #include <QKeyEvent>
0011 
0012 SwitchChannelTreeView::SwitchChannelTreeView(QWidget *parent)
0013     : QTreeView(parent)
0014     , mSwitchChannelDelegate(new SwitchChannelDelegate(this))
0015 {
0016     setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
0017     setSelectionBehavior(QAbstractItemView::SelectRows);
0018     setSelectionMode(QAbstractItemView::SingleSelection);
0019     setTextElideMode(Qt::ElideMiddle);
0020     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0021 
0022     setHeaderHidden(true);
0023     setRootIsDecorated(false);
0024     mSwitchChannelDelegate->setObjectName(QStringLiteral("mSwitchChannelDelegate"));
0025     setItemDelegate(mSwitchChannelDelegate);
0026 }
0027 
0028 SwitchChannelTreeView::~SwitchChannelTreeView() = default;
0029 
0030 void SwitchChannelTreeView::setCurrentRocketChatAccount(RocketChatAccount *newCurrentRocketChatAccount)
0031 {
0032     mSwitchChannelDelegate->setCurrentRocketChatAccount(newCurrentRocketChatAccount);
0033     setModel(newCurrentRocketChatAccount->switchChannelHistoryModel());
0034 }
0035 
0036 int SwitchChannelTreeView::sizeHintWidth() const
0037 {
0038     return sizeHintForColumn(0);
0039 }
0040 
0041 void SwitchChannelTreeView::resizeColumnsToContents()
0042 {
0043     resizeColumnToContents(0);
0044 }
0045 
0046 void SwitchChannelTreeView::keyReleaseEvent(QKeyEvent *event)
0047 {
0048     if (event->key() == Qt::Key_Control) {
0049         Q_EMIT channelSelected(selectionModel()->currentIndex());
0050         event->accept();
0051         hide();
0052     } else {
0053         QTreeView::keyReleaseEvent(event);
0054     }
0055 }
0056 
0057 void SwitchChannelTreeView::keyPressEvent(QKeyEvent *event)
0058 {
0059     if (event->key() == Qt::Key_Escape) {
0060         event->accept();
0061         hide();
0062     } else {
0063         QTreeView::keyPressEvent(event);
0064     }
0065 }
0066 
0067 void SwitchChannelTreeView::showEvent(QShowEvent *event)
0068 {
0069     resizeColumnsToContents();
0070     reset();
0071     QTreeView::showEvent(event);
0072 }
0073 
0074 #include "moc_switchchanneltreeview.cpp"