File indexing completed on 2024-05-05 04:48:25

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Daniel Dewald <Daniel.Dewald@time-shift.de>                       *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "dialogs/LabelListModel.h"
0018 
0019 LabelListModel::LabelListModel( const QStringList &labels, QObject *parent )
0020     : QAbstractListModel( parent )
0021     , m_labels( labels )
0022 {
0023 
0024 }
0025 
0026 int LabelListModel::rowCount( const QModelIndex &parent ) const
0027 {
0028     Q_UNUSED( parent );
0029     return m_labels.count();
0030 }
0031 
0032 QVariant LabelListModel::data( const QModelIndex &index, int role ) const
0033 {
0034     if ( !index.isValid() )
0035         return QVariant();
0036 
0037     if ( index.row() >= m_labels.size() )
0038         return QVariant();
0039 
0040     if ( role == Qt::DisplayRole )
0041         return m_labels.at( index.row() );
0042     else
0043         return QVariant();
0044 }
0045 
0046 QVariant LabelListModel::headerData( int section, Qt::Orientation orientation, int role ) const
0047 {
0048     Q_UNUSED( section );
0049     Q_UNUSED( orientation );
0050     Q_UNUSED( role );
0051     return QVariant();
0052 }
0053 
0054 Qt::ItemFlags LabelListModel::flags(const QModelIndex &index) const
0055 {
0056     if (!index.isValid())
0057         return Qt::ItemIsEnabled;
0058 
0059     return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
0060 }
0061 
0062 bool LabelListModel::setData( const QModelIndex &index, const QVariant &value, int role )
0063 {
0064     if ( index.isValid() && role == Qt::EditRole )
0065     {
0066         m_labels.replace( index.row(), value.toString() );
0067         Q_EMIT dataChanged( index, index );
0068         m_labels.sort();
0069         return true;
0070     }
0071     return false;
0072 }
0073 
0074 //Adds a label to the list if not present
0075 void LabelListModel::addLabel( const QString &label )
0076 {
0077     if ( ( !label.isEmpty() ) && ( !isPresent( label ) ) )
0078     {
0079         beginInsertRows( QModelIndex(), 0 , m_labels.length()+1 );
0080         m_labels << label ;
0081         m_labels.sort();
0082         endInsertRows();
0083     }
0084 }
0085 
0086 //Checks if a label is already present
0087 bool LabelListModel::isPresent( const QString &label )
0088 {
0089     if ( m_labels.indexOf( label ) > -1 )
0090     {
0091         return true;
0092     }
0093 
0094     return false;
0095 }
0096 
0097 // Removes the label "label" from the list if present
0098 void LabelListModel::removeLabel( const QString &label )
0099 {
0100     int index = m_labels.indexOf( label );
0101 
0102     if ( index >= 0 )
0103     {
0104         beginRemoveRows( QModelIndex(), index, index );
0105         m_labels.removeAt( index );
0106         endRemoveRows();
0107     }
0108 }
0109 
0110 //Wrapper function for remove label
0111 void LabelListModel::removeLabels( const QStringList &labels )
0112 {
0113     if ( !labels.isEmpty() )
0114     {
0115         for (int x = 0; x < labels.size(); ++x)
0116         {
0117             removeLabel( labels.at( x ) );
0118         }
0119     }
0120 }
0121 
0122 //Returns all labels in the list
0123 QStringList LabelListModel::labels() const
0124 {
0125     return m_labels;
0126 }
0127 
0128 //Sets label list to "labels"
0129 void LabelListModel::setLabels( const QStringList &labels )
0130 {
0131     beginInsertRows( QModelIndex(), 0, m_labels.length() );
0132     m_labels = labels;
0133     endInsertRows();
0134 }
0135 
0136 bool LabelListModel::insertRows( int position, int rows, const QModelIndex &parent )
0137 {
0138     Q_UNUSED( parent );
0139 
0140     beginInsertRows( QModelIndex(), position, position+rows-1 );
0141 
0142     for ( int row = 0; row < rows; ++row )
0143     {
0144         m_labels.insert( position, "" );
0145     }
0146 
0147     endInsertRows();
0148     return true;
0149 }
0150 
0151 bool LabelListModel::removeRows( int position, int rows, const QModelIndex &parent )
0152 {
0153     Q_UNUSED( parent );
0154 
0155     beginRemoveRows( QModelIndex(), position, position+rows-1 );
0156 
0157     for ( int row = 0; row < rows; ++row )
0158     {
0159         m_labels.removeAt( position );
0160     }
0161 
0162     endRemoveRows();
0163     return true;
0164 }