Make EntryView sortable.

This commit is contained in:
Felix Geyer 2012-05-11 12:01:01 +02:00
parent 44489bf6f8
commit 8faac078fd
3 changed files with 29 additions and 6 deletions

View File

@ -17,20 +17,31 @@
#include "EntryView.h" #include "EntryView.h"
#include <QtGui/QSortFilterProxyModel>
#include "gui/EntryModel.h" #include "gui/EntryModel.h"
EntryView::EntryView(QWidget* parent) EntryView::EntryView(QWidget* parent)
: QTreeView(parent) : QTreeView(parent)
, m_model(new EntryModel(this)) , m_model(new EntryModel(this))
, m_sortModel(new QSortFilterProxyModel(this))
{ {
QTreeView::setModel(m_model); m_sortModel->setSourceModel(m_model);
m_sortModel->setDynamicSortFilter(true);
m_sortModel->setSortLocaleAware(true);
m_sortModel->setSortCaseSensitivity(Qt::CaseInsensitive);
m_sortModel->setSupportedDragActions(m_model->supportedDragActions());
QTreeView::setModel(m_sortModel);
setUniformRowHeights(true); setUniformRowHeights(true);
setRootIsDecorated(false); setRootIsDecorated(false);
setDragEnabled(true); setDragEnabled(true);
setSortingEnabled(true);
connect(this, SIGNAL(activated(const QModelIndex&)), SLOT(emitEntryActivated(const QModelIndex&))); connect(this, SIGNAL(activated(const QModelIndex&)), SLOT(emitEntryActivated(const QModelIndex&)));
connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SIGNAL(entrySelectionChanged())); connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SIGNAL(entrySelectionChanged()));
sortByColumn(0, Qt::AscendingOrder);
} }
void EntryView::setGroup(Group* group) void EntryView::setGroup(Group* group)
@ -41,7 +52,7 @@ void EntryView::setGroup(Group* group)
void EntryView::emitEntryActivated(const QModelIndex& index) void EntryView::emitEntryActivated(const QModelIndex& index)
{ {
Q_EMIT entryActivated(m_model->entryFromIndex(index)); Q_EMIT entryActivated(entryFromIndex(index));
} }
void EntryView::setModel(QAbstractItemModel* model) void EntryView::setModel(QAbstractItemModel* model)
@ -53,7 +64,7 @@ void EntryView::setModel(QAbstractItemModel* model)
Entry* EntryView::currentEntry() Entry* EntryView::currentEntry()
{ {
// TODO use selection instead of current? // TODO use selection instead of current?
return m_model->entryFromIndex(currentIndex()); return m_model->entryFromIndex(m_sortModel->mapToSource(currentIndex()));
} }
bool EntryView::isSingleEntrySelected() bool EntryView::isSingleEntrySelected()
@ -63,5 +74,15 @@ bool EntryView::isSingleEntrySelected()
void EntryView::setCurrentEntry(Entry* entry) void EntryView::setCurrentEntry(Entry* entry)
{ {
setCurrentIndex(m_model->indexFromEntry(entry)); setCurrentIndex(m_sortModel->mapFromSource(m_model->indexFromEntry(entry)));
}
Entry* EntryView::entryFromIndex(const QModelIndex& index)
{
if (index.isValid()) {
return m_model->entryFromIndex(m_sortModel->mapToSource(index));
}
else {
return 0;
}
} }

View File

@ -23,6 +23,7 @@
class Entry; class Entry;
class EntryModel; class EntryModel;
class Group; class Group;
class QSortFilterProxyModel;
class EntryView : public QTreeView class EntryView : public QTreeView
{ {
@ -34,6 +35,7 @@ public:
Entry* currentEntry(); Entry* currentEntry();
bool isSingleEntrySelected(); bool isSingleEntrySelected();
void setCurrentEntry(Entry* entry); void setCurrentEntry(Entry* entry);
Entry* entryFromIndex(const QModelIndex& index);
public Q_SLOTS: public Q_SLOTS:
void setGroup(Group* group); void setGroup(Group* group);
@ -47,6 +49,7 @@ Q_SIGNALS:
private: private:
EntryModel* const m_model; EntryModel* const m_model;
QSortFilterProxyModel* const m_sortModel;
}; };
#endif // KEEPASSX_ENTRYVIEW_H #endif // KEEPASSX_ENTRYVIEW_H

View File

@ -33,7 +33,6 @@
#include "gui/DatabaseWidget.h" #include "gui/DatabaseWidget.h"
#include "gui/EditEntryWidget.h" #include "gui/EditEntryWidget.h"
#include "gui/EntryView.h" #include "gui/EntryView.h"
#include "gui/EntryModel.h"
#include "gui/FileDialog.h" #include "gui/FileDialog.h"
#include "gui/MainWindow.h" #include "gui/MainWindow.h"
@ -128,7 +127,7 @@ void TestGui::testAddEntry()
QCOMPARE(dbWidget->currentMode(), DatabaseWidget::ViewMode); QCOMPARE(dbWidget->currentMode(), DatabaseWidget::ViewMode);
QModelIndex item = entryView->model()->index(1, 0); QModelIndex item = entryView->model()->index(1, 0);
Entry* entry = static_cast<EntryModel*>(entryView->model())->entryFromIndex(item); Entry* entry = entryView->entryFromIndex(item);
QCOMPARE(entry->title(), QString("test")); QCOMPARE(entry->title(), QString("test"));
QCOMPARE(entry->historyItems().size(), 0); QCOMPARE(entry->historyItems().size(), 0);