emailuis/nmailuiengine/src/nmmessagesearchlistmodel.cpp
branchGCC_SURGE
changeset 55 cdd802add233
parent 28 011f79704660
parent 54 997a02608b3a
equal deleted inserted replaced
28:011f79704660 55:cdd802add233
     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 "nmuiengineheaders.h"
       
    19 
       
    20 /*!
       
    21     \class NmMessageSearchListModel
       
    22     \brief A proxy model used to filter out messages from the message list model
       
    23            that are not amongst the search results.
       
    24 */
       
    25 
       
    26 /*!
       
    27     Class constructor.
       
    28 */
       
    29 NmMessageSearchListModel::NmMessageSearchListModel(QObject *parent)
       
    30 : QSortFilterProxyModel(parent)
       
    31 {
       
    32     // No implementation required.
       
    33 }
       
    34 
       
    35 
       
    36 /*!
       
    37     Class destructor.
       
    38 */
       
    39 NmMessageSearchListModel::~NmMessageSearchListModel()
       
    40 {
       
    41     clearSearchResults();
       
    42 }
       
    43 
       
    44 
       
    45 /*!
       
    46     Clears search results and removes the accepted source model items from this
       
    47     proxy model.
       
    48 */
       
    49 void NmMessageSearchListModel::clearSearchResults()
       
    50 {
       
    51     mSearchResults.clear();
       
    52     invalidateFilter();
       
    53 }
       
    54 
       
    55 
       
    56 /*!
       
    57     Returns the number of search results i.e. IDs in the container.
       
    58 
       
    59     \return The number of search results.
       
    60 */
       
    61 int NmMessageSearchListModel::searchResultCount() const
       
    62 {
       
    63     return mSearchResults.count();
       
    64 }
       
    65 
       
    66 
       
    67 /*!
       
    68     From QSortFilterProxyModel.
       
    69     Returns the data from the source model according to the given proxy index.
       
    70 
       
    71     \param index The index of this proxy model.
       
    72     \param role The item role.
       
    73 
       
    74     \return The item data wrapped in a QVariant.
       
    75 */
       
    76 QVariant NmMessageSearchListModel::data(const QModelIndex &index,
       
    77                                         int role /* = Qt::DisplayRole */) const
       
    78 {
       
    79     QVariant retVal;
       
    80     QAbstractItemModel *model = sourceModel();
       
    81 
       
    82     if (model) {
       
    83         // Return the data from the source model by mapping the given index
       
    84         // in respect to the source model.
       
    85         QModelIndex sourceIndex = mapToSource(index);
       
    86         retVal = model->data(sourceIndex, role);
       
    87     }
       
    88     else {
       
    89         // Since no source model set, use the base class method implementation.
       
    90         retVal = QSortFilterProxyModel::data(index, role);
       
    91     }
       
    92 
       
    93     return retVal;
       
    94 }
       
    95 
       
    96 
       
    97 /*!
       
    98     From QSortFilterProxyModel.
       
    99     Filters the items of the source model depending on the current search results.
       
   100 
       
   101     \param source_row A source model row index.
       
   102     \param source_parent The parent model index of the row.
       
   103 
       
   104     \return True if the row should be accepted to the search list model, false
       
   105             otherwise.
       
   106 */
       
   107 bool NmMessageSearchListModel::filterAcceptsRow(int source_row,
       
   108                                                 const QModelIndex &source_parent) const
       
   109 {
       
   110     bool accept(false);
       
   111 
       
   112     // Get the source model.
       
   113     NmMessageListModel *model = qobject_cast<NmMessageListModel*>(sourceModel());
       
   114 
       
   115     if (model) {
       
   116         // Extract the model item corresponding the given row and index.
       
   117         QModelIndex listIndex = model->index(source_row, 0, source_parent);
       
   118         QVariant qVariant = model->data(listIndex);
       
   119         NmMessageListModelItem *item = qVariant.value<NmMessageListModelItem*>();
       
   120 
       
   121         if (item) {
       
   122             // Get the message ID from the item and compare it to the current
       
   123             // search results.
       
   124             const NmId itemId = item->envelope().messageId();
       
   125 
       
   126             if (mSearchResults.contains(itemId)) {
       
   127                 // The message ID matches the search result => do accept the row.
       
   128                 accept = true;
       
   129             }
       
   130         }
       
   131     }
       
   132 
       
   133     return accept;
       
   134 }
       
   135 
       
   136 
       
   137 /*!
       
   138     Adds the given message ID to the search results.
       
   139 
       
   140     \param messageId The ID to add.
       
   141 
       
   142     \return True if the given ID was added, false otherwise.
       
   143 */
       
   144 bool NmMessageSearchListModel::addSearchResult(const NmId &messageId)
       
   145 {
       
   146     bool resultAdded(false);
       
   147 
       
   148     // Make sure not to add the same ID twice.
       
   149     if (!mSearchResults.contains(messageId)) {
       
   150         // Add the given ID to the results.
       
   151         mSearchResults.append(messageId);
       
   152         resultAdded = true;
       
   153 
       
   154         // Invalidate the filter in order to update the model. This forces
       
   155         // filterAcceptsRow() to be ran again for each row in the source model.
       
   156         invalidateFilter();
       
   157     }
       
   158 
       
   159     return resultAdded;
       
   160 }
       
   161 
       
   162 
       
   163 /*!
       
   164     Refreshes the content by forcing the model to re-process the source model
       
   165     items using the current filter (search results).
       
   166 */
       
   167 void NmMessageSearchListModel::refreshContent()
       
   168 {
       
   169     invalidateFilter();
       
   170 }
       
   171 
       
   172 
       
   173 // End of file.