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
|
/*
SPDX-FileCopyrightText: 2018 Amish K. Naidu <amhndu@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "scratchpadview.h"
#include "scratchpad.h"
#include <debug.h>
#include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/iuicontroller.h>
#include <interfaces/idocument.h>
#include <sublime/message.h>
#include <KLocalizedString>
#include <QAction>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include <QStyledItemDelegate>
#include <QWidgetAction>
#include <QLineEdit>
#include <QInputDialog>
#include <QPainter>
#include <QAbstractItemView>
// Use a delegate because the dataChanged signal doesn't tell us the previous name
class FileRenameDelegate
: public QStyledItemDelegate
{
Q_OBJECT
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(i18nc("@title:window", "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);
connect(m_scratchpad, &Scratchpad::actionFailed, this, [](const QString& messageText) {
// TODO: could be also messagewidget inside toolview?
auto* message = new Sublime::Message(messageText, Sublime::Message::Error);
KDevelop::ICore::self()->uiController()->postMessage(message);
});
connect(commandWidget, &QLineEdit::returnPressed, this, &ScratchpadView::runSelectedScratch);
connect(commandWidget, &QLineEdit::returnPressed, this, [this] {
m_scratchpad->setCommand(proxyModel()->mapToSource(currentIndex()), commandWidget->text());
});
commandWidget->setToolTip(i18nc("@info:tooltip", "Command to run this scratch. '$f' will expand to the scratch path."));
commandWidget->setPlaceholderText(commandWidget->toolTip());
// change active scratch when changing document
connect(KDevelop::ICore::self()->documentController(), &KDevelop::IDocumentController::documentActivated, this,
[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);
}
}
});
connect(scratchView, &QAbstractItemView::pressed, this, &ScratchpadView::validateItemActions);
validateItemActions();
}
void ScratchpadView::setupActions()
{
auto* action = new QAction(QIcon::fromTheme(QStringLiteral("list-add")), i18nc("@action", "New Scratch"), this);
connect(action, &QAction::triggered, this, &ScratchpadView::createScratch);
addAction(action);
action = new QAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@action", "Remove Scratch"), this);
connect(action, &QAction::triggered, this, [this] {
m_scratchpad->removeScratch(proxyModel()->mapToSource(currentIndex()));
validateItemActions();
});
addAction(action);
m_itemActions.push_back(action);
action = new QAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18nc("@action", "Rename Scratch"), this);
connect(action, &QAction::triggered, this, [this] {
scratchView->edit(scratchView->currentIndex());
});
addAction(action);
m_itemActions.push_back(action);
action = m_scratchpad->runAction();
action->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
action->setText(i18nc("@action", "Run Scratch"));
connect(action, &QAction::triggered, this, &ScratchpadView::runSelectedScratch);
addAction(action);
m_itemActions.push_back(action);
m_filter = new QLineEdit(this);
m_filter->setPlaceholderText(i18nc("@info:placeholder", "Filter..."));
auto filterAction = new QWidgetAction(this);
filterAction->setDefaultWidget(m_filter);
addAction(filterAction);
}
void ScratchpadView::validateItemActions()
{
bool enable = currentIndex().isValid();
for (auto* action : std::as_const(m_itemActions)) {
action->setEnabled(enable);
}
commandWidget->setReadOnly(!enable);
if (!enable) {
commandWidget->clear();
}
commandWidget->setText(currentIndex().data(Scratchpad::RunCommandRole).toString());
}
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, i18nc("@title:window", "Create New Scratch"),
i18nc("@label:textbox", "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();
}
#include "scratchpadview.moc"
#include "moc_emptymessagelistview.cpp"
#include "moc_scratchpadview.cpp"
|