Commit d17edf9b authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Add model

parent f70e72aa
......@@ -309,6 +309,8 @@ target_sources(kmailprivate PRIVATE
historyswitchfolder/collectionswitchertreeview.h
historyswitchfolder/collectionswitchertreeviewmanager.h
historyswitchfolder/collectionswitchertreeviewmanager.cpp
historyswitchfolder/collectionswitchermodel.h
historyswitchfolder/collectionswitchermodel.cpp
)
qt_add_dbus_adaptor(kmailprivate_LIB_SRCS
......
/*
This file is part of KMail, the KDE mail client.
SPDX-FileCopyrightText: 2021 Laurent Montel <montel@kde.org>
SPDX-License-Identifier: GPL-2.0-only
*/
#include "collectionswitchermodel.h"
CollectionSwitcherModel::CollectionSwitcherModel(QObject *parent)
: QAbstractListModel(parent)
{
}
CollectionSwitcherModel::~CollectionSwitcherModel()
{
}
int CollectionSwitcherModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0; // flat model
}
return mCollectionsInfo.count();
}
QVariant CollectionSwitcherModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= mCollectionsInfo.count()) {
return {};
}
const CollectionInfo cat = mCollectionsInfo.at(index.row());
#if 0 // TODO implement it.
switch (role) {
case Name:
return cat.name();
case Category:
return cat.category();
}
#endif
return {};
}
/*
This file is part of KMail, the KDE mail client.
SPDX-FileCopyrightText: 2021 Laurent Montel <montel@kde.org>
SPDX-License-Identifier: GPL-2.0-only
*/
#pragma once
#include <Akonadi/Collection>
#include <QAbstractListModel>
#include <QVector>
class CollectionSwitcherModel : public QAbstractListModel
{
Q_OBJECT
public:
struct CollectionInfo {
const Akonadi::Collection mNewCollection;
QString mFullPath;
};
explicit CollectionSwitcherModel(QObject *parent = nullptr);
~CollectionSwitcherModel() override;
Q_REQUIRED_RESULT int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role) const override;
private:
QVector<CollectionInfo> mCollectionsInfo;
};
Q_DECLARE_METATYPE(CollectionSwitcherModel::CollectionInfo)
Q_DECLARE_TYPEINFO(CollectionSwitcherModel::CollectionInfo, Q_MOVABLE_TYPE);
......@@ -6,16 +6,42 @@ SPDX-License-Identifier: GPL-2.0-only
*/
#include "collectionswitchertreeviewmanager.h"
#include "collectionswitchermodel.h"
#include "collectionswitchertreeview.h"
CollectionSwitcherTreeViewManager::CollectionSwitcherTreeViewManager(QObject *parent)
: QObject{parent}
, mCollectionSwitcherTreeView(new CollectionSwitcherTreeView(nullptr)) // TODO use parent ???
, mCollectionSwitcherModel(new CollectionSwitcherModel(this))
{
mCollectionSwitcherTreeView->setModel(mCollectionSwitcherModel);
connect(mCollectionSwitcherTreeView, &CollectionSwitcherTreeView::pressed, this, &CollectionSwitcherTreeViewManager::switchToCollectionClicked);
connect(mCollectionSwitcherTreeView, &CollectionSwitcherTreeView::collectionSelected, this, &CollectionSwitcherTreeViewManager::activateCollection);
}
CollectionSwitcherTreeViewManager::~CollectionSwitcherTreeViewManager()
{
}
void CollectionSwitcherTreeViewManager::activateCollection(const QModelIndex &index)
{
Q_UNUSED(index)
if (mCollectionSwitcherTreeView->selectionModel()->selectedRows().isEmpty()) {
return;
}
const int row = mCollectionSwitcherTreeView->selectionModel()->selectedRows().first().row();
// TODO activate it. Q_EMIT switchToFolder(...)
mCollectionSwitcherTreeView->hide();
}
void CollectionSwitcherTreeViewManager::switchToCollectionClicked(const QModelIndex &index)
{
mCollectionSwitcherTreeView->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
activateCollection(index);
}
QWidget *CollectionSwitcherTreeViewManager::parentWidget() const
{
return mParentWidget;
......
......@@ -6,8 +6,10 @@ SPDX-License-Identifier: GPL-2.0-only
*/
#pragma once
#include <Akonadi/Collection>
#include <QObject>
class CollectionSwitcherTreeView;
class CollectionSwitcherModel;
class CollectionSwitcherTreeViewManager : public QObject
{
Q_OBJECT
......@@ -18,6 +20,13 @@ public:
Q_REQUIRED_RESULT QWidget *parentWidget() const;
void setParentWidget(QWidget *newParentWidget);
Q_SIGNALS:
void switchToFolder(const Akonadi::Collection &col);
private:
void activateCollection(const QModelIndex &index);
void switchToCollectionClicked(const QModelIndex &index);
QWidget *mParentWidget = nullptr;
CollectionSwitcherTreeView *const mCollectionSwitcherTreeView;
CollectionSwitcherModel *const mCollectionSwitcherModel;
};
Supports Markdown
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