File indexing completed on 2024-04-28 05:52:07
0001 /* 0002 SPDX-FileCopyrightText: 2007-2009 Stefan Böhmann <kde@hilefoks.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "tealistmodel.h" 0008 #include "tea.h" 0009 0010 #include <KLocalizedString> 0011 0012 0013 TeaListModel::TeaListModel(const QList<Tea> &tealist, QObject *parent) 0014 : QAbstractTableModel(parent), m_tealist(tealist) 0015 { 0016 } 0017 0018 0019 QModelIndex TeaListModel::index(int row, int column, const QModelIndex &parent) const 0020 { 0021 Q_UNUSED( parent ) 0022 0023 return createIndex(row, column); 0024 } 0025 0026 0027 int TeaListModel::rowCount(const QModelIndex &parent) const 0028 { 0029 if( parent.isValid() ) { 0030 return 0; 0031 } 0032 0033 return m_tealist.size(); 0034 } 0035 0036 0037 int TeaListModel::columnCount(const QModelIndex &parent) const 0038 { 0039 Q_UNUSED( parent ) 0040 0041 return 2; 0042 } 0043 0044 0045 QVariant TeaListModel::data(const QModelIndex &index, int role) const 0046 { 0047 if( !index.isValid() ) { 0048 return QVariant(); 0049 } 0050 0051 if( role == Qt::DisplayRole ) { 0052 if( index.column() == 0 ) { 0053 return m_tealist.at( index.row() ).name(); 0054 } 0055 0056 return m_tealist.at( index.row() ).timeToString(); 0057 } 0058 0059 if( role == Qt::EditRole ) { 0060 if( index.column() == 0 ) { 0061 return m_tealist.at( index.row() ).name(); 0062 } 0063 return m_tealist.at( index.row() ).time(); 0064 } 0065 0066 if( role == Qt::ToolTipRole ) { 0067 QString s( m_tealist.at( index.row() ).name() ); 0068 s.append( i18n( " (" ) ); 0069 s.append( m_tealist.at( index.row() ).timeToString( true ) ); 0070 s.append( i18n( ")" ) ); 0071 0072 return s; 0073 } 0074 0075 return QVariant(); 0076 } 0077 0078 0079 0080 bool TeaListModel::setData(const QModelIndex &index, const QVariant &value, int role) 0081 { 0082 if( index.isValid() && ( role == Qt::EditRole || role == Qt::DisplayRole ) ) { 0083 if( index.column() == 0 ) { 0084 m_tealist[ index.row() ].setName( value.toString() ); 0085 } else if( index.column() == 1 ) { 0086 m_tealist[ index.row() ].setTime( value.toUInt() ); 0087 } 0088 Q_EMIT dataChanged( index, index ); 0089 return true; 0090 } 0091 0092 return false; 0093 } 0094 0095 0096 QVariant TeaListModel::headerData(int section, Qt::Orientation orientation, int role) const 0097 { 0098 if( orientation == Qt::Horizontal ) { 0099 if( role == Qt::DisplayRole ) 0100 return section == 0 ? i18n("Name") : i18n("Time"); 0101 } 0102 0103 return QAbstractTableModel::headerData( section, orientation, role ); 0104 } 0105 0106 0107 bool TeaListModel::insertRows(int row, int count, const QModelIndex &parent) 0108 { 0109 Q_UNUSED( parent ) 0110 beginInsertRows( QModelIndex(), row, row+count-1 ); 0111 0112 for(int i = 0; i < count; ++i) { 0113 m_tealist.insert( row, Tea( i18n( "Unnamed Tea" ), 180 ) ); 0114 } 0115 0116 endInsertRows(); 0117 return true; 0118 } 0119 0120 0121 bool TeaListModel::removeRows(int row, int count, const QModelIndex &parent) 0122 { 0123 Q_UNUSED(parent) 0124 if( row-count > m_tealist.size() ) { 0125 return false; 0126 } 0127 0128 beginRemoveRows(QModelIndex(), row, row+count-1); 0129 for(int i=0; i < count; ++i) { 0130 m_tealist.removeAt( row ); 0131 } 0132 0133 endRemoveRows(); 0134 return true; 0135 } 0136 0137 0138 const QList<Tea> TeaListModel::getTeaList() const 0139 { 0140 return m_tealist; 0141 } 0142 0143 0144 // kate: word-wrap off; encoding utf-8; indent-width 4; tab-width 4; line-numbers on; mixed-indent off; remove-trailing-space-save on; replace-tabs-save on; replace-tabs on; space-indent on; 0145 // vim:set spell et sw=4 ts=4 nowrap cino=l1,cs,U1: 0146