Commit 399ee267 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Use collectionswitchertreeviewmanager

parent 8f2b59f5
Pipeline #97948 canceled with stage
......@@ -39,3 +39,16 @@ QVariant CollectionSwitcherModel::data(const QModelIndex &index, int role) const
}
return {};
}
void CollectionSwitcherModel::addHistory(const Akonadi::Collection &currentCol, const QString &fullPath)
{
mCollectionsInfo.append({currentCol, fullPath});
}
const Akonadi::Collection CollectionSwitcherModel::collection(int index)
{
if (index < 0 || index >= mCollectionsInfo.count()) {
return {};
}
return mCollectionsInfo.at(index).mNewCollection;
}
......@@ -19,6 +19,13 @@ public:
};
struct CollectionInfo {
CollectionInfo(){};
CollectionInfo(const Akonadi::Collection &col, const QString &path)
: mNewCollection(col)
, mFullPath(path)
{
}
const Akonadi::Collection mNewCollection;
QString mFullPath;
};
......@@ -29,6 +36,10 @@ public:
Q_REQUIRED_RESULT int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role) const override;
void addHistory(const Akonadi::Collection &currentCol, const QString &fullPath = {});
Q_REQUIRED_RESULT const Akonadi::Collection collection(int index);
private:
QVector<CollectionInfo> mCollectionsInfo;
};
......
......@@ -8,6 +8,8 @@ SPDX-License-Identifier: GPL-2.0-only
#include "collectionswitchertreeviewmanager.h"
#include "collectionswitchermodel.h"
#include "collectionswitchertreeview.h"
#include "kmail_debug.h"
#include <QScrollBar>
CollectionSwitcherTreeViewManager::CollectionSwitcherTreeViewManager(QObject *parent)
: QObject{parent}
......@@ -21,6 +23,13 @@ CollectionSwitcherTreeViewManager::CollectionSwitcherTreeViewManager(QObject *pa
CollectionSwitcherTreeViewManager::~CollectionSwitcherTreeViewManager()
{
delete mCollectionSwitcherTreeView;
}
void CollectionSwitcherTreeViewManager::addActions(const QList<QAction *> &lst)
{
// Make sure that actions works when mCollectionSwitcherTreeView is show.
mCollectionSwitcherTreeView->addActions(lst);
}
void CollectionSwitcherTreeViewManager::activateCollection(const QModelIndex &index)
......@@ -31,7 +40,8 @@ void CollectionSwitcherTreeViewManager::activateCollection(const QModelIndex &in
}
const int row = mCollectionSwitcherTreeView->selectionModel()->selectedRows().first().row();
// TODO activate it. Q_EMIT switchToFolder(...)
const Akonadi::Collection col = mCollectionSwitcherModel->collection(row);
Q_EMIT switchToFolder(col);
mCollectionSwitcherTreeView->hide();
}
......@@ -51,3 +61,69 @@ void CollectionSwitcherTreeViewManager::setParentWidget(QWidget *newParentWidget
{
mParentWidget = newParentWidget;
}
void CollectionSwitcherTreeViewManager::selectCollection(const int from, const int to)
{
QModelIndex index;
const int step = from < to ? 1 : -1;
if (!mCollectionSwitcherTreeView->isVisible()) {
updateViewGeometry();
index = mCollectionSwitcherModel->index(from + step, 0);
if (!index.isValid()) {
index = mCollectionSwitcherModel->index(0, 0);
}
mCollectionSwitcherTreeView->show();
mCollectionSwitcherTreeView->setFocus();
} else {
int newRow = mCollectionSwitcherTreeView->selectionModel()->currentIndex().row() + step;
if (newRow == to + step) {
newRow = from;
}
index = mCollectionSwitcherModel->index(newRow, 0);
}
mCollectionSwitcherTreeView->selectionModel()->select(index, QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect);
mCollectionSwitcherTreeView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
}
void CollectionSwitcherTreeViewManager::selectForward()
{
selectCollection(0, mCollectionSwitcherModel->rowCount() - 1);
}
void CollectionSwitcherTreeViewManager::selectBackward()
{
selectCollection(mCollectionSwitcherModel->rowCount() - 1, 0);
}
void CollectionSwitcherTreeViewManager::updateViewGeometry()
{
QWidget *window = mParentWidget ? mParentWidget->window() : nullptr;
if (window) {
const QSize centralSize = window->size();
const QSize viewMaxSize(centralSize.width() * 3 / 4, centralSize.height() * 3 / 4);
const int rowHeight = mCollectionSwitcherTreeView->sizeHintForRow(0);
const int frameWidth = mCollectionSwitcherTreeView->frameWidth();
const QSize viewSize(std::min(mCollectionSwitcherTreeView->sizeHintWidth() + 2 * frameWidth + mCollectionSwitcherTreeView->verticalScrollBar()->width(),
viewMaxSize.width()),
std::min(std::max(rowHeight * mCollectionSwitcherModel->rowCount() + 2 * frameWidth, rowHeight * 6), viewMaxSize.height()));
// Position should be central over the editor area, so map to global from
// parent of central widget since the view is positioned in global coords
const QPoint centralWidgetPos = window->parentWidget() ? window->mapToGlobal(window->pos()) : window->pos();
const int xPos = std::max(0, centralWidgetPos.x() + (centralSize.width() - viewSize.width()) / 2);
const int yPos = std::max(0, centralWidgetPos.y() + (centralSize.height() - viewSize.height()) / 2);
mCollectionSwitcherTreeView->setFixedSize(viewSize);
mCollectionSwitcherTreeView->move(xPos, yPos);
} else {
qCWarning(KMAIL_LOG) << "Problem mParentWidget is null! it's a bug";
}
}
void CollectionSwitcherTreeViewManager::addHistory(const Akonadi::Collection &currentCol, const QString &fullPath)
{
mCollectionSwitcherModel->addHistory(currentCol, fullPath);
}
......@@ -7,9 +7,11 @@ SPDX-License-Identifier: GPL-2.0-only
#pragma once
#include <Akonadi/Collection>
#include <QList>
#include <QObject>
class CollectionSwitcherTreeView;
class CollectionSwitcherModel;
class QAction;
class CollectionSwitcherTreeViewManager : public QObject
{
Q_OBJECT
......@@ -17,15 +19,24 @@ public:
explicit CollectionSwitcherTreeViewManager(QObject *parent = nullptr);
~CollectionSwitcherTreeViewManager() override;
void addActions(const QList<QAction *> &lst);
Q_REQUIRED_RESULT QWidget *parentWidget() const;
void setParentWidget(QWidget *newParentWidget);
void selectForward();
void selectBackward();
void updateViewGeometry();
void addHistory(const Akonadi::Collection &currentCol, const QString &fullPath = {});
Q_SIGNALS:
void switchToFolder(const Akonadi::Collection &col);
private:
void activateCollection(const QModelIndex &index);
void switchToCollectionClicked(const QModelIndex &index);
void selectCollection(const int from, const int to);
QWidget *mParentWidget = nullptr;
CollectionSwitcherTreeView *const mCollectionSwitcherTreeView;
CollectionSwitcherModel *const mCollectionSwitcherModel;
......
......@@ -176,6 +176,7 @@
#include <MessageViewer/DKIMWidgetInfo>
#include <MessageViewer/RemoteContentMenu>
#include "historyswitchfolder/collectionswitchertreeviewmanager.h"
#include "plugininterface/kmailplugincheckbeforedeletingmanagerinterface.h"
#ifdef WITH_KUSERFEEDBACK
......@@ -202,6 +203,7 @@ KMMainWidget::KMMainWidget(QWidget *parent, KXMLGUIClient *aGUIClient, KActionCo
, mLaunchExternalComponent(new KMLaunchExternalComponent(this, this))
, mManageShowCollectionProperties(new ManageShowCollectionProperties(this, this))
, mHistorySwitchFolderManager(new HistorySwitchFolderManager(this))
, mCollectionSwitcherTreeViewManager(new CollectionSwitcherTreeViewManager(this))
{
// must be the first line of the constructor:
mStartupDone = false;
......@@ -338,6 +340,7 @@ KMMainWidget::KMMainWidget(QWidget *parent, KXMLGUIClient *aGUIClient, KActionCo
setupUnifiedMailboxChecker();
connect(mHistorySwitchFolderManager, &HistorySwitchFolderManager::switchToFolder, this, &KMMainWidget::slotHistorySwitchFolder);
mCollectionSwitcherTreeViewManager->setParentWidget(this);
}
QWidget *KMMainWidget::dkimWidgetInfo() const
......@@ -485,6 +488,7 @@ void KMMainWidget::slotFolderChanged(const Akonadi::Collection &collection)
return;
}
mHistorySwitchFolderManager->addHistory(mCurrentCollection, collection, MailCommon::Util::fullCollectionPath(collection));
mCollectionSwitcherTreeViewManager->addHistory(collection, MailCommon::Util::fullCollectionPath(collection));
}
void KMMainWidget::slotHistorySwitchFolder(const Akonadi::Collection &collection)
......@@ -3525,19 +3529,36 @@ void KMMainWidget::setupActions()
actionCollection()->addAction(QStringLiteral("resource_restart"), mRestartAccountSettings);
connect(mRestartAccountSettings, &QAction::triggered, this, &KMMainWidget::slotRestartAccount);
{
QList<QAction *> listActions;
auto act = new QAction(i18n("Previous Selected Folder"), this); // TODO fix me i18n
actionCollection()->setDefaultShortcut(act, QKeySequence(Qt::CTRL | Qt::Key_Tab));
actionCollection()->addAction(QStringLiteral("previous_folder"), act);
listActions.append(act);
connect(act, &QAction::triggered, mHistorySwitchFolderManager, &HistorySwitchFolderManager::undo);
connect(act, &QAction::triggered, this, &KMMainWidget::undoSwitchFolder);
act = new QAction(i18n("Next Selected Folder"), this); // TODO fix me i18n
actionCollection()->addAction(QStringLiteral("next_folder"), act);
actionCollection()->setDefaultShortcut(act, QKeySequence(Qt::SHIFT | Qt::Key_Tab | Qt::CTRL));
connect(act, &QAction::triggered, mHistorySwitchFolderManager, &HistorySwitchFolderManager::redo);
connect(act, &QAction::triggered, this, &KMMainWidget::redoSwitchFolder);
listActions.append(act);
mCollectionSwitcherTreeViewManager->addActions(listActions);
}
}
void KMMainWidget::redoSwitchFolder()
{
mHistorySwitchFolderManager->redo();
mCollectionSwitcherTreeViewManager->selectBackward();
}
void KMMainWidget::undoSwitchFolder()
{
mHistorySwitchFolderManager->undo();
mCollectionSwitcherTreeViewManager->selectForward();
}
void KMMainWidget::slotAddFavoriteFolder()
{
if (!mFavoritesModel) {
......
......@@ -92,6 +92,7 @@ class MailFilter;
class QStatusBar;
class KMailPluginCheckBeforeDeletingManagerInterface;
class HistorySwitchFolderManager;
class CollectionSwitcherTreeViewManager;
class KMAIL_EXPORT KMMainWidget : public QWidget
{
Q_OBJECT
......@@ -530,6 +531,8 @@ private:
void slotAccountSettings();
void updateDisplayFormatMessage();
void slotHistorySwitchFolder(const Akonadi::Collection &collection);
void redoSwitchFolder();
void undoSwitchFolder();
// Message actions
QAction *mDeleteAction = nullptr;
......@@ -654,5 +657,6 @@ private:
#endif
KMailPluginCheckBeforeDeletingManagerInterface *mPluginCheckBeforeDeletingManagerInterface = nullptr;
HistorySwitchFolderManager *const mHistorySwitchFolderManager;
CollectionSwitcherTreeViewManager *const mCollectionSwitcherTreeViewManager;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment