Skip to content

Commit

Permalink
First commit of the feature: 'Filtering documents'.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian.vallet committed Oct 19, 2024
1 parent 2375205 commit 6b0e4c5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
30 changes: 4 additions & 26 deletions resources/forms/documents.ui
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,6 @@
</item>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="sortOrder">
<property name="sizePolicy">
Expand Down Expand Up @@ -146,17 +130,11 @@
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QLineEdit" name="filterText">
<property name="placeholderText">
<string>filter the documents</string>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</widget>
</item>
</layout>
</item>
Expand Down
7 changes: 7 additions & 0 deletions src/document/UBDocumentController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,7 @@ void UBDocumentController::setupViews()

connect(mDocumentUI->sortKind, SIGNAL(activated(int)), this, SLOT(onSortKindChanged(int)));
connect(mDocumentUI->sortOrder, SIGNAL(toggled(bool)), this, SLOT(onSortOrderChanged(bool)));
connect(mDocumentUI->filterText, SIGNAL(textChanged(const QString&)), this, SLOT(onFilterTextChanged(const QString&)));

connect(mDocumentUI->splitter, SIGNAL(splitterMoved(int,int)), this, SLOT(onSplitterMoved(int, int)));

Expand Down Expand Up @@ -2416,6 +2417,12 @@ void UBDocumentController::onSortKindChanged(int index)
UBSettings::settings()->documentSortKind->setInt(index);
}

void UBDocumentController::onFilterTextChanged(const QString& filter)
{
mSortFilterProxyModel->setFilterCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
mSortFilterProxyModel->setFilterRegularExpression(filter);
}

void UBDocumentController::onSplitterMoved(int size, int index)
{
Q_UNUSED(index);
Expand Down
1 change: 1 addition & 0 deletions src/document/UBDocumentController.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ class UBDocumentController : public UBDocumentContainer
//N/C - NNE - 20140403
void onSortKindChanged(int index);
void onSortOrderChanged(bool order);
void onFilterTextChanged(const QString& filter);
void onSplitterMoved(int size, int index);
void collapseAll();
void expandAll();
Expand Down
16 changes: 16 additions & 0 deletions src/document/UBSortFilterProxyModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ bool UBSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex

return QSortFilterProxyModel::lessThan(left, right);
}

bool UBSortFilterProxyModel::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
if(UBPersistenceManager::persistenceManager()->mDocumentTreeStructureModel->isCatalog(index0))
{
// Always show the catalog folders
return true;
}
else
{
// Filter the documents
return (sourceModel()->data(index0).toString().contains(filterRegularExpression()));

This comment has been minimized.

Copy link
@letsfindaway

letsfindaway Oct 20, 2024

Collaborator

Instead of implementing the comparison on your own, one typically invokes the base class implementation for this. This makes it sure that it works for all options, fixed string, wildcard and regular expression. So something like

return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
}
2 changes: 1 addition & 1 deletion src/document/UBSortFilterProxyModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class UBSortFilterProxyModel : public QSortFilterProxyModel
UBSortFilterProxyModel();

bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;

This comment has been minimized.

Copy link
@letsfindaway

letsfindaway Oct 20, 2024

Collaborator

And here I would fix a visibility issue: lessThan is protected in the base class, so any override should also be protected. Same holds for filterAcceptsRow.

And to make the override clear, I would add override to both function declarations.

But I'm not sure how many fixes a feature commit should contain. In fact also the include of the UBPersistenceManager could be removed as it is not needed. I'm typically not doing something like this in my feature commits, but here, it is so obvious. So let's see what Clement says how much cleanup would be acceptable.

};

#endif // UBSORTFILTERPROXYMODEL_H

0 comments on commit 6b0e4c5

Please sign in to comment.