File: projectitemlineedit.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (277 lines) | stat: -rw-r--r-- 9,546 bytes parent folder | download
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/***************************************************************************
 *   Copyright 2008 Aleix Pol <aleixpol@gmail.com>                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "projectitemlineedit.h"

#include <QAction>
#include <QCompleter>
#include <QDialog>
#include <QDialogButtonBox>
#include <QHeaderView>
#include <QLabel>
#include <QMenu>
#include <QPushButton>
#include <QTreeView>
#include <QValidator>
#include <QVBoxLayout>

#include <KLocalizedString>

#include <interfaces/icore.h>
#include <interfaces/iprojectcontroller.h>
#include <project/projectmodel.h>
#include <util/kdevstringhandler.h>
#include <interfaces/iproject.h>
#include "projectproxymodel.h"

constexpr QChar sep = QLatin1Char('/');
constexpr QChar escape = QLatin1Char('\\');


class ProjectItemCompleter : public QCompleter
{
    Q_OBJECT
public:
    explicit ProjectItemCompleter(QObject* parent=nullptr);

    QString separator() const { return sep; }
    QStringList splitPath(const QString &path) const override;
    QString pathFromIndex(const QModelIndex& index) const override;

    void setBaseItem( KDevelop::ProjectBaseItem* item ) { mBase = item; }

private:
    KDevelop::ProjectModel* mModel;
    KDevelop::ProjectBaseItem* mBase = nullptr;
};

class ProjectItemValidator : public QValidator
{
    Q_OBJECT
public:
    explicit ProjectItemValidator(QObject* parent = nullptr );
    QValidator::State validate( QString& input, int& pos ) const override;

    void setBaseItem( KDevelop::ProjectBaseItem* item ) { mBase = item; }

private:
    KDevelop::ProjectBaseItem* mBase = nullptr;
};

ProjectItemCompleter::ProjectItemCompleter(QObject* parent)
    : QCompleter(parent)
    , mModel(KDevelop::ICore::self()->projectController()->projectModel())

{
    setModel(mModel);
    setCaseSensitivity( Qt::CaseInsensitive );
}


QStringList ProjectItemCompleter::splitPath(const QString& path) const
{
    return joinProjectBasePath( KDevelop::splitWithEscaping( path, sep, escape ), mBase );
}

QString ProjectItemCompleter::pathFromIndex(const QModelIndex& index) const
{
    QString postfix;
    if(mModel->itemFromIndex(index)->folder())
        postfix=sep;
    return KDevelop::joinWithEscaping(removeProjectBasePath( mModel->pathFromIndex(index), mBase ), sep, escape)+postfix;
}


ProjectItemValidator::ProjectItemValidator(QObject* parent): QValidator(parent)
{
}


QValidator::State ProjectItemValidator::validate(QString& input, int& pos) const
{
    Q_UNUSED( pos );
    KDevelop::ProjectModel* model = KDevelop::ICore::self()->projectController()->projectModel();
    QStringList path = joinProjectBasePath( KDevelop::splitWithEscaping( input, sep, escape ), mBase );
    QModelIndex idx = model->pathToIndex( path );
    QValidator::State state = input.isEmpty() ? QValidator::Intermediate : QValidator::Invalid;
    if( idx.isValid() )
    {
        state = QValidator::Acceptable;
    } else if( path.count() > 1 )
    {
        // Check beginning of path and if that is ok, then try to find a child
        QString end = path.takeLast();
        idx = model->pathToIndex( path );
        if( idx.isValid() )
        {
            for( int i = 0; i < model->rowCount( idx ); i++ )
            {
                if( model->data( model->index( i, 0, idx ) ).toString().startsWith( end, Qt::CaseInsensitive ) )
                {
                    state = QValidator::Intermediate;
                    break;
                }
            }
        }
    } else if( path.count() == 1 )
    {
        // Check for a project whose name beings with the input
        QString first = path.first();
        const auto projects = KDevelop::ICore::self()->projectController()->projects();
        bool matchesAnyName = std::any_of(projects.begin(), projects.end(), [&](KDevelop::IProject* project) {
            return (project->name().startsWith(first, Qt::CaseInsensitive));
        });
        if (matchesAnyName) {
            state = QValidator::Intermediate;
        }
    }
    return state;
}

class ProjectItemLineEditPrivate
{
public:
    explicit ProjectItemLineEditPrivate(ProjectItemLineEdit* q)
        : completer(new ProjectItemCompleter(q))
        , validator(new ProjectItemValidator(q))
    {
    }
    KDevelop::ProjectBaseItem* base = nullptr;
    ProjectItemCompleter* completer;
    ProjectItemValidator* validator;
    KDevelop::IProject* suggestion = nullptr;
};

ProjectItemLineEdit::ProjectItemLineEdit(QWidget* parent)
    : QLineEdit(parent),
      d_ptr(new ProjectItemLineEditPrivate(this))
{
    Q_D(ProjectItemLineEdit);

    setCompleter(d->completer);
    setValidator(d->validator);
    setPlaceholderText( i18nc("@info:placeholder", "Enter the path to an item from the projects tree..." ) );

    auto* selectItemAction = new QAction(QIcon::fromTheme(QStringLiteral("folder-document")), i18nc("@action", "Select..."), this);
    connect(selectItemAction, &QAction::triggered, this, &ProjectItemLineEdit::selectItemDialog);
    addAction(selectItemAction);

    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, &ProjectItemLineEdit::customContextMenuRequested, this, &ProjectItemLineEdit::showCtxMenu);
}

ProjectItemLineEdit::~ProjectItemLineEdit() = default;

void ProjectItemLineEdit::showCtxMenu(const QPoint& p)
{
    QScopedPointer<QMenu> menu(createStandardContextMenu());
    menu->addActions(actions());
    menu->exec(mapToGlobal(p));
}

bool ProjectItemLineEdit::selectItemDialog()
{
    Q_D(ProjectItemLineEdit);

    KDevelop::ProjectModel* model=KDevelop::ICore::self()->projectController()->projectModel();

    QDialog dialog;
    dialog.setWindowTitle(i18nc("@title:window", "Select an Item"));

    auto mainLayout = new QVBoxLayout(&dialog);

    auto* view = new QTreeView(&dialog);
    auto* proxymodel = new ProjectProxyModel(view);
    proxymodel->setSourceModel(model);
    view->header()->hide();
    view->setModel(proxymodel);
    view->setSelectionMode(QAbstractItemView::SingleSelection);
    mainLayout->addWidget(new QLabel(i18n("Select the item you want to get the path from.")));
    mainLayout->addWidget(view);

    auto* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
    QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
    okButton->setDefault(true);
    okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
    mainLayout->addWidget(buttonBox);

    if (d->suggestion) {
        const QModelIndex idx = proxymodel->proxyIndexFromItem(d->suggestion->projectItem());
        view->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
    }

    int res = dialog.exec();

    if(res==QDialog::Accepted && view->selectionModel()->hasSelection()) {
        QModelIndex idx=proxymodel->mapToSource(view->selectionModel()->selectedIndexes().first());

        setText(KDevelop::joinWithEscaping(model->pathFromIndex(idx), sep, escape));
        selectAll();
        return true;
    }
    return false;
}

void ProjectItemLineEdit::setItemPath(const QStringList& list)
{
    Q_D(ProjectItemLineEdit);

    setText(KDevelop::joinWithEscaping(removeProjectBasePath(list, d->base), sep, escape));
}

QStringList ProjectItemLineEdit::itemPath() const
{
    Q_D(const ProjectItemLineEdit);

    return joinProjectBasePath(KDevelop::splitWithEscaping(text(), sep, escape), d->base);
}

void ProjectItemLineEdit::setBaseItem(KDevelop::ProjectBaseItem* item)
{
    Q_D(ProjectItemLineEdit);

    d->base = item;
    d->validator->setBaseItem(d->base);
    d->completer->setBaseItem(d->base);
}

KDevelop::ProjectBaseItem* ProjectItemLineEdit::baseItem() const
{
    Q_D(const ProjectItemLineEdit);

    return d->base;
}

KDevelop::ProjectBaseItem* ProjectItemLineEdit::currentItem() const
{
    KDevelop::ProjectModel* model = KDevelop::ICore::self()->projectController()->projectModel();
    return model->itemFromIndex(model->pathToIndex(KDevelop::splitWithEscaping(text(), QLatin1Char('/'), QLatin1Char('\\'))));
}

void ProjectItemLineEdit::setSuggestion(KDevelop::IProject* project)
{
    Q_D(ProjectItemLineEdit);

    d->suggestion = project;
}

#include "projectitemlineedit.moc"
#include "moc_projectitemlineedit.cpp"