Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ set(MM_SRCS
featuresmodel.cpp
fieldsmodel.cpp
filter/uniquevaluesfiltermodel.cpp
filter/valuemapfiltermodel.cpp
filtercontroller.cpp
guidelinecontroller.cpp
hapticsmodel.cpp
Expand Down Expand Up @@ -150,6 +151,7 @@ set(MM_HDRS
featuresmodel.h
fieldsmodel.h
filter/uniquevaluesfiltermodel.h
filter/valuemapfiltermodel.h
filtercontroller.h
guidelinecontroller.h
hapticsmodel.h
Expand Down
120 changes: 120 additions & 0 deletions app/filter/valuemapfiltermodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "valuemapfiltermodel.h"

ValueMapFilterModel::ValueMapFilterModel( QObject *parent )
: QAbstractListModel( parent )
{
}

int ValueMapFilterModel::rowCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent )
return mFilteredItems.size();
}

QVariant ValueMapFilterModel::data( const QModelIndex &index, int role ) const
{
if ( !index.isValid() || index.row() >= mFilteredItems.size() )
return {};

const Item &item = mFilteredItems.at( index.row() );

switch ( role )
{
case Qt::DisplayRole:
case TextRole:
return item.text;
case ValueRole:
return item.value;
default:
return {};
}
}

QHash<int, QByteArray> ValueMapFilterModel::roleNames() const
{
return
{
{ TextRole, "text" },
{ ValueRole, "value" },
};
}

QVariantMap ValueMapFilterModel::config() const
{
return mConfig;
}

void ValueMapFilterModel::setConfig( const QVariantMap &config )
{
if ( mConfig == config )
return;

mConfig = config;
emit configChanged();

populate();
}

QString ValueMapFilterModel::searchText() const
{
return mSearchText;
}

void ValueMapFilterModel::setSearchText( const QString &searchText )
{
if ( mSearchText == searchText )
return;

mSearchText = searchText;
emit searchTextChanged();

applyFilter();
}

void ValueMapFilterModel::populate()
{
mItems.clear();

const QVariantList mapList = mConfig.value( QStringLiteral( "map" ) ).toList();
mItems.reserve( mapList.size() );

for ( const QVariant &entry : mapList )
{
const QVariantMap entryMap = entry.toMap();
if ( entryMap.isEmpty() )
continue;

// Each entry is a single-key map: {"Display Text": "stored_value"}
Item item;
item.text = entryMap.constBegin().key();
item.value = entryMap.constBegin().value().toString();
mItems.append( item );
}

applyFilter();
}

void ValueMapFilterModel::applyFilter()
{
beginResetModel();
mFilteredItems.clear();

for ( const Item &item : std::as_const( mItems ) )
{
if ( mSearchText.isEmpty() || item.text.contains( mSearchText, Qt::CaseInsensitive ) )
{
mFilteredItems.append( item );
}
}

endResetModel();
}
69 changes: 69 additions & 0 deletions app/filter/valuemapfiltermodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef VALUEMAPFILTERMODEL_H
#define VALUEMAPFILTERMODEL_H

#include <QAbstractListModel>
#include <QtQml/qqmlregistration.h>

/**
* Populates a list model from a QGIS ValueMap editor widget config.
* Exposes TextRole (display label) and ValueRole (stored key) for each entry.
*/
class ValueMapFilterModel : public QAbstractListModel
{
Q_OBJECT
QML_ELEMENT

Q_PROPERTY( QVariantMap config READ config WRITE setConfig NOTIFY configChanged )
Q_PROPERTY( QString searchText READ searchText WRITE setSearchText NOTIFY searchTextChanged )

public:
enum Roles
{
TextRole = Qt::UserRole + 1,
ValueRole
};
Q_ENUM( Roles )

explicit ValueMapFilterModel( QObject *parent = nullptr );
~ValueMapFilterModel() override = default;

int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
QHash<int, QByteArray> roleNames() const override;

QVariantMap config() const;
void setConfig( const QVariantMap &config );

QString searchText() const;
void setSearchText( const QString &searchText );

signals:
void configChanged();
void searchTextChanged();

private:
void populate();
void applyFilter();

struct Item
{
QString text;
QString value;
};

QVariantMap mConfig;
QString mSearchText;
QList<Item> mItems;
QList<Item> mFilteredItems;
};

#endif // VALUEMAPFILTERMODEL_H
Loading