DockFocusController.cpp
Go to the documentation of this file.
1 //============================================================================
6 //============================================================================
7 
8 //============================================================================
9 // INCLUDES
10 //============================================================================
11 #include "DockFocusController.h"
12 
13 #include <algorithm>
14 #include <iostream>
15 
16 #include <QPointer>
17 #include <QApplication>
18 #include <QAbstractButton>
19 
20 #include "DockWidget.h"
21 #include "DockAreaWidget.h"
22 #include "DockWidgetTab.h"
23 #include "DockContainerWidget.h"
24 #include "FloatingDockContainer.h"
25 #include "DockManager.h"
26 #include "DockAreaTitleBar.h"
27 
28 #ifdef Q_OS_LINUX
30 #endif
31 
32 namespace ads
33 {
38 {
40  QPointer<CDockWidget> FocusedDockWidget = nullptr;
41  QPointer<CDockAreaWidget> FocusedArea = nullptr;
43 #ifdef Q_OS_LINUX
44  QPointer<CFloatingDockContainer> FloatingWidget = nullptr;
45 #endif
48 
53 
59 };
60 // struct DockFocusControllerPrivate
61 
62 
63 //===========================================================================
65 {
66  DockWidget->setProperty("focused", Focused);
67  DockWidget->tabWidget()->setProperty("focused", Focused);
70 }
71 
72 
73 //===========================================================================
74 static void updateDockAreaFocusStyle(CDockAreaWidget* DockArea, bool Focused)
75 {
76  DockArea->setProperty("focused", Focused);
77  internal::repolishStyle(DockArea);
78  internal::repolishStyle(DockArea->titleBar());
79 }
80 
81 
82 //===========================================================================
83 #ifdef Q_OS_LINUX
84 static void updateFloatingWidgetFocusStyle(CFloatingDockContainer* FloatingWidget, bool Focused)
85 {
86  if (FloatingWidget->hasNativeTitleBar())
87  {
88  return;
89  }
90  auto TitleBar = qobject_cast<CFloatingWidgetTitleBar*>(FloatingWidget->titleBarWidget());
91  if (!TitleBar)
92  {
93  return;
94  }
95  TitleBar->setProperty("focused", Focused);
96  TitleBar->updateStyle();
97 }
98 #endif
99 
100 
101 //============================================================================
103  CDockFocusController *_public) :
104  _this(_public)
105 {
106 
107 }
108 
109 
110 //============================================================================
112 {
114  {
115  return;
116  }
117 
118  CDockAreaWidget* NewFocusedDockArea = nullptr;
119  if (FocusedDockWidget)
120  {
122  }
123 
127  NewFocusedDockArea = FocusedDockWidget->dockAreaWidget();
128  if (NewFocusedDockArea && (FocusedArea != NewFocusedDockArea))
129  {
130  if (FocusedArea)
131  {
132  QObject::disconnect(FocusedArea, SIGNAL(viewToggled(bool)), _this, SLOT(onFocusedDockAreaViewToggled(bool)));
134  }
135 
136  FocusedArea = NewFocusedDockArea;
138  QObject::connect(FocusedArea, SIGNAL(viewToggled(bool)), _this, SLOT(onFocusedDockAreaViewToggled(bool)));
139  }
140 
141 
142  auto NewFloatingWidget = FocusedDockWidget->dockContainer()->floatingWidget();
143  if (NewFloatingWidget)
144  {
145  NewFloatingWidget->setProperty("FocusedDockWidget", QVariant::fromValue(DockWidget));
146  }
147 
148 
149 #ifdef Q_OS_LINUX
150  // This code is required for styling the floating widget titlebar for linux
151  // depending on the current focus state
152  if (FloatingWidget == NewFloatingWidget)
153  {
154  return;
155  }
156 
157  if (FloatingWidget)
158  {
159  updateFloatingWidgetFocusStyle(FloatingWidget, false);
160  }
161  FloatingWidget = NewFloatingWidget;
162 
163  if (FloatingWidget)
164  {
165  updateFloatingWidgetFocusStyle(FloatingWidget, true);
166  }
167 #endif
168 
169  if (old == DockWidget && !ForceFocusChangedSignal)
170  {
171  return;
172  }
173 
174  ForceFocusChangedSignal = false;
175  if (DockWidget->isVisible())
176  {
178  }
179  else
180  {
181  OldFocusedDockWidget = old;
182  QObject::connect(DockWidget, SIGNAL(visibilityChanged(bool)), _this, SLOT(onDockWidgetVisibilityChanged(bool)));
183  }
184 }
185 
186 
187 
188 //============================================================================
190 {
191  auto Sender = sender();
192  auto DockWidget = qobject_cast<ads::CDockWidget*>(Sender);
193  disconnect(Sender, SIGNAL(visibilityChanged(bool)), this, SLOT(onDockWidgetVisibilityChanged(bool)));
194  if (DockWidget && Visible)
195  {
197  }
198 }
199 
200 
201 //============================================================================
203  Super(DockManager),
204  d(new DockFocusControllerPrivate(this))
205 {
206  d->DockManager = DockManager;
207  connect(QApplication::instance(), SIGNAL(focusChanged(QWidget*, QWidget*)),
208  this, SLOT(onApplicationFocusChanged(QWidget*, QWidget*)));
209  connect(d->DockManager, SIGNAL(stateRestored()), SLOT(onStateRestored()));
210 }
211 
212 //============================================================================
214 {
215  delete d;
216 }
217 
218 
219 //===========================================================================
220 void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidget* focusedNow)
221 {
223  {
224  return;
225  }
226 
227  ADS_PRINT("CDockFocusController::onApplicationFocusChanged "
228  << " old: " << focusedOld << " new: " << focusedNow);
229  if (!focusedNow)
230  {
231  return;
232  }
233 
234  // If the close button in another tab steals the focus from the current
235  // active dock widget content, i.e. if the user clicks its close button,
236  // then we immediately give the focus back to the previous focused widget
237  // focusedOld
239  {
240  auto OtherDockWidgetTab = internal::findParent<CDockWidgetTab*>(focusedNow);
241  if (OtherDockWidgetTab && focusedOld)
242  {
243  auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld);
244  if (OldFocusedDockWidget)
245  {
246  focusedOld->setFocus();
247  }
248  return;
249  }
250  }
251 
252  CDockWidget* DockWidget = nullptr;
253  auto DockWidgetTab = qobject_cast<CDockWidgetTab*>(focusedNow);
254  if (DockWidgetTab)
255  {
256  DockWidget = DockWidgetTab->dockWidget();
257  // If the DockWidgetTab "steals" the focus from a widget in the same
258  // DockWidget, then we immediately give the focus back to the previous
259  // focused widget focusedOld
260  if (focusedOld)
261  {
262  auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld);
263  if (OldFocusedDockWidget && OldFocusedDockWidget == DockWidget)
264  {
265  focusedOld->setFocus();
266  }
267  }
268  }
269 
270  if (!DockWidget)
271  {
272  DockWidget = qobject_cast<CDockWidget*>(focusedNow);
273  }
274 
275  if (!DockWidget)
276  {
277  DockWidget = internal::findParent<CDockWidget*>(focusedNow);
278  }
279 
280 #ifdef Q_OS_LINUX
281  if (!DockWidget)
282  {
283  return;
284  }
285 #else
286  if (!DockWidget || DockWidget->tabWidget()->isHidden())
287  {
288  return;
289  }
290 #endif
291 
293 }
294 
295 
296 //===========================================================================
298 {
299  d->updateDockWidgetFocus(focusedNow);
300 }
301 
302 
303 //===========================================================================
305 {
307  {
308  return;
309  }
310 
311  CDockAreaWidget* DockArea = qobject_cast<CDockAreaWidget*>(sender());
312  if (!DockArea || Open)
313  {
314  return;
315  }
316  auto Container = DockArea->dockContainer();
317  auto OpenedDockAreas = Container->openedDockAreas();
318  if (OpenedDockAreas.isEmpty())
319  {
320  return;
321  }
322 
323  CDockManager::setWidgetFocus(OpenedDockAreas[0]->currentDockWidget()->tabWidget());
324 }
325 
326 
327 //===========================================================================
329 {
331  {
332  return;
333  }
334 
335  CDockWidget* DockWidget = qobject_cast<CDockWidget*>(DroppedWidget);
336  if (!DockWidget)
337  {
338  CDockAreaWidget* DockArea = qobject_cast<CDockAreaWidget*>(DroppedWidget);
339  if (DockArea)
340  {
341  DockWidget = DockArea->currentDockWidget();
342  }
343  }
344 
345  if (!DockWidget)
346  {
347  return;
348  }
349 
350  d->ForceFocusChangedSignal = true;
352 }
353 
354 
355 //===========================================================================
357 {
358  if (!FloatingWidget || d->DockManager->isRestoringState())
359  {
360  return;
361  }
362 
363  auto vDockWidget = FloatingWidget->property("FocusedDockWidget");
364  if (!vDockWidget.isValid())
365  {
366  return;
367  }
368 
369  auto DockWidget = vDockWidget.value<CDockWidget*>();
370  if (DockWidget)
371  {
372  d->FocusedDockWidget = nullptr;
375  }
376 }
377 
378 
379 //==========================================================================
381 {
382  if (d->FocusedDockWidget)
383  {
385  }
386 }
387 
388 
389 //==========================================================================
391 {
392  return d->FocusedDockWidget.data();
393 }
394 
395 } // namespace ads
396 
397 //---------------------------------------------------------------------------
398 // EOF DockFocusController.cpp
ads::CDockFocusController::CDockFocusController
CDockFocusController(CDockManager *DockManager)
Definition: DockFocusController.cpp:202
ads::DockFocusControllerPrivate::_this
CDockFocusController * _this
Definition: DockFocusController.cpp:39
ads::CDockFocusController::notifyFloatingWidgetDrop
void notifyFloatingWidgetDrop(CFloatingDockContainer *FloatingWidget)
Definition: DockFocusController.cpp:356
FloatingWidgetTitleBar.h
Declaration of CFloatingWidgetTitleBar class.
ads::CDockAreaWidget::titleBar
CDockAreaTitleBar * titleBar() const
Definition: DockAreaWidget.cpp:956
ads::CDockWidget::dockAreaWidget
CDockAreaWidget * dockAreaWidget() const
Definition: DockWidget.cpp:394
ads::CDockContainerWidget::openedDockAreas
QList< CDockAreaWidget * > openedDockAreas() const
Definition: DockContainerWidget.cpp:1612
ads::DockFocusControllerPrivate::ForceFocusChangedSignal
bool ForceFocusChangedSignal
Definition: DockFocusController.cpp:47
DockContainerWidget.h
Declaration of CDockContainerWidget class.
ads::CDockFocusController::setDockWidgetFocused
void setDockWidgetFocused(CDockWidget *focusedNow)
Definition: DockFocusController.cpp:297
FloatingDockContainer.h
Declaration of CFloatingDockContainer class.
ads::CDockWidgetTab::updateStyle
void updateStyle()
Definition: DockWidgetTab.cpp:702
ads::DockFocusControllerPrivate::FocusedArea
QPointer< CDockAreaWidget > FocusedArea
Definition: DockFocusController.cpp:41
ads::CDockManager::focusedDockWidgetChanged
void focusedDockWidgetChanged(ads::CDockWidget *old, ads::CDockWidget *now)
ads::CDockWidget::DockWidgetFocusable
@ DockWidgetFocusable
if this is enabled, a dock widget can get focus highlighting
Definition: DockWidget.h:155
ads::DockFocusControllerPrivate::updateDockWidgetFocus
void updateDockWidgetFocus(CDockWidget *DockWidget)
Definition: DockFocusController.cpp:111
ads::updateDockAreaFocusStyle
static void updateDockAreaFocusStyle(CDockAreaWidget *DockArea, bool Focused)
Definition: DockFocusController.cpp:74
ads::DockFocusControllerPrivate::DockManager
CDockManager * DockManager
Definition: DockFocusController.cpp:46
DockWidget.h
Declaration of CDockWidget class.
ads::DockFocusControllerPrivate::DockFocusControllerPrivate
DockFocusControllerPrivate(CDockFocusController *_public)
Definition: DockFocusController.cpp:102
ads::CDockAreaWidget
Definition: DockAreaWidget.h:55
ads::CDockManager::setWidgetFocus
static void setWidgetFocus(QWidgetPtr widget)
Definition: DockManager.h:468
ads::CDockFocusController::onStateRestored
void onStateRestored()
Definition: DockFocusController.cpp:380
ads::CDockFocusController::onDockWidgetVisibilityChanged
void onDockWidgetVisibilityChanged(bool Visible)
Definition: DockFocusController.cpp:189
ads::CDockManager
Definition: DockManager.h:69
ads
Definition: ads_globals.h:69
DockWidget
Definition: plot_docker.h:16
ads::CDockWidget::tabWidget
CDockWidgetTab * tabWidget() const
Definition: DockWidget.cpp:328
DockFocusController.h
Declaration of CDockFocusController class.
ads::CDockFocusController
Definition: DockFocusController.h:26
DockAreaTitleBar.h
Declaration of CDockAreaTitleBar class.
ADS_PRINT
#define ADS_PRINT(s)
Definition: ads_globals.h:60
ads::CDockWidget
Definition: DockWidget.h:54
ads::CDockAreaWidget::currentDockWidget
CDockWidget * currentDockWidget() const
Definition: DockAreaWidget.cpp:554
DockAreaWidget.h
Declaration of CDockAreaWidget class.
ads::DockFocusControllerPrivate::FocusedDockWidget
QPointer< CDockWidget > FocusedDockWidget
Definition: DockFocusController.cpp:40
d
d
ads::CDockFocusController::onFocusedDockAreaViewToggled
void onFocusedDockAreaViewToggled(bool Open)
Definition: DockFocusController.cpp:304
ads::CDockWidget::features
DockWidgetFeatures features() const
Definition: DockWidget.cpp:359
ads::CDockAreaWidget::dockContainer
CDockContainerWidget * dockContainer() const
Definition: DockAreaWidget.cpp:403
DockManager.h
Declaration of CDockManager class.
ads::CDockFocusController::notifyWidgetOrAreaRelocation
void notifyWidgetOrAreaRelocation(QWidget *RelocatedWidget)
Definition: DockFocusController.cpp:328
DockWidgetTab.h
Declaration of CDockWidgetTab class.
ads::CDockAreaWidget::setCurrentDockWidget
void setCurrentDockWidget(CDockWidget *DockWidget)
Definition: DockAreaWidget.cpp:567
ads::CDockManager::isRestoringState
bool isRestoringState() const
Definition: DockManager.cpp:1007
ads::CDockFocusController::d
DockFocusControllerPrivate * d
private data (pimpl)
Definition: DockFocusController.h:30
ads::CFloatingDockContainer
Definition: FloatingDockContainer.h:104
ads::DockFocusControllerPrivate
Definition: DockFocusController.cpp:37
ads::CDockFocusController::~CDockFocusController
virtual ~CDockFocusController()
Definition: DockFocusController.cpp:213
ads::CDockFocusController::onApplicationFocusChanged
void onApplicationFocusChanged(QWidget *old, QWidget *now)
Definition: DockFocusController.cpp:220
ads::CDockManager::AllTabsHaveCloseButton
@ AllTabsHaveCloseButton
If enabled the tab close buttons will be QToolButtons instead of QPushButtons - disabled by default.
Definition: DockManager.h:166
ads::CDockFocusController::focusedDockWidget
CDockWidget * focusedDockWidget() const
Definition: DockFocusController.cpp:390
ads::internal::repolishStyle
void repolishStyle(QWidget *w, eRepolishChildOptions Options=RepolishIgnoreChildren)
Definition: ads_globals.cpp:367
ads::CDockManager::testConfigFlag
static bool testConfigFlag(eConfigFlag Flag)
Definition: DockManager.cpp:1041
ads::CDockFocusController::Super
QObject Super
Definition: DockFocusController.h:40
ads::DockFocusControllerPrivate::OldFocusedDockWidget
CDockWidget * OldFocusedDockWidget
Definition: DockFocusController.cpp:42
ads::updateDockWidgetFocusStyle
static void updateDockWidgetFocusStyle(CDockWidget *DockWidget, bool Focused)
Definition: DockFocusController.cpp:64


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:44