Changeset 668635e in subsurface


Ignore:
Timestamp:
Dec 24, 2017, 8:24:11 AM (4 weeks ago)
Author:
Dirk Hohndel <dirk@…>
Branches:
master
Children:
7451517e
Parents:
7eed752
git-author:
Berthold Stoeger <bstoeger@…> (12/23/17 06:25:54)
git-committer:
Dirk Hohndel <dirk@…> (12/24/17 08:24:11)
Message:

Move *FilterModel? functions into base class

The *FilterModels? had a number of of virtual functions, which only
accessed members of the base class. Moreover, these functions were
identical and generated with macros. Therefore, move these functions
to the base class.

The one excption is data(), which uses different count functions
(passed as a macro parameter). Thus, introduce a virtual countDives()
function and likewise move data() to the base class. A function pointer
might be even more clear, but since the rest of the code/Qt relies
heavily on runtime polymorphism, let's do the same here.

The only macros left are those creating the singleton accessors.
This could be more clearly realized by templates, but let's
likewise keep it the way is.

Signed-off-by: Berthold Stoeger <bstoeger@…>

Location:
qt-models
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • qt-models/filtermodels.cpp

    r7eed752 r668635e  
    2020}
    2121
    22 #define CREATE_MODEL_SET_DATA_METHOD( CLASS ) \
    23 bool CLASS::setData(const QModelIndex &index, const QVariant &value, int role) \
    24 { \
    25         if (role == Qt::CheckStateRole) { \
    26                 checkState[index.row()] = value.toBool(); \
    27                 anyChecked = false; \
    28                 for (int i = 0; i < rowCount(); i++) { \
    29                         if (checkState[i] == true) { \
    30                                 anyChecked = true; \
    31                                 break; \
    32                         } \
    33                 } \
    34                 dataChanged(index, index); \
    35                 return true; \
    36         } \
    37         return false; \
    38 }
    39 
    40 #define CREATE_CLEAR_FILTER_METHOD( CLASS ) \
    41 void CLASS::clearFilter() \
    42 { \
    43         std::fill(checkState.begin(), checkState.end(), false); \
    44         anyChecked = false; \
    45         emit dataChanged(createIndex(0,0), createIndex(rowCount()-1, 0)); \
    46 }
    47 
    48 #define CREATE_FLAGS_METHOD( CLASS ) \
    49 Qt::ItemFlags CLASS::flags(const QModelIndex &index) const \
    50 { \
    51         return QStringListModel::flags(index) | Qt::ItemIsUserCheckable; \
    52 }
    53 
    54 #define CREATE_DATA_METHOD( CLASS, COUNTER_FUNCTION ) \
    55 QVariant CLASS::data(const QModelIndex &index, int role) const \
    56 { \
    57         if (role == Qt::CheckStateRole) { \
    58                 return checkState[index.row()] ? Qt::Checked : Qt::Unchecked; \
    59         } else if (role == Qt::DisplayRole) { \
    60                 QString value = stringList()[index.row()]; \
    61                 int count = COUNTER_FUNCTION((index.row() == rowCount() - 1) ? "" : value.toUtf8().data()); \
    62                 return value + QString(" (%1)").arg(count); \
    63         } \
    64         return QVariant(); \
    65 }
    66 
    67 #define CREATE_COMMON_METHODS_FOR_FILTER( CLASS, COUNTER_FUNCTION ) \
    68 CREATE_FLAGS_METHOD( CLASS ); \
    69 CREATE_CLEAR_FILTER_METHOD( CLASS ); \
    70 CREATE_MODEL_SET_DATA_METHOD( CLASS ); \
    71 CREATE_INSTANCE_METHOD( CLASS ); \
    72 CREATE_DATA_METHOD( CLASS, COUNTER_FUNCTION )
    73 
    74 CREATE_COMMON_METHODS_FOR_FILTER(TagFilterModel, count_dives_with_tag)
    75 CREATE_COMMON_METHODS_FOR_FILTER(BuddyFilterModel, count_dives_with_person)
    76 CREATE_COMMON_METHODS_FOR_FILTER(LocationFilterModel, count_dives_with_location)
    77 CREATE_COMMON_METHODS_FOR_FILTER(SuitsFilterModel, count_dives_with_suit)
    78 
     22CREATE_INSTANCE_METHOD(TagFilterModel)
     23CREATE_INSTANCE_METHOD(BuddyFilterModel)
     24CREATE_INSTANCE_METHOD(LocationFilterModel)
     25CREATE_INSTANCE_METHOD(SuitsFilterModel)
    7926CREATE_INSTANCE_METHOD(MultiFilterSortModel)
    8027
     
    11360}
    11461
     62Qt::ItemFlags FilterModelBase::flags(const QModelIndex &index) const
     63{
     64        return QStringListModel::flags(index) | Qt::ItemIsUserCheckable;
     65}
     66
     67bool FilterModelBase::setData(const QModelIndex &index, const QVariant &value, int role)
     68{
     69        if (role == Qt::CheckStateRole) {
     70                checkState[index.row()] = value.toBool();
     71                anyChecked = false;
     72                for (int i = 0; i < rowCount(); i++) {
     73                        if (checkState[i] == true) {
     74                                anyChecked = true;
     75                                break;
     76                        }
     77                }
     78                dataChanged(index, index);
     79                return true;
     80        }
     81        return false;
     82}
     83
     84QVariant FilterModelBase::data(const QModelIndex &index, int role) const
     85{
     86        if (role == Qt::CheckStateRole) {
     87                return checkState[index.row()] ? Qt::Checked : Qt::Unchecked;
     88        } else if (role == Qt::DisplayRole) {
     89                QString value = stringList()[index.row()];
     90                int count = countDives((index.row() == rowCount() - 1) ? "" : value.toUtf8().data());
     91                return value + QString(" (%1)").arg(count);
     92        }
     93        return QVariant();
     94}
     95
     96void FilterModelBase::clearFilter()
     97{
     98        std::fill(checkState.begin(), checkState.end(), false);
     99        anyChecked = false;
     100        emit dataChanged(createIndex(0,0), createIndex(rowCount()-1, 0));
     101}
     102
    115103SuitsFilterModel::SuitsFilterModel(QObject *parent) : FilterModelBase(parent)
    116104{
     105}
     106
     107int SuitsFilterModel::countDives(const char *s) const
     108{
     109        return count_dives_with_suit(s);
    117110}
    118111
     
    164157TagFilterModel::TagFilterModel(QObject *parent) : FilterModelBase(parent)
    165158{
     159}
     160
     161int TagFilterModel::countDives(const char *s) const
     162{
     163        return count_dives_with_tag(s);
    166164}
    167165
     
    220218}
    221219
     220int BuddyFilterModel::countDives(const char *s) const
     221{
     222        return count_dives_with_person(s);
     223}
     224
    222225bool BuddyFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const
    223226{
     
    273276LocationFilterModel::LocationFilterModel(QObject *parent) : FilterModelBase(parent)
    274277{
     278}
     279
     280int LocationFilterModel::countDives(const char *s) const
     281{
     282        return count_dives_with_location(s);
    275283}
    276284
  • qt-models/filtermodels.h

    r7eed752 r668635e  
    1111public:
    1212        virtual bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const = 0;
    13         virtual void clearFilter() = 0;
     13        void clearFilter();
    1414        std::vector<char> checkState;
    1515        bool anyChecked;
     
    1717        explicit FilterModelBase(QObject *parent = 0);
    1818        void updateList(const QStringList &new_list);
     19        virtual int countDives(const char *) const = 0;
     20private:
     21        Qt::ItemFlags flags(const QModelIndex &index) const;
     22        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
     23        bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
    1924};
    2025
     
    2328public:
    2429        static TagFilterModel *instance();
    25         virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    26         virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
    27         virtual Qt::ItemFlags flags(const QModelIndex &index) const;
    2830        bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
    29         void clearFilter();
    3031public
    3132slots:
     
    3435private:
    3536        explicit TagFilterModel(QObject *parent = 0);
     37        int countDives(const char *) const;
    3638};
    3739
     
    4042public:
    4143        static BuddyFilterModel *instance();
    42         virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    43         virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
    44         virtual Qt::ItemFlags flags(const QModelIndex &index) const;
    4544        bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
    46         void clearFilter();
    4745public
    4846slots:
     
    5149private:
    5250        explicit BuddyFilterModel(QObject *parent = 0);
     51        int countDives(const char *) const;
    5352};
    5453
     
    5756public:
    5857        static LocationFilterModel *instance();
    59         virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    60         virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
    61         virtual Qt::ItemFlags flags(const QModelIndex &index) const;
    6258        bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
    63         void clearFilter();
    6459public
    6560slots:
     
    7065private:
    7166        explicit LocationFilterModel(QObject *parent = 0);
     67        int countDives(const char *) const;
    7268};
    7369
     
    7672public:
    7773        static SuitsFilterModel *instance();
    78         virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    79         virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
    80         virtual Qt::ItemFlags flags(const QModelIndex &index) const;
    8174        bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
    82         void clearFilter();
    8375public
    8476slots:
     
    8779private:
    8880        explicit SuitsFilterModel(QObject *parent = 0);
     81        int countDives(const char *) const;
    8982};
    9083
Note: See TracChangeset for help on using the changeset viewer.