bluetoothengine/btui/btuimodel/btuimodelsortfilter.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <btuimodelsortfilter.h>
       
    19 #include <btdevicemodel.h>
       
    20 #include "btuidevtypemap.h"
       
    21 /*!
       
    22     Constructor.
       
    23  */
       
    24 BtuiModelSortFilter::BtuiModelSortFilter( QObject *parent )
       
    25     : QSortFilterProxyModel( parent )
       
    26 {
       
    27     setDynamicSortFilter( true );
       
    28 }
       
    29 
       
    30 /*!
       
    31     Destructor.
       
    32  */
       
    33 BtuiModelSortFilter::~BtuiModelSortFilter()
       
    34 {
       
    35 }
       
    36 
       
    37 /*!
       
    38     Replace current filter values for filtering on major device class with
       
    39     the specified.
       
    40  */
       
    41 void BtuiModelSortFilter::setDeviceMajorFilter( int filter, FilterMode mode )
       
    42 {
       
    43     mFilters.clear();
       
    44     addDeviceMajorFilter( filter, mode );
       
    45 }
       
    46 
       
    47 /*!
       
    48     Add the specified filter value for filtering on major device class 
       
    49     if the specified filter doesn't exist when this function is called.
       
    50  */
       
    51 void BtuiModelSortFilter::addDeviceMajorFilter( int filter, FilterMode mode )
       
    52 {
       
    53     FilterItem f(filter, mode);
       
    54     if ( mFilters.indexOf(f) == -1 ) {
       
    55         mFilters.append( f );
       
    56         invalidateFilter();
       
    57     }
       
    58 }
       
    59 
       
    60 /*!
       
    61     Clear the specified filter value for filtering on major device class from this model.
       
    62  */
       
    63 void BtuiModelSortFilter::clearDeviceMajorFilter( int filter, FilterMode mode )
       
    64 {
       
    65     FilterItem f(filter, mode);
       
    66     int i = mFilters.indexOf(f);
       
    67     if ( i > -1 ) {
       
    68         mFilters.removeAt( i );
       
    69         invalidateFilter();
       
    70     }
       
    71 }
       
    72 
       
    73 /*!
       
    74     clear all filters for filtering on major device class.
       
    75     This Model will reset itself.
       
    76  */
       
    77 void BtuiModelSortFilter::clearDeviceMajorFilters()
       
    78 {
       
    79     // model reset is needed if there are filters :
       
    80     if ( mFilters.size() > 0 ) {
       
    81         mFilters.clear();
       
    82         invalidateFilter();
       
    83     }
       
    84 }
       
    85 
       
    86 /*!
       
    87     return true if the specified filter exists; return false otherwise.
       
    88  */
       
    89 bool BtuiModelSortFilter::hasFilter( int filter, FilterMode mode )
       
    90 {
       
    91     FilterItem f(filter, mode);
       
    92     return mFilters.indexOf(f) > -1 ;
       
    93 }
       
    94 
       
    95 /*!
       
    96     \reimp
       
    97  */
       
    98 bool BtuiModelSortFilter::filterAcceptsRow(
       
    99     int sourceRow, const QModelIndex &sourceParent) const 
       
   100 {
       
   101     bool accepted (false );
       
   102     // Get the device name from the model
       
   103     QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
       
   104 
       
   105     // the row shall pass all filters:
       
   106     for (int i = 0; i < mFilters.size(); i++ ) {
       
   107         if ( mFilters.at(i).mFilter == BtuiDevProperty::NullProperty ) {
       
   108             accepted = true;    // There is no filter, we accept all
       
   109         }
       
   110         else {
       
   111             int majorProperty = 
       
   112                     sourceModel()->data(index, BtDeviceModel::MajorPropertyRole).toInt();
       
   113             switch (mFilters.at(i).mMode) {
       
   114                 case ExactMatch:
       
   115                     // Accept if the match is spot-on
       
   116                     accepted = majorProperty == mFilters.at(i).mFilter ;
       
   117                     break;
       
   118                 case AtLeastMatch:
       
   119                     // accept if it matches all specified properties:
       
   120                     accepted = ( mFilters.at(i).mFilter == 
       
   121                         ( majorProperty & mFilters.at(i).mFilter ) );
       
   122                     break;
       
   123                 case RoughMatch:
       
   124                     // Accept if it matches one of specified properties:
       
   125                     accepted = (majorProperty & mFilters.at(i).mFilter) != 0;
       
   126                     break;
       
   127                 case Exclusive:
       
   128                     // Accept if this is not one that we want to filter out
       
   129                     accepted = (majorProperty & mFilters.at(i).mFilter) == 0;
       
   130                     break;
       
   131                 default:
       
   132                     accepted = false;
       
   133             }
       
   134         }
       
   135         // Break out of the loop at first non-accepted condition
       
   136         if (!accepted)
       
   137             break;
       
   138     }
       
   139     if (accepted) {
       
   140         // emit signal to inform a row has been accepted by fitler,
       
   141         // currently this is only needed by search view
       
   142         emit const_cast<BtuiModelSortFilter*>(this)->deviceAcceptedByFilter( sourceRow );
       
   143     }
       
   144     return accepted;
       
   145 }
       
   146 
       
   147 /*!
       
   148     \reimp
       
   149  */
       
   150 bool BtuiModelSortFilter::lessThan(
       
   151         const QModelIndex &left, const QModelIndex &right) const
       
   152 {
       
   153     if (sortRole() == BtDeviceModel::NameAliasRole ||
       
   154         sortRole() == BtDeviceModel::LastUsedTimeRole ||
       
   155         sortRole() == BtDeviceModel::RssiRole ||
       
   156         sortRole() == BtDeviceModel::SeqNumRole) {
       
   157         // base class provides sorting for these types already:
       
   158         return QSortFilterProxyModel::lessThan(left, right);
       
   159     }
       
   160     // no custom sort supported yet.
       
   161     return true;
       
   162 }