Clean up Entry Model/View code

This commit is contained in:
Jonathan White 2018-03-21 21:52:57 -04:00
parent d8d758f0e1
commit 4b57fcb563
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
7 changed files with 37 additions and 72 deletions

View File

@ -100,7 +100,7 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent)
m_entryView = new EntryView(rightHandSideWidget);
m_entryView->setObjectName("entryView");
m_entryView->setContextMenuPolicy(Qt::CustomContextMenu);
m_entryView->setGroup(db->rootGroup());
m_entryView->displayGroup(db->rootGroup());
connect(m_entryView, SIGNAL(customContextMenuRequested(QPoint)), SLOT(emitEntryContextMenuRequested(QPoint)));
// Add a notification for when we are searching
@ -291,7 +291,7 @@ bool DatabaseWidget::isUsernamesHidden() const
/**
* Set state of entry view 'Hide Usernames' setting
*/
void DatabaseWidget::setUsernamesHidden(const bool hide)
void DatabaseWidget::setUsernamesHidden(bool hide)
{
m_entryView->setUsernamesHidden(hide);
}
@ -307,7 +307,7 @@ bool DatabaseWidget::isPasswordsHidden() const
/**
* Set state of entry view 'Hide Passwords' setting
*/
void DatabaseWidget::setPasswordsHidden(const bool hide)
void DatabaseWidget::setPasswordsHidden(bool hide)
{
m_entryView->setPasswordsHidden(hide);
}
@ -1018,7 +1018,7 @@ void DatabaseWidget::search(const QString& searchtext)
QList<Entry*> searchResult = EntrySearcher().search(searchtext, searchGroup, caseSensitive);
m_entryView->setEntryList(searchResult);
m_entryView->displaySearch(searchResult);
m_lastSearchText = searchtext;
// Display a label detailing our search results
@ -1054,7 +1054,7 @@ void DatabaseWidget::onGroupChanged(Group* group)
// Otherwise cancel search
emit clearSearch();
} else {
m_entryView->setGroup(group);
m_entryView->displayGroup(group);
}
}
@ -1069,7 +1069,7 @@ void DatabaseWidget::endSearch()
emit listModeAboutToActivate();
// Show the normal entry view of the current group
m_entryView->setGroup(currentGroup());
m_entryView->displayGroup(currentGroup());
emit listModeActivated();
}

View File

@ -93,9 +93,9 @@ public:
QList<int> previewSplitterSizes() const;
void setPreviewSplitterSizes(const QList<int>& sizes);
bool isUsernamesHidden() const;
void setUsernamesHidden(const bool hide);
void setUsernamesHidden(bool hide);
bool isPasswordsHidden() const;
void setPasswordsHidden(const bool hide);
void setPasswordsHidden(bool hide);
QByteArray entryViewState() const;
bool setEntryViewState(const QByteArray& state) const;
void clearAllWidgets();

View File

@ -72,10 +72,9 @@ void EntryModel::setGroup(Group* group)
makeConnections(group);
endResetModel();
emit switchedToListMode();
}
void EntryModel::setEntryList(const QList<Entry*>& entries)
void EntryModel::setEntries(const QList<Entry*>& entries)
{
beginResetModel();
@ -109,7 +108,6 @@ void EntryModel::setEntryList(const QList<Entry*>& entries)
}
endResetModel();
emit switchedToSearchMode();
}
int EntryModel::rowCount(const QModelIndex& parent) const

View File

@ -60,22 +60,20 @@ public:
QStringList mimeTypes() const override;
QMimeData* mimeData(const QModelIndexList& indexes) const override;
void setEntryList(const QList<Entry*>& entries);
void setPaperClipPixmap(const QPixmap& paperclip);
void setGroup(Group* group);
void setEntries(const QList<Entry*>& entries);
bool isUsernamesHidden() const;
void setUsernamesHidden(bool hide);
bool isPasswordsHidden() const;
void setPasswordsHidden(bool hide);
void setPaperClipPixmap(const QPixmap& paperclip);
signals:
void switchedToListMode();
void switchedToSearchMode();
void usernamesHiddenChanged();
void passwordsHiddenChanged();
public slots:
void setGroup(Group* group);
void setUsernamesHidden(bool hide);
void setPasswordsHidden(bool hide);
private slots:
void entryAboutToAdd(Entry* entry);
void entryAdded(Entry* entry);

View File

@ -50,11 +50,7 @@ EntryView::EntryView(QWidget* parent)
setDefaultDropAction(Qt::MoveAction);
connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(emitEntryActivated(QModelIndex)));
connect(selectionModel(),
SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SIGNAL(entrySelectionChanged()));
connect(m_model, SIGNAL(switchedToListMode()), SLOT(switchToListMode()));
connect(m_model, SIGNAL(switchedToSearchMode()), SLOT(switchToSearchMode()));
connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SIGNAL(entrySelectionChanged()));
connect(m_model, SIGNAL(usernamesHiddenChanged()), SIGNAL(viewStateChanged()));
connect(m_model, SIGNAL(passwordsHiddenChanged()), SIGNAL(viewStateChanged()));
@ -158,16 +154,25 @@ void EntryView::focusOutEvent(QFocusEvent* event)
QTreeView::focusOutEvent(event);
}
void EntryView::setGroup(Group* group)
void EntryView::displayGroup(Group* group)
{
m_model->setGroup(group);
header()->hideSection(EntryModel::ParentGroup);
setFirstEntryActive();
m_inSearchMode = false;
}
void EntryView::setEntryList(const QList<Entry*>& entries)
void EntryView::displaySearch(const QList<Entry*>& entries)
{
m_model->setEntryList(entries);
m_model->setEntries(entries);
header()->showSection(EntryModel::ParentGroup);
// Reset sort column to 'Group', overrides DatabaseWidgetStateSync
m_sortModel->sort(EntryModel::ParentGroup, Qt::AscendingOrder);
sortByColumn(EntryModel::ParentGroup, Qt::AscendingOrder);
setFirstEntryActive();
m_inSearchMode = true;
}
void EntryView::setFirstEntryActive()
@ -227,39 +232,6 @@ Entry* EntryView::entryFromIndex(const QModelIndex& index)
}
}
/**
* Switch to list mode, i.e. list entries of group
*/
void EntryView::switchToListMode()
{
if (!m_inSearchMode) {
return;
}
header()->hideSection(EntryModel::ParentGroup);
m_inSearchMode = false;
}
/**
* Switch to search mode, i.e. list search results
*/
void EntryView::switchToSearchMode()
{
if (m_inSearchMode) {
return;
}
header()->showSection(EntryModel::ParentGroup);
// Always set sorting to column 'Group', as it does not feel right to
// have the last known sort configuration of search view restored by
// 'DatabaseWidgetStateSync', which is what happens without this
m_sortModel->sort(EntryModel::ParentGroup, Qt::AscendingOrder);
sortByColumn(EntryModel::ParentGroup, Qt::AscendingOrder);
m_inSearchMode = true;
}
/**
* Get current state of 'Hide Usernames' setting (NOTE: just pass-through for
* m_model)
@ -272,7 +244,7 @@ bool EntryView::isUsernamesHidden() const
/**
* Set state of 'Hide Usernames' setting (NOTE: just pass-through for m_model)
*/
void EntryView::setUsernamesHidden(const bool hide)
void EntryView::setUsernamesHidden(bool hide)
{
bool block = m_hideUsernamesAction->signalsBlocked();
m_hideUsernamesAction->blockSignals(true);
@ -294,7 +266,7 @@ bool EntryView::isPasswordsHidden() const
/**
* Set state of 'Hide Passwords' setting (NOTE: just pass-through for m_model)
*/
void EntryView::setPasswordsHidden(const bool hide)
void EntryView::setPasswordsHidden(bool hide)
{
bool block = m_hidePasswordsAction->signalsBlocked();
m_hidePasswordsAction->blockSignals(true);

View File

@ -39,19 +39,18 @@ public:
Entry* currentEntry();
void setCurrentEntry(Entry* entry);
Entry* entryFromIndex(const QModelIndex& index);
void setEntryList(const QList<Entry*>& entries);
bool inSearchMode();
int numberOfSelectedEntries();
void setFirstEntryActive();
bool isUsernamesHidden() const;
void setUsernamesHidden(const bool hide);
void setUsernamesHidden(bool hide);
bool isPasswordsHidden() const;
void setPasswordsHidden(const bool hide);
void setPasswordsHidden(bool hide);
QByteArray viewState() const;
bool setViewState(const QByteArray& state);
public slots:
void setGroup(Group* group);
void displayGroup(Group* group);
void displaySearch(const QList<Entry*>& entries);
signals:
void entryActivated(Entry* entry, EntryModel::ModelColumn column);
@ -65,8 +64,6 @@ protected:
private slots:
void emitEntryActivated(const QModelIndex& index);
void switchToListMode();
void switchToSearchMode();
void showHeaderMenu(const QPoint& position);
void toggleColumnVisibility(QAction* action);
void fitColumnsToWindow();

View File

@ -307,7 +307,7 @@ void TestEntryModel::testProxyModel()
QList<Entry*> entryList;
entryList << entry;
modelSource->setEntryList(entryList);
modelSource->setEntries(entryList);
/**
* @author Fonic <https://github.com/fonic>
@ -346,7 +346,7 @@ void TestEntryModel::testDatabaseDelete()
Entry* entry2 = new Entry();
entry2->setGroup(db2->rootGroup());
model->setEntryList(QList<Entry*>() << entry1 << entry2);
model->setEntries(QList<Entry*>() << entry1 << entry2);
QCOMPARE(model->rowCount(), 2);