• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

plasmawebapplet.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2007 Zack Rusin <zack@kde.org>
00003 Copyright (c) 2008 Petri Damstén <damu@iki.fi>
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "plasmawebapplet.h"
00025 
00026 #include <QWebPage>
00027 #include <QWebFrame>
00028 
00029 #include <KColorScheme>
00030 
00031 #include <Plasma/WebView>
00032 #include <Plasma/Applet>
00033 #include <Plasma/Theme>
00034 
00035 #define JS_CONSTANTS_CONSTRAINT \
00036 "var NoConstraint = %1;\n"\
00037 "var FormFactorConstraint = %2;\n"\
00038 "var LocationConstraint = %3;\n"\
00039 "var ScreenConstraint = %4;\n"\
00040 "var SizeConstraint = %5;\n"\
00041 "var ImmutableConstraint = %6;\n"\
00042 "var StartupCompletedConstraint = %7;\n"\
00043 "var ContextConstraint = %8;\n"\
00044 "var AllConstraints = %9;\n"
00045 
00046 #define JS_CONSTANTS_BACKGROUND \
00047 "var NoBackground = %1;\n"\
00048 "var StandardBackground = %2;\n"\
00049 "var TranslucentBackground = %3;\n"\
00050 "var DefaultBackground = %5;\n"
00051 
00052 #define JS_CONSTANTS_SCROLLBAR \
00053 "var QtHorizontal = %1;\n"\
00054 "var QtVertical = %2;\n"\
00055 "var ScrollBarAsNeeded = %3;\n"\
00056 "var ScrollBarAlwaysOff = %4;\n"\
00057 "var ScrollBarAlwaysOn = %5;\n"\
00058 
00059 #define JS_CONSTANTS_ASPECTRATIO \
00060 "var InvalidAspectRatioMode = %1;\n"\
00061 "var IgnoreAspectRatio = %2;\n"\
00062 "var KeepAspectRatio = %3;\n"\
00063 "var Square = %4;\n"\
00064 "var ConstrainedSquare = %5;\n"\
00065 "var FixedSize = %6;\n"
00066 
00067 #define JS_CONSTANTS_FORMFACTOR \
00068 "var Planar = %1;\n"\
00069 "var MediaCenter = %2;\n"\
00070 "var Horizontal = %3;\n"\
00071 "var Vertical = %4;\n"\
00072 
00073 #define JS_CONSTANTS_LOCATION \
00074 "var Floating = %1;\n"\
00075 "var Desktop = %2;\n"\
00076 "var FullScreen = %3;\n"\
00077 "var TopEdge = %4;\n"\
00078 "var BottomEdge = %5;\n"\
00079 "var LeftEdge = %6;\n"\
00080 "var RightEdge = %7;\n"
00081 
00082 #define JS_CONSTANTS_OTHER \
00083 "var size_width = 0;\n"\
00084 "var size_height = 1;\n"\
00085 "var point_x = 0;\n"\
00086 "var point_y = 1;\n"\
00087 "var rect_x = 0;\n"\
00088 "var rect_y = 1;\n"\
00089 "var rect_width = 2;\n"\
00090 "var rect_height = 3;\n"\
00091 "var margin_left = 0;\n"\
00092 "var margin_top = 1;\n"\
00093 "var margin_right = 2;\n"\
00094 "var margin_bottom = 3;\n"\
00095 
00096 #define CSS "body { font-family: %3; font-size: %4pt; color:%1; background-color:%2 }\n"
00097 
00098 QString PlasmaWebApplet::m_jsConstants;
00099 
00100 PlasmaWebApplet::PlasmaWebApplet(QObject *parent, const QVariantList &args)
00101 : WebApplet(parent, args)
00102 {
00103     if (m_jsConstants.isEmpty()) {
00104         m_jsConstants = JS_CONSTANTS_OTHER;
00105         m_jsConstants += QString(JS_CONSTANTS_CONSTRAINT)
00106                 .arg(Plasma::NoConstraint)
00107                 .arg(Plasma::FormFactorConstraint)
00108                 .arg(Plasma::LocationConstraint)
00109                 .arg(Plasma::ScreenConstraint)
00110                 .arg(Plasma::SizeConstraint)
00111                 .arg(Plasma::ImmutableConstraint)
00112                 .arg(Plasma::StartupCompletedConstraint)
00113                 .arg(Plasma::ContextConstraint)
00114                 .arg(Plasma::AllConstraints);
00115         m_jsConstants += QString(JS_CONSTANTS_BACKGROUND)
00116                 .arg(Plasma::Applet::NoBackground)
00117                 .arg(Plasma::Applet::StandardBackground)
00118                 .arg(Plasma::Applet::TranslucentBackground)
00119                 .arg(Plasma::Applet::DefaultBackground);
00120         m_jsConstants += QString(JS_CONSTANTS_SCROLLBAR)
00121                 .arg(Qt::Horizontal)
00122                 .arg(Qt::Vertical)
00123                 .arg(Qt::ScrollBarAsNeeded)
00124                 .arg(Qt::ScrollBarAlwaysOff)
00125                 .arg(Qt::ScrollBarAlwaysOn);
00126         m_jsConstants += QString(JS_CONSTANTS_ASPECTRATIO)
00127                 .arg(Plasma::InvalidAspectRatioMode)
00128                 .arg(Plasma::IgnoreAspectRatio)
00129                 .arg(Plasma::KeepAspectRatio)
00130                 .arg(Plasma::Square)
00131                 .arg(Plasma::ConstrainedSquare)
00132                 .arg(Plasma::FixedSize);
00133         m_jsConstants += QString(JS_CONSTANTS_FORMFACTOR)
00134                 .arg(Plasma::Planar)
00135                 .arg(Plasma::MediaCenter)
00136                 .arg(Plasma::Horizontal)
00137                 .arg(Plasma::Vertical);
00138         m_jsConstants += QString(JS_CONSTANTS_LOCATION)
00139                 .arg(Plasma::Floating)
00140                 .arg(Plasma::Desktop)
00141                 .arg(Plasma::FullScreen)
00142                 .arg(Plasma::TopEdge)
00143                 .arg(Plasma::BottomEdge)
00144                 .arg(Plasma::LeftEdge)
00145                 .arg(Plasma::RightEdge);
00146     }
00147 }
00148 
00149 PlasmaWebApplet::~PlasmaWebApplet()
00150 {
00151 }
00152 
00153 bool PlasmaWebApplet::init()
00154 {
00155     m_useDefaultSize = (applet()->contentsRect().size() == QSizeF(0, 0));
00156     m_config.setConfig(applet()->config());
00157     m_globalConfig.setConfig(applet()->globalConfig());
00158     if (WebApplet::init()) {
00159         connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
00160                 this, SLOT(themeChanged()));
00161         makeStylesheet();
00162         return true;
00163     }
00164     return false;
00165 }
00166 
00167 void PlasmaWebApplet::makeStylesheet()
00168 {
00169     if (m_temp.open()) {
00170         KColorScheme plasmaColorTheme = KColorScheme(QPalette::Active, KColorScheme::View,
00171                 Plasma::Theme::defaultTheme()->colorScheme());
00172         QColor textColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
00173         QColor backgroundColor =
00174                 Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor);
00175         QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
00176 
00177         QString css = QString(CSS).arg(textColor.name())
00178                                   .arg(backgroundColor.name())
00179                                   .arg(font.family())
00180                                   .arg(font.pointSize());
00181         m_temp.write(css.toUtf8());
00182         page()->page()->settings()->setUserStyleSheetUrl(QUrl(m_temp.fileName()));
00183         m_temp.close();
00184     }
00185 }
00186 
00187 void PlasmaWebApplet::themeChanged()
00188 {
00189     makeStylesheet();
00190     callJsFunction("themeChanged");
00191 }
00192 
00193 void PlasmaWebApplet::loadFinished(bool success)
00194 {
00195     WebApplet::loadFinished(success);
00196     if (success) {
00197         page()->mainFrame()->evaluateJavaScript(m_jsConstants);
00198         callJsFunction("init");
00199     }
00200 }
00201 
00202 void PlasmaWebApplet::constraintsEvent(Plasma::Constraints constraints)
00203 {
00204     if (page() && constraints & Plasma::SizeConstraint) {
00205         qreal left;
00206         qreal top;
00207         qreal right;
00208         qreal bottom;
00209         applet()->getContentsMargins(&left, &top, &right, &bottom);
00210         page()->setPos(QPointF(left, top));
00211         page()->resize(WebApplet::size() - QSizeF(left + right, top + bottom));
00212         //kDebug() << WebApplet::size() << left << right << top << bottom << page()->size();
00213     }
00214     callJsFunction("constraintsEvent", QVariantList() << (int)constraints);
00215 }
00216 
00217 QVariant PlasmaWebApplet::arg(int index) const
00218 {
00219     return m_args[index];
00220 }
00221 
00222 QObject* PlasmaWebApplet::objArg(int index) const
00223 {
00224     return m_args[index].value<QObject*>();
00225 }
00226 
00227 QString PlasmaWebApplet::name() const
00228 {
00229     return applet()->name();
00230 }
00231 
00232 uint PlasmaWebApplet::id() const
00233 {
00234     return applet()->id();
00235 }
00236 
00237 int PlasmaWebApplet::formFactor() const
00238 {
00239     return (int)applet()->formFactor();
00240 }
00241 
00242 int PlasmaWebApplet::location() const
00243 {
00244     return (int)applet()->location();
00245 }
00246 
00247 QString PlasmaWebApplet::pluginName() const
00248 {
00249     return applet()->pluginName();
00250 }
00251 
00252 QString PlasmaWebApplet::icon() const
00253 {
00254     return applet()->icon();
00255 }
00256 
00257 QString PlasmaWebApplet::category() const
00258 {
00259     return applet()->category();
00260 }
00261 
00262 bool PlasmaWebApplet::shouldConserveResources() const
00263 {
00264     return applet()->shouldConserveResources();
00265 }
00266 
00267 QObject* PlasmaWebApplet::dataEngine(const QString& name)
00268 {
00269     QString id = QString("%1").arg(applet()->id());
00270     Plasma::DataEngine *de = applet()->dataEngine(name);
00271     DataEngineWrapper *wrapper = de->findChild<DataEngineWrapper*>(id);
00272     if (!wrapper) {
00273         wrapper = new DataEngineWrapper(de, this);
00274         wrapper->setObjectName(id);
00275     }
00276     return wrapper;
00277 }
00278 
00279 QObject* PlasmaWebApplet::config()
00280 {
00281     return &m_config;
00282 }
00283 
00284 QObject* PlasmaWebApplet::globalConfig()
00285 {
00286     return &m_globalConfig;
00287 }
00288 
00289 void PlasmaWebApplet::resize(qreal w, qreal h)
00290 {
00291     applet()->resize(w, h);
00292 }
00293 
00294 void PlasmaWebApplet::setBackgroundHints(int hints)
00295 {
00296     applet()->setBackgroundHints((Plasma::Applet::BackgroundHints)hints);
00297 }
00298 
00299 void PlasmaWebApplet::setScrollBarPolicy(int orientation, int policy)
00300 {
00301     page()->mainFrame()->setScrollBarPolicy((Qt::Orientation)orientation,
00302                                             (Qt::ScrollBarPolicy)policy);
00303 }
00304 
00305 void PlasmaWebApplet::setAspectRatioMode(int mode)
00306 {
00307     applet()->setAspectRatioMode((Plasma::AspectRatioMode)mode);
00308 }
00309 
00310 QVariant PlasmaWebApplet::callJsFunction(const QString& func, const QVariantList& args)
00311 {
00312     if (loaded()) {
00313         m_args = args;
00314         QString cmd = func + '(';
00315         for(int i = 0; i < args.count(); ++i) {
00316             if (i > 0) {
00317                 cmd += ',';
00318             }
00319             if (args[i].canConvert<QObject*>()) {
00320                 cmd += QString("window.applet.objArg(%1)").arg(i);
00321             } else {
00322                 cmd += QString("window.applet.arg(%1)").arg(i);
00323             }
00324         }
00325         cmd += ')';
00326         //kDebug() << cmd;
00327         return page()->mainFrame()->evaluateJavaScript(cmd);
00328     }
00329     return QVariant();
00330 }
00331 
00332 void PlasmaWebApplet::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
00333 {
00334     m_dataEngineData.setData(data);
00335     callJsFunction("dataUpdated",
00336                    QVariantList() << source << QVariant::fromValue((QObject*)&m_dataEngineData));
00337 }
00338 
00339 void PlasmaWebApplet::configChanged()
00340 {
00341     callJsFunction("configChanged");
00342 }
00343 
00344 void PlasmaWebApplet::initJsObjects()
00345 {
00346     QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
00347     Q_ASSERT(frame);
00348     frame->addToJavaScriptWindowObject(QLatin1String("applet"), this);
00349     frame->addToJavaScriptWindowObject(QLatin1String("plasma"), new PlasmaJs(this));
00350 }
00351 
00352 void PlasmaWebApplet::setDefaultSize(qreal w, qreal h)
00353 {
00354     if (m_useDefaultSize) {
00355         applet()->resize(w, h);
00356     }
00357 }
00358 
00359 QVariantList PlasmaWebApplet::geometry()
00360 {
00361     return QVariantList() << applet()->geometry().left() << applet()->geometry().top()
00362                           << applet()->geometry().width() << applet()->geometry().height();
00363 }
00364 
00365 QVariantList PlasmaWebApplet::screenRect()
00366 {
00367     return QVariantList() << applet()->screenRect().left() << applet()->screenRect().top()
00368                           << applet()->screenRect().width() << applet()->screenRect().height();
00369 }
00370 
00371 int PlasmaWebApplet::backgroundHints()
00372 {
00373     return applet()->backgroundHints();
00374 }
00375 
00376 int PlasmaWebApplet::aspectRatioMode()
00377 {
00378     return applet()->aspectRatioMode();
00379 }
00380 
00381 void PlasmaWebApplet::setConfigurationRequired(bool needsConfiguring)
00382 {
00383     // configurationRequired is protected
00384     applet()->setProperty("configurationRequired", needsConfiguring);
00385 }
00386 
00387 void PlasmaWebApplet::setMaximumSize(qreal w, qreal h)
00388 {
00389     applet()->setMaximumSize(w, h);
00390 }
00391 
00392 void PlasmaWebApplet::setMinimumSize(qreal w, qreal h)
00393 {
00394     applet()->setMinimumSize(w, h);
00395 }
00396 
00397 void PlasmaWebApplet::setPreferredSize(qreal w, qreal h)
00398 {
00399     applet()->setPreferredSize(w, h);
00400 }
00401 
00402 QVariantList PlasmaWebApplet::maximumSize()
00403 {
00404     return QVariantList() << applet()->maximumSize().width() << applet()->maximumSize().height();
00405 }
00406 
00407 QVariantList PlasmaWebApplet::minimumSize()
00408 {
00409     return QVariantList() << applet()->minimumSize().width() << applet()->minimumSize().height();
00410 }
00411 
00412 QVariantList PlasmaWebApplet::preferredSize()
00413 {
00414     return QVariantList() << applet()->preferredSize().width()
00415                           << applet()->preferredSize().height();
00416 }
00417 
00418 QVariantList PlasmaWebApplet::getContentsMargins()
00419 {
00420     qreal left;
00421     qreal top;
00422     qreal right;
00423     qreal bottom;
00424     applet()->getContentsMargins(&left, &top, &right, &bottom);
00425     return QVariantList() << left << top << right << bottom;
00426 }
00427 
00428 void PlasmaWebApplet::setGeometry(qreal x, qreal y, qreal w, qreal h)
00429 {
00430     applet()->setGeometry(x, y, w, h);
00431 }
00432 
00433 void PlasmaWebApplet::setPos(qreal x, qreal y)
00434 {
00435     applet()->setPos(x, y);
00436 }
00437 
00438 QVariantList PlasmaWebApplet::pos()
00439 {
00440     return QVariantList() << applet()->pos().x() << applet()->pos().y();
00441 }
00442 
00443 QVariantList PlasmaWebApplet::size()
00444 {
00445     return QVariantList() << applet()->size().width() << applet()->size().height();
00446 }
00447 
00448 #include "plasmawebapplet.moc"

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal