Changeset View
Changeset View
Standalone View
Standalone View
plugins/scratchpad/scratchpadview.cpp
Show All 32 Lines | |||||
33 | #include <QAction> | 33 | #include <QAction> | ||
34 | #include <QStandardItemModel> | 34 | #include <QStandardItemModel> | ||
35 | #include <QSortFilterProxyModel> | 35 | #include <QSortFilterProxyModel> | ||
36 | #include <QStyledItemDelegate> | 36 | #include <QStyledItemDelegate> | ||
37 | #include <QWidgetAction> | 37 | #include <QWidgetAction> | ||
38 | #include <QLineEdit> | 38 | #include <QLineEdit> | ||
39 | #include <QInputDialog> | 39 | #include <QInputDialog> | ||
40 | #include <QPainter> | 40 | #include <QPainter> | ||
41 | #include <QAbstractItemView> | ||||
41 | 42 | | |||
42 | // Use a delegate because the dataChanged signal doesn't tell us the previous name | 43 | // Use a delegate because the dataChanged signal doesn't tell us the previous name | ||
43 | class FileRenameDelegate | 44 | class FileRenameDelegate | ||
44 | : public QStyledItemDelegate | 45 | : public QStyledItemDelegate | ||
45 | { | 46 | { | ||
46 | public: | 47 | public: | ||
47 | FileRenameDelegate(QObject* parent, Scratchpad* scratchpad) | 48 | FileRenameDelegate(QObject* parent, Scratchpad* scratchpad) | ||
48 | : QStyledItemDelegate(parent) | 49 | : QStyledItemDelegate(parent) | ||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Line(s) | 129 | if (document->url().isLocalFile()) { | |||
130 | const auto index = model->match(model->index(0, 0), Scratchpad::FullPathRole, | 131 | const auto index = model->match(model->index(0, 0), Scratchpad::FullPathRole, | ||
131 | document->url().toLocalFile()).value({}); | 132 | document->url().toLocalFile()).value({}); | ||
132 | if (index.isValid()) { | 133 | if (index.isValid()) { | ||
133 | scratchView->setCurrentIndex(index); | 134 | scratchView->setCurrentIndex(index); | ||
134 | } | 135 | } | ||
135 | } | 136 | } | ||
136 | }); | 137 | }); | ||
137 | 138 | | |||
139 | connect(scratchView, &QAbstractItemView::pressed, this, &ScratchpadView::validateItemActions); | ||||
140 | | ||||
138 | validateItemActions(); | 141 | validateItemActions(); | ||
139 | } | 142 | } | ||
140 | 143 | | |||
141 | void ScratchpadView::setupActions() | 144 | void ScratchpadView::setupActions() | ||
142 | { | 145 | { | ||
143 | QAction* action = new QAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("New Scratch"), this); | 146 | QAction* action = new QAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("New Scratch"), this); | ||
144 | connect(action, &QAction::triggered, this, &ScratchpadView::createScratch); | 147 | connect(action, &QAction::triggered, this, &ScratchpadView::createScratch); | ||
145 | addAction(action); | 148 | addAction(action); | ||
Show All 32 Lines | 178 | { | |||
178 | for (auto* action : m_itemActions) { | 181 | for (auto* action : m_itemActions) { | ||
179 | action->setEnabled(enable); | 182 | action->setEnabled(enable); | ||
180 | } | 183 | } | ||
181 | 184 | | |||
182 | commandWidget->setReadOnly(!enable); | 185 | commandWidget->setReadOnly(!enable); | ||
183 | if (!enable) { | 186 | if (!enable) { | ||
184 | commandWidget->clear(); | 187 | commandWidget->clear(); | ||
185 | } | 188 | } | ||
189 | commandWidget->setText(currentIndex().data(Scratchpad::RunCommandRole).toString()); | ||||
186 | } | 190 | } | ||
187 | 191 | | |||
188 | void ScratchpadView::runSelectedScratch() | 192 | void ScratchpadView::runSelectedScratch() | ||
189 | { | 193 | { | ||
190 | const auto sourceIndex = proxyModel()->mapToSource(currentIndex()); | 194 | const auto sourceIndex = proxyModel()->mapToSource(currentIndex()); | ||
191 | if (auto* document = KDevelop::ICore::self()->documentController()->documentForUrl( | 195 | if (auto* document = KDevelop::ICore::self()->documentController()->documentForUrl( | ||
192 | QUrl::fromLocalFile(sourceIndex.data(Scratchpad::FullPathRole).toString()))) { | 196 | QUrl::fromLocalFile(sourceIndex.data(Scratchpad::FullPathRole).toString()))) { | ||
193 | document->save(); | 197 | document->save(); | ||
194 | } | 198 | } | ||
195 | m_scratchpad->setCommand(sourceIndex, commandWidget->text()); | 199 | m_scratchpad->setCommand(sourceIndex, commandWidget->text()); | ||
196 | m_scratchpad->runScratch(sourceIndex); | 200 | m_scratchpad->runScratch(sourceIndex); | ||
197 | } | 201 | } | ||
198 | 202 | | |||
199 | void ScratchpadView::scratchActivated(const QModelIndex& index) | 203 | void ScratchpadView::scratchActivated(const QModelIndex& index) | ||
200 | { | 204 | { | ||
201 | validateItemActions(); | 205 | validateItemActions(); | ||
202 | m_scratchpad->openScratch(proxyModel()->mapToSource(index)); | 206 | m_scratchpad->openScratch(proxyModel()->mapToSource(index)); | ||
203 | commandWidget->setText(index.data(Scratchpad::RunCommandRole).toString()); | | |||
204 | } | 207 | } | ||
205 | 208 | | |||
206 | void ScratchpadView::createScratch() | 209 | void ScratchpadView::createScratch() | ||
207 | { | 210 | { | ||
208 | QString name = QInputDialog::getText(this, i18n("Create New Scratch"), | 211 | QString name = QInputDialog::getText(this, i18n("Create New Scratch"), | ||
209 | i18n("Enter name for scratch file:"), | 212 | i18n("Enter name for scratch file:"), | ||
210 | QLineEdit::Normal, | 213 | QLineEdit::Normal, | ||
211 | QStringLiteral("example.cpp")); | 214 | QStringLiteral("example.cpp")); | ||
Show All 14 Lines |