Changeset View
Changeset View
Standalone View
Standalone View
kstyle/breezetoolsareamanager.cpp
- This file was added.
1 | #include "breezetoolsareamanager.h" | ||||
---|---|---|---|---|---|
2 | #include <QObject> | ||||
3 | #include <QWidget> | ||||
4 | #include <QToolBar> | ||||
5 | #include <QWindow> | ||||
6 | #include <QMainWindow> | ||||
7 | #include <QMoveEvent> | ||||
8 | #include <QDebug> | ||||
9 | | ||||
10 | namespace Breeze { | ||||
11 | ToolsAreaManager::ToolsAreaManager(Helper *helper, QObject *parent) : QObject(parent), _helper(helper) {} | ||||
12 | | ||||
13 | ToolsAreaManager::~ToolsAreaManager() {} | ||||
14 | | ||||
15 | void ToolsAreaManager::updateAnimations() { | ||||
16 | for (auto entry : animationMap) { | ||||
17 | entry.foregroundColorAnimation->setStartValue(_helper->titleBarTextColor(false)); | ||||
18 | entry.foregroundColorAnimation->setEndValue(_helper->titleBarTextColor(true)); | ||||
19 | | ||||
20 | entry.backgroundColorAnimation->setStartValue(_helper->titleBarColor(false)); | ||||
21 | entry.backgroundColorAnimation->setEndValue(_helper->titleBarColor(true)); | ||||
22 | | ||||
23 | entry.foregroundColorAnimation->setDuration( | ||||
24 | _helper->decorationConfig()->animationsEnabled() ? | ||||
25 | _helper->decorationConfig()->animationsDuration() : | ||||
26 | 0 | ||||
27 | ); | ||||
28 | entry.backgroundColorAnimation->setDuration( | ||||
29 | _helper->decorationConfig()->animationsEnabled() ? | ||||
30 | _helper->decorationConfig()->animationsDuration() : | ||||
31 | 0 | ||||
32 | ); | ||||
33 | } | ||||
34 | } | ||||
35 | | ||||
36 | void ToolsAreaManager::registerAnimation(QWidget *widget) { | ||||
37 | auto window = widget->window()->windowHandle(); | ||||
38 | if (window && !animationMap.contains(window)) { | ||||
39 | | ||||
40 | auto foregroundColorAnimation = new QVariantAnimation(this); | ||||
41 | connect(foregroundColorAnimation, &QVariantAnimation::valueChanged, | ||||
42 | this, &ToolsAreaManager::toolbarUpdated); | ||||
43 | | ||||
44 | auto backgroundColorAnimation = new QVariantAnimation(this); | ||||
45 | connect(backgroundColorAnimation, &QVariantAnimation::valueChanged, | ||||
46 | this, &ToolsAreaManager::toolbarUpdated); | ||||
47 | | ||||
48 | foregroundColorAnimation->setStartValue(_helper->titleBarTextColor(false)); | ||||
49 | foregroundColorAnimation->setEndValue(_helper->titleBarTextColor(true)); | ||||
50 | | ||||
51 | backgroundColorAnimation->setStartValue(_helper->titleBarColor(false)); | ||||
52 | backgroundColorAnimation->setEndValue(_helper->titleBarColor(true)); | ||||
53 | | ||||
54 | foregroundColorAnimation->setDuration( | ||||
55 | _helper->decorationConfig()->animationsEnabled() ? | ||||
56 | _helper->decorationConfig()->animationsDuration() : | ||||
57 | 0 | ||||
58 | ); | ||||
59 | backgroundColorAnimation->setDuration( | ||||
60 | _helper->decorationConfig()->animationsEnabled() ? | ||||
61 | _helper->decorationConfig()->animationsDuration() : | ||||
62 | 0 | ||||
63 | ); | ||||
64 | | ||||
65 | animationMap[window] = ToolsAreaAnimation{ | ||||
66 | foregroundColorAnimation, | ||||
67 | backgroundColorAnimation, | ||||
68 | window->isActive(), | ||||
69 | }; | ||||
70 | | ||||
71 | connect(window, &QWindow::activeChanged, | ||||
72 | this, [=]() { | ||||
73 | if (animationMap[window].foregroundColorAnimation.isNull() || animationMap[window].backgroundColorAnimation.isNull()) return; | ||||
74 | | ||||
75 | auto prevActive = animationMap[window].prevActive; | ||||
76 | if (prevActive && !window->isActive()) { | ||||
77 | animationMap[window].foregroundColorAnimation->setDirection(QAbstractAnimation::Backward); | ||||
78 | animationMap[window].backgroundColorAnimation->setDirection(QAbstractAnimation::Backward); | ||||
79 | | ||||
80 | animationMap[window].foregroundColorAnimation->start(); | ||||
81 | animationMap[window].backgroundColorAnimation->start(); | ||||
82 | } else if (!prevActive && window->isActive()) { | ||||
83 | animationMap[window].foregroundColorAnimation->setDirection(QAbstractAnimation::Forward); | ||||
84 | animationMap[window].backgroundColorAnimation->setDirection(QAbstractAnimation::Forward); | ||||
85 | | ||||
86 | animationMap[window].foregroundColorAnimation->start(); | ||||
87 | animationMap[window].backgroundColorAnimation->start(); | ||||
88 | } | ||||
89 | animationMap[window].prevActive = window->isActive(); | ||||
90 | }); | ||||
91 | | ||||
92 | } | ||||
93 | } | ||||
94 | | ||||
95 | bool ToolsAreaManager::animationRunning(const QWidget *widget) { | ||||
96 | auto window = widget->window()->windowHandle(); | ||||
97 | if (window && animationMap.contains(window)) { | ||||
98 | return ( | ||||
99 | animationMap[window].foregroundColorAnimation->state() == QAbstractAnimation::Running | ||||
100 | && | ||||
101 | animationMap[window].backgroundColorAnimation->state() == QAbstractAnimation::Running | ||||
102 | ); | ||||
103 | } | ||||
104 | return false; | ||||
105 | } | ||||
106 | | ||||
107 | QColor ToolsAreaManager::foreground(const QWidget *widget) { | ||||
108 | auto window = widget->window()->windowHandle(); | ||||
109 | if (window && animationMap.contains(window) && animationMap[window].foregroundColorAnimation) { | ||||
110 | return animationMap[window].foregroundColorAnimation->currentValue().value<QColor>(); | ||||
111 | } | ||||
112 | return QColor(); | ||||
113 | } | ||||
114 | | ||||
115 | QColor ToolsAreaManager::background(const QWidget *widget) { | ||||
116 | auto window = widget->window()->windowHandle(); | ||||
117 | if (window && animationMap.contains(window) && animationMap[window].backgroundColorAnimation) { | ||||
118 | return animationMap[window].backgroundColorAnimation->currentValue().value<QColor>(); | ||||
119 | } | ||||
120 | return QColor(); | ||||
121 | } | ||||
122 | | ||||
123 | void ToolsAreaManager::registerWindow(QWindow *window) | ||||
124 | { | ||||
125 | if (!_registeredWindows.contains(window)) { | ||||
126 | auto geoUpdate = [=]() { | ||||
127 | _helper->_invalidateCachedRects = true; | ||||
128 | emit toolbarUpdated(); | ||||
129 | }; | ||||
130 | connect(window, &QWindow::widthChanged, geoUpdate); | ||||
131 | connect(window, &QWindow::heightChanged, geoUpdate); | ||||
132 | connect(window, &QWindow::destroyed, geoUpdate); | ||||
133 | _registeredWindows << window; | ||||
134 | } | ||||
135 | } | ||||
136 | | ||||
137 | bool ToolsAreaManager::eventFilter(QObject *watched, QEvent *event) | ||||
138 | { | ||||
139 | if (event->type() == QEvent::Move) { | ||||
140 | auto moveEvent = static_cast<QMoveEvent*>(event); | ||||
141 | if (moveEvent->oldPos() != moveEvent->pos()) { | ||||
142 | Q_EMIT toolbarUpdated(); | ||||
143 | } | ||||
144 | } | ||||
145 | return false; | ||||
146 | } | ||||
147 | | ||||
148 | void ToolsAreaManager::registerWidget(QWidget *widget) | ||||
149 | { | ||||
150 | auto win = widget->window(); | ||||
151 | if (win) { | ||||
152 | auto handle = win->windowHandle(); | ||||
153 | if (handle) { | ||||
154 | registerWindow(handle); | ||||
155 | } | ||||
156 | } | ||||
157 | auto window = qobject_cast<QMainWindow*> (widget); | ||||
158 | if (window) { | ||||
159 | connect(this, &ToolsAreaManager::toolbarUpdated, | ||||
160 | window, [this, window]() { | ||||
161 | if (_helper->toolsAreaHasContents(window)) { | ||||
162 | window->setContentsMargins(0,0,0,0); | ||||
163 | } else { | ||||
164 | window->setContentsMargins(0,1,0,0); | ||||
165 | } | ||||
166 | }); | ||||
167 | } | ||||
168 | connect(this, &ToolsAreaManager::toolbarUpdated, | ||||
169 | widget, [widget, this]() { | ||||
170 | widget->update(); | ||||
171 | auto win = widget->window(); | ||||
172 | if (win) { | ||||
173 | auto handle = win->windowHandle(); | ||||
174 | if (handle) { | ||||
175 | _helper->_cachedRects.remove(handle); | ||||
176 | widget->repaint(); | ||||
177 | } | ||||
178 | } | ||||
179 | }); | ||||
180 | auto toolbar = qobject_cast<QToolBar*>(widget); | ||||
181 | if (toolbar) { | ||||
182 | connect(this, &ToolsAreaManager::toolbarUpdated, | ||||
183 | widget, [=]() { | ||||
184 | const auto rect = _helper->toolsAreaToolbarsRect(widget); | ||||
185 | if (rect.bottom() != widget->geometry().bottom()) { | ||||
186 | toolbar->setContentsMargins(0,0,0,0); | ||||
187 | } else { | ||||
188 | toolbar->setContentsMargins(0,0,0,4); | ||||
189 | } | ||||
190 | }); | ||||
191 | connect(toolbar, &QToolBar::visibilityChanged, | ||||
192 | this, [this]() { | ||||
193 | emit toolbarUpdated(); | ||||
194 | }); | ||||
195 | connect(toolbar, &QToolBar::orientationChanged, | ||||
196 | this, [this]() { | ||||
197 | emit toolbarUpdated(); | ||||
198 | }); | ||||
199 | connect(toolbar, &QToolBar::topLevelChanged, | ||||
200 | this, [this]() { | ||||
201 | emit toolbarUpdated(); | ||||
202 | }); | ||||
203 | toolbar->installEventFilter(this); | ||||
204 | } | ||||
205 | connect(widget, &QObject::destroyed, | ||||
206 | this, [this, widget]() { | ||||
207 | unregisterWidget(widget); | ||||
208 | }); | ||||
209 | registerAnimation(widget); | ||||
210 | _registeredWidgets << widget; | ||||
211 | emit toolbarUpdated(); | ||||
212 | } | ||||
213 | | ||||
214 | bool ToolsAreaManager::widgetHasCorrectPaletteSet(const QWidget *widget) | ||||
215 | { | ||||
216 | if (animationRunning(widget)) return true; | ||||
217 | return ( | ||||
218 | widget->palette().color(QPalette::Window) == background(widget) | ||||
219 | && | ||||
220 | widget->palette().color(QPalette::WindowText) == foreground(widget) | ||||
221 | ); | ||||
222 | } | ||||
223 | | ||||
224 | void ToolsAreaManager::unregisterWidget(QWidget *widget) | ||||
225 | { | ||||
226 | if (qobject_cast<QToolBar*>(widget)) { | ||||
227 | widget->setContentsMargins(0,0,0,0); | ||||
228 | widget->removeEventFilter(this); | ||||
229 | } | ||||
230 | _registeredWidgets.remove(widget); | ||||
231 | QList<QWindow*> toRemove; | ||||
232 | for (auto window : animationMap.keys()) { | ||||
233 | if (std::none_of(_registeredWidgets.begin(), _registeredWidgets.end(), [window](QWidget *widget) { | ||||
234 | return window == widget->window()->windowHandle(); | ||||
235 | })) { | ||||
236 | delete animationMap[window].foregroundColorAnimation; | ||||
237 | delete animationMap[window].backgroundColorAnimation; | ||||
238 | toRemove << window; | ||||
239 | } | ||||
240 | } | ||||
241 | for (auto entry : toRemove) { | ||||
242 | animationMap.remove(entry); | ||||
243 | } | ||||
244 | } | ||||
245 | } | ||||
246 | No newline at end of file |