Skip to content

Commit

Permalink
Merge pull request #226 from Sriep/issue-207
Browse files Browse the repository at this point in the history
feature request: in MMM, sort credentials by modified date feature request good first issue help wanted #207
  • Loading branch information
limpkin authored Feb 27, 2018
2 parents dd7492d + dc271c0 commit 342ac81
Show file tree
Hide file tree
Showing 18 changed files with 439 additions and 114 deletions.
9 changes: 9 additions & 0 deletions src/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,14 @@ Q_DECLARE_METATYPE(Common::MPHwVersion)
"background-color: #237C95;" \
"}"

#define CSS_CREDVIEW_HEADER "QHeaderView::section {" \
"background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,"\
"stop:0 #F1F1F1, stop: 0.5 #E0E0E0,"\
"stop: 0.6 #D3D3D3, stop:1 #F5F5F5);"\
"color: black;"\
"padding-left: 4px;"\
"border: 1px solid #FcFcFc;"\
"}"

#endif // COMMON_H

67 changes: 43 additions & 24 deletions src/CredentialModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QColor>
#include <QFont>
#include <QApplication>
#include <QBrush>

// Application
#include "CredentialModel.h"
Expand Down Expand Up @@ -35,13 +36,15 @@ QVariant CredentialModel::data(const QModelIndex &idx, int role) const
// Cast to login item
LoginItem *pLoginItem = getLoginItemByIndex(idx);

if (role == Qt::DisplayRole)
return pItem->name();

if (role == Qt::ForegroundRole)
if (role == Qt::DisplayRole && idx.column() == 0)
{
if (pServiceItem != nullptr)
return pItem->name();
}
else if (role == Qt::DisplayRole && idx.column() == 1)
{
if (pLoginItem != nullptr)
return QColor("#3D96AF");
return pItem->updatedDate();
}

if (role == Qt::FontRole)
Expand All @@ -53,21 +56,42 @@ QVariant CredentialModel::data(const QModelIndex &idx, int role) const
font.setPointSize(10);
return font;
}
else
if (pLoginItem != nullptr)

return qApp->font();
}

if (Qt::TextAlignmentRole == role)
{
if (pLoginItem != nullptr && idx.column() == 1)
{
QFont font = qApp->font();
font.setBold(true);
font.setItalic(true);
font.setPointSize(10);
return font;
return Qt::AlignHCenter;
}
return qApp->font();
}

if (role == Qt::DecorationRole) {
if (pLoginItem != nullptr)
return loginItemIcon;
return QVariant();
}

QVariant CredentialModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (Qt::DisplayRole == role && orientation == Qt::Horizontal)
{
switch (section)
{
case 0:
return QString(" Service or Website");
case 1:
return QString("Modified Date");
default:
return QVariant();
}
}

if (Qt::TextAlignmentRole == role)
{
if (section == 1)
{
return Qt::AlignHCenter;
}
}

return QVariant();
Expand Down Expand Up @@ -131,7 +155,7 @@ int CredentialModel::rowCount(const QModelIndex &parent) const
int CredentialModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
return 2;
}

TreeItem *CredentialModel::getItemByIndex(const QModelIndex &idx) const
Expand Down Expand Up @@ -230,9 +254,9 @@ ServiceItem *CredentialModel::addService(const QString &sServiceName)
return new ServiceItem(sServiceName);
}

QModelIndex CredentialModel::getServiceIndexByName(const QString &sServiceName) const
QModelIndex CredentialModel::getServiceIndexByName(const QString &sServiceName, int column) const
{
QModelIndexList lMatches = match(index(0, 0, QModelIndex()), Qt::DisplayRole, sServiceName, 1);
QModelIndexList lMatches = match(index(0, column, QModelIndex()), Qt::DisplayRole, sServiceName, 1);
if (!lMatches.isEmpty())
return lMatches.first();

Expand All @@ -249,11 +273,6 @@ ServiceItem *CredentialModel::getServiceItemByIndex(const QModelIndex &idx) cons
return dynamic_cast<ServiceItem *>(getItemByIndex(idx));
}

void CredentialModel::setLoginItemIcon(const QIcon &icon)
{
loginItemIcon = icon;
}

