File indexing completed on 2024-12-22 05:01:09

0001 /*
0002 This file is part of KMail, the KDE mail client.
0003 SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0004 
0005 SPDX-License-Identifier: GPL-2.0-only
0006 */
0007 
0008 #include "collectionswitchertreeviewmanager.h"
0009 #include "collectionswitchermodel.h"
0010 #include "collectionswitchertreeview.h"
0011 #include "kmail_debug.h"
0012 #include <QScrollBar>
0013 
0014 CollectionSwitcherTreeViewManager::CollectionSwitcherTreeViewManager(QObject *parent)
0015     : QObject{parent}
0016     , mCollectionSwitcherTreeView(new CollectionSwitcherTreeView(nullptr))
0017     , mCollectionSwitcherModel(new CollectionSwitcherModel(this))
0018 {
0019     mCollectionSwitcherTreeView->setModel(mCollectionSwitcherModel);
0020     connect(mCollectionSwitcherTreeView, &CollectionSwitcherTreeView::pressed, this, &CollectionSwitcherTreeViewManager::switchToCollectionClicked);
0021     connect(mCollectionSwitcherTreeView, &CollectionSwitcherTreeView::collectionSelected, this, &CollectionSwitcherTreeViewManager::activateCollection);
0022 }
0023 
0024 CollectionSwitcherTreeViewManager::~CollectionSwitcherTreeViewManager()
0025 {
0026     delete mCollectionSwitcherTreeView;
0027 }
0028 
0029 void CollectionSwitcherTreeViewManager::addActions(const QList<QAction *> &lst)
0030 {
0031     // Make sure that actions works when mCollectionSwitcherTreeView is show.
0032     mCollectionSwitcherTreeView->addActions(lst);
0033 }
0034 
0035 void CollectionSwitcherTreeViewManager::activateCollection(const QModelIndex &index)
0036 {
0037     Q_UNUSED(index)
0038     if (mCollectionSwitcherTreeView->selectionModel()->selectedRows().isEmpty()) {
0039         return;
0040     }
0041 
0042     const int row = mCollectionSwitcherTreeView->selectionModel()->selectedRows().first().row();
0043     const Akonadi::Collection col = mCollectionSwitcherModel->collection(row);
0044     Q_EMIT switchToFolder(col);
0045 
0046     mCollectionSwitcherTreeView->hide();
0047 }
0048 
0049 void CollectionSwitcherTreeViewManager::switchToCollectionClicked(const QModelIndex &index)
0050 {
0051     mCollectionSwitcherTreeView->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
0052     activateCollection(index);
0053 }
0054 
0055 QWidget *CollectionSwitcherTreeViewManager::parentWidget() const
0056 {
0057     return mParentWidget;
0058 }
0059 
0060 void CollectionSwitcherTreeViewManager::setParentWidget(QWidget *newParentWidget)
0061 {
0062     mParentWidget = newParentWidget;
0063 }
0064 
0065 void CollectionSwitcherTreeViewManager::selectCollection(const int from, const int to)
0066 {
0067     QModelIndex index;
0068     const int step = from < to ? 1 : -1;
0069     if (!mCollectionSwitcherTreeView->isVisible()) {
0070         updateViewGeometry();
0071         index = mCollectionSwitcherModel->index(from + step, 0);
0072         if (!index.isValid()) {
0073             index = mCollectionSwitcherModel->index(0, 0);
0074         }
0075         mCollectionSwitcherTreeView->show();
0076         mCollectionSwitcherTreeView->setFocus();
0077     } else {
0078         int newRow = mCollectionSwitcherTreeView->selectionModel()->currentIndex().row() + step;
0079         if (newRow == to + step) {
0080             newRow = from;
0081         }
0082         index = mCollectionSwitcherModel->index(newRow, 0);
0083     }
0084 
0085     mCollectionSwitcherTreeView->selectionModel()->select(index, QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect);
0086     mCollectionSwitcherTreeView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
0087 }
0088 
0089 CollectionSwitcherTreeView *CollectionSwitcherTreeViewManager::collectionSwitcherTreeView() const
0090 {
0091     return mCollectionSwitcherTreeView;
0092 }
0093 
0094 void CollectionSwitcherTreeViewManager::selectForward()
0095 {
0096     selectCollection(0, mCollectionSwitcherModel->rowCount() - 1);
0097 }
0098 
0099 void CollectionSwitcherTreeViewManager::selectBackward()
0100 {
0101     selectCollection(mCollectionSwitcherModel->rowCount() - 1, 0);
0102 }
0103 
0104 void CollectionSwitcherTreeViewManager::updateViewGeometry()
0105 {
0106     QWidget *window = mParentWidget ? mParentWidget->window() : nullptr;
0107     if (window) {
0108         const QSize centralSize = window->size();
0109 
0110         const QSize viewMaxSize(centralSize.width() * 3 / 4, centralSize.height() * 3 / 4);
0111 
0112         const int rowHeight = mCollectionSwitcherTreeView->sizeHintForRow(0);
0113         const int frameWidth = mCollectionSwitcherTreeView->frameWidth();
0114         const QSize viewSize(std::min(mCollectionSwitcherTreeView->sizeHintWidth() + 2 * frameWidth + mCollectionSwitcherTreeView->verticalScrollBar()->width(),
0115                                       viewMaxSize.width()),
0116                              std::min(std::max(rowHeight * mCollectionSwitcherModel->rowCount() + 2 * frameWidth, rowHeight * 6), viewMaxSize.height()));
0117 
0118         // Position should be central over the editor area, so map to global from
0119         // parent of central widget since the view is positioned in global coords
0120         const QPoint centralWidgetPos = window->parentWidget() ? window->mapToGlobal(window->pos()) : window->pos();
0121         const int xPos = std::max(0, centralWidgetPos.x() + (centralSize.width() - viewSize.width()) / 2);
0122         const int yPos = std::max(0, centralWidgetPos.y() + (centralSize.height() - viewSize.height()) / 2);
0123 
0124         mCollectionSwitcherTreeView->setFixedSize(viewSize);
0125         mCollectionSwitcherTreeView->move(xPos, yPos);
0126     } else {
0127         qCWarning(KMAIL_LOG) << "Problem mParentWidget is null! it's a bug";
0128     }
0129 }
0130 
0131 void CollectionSwitcherTreeViewManager::addHistory(const Akonadi::Collection &currentCol, const QString &fullPath)
0132 {
0133     mCollectionSwitcherModel->addHistory(currentCol, fullPath);
0134 }
0135 
0136 #include "moc_collectionswitchertreeviewmanager.cpp"