controlpanelplugins/themeplugin/src/cpthemelistmodel.cpp
branchRCL_3
changeset 14 5f281e37a2f5
parent 13 90fe62538f66
equal deleted inserted replaced
13:90fe62538f66 14:5f281e37a2f5
     1 /*
       
     2  * Copyright (c) 2009 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 <QDir>
       
    19 #include <QStringList>
       
    20 #include <QFileSystemWatcher>
       
    21 #include <QPair>
       
    22 
       
    23 #include <HbIcon>
       
    24 
       
    25 #include "cpthemelistmodel.h"
       
    26 #include "cpthemeinfo.h"
       
    27 #include "cpthemeutil.h"
       
    28 
       
    29 /*
       
    30     CpThemeChangerModel provides an interface to the data contained in the
       
    31     theme list using QAbstractListModel.
       
    32 */
       
    33 
       
    34 /*
       
    35     Constructor
       
    36 */
       
    37 CpThemeListModel::CpThemeListModel(QObject* parent)
       
    38     : QAbstractListModel(parent)
       
    39     , mTopThemeList()
       
    40     , mThemeList()
       
    41     , mBottomThemeList()
       
    42     , mFileWatcher(new QFileSystemWatcher(this))
       
    43 {
       
    44     //Build theme list
       
    45     mThemeList = CpThemeUtil::buildThemeList();
       
    46     
       
    47     //Look into theme paths and add a file watcher for it
       
    48     //to get notified when themes are added.
       
    49     QStringList themePaths = CpThemeUtil::themeDirectories(mThemeList);
       
    50     if(!themePaths.empty()) {
       
    51         mFileWatcher->addPaths(themePaths);
       
    52     }
       
    53    
       
    54     connect(mFileWatcher, SIGNAL(directoryChanged(QString)),
       
    55            this, SLOT(themeListChanged()));
       
    56    
       
    57 }
       
    58 
       
    59 /*
       
    60     Destructor
       
    61 */
       
    62 CpThemeListModel::~CpThemeListModel()
       
    63 {
       
    64     delete mFileWatcher;
       
    65     mFileWatcher = 0;
       
    66 
       
    67 }
       
    68 
       
    69 /*
       
    70     Reimplemented from QAbstractListModel.
       
    71 */
       
    72 int CpThemeListModel::rowCount(const QModelIndex&) const
       
    73 {
       
    74     return mTopThemeList.size() +
       
    75         mThemeList.size() +
       
    76         mBottomThemeList.size();
       
    77 }
       
    78 
       
    79 /*
       
    80     Reimplemented from QAbstractListModel. 
       
    81 */
       
    82 QVariant CpThemeListModel::data(const QModelIndex& index, int role) const
       
    83 {
       
    84     QVariant retVal = QVariant();
       
    85 
       
    86     if (index.isValid()) {
       
    87         // figure out which list we're in and do the appropriate mapping
       
    88         int row = index.row();
       
    89         const QList<CpThemeInfo> *list = 0;
       
    90         if (row < mTopThemeList.size()) {
       
    91             list = &mTopThemeList;
       
    92         } else {
       
    93             row -= mTopThemeList.size();
       
    94             if ( row < mThemeList.size() ) {
       
    95                 list = &mThemeList;
       
    96             } else {
       
    97                 row -= mThemeList.size();
       
    98                 if ( row < mBottomThemeList.size() ) {
       
    99                     list = &mBottomThemeList;
       
   100                 }
       
   101             }
       
   102         }
       
   103 
       
   104         if (list) {
       
   105             switch (role) {
       
   106                 case Qt::DisplayRole:
       
   107                     retVal = list->at(row).name();
       
   108                     break;
       
   109                     
       
   110                 case Qt::SizeHintRole:
       
   111                     retVal = list->at(row).icon().size();
       
   112                     break;
       
   113                                 
       
   114                 case ItemDataRole:
       
   115                     retVal = list->at(row).itemData();
       
   116                     break;
       
   117 
       
   118                 case ItemTypeRole:
       
   119                     retVal = QVariant::fromValue<CpThemeInfo::ThemeListItemType>(list->at(row).itemType());
       
   120                     break;
       
   121 
       
   122                 default:
       
   123                     // do nothing
       
   124                     qt_noop();
       
   125             }
       
   126         }
       
   127     }
       
   128 
       
   129     return retVal;
       
   130 }
       
   131 
       
   132 /*
       
   133     Responds appropriately when the underlying data in the theme changer is modified.
       
   134 
       
   135     Unfortunately the directory watcher from QFileWatcher only says when something changed
       
   136     not what changed.  Therefore the model is considered reset rather than having rows
       
   137     with dataChanged.
       
   138 */
       
   139 void CpThemeListModel::themeListChanged()
       
   140 {
       
   141     beginResetModel();
       
   142     if(!mThemeList.empty()) {
       
   143         mThemeList.clear();
       
   144     }
       
   145     mThemeList = CpThemeUtil::buildThemeList();
       
   146   
       
   147     endResetModel();
       
   148 }
       
   149 /*!
       
   150  * Returns index of theme infor within the theme list.
       
   151  */
       
   152 int CpThemeListModel::indexOf(const CpThemeInfo& theme) const
       
   153 {
       
   154     return mThemeList.indexOf(theme);
       
   155 }