Skip to content

Commit

Permalink
impl simple search bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsenD98 committed Aug 17, 2024
1 parent 833d983 commit 1158130
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/core/qfieldcloudprojectsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2453,22 +2453,38 @@ bool QFieldCloudProjectsFilterModel::showLocalOnly() const

bool QFieldCloudProjectsFilterModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
{
if ( mShowLocalOnly && mSourceModel->data( mSourceModel->index( source_row, 0, source_parent ), QFieldCloudProjectsModel::LocalPathRole ).toString().isEmpty() )
auto currentRowIndex = mSourceModel->index( source_row, 0, source_parent );
if ( mShowLocalOnly && mSourceModel->data( currentRowIndex, QFieldCloudProjectsModel::LocalPathRole ).toString().isEmpty() )
{
return false;
}

bool ok = false;
bool matchesProjectType = false;
switch ( mFilter )
{
case PrivateProjects:
// the list will include public "community" projects that are present locally so they can appear in the "My projects" list
ok = mSourceModel->data( mSourceModel->index( source_row, 0, source_parent ), QFieldCloudProjectsModel::UserRoleOriginRole ).toString() != QStringLiteral( "public" )
|| !mSourceModel->data( mSourceModel->index( source_row, 0, source_parent ), QFieldCloudProjectsModel::LocalPathRole ).toString().isEmpty();
matchesProjectType = mSourceModel->data( currentRowIndex, QFieldCloudProjectsModel::UserRoleOriginRole ).toString() != QStringLiteral( "public" )
|| !mSourceModel->data( currentRowIndex, QFieldCloudProjectsModel::LocalPathRole ).toString().isEmpty();
break;
case PublicProjects:
ok = mSourceModel->data( mSourceModel->index( source_row, 0, source_parent ), QFieldCloudProjectsModel::UserRoleOriginRole ).toString() == QStringLiteral( "public" );
matchesProjectType = mSourceModel->data( currentRowIndex, QFieldCloudProjectsModel::UserRoleOriginRole ).toString() == QStringLiteral( "public" );
break;
}
return ok;

QString Name = mSourceModel->data( currentRowIndex, QFieldCloudProjectsModel::NameRole ).toString();
QString Description = mSourceModel->data( currentRowIndex, QFieldCloudProjectsModel::DescriptionRole ).toString();
QString Owner = mSourceModel->data( currentRowIndex, QFieldCloudProjectsModel::OwnerRole ).toString();

bool matchesTextFilter = mTextFilter.isEmpty() || Name.contains( mTextFilter, Qt::CaseInsensitive ) || Description.contains( mTextFilter, Qt::CaseInsensitive ) || Owner.contains( mTextFilter, Qt::CaseInsensitive );

return matchesProjectType && matchesTextFilter;
}

void QFieldCloudProjectsFilterModel::setTextFilter( const QString &newTextFilter )
{
if ( mTextFilter == newTextFilter )
return;
mTextFilter = newTextFilter;
invalidateFilter();
}
3 changes: 3 additions & 0 deletions src/core/qfieldcloudprojectsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ class QFieldCloudProjectsFilterModel : public QSortFilterProxyModel
*/
void setShowLocalOnly( bool showLocalOnly );

Q_INVOKABLE void setTextFilter( const QString &newTextFilter );

signals:

void projectsModelChanged();
Expand All @@ -566,6 +568,7 @@ class QFieldCloudProjectsFilterModel : public QSortFilterProxyModel
QFieldCloudProjectsModel *mSourceModel = nullptr;
ProjectsFilter mFilter = PrivateProjects;
bool mShowLocalOnly = false;
QString mTextFilter;
};

Q_DECLARE_METATYPE( QFieldCloudProjectsFilterModel::ProjectsFilter )
Expand Down
68 changes: 68 additions & 0 deletions src/qml/QFieldCloudScreen.qml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,71 @@ Page {

property bool overshootRefresh: false
property bool refreshing: false
headerPositioning: ListView.OverlayHeader

header: Rectangle {
width: table.width
height: 41
color: Theme.mainBackgroundColor
z: 20

Rectangle {
width: table.width
height: 40
radius: 6
border.width: 1
color: Theme.mainBackgroundColor
border.color: searchBar.activeFocus ? Theme.mainColor : "transparent"

QfToolButton {
id: searchButton
width: 40
height: 40
anchors.left: parent.left
bgcolor: "transparent"
iconSource: Theme.getThemeIcon("ic_baseline_search_black")
iconColor: Theme.mainTextColor
onClicked: {
table.model.setTextFilter(searchBar.text);
}
}

QfToolButton {
id: clearButton
anchors.right: parent.right
width: 40
height: 40
iconSource: Theme.getThemeIcon('ic_close_black_24dp')
iconColor: Theme.mainTextColor
bgcolor: "transparent"
visible: searchBar.text !== ""
onClicked: {
searchBar.text = '';
table.model.setTextFilter('');
}
}

TextField {
id: searchBar
padding: 7
anchors.left: searchButton.right
anchors.right: clearButton.left
anchors.rightMargin: 4
height: 40
leftPadding: padding + 4
selectByMouse: true
placeholderText: (!searchBar.activeFocus && text === "" && displayText === "") ? qsTr("Search for project") : ""
background: Item {
}
Keys.onEnterPressed: {
table.model.setTextFilter(text);
}
Keys.onReturnPressed: {
table.model.setTextFilter(text);
}
}
}
}

model: QFieldCloudProjectsFilterModel {
projectsModel: cloudProjectsModel
Expand Down Expand Up @@ -420,6 +485,7 @@ Page {

MouseArea {
property Item pressedItem
propagateComposedEvents: true
anchors.fill: parent
onClicked: mouse => {
var item = table.itemAt(table.contentX + mouse.x, table.contentY + mouse.y);
Expand All @@ -433,13 +499,15 @@ Page {
cloudProjectsModel.projectPackageAndDownload(item.projectId);
}
}
mouse.accepted = false;
}
onPressed: mouse => {
var item = table.itemAt(table.contentX + mouse.x, table.contentY + mouse.y);
if (item) {
pressedItem = item;
pressedItem.isPressed = true;
}
mouse.accepted = false;
}
onCanceled: {
if (pressedItem) {
Expand Down

0 comments on commit 1158130

Please sign in to comment.