add more spacing
[personal-kdebase.git] / workspace / plasma / scriptengines / qedjescript / qedje_applet.cpp
blobe34ede159066f3d17325c012fe5f5dbf2e5d1d1a
1 /***************************************************************************
2 * Copyright (C) 2008 by Artur Duque de Souza <morpheuz@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
20 #include <qedje_applet.h>
22 #include <QtGui/QGraphicsProxyWidget>
24 #include <Plasma/Package>
25 #include <Plasma/Applet>
27 using namespace Plasma;
29 K_EXPORT_PLASMA_APPLETSCRIPTENGINE(qedjescripts, QEdjeAppletScript)
31 QEdjeAppletScript::QEdjeAppletScript(QObject *parent, const QVariantList &args)
32 : Plasma::AppletScript(parent), dialog(0), config_widget(0),
33 m_edje_file(""), m_edje_group(""), currentIndex(0)
35 Q_UNUSED(args);
38 QEdjeAppletScript::~QEdjeAppletScript()
40 // the proxy gets the ownership of the applet
41 // so we need to free the applet in order to avoid segfaults later
42 proxy->setWidget(0);
44 // deleting setting's dialog stuff
45 if (dialog) {
46 delete previewWorld;
47 delete previewCanvas;
48 delete dialog;
51 delete world;
52 delete canvas;
55 void QEdjeAppletScript::resizeAll(QSize size)
57 // minimum required size
58 if (size == QSize(0, 0))
59 size = QSize(100, 100);
61 // resize the applet and qzion's canvas
62 QSizeF new_size = applet()->size() - applet()->contentsRect().size() + size;
63 applet()->resize(new_size.toSize());
64 canvas->resize(size);
67 void QEdjeAppletScript::setup_canvas()
69 // we need a proxy so we can put qzion inside the applet
70 proxy = new QGraphicsProxyWidget(applet());
71 canvas = new QZionCanvas();
73 // create the canvasd (qzion) needed by qedje
74 proxy->setWidget(canvas->widget());
75 canvas->show();
77 // minimum size
78 canvas->resize(100, 100);
81 bool QEdjeAppletScript::init()
83 setup_canvas();
85 // set plasma options
86 applet()->setBackgroundHints(Applet::TranslucentBackground);
87 setHasConfigurationInterface(true);
89 // get config info
90 KConfigGroup cg = applet()->config();
91 m_edje_group = cg.readEntry("EdjeGroup", "");
93 // setup edje file
94 m_edje_file = package()->filePath("edje_file");
96 // check groups
97 m_groups_list = groupNamesFromFile(m_edje_file);
99 if (m_groups_list.count() <= 0)
100 return false;
102 if (m_edje_group.isEmpty()) {
103 m_edje_group = m_groups_list.first();
104 currentIndex = 0;
105 } else
106 currentIndex = m_groups_list.indexOf(m_edje_group);
108 // create qedje object
109 world = new QEdje(canvas, m_edje_file, m_edje_group);
111 // show qedje object and resize applet and plasmoid based on
112 // the object's min size
113 world->show();
114 resizeAll(world->propMin());
115 return true;
118 void QEdjeAppletScript::showConfigurationInterface()
120 if (!dialog) {
121 dialog = new KDialog();
122 config_widget = new QWidget(dialog);
123 previewCanvas = new QZionCanvas(config_widget);
125 dialog->setCaption(i18n("QEdje Applet Config"));
126 dialog->setButtons(KDialog::Ok | KDialog::Cancel);
128 ui.setupUi(config_widget);
129 ui.edje_groups->addItems(m_groups_list);
130 ui.edje_groups->setCurrentIndex(ui.edje_groups->findText(m_edje_group));
131 previewCanvas->widget()->setGeometry(ui.preview->frameGeometry());
133 // connect the signals
134 connect(dialog, SIGNAL(okClicked()), this, SLOT(configChanged()));
135 connect(ui.edje_groups, SIGNAL(activated(int)), this, SLOT(groupSelected(int)));
137 // show the config dialog
138 dialog->setMainWidget(config_widget);
140 previewWorld = new QEdje(previewCanvas, m_edje_file, m_edje_group);
143 dialog->show();
144 previewWorld->show();
147 void QEdjeAppletScript::groupSelected(int index)
149 if (index == currentIndex)
150 return;
152 m_edje_group = m_groups_list[index];
153 currentIndex = index;
155 previewWorld->hide();
156 delete previewWorld;
158 previewWorld = new QEdje(previewCanvas, m_edje_file, m_edje_group);
159 previewWorld->show();
162 void QEdjeAppletScript::configChanged()
164 KConfigGroup cg = applet()->config();
165 cg.writeEntry("EdjeGroup", m_edje_group);
167 world->hide();
168 delete world;
170 world = new QEdje(canvas, m_edje_file, m_edje_group);
171 world->show();
172 resizeAll(world->propMin());
175 void QEdjeAppletScript::paintInterface(QPainter *,
176 const QStyleOptionGraphicsItem *,
177 const QRect &contentsRect)
179 // make sure qzion have the correct geometry
180 canvas->widget()->setGeometry(contentsRect);
183 #include "qedje_applet.moc"