scratchpadview.cpp 8.02 KB
Newer Older
Amish Naidu's avatar
Amish Naidu committed
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
/* This file is part of KDevelop
 *
 * Copyright 2018 Amish K. Naidu <amhndu@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU 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 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 "scratchpadview.h"
#include "scratchpad.h"

#include <debug.h>

#include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/idocument.h>

#include <KLocalizedString>
#include <KMessageBox>

#include <QAction>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include <QStyledItemDelegate>
#include <QWidgetAction>
#include <QLineEdit>
#include <QInputDialog>
#include <QPainter>
41
#include <QAbstractItemView>
Amish Naidu's avatar
Amish Naidu committed
42
43
44
45
46

// Use a delegate because the dataChanged signal doesn't tell us the previous name
class FileRenameDelegate
    : public QStyledItemDelegate
{
47
48
    Q_OBJECT

Amish Naidu's avatar
Amish Naidu committed
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
public:
    FileRenameDelegate(QObject* parent, Scratchpad* scratchpad)
        : QStyledItemDelegate(parent)
        , m_scratchpad(scratchpad)
    {
    }

    void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override
    {
        const QString previousName = index.data().toString();
        QStyledItemDelegate::setModelData(editor, model, index);
        const auto* proxyModel = static_cast<QAbstractProxyModel*>(model);
        m_scratchpad->renameScratch(proxyModel->mapToSource(index), previousName);
    }

private:
    Scratchpad* m_scratchpad;
};

EmptyMessageListView::EmptyMessageListView(QWidget* parent)
    : QListView(parent)
{
}

void EmptyMessageListView::paintEvent(QPaintEvent* event)
{
    if (model() && model()->rowCount(rootIndex()) > 0) {
        QListView::paintEvent(event);
    } else {
        QPainter painter(viewport());
        const auto margin =
            QMargins(parentWidget()->style()->pixelMetric(QStyle::PM_LayoutLeftMargin), 0,
                     parentWidget()->style()->pixelMetric(QStyle::PM_LayoutRightMargin), 0);
        painter.drawText(rect() - margin, Qt::AlignCenter | Qt::TextWordWrap, m_message);
    }
}

void EmptyMessageListView::setEmptyMessage(const QString& message)
{
    m_message = message;
}


ScratchpadView::ScratchpadView(QWidget* parent, Scratchpad* scratchpad)
    : QWidget(parent)
    , m_scratchpad(scratchpad)
{
    setupUi(this);

    setupActions();

    setWindowTitle(i18n("Scratchpad"));
    setWindowIcon(QIcon::fromTheme(QStringLiteral("note")));

    auto* const modelProxy = new QSortFilterProxyModel(this);
    modelProxy->setSourceModel(m_scratchpad->model());
    modelProxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
    modelProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
    modelProxy->setSortRole(Qt::DisplayRole);
    connect(m_filter, &QLineEdit::textEdited,
            modelProxy, &QSortFilterProxyModel::setFilterWildcard);

    scratchView->setModel(modelProxy);
    scratchView->setItemDelegate(new FileRenameDelegate(this, m_scratchpad));
    scratchView->setEmptyMessage(i18n("Scratchpad lets you quickly run and experiment with code without a full project, and even store todos. Create a new scratch to start."));

    connect(scratchView, &QListView::activated, this, &ScratchpadView::scratchActivated);

117
    connect(m_scratchpad, &Scratchpad::actionFailed, this, [this](const QString& message) {
Amish Naidu's avatar
Amish Naidu committed
118
119
120
121
        KMessageBox::sorry(this, message);
    });

    connect(commandWidget, &QLineEdit::returnPressed, this, &ScratchpadView::runSelectedScratch);
122
    connect(commandWidget, &QLineEdit::returnPressed, this, [this] {
Amish Naidu's avatar
Amish Naidu committed
123
124
125
126
127
128
        m_scratchpad->setCommand(proxyModel()->mapToSource(currentIndex()), commandWidget->text());
    });
    commandWidget->setToolTip(i18n("Command to run this scratch. $f will expand to the scratch path"));
    commandWidget->setPlaceholderText(commandWidget->toolTip());

    // change active scratch when changing document
129
    connect(KDevelop::ICore::self()->documentController(), &KDevelop::IDocumentController::documentActivated, this,
Amish Naidu's avatar
Amish Naidu committed
130
131
132
133
134
135
136
137
138
139
140
        [this](const KDevelop::IDocument* document) {
        if (document->url().isLocalFile()) {
            const auto* model = scratchView->model();
            const auto index = model->match(model->index(0, 0), Scratchpad::FullPathRole,
                                            document->url().toLocalFile()).value({});
            if (index.isValid()) {
                scratchView->setCurrentIndex(index);
            }
        }
    });

141
142
    connect(scratchView, &QAbstractItemView::pressed, this, &ScratchpadView::validateItemActions);

Amish Naidu's avatar
Amish Naidu committed
143
144
145
146
147
148
149
150
151
152
    validateItemActions();
}

void ScratchpadView::setupActions()
{
    QAction* action = new QAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("New Scratch"), this);
    connect(action, &QAction::triggered, this, &ScratchpadView::createScratch);
    addAction(action);

    action = new QAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Remove Scratch"), this);
153
    connect(action, &QAction::triggered, this, [this] {
Amish Naidu's avatar
Amish Naidu committed
154
155
156
157
158
159
160
        m_scratchpad->removeScratch(proxyModel()->mapToSource(currentIndex()));
        validateItemActions();
    });
    addAction(action);
    m_itemActions.push_back(action);

    action = new QAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18n("Rename Scratch"), this);
161
    connect(action, &QAction::triggered, this, [this] {
Amish Naidu's avatar
Amish Naidu committed
162
163
164
165
166
        scratchView->edit(scratchView->currentIndex());
    });
    addAction(action);
    m_itemActions.push_back(action);

167
168
169
    action = m_scratchpad->runAction();
    action->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
    action->setText(i18n("Run Scratch"));
Amish Naidu's avatar
Amish Naidu committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    connect(action, &QAction::triggered, this, &ScratchpadView::runSelectedScratch);
    addAction(action);
    m_itemActions.push_back(action);

    m_filter = new QLineEdit(this);
    m_filter->setPlaceholderText(i18n("Filter..."));
    auto filterAction = new QWidgetAction(this);
    filterAction->setDefaultWidget(m_filter);
    addAction(filterAction);
}

void ScratchpadView::validateItemActions()
{
    bool enable = currentIndex().isValid();

185
    for (auto* action : qAsConst(m_itemActions)) {
Amish Naidu's avatar
Amish Naidu committed
186
187
188
189
190
191
192
        action->setEnabled(enable);
    }

    commandWidget->setReadOnly(!enable);
    if (!enable) {
        commandWidget->clear();
    }
193
    commandWidget->setText(currentIndex().data(Scratchpad::RunCommandRole).toString());
Amish Naidu's avatar
Amish Naidu committed
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
}

void ScratchpadView::runSelectedScratch()
{
    const auto sourceIndex = proxyModel()->mapToSource(currentIndex());
    if (auto* document = KDevelop::ICore::self()->documentController()->documentForUrl(
        QUrl::fromLocalFile(sourceIndex.data(Scratchpad::FullPathRole).toString()))) {
            document->save();
    }
    m_scratchpad->setCommand(sourceIndex, commandWidget->text());
    m_scratchpad->runScratch(sourceIndex);
}

void ScratchpadView::scratchActivated(const QModelIndex& index)
{
    validateItemActions();
    m_scratchpad->openScratch(proxyModel()->mapToSource(index));
}

void ScratchpadView::createScratch()
{
    QString name = QInputDialog::getText(this, i18n("Create New Scratch"),
                                         i18n("Enter name for scratch file:"),
                                         QLineEdit::Normal,
                                         QStringLiteral("example.cpp"));
    if (!name.isEmpty()) {
        m_scratchpad->createScratch(name);
    }
}

QAbstractProxyModel* ScratchpadView::proxyModel() const
{
    return static_cast<QAbstractProxyModel*>(scratchView->model());
}

QModelIndex ScratchpadView::currentIndex() const
{
    return scratchView->currentIndex();
}
233
234
235

#include "scratchpadview.moc"
#include "moc_emptymessagelistview.cpp"