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
|
/*
This plugin is part of KDevelop.
Copyright (C) 2010 Milian Wolff <mail@milianw.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "externalscriptview.h"
#include "externalscriptplugin.h"
#include "externalscriptitem.h"
#include "editexternalscript.h"
#include <QAction>
#include <QMenu>
#include <QMouseEvent>
#include <QSortFilterProxyModel>
#include <KLocalizedString>
#include <KMessageBox>
#include <util/scopeddialog.h>
ExternalScriptView::ExternalScriptView(ExternalScriptPlugin* plugin, QWidget* parent)
: QWidget(parent)
, m_plugin(plugin)
{
Ui::ExternalScriptViewBase::setupUi(this);
setFocusProxy(filterText);
setWindowTitle(i18nc("@title:window", "External Scripts"));
setWindowIcon(QIcon::fromTheme(QStringLiteral("dialog-scripts"), windowIcon()));
m_model = new QSortFilterProxyModel(this);
m_model->setSourceModel(m_plugin->model());
m_model->setDynamicSortFilter(true);
m_model->sort(0);
connect(filterText, &QLineEdit::textEdited,
m_model, &QSortFilterProxyModel::setFilterWildcard);
scriptTree->setModel(m_model);
scriptTree->setContextMenuPolicy(Qt::CustomContextMenu);
scriptTree->viewport()->installEventFilter(this);
scriptTree->header()->hide();
connect(scriptTree, &QTreeView::customContextMenuRequested,
this, &ExternalScriptView::contextMenu);
m_addScriptAction =
new QAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@action", "Add External Script"), this);
connect(m_addScriptAction, &QAction::triggered, this, &ExternalScriptView::addScript);
addAction(m_addScriptAction);
m_editScriptAction = new QAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18nc("@action",
"Edit External Script"), this);
connect(m_editScriptAction, &QAction::triggered, this, &ExternalScriptView::editScript);
addAction(m_editScriptAction);
m_removeScriptAction = new QAction(QIcon::fromTheme(QStringLiteral("document-close")), i18nc("@action",
"Remove External Script"), this);
connect(m_removeScriptAction, &QAction::triggered, this, &ExternalScriptView::removeScript);
addAction(m_removeScriptAction);
connect(scriptTree->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &ExternalScriptView::validateActions);
validateActions();
}
ExternalScriptView::~ExternalScriptView()
{
}
ExternalScriptItem* ExternalScriptView::currentItem() const
{
return itemForIndex(scriptTree->currentIndex());
}
ExternalScriptItem* ExternalScriptView::itemForIndex(const QModelIndex& index) const
{
if (!index.isValid()) {
return nullptr;
}
const QModelIndex mappedIndex = m_model->mapToSource(index);
return static_cast<ExternalScriptItem*>(m_plugin->model()->itemFromIndex(mappedIndex));
}
void ExternalScriptView::validateActions()
{
bool itemSelected = currentItem();
m_removeScriptAction->setEnabled(itemSelected);
m_editScriptAction->setEnabled(itemSelected);
}
void ExternalScriptView::contextMenu(const QPoint& pos)
{
QMenu menu(this);
menu.addActions(actions());
menu.exec(scriptTree->viewport()->mapToGlobal(pos));
}
bool ExternalScriptView::eventFilter(QObject* obj, QEvent* e)
{
// no, listening to activated() is not enough since that would also trigger the edit mode which we do _not_ want here
// users may still rename stuff via select + F2 though
if (obj == scriptTree->viewport()) {
// const bool singleClick = KGlobalSettings::singleClick();
const bool singleClick = true; //FIXME: enable singleClick for the sake of porting, should find a proper way
if ((!singleClick && e->type() == QEvent::MouseButtonDblClick) ||
(singleClick && e->type() == QEvent::MouseButtonRelease)) {
auto* mouseEvent = dynamic_cast<QMouseEvent*>(e);
Q_ASSERT(mouseEvent);
ExternalScriptItem* item = itemForIndex(scriptTree->indexAt(mouseEvent->pos()));
if (item) {
m_plugin->execute(item);
e->accept();
return true;
}
}
}
return QObject::eventFilter(obj, e);
}
void ExternalScriptView::addScript()
{
auto* item = new ExternalScriptItem;
KDevelop::ScopedDialog<EditExternalScript> dlg(item, this);
if (dlg->exec() == QDialog::Accepted) {
m_plugin->model()->appendRow(item);
} else {
delete item;
}
}
void ExternalScriptView::removeScript()
{
ExternalScriptItem* item = currentItem();
if (!item) {
return;
}
int ret = KMessageBox::questionYesNo(this,
i18n(
"<p>Do you really want to remove the external script configuration for <i>%1</i>?</p>"
"<p><i>Note:</i> The script itself will not be removed.</p>",
item->text()),
i18nc("@title:window", "Confirm External Script Removal")
);
if (ret == KMessageBox::Yes) {
m_plugin->model()->removeRow(
m_plugin->model()->indexFromItem(item).row()
);
}
}
void ExternalScriptView::editScript()
{
ExternalScriptItem* item = currentItem();
if (!item) {
return;
}
KDevelop::ScopedDialog<EditExternalScript> dlg(item, this);
if (dlg->exec() == QDialog::Accepted) {
item->save();
}
}
|