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 <QtGui/QSortFilterProxyModel>
#include "gui/EntryModel.h"
EntryView::EntryView(QWidget* parent)
: QTreeView(parent)
, 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);
setRootIsDecorated(false);
setDragEnabled(true);
setSortingEnabled(true);
connect(this, SIGNAL(activated(const QModelIndex&)), SLOT(emitEntryActivated(const QModelIndex&)));
connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SIGNAL(entrySelectionChanged()));
sortByColumn(0, Qt::AscendingOrder);
}
void EntryView::setGroup(Group* group)
@ -41,7 +52,7 @@ void EntryView::setGroup(Group* group)
void EntryView::emitEntryActivated(const QModelIndex& index)
{
Q_EMIT entryActivated(m_model->entryFromIndex(index));
Q_EMIT entryActivated(entryFromIndex(index));
}
void EntryView::setModel(QAbstractItemModel* model)
@ -53,7 +64,7 @@ void EntryView::setModel(QAbstractItemModel* model)
Entry* EntryView::currentEntry()
{
// TODO use selection instead of current?
return m_model->entryFromIndex(currentIndex());
return m_model->entryFromIndex(m_sortModel->mapToSource(currentIndex()));
}
bool EntryView::isSingleEntrySelected()
@ -63,5 +74,15 @@ bool EntryView::isSingleEntrySelected()
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 EntryModel;
class Group;
class QSortFilterProxyModel;
class EntryView : public QTreeView
{
@ -34,6 +35,7 @@ public:
Entry* currentEntry();
bool isSingleEntrySelected();
void setCurrentEntry(Entry* entry);
Entry* entryFromIndex(const QModelIndex& index);
public Q_SLOTS:
void setGroup(Group* group);
@ -47,6 +49,7 @@ Q_SIGNALS:
private:
EntryModel* const m_model;
QSortFilterProxyModel* const m_sortModel;
};
#endif // KEEPASSX_ENTRYVIEW_H

View File

@ -33,7 +33,6 @@
#include "gui/DatabaseWidget.h"
#include "gui/EditEntryWidget.h"
#include "gui/EntryView.h"
#include "gui/EntryModel.h"
#include "gui/FileDialog.h"
#include "gui/MainWindow.h"
@ -128,7 +127,7 @@ void TestGui::testAddEntry()
QCOMPARE(dbWidget->currentMode(), DatabaseWidget::ViewMode);
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->historyItems().size(), 0);