Changeset View
Changeset View
Standalone View
Standalone View
applets/panelspacer/plugin/panelspacer.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2020 Marco Martin <mart@kde.org> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program; if not, write to the * | ||||
16 | * Free Software Foundation, Inc., * | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | ||||
18 | ***************************************************************************/ | ||||
19 | | ||||
20 | #include "panelspacer.h" | ||||
21 | | ||||
22 | #include <QDebug> | ||||
23 | #include <QProcess> | ||||
24 | #include <QtQml> | ||||
25 | | ||||
26 | #include <Plasma/Containment> | ||||
27 | #include <Plasma/Corona> | ||||
28 | #include <PlasmaQuick/AppletQuickItem> | ||||
29 | | ||||
30 | class SpacersTrackerSingleton | ||||
31 | { | ||||
32 | public: | ||||
33 | SpacersTracker self; | ||||
34 | }; | ||||
35 | | ||||
36 | Q_GLOBAL_STATIC(SpacersTrackerSingleton, privateSpacersTrackerSelf) | ||||
37 | | ||||
38 | SpacersTracker::SpacersTracker(QObject *parent) | ||||
39 | : QObject(parent) | ||||
40 | { | ||||
41 | } | ||||
42 | | ||||
43 | SpacersTracker::~SpacersTracker() | ||||
44 | { | ||||
45 | } | ||||
46 | | ||||
47 | SpacersTracker *SpacersTracker::self() | ||||
48 | { | ||||
49 | return &privateSpacersTrackerSelf()->self; | ||||
50 | } | ||||
51 | | ||||
52 | void SpacersTracker::insertSpacer(Plasma::Containment *containment, PanelSpacer *spacer) | ||||
53 | { | ||||
54 | const bool wasTwin = m_spacers[containment].count() == 2; | ||||
55 | m_spacers[containment] << (spacer); | ||||
56 | const bool isTwin = m_spacers[containment].count() == 2; | ||||
57 | | ||||
58 | if (isTwin) { | ||||
59 | auto *lay1 = m_spacers[containment].first(); | ||||
60 | auto *lay2 = m_spacers[containment].last(); | ||||
61 | lay1->setTwinSpacer(lay2->property("_plasma_graphicObject").value<PlasmaQuick::AppletQuickItem *>()); | ||||
62 | lay2->setTwinSpacer(lay1->property("_plasma_graphicObject").value<PlasmaQuick::AppletQuickItem *>()); | ||||
63 | } else if (wasTwin) { | ||||
64 | for (auto *lay : m_spacers[containment]) { | ||||
65 | lay->setTwinSpacer(nullptr); | ||||
66 | } | ||||
67 | } | ||||
68 | } | ||||
69 | | ||||
70 | void SpacersTracker::removeSpacer(Plasma::Containment *containment, PanelSpacer *spacer) | ||||
71 | { | ||||
72 | const bool wasTwin = m_spacers[containment].count() == 2; | ||||
73 | m_spacers[containment].removeAll(spacer); | ||||
74 | const bool isTwin = m_spacers[containment].count() == 2; | ||||
75 | | ||||
76 | if (isTwin) { | ||||
77 | auto *lay1 = m_spacers[containment].first(); | ||||
78 | auto *lay2 = m_spacers[containment].last(); | ||||
79 | lay1->setTwinSpacer(lay2->property("_plasma_graphicObject").value<PlasmaQuick::AppletQuickItem *>()); | ||||
80 | lay2->setTwinSpacer(lay1->property("_plasma_graphicObject").value<PlasmaQuick::AppletQuickItem *>()); | ||||
81 | | ||||
82 | } else if (wasTwin) { | ||||
83 | for (auto *lay : m_spacers[containment]) { | ||||
84 | lay->setTwinSpacer(nullptr); | ||||
85 | } | ||||
86 | } | ||||
87 | | ||||
88 | if (m_spacers[containment].isEmpty()) { | ||||
89 | m_spacers.remove(containment); | ||||
90 | } | ||||
91 | } | ||||
92 | | ||||
93 | ///////////////////////////////////////////////////////////////////// | ||||
94 | | ||||
95 | PanelSpacer::PanelSpacer(QObject *parent, const QVariantList &args) | ||||
96 | : Plasma::Applet(parent, args) | ||||
97 | { | ||||
98 | } | ||||
99 | | ||||
100 | PanelSpacer::~PanelSpacer() | ||||
101 | { | ||||
102 | SpacersTracker::self()->removeSpacer(containment(), this); | ||||
103 | } | ||||
104 | | ||||
105 | void PanelSpacer::init() | ||||
106 | { | ||||
107 | | ||||
108 | } | ||||
109 | | ||||
110 | void PanelSpacer::constraintsEvent(Plasma::Types::Constraints constraints) | ||||
111 | { | ||||
112 | // At this point we're sure the AppletQuickItem has been created already | ||||
113 | if (constraints & Plasma::Types::UiReadyConstraint) { | ||||
114 | Q_ASSERT(containment()); | ||||
115 | Q_ASSERT(containment()->corona()); | ||||
116 | | ||||
117 | SpacersTracker::self()->insertSpacer(containment(), this); | ||||
118 | } | ||||
119 | | ||||
120 | Plasma::Applet::constraintsEvent(constraints); | ||||
121 | } | ||||
122 | | ||||
123 | void PanelSpacer::setTwinSpacer(PlasmaQuick::AppletQuickItem *spacer) | ||||
124 | { | ||||
125 | if (m_twinSpacer == spacer) { | ||||
126 | return; | ||||
127 | } | ||||
128 | | ||||
129 | m_twinSpacer = spacer; | ||||
130 | emit twinSpacerChanged(); | ||||
131 | } | ||||
132 | | ||||
133 | PlasmaQuick::AppletQuickItem *PanelSpacer::twinSpacer() const | ||||
134 | { | ||||
135 | return m_twinSpacer; | ||||
136 | } | ||||
137 | | ||||
138 | PlasmaQuick::AppletQuickItem *PanelSpacer::containmentGraphicObject() const | ||||
139 | { | ||||
140 | return containment()->property("_plasma_graphicObject").value<PlasmaQuick::AppletQuickItem *>(); | ||||
141 | } | ||||
142 | | ||||
143 | | ||||
144 | K_EXPORT_PLASMA_APPLET_WITH_JSON(panelspacer, PanelSpacer, "metadata.json") | ||||
145 | | ||||
146 | #include "panelspacer.moc" |