From 390ea0596c6732b957c0409531994b2e4dacfdfa Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Fri, 5 Feb 2021 23:47:51 +0200 Subject: [PATCH] provide newView function for Layouts::Storage --- app/layouts/storage.cpp | 52 ++++++++++++++++++++++++++++++++++++++++- app/layouts/storage.h | 2 +- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/app/layouts/storage.cpp b/app/layouts/storage.cpp index 5aa565a49..f870e9724 100644 --- a/app/layouts/storage.cpp +++ b/app/layouts/storage.cpp @@ -525,6 +525,55 @@ QList Storage::importLayoutFile(const Layout::GenericLayo return importedDocks; } +ViewDelayedCreationData Storage::newView(const Layout::GenericLayout *destination, const QString &templateFile) +{ + if (!destination || !destination->corona()) { + return ViewDelayedCreationData(); + } + + qDebug() << "new view for layout"; + //! Setting mutable for create a containment + destination->corona()->setImmutability(Plasma::Types::Mutable); + + //! update ids to unique ones + QString temp2File = newUniqueIdsLayoutFromFile(destination, templateFile); + + //! Finally import the configuration + QList importedViews = importLayoutFile(destination, temp2File); + + Plasma::Containment *newContainment = (importedViews.size() == 1 ? importedViews[0] : nullptr); + + if (!newContainment || !newContainment->kPackage().isValid()) { + qWarning() << "the requested containment plugin can not be located or loaded"; + return ViewDelayedCreationData(); + } + + auto config = newContainment->config(); + int primaryScrId = destination->corona()->screenPool()->primaryScreenId(); + + QList edges = destination->freeEdges(newContainment->screen()); + + if (edges.count() > 0) { + newContainment->setLocation(edges.at(0)); + } else { + newContainment->setLocation(Plasma::Types::BottomEdge); + } + + config.writeEntry("onPrimary", true); + config.writeEntry("lastScreen", primaryScrId); + + newContainment->config().sync(); + + ViewDelayedCreationData result; + + result.containment = newContainment; + result.forceOnPrimary = false; + result.explicitScreen = primaryScrId; + result.reactToScreenChange = false; + + return result; +} + ViewDelayedCreationData Storage::copyView(const Layout::GenericLayout *layout, Plasma::Containment *containment) { if (!containment || !layout->corona()) { @@ -540,8 +589,9 @@ ViewDelayedCreationData Storage::copyView(const Layout::GenericLayout *layout, P //! WE NEED A WAY TO COPY A CONTAINMENT!!!! QFile copyFile(temp1File); - if (copyFile.exists()) + if (copyFile.exists()) { copyFile.remove(); + } KSharedConfigPtr newFile = KSharedConfig::openConfig(temp1File); KConfigGroup copied_conts = KConfigGroup(newFile, "Containments"); diff --git a/app/layouts/storage.h b/app/layouts/storage.h index 14e52c71f..2777a3bef 100644 --- a/app/layouts/storage.h +++ b/app/layouts/storage.h @@ -83,7 +83,7 @@ public: void importToCorona(const Layout::GenericLayout *layout); void syncToLayoutFile(const Layout::GenericLayout *layout, bool removeLayoutId); ViewDelayedCreationData copyView(const Layout::GenericLayout *layout, Plasma::Containment *containment); - + ViewDelayedCreationData newView(const Layout::GenericLayout *destination, const QString &templateFile); /// STATIC //! Check if an applet config group is valid or belongs to removed applet