File indexing completed on 2024-05-12 04:39:40

0001 /*
0002     SPDX-FileCopyrightText: 2010 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-or-later
0005 */
0006 
0007 #include "includesmodel.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 IncludesModel::IncludesModel( QObject* parent )
0012     : QAbstractListModel( parent )
0013 {
0014 }
0015 
0016 QVariant IncludesModel::data( const QModelIndex& index, int role ) const
0017 {
0018     if( !index.isValid() || ( role != Qt::DisplayRole && role != Qt::EditRole ) ) {
0019         return QVariant();
0020     }
0021 
0022     if( index.row() < 0 || index.row() >= rowCount() || index.column() != 0 ) {
0023         return QVariant();
0024     }
0025 
0026     return m_includes.at( index.row() );
0027 }
0028 
0029 int IncludesModel::rowCount( const QModelIndex& parent ) const
0030 {
0031     return parent.isValid() ? 0 : m_includes.count();
0032 }
0033 
0034 bool IncludesModel::setData( const QModelIndex& index, const QVariant& value, int role )
0035 {
0036     if( !index.isValid() || role != Qt::EditRole ) {
0037         return false;
0038     }
0039     if( index.row() < 0 || index.row() >= rowCount() || index.column() != 0 ) {
0040         return false;
0041     }
0042 
0043     m_includes[index.row()] = value.toString().trimmed();
0044     emit dataChanged( index, index );
0045     return true;
0046 }
0047 
0048 Qt::ItemFlags IncludesModel::flags( const QModelIndex& index ) const
0049 {
0050     if( !index.isValid() ) {
0051         return Qt::NoItemFlags;
0052     }
0053 
0054     return Qt::ItemFlags( Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled );
0055 }
0056 
0057 QStringList IncludesModel::includes() const
0058 {
0059     return m_includes;
0060 }
0061 
0062 void IncludesModel::setIncludes(const QStringList& includes )
0063 {
0064     beginResetModel();
0065     m_includes.clear();
0066     for (const QString& includePath : includes) {
0067         addIncludeInternal( includePath.trimmed() );
0068     }
0069     endResetModel();
0070 }
0071 
0072 bool IncludesModel::removeRows( int row, int count, const QModelIndex& parent )
0073 {
0074     if( row >= 0 && count > 0 && row < m_includes.count() ) {
0075         beginRemoveRows( parent, row, row + count - 1 );
0076         for( int i = 0; i < count; ++i ) {
0077             m_includes.removeAt( row );
0078         }
0079         endRemoveRows();
0080         return true;
0081     }
0082     return false;
0083 }
0084 
0085 void IncludesModel::addInclude( const QString& includePath )
0086 {
0087     if( !includePath.isEmpty() ) {
0088         beginInsertRows( QModelIndex(), rowCount(), rowCount() );
0089         addIncludeInternal( includePath );
0090         endInsertRows();
0091     }
0092 }
0093 
0094 void IncludesModel::addIncludeInternal( const QString& includePath )
0095 {
0096     if ( includePath.isEmpty() ) {
0097         return;
0098     }
0099 
0100     // Do not allow duplicates
0101     if (m_includes.contains(includePath)) {
0102         return;
0103     }
0104 
0105     m_includes << includePath;
0106 }
0107 
0108 #include "moc_includesmodel.cpp"