~ubuntu-branches/ubuntu/lucid/ktorrent/lucid

« back to all changes in this revision

Viewing changes to plugins/scripting/scriptmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-02-16 18:37:14 UTC
  • mfrom: (1.1.25 upstream) (0.4.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090216183714-52tf47jrnmk4xkmp
Tags: 3.2+dfsg.1-2ubuntu1
* Merge with Debian, remaining changes: (LP: #296433)
  - Use Kubuntu's kde4.mk
  - Build-depend on libboost-serialization1.35-dev since unversioned -dev is
    in universe
  - Change plasma-applet-ktorrent to plasma-widget-ktorrent since we're
    supposed to call them widgets for the users

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 by Joris Guisson and Ivan Vasic                    *
 
3
 *   joris.guisson@gmail.com                                               *
 
4
 *   ivasic@gmail.com                                                      *
 
5
 *                                                                         *
 
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.                                   *
 
10
 *                                                                         *
 
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.                          *
 
15
 *                                                                         *
 
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
 ***************************************************************************/
 
21
#include <QAction>
 
22
#include <QVBoxLayout>
 
23
#include <util/log.h>
 
24
#include <kmenu.h>
 
25
#include <ktoolbar.h>
 
26
#include <kactioncollection.h>
 
27
#include <kross/core/manager.h>
 
28
#include "scriptmanager.h"
 
29
#include "scriptmodel.h"
 
30
#include "script.h"
 
31
 
 
32
using namespace Kross;
 
33
using namespace bt;
 
34
 
 
35
namespace kt
 
36
{
 
37
 
 
38
        ScriptManager::ScriptManager(ScriptModel* model,KActionCollection* ac,QWidget* parent)
 
39
                        : QWidget(parent),model(model)
 
40
        {
 
41
                QVBoxLayout* layout = new QVBoxLayout(this);
 
42
                layout->setSpacing(0);
 
43
                layout->setMargin(0);
 
44
                
 
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");
 
52
                
 
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);
 
68
                
 
69
                view = new QListView(this);
 
70
                layout->addWidget(view);
 
71
        
 
72
                view->setModel(model);
 
73
                view->setContextMenuPolicy(Qt::CustomContextMenu);
 
74
                view->setSelectionMode(QAbstractItemView::ExtendedSelection);
 
75
                view->setSelectionBehavior(QAbstractItemView::SelectRows);
 
76
                
 
77
                connect(view->selectionModel(),SIGNAL(selectionChanged(const QItemSelection &,const QItemSelection)),
 
78
                                this,SLOT(onSelectionChanged(const QItemSelection &,const QItemSelection)));
 
79
                
 
80
                connect(view,SIGNAL(customContextMenuRequested(const QPoint & )),
 
81
                                this,SLOT(showContextMenu(const QPoint& )));
 
82
                
 
83
                connect(model,SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
 
84
                                this,SLOT(dataChanged(const QModelIndex&, const QModelIndex&)));
 
85
                
 
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);
 
97
                
 
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);
 
105
        }
 
106
 
 
107
 
 
108
        ScriptManager::~ScriptManager()
 
109
        {
 
110
        }
 
111
 
 
112
        void ScriptManager::onSelectionChanged(const QItemSelection & selected,const QItemSelection & deselected)
 
113
        {
 
114
                Q_UNUSED(deselected);
 
115
                Q_UNUSED(selected);
 
116
                updateActions(selectedScripts());
 
117
        }
 
118
        
 
119
        void ScriptManager::updateActions(const QModelIndexList & selected)
 
120
        {
 
121
                int num_removeable = 0;
 
122
                int num_running = 0;
 
123
                int num_not_running = 0;
 
124
                foreach (const QModelIndex & idx,selected)
 
125
                {
 
126
                        Script* s = model->scriptForIndex(idx);
 
127
                        if (s)
 
128
                        {
 
129
                                if (s->running())
 
130
                                        num_running++;
 
131
                                else
 
132
                                        num_not_running++;
 
133
                                if (s->removeable())
 
134
                                        num_removeable++;
 
135
                        }
 
136
                        else
 
137
                                num_not_running++;
 
138
                }
 
139
                
 
140
                enableRemoveScript(num_removeable > 0);
 
141
                
 
142
                enableRunScript(selected.count() > 0 && num_not_running > 0);
 
143
                enableStopScript(selected.count() > 0 && num_running > 0);
 
144
                Script* s = 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());
 
149
        }
 
150
        
 
151
        QModelIndexList ScriptManager::selectedScripts()
 
152
        {
 
153
                return view->selectionModel()->selectedRows();
 
154
        }
 
155
 
 
156
        void ScriptManager::showContextMenu(const QPoint& p)
 
157
        {
 
158
                context_menu->popup(view->mapToGlobal(p));
 
159
        }
 
160
        
 
161
        void ScriptManager::dataChanged(const QModelIndex & from,const QModelIndex & to)
 
162
        {
 
163
                Q_UNUSED(from);
 
164
                Q_UNUSED(to);
 
165
                updateActions(selectedScripts());
 
166
        }
 
167
}