filemanager/src/filemanager/src/components/fmdatalistmodel.cpp
changeset 37 15bc28c9dd51
parent 16 ada7962b4308
child 46 d58987eac7e8
equal deleted inserted replaced
16:ada7962b4308 37:15bc28c9dd51
     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  * 
       
    15  * Description:
       
    16  *     The source file of the data list model of file manager
       
    17  */
       
    18 
       
    19 #include "fmdatalistmodel.h"
       
    20 
       
    21 
       
    22 FmDataListModel::FmDataListModel( QObject *parent )
       
    23     : QAbstractListModel( parent )
       
    24 {
       
    25 }
       
    26 
       
    27 int FmDataListModel::rowCount( const QModelIndex &parent ) const
       
    28 {
       
    29     if (parent.isValid())
       
    30         return 0;
       
    31 
       
    32     return mDisplayLst.count();
       
    33 }
       
    34 
       
    35 QVariant FmDataListModel::data( const QModelIndex &index, int role ) const
       
    36 {
       
    37     if ( index.row() < 0 || index.row() >= mDisplayLst.size() )
       
    38         return QVariant();
       
    39 
       
    40     if ( role == Qt::DisplayRole || role == Qt::EditRole )
       
    41         return mDisplayLst.at( index.row() );
       
    42 
       
    43     if ( role == Qt::UserRole )
       
    44         return mUserDataLst.at( index.row() );
       
    45 
       
    46     return QVariant();
       
    47 }
       
    48 
       
    49 Qt::ItemFlags FmDataListModel::flags( const QModelIndex &index ) const
       
    50 {
       
    51     if ( !index.isValid() )
       
    52         return QAbstractItemModel::flags( index ) | Qt::ItemIsDropEnabled;
       
    53 
       
    54     return QAbstractItemModel::flags( index ) | Qt::ItemIsEditable;
       
    55 }
       
    56 
       
    57 bool FmDataListModel::setData( const QModelIndex &index, const QVariant &value, int role )
       
    58 {
       
    59     if ( index.row() >= 0 && index.row() < mDisplayLst.size()
       
    60         && ( role == Qt::EditRole || role == Qt::DisplayRole ) ){
       
    61         mDisplayLst.replace( index.row(), value.toString() );
       
    62         emit dataChanged( index, index );
       
    63         return true;
       
    64     }
       
    65 
       
    66     if ( index.row() >= 0 && index.row() < mUserDataLst.size()
       
    67         && ( role == Qt::UserRole ) ) {
       
    68         mUserDataLst.replace( index.row(), value.toString() );
       
    69         emit dataChanged( index, index );
       
    70         return true;
       
    71     }
       
    72     return false;
       
    73 }
       
    74 
       
    75 bool FmDataListModel::insertRows( int row, int count, const QModelIndex &parent )
       
    76 {
       
    77     if ( count < 1 || row < 0 || row > rowCount( parent ) )
       
    78         return false;
       
    79 
       
    80     beginInsertRows( QModelIndex(), row, row + count - 1 );
       
    81 
       
    82     for ( int r = 0; r < count; ++r )
       
    83     {
       
    84         mDisplayLst.insert( row, QString() );
       
    85         mUserDataLst.insert( row, QString() );
       
    86     }
       
    87 
       
    88     endInsertRows();
       
    89 
       
    90     return true;
       
    91 }
       
    92 
       
    93 bool FmDataListModel::removeRows( int row, int count, const QModelIndex &parent )
       
    94 {
       
    95     if ( count <= 0 || row < 0 || ( row + count ) > rowCount( parent ) )
       
    96         return false;
       
    97 
       
    98     beginRemoveRows( QModelIndex(), row, row + count - 1 );
       
    99 
       
   100     for ( int r = 0; r < count; ++r )
       
   101     {
       
   102         mDisplayLst.removeAt( row );
       
   103         mUserDataLst.removeAt( row );
       
   104     }
       
   105 
       
   106     endRemoveRows();
       
   107     return true;
       
   108 }