void CredentialModel::updateLoginItem(const QModelIndex &idx, const QString &sPassword, const QString &sDescription, const QString &sName)
{
// Retrieve item
Expand Down
5 changes: 2 additions & 3 deletions src/CredentialModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CredentialModel : public QAbstractItemModel
CredentialModel(QObject *parent=nullptr);
~CredentialModel();
virtual QVariant data(const QModelIndex &idx, int role) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
virtual Qt::ItemFlags flags(const QModelIndex &idx) const;
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
virtual QModelIndex parent(const QModelIndex &idx) const;
Expand All @@ -47,17 +48,15 @@ class CredentialModel : public QAbstractItemModel
void updateLoginItem(const QModelIndex &idx, const QString &sPassword, const QString &sDescription, const QString &sName);
void updateLoginItem(const QModelIndex &idx, const ItemRole &role, const QVariant &vValue);
void clear();
QModelIndex getServiceIndexByName(const QString &sServiceName) const;
QModelIndex getServiceIndexByName(const QString &sServiceName, int column = 0) const;
LoginItem *getLoginItemByIndex(const QModelIndex &idx) const;
ServiceItem *getServiceItemByIndex(const QModelIndex &idx) const;

void setLoginItemIcon(const QIcon &icon);
private:
ServiceItem *addService(const QString &sServiceName);

private:
RootItem *m_pRootItem;
QIcon loginItemIcon;

signals:
void modelLoaded(bool bClearLoginDescription);
Expand Down
90 changes: 88 additions & 2 deletions src/CredentialModelFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
CredentialModelFilter::CredentialModelFilter(QObject *parent) : QSortFilterProxyModel(parent)
{
setDynamicSortFilter(true);
sort(0);
}

CredentialModelFilter::~CredentialModelFilter()
Expand Down Expand Up @@ -35,6 +34,51 @@ const TreeItem *CredentialModelFilter::getItemByProxyIndex(const QModelIndex &pr
return pSrcModel->getItemByIndex(srcIndex);
}

void CredentialModelFilter::sort(int column, Qt::SortOrder order)
{
// sort order is not passed as a parameter to lessThan so need
// to save sort order globally.
tempSortOrder = order;
return QSortFilterProxyModel::sort(column, order);
}

QModelIndexList CredentialModelFilter::getNextRow(const QModelIndex& rowIdx)
{
QModelIndexList nextRow;
QModelIndex parent = rowIdx.parent();
if (!rowIdx.isValid())
return nextRow;

if (rowCount(parent) > rowIdx.row()+1)
{
nextRow << index(rowIdx.row()+1, 0, parent);
nextRow << index(rowIdx.row()+1, 1, parent);
}
else if (parent.isValid())
{
if (rowCount(parent.parent()) > parent.row()+1)
{
QModelIndex nextParent = index(parent.row()+1, 0, parent.parent());
nextRow << index(0, 0, nextParent);
nextRow << index(0, 1, nextParent);
}
else if (rowIdx.row() > 0)
{
nextRow << index(rowIdx.row()-1, 0, parent);
nextRow << index(rowIdx.row()-1, 1, parent);

}
else if (parent.row() > 0)
{
QModelIndex prevousParent = index(parent.row()-1, 0, parent.parent());
nextRow << index(rowCount(prevousParent)-1, 0, prevousParent);
nextRow << index(rowCount(prevousParent)-1, 1, prevousParent);
}
}

return nextRow;
}

bool CredentialModelFilter::filterAcceptsRow(int iSrcRow, const QModelIndex &srcParent) const
{
// Get source index
Expand Down Expand Up @@ -103,8 +147,50 @@ bool CredentialModelFilter::lessThan(const QModelIndex &srcLeft, const QModelInd
TreeItem *pRightItem = pSrcModel->getItemByIndex(srcRight);

if ((pLeftItem != nullptr) && (pRightItem != nullptr))
return pLeftItem->name() < pRightItem->name();
{
if (srcLeft.column() == 0 && srcRight.column() == 0)
{
return pLeftItem->name() < pRightItem->name();
}

if ((srcLeft.column() == 1 && srcRight.column() == 1))
{
return pLeftItem->bestUpdateDate(tempSortOrder)
< pRightItem->bestUpdateDate(tempSortOrder);
}
}

return false;
}

QModelIndex CredentialModelFilter::getProxyIndexFromItem(TreeItem *pItem, int column)
{
CredentialModel *pCredModel = dynamic_cast<CredentialModel *>(sourceModel());
switch (pItem->treeType())
{
case TreeItem::TreeType::Service:
{
QModelIndex serviceIndex = pCredModel->getServiceIndexByName(pItem->name(), column);
return mapFromSource(serviceIndex);
}
case TreeItem::TreeType::Login:
{
QModelIndex parentIndex = getProxyIndexFromItem(pItem->parentItem());

for (int i = 0; i < rowCount(parentIndex); i ++)
{
QModelIndex loginIndex = index(i, column, parentIndex);
if (loginIndex.isValid())
{
TreeItem *pLoginItem = getItemByProxyIndex(loginIndex);
if (pLoginItem->name() == pItem->name())
return loginIndex;
}
}
return QModelIndex();
}
case TreeItem::TreeType::Root:
default:
return QModelIndex();
}
}
4 changes: 4 additions & 0 deletions src/CredentialModelFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class CredentialModelFilter : public QSortFilterProxyModel
void setFilter(const QString &sFilter);
TreeItem *getItemByProxyIndex(const QModelIndex &proxyIndex);
const TreeItem *getItemByProxyIndex(const QModelIndex &proxyIndex) const;
QModelIndex getProxyIndexFromItem(TreeItem* pItem, int column = 0);
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) Q_DECL_OVERRIDE;
QModelIndexList getNextRow(const QModelIndex &rowIdx);

protected:
virtual bool filterAcceptsRow(int iSrcRow, const QModelIndex &srcParent) const;
Expand All @@ -28,6 +31,7 @@ class CredentialModelFilter : public QSortFilterProxyModel

private:
QString m_sFilter;
Qt::SortOrder tempSortOrder;
};

#endif // CREDENTIALMODELFILTER_H
Loading

0 comments on commit 342ac81

Please sign in to comment.