File indexing completed on 2024-05-12 16:27:37

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "switchchanneltreeviewmanager.h"
0008 #include "model/switchchannelhistorymodel.h"
0009 #include "rocketchataccount.h"
0010 #include "ruqolawidgets_debug.h"
0011 #include "switchchanneltreeview.h"
0012 #include <QScrollBar>
0013 
0014 SwitchChannelTreeViewManager::SwitchChannelTreeViewManager(QObject *parent)
0015     : QObject{parent}
0016     , mSwitcherChannelTreeView(new SwitchChannelTreeView(nullptr)) // TODO use parent ???
0017 {
0018     connect(mSwitcherChannelTreeView, &SwitchChannelTreeView::pressed, this, &SwitchChannelTreeViewManager::switchToCollectionClicked);
0019     connect(mSwitcherChannelTreeView, &SwitchChannelTreeView::channelSelected, this, &SwitchChannelTreeViewManager::activateChannel);
0020 }
0021 
0022 SwitchChannelTreeViewManager::~SwitchChannelTreeViewManager()
0023 {
0024     delete mSwitcherChannelTreeView;
0025 }
0026 
0027 void SwitchChannelTreeViewManager::addActions(const QList<QAction *> &lst)
0028 {
0029     // Make sure that actions works when mSwitcherChannelTreeView is show.
0030     mSwitcherChannelTreeView->addActions(lst);
0031 }
0032 
0033 void SwitchChannelTreeViewManager::activateChannel(const QModelIndex &index)
0034 {
0035     if (!index.isValid()) {
0036         return;
0037     }
0038     const QString identifier = index.data(SwitchChannelHistoryModel::SwitchChannelHistoryRoles::Identifier).toString();
0039     Q_EMIT switchToChannel(identifier);
0040 
0041     mSwitcherChannelTreeView->hide();
0042 }
0043 
0044 void SwitchChannelTreeViewManager::switchToCollectionClicked(const QModelIndex &index)
0045 {
0046     mSwitcherChannelTreeView->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
0047     activateChannel(index);
0048 }
0049 
0050 QWidget *SwitchChannelTreeViewManager::parentWidget() const
0051 {
0052     return mParentWidget;
0053 }
0054 
0055 void SwitchChannelTreeViewManager::setParentWidget(QWidget *newParentWidget)
0056 {
0057     mParentWidget = newParentWidget;
0058 }
0059 
0060 void SwitchChannelTreeViewManager::selectChannel(const int from, const int to)
0061 {
0062     if (!mCurrentRocketChatAccount) {
0063         return;
0064     }
0065     if (mCurrentRocketChatAccount->switchChannelHistoryModel()->rowCount() == 0) {
0066         return;
0067     }
0068     QModelIndex index;
0069     const int step = from < to ? 1 : -1;
0070     if (!mSwitcherChannelTreeView->isVisible()) {
0071         updateViewGeometry();
0072         index = mCurrentRocketChatAccount->switchChannelHistoryModel()->index(from + step, 0);
0073         if (!index.isValid()) {
0074             index = mCurrentRocketChatAccount->switchChannelHistoryModel()->index(0, 0);
0075         }
0076         mSwitcherChannelTreeView->show();
0077         mSwitcherChannelTreeView->setFocus();
0078     } else {
0079         int newRow = mSwitcherChannelTreeView->selectionModel()->currentIndex().row() + step;
0080         if (newRow == to + step) {
0081             newRow = from;
0082         }
0083         index = mCurrentRocketChatAccount->switchChannelHistoryModel()->index(newRow, 0);
0084     }
0085 
0086     mSwitcherChannelTreeView->selectionModel()->select(index, QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect);
0087     mSwitcherChannelTreeView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
0088 }
0089 
0090 RocketChatAccount *SwitchChannelTreeViewManager::currentRocketChatAccount() const
0091 {
0092     return mCurrentRocketChatAccount;
0093 }
0094 
0095 void SwitchChannelTreeViewManager::setCurrentRocketChatAccount(RocketChatAccount *newCurrentRocketChatAccount)
0096 {
0097     if (mCurrentRocketChatAccount != newCurrentRocketChatAccount) {
0098         mCurrentRocketChatAccount = newCurrentRocketChatAccount;
0099         mSwitcherChannelTreeView->setCurrentRocketChatAccount(mCurrentRocketChatAccount);
0100     }
0101 }
0102 
0103 SwitchChannelTreeView *SwitchChannelTreeViewManager::switchChannelTreeView() const
0104 {
0105     return mSwitcherChannelTreeView;
0106 }
0107 
0108 void SwitchChannelTreeViewManager::selectForward()
0109 {
0110     selectChannel(0, mCurrentRocketChatAccount->switchChannelHistoryModel()->rowCount() - 1);
0111 }
0112 
0113 void SwitchChannelTreeViewManager::selectBackward()
0114 {
0115     selectChannel(mCurrentRocketChatAccount->switchChannelHistoryModel()->rowCount() - 1, 0);
0116 }
0117 
0118 void SwitchChannelTreeViewManager::updateViewGeometry()
0119 {
0120     QWidget *window = mParentWidget ? mParentWidget->window() : nullptr;
0121     if (window) {
0122         const QSize centralSize = window->size();
0123 
0124         const QSize viewMaxSize(centralSize.width() * 3 / 4, centralSize.height() * 3 / 4);
0125 
0126         const int rowHeight = mSwitcherChannelTreeView->sizeHintForRow(0);
0127         const int frameWidth = mSwitcherChannelTreeView->frameWidth();
0128         const QSize viewSize(
0129             std::min(mSwitcherChannelTreeView->sizeHintWidth() + 2 * frameWidth + mSwitcherChannelTreeView->verticalScrollBar()->width(), viewMaxSize.width()),
0130             std::min(std::max(rowHeight * mCurrentRocketChatAccount->switchChannelHistoryModel()->rowCount() + 2 * frameWidth, rowHeight * 6),
0131                      viewMaxSize.height()));
0132 
0133         // Position should be central over the editor area, so map to global from
0134         // parent of central widget since the view is positioned in global coords
0135         const QPoint centralWidgetPos = window->parentWidget() ? window->mapToGlobal(window->pos()) : window->pos();
0136         const int xPos = std::max(0, centralWidgetPos.x() + (centralSize.width() - viewSize.width()) / 2);
0137         const int yPos = std::max(0, centralWidgetPos.y() + (centralSize.height() - viewSize.height()) / 2);
0138 
0139         mSwitcherChannelTreeView->setFixedSize(viewSize);
0140         mSwitcherChannelTreeView->move(xPos, yPos);
0141     } else {
0142         qCWarning(RUQOLAWIDGETS_LOG) << "Problem mParentWidget is null! it's a bug";
0143     }
0144 }
0145 
0146 #include "moc_switchchanneltreeviewmanager.cpp"