Skip to content

Commit

Permalink
Minor refactors, added group sort tests with numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
ba32107 committed Jun 18, 2019
1 parent d3cc57f commit 28dd53e
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 91 deletions.
13 changes: 9 additions & 4 deletions src/core/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,11 @@ QStringList Group::hierarchy() const
return hierarchy;
}

bool Group::hasChildren() const
{
return !children().isEmpty();
}

Database* Group::database()
{
return m_db;
Expand Down Expand Up @@ -1074,21 +1079,21 @@ void Group::applyGroupIconTo(Entry* entry)
}
}

void Group::sortChildrenRecursively(bool desc)
void Group::sortChildrenRecursively(bool reverse)
{
std::sort(m_children.begin(), m_children.end(),
[desc](const Group* childGroup1, const Group* childGroup2) -> bool
[reverse](const Group* childGroup1, const Group* childGroup2) -> bool
{
QString name1 = childGroup1->name();
QString name2 = childGroup2->name();
return desc ?
return reverse ?
name1.compare(name2, Qt::CaseInsensitive) > 0 :
name1.compare(name2, Qt::CaseInsensitive) < 0;
});

for (int i = 0; i < m_children.count(); i++)
{
m_children[i]->sortChildrenRecursively(desc);
m_children[i]->sortChildrenRecursively(reverse);
}

emit groupModified();
Expand Down
3 changes: 2 additions & 1 deletion src/core/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class Group : public QObject
const Group* parentGroup() const;
void setParent(Group* parent, int index = -1);
QStringList hierarchy() const;
bool hasChildren() const;

Database* database();
const Database* database() const;
Expand All @@ -169,7 +170,7 @@ class Group : public QObject

void applyGroupIconTo(Entry* entry);

void sortChildrenRecursively(bool desc = false);
void sortChildrenRecursively(bool reverse = false);

signals:
void groupDataChanged(Group* group);
Expand Down
6 changes: 0 additions & 6 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,12 +1450,6 @@ bool DatabaseWidget::isGroupSelected() const
return m_groupView->currentGroup();
}

bool DatabaseWidget::currentGroupHasChildren() const
{
Group* currentGrp = m_groupView->currentGroup();
return currentGrp && !currentGrp->children().isEmpty();
}

bool DatabaseWidget::currentEntryHasFocus()
{
return m_entryView->numberOfSelectedEntries() > 0 && m_entryView->hasFocus();
Expand Down
1 change: 0 additions & 1 deletion src/gui/DatabaseWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class DatabaseWidget : public QStackedWidget
Group* currentGroup() const;
bool canDeleteCurrentGroup() const;
bool isGroupSelected() const;
bool currentGroupHasChildren() const;
bool isRecycleBinSelected() const;
int numberOfSelectedEntries() const;

Expand Down
10 changes: 5 additions & 5 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ MainWindow::MainWindow()
m_actionMultiplexer.connect(m_ui->actionGroupEdit, SIGNAL(triggered()), SLOT(switchToGroupEdit()));
m_actionMultiplexer.connect(m_ui->actionGroupDelete, SIGNAL(triggered()), SLOT(deleteGroup()));
m_actionMultiplexer.connect(m_ui->actionGroupEmptyRecycleBin, SIGNAL(triggered()), SLOT(emptyRecycleBin()));
m_actionMultiplexer.connect(m_ui->actionGroupSortAZ, SIGNAL(triggered()), SLOT(sortGroupsAsc()));
m_actionMultiplexer.connect(m_ui->actionGroupSortZA, SIGNAL(triggered()), SLOT(sortGroupsDesc()));
m_actionMultiplexer.connect(m_ui->actionGroupSortAsc, SIGNAL(triggered()), SLOT(sortGroupsAsc()));
m_actionMultiplexer.connect(m_ui->actionGroupSortDesc, SIGNAL(triggered()), SLOT(sortGroupsDesc()));

connect(m_ui->actionSettings, SIGNAL(toggled(bool)), SLOT(switchToSettings(bool)));
connect(m_ui->actionPasswordGenerator, SIGNAL(toggled(bool)), SLOT(switchToPasswordGen(bool)));
Expand Down Expand Up @@ -572,7 +572,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
bool singleEntrySelected = dbWidget->numberOfSelectedEntries() == 1 && hasFocus;
bool entriesSelected = dbWidget->numberOfSelectedEntries() > 0 && hasFocus;
bool groupSelected = dbWidget->isGroupSelected();
bool currentGroupHasChildren = dbWidget->currentGroupHasChildren();
bool currentGroupHasChildren = dbWidget->currentGroup()->hasChildren();
bool recycleBinSelected = dbWidget->isRecycleBinSelected();

m_ui->actionEntryNew->setEnabled(true);
Expand All @@ -595,8 +595,8 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
m_ui->actionGroupNew->setEnabled(groupSelected);
m_ui->actionGroupEdit->setEnabled(groupSelected);
m_ui->actionGroupDelete->setEnabled(groupSelected && dbWidget->canDeleteCurrentGroup());
m_ui->actionGroupSortAZ->setEnabled(groupSelected && currentGroupHasChildren);
m_ui->actionGroupSortZA->setEnabled(groupSelected && currentGroupHasChildren);
m_ui->actionGroupSortAsc->setEnabled(groupSelected && currentGroupHasChildren);
m_ui->actionGroupSortDesc->setEnabled(groupSelected && currentGroupHasChildren);
m_ui->actionGroupEmptyRecycleBin->setVisible(recycleBinSelected);
m_ui->actionGroupEmptyRecycleBin->setEnabled(recycleBinSelected);
m_ui->actionChangeMasterKey->setEnabled(true);
Expand Down
8 changes: 4 additions & 4 deletions src/gui/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@
<addaction name="actionGroupDelete"/>
<addaction name="actionGroupEmptyRecycleBin"/>
<addaction name="separator"/>
<addaction name="actionGroupSortAZ"/>
<addaction name="actionGroupSortZA"/>
<addaction name="actionGroupSortAsc"/>
<addaction name="actionGroupSortDesc"/>
</widget>
<widget class="QMenu" name="menuTools">
<property name="title">
Expand Down Expand Up @@ -455,15 +455,15 @@
<string>&amp;Delete group</string>
</property>
</action>
<action name="actionGroupSortAZ">
<action name="actionGroupSortAsc">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Sort &amp;A-Z</string>
</property>
</action>
<action name="actionGroupSortZA">
<action name="actionGroupSortDesc">
<property name="enabled">
<bool>false</bool>
</property>
Expand Down
11 changes: 5 additions & 6 deletions src/gui/group/GroupModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,17 @@ void GroupModel::groupMoved()
endMoveRows();
}

void GroupModel::sortChildren(Group* parent, bool desc)
void GroupModel::sortChildren(Group* rootGroup, bool reverse)
{
emit layoutAboutToBeChanged();

QList<QModelIndex> oldIndexes;
collectIndexesRecursively(oldIndexes, parent->children());
collectIndexesRecursively(oldIndexes, rootGroup->children());

parent->sortChildrenRecursively(desc);
rootGroup->sortChildrenRecursively(reverse);

QList<QModelIndex> newIndexes;
collectIndexesRecursively(newIndexes, parent->children());
collectIndexesRecursively(newIndexes, rootGroup->children());

for (int i = 0; i < oldIndexes.count(); i++) {
changePersistentIndex(oldIndexes[i], newIndexes[i]);
Expand All @@ -432,8 +432,7 @@ void GroupModel::sortChildren(Group* parent, bool desc)

void GroupModel::collectIndexesRecursively(QList<QModelIndex>& indexes, QList<Group*> groups)
{
for (int i = 0; i < groups.count(); i++) {
Group* group = groups[i];
for (auto group : groups) {
indexes.append(index(group));
collectIndexesRecursively(indexes, group->children());
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/group/GroupModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GroupModel : public QAbstractItemModel
dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) override;
QStringList mimeTypes() const override;
QMimeData* mimeData(const QModelIndexList& indexes) const override;
void sortChildren(Group* parent, bool desc = false);
void sortChildren(Group* rootGroup, bool reverse = false);

private:
QModelIndex parent(Group* group) const;
Expand Down
8 changes: 4 additions & 4 deletions src/gui/group/GroupView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ void GroupView::expandGroup(Group* group, bool expand)
setExpanded(index, expand);
}

void GroupView::sortGroups(bool desc) {
Group* currentGrp = currentGroup();
if (currentGrp) {
m_model->sortChildren(currentGrp, desc);
void GroupView::sortGroups(bool reverse) {
Group* group = currentGroup();
if (group) {
m_model->sortChildren(group, reverse);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/group/GroupView.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GroupView : public QTreeView
Group* currentGroup();
void setCurrentGroup(Group* group);
void expandGroup(Group* group, bool expand = true);
void sortGroups(bool desc = false);
void sortGroups(bool reverse = false);

signals:
void groupSelectionChanged(Group* group);
Expand Down
Loading

0 comments on commit 28dd53e

Please sign in to comment.