Index: plasma/tools/plasmoidviewer/plasmoidpart.cpp =================================================================== --- plasma/tools/plasmoidviewer/plasmoidpart.cpp (revision 0) +++ plasma/tools/plasmoidviewer/plasmoidpart.cpp (revision 0) @@ -0,0 +1,155 @@ +/* + * Copyright 2007 Harri Porten + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class PlasmoidPartFactory : public KParts::Factory { + Q_OBJECT +public: + PlasmoidPartFactory (); + virtual KParts::Part *createPartObject(QWidget *wparent, + QObject *parent, + const char *className, + const QStringList &args); +}; + +class PlasmoidView : public KParts::ReadOnlyPart { + Q_OBJECT +public: + PlasmoidView(QWidget* wparent, QObject* parent, const QStringList& args); + +protected: + bool openFile(); + static QMap parseArguments(const QStringList &args); + void displayError(const QString &msg); + +private: + static QString appletName(const QString &classId); + + Plasma::Corona corona; +}; + +PlasmoidView::PlasmoidView(QWidget* wparent, QObject* parent, + const QStringList& args) + : KParts::ReadOnlyPart(parent) +{ + QGraphicsView* view = new QGraphicsView(&corona); + view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + view->setAlignment(Qt::AlignLeft | Qt::AlignTop); + view->setFrameStyle(0); + + Plasma::Containment *containment = corona.addContainment("null"); + + QMap m = parseArguments(args); + QString n = appletName(m["__KHTML__CLASSID"]); + int w = m.contains("WIDTH") ? m["WIDTH"].toInt() : -1; + int h = m.contains("HEIGHT") ? m["HEIGHT"].toInt() : -1; + Plasma::Applet *applet = containment->addApplet(n); + if (!applet) { + displayError(i18n("Failed to load applet '%1'").arg(n)); + } else { + kDebug() << "sizeHint" << applet->sizeHint(); + kDebug() << "contentSizeHint" << applet->contentSizeHint(); + const qreal borderWidth = (applet->sizeHint().width() - + applet->contentSizeHint().width()) / 2; + applet->setPos(borderWidth, borderWidth); + applet->setFlag(QGraphicsItem::ItemIsMovable, false); + view->setSceneRect(0, 0, + applet->sizeHint().width(), + applet->sizeHint().height()); + view->resize(view->sceneRect().size().toSize()); + } + + if (w > 0 && h > 0) { + view->resize(w, h); + } + + KParts::Part::setWidget(view); +} + +bool PlasmoidView::openFile() +{ + return true; +} + +QMap PlasmoidView::parseArguments(const QStringList &args) +{ + QMap m; + QStringList::const_iterator it, end = args.end(); + for (it = args.begin(); it != end; ++it) { + QString a = *it; + int equal = a.indexOf('='); + if (equal == 0) { + kWarning() << "Invalid argument" << a << "passed to PlasmoidView"; + } else if (equal > 0) { + QString v = a.mid(equal + 1); + int vl = v.length(); + if (vl >= 2 && v.at(0) == '"' && v.at(vl - 1) == '"') + v = v.mid(1, vl - 2); + m.insert(a.left(equal).toUpper(), v); + } else { + m.insert(a, ""); + } + } + return m; +} + +QString PlasmoidView::appletName(const QString &classId) +{ + if (classId.startsWith("plasmoid:")) + return classId.mid(9); + else + return QString(); +} + +void PlasmoidView::displayError(const QString &msg) +{ + QGraphicsTextItem *ti = corona.addText(msg); +} + +K_EXPORT_COMPONENT_FACTORY(plasmoidpart, PlasmoidPartFactory) + +PlasmoidPartFactory::PlasmoidPartFactory() +{ +} + +KParts::Part* PlasmoidPartFactory::createPartObject(QWidget* wparent, + QObject* parent, + const char* className, + const QStringList& args) +{ + return new PlasmoidView(wparent, parent, args); +} + +#include "plasmoidpart.moc" Index: plasma/tools/plasmoidviewer/plasmoidpart.desktop =================================================================== --- plasma/tools/plasmoidviewer/plasmoidpart.desktop (revision 0) +++ plasma/tools/plasmoidviewer/plasmoidpart.desktop (revision 0) @@ -0,0 +1,8 @@ +[Desktop Entry] +Encoding=UTF-8 +Name=Plasma Viewer +X-KDE-Library=plasmoidpart +MimeType=application/x-plasma; +ServiceTypes=KParts/ReadOnlyPart,Browser/View +Type=Service +InitialPreference=4 Index: plasma/tools/plasmoidviewer/CMakeLists.txt =================================================================== --- plasma/tools/plasmoidviewer/CMakeLists.txt (revision 732068) +++ plasma/tools/plasmoidviewer/CMakeLists.txt (working copy) @@ -9,3 +9,11 @@ install(TARGETS plasmoidviewer DESTINATION ${BIN_INSTALL_DIR}) install(FILES checker.png DESTINATION ${DATA_INSTALL_DIR}/plasmoidviewer) +########### next target ############### + +kde4_add_plugin(plasmoidpart plasmoidpart.cpp) + +target_link_libraries(plasmoidpart plasma ${KDE4_KPARTS_LIBS}) + +install(TARGETS plasmoidpart DESTINATION ${PLUGIN_INSTALL_DIR} ) +install(FILES plasmoidpart.desktop DESTINATION ${SERVICES_INSTALL_DIR})