Skip to content
Snippets Groups Projects
Commit c40c1400 authored by Emmanuel Lepage Vallee's avatar Emmanuel Lepage Vallee Committed by gerrit2
Browse files

profile: Add a proxy to restore the account order

Previously, the profilemodel ignored the account order. However,
setting the account order/priority is still a necessary
feature when Ring is used with more advanced configurations.

This commit add a proxy and selection model with the restored
functionalities.

Issue: #79575
Change-Id: I72e95b4ac4ea7caf61ff19041509a39d635db586
parent 7561daf9
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "profilemodel.h" #include "profilemodel.h"
//Qt //Qt
#include <QtCore/QSortFilterProxyModel>
#include <QtCore/QTimer> #include <QtCore/QTimer>
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QDateTime> #include <QtCore/QDateTime>
...@@ -125,6 +126,7 @@ struct Node { ...@@ -125,6 +126,7 @@ struct Node {
Account* account; Account* account;
Person* contact; Person* contact;
int m_Index; int m_Index;
uint m_ParentIndex {(uint)-1}; //INT_MAX
}; };
bool ProfileEditor::save(const Person* contact) bool ProfileEditor::save(const Person* contact)
...@@ -338,6 +340,7 @@ void ProfileContentBackend::addAccount(Node* parent, Account* acc) ...@@ -338,6 +340,7 @@ void ProfileContentBackend::addAccount(Node* parent, Account* acc)
account_pro->parent = parent; account_pro->parent = parent;
account_pro->account = acc; account_pro->account = acc;
account_pro->m_Index = parent->children.size(); account_pro->m_Index = parent->children.size();
account_pro->m_ParentIndex = acc->index().row();
ProfileModel::instance()->beginInsertRows(ProfileModel::instance()->index(parent->m_Index,0), parent->children.size(), parent->children.size()); ProfileModel::instance()->beginInsertRows(ProfileModel::instance()->index(parent->m_Index,0), parent->children.size(), parent->children.size());
parent->children << account_pro; parent->children << account_pro;
...@@ -504,14 +507,20 @@ public: ...@@ -504,14 +507,20 @@ public:
ProfileContentBackend* m_pProfileBackend; ProfileContentBackend* m_pProfileBackend;
QStringList m_lMimes; QStringList m_lMimes;
QItemSelectionModel* m_pSelectionModel {nullptr}; QItemSelectionModel* m_pSelectionModel {nullptr};
QItemSelectionModel* m_pSortedProxySelectionModel {nullptr};
QSortFilterProxyModel* m_pSortedProxyModel {nullptr};
//Helpers //Helpers
void updateIndexes(); void updateIndexes();
void regenParentIndexes();
private Q_SLOTS: private Q_SLOTS:
void slotDataChanged(const QModelIndex& tl,const QModelIndex& br); void slotDataChanged(const QModelIndex& tl,const QModelIndex& br);
void slotLayoutchanged(); void slotLayoutchanged();
void slotDelayedInit(); void slotDelayedInit();
void slotRowsRemoved (const QModelIndex& index, int first, int last);
void slotRowsInserted(const QModelIndex& index, int first, int last);
void slotRowsMoved (const QModelIndex& index, int first, int last, const QModelIndex& newPar, int newIdx);
private: private:
ProfileModel* q_ptr; ProfileModel* q_ptr;
...@@ -526,6 +535,9 @@ ProfileModelPrivate::ProfileModelPrivate(ProfileModel* parent) : QObject(parent) ...@@ -526,6 +535,9 @@ ProfileModelPrivate::ProfileModelPrivate(ProfileModel* parent) : QObject(parent)
void ProfileModelPrivate::slotDelayedInit() void ProfileModelPrivate::slotDelayedInit()
{ {
connect(AccountModel::instance(),SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(slotDataChanged(QModelIndex,QModelIndex))); connect(AccountModel::instance(),SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(slotDataChanged(QModelIndex,QModelIndex)));
connect(AccountModel::instance(),&QAbstractItemModel::rowsRemoved, this, &ProfileModelPrivate::slotRowsRemoved);
connect(AccountModel::instance(),&QAbstractItemModel::rowsInserted, this, &ProfileModelPrivate::slotRowsInserted);
connect(AccountModel::instance(),&QAbstractItemModel::rowsMoved, this, &ProfileModelPrivate::slotRowsMoved);
connect(AccountModel::instance(),SIGNAL(layoutChanged()),this,SLOT(slotLayoutchanged())); connect(AccountModel::instance(),SIGNAL(layoutChanged()),this,SLOT(slotLayoutchanged()));
} }
...@@ -594,6 +606,10 @@ QVariant ProfileModel::data(const QModelIndex& index, int role ) const ...@@ -594,6 +606,10 @@ QVariant ProfileModel::data(const QModelIndex& index, int role ) const
//Accounts //Accounts
if (account_node->account) { if (account_node->account) {
switch(role) {
case 9999: //TODO add an Account:: role once rebased
return account_node->m_ParentIndex;
};
return account_node->account->roleData(role); return account_node->account->roleData(role);
} }
//Profiles //Profiles
...@@ -714,6 +730,34 @@ QItemSelectionModel* ProfileModel::selectionModel() const ...@@ -714,6 +730,34 @@ QItemSelectionModel* ProfileModel::selectionModel() const
return d_ptr->m_pSelectionModel; return d_ptr->m_pSelectionModel;
} }
QItemSelectionModel* ProfileModel::sortedProxySelectionModel() const
{
if (!d_ptr->m_pSortedProxySelectionModel) {
d_ptr->m_pSortedProxySelectionModel = new QItemSelectionModel(static_cast<QSortFilterProxyModel*>(sortedProxyModel()));
connect(d_ptr->m_pSortedProxySelectionModel, &QItemSelectionModel::currentChanged, [this](const QModelIndex& i) {
const QModelIndex& accIdx = mapToSource(
static_cast<QSortFilterProxyModel*>(sortedProxyModel())->mapToSource(i)
);
AccountModel::instance()->selectionModel()->setCurrentIndex(accIdx, QItemSelectionModel::ClearAndSelect);
});
}
return d_ptr->m_pSortedProxySelectionModel;
}
QAbstractItemModel* ProfileModel::sortedProxyModel() const
{
if (!d_ptr->m_pSortedProxyModel) {
d_ptr->m_pSortedProxyModel = new QSortFilterProxyModel(ProfileModel::instance());
d_ptr->m_pSortedProxyModel->setSourceModel(const_cast<ProfileModel*>(this));
d_ptr->m_pSortedProxyModel->setSortRole(9999);
d_ptr->m_pSortedProxyModel->sort(0);
}
return d_ptr->m_pSortedProxyModel;
}
void ProfileModelPrivate::updateIndexes() void ProfileModelPrivate::updateIndexes()
{ {
for (int i = 0; i < m_pProfileBackend->m_pEditor->m_lProfiles.size(); ++i) { for (int i = 0; i < m_pProfileBackend->m_pEditor->m_lProfiles.size(); ++i) {
...@@ -851,6 +895,45 @@ void ProfileModelPrivate::slotLayoutchanged() ...@@ -851,6 +895,45 @@ void ProfileModelPrivate::slotLayoutchanged()
emit q_ptr->layoutChanged(); emit q_ptr->layoutChanged();
} }
void ProfileModelPrivate::regenParentIndexes()
{
foreach(Node* n, m_pProfileBackend->m_pEditor->m_lProfiles) {
foreach(Node* a, n->children) {
a->m_ParentIndex = a->account->index().row();
}
const QModelIndex par = q_ptr->index(n->m_Index,0,QModelIndex());
emit q_ptr->dataChanged(q_ptr->index(0,0,par),q_ptr->index(n->children.size()-1,0,par));
}
}
void ProfileModelPrivate::slotRowsRemoved(const QModelIndex& index, int first, int last)
{
Q_UNUSED(index)
Q_UNUSED(first)
Q_UNUSED(last)
//TODO implement removing
regenParentIndexes();
}
void ProfileModelPrivate::slotRowsInserted(const QModelIndex& index, int first, int last)
{
Q_UNUSED(index)
Q_UNUSED(first)
Q_UNUSED(last)
//TODO implement insertion
regenParentIndexes();
}
void ProfileModelPrivate::slotRowsMoved(const QModelIndex& index, int first, int last, const QModelIndex& newPar, int newIdx)
{
Q_UNUSED(index)
Q_UNUSED(first)
Q_UNUSED(last)
Q_UNUSED(newPar)
Q_UNUSED(newIdx)
regenParentIndexes();
}
Person* ProfileModel::getPerson(const QModelIndex& idx) Person* ProfileModel::getPerson(const QModelIndex& idx)
{ {
if ((!idx.isValid()) || (idx.model() != this)) if ((!idx.isValid()) || (idx.model() != this))
......
...@@ -61,6 +61,8 @@ public: ...@@ -61,6 +61,8 @@ public:
QModelIndex mapFromSource(const QModelIndex& idx) const; QModelIndex mapFromSource(const QModelIndex& idx) const;
int acceptedPayloadTypes() const; int acceptedPayloadTypes() const;
QItemSelectionModel* selectionModel() const; QItemSelectionModel* selectionModel() const;
QItemSelectionModel* sortedProxySelectionModel() const;
QAbstractItemModel* sortedProxyModel() const;
Person* getPerson(const QModelIndex& idx); Person* getPerson(const QModelIndex& idx);
private: private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment