Plasma
containment.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "containment.h"
00021
00022 #include <QAction>
00023
00024 #include <Plasma/Corona>
00025 #include <Plasma/Containment>
00026
00027 #include "panelview.h"
00028 #include "plasmaapp.h"
00029 #include "scriptengine.h"
00030 #include "widget.h"
00031
00032 Containment::Containment(Plasma::Containment *containment, QObject *parent)
00033 : QObject(parent),
00034 m_containment(containment),
00035 m_isPanel(containment && ScriptEngine::isPanel(containment))
00036 {
00037 }
00038
00039 Containment::~Containment()
00040 {
00041 }
00042
00043 QString Containment::location() const
00044 {
00045 if (!m_containment) {
00046 return "floating";
00047 }
00048
00049 switch (m_containment.data()->location()) {
00050 case Plasma::Floating:
00051 return "floating";
00052 break;
00053 case Plasma::Desktop:
00054 return "desktop";
00055 break;
00056 case Plasma::FullScreen:
00057 return "fullscreen";
00058 break;
00059 case Plasma::TopEdge:
00060 return "top";
00061 break;
00062 case Plasma::BottomEdge:
00063 return "bottom";
00064 break;
00065 case Plasma::LeftEdge:
00066 return "left";
00067 break;
00068 case Plasma::RightEdge:
00069 return "right";
00070 break;
00071 }
00072
00073 return "floating";
00074 }
00075
00076 void Containment::setLocation(const QString &locationString)
00077 {
00078 if (!m_containment) {
00079 return;
00080 }
00081
00082 const QString lower = locationString.toLower();
00083 Plasma::Location loc = Plasma::Floating;
00084 if (lower == "desktop") {
00085 loc = Plasma::Desktop;
00086 } else if (lower == "fullscreen") {
00087 loc = Plasma::FullScreen;
00088 } else if (lower == "top") {
00089 loc = Plasma::TopEdge;
00090 } else if (lower == "bottom") {
00091 loc = Plasma::BottomEdge;
00092 } else if (lower == "left") {
00093 loc = Plasma::LeftEdge;
00094 } else if (lower == "right") {
00095 loc = Plasma::RightEdge;
00096 }
00097
00098 m_containment.data()->setLocation(loc);
00099 }
00100
00101 int Containment::screen() const
00102 {
00103 if (!m_containment) {
00104 return -1;
00105 }
00106
00107 return m_containment.data()->screen();
00108 }
00109
00110 void Containment::setScreen(int screen)
00111 {
00112 if (m_containment) {
00113 m_containment.data()->setScreen(screen);
00114 }
00115 }
00116
00117 int Containment::desktop() const
00118 {
00119 if (!m_containment) {
00120 return -1;
00121 }
00122
00123 return m_containment.data()->desktop();
00124 }
00125
00126 void Containment::setDesktop(int desktop)
00127 {
00128 if (m_containment) {
00129 m_containment.data()->setScreen(m_containment.data()->screen(), desktop);
00130 }
00131 }
00132
00133 QString Containment::formFactor() const
00134 {
00135 if (!m_containment) {
00136 return "Planar";
00137 }
00138
00139 switch (m_containment.data()->formFactor()) {
00140 case Plasma::Planar:
00141 return "planar";
00142 break;
00143 case Plasma::MediaCenter:
00144 return "mediacenter";
00145 break;
00146 case Plasma::Horizontal:
00147 return "horizontal";
00148 break;
00149 case Plasma::Vertical:
00150 return "vertical";
00151 break;
00152 }
00153
00154 return "Planar";
00155 }
00156
00157 QList<int> Containment::widgetIds() const
00158 {
00159
00160
00161 QList<int> w;
00162
00163 if (m_containment) {
00164 foreach (const Plasma::Applet *applet, m_containment.data()->applets()) {
00165 w.append(applet->id());
00166 }
00167 }
00168
00169 return w;
00170 }
00171
00172 QScriptValue Containment::widgetById(QScriptContext *context, QScriptEngine *engine)
00173 {
00174 if (context->argumentCount() == 0) {
00175 return context->throwError(i18n("widgetById requires an id"));
00176 }
00177
00178 const uint id = context->argument(0).toInt32();
00179 Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
00180
00181 if (!c) {
00182 return engine->undefinedValue();
00183 }
00184
00185 if (c->m_containment) {
00186 foreach (Plasma::Applet *w, c->m_containment.data()->applets()) {
00187 if (w->id() == id) {
00188 return ScriptEngine::wrap(w, engine);
00189 }
00190 }
00191 }
00192
00193 return engine->undefinedValue();
00194 }
00195
00196 QScriptValue Containment::addWidget(QScriptContext *context, QScriptEngine *engine)
00197 {
00198 if (context->argumentCount() == 0) {
00199 return context->throwError(i18n("widgetById requires a name of a widget or a widget object"));
00200 }
00201
00202 Containment *c = qobject_cast<Containment*>(context->thisObject().toQObject());
00203
00204 if (!c || !c->m_containment) {
00205 return engine->undefinedValue();
00206 }
00207
00208 QScriptValue v = context->argument(0);
00209 Plasma::Applet *applet = 0;
00210 if (v.isString()) {
00211 applet = c->m_containment.data()->addApplet(v.toString());
00212 if (applet) {
00213 return ScriptEngine::wrap(applet, engine);
00214 }
00215 } else if (Widget *widget = qobject_cast<Widget*>(v.toQObject())) {
00216 applet = widget->applet();
00217 c->m_containment.data()->addApplet(applet);
00218 return v;
00219 }
00220
00221 return engine->undefinedValue();
00222 }
00223
00224 uint Containment::id() const
00225 {
00226 if (!m_containment) {
00227 return 0;
00228 }
00229
00230 return m_containment.data()->id();
00231 }
00232
00233 QString Containment::name() const
00234 {
00235 if (!m_containment) {
00236 return QString();
00237 }
00238
00239 return m_containment.data()->activity();
00240 }
00241
00242 void Containment::setName(const QString &name)
00243 {
00244 if (m_containment) {
00245 m_containment.data()->setActivity(name);
00246 }
00247 }
00248
00249 QString Containment::type() const
00250 {
00251 if (!m_containment) {
00252 return QString();
00253 }
00254
00255 return m_containment.data()->pluginName();
00256 }
00257
00258 void Containment::remove()
00259 {
00260 m_isPanel = false;
00261
00262 if (m_containment) {
00263 m_containment.data()->destroy(false);
00264 }
00265 }
00266
00267 void Containment::showConfigurationInterface()
00268 {
00269 if (m_containment) {
00270 QAction *configAction = m_containment.data()->action("configure");
00271 if (configAction && configAction->isEnabled()) {
00272 configAction->trigger();
00273 }
00274 }
00275 }
00276
00277 PanelView *Containment::panel() const
00278 {
00279 if (!m_isPanel || !m_containment) {
00280 return 0;
00281 }
00282
00283 foreach (PanelView *v, PlasmaApp::self()->panelViews()) {
00284 if (v->containment() == m_containment.data()) {
00285 return v;
00286 }
00287 }
00288
00289 return 0;
00290 }
00291
00292 QString Containment::alignment() const
00293 {
00294 PanelView *v = panel();
00295 if (!v) {
00296 return "left";
00297 }
00298
00299 switch (v->alignment()) {
00300 case Qt::AlignRight:
00301 return "right";
00302 break;
00303 case Qt::AlignCenter:
00304 return "center";
00305 break;
00306 default:
00307 return "left";
00308 break;
00309 }
00310
00311 return "left";
00312 }
00313
00314 void Containment::setAlignment(const QString &alignment)
00315 {
00316 PanelView *v = panel();
00317 if (v) {
00318 bool success = false;
00319
00320 if (alignment.compare("left", Qt::CaseInsensitive) == 0) {
00321 if (v->alignment() != Qt::AlignLeft) {
00322 success = true;
00323 v->setAlignment(Qt::AlignLeft);
00324 }
00325 } else if (alignment.compare("right", Qt::CaseInsensitive) == 0) {
00326 if (v->alignment() != Qt::AlignRight) {
00327 success = true;
00328 v->setAlignment(Qt::AlignRight);
00329 }
00330 } else if (alignment.compare("center", Qt::CaseInsensitive) == 0) {
00331 if (v->alignment() != Qt::AlignCenter) {
00332 success = true;
00333 v->setAlignment(Qt::AlignCenter);
00334 }
00335 }
00336
00337 if (success) {
00338 v->setOffset(0);
00339 }
00340 }
00341 }
00342
00343 int Containment::offset() const
00344 {
00345 PanelView *v = panel();
00346 if (v) {
00347 return v->offset();
00348 }
00349
00350 return 0;
00351 }
00352
00353 void Containment::setOffset(int pixels)
00354 {
00355 if (pixels < 0 || !m_containment) {
00356 return;
00357 }
00358
00359 PanelView *v = panel();
00360 if (v) {
00361 Plasma::Containment *containment = m_containment.data();
00362 QRectF screen = containment->corona()->screenGeometry(v->screen());
00363 QSizeF size = containment->size();
00364
00365 if (containment->formFactor() == Plasma::Vertical) {
00366 if (pixels > screen.height()) {
00367 return;
00368 }
00369
00370 if (size.height() + pixels > screen.height()) {
00371 containment->resize(size.width(), screen.height() - pixels);
00372 }
00373 } else if (pixels > screen.width()) {
00374 return;
00375 } else if (size.width() + pixels > screen.width()) {
00376 size.setWidth(screen.width() - pixels);
00377 containment->resize(size);
00378 containment->setMinimumSize(size);
00379 containment->setMaximumSize(size);
00380 }
00381
00382 v->setOffset(pixels);
00383 }
00384 }
00385
00386 int Containment::length() const
00387 {
00388 if (!m_containment) {
00389 return 0;
00390 }
00391
00392 Plasma::Containment *containment = m_containment.data();
00393 if (containment->formFactor() == Plasma::Vertical) {
00394 return containment->size().height();
00395 } else {
00396 return containment->size().width();
00397 }
00398 }
00399
00400 void Containment::setLength(int pixels)
00401 {
00402 if (pixels < 0 || !m_containment) {
00403 return;
00404 }
00405
00406 PanelView *v = panel();
00407 if (v) {
00408 Plasma::Containment *containment = m_containment.data();
00409 QRectF screen = containment->corona()->screenGeometry(v->screen());
00410 QSizeF s = containment->size();
00411 if (containment->formFactor() == Plasma::Vertical) {
00412 if (pixels > screen.height() - v->offset()) {
00413 return;
00414 }
00415
00416 s.setHeight(pixels);
00417 } else if (pixels > screen.width() - v->offset()) {
00418 return;
00419 } else {
00420 s.setWidth(pixels);
00421 }
00422
00423 containment->resize(s);
00424 containment->setMinimumSize(s);
00425 containment->setMaximumSize(s);
00426 }
00427 }
00428
00429 int Containment::height() const
00430 {
00431 if (!m_isPanel || !m_containment) {
00432 return 0;
00433 }
00434
00435 Plasma::Containment *containment = m_containment.data();
00436 return containment->formFactor() == Plasma::Vertical ? containment->size().width() :
00437 containment->size().height();
00438 }
00439
00440 void Containment::setHeight(int height)
00441 {
00442 if (height < 16 || !m_containment) {
00443 return;
00444 }
00445
00446 PanelView *v = panel();
00447 if (v) {
00448 Plasma::Containment *containment = m_containment.data();
00449 QRect screen = containment->corona()->screenGeometry(v->screen());
00450 QSizeF size = containment->size();
00451 const int max = (containment->formFactor() == Plasma::Vertical ? screen.width() : screen.height()) / 3;
00452 height = qBound(16, height, max);
00453
00454 if (containment->formFactor() == Plasma::Vertical) {
00455 size.setWidth(height);
00456 } else {
00457 size.setHeight(height);
00458 }
00459
00460 containment->resize(size);
00461 containment->setMinimumSize(size);
00462 containment->setMaximumSize(size);
00463 }
00464 }
00465
00466 QString Containment::hiding() const
00467 {
00468 PanelView *v = panel();
00469 if (v) {
00470 switch (v->visibilityMode()) {
00471 case PanelView::NormalPanel:
00472 return "none";
00473 break;
00474 case PanelView::AutoHide:
00475 return "autohide";
00476 break;
00477 case PanelView::LetWindowsCover:
00478 return "windowscover";
00479 break;
00480 case PanelView::WindowsGoBelow:
00481 return "windowsbelow";
00482 break;
00483 }
00484 }
00485
00486 return "none";
00487 }
00488
00489 void Containment::setHiding(const QString &mode)
00490 {
00491 PanelView *v = panel();
00492 if (v) {
00493 if (mode.compare("autohide", Qt::CaseInsensitive) == 0) {
00494 v->setVisibilityMode(PanelView::AutoHide);
00495 } else if (mode.compare("windowscover", Qt::CaseInsensitive) == 0) {
00496 v->setVisibilityMode(PanelView::LetWindowsCover);
00497 } else if (mode.compare("windowsbelow", Qt::CaseInsensitive) == 0) {
00498 v->setVisibilityMode(PanelView::WindowsGoBelow);
00499 } else {
00500 v->setVisibilityMode(PanelView::NormalPanel);
00501 }
00502 }
00503 }
00504
00505 #include "containment.moc"
00506