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

View File

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

View File

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

View File

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

View File

@ -50,11 +50,7 @@ EntryView::EntryView(QWidget* parent)
setDefaultDropAction(Qt::MoveAction); setDefaultDropAction(Qt::MoveAction);
connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(emitEntryActivated(QModelIndex))); connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(emitEntryActivated(QModelIndex)));
connect(selectionModel(), connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SIGNAL(entrySelectionChanged()));
SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SIGNAL(entrySelectionChanged()));
connect(m_model, SIGNAL(switchedToListMode()), SLOT(switchToListMode()));
connect(m_model, SIGNAL(switchedToSearchMode()), SLOT(switchToSearchMode()));
connect(m_model, SIGNAL(usernamesHiddenChanged()), SIGNAL(viewStateChanged())); connect(m_model, SIGNAL(usernamesHiddenChanged()), SIGNAL(viewStateChanged()));
connect(m_model, SIGNAL(passwordsHiddenChanged()), SIGNAL(viewStateChanged())); connect(m_model, SIGNAL(passwordsHiddenChanged()), SIGNAL(viewStateChanged()));
@ -158,16 +154,25 @@ void EntryView::focusOutEvent(QFocusEvent* event)
QTreeView::focusOutEvent(event); QTreeView::focusOutEvent(event);
} }
void EntryView::setGroup(Group* group) void EntryView::displayGroup(Group* group)
{ {
m_model->setGroup(group); m_model->setGroup(group);
header()->hideSection(EntryModel::ParentGroup);
setFirstEntryActive(); 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(); setFirstEntryActive();
m_inSearchMode = true;
} }
void EntryView::setFirstEntryActive() 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 * Get current state of 'Hide Usernames' setting (NOTE: just pass-through for
* m_model) * m_model)
@ -272,7 +244,7 @@ bool EntryView::isUsernamesHidden() const
/** /**
* Set state of 'Hide Usernames' setting (NOTE: just pass-through for m_model) * 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(); bool block = m_hideUsernamesAction->signalsBlocked();
m_hideUsernamesAction->blockSignals(true); 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) * 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(); bool block = m_hidePasswordsAction->signalsBlocked();
m_hidePasswordsAction->blockSignals(true); m_hidePasswordsAction->blockSignals(true);

View File

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

View File

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