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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
/*
SPDX-FileCopyrightText: 2021 Cyril Rossi <cyril.rossi@enioka.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "shellcontainmentconfig.h"
#include <KActionCollection>
#include <KActivities/Consumer>
#include <KActivities/Info>
#include <KLocalizedContext>
#include <KLocalizedString>
#include <KPackage/Package>
#include <QQmlContext>
#include <QQuickItem>
#include <QScreen>
#include "panelview.h"
#include "screenpool.h"
#include "shellcorona.h"
#include <chrono>
using namespace std::chrono_literals;
ScreenPoolModel::ScreenPoolModel(ShellCorona *corona, QObject *parent)
: QAbstractListModel(parent)
, m_corona(corona)
{
m_reloadTimer = new QTimer(this);
m_reloadTimer->setSingleShot(true);
m_reloadTimer->setInterval(200ms);
connect(m_reloadTimer, &QTimer::timeout, this, &ScreenPoolModel::load);
connect(m_corona, &Plasma::Corona::screenAdded, m_reloadTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
connect(m_corona, &Plasma::Corona::screenRemoved, m_reloadTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
}
ScreenPoolModel::~ScreenPoolModel() = default;
QVariant ScreenPoolModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.column() != 0 || index.row() < 0 || index.row() >= int(m_screens.size())) {
return QVariant();
}
const Data &d = m_screens.at(index.row());
switch (role) {
case ScreenIdRole:
return d.id;
case ScreenNameRole:
return d.name;
case ContainmentsRole: {
auto *cont = m_containments.at(index.row());
return QVariant::fromValue<QObject *>(cont);
}
case PrimaryRole:
return d.primary;
case EnabledRole:
return d.enabled;
}
return QVariant();
}
int ScreenPoolModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return m_screens.size();
}
QHash<int, QByteArray> ScreenPoolModel::roleNames() const
{
QHash<int, QByteArray> roles({{ScreenIdRole, QByteArrayLiteral("screenId")},
{ScreenNameRole, QByteArrayLiteral("screenName")},
{ContainmentsRole, QByteArrayLiteral("containments")},
{EnabledRole, QByteArrayLiteral("isEnabled")},
{PrimaryRole, QByteArrayLiteral("isPrimary")}});
return roles;
}
void ScreenPoolModel::load()
{
beginResetModel();
m_screens.clear();
qDeleteAll(m_containments);
m_containments.clear();
QSet<int> unknownScreenIds;
for (auto *cont : m_corona->containments()) {
connect(cont, &Plasma::Containment::destroyedChanged, this, &ScreenPoolModel::load, Qt::UniqueConnection);
if (!cont->destroyed()) {
unknownScreenIds.insert(cont->lastScreen());
}
}
int knownId = 0;
for (QScreen *screen : m_corona->screenPool()->screenOrder()) {
Data d;
unknownScreenIds.remove(knownId);
d.id = knownId;
if (screen->name().contains(QStringLiteral("eDP"))) {
d.name = i18n("Internal Screen on %1", screen->name());
} else if (screen->model().contains(screen->name())) {
d.name = screen->model();
} else {
d.name = i18nc("Screen manufacturer and model on connector", "%1 %2 on %3", screen->manufacturer(), screen->model(), screen->name());
}
d.primary = knownId == 0;
d.enabled = true;
auto *conts = new ShellContainmentModel(m_corona, knownId, this);
conts->load();
// Exclude screens which don't have any containemnt assigned
if (conts->rowCount() > 0) {
m_containments.push_back(conts);
m_screens.push_back(d);
} else {
delete conts;
}
++knownId;
}
QList sortedIds = unknownScreenIds.values();
std::sort(sortedIds.begin(), sortedIds.end());
int i = 1;
for (int id : sortedIds) {
Data d;
d.id = id;
d.name = i18n("Disconnected Screen %1", id + 1);
d.primary = id == 0;
d.enabled = false;
auto *conts = new ShellContainmentModel(m_corona, id, this);
conts->load();
m_containments.push_back(conts);
m_screens.push_back(d);
i++;
}
endResetModel();
}
void ScreenPoolModel::remove(int screenId)
{
// Don't allow to remove currently used containemnts
if (m_corona->screenPool()->screenForId(screenId)) {
return;
}
// remove containments of *all* activities
auto conts = m_corona->containmentsForScreen(screenId);
for (auto *cont : std::as_const(conts)) {
// Don't call destroy directly, so we can have the undo action notification
auto *destroyAction = cont->actions()->action("remove");
if (destroyAction) {
destroyAction->trigger();
}
}
}
// ---
ShellContainmentModel::ShellContainmentModel(ShellCorona *corona, int screenId, ScreenPoolModel *parent)
: QAbstractListModel(parent)
, m_screenId(screenId)
, m_corona(corona)
, m_screenPoolModel(parent)
, m_activityConsumer(new KActivities::Consumer(this))
{
m_reloadTimer = new QTimer(this);
m_reloadTimer->setSingleShot(true);
m_reloadTimer->setInterval(200ms);
connect(m_reloadTimer, &QTimer::timeout, this, &ShellContainmentModel::load);
connect(m_corona, &ShellCorona::startupCompleted, this, &ShellContainmentModel::load);
connect(m_corona, &Plasma::Corona::containmentAdded, m_reloadTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
connect(m_corona, &Plasma::Corona::screenOwnerChanged, m_reloadTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
connect(m_corona, &ShellCorona::containmentPreviewReady, this, [this](Plasma::Containment *containment, const QString &path) {
int i = 0;
for (auto &d : m_containments) {
if (d.containment == containment) {
d.image = path;
emit dataChanged(index(i, 0), index(i, 0));
break;
}
++i;
}
});
}
ShellContainmentModel::~ShellContainmentModel() = default;
QVariant ShellContainmentModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.column() != 0 || index.row() < 0 || index.row() >= int(m_containments.size())) {
return QVariant();
}
const Data &d = m_containments.at(index.row());
switch (role) {
case Qt::DisplayRole:
return d.name;
case ContainmentIdRole:
return d.id;
case NameRole:
return d.name;
case ScreenRole:
return d.screen;
case EdgeRole:
return ShellContainmentModel::plasmaLocationToString(d.edge);
case EdgePositionRole:
return qMax(0, m_edgeCount.value(d.screen).value(d.edge).indexOf(d.id));
case PanelCountAtRightRole:
return qMax(0, m_edgeCount.value(d.screen).value(Plasma::Types::RightEdge).count());
case PanelCountAtTopRole:
return qMax(0, m_edgeCount.value(d.screen).value(Plasma::Types::TopEdge).count());
case PanelCountAtLeftRole:
return qMax(0, m_edgeCount.value(d.screen).value(Plasma::Types::LeftEdge).count());
case PanelCountAtBottomRole:
return qMax(0, m_edgeCount.value(d.screen).value(Plasma::Types::BottomEdge).count());
case ActivityRole: {
const auto *activityInfo = m_activitiesInfos.value(d.activity);
if (activityInfo) {
return activityInfo->name();
}
break;
}
case IsActiveRole:
return d.isActive;
case ImageSourceRole:
return d.image;
case DestroyedRole:
return d.containment->destroyed();
}
return QVariant();
}
int ShellContainmentModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return m_containments.size();
}
QHash<int, QByteArray> ShellContainmentModel::roleNames() const
{
QHash<int, QByteArray> roles({{ContainmentIdRole, QByteArrayLiteral("containmentId")},
{NameRole, QByteArrayLiteral("name")},
{ScreenRole, QByteArrayLiteral("screen")},
{EdgeRole, QByteArrayLiteral("edge")},
{EdgePositionRole, QByteArrayLiteral("edgePosition")},
{PanelCountAtRightRole, QByteArrayLiteral("panelCountAtRight")},
{PanelCountAtTopRole, QByteArrayLiteral("panelCountAtTop")},
{PanelCountAtLeftRole, QByteArrayLiteral("panelCountAtLeft")},
{PanelCountAtBottomRole, QByteArrayLiteral("panelCountAtBottom")},
{ActivityRole, QByteArrayLiteral("activity")},
{IsActiveRole, QByteArrayLiteral("active")},
{ImageSourceRole, QByteArrayLiteral("imageSource")},
{DestroyedRole, QByteArrayLiteral("isDestroyed")}});
return roles;
}
ScreenPoolModel *ShellContainmentModel::screenPoolModel() const
{
return m_screenPoolModel;
}
void ShellContainmentModel::remove(int contId)
{
if (contId < 0) {
return;
}
auto *cont = containmentById(contId);
if (cont) {
disconnect(cont, nullptr, this, nullptr);
// Don't call destroy directly, so we can have the undo action notification
auto *destroyAction = cont->actions()->action("remove");
if (destroyAction) {
destroyAction->trigger();
}
}
load();
}
void ShellContainmentModel::moveContainementToScreen(unsigned int contId, int newScreen)
{
if (contId == 0 || newScreen < 0) {
return;
}
auto containmentIt = std::find_if(m_containments.begin(), m_containments.end(), [contId](Data &d) {
return d.id == contId;
});
if (containmentIt == m_containments.end()) {
return;
}
if (containmentIt->screen == newScreen) {
return;
}
auto *cont = containmentById(contId);
if (cont == nullptr) {
return;
}
// If it's a panel, only move that one
if (cont->containmentType() == Plasma::Types::PanelContainment || cont->containmentType() == Plasma::Types::CustomPanelContainment) {
m_corona->setScreenForContainment(cont, newScreen);
} else {
// If it's a desktop, for now move all desktops for all activities
const int oldScreen = cont->screen() >= 0 ? cont->screen() : cont->lastScreen();
m_corona->swapDesktopScreens(oldScreen, newScreen);
}
}
bool ShellContainmentModel::findContainment(unsigned int containmentId) const
{
return m_containments.cend() != std::find_if(m_containments.cbegin(), m_containments.cend(), [containmentId](const Data &d) {
return d.id == containmentId;
});
}
void ShellContainmentModel::load()
{
beginResetModel();
for (auto &d : m_containments) {
disconnect(d.containment, nullptr, this, nullptr);
}
m_containments.clear();
m_edgeCount.clear();
for (const auto *cont : m_corona->containments()) {
// Skip the systray
if (qobject_cast<Plasma::Applet *>(cont->parent())) {
continue;
}
// Only allow current activity for now (panels always go in)
if (cont->containmentType() != Plasma::Types::PanelContainment && cont->containmentType() != Plasma::Types::CustomPanelContainment
&& cont->activity() != m_activityConsumer->currentActivity()) {
continue;
}
if (!m_edgeCount.contains(cont->lastScreen())) {
m_edgeCount[cont->lastScreen()] = QHash<Plasma::Types::Location, QList<int>>();
m_edgeCount[cont->lastScreen()][cont->location()] = QList<int>();
}
m_edgeCount[cont->lastScreen()][cont->location()].append(cont->id());
m_corona->grabContainmentPreview(const_cast<Plasma::Containment *>(cont));
Data d;
d.id = cont->id();
d.name = cont->title() + " (" + ShellContainmentModel::containmentTypeToString(cont->containmentType()) + ")";
d.screen = cont->lastScreen();
d.edge = cont->location();
d.activity = cont->activity();
d.isActive = cont->screen() != -1;
d.containment = cont;
d.image = containmentPreview(const_cast<Plasma::Containment *>(cont));
if (cont->lastScreen() == m_screenId || (cont->lastScreen() == -1 && cont->screen() == m_screenId)) {
m_containments.push_back(d);
connect(cont, &QObject::destroyed, this, &ShellContainmentModel::load);
connect(cont, &Plasma::Containment::destroyedChanged, this, &ShellContainmentModel::load);
connect(cont, &Plasma::Containment::locationChanged, this, &ShellContainmentModel::load);
}
}
endResetModel();
}
void ShellContainmentModel::loadActivitiesInfos()
{
beginResetModel();
for (const auto &cont : m_containments) {
const auto activitId = cont.activity;
if (activitId.isEmpty()) {
continue;
}
auto *activityInfo = new KActivities::Info(cont.activity, this);
if (activityInfo) {
if (!m_activitiesInfos.value(cont.activity)) {
m_activitiesInfos[cont.activity] = activityInfo;
}
}
}
endResetModel();
}
QString ShellContainmentModel::plasmaLocationToString(Plasma::Types::Location location)
{
switch (location) {
case Plasma::Types::Floating:
return QStringLiteral("floating");
case Plasma::Types::Desktop:
return QStringLiteral("desktop");
case Plasma::Types::FullScreen:
return QStringLiteral("Full Screen");
case Plasma::Types::TopEdge:
return QStringLiteral("top");
case Plasma::Types::BottomEdge:
return QStringLiteral("bottom");
case Plasma::Types::LeftEdge:
return QStringLiteral("left");
case Plasma::Types::RightEdge:
return QStringLiteral("right");
default:
return QString("unknown");
}
}
QString ShellContainmentModel::containmentTypeToString(Plasma::Types::ContainmentType containmentType)
{
switch (containmentType) {
case Plasma::Types::DesktopContainment: /**< A desktop containment */
return QStringLiteral("Desktop");
case Plasma::Types::PanelContainment: /**< A desktop panel */
return QStringLiteral("Panel");
case Plasma::Types::CustomContainment: /**< A containment that is neither a desktop nor a panel
but something application specific */
return QStringLiteral("Custom");
case Plasma::Types::CustomPanelContainment: /**< A customized desktop panel */
return QStringLiteral("Custom Desktop");
case Plasma::Types::CustomEmbeddedContainment: /**< A customized containment embedded in another applet */
return QStringLiteral("Embedded");
default:
return QStringLiteral("Unknown");
}
}
Plasma::Containment *ShellContainmentModel::containmentById(unsigned int id)
{
for (auto *cont : m_corona->containments()) {
if (cont->id() == id) {
return cont;
}
}
return nullptr;
}
QString ShellContainmentModel::containmentPreview(Plasma::Containment *containment)
{
QString savedThumbnail = m_corona->containmentPreviewPath(containment);
if (!savedThumbnail.isEmpty()) {
return savedThumbnail;
}
m_corona->grabContainmentPreview(containment);
// If not found, try to understand the configured wallpaper for the containment, assuming is using the Image plugin
KSharedConfig::Ptr conf = KSharedConfig::openConfig(QLatin1String("plasma-") + m_corona->shell() + QLatin1String("-appletsrc"), KConfig::SimpleConfig);
KConfigGroup containmentsGroup(conf, "Containments");
KConfigGroup config = containmentsGroup.group(QString::number(containment->id()));
auto wallpaperPlugin = config.readEntry("wallpaperplugin");
auto wallpaperConfig = config.group("Wallpaper").group(wallpaperPlugin).group("General");
if (wallpaperConfig.hasKey("Image")) {
// Trying for the wallpaper
auto wallpaper = wallpaperConfig.readEntry("Image", QString());
if (!wallpaper.isEmpty()) {
return wallpaper;
}
}
if (wallpaperConfig.hasKey("Color")) {
auto backgroundColor = wallpaperConfig.readEntry("Color", QColor(0, 0, 0));
return backgroundColor.name();
}
return QString();
}
// ---
ShellContainmentConfig::ShellContainmentConfig(ShellCorona *corona, QWindow *parent)
: QQmlApplicationEngine(parent)
, m_corona(corona)
, m_model(nullptr)
{
}
ShellContainmentConfig::~ShellContainmentConfig() = default;
void ShellContainmentConfig::init()
{
m_model = new ScreenPoolModel(m_corona, this);
m_model->load();
auto *localizedContext = new KLocalizedContext(this);
localizedContext->setTranslationDomain(QStringLiteral("plasma_shell_") + m_corona->shell());
rootContext()->setContextObject(localizedContext);
rootContext()->setContextProperty(QStringLiteral("ShellContainmentModel"), m_model);
load(m_corona->kPackage().fileUrl("containmentmanagementui"));
if (!rootObjects().isEmpty()) {
auto *obj = qobject_cast<QWindow *>(rootObjects().first());
connect(obj, &QWindow::visibleChanged, this, [this, obj]() {
deleteLater();
});
}
}
|