1
/***************************************************************************
2
* Copyright (C) 2008 by Joris Guisson and Ivan Vasic *
3
* joris.guisson@gmail.com *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20
***************************************************************************/
22
#include <QVBoxLayout>
26
#include <kactioncollection.h>
27
#include <kross/core/manager.h>
28
#include "scriptmanager.h"
29
#include "scriptmodel.h"
32
using namespace Kross;
38
ScriptManager::ScriptManager(ScriptModel* model,KActionCollection* ac,QWidget* parent)
39
: QWidget(parent),model(model)
41
QVBoxLayout* layout = new QVBoxLayout(this);
42
layout->setSpacing(0);
45
QAction* remove = ac->action("remove_script");
46
QAction* add = ac->action("add_script");
47
QAction* run = ac->action("run_script");
48
QAction* stop = ac->action("stop_script");
49
QAction* edit = ac->action("edit_script");
50
QAction* properties = ac->action("script_properties");
51
QAction* configure = ac->action("configure_script");
53
toolbar = new KToolBar(this);
54
toolbar->setToolButtonStyle(Qt::ToolButtonIconOnly);
55
layout->addWidget(toolbar);
56
toolbar->addAction(add);
57
toolbar->addAction(remove);
58
toolbar->addAction(run);
59
toolbar->addAction(stop);
60
toolbar->addAction(configure);
61
connect(this,SIGNAL(enableRemoveScript(bool)),remove,SLOT(setEnabled(bool)));
62
connect(this,SIGNAL(enableRemoveScript(bool)),edit,SLOT(setEnabled(bool)));
63
connect(this,SIGNAL(enableStopScript(bool)),stop,SLOT(setEnabled(bool)));
64
connect(this,SIGNAL(enableRunScript(bool)),run,SLOT(setEnabled(bool)));
65
connect(this,SIGNAL(enableProperties(bool)),properties,SLOT(setEnabled(bool)));
66
connect(this,SIGNAL(enableConfigure(bool)),configure,SLOT(setEnabled(bool)));
67
remove->setEnabled(false);
69
view = new QListView(this);
70
layout->addWidget(view);
72
view->setModel(model);
73
view->setContextMenuPolicy(Qt::CustomContextMenu);
74
view->setSelectionMode(QAbstractItemView::ExtendedSelection);
75
view->setSelectionBehavior(QAbstractItemView::SelectRows);
77
connect(view->selectionModel(),SIGNAL(selectionChanged(const QItemSelection &,const QItemSelection)),
78
this,SLOT(onSelectionChanged(const QItemSelection &,const QItemSelection)));
80
connect(view,SIGNAL(customContextMenuRequested(const QPoint & )),
81
this,SLOT(showContextMenu(const QPoint& )));
83
connect(model,SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
84
this,SLOT(dataChanged(const QModelIndex&, const QModelIndex&)));
86
context_menu = new KMenu(this);
87
context_menu->addAction(add);
88
context_menu->addAction(remove);
89
context_menu->addSeparator();
90
context_menu->addAction(run);
91
context_menu->addAction(stop);
92
context_menu->addSeparator();
93
context_menu->addAction(edit);
94
context_menu->addSeparator();
95
context_menu->addAction(properties);
96
context_menu->addAction(configure);
98
add->setEnabled(true);
99
remove->setEnabled(false);
100
run->setEnabled(false);
101
stop->setEnabled(false);
102
edit->setEnabled(false);
103
properties->setEnabled(false);
104
configure->setEnabled(false);
108
ScriptManager::~ScriptManager()
112
void ScriptManager::onSelectionChanged(const QItemSelection & selected,const QItemSelection & deselected)
114
Q_UNUSED(deselected);
116
updateActions(selectedScripts());
119
void ScriptManager::updateActions(const QModelIndexList & selected)
121
int num_removeable = 0;
123
int num_not_running = 0;
124
foreach (const QModelIndex & idx,selected)
126
Script* s = model->scriptForIndex(idx);
140
enableRemoveScript(num_removeable > 0);
142
enableRunScript(selected.count() > 0 && num_not_running > 0);
143
enableStopScript(selected.count() > 0 && num_running > 0);
145
if (selected.count() > 0)
146
s = model->scriptForIndex(selected.front());
147
enableProperties(selected.count() == 1 && s && s->metaInfo().valid());
148
enableConfigure(selected.count() == 1 && s && s->hasConfigure());
151
QModelIndexList ScriptManager::selectedScripts()
153
return view->selectionModel()->selectedRows();
156
void ScriptManager::showContextMenu(const QPoint& p)
158
context_menu->popup(view->mapToGlobal(p));
161
void ScriptManager::dataChanged(const QModelIndex & from,const QModelIndex & to)
165
updateActions(selectedScripts());