1 /***************************************************************************
2  * KoScriptingDocker.cpp
3  * This file is part of the KDE project
4  * copyright (C) 2006-2007 Sebastian Sauer <mail@dipe.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  ***************************************************************************/
21 
22 #include "KoScriptingDocker.h"
23 
24 #include "KoScriptingModule.h"
25 #include "KoScriptManager.h"
26 #include "KoKrossDebug.h"
27 
28 #include <KoIcon.h>
29 
30 #include <QToolBar>
31 #include <QBoxLayout>
32 
33 #include <klocalizedstring.h>
34 #include <kactioncollection.h>
35 
36 #include <kross/core/action.h>
37 #include <kross/ui/model.h>
38 #include <kross/ui/view.h>
39 
40 /***********************************************************************
41  * KoScriptingDockerFactory
42  */
43 
KoScriptingDockerFactory(QWidget * parent,KoScriptingModule * module,Kross::Action * action)44 KoScriptingDockerFactory::KoScriptingDockerFactory(QWidget *parent, KoScriptingModule *module, Kross::Action *action)
45     : KoDockFactoryBase(),
46     m_parent(parent),
47     m_module(module),
48     m_action(action)
49 {
50 }
51 
id() const52 QString KoScriptingDockerFactory::id() const
53 {
54     return m_action ? m_action->name() : "Scripting";
55 }
56 
defaultDockPosition() const57 KoDockFactoryBase::DockPosition  KoScriptingDockerFactory::defaultDockPosition() const
58 {
59     return DockMinimized;
60 }
61 
createDockWidget()62 QDockWidget *KoScriptingDockerFactory::createDockWidget()
63 {
64     QDockWidget *dw = 0;
65     if (m_action)
66         dw = new KoScriptingActionDocker(m_module, m_action, m_parent);
67     else
68         dw = new KoScriptingDocker(m_parent);
69     dw->setObjectName(id());
70     return dw;
71 }
72 
73 /***********************************************************************
74  * KoScriptingDocker
75  */
76 
KoScriptingDocker(QWidget * parent)77 KoScriptingDocker::KoScriptingDocker(QWidget *parent)
78     : QDockWidget(i18n("Scripts"), parent)
79 {
80     QWidget *widget = new QWidget(this);
81     QBoxLayout *layout = new QVBoxLayout(widget);
82     layout->setMargin(0);
83     widget->setLayout(layout);
84 
85     m_view = new Kross::ActionCollectionView(widget);
86     m_view->setRootIsDecorated(false);
87 
88     //Kross::ActionCollectionModel::Mode modelmode = Kross::ActionCollectionModel::Mode(Kross::ActionCollectionModel::ToolTips);
89     //d->model = new Kross::ActionCollectionProxyModel(this, new Kross::ActionCollectionModel(this, 0, modelmode));
90     Kross::ActionCollectionProxyModel *model = new Kross::ActionCollectionProxyModel(this);
91 
92     m_view->setModel(model);
93     layout->addWidget(m_view, 1);
94     m_view->expandAll();
95 
96     QToolBar *tb = new QToolBar(widget);
97     layout->addWidget(tb);
98     tb->setMovable(false);
99     //tb->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
100 
101     KActionCollection *collection = m_view->actionCollection();
102     if (QAction *a = collection->action("run")) {
103         a = tb->addAction(a->icon(), a->text(), a, SLOT(trigger()));
104         a->setEnabled(false);
105         m_actions.insert("run", a);
106     }
107     if (QAction *a = collection->action("stop")) {
108         a = tb->addAction(a->icon(), a->text(), a, SLOT(trigger()));
109         a->setEnabled(false);
110         m_actions.insert("stop", a);
111     }
112 
113     tb->addAction(koIcon("configure"), i18n("Script Manager"), this, SLOT(slotShowScriptManager()));
114 
115     /*
116     d->tb->addSeparator();
117     QLineEdit *filter = new QLineEdit(tb);
118     d->tb->addWidget(filter);
119     connect(filter, SIGNAL(textChanged(QString)), model, SLOT(setFilterRegExp(QString)));
120     */
121 
122     setWidget(widget);
123 
124     connect(m_view, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(slotDoubleClicked()));
125     connect(m_view, SIGNAL(enabledChanged(QString)), this, SLOT(slotEnabledChanged(QString)));
126 }
127 
slotShowScriptManager()128 void KoScriptingDocker::slotShowScriptManager()
129 {
130     KoScriptManagerDialog *dialog = new KoScriptManagerDialog();
131     dialog->exec();
132     dialog->delayedDestruct();
133 }
134 
slotEnabledChanged(const QString & actionname)135 void KoScriptingDocker::slotEnabledChanged(const QString &actionname)
136 {
137     if (m_actions.contains(actionname))
138         if (QAction *a = m_view->actionCollection()->action(actionname))
139             m_actions[actionname]->setEnabled(a->isEnabled());
140 }
141 
slotDoubleClicked()142 void KoScriptingDocker::slotDoubleClicked()
143 {
144     //debugKoKross<<"KoScriptingDocker::slotDoubleClicked()";
145     m_view->slotRun();
146 }
147 
148 /***********************************************************************
149  * KoScriptingDocker
150  */
151 
KoScriptingActionDocker(KoScriptingModule * module,Kross::Action * action,QWidget * parent)152 KoScriptingActionDocker::KoScriptingActionDocker(KoScriptingModule *module, Kross::Action *action, QWidget *parent)
153     : QDockWidget(action->text(), parent),
154     m_module(module),
155     m_action(action)
156 {
157     debugKoKross;
158     m_action->addObject(this, "KoDocker", Kross::ChildrenInterface::AutoConnectSignals);
159     //connect(this, SIGNAL(visibilityChanged(bool)), this, SLOT(slotVisibilityChanged(bool)));
160 }
161 
~KoScriptingActionDocker()162 KoScriptingActionDocker::~KoScriptingActionDocker()
163 {
164     debugKoKross;
165     m_action->finalize();
166 }
167 
slotVisibilityChanged(bool visible)168 void KoScriptingActionDocker::slotVisibilityChanged(bool visible)
169 {
170     debugKoKross<<"visible="<<visible;
171     if (visible) {
172         if (m_module && m_action->isFinalized()) {
173             //KoView *view = m_module->view();
174             //KoMainWindow *mainwindow = view ? view->mainWindow() : 0;
175             m_action->trigger();
176         }
177     } else {
178         //m_action->finalize();
179     }
180 }
181 
widget()182 QWidget *KoScriptingActionDocker::widget()
183 {
184     return QDockWidget::widget();
185 }
186 
setWidget(QWidget * widget)187 void KoScriptingActionDocker::setWidget(QWidget *widget)
188 {
189     QDockWidget::setWidget(widget);
190 }
191