|
| 1 | +/*************************************************************************** |
| 2 | + * * |
| 3 | + * This program is free software; you can redistribute it and/or modify * |
| 4 | + * it under the terms of the GNU General Public License as published by * |
| 5 | + * the Free Software Foundation; either version 2 of the License, or * |
| 6 | + * (at your option) any later version. * |
| 7 | + * * |
| 8 | + ***************************************************************************/ |
| 9 | + |
| 10 | +#include "uniquevaluesfiltermodel.h" |
| 11 | +#include "coreutils.h" |
| 12 | + |
| 13 | +#include <qgsproject.h> |
| 14 | +#include <qgsvectorlayer.h> |
| 15 | +#include <QtConcurrentRun> |
| 16 | + |
| 17 | + |
| 18 | +UniqueValuesFilterModel::UniqueValuesFilterModel( QObject *parent ) |
| 19 | + : QAbstractListModel( parent ) |
| 20 | +{ |
| 21 | + // <TODO> remove me once this class is used! :) |
| 22 | + const QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers(); |
| 23 | + for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it ) |
| 24 | + { |
| 25 | + QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( it.value() ); |
| 26 | + if ( vectorLayer ) |
| 27 | + { |
| 28 | + setLayer( vectorLayer ); |
| 29 | + setFieldName( "fid" ); |
| 30 | + break; |
| 31 | + } |
| 32 | + } |
| 33 | + // </TODO> |
| 34 | + |
| 35 | + connect( &mResultWatcher, &QFutureWatcher<QVariantList>::finished, this, &UniqueValuesFilterModel::onLoadingFinished ); |
| 36 | +} |
| 37 | + |
| 38 | +int UniqueValuesFilterModel::rowCount( const QModelIndex &parent ) const |
| 39 | +{ |
| 40 | + Q_UNUSED( parent ) |
| 41 | + return mItems.size(); |
| 42 | +} |
| 43 | + |
| 44 | +QVariant UniqueValuesFilterModel::data( const QModelIndex &index, int role ) const |
| 45 | +{ |
| 46 | + if ( !index.isValid() || index.row() >= mItems.size() ) |
| 47 | + return {}; |
| 48 | + |
| 49 | + switch ( role ) |
| 50 | + { |
| 51 | + case Qt::DisplayRole: |
| 52 | + return mItems.at( index.row() ); |
| 53 | + default: |
| 54 | + return {}; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +QgsVectorLayer *UniqueValuesFilterModel::layer() const |
| 59 | +{ |
| 60 | + return mLayer; |
| 61 | +} |
| 62 | + |
| 63 | +void UniqueValuesFilterModel::setLayer( QgsVectorLayer *layer ) |
| 64 | +{ |
| 65 | + if ( mLayer == layer ) |
| 66 | + return; |
| 67 | + |
| 68 | + mLayer = layer; |
| 69 | + emit layerChanged(); |
| 70 | +} |
| 71 | + |
| 72 | +QString UniqueValuesFilterModel::fieldName() const |
| 73 | +{ |
| 74 | + return mFieldName; |
| 75 | +} |
| 76 | + |
| 77 | +void UniqueValuesFilterModel::setFieldName( const QString &fieldName ) |
| 78 | +{ |
| 79 | + if ( mFieldName == fieldName ) |
| 80 | + return; |
| 81 | + |
| 82 | + mFieldName = fieldName; |
| 83 | + emit fieldNameChanged(); |
| 84 | +} |
| 85 | + |
| 86 | +void UniqueValuesFilterModel::populate() |
| 87 | +{ |
| 88 | + if ( !mLayer || mFieldName.isEmpty() ) |
| 89 | + return; |
| 90 | + |
| 91 | + int fieldIndex = mLayer->fields().lookupField( mFieldName ); |
| 92 | + if ( fieldIndex < 0 ) |
| 93 | + { |
| 94 | + CoreUtils::log( QStringLiteral( "Filtering" ), QStringLiteral( "Error, field %1 could not be found, dropdown filter won't work." ).arg( mFieldName ) ); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if ( mItems.size() > 0 ) return; |
| 99 | + |
| 100 | + if ( mResultWatcher.isRunning() ) return; |
| 101 | + |
| 102 | + QgsVectorLayer *layerClone = mLayer->clone(); |
| 103 | + |
| 104 | + mResultWatcher.setFuture( QtConcurrent::run( &UniqueValuesFilterModel::loadUniqueValues, this, layerClone, fieldIndex ) ); |
| 105 | +} |
| 106 | + |
| 107 | +QVariantList UniqueValuesFilterModel::loadUniqueValues( QgsVectorLayer *layer, int fieldIndex ) |
| 108 | +{ |
| 109 | + std::unique_ptr<QgsVectorLayer> l( layer ); |
| 110 | + |
| 111 | + const QSet<QVariant> uniqueValues = l->uniqueValues( fieldIndex, 1000000 ); |
| 112 | + |
| 113 | + QVariantList results; |
| 114 | + |
| 115 | + results.reserve( uniqueValues.size() ); |
| 116 | + |
| 117 | + for ( const QVariant &v : uniqueValues ) |
| 118 | + { |
| 119 | + results.append( v ); |
| 120 | + } |
| 121 | + |
| 122 | + std::sort( results.begin(), results.end(), []( const QVariant & a, const QVariant & b ) |
| 123 | + { |
| 124 | + return a.toString() < b.toString(); |
| 125 | + } ); |
| 126 | + |
| 127 | + return results; |
| 128 | +} |
| 129 | + |
| 130 | +void UniqueValuesFilterModel::onLoadingFinished() |
| 131 | +{ |
| 132 | + beginResetModel(); |
| 133 | + |
| 134 | + mItems.clear(); |
| 135 | + mItems = mResultWatcher.result(); |
| 136 | + |
| 137 | + // TODO: do we need boolean to indicate if the model is loading? |
| 138 | + // TODO: measure how long it takes to move results from future result to mItems ~ there might be a way to avoid the copy |
| 139 | + |
| 140 | + endResetModel(); |
| 141 | +} |
0 commit comments