• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • plasma
skappletscript.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Sebastian Sauer <mail@dipe.org>
3  *
4  * This file is part of SuperKaramba.
5  *
6  * SuperKaramba 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  * SuperKaramba 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 SuperKaramba; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "skappletscript.h"
22 #include "skappletadaptor.h"
23 
24 #include "karamba.h"
25 #include "karambamanager.h"
26 
27 #include <QTimer>
28 #include <QLabel>
29 #include <QComboBox>
30 #include <QGridLayout>
31 #include <QGraphicsScene>
32 #include <QGraphicsView>
33 #include <KToggleAction>
34 #include <KColorScheme>
35 #include <KGlobalSettings>
36 #include <KDebug>
37 
38 K_EXPORT_PLASMA_APPLETSCRIPTENGINE(superkaramba, SkAppletScript)
39 
40 class SkAppletScript::Private
41 {
42  public:
43  QString themeFile;
44  QPointer<Karamba> theme;
45  SkAppletAdaptor* appletadaptor;
46  QList<QAction*> actions;
47  QStringList errors;
48 
49  Private() : theme(0), appletadaptor(0) {}
50  ~Private() { delete theme; }
51 };
52 
53 SkAppletScript::SkAppletScript(QObject *parent, const QVariantList &args)
54  : Plasma::AppletScript(parent)
55  , d(new Private)
56 {
57  Q_UNUSED(args);
58 }
59 
60 SkAppletScript::~SkAppletScript()
61 {
62  kDebug();
63  delete d;
64 }
65 
66 bool SkAppletScript::init()
67 {
68  Q_ASSERT( applet() );
69  Q_ASSERT( package() );
70 
71  if( applet()->isContainment() ) { // Plasma::Containment
72  Plasma::Containment *c = dynamic_cast<Plasma::Containment *>(applet());
73 
74  //c->setContainmentType(Plasma::Containment::PanelContainment);
75  c->setZValue(150);
76  c->setFormFactor(Plasma::Horizontal);
77  c->setLocation(Plasma::TopEdge);
78  //c->setScreen(0);
79  //c->addApplet("clock");
80  //c->flushUpdatedConstraints();
81  //if (c->layout()) c->layout()->invalidate();
82  //c->updateConstraints(Plasma::AllConstraints);
83  //package()->metadata()->;
84  }
85  else { // Plasma::Applet
86  //applet()->setHasConfigurationInterface(false);//true);
87 //applet()->setDrawStandardBackground(false);
88  applet()->setAspectRatioMode(Plasma::IgnoreAspectRatio);
89  }
90 
91  QDir dir(package()->path());
92  QString name = dir.dirName();
93  if( name.toLower().startsWith("sk_") )
94  name = name.mid(3);
95  QFileInfo fi(dir, QString("%1.theme").arg(name));
96  if( ! fi.exists() ) {
97  QFileInfoList files = dir.entryInfoList(QStringList() << "*.skz" << "*.theme", QDir::Files);
98  if(files.count() < 1) {
99  kWarning() << "Failed to locate the themefile in path=" << package()->path();
100  return false;
101  }
102  fi = files[0];
103  }
104 
105  d->themeFile = fi.absoluteFilePath();
106  QTimer::singleShot(50, this, SLOT(loadKaramba()));
107  return true;
108 }
109 
110 void SkAppletScript::loadKaramba()
111 {
112  d->errors.clear();
113 
114  Q_ASSERT( applet() );
115  Q_ASSERT( applet()->scene() );
116  Q_ASSERT( applet()->scene()->views().count() > 0 );
117  QGraphicsScene *scene = applet()->scene();
118  QGraphicsView* view = scene->views()[0];
119 
120  connect(KarambaManager::self(), SIGNAL(karambaStarted(QGraphicsItemGroup*)), this, SLOT(karambaStarted(QGraphicsItemGroup*)));
121  connect(KarambaManager::self(), SIGNAL(karambaClosed(QGraphicsItemGroup*)), this, SLOT(karambaClosed(QGraphicsItemGroup*)));
122 
123  Q_ASSERT( ! d->theme );
124  d->theme = new Karamba(d->themeFile, view, -1, false, QPoint(), false, false);
125  d->theme->setParentItem(applet());
126  const QRectF geometry = applet()->geometry();
127  d->theme->moveToPos(QPoint(int(geometry.x()), int(geometry.y())));
128 
129  //view->viewport()->installEventFilter(this);
130 
131  if( applet()->isContainment() ) { // Plasma::Containment
132  Plasma::Containment *c = dynamic_cast<Plasma::Containment *>(applet());
133  Q_ASSERT(c);
134  d->appletadaptor = new SkContainmentAdaptor(d->theme, c);
135 
136  // While Plasma::Applet does provide such a functionality, Plasma::Containment
137  // does not. So, let's add it manualy...
138  //QAction* configure = new QAction(i18n("Panel Settings"), this);
139  //configure->setIcon(KIcon( QLatin1String( "configure" )));
140  //connect(configure, SIGNAL(triggered()), this, SLOT(showConfigurationInterface()));
141  //d->actions << configure;
142  }
143  else { // Plasma::Applet
144  d->appletadaptor = new SkAppletAdaptor(d->theme, applet());
145  }
146 
147  if( KToggleAction* lockedAction = d->theme->findChild<KToggleAction*>("lockedAction") ) {
148  // disable locked action since Plasma will handle it for us.
149  if( ! lockedAction->isChecked() )
150  lockedAction->setChecked(true);
151  lockedAction->setVisible(false);
152  }
153 
154  if( QAction* configAction = d->theme->findChild<QAction*>("configureTheme") ) {
155  d->actions << configAction;
156  }
157 
158  connect(d->theme, SIGNAL(positionChanged()), this, SLOT(positionChanged()));
159  connect(d->theme, SIGNAL(sizeChanged()), this, SLOT(sizeChanged()));
160  connect(d->theme, SIGNAL(error(QString)), this, SLOT(scriptError(QString)));
161 
162  // hack to prevent the applet's background from being drawn
163  //applet()->setOpacity(0.0);
164 
165  d->theme->startKaramba();
166 }
167 
168 void SkAppletScript::positionChanged()
169 {
170  QPointF p = applet()->pos();
171  //QRectF r = applet()->geometry();
172 
173 //QSizeF s = r.size();
174 //s = QSizeF(s.width()+p.x(),s.height()+p.y());
175 //applet()->setContentSize(s);
176 
177 
178  //FIXME WTF, sebsauer, 2008-03-07; somehow this doesn't seem to work correct if we are a panel :-(
179 
180  //Q_ASSERT( ! managingLayout() );
181 
182  //applet()->setPos(p);
183  //d->theme->getView()->move(p.x(),p.y());
184 
185 /*
186 QPointF p2;// = r.topLeft();
187 p = QPointF(p.x()+p2.x(),p.y()+p2.y());
188 
189  r.moveTo(p);
190  applet()->setGeometry(r);
191 */
192 
193 applet()->moveBy(p.x(),p.y());
194 //applet()->parentItem()->moveBy(p.x(),p.y());
195 
196 
197  //applet()->update(r);
198  //d->theme->getView()->update();
199  //applet()->updateConstraints();//Plasma::SizeConstraint
200 }
201 
202 void SkAppletScript::sizeChanged()
203 {
204  QRectF r = d->theme->boundingRect();
205  QSizeF s = r.size();
206  applet()->resize(s);
207  applet()->setMinimumSize(s);//hack else a Panel may lose its size :-/
208  //applet()->updateConstraints(Plasma::SizeConstraint);
209 }
210 
211 void SkAppletScript::scriptError(const QString& err)
212 {
213  d->errors << err;
214 }
215 
216 #if 0
217 QSizeF SkAppletScript::contentSizeHint() const
218 {
219  if( ! d->theme )
220  return Plasma::AppletScript::contentSizeHint();
221  QRectF r = d->theme->boundingRect();
222  QSizeF s = r.size();
223  return s;
224 }
225 #endif
226 
227 void SkAppletScript::paintInterface(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
228 {
229  if( d->errors.count() > 0 ) {
230  QColor fontcolor = KColorScheme(QPalette::Active, KColorScheme::View, Plasma::Theme::defaultTheme()->colorScheme()).foreground().color();
231  painter->setPen(QPen(fontcolor));
232 
233  painter->setRenderHint(QPainter::SmoothPixmapTransform);
234  painter->setRenderHint(QPainter::Antialiasing);
235 
236  const QString title = i18n("Failed to launch SuperKaramba Theme");
237 
238  QFont titlefont;
239  titlefont.setBold( true );
240  painter->setFont(titlefont);
241 
242  QRect titlerect = painter->boundingRect(contentsRect, 0, title);
243  painter->drawText(titlerect, title, QTextOption());
244 
245  QRect textrect = contentsRect;
246  textrect.setY( titlerect.y() + titlerect.height() + 4 );
247 
248  const QString text = d->errors.join( QLatin1String( "\n" ));
249 
250  QFont textfont;
251  textfont.setPointSize( KGlobalSettings::smallestReadableFont().pointSize() );
252  painter->setFont(textfont);
253 
254  QTextOption textopts;
255  textopts.setWrapMode(QTextOption::WrapAnywhere);
256  painter->drawText(textrect, text, textopts);
257  }
258  else {
259  //painter->save();
260  //painter->setCompositionMode(QPainter::CompositionMode_Source);
261  //painter->fillRect(contentsRect,Qt::transparent);
262  //painter->restore();
263 
264  if( d->appletadaptor ) {
265  d->appletadaptor->paintInterface(painter, option, contentsRect);
266  }
267  }
268 }
269 
270 QList<QAction*> SkAppletScript::contextualActions()
271 {
272  return d->actions;
273 }
274 
275 void SkAppletScript::constraintsEvent(Plasma::Constraints constraints)
276 {
277  if( constraints & Plasma::FormFactorConstraint ) {
278 #if 0
279  if( ! applet()->isContainment() ) {
280  applet()->setDrawStandardBackground(false);
281  }
282 #endif
283  }
284 
285  if( constraints & Plasma::SizeConstraint ) {
286  if( d->theme ) {
287  const QRectF r = d->theme->boundingRect();
288  const QSizeF s = applet()->geometry().size();
289  const qreal x = s.width() / r.width();
290  const qreal y = s.height() / r.height();
291 
292  QTransform t = d->theme->transform();
293  t.reset();
294  d->theme->setTransform(t.scale(x, y));
295  }
296  }
297 
298 #if 0
299  if( constraints & Plasma::ImmutableConstraint ) {
300  Q_ASSERT( applet() );
301  //applet()->setDrawStandardBackground();
302  //applet()->setHandlesChildEvents();
303  //if( d->theme ) d->theme->update();
304  //applet()->update();
305 
306  if( applet()->drawStandardBackground() && d->theme ) {
307  //QRectF geometry = applet()->geometry();
308  //geometry.setSize( );
309  applet()->resize( d->theme->boundingRect().size() );
310  }
311  }
312 #endif
313 
314 }
315 
316 void SkAppletScript::showConfigurationInterface()
317 {
318  //TODO
319 }
320 
321 void SkAppletScript::karambaStarted(QGraphicsItemGroup* group)
322 {
323  if( d->theme && d->theme == group ) {
324  kDebug()<<">>>>>>>>>>>> SkAppletScript::karambaStarted theme-name="<<d->theme->theme().name();
325 
326  if( applet()->isContainment() ) {
327  Plasma::Containment *c = dynamic_cast<Plasma::Containment *>(applet());
328  Q_ASSERT(c);
329  foreach(Plasma::Applet* a, c->applets()) {
330  a->raise();
331  }
332  }
333 
334  applet()->resize(d->theme->boundingRect().size());
335  applet()->updateConstraints(Plasma::SizeConstraint);
336  }
337 }
338 
339 void SkAppletScript::karambaClosed(QGraphicsItemGroup* group)
340 {
341  if( d->theme && d->theme == group ) {
342  kDebug()<<">>>>>>>>>>>> SkAppletScript::karambaClosed theme-name="<<d->theme->theme().name();
343  //d->themeFile = QString();
344  d->theme = 0;
345  Q_ASSERT( applet() );
346  Q_ASSERT( applet()->scene() );
347  //applet()->scene()->removeItem(applet());
348  applet()->destroy();
349  deleteLater();
350  }
351 }
352 
353 bool SkAppletScript::eventFilter(QObject* watched, QEvent* event)
354 {
355  switch( event->type() ) {
356  case QEvent::ContextMenu: {
357  kDebug() << "eventFilter type=ContextMenu watched=" << (watched ? QString("%1 [%2]").arg(watched->objectName()).arg(watched->metaObject()->className()) : "NULL") ;
358  //static_cast<QContextMenuEvent*>(event);
359  } break;
360  case QEvent::GraphicsSceneContextMenu:
361  kDebug() << "eventFilter type=GraphicsSceneContextMenu watched=" << (watched ? QString("%1 [%2]").arg(watched->objectName()).arg(watched->metaObject()->className()) : "NULL") ;
362  //return true;
363  break;
364  case QEvent::KeyPress:
365  kDebug() << "eventFilter type=KeyPress watched=" << (watched ? QString("%1 [%2]").arg(watched->objectName()).arg(watched->metaObject()->className()) : "NULL") ;
366  //QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
367  //kDebug() << "Ate key press" << keyEvent->key();
368  break;
369  default:
370  break;
371  }
372  return Plasma::AppletScript::eventFilter(watched, event);
373 }
374 
375 #include "skappletscript.moc"
skappletscript.h
QObject
SkAppletScript::paintInterface
virtual void paintInterface(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
Called when the applet should be painted.
Definition: skappletscript.cpp:227
SkAppletScript::contextualActions
virtual QList< QAction * > contextualActions()
Returns a list of context-related QAction instances.
Definition: skappletscript.cpp:270
SkContainmentAdaptor
The SkContainmentAdaptor class extends the SkAppletAdaptor class to implements an adaptor for Plasma:...
Definition: skappletadaptor.h:258
Karamba
Definition: karamba.h:52
skappletadaptor.h
SkAppletScript::~SkAppletScript
virtual ~SkAppletScript()
Destructor.
Definition: skappletscript.cpp:60
SkAppletScript::showConfigurationInterface
virtual void showConfigurationInterface()
Shows the configuration dialog for general SkAppletScript settings.
Definition: skappletscript.cpp:316
karambamanager.h
KarambaManager::self
static KarambaManager * self()
Definition: karambamanager.cpp:47
SkAppletScript
The SkAppletScript class implements a Plasma::AppletScript to implement a Plasma::ScriptEngine for Su...
Definition: skappletscript.h:35
SkAppletScript::SkAppletScript
SkAppletScript(QObject *parent, const QVariantList &args)
Constructor.
Definition: skappletscript.cpp:53
QGraphicsItemGroup
QGraphicsView
SkAppletScript::constraintsEvent
virtual void constraintsEvent(Plasma::Constraints constraints)
Called when any of the geometry constraints have been updated.
Definition: skappletscript.cpp:275
SkAppletScript::init
virtual bool init()
Called when it is safe to initialize the internal state.
Definition: skappletscript.cpp:66
SkAppletAdaptor
The SkAppletAdaptor class implements an adaptor for Plasma::Applet objects.
Definition: skappletadaptor.h:130
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal