Make history view sortable.

This commit is contained in:
Florian Geyer 2012-05-30 16:37:46 +02:00
parent 2448976643
commit 9f1ab080ee
3 changed files with 23 additions and 4 deletions

View File

@ -26,6 +26,7 @@
#include <QtGui/QDesktopServices> #include <QtGui/QDesktopServices>
#include <QtGui/QStackedLayout> #include <QtGui/QStackedLayout>
#include <QtGui/QMessageBox> #include <QtGui/QMessageBox>
#include <QtGui/QSortFilterProxyModel>
#include "core/Database.h" #include "core/Database.h"
#include "core/Entry.h" #include "core/Entry.h"
@ -89,7 +90,15 @@ EditEntryWidget::EditEntryWidget(QWidget* parent)
connect(m_mainUi->passwordRepeatEdit, SIGNAL(textEdited(QString)), SLOT(setPasswordCheckColors())); connect(m_mainUi->passwordRepeatEdit, SIGNAL(textEdited(QString)), SLOT(setPasswordCheckColors()));
m_historyModel = new EntryHistoryModel(this); m_historyModel = new EntryHistoryModel(this);
m_historyUi->historyView->setModel(m_historyModel);
QSortFilterProxyModel* sortModel = new QSortFilterProxyModel(this);
sortModel->setSourceModel(m_historyModel);
sortModel->setDynamicSortFilter(true);
sortModel->setSortLocaleAware(true);
sortModel->setSortCaseSensitivity(Qt::CaseInsensitive);
sortModel->setSortRole(Qt::UserRole);
m_historyUi->historyView->setModel(sortModel);
m_historyUi->historyView->setRootIsDecorated(false); m_historyUi->historyView->setRootIsDecorated(false);
connect(m_historyUi->historyView, SIGNAL(activated(const QModelIndex&)), connect(m_historyUi->historyView, SIGNAL(activated(const QModelIndex&)),
@ -230,6 +239,7 @@ void EditEntryWidget::setForms(const Entry* entry, bool restore)
if (!m_history && !restore) { if (!m_history && !restore) {
m_historyModel->setEntries(entry->historyItems()); m_historyModel->setEntries(entry->historyItems());
m_historyUi->historyView->sortByColumn(0, Qt::DescendingOrder);
} }
if (m_historyModel->rowCount() > 0) { if (m_historyModel->rowCount() > 0) {
m_historyUi->deleteAllButton->setEnabled(true); m_historyUi->deleteAllButton->setEnabled(true);

View File

@ -12,7 +12,11 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<widget class="QTreeView" name="historyView"/> <widget class="QTreeView" name="historyView">
<property name="sortingEnabled">
<bool>true</bool>
</property>
</widget>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">

View File

@ -56,10 +56,15 @@ QVariant EntryHistoryModel::data(const QModelIndex& index, int role) const
TimeInfo timeInfo = entry->timeInfo(); TimeInfo timeInfo = entry->timeInfo();
QDateTime lastModificationLocalTime = timeInfo.lastModificationTime().toLocalTime(); QDateTime lastModificationLocalTime = timeInfo.lastModificationTime().toLocalTime();
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole || role == Qt::UserRole) {
switch (index.column()) { switch (index.column()) {
case 0: case 0:
return lastModificationLocalTime.toString(Qt::SystemLocaleShortDate); if (role == Qt::DisplayRole) {
return lastModificationLocalTime.toString(Qt::SystemLocaleShortDate);
}
else {
return lastModificationLocalTime;
}
case 1: case 1:
return entry->title(); return entry->title();
case 2: case 2: