File: projectpathsmodel.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (219 lines) | stat: -rw-r--r-- 6,721 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
    SPDX-FileCopyrightText: 2010 Andreas Pakulat <apaku@gmx.de>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-or-later
*/

#include "projectpathsmodel.h"

#include <KLocalizedString>
#include <interfaces/iproject.h>
#include <serialization/indexedstring.h>
#include <util/path.h>

using namespace KDevelop;

ProjectPathsModel::ProjectPathsModel( QObject* parent )
    : QAbstractListModel( parent )
{
}

void ProjectPathsModel::setProject(IProject* w_project)
{
    project = w_project;
}

QVariant ProjectPathsModel::data( const QModelIndex& index, int role ) const
{
    if( !index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0 ) {
        return QVariant();
    }

    const ConfigEntry& pathConfig = projectPaths.at( index.row() );
    switch( role ) {
    case IncludesDataRole:
        return pathConfig.includes;
    case DefinesDataRole:
        return QVariant::fromValue(pathConfig.defines);
    case Qt::EditRole:
        return sanitizePath( pathConfig.path, true, false );
    case Qt::DisplayRole: {
        const QString& path = pathConfig.path;
        return (path == QLatin1String(".")) ? QStringLiteral("(project root)") : path;
    }
    case FullUrlDataRole:
        return QVariant::fromValue(QUrl::fromUserInput( sanitizePath( pathConfig.path, true, false ) ));
    case CompilerDataRole:
        return QVariant::fromValue(pathConfig.compiler);
    case ParserArgumentsRole:
        return QVariant::fromValue(pathConfig.parserArguments);
    default:
        break;
    }
    return QVariant();
}

int ProjectPathsModel::rowCount( const QModelIndex& parent ) const
{
    if( parent.isValid() ) {
        return 0;
    }
    return projectPaths.count();
}

bool ProjectPathsModel::setData( const QModelIndex& index, const QVariant& value, int role )
{
    if( !index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0 ) {
        return false;
    }

    // Do not allow to change path of the first entry; instead, add a new one in that case
    if( index.row() == 0 && ( role == Qt::EditRole || role == Qt::DisplayRole || role == FullUrlDataRole ) ) {
        QString addedPath = sanitizePath( value.toString(), false );

        // Do not allow duplicates
        for (const ConfigEntry& existingConfig : std::as_const(projectPaths)) {
            if( addedPath == existingConfig.path ) {
                return false;
            }
        }
        projectPaths.insert( 1, ConfigEntry(sanitizePath( value.toString(), false ) ));
        emit dataChanged( this->index( 1, 0 ), this->index( projectPaths.count() - 1, 0 ) );
        return true;
    }

    ConfigEntry& pathConfig = projectPaths[ index.row() ];
    switch( role ) {
    case IncludesDataRole:
        pathConfig.includes = value.toStringList();
        break;
    case DefinesDataRole:
        pathConfig.defines = value.value<Defines>();
        break;
    case Qt::EditRole:
        pathConfig.path = sanitizePath( value.toString(), false );
        break;
    case Qt::DisplayRole:
        pathConfig.path = sanitizePath( value.toString(), true );
        break;
    case FullUrlDataRole:
        pathConfig.path = sanitizeUrl(value.toUrl());
        break;
    case CompilerDataRole:
        pathConfig.compiler = value.value<CompilerPointer>();
        break;
    case ParserArgumentsRole:
        pathConfig.parserArguments = value.value<ParserArguments>();
        break;
    default:
        return false;
    }
    emit dataChanged( index, index );
    return true;
}

Qt::ItemFlags ProjectPathsModel::flags( const QModelIndex& index ) const
{
    if( !index.isValid() ) {
        return Qt::NoItemFlags;
    }

    if( index.row() == 0 ) {
        return Qt::ItemFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
    }

    return Qt::ItemFlags( Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled );
}

QVector< ConfigEntry > ProjectPathsModel::paths() const
{
    return projectPaths;
}

void ProjectPathsModel::setPaths(const QVector< ConfigEntry >& paths )
{
    beginResetModel();
    projectPaths.clear();
    for (const ConfigEntry& existingPathConfig : paths) {
        // Sanitize the path of loaded config
        ConfigEntry config = existingPathConfig;
        bool rootPath = config.path == QLatin1String(".") ? true : false;
        config.path = sanitizePath(rootPath ? QString() : config.path );
        addPathInternal(config, rootPath);
    }
    addPathInternal( ConfigEntry(sanitizePath( QString() )), true ); // add an empty "root" config entry if one does not exist
    endResetModel();
}

bool ProjectPathsModel::removeRows( int row, int count, const QModelIndex& parent )
{
    if( row >= 0 && count > 0 && row < rowCount() ) {
        beginRemoveRows( parent, row, row + count - 1 );

        for( int i = 0; i < count; ++i ) {
            if( projectPaths.at(row).path == QLatin1String(".") ) {
                continue; // we won't remove the root item
            }
            projectPaths.removeAt(row);
        }

        endRemoveRows();
        return true;
    }
    return false;
}

void ProjectPathsModel::addPath( const QUrl &url )
{
    if( !project->path().isParentOf(KDevelop::Path(url)) ) {
        return;
    }

    beginInsertRows( QModelIndex(), rowCount(), rowCount() );
    addPathInternal( ConfigEntry(sanitizeUrl(url)), false );
    endInsertRows();
}

void ProjectPathsModel::addPathInternal( const ConfigEntry& config, bool prepend )
{
    Q_ASSERT(!config.parserArguments.isAnyEmpty());

    // Do not allow duplicates
    for (const ConfigEntry& existingConfig : std::as_const(projectPaths)) {
        if( config.path == existingConfig.path ) {
            return;
        }
    }
    if( prepend ) {
        projectPaths.prepend( config );
    } else {
        projectPaths.append( config );
    }
}

QString ProjectPathsModel::sanitizeUrl( const QUrl& url, bool needRelative ) const
{
    Q_ASSERT( project );

    if (needRelative) {
        const auto relativePath = project->path().relativePath(KDevelop::Path(url));
        return relativePath.isEmpty() ? QStringLiteral(".") : relativePath;
    }
    return url.adjusted(QUrl::StripTrailingSlash | QUrl::NormalizePathSegments).toString(QUrl::PreferLocalFile);
}

QString ProjectPathsModel::sanitizePath( const QString& path, bool expectRelative, bool needRelative ) const
{
    Q_ASSERT( project );
    Q_ASSERT( expectRelative || project->inProject(IndexedString(path)) );

    QUrl url;
    if( expectRelative ) {
        url = KDevelop::Path(project->path(), path).toUrl();
    } else {
        url = QUrl::fromUserInput(path);
    }
    return sanitizeUrl( url, needRelative );
}

#include "moc_projectpathsmodel.cpp"