1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/*
* SPDX-FileCopyrightText: 2013 Giorgos Tsiapaliokas <terietor@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "view.h"
#include <QDBusInterface>
#include <QDBusReply>
#include <QDebug>
#include <QFileDialog>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickItem>
#include <KDesktopFile>
#include <KLocalizedString>
#include <KPackage/Package>
#include <KPackage/PackageLoader>
#include <Plasma/PluginLoader>
#include <plasmaquick/appletquickitem.h>
class ViewerCorona : public Plasma::Corona
{
public:
ViewerCorona(QObject *parent)
: Plasma::Corona(parent)
, m_view(nullptr)
{
}
void setView(View *view)
{
m_view = view;
}
QRect screenGeometry(int id) const override
{
Q_UNUSED(id);
if (m_view) {
return m_view->geometry();
} else {
return QRect();
}
}
int screenForContainment(const Plasma::Containment *containment) const override
{
return 0;
}
private:
View *m_view;
};
View::View(ViewerCorona *cor, QWindow *parent)
: PlasmaQuick::ContainmentView(cor, parent)
{
cor->setView(this);
engine()->rootContext()->setContextProperty("desktop", this);
setSource(QUrl::fromLocalFile(cor->kPackage().filePath("views", "Desktop.qml")));
}
View::~View()
{
}
void View::resizeEvent(QResizeEvent *event)
{
emit corona()->screenGeometryChanged(0);
emit corona()->availableScreenRectChanged(0);
emit corona()->availableScreenRegionChanged(0);
PlasmaQuick::ContainmentView::resizeEvent(event);
}
QString View::pluginFromPath(const QString &path) const
{
QDir dir(path);
if (!dir.exists()) {
return QString();
}
QString metadataPath = dir.absolutePath();
if (!QFile(metadataPath).exists()) {
return QString();
} else {
return metadataPath;
}
}
void View::addApplet(const QString &applet)
{
QString metadataPath = pluginFromPath(applet);
Plasma::Containment *c = containment();
if (!c) {
qCritical("Containment doesn't exist");
return;
}
Plasma::Applet *a = nullptr;
if (metadataPath.isEmpty()) {
a = containment()->createApplet(applet);
} else {
a = Plasma::PluginLoader::self()->loadApplet(metadataPath);
// Load translations from KPackage files if bundled
// TODO: what to do in KF6?
/*const QString localePath = a->kPackage().filePath("translations");
if (!localePath.isEmpty()) {
const QString localeDomain = QByteArray("plasma_applet_") + a->pluginMetaData().pluginId();
KLocalizedString::addDomainLocaleDir(localeDomain.toLatin1(), localePath);
}*/
containment()->addApplet(a);
}
if (!a->pluginMetaData().isValid()) {
// xgettext:no-c-format
qCritical() << i18n("Applet %1 does not exist.", applet);
return;
}
m_lastAppletName = applet;
}
void View::addContainment(const QString &cont)
{
QString actualCont = pluginFromPath(cont);
if (actualCont.isEmpty()) {
actualCont = cont;
}
Plasma::Containment *c = corona()->createContainment(actualCont);
if (!c->pluginMetaData().isValid()) {
// xgettext:no-c-format
qCritical() << i18n("Containment %1 does not exist.", actualCont);
return;
}
setContainment(c);
connect(containment(), &Plasma::Containment::appletRemoved, [this](Plasma::Applet *applet) {
if (applet && applet->pluginMetaData().isValid()) {
addApplet(applet->pluginMetaData().pluginId());
}
});
}
void View::addFormFactor(const QString &formFactor)
{
Plasma::Types::FormFactor formFactorType = Plasma::Types::Planar;
const QString ff = formFactor.toLower();
if (ff.isEmpty() || ff == QStringLiteral("planar")) {
formFactorType = Plasma::Types::Planar;
} else if (ff == QStringLiteral("vertical")) {
formFactorType = Plasma::Types::Vertical;
} else if (ff == QStringLiteral("horizontal")) {
formFactorType = Plasma::Types::Horizontal;
} else if (ff == QStringLiteral("mediacenter")) {
formFactorType = Plasma::Types::MediaCenter;
} else if (ff == QStringLiteral("application")) {
formFactorType = Plasma::Types::Application;
} else {
qWarning() << "FormFactor " << ff << "doesn't exist. Planar formFactor has been used!!";
}
Plasma::Containment *c = containment();
if (!c) {
qCritical("Containment doesn't exist!");
return;
}
c->setFormFactor(formFactorType);
}
void View::changeFormFactor(int formFactor)
{
QString formFactorType = "planar";
switch (formFactor) {
case Plasma::Types::Planar:
formFactorType = "planar";
break;
case Plasma::Types::Vertical:
formFactorType = "vertical";
break;
case Plasma::Types::Horizontal:
formFactorType = "horizontal";
break;
case Plasma::Types::MediaCenter:
formFactorType = "mediacenter";
break;
case Plasma::Types::Application:
formFactorType = "application";
break;
}
addFormFactor(formFactorType);
}
void View::addLocation(const QString &location)
{
Plasma::Types::Location locationType = Plasma::Types::Floating;
const QString l = location.toLower();
if (l.isEmpty() || l == QStringLiteral("floating")) {
locationType = Plasma::Types::Floating;
} else if (l == QStringLiteral("desktop")) {
locationType = Plasma::Types::Desktop;
} else if (l == QStringLiteral("fullscreen")) {
locationType = Plasma::Types::FullScreen;
} else if (l == QStringLiteral("topedge")) {
locationType = Plasma::Types::TopEdge;
} else if (l == QStringLiteral("bottomedge")) {
locationType = Plasma::Types::BottomEdge;
} else if (l == QStringLiteral("rightedge")) {
locationType = Plasma::Types::RightEdge;
} else if (l == QStringLiteral("leftedge")) {
locationType = Plasma::Types::LeftEdge;
} else {
qWarning() << "Location " << l << "doesn't exist. Floating location has been used!!";
}
Plasma::Containment *c = containment();
if (!c) {
qCritical("Containment doesn't exist!");
return;
}
setLocation(locationType);
}
void View::emitExternalData(const QString &data)
{
if (data.isEmpty()) {
return;
}
Plasma::Applet *applet = containment()->applets().constFirst();
PlasmaQuick::AppletQuickItem *graphicsObject = PlasmaQuick::AppletQuickItem::itemForApplet(applet);
if (!graphicsObject) {
return;
}
QMetaObject::invokeMethod(graphicsObject, "externalData", Q_ARG(QString, QString()), Q_ARG(QVariant, data));
}
void View::changeLocation(int location)
{
QString locationType = "floating";
switch (location) {
case Plasma::Types::Floating:
locationType = "floating";
break;
case Plasma::Types::Desktop:
locationType = "desktop";
break;
case Plasma::Types::FullScreen:
locationType = "fullscreen";
break;
case Plasma::Types::TopEdge:
locationType = "topedge";
break;
case Plasma::Types::BottomEdge:
locationType = "bottomedge";
break;
case Plasma::Types::RightEdge:
locationType = "rightedge";
break;
case Plasma::Types::LeftEdge:
locationType = "leftedge";
break;
}
addLocation(locationType);
}
ViewerCorona *View::createCorona(QObject *parent)
{
KPackage::Package package = KPackage::PackageLoader::self()->loadPackage("Plasma/Shell");
package.setPath("org.kde.plasma.plasmoidviewershell");
ViewerCorona *cor = new ViewerCorona(parent);
cor->setKPackage(package);
return cor;
}
void View::takeScreenShot()
{
QDBusInterface interface(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QStringLiteral("org.kde.kwin.Screenshot"));
QDBusPendingCall async = interface.asyncCall(QStringLiteral("screenshotArea"), x(), y(), width(), height());
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this);
connect(watcher, &QDBusPendingCallWatcher::finished, [](QDBusPendingCallWatcher *call) {
QDBusPendingReply<QString> reply = *call;
call->deleteLater();
if (!reply.isValid()) {
qDebug() << "The screenshot has failed, the reply is invalid with error" << reply.error().message();
return;
}
QString dest = QFileDialog::getSaveFileName(nullptr, i18nc("@title:window", "Save Screenshot"), QDir::homePath(), QStringLiteral("Images (*.png)"));
if (dest.isEmpty()) {
return;
}
if (!dest.endsWith(QStringLiteral(".png"))) {
dest.append(QStringLiteral(".png"));
}
QFile f(reply.value());
f.rename(dest);
});
}
#include "moc_view.cpp"
|