Akonadi Contacts

waitingoverlay.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  SPDX-FileCopyrightText: 2008 Volker Krause <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "waitingoverlay_p.h"
10 
11 #include <KJob>
12 #include <KLocalizedString>
13 
14 #include <QBoxLayout>
15 #include <QEvent>
16 #include <QLabel>
17 #include <QPalette>
18 
19 //@cond PRIVATE
20 
21 WaitingOverlay::WaitingOverlay(KJob *job, QWidget *baseWidget, QWidget *parent)
22  : QWidget(parent ? parent : baseWidget->window())
23  , mBaseWidget(baseWidget)
24 {
25  Q_ASSERT(baseWidget);
26  Q_ASSERT(parentWidget() != baseWidget);
27 
28  connect(baseWidget, &QObject::destroyed, this, &QObject::deleteLater);
29  connect(job, &KJob::result, this, &QObject::deleteLater);
30  mPreviousState = mBaseWidget->isEnabled();
31 
32  QBoxLayout *topLayout = new QVBoxLayout(this);
33  topLayout->addStretch();
34  auto description = new QLabel(this);
35  description->setText(i18n("<p style=\"color: white;\"><b>Waiting for operation</b><br/></p>"));
36  description->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
37  topLayout->addWidget(description);
38  topLayout->addStretch();
39 
40  QPalette p = palette();
41  p.setColor(backgroundRole(), QColor(0, 0, 0, 128));
42  setPalette(p);
43  setAutoFillBackground(true);
44 
45  mBaseWidget->installEventFilter(this);
46 
47  reposition();
48 }
49 
50 WaitingOverlay::~WaitingOverlay()
51 {
52  if (mBaseWidget) {
53  mBaseWidget->setEnabled(mPreviousState);
54  }
55 }
56 
57 void WaitingOverlay::reposition()
58 {
59  if (!mBaseWidget) {
60  return;
61  }
62 
63  // reparent to the current top level widget of the base widget if needed
64  // needed eg. in dock widgets
65  if (parentWidget() != mBaseWidget->window()) {
66  setParent(mBaseWidget->window());
67  }
68 
69  // follow base widget visibility
70  // needed eg. in tab widgets
71  if (!mBaseWidget->isVisible()) {
72  hide();
73  return;
74  }
75  show();
76 
77  // follow position changes
78  const QPoint topLevelPos = mBaseWidget->mapTo(window(), QPoint(0, 0));
79  const QPoint parentPos = parentWidget()->mapFrom(window(), topLevelPos);
80  move(parentPos);
81 
82  // follow size changes
83  // TODO: hide/scale icon if we don't have enough space
84  resize(mBaseWidget->size());
85 }
86 
87 bool WaitingOverlay::eventFilter(QObject *object, QEvent *event)
88 {
89  if (object == mBaseWidget
90  && (event->type() == QEvent::Move || event->type() == QEvent::Resize || event->type() == QEvent::Show || event->type() == QEvent::Hide
91  || event->type() == QEvent::ParentChange)) {
92  reposition();
93  }
94  return QWidget::eventFilter(object, event);
95 }
96 
97 //@endcond
QEvent::Type type() const const
KJOBWIDGETS_EXPORT QWidget * window(KJob *job)
void setColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QColor &color)
AlignHCenter
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
virtual bool eventFilter(QObject *watched, QEvent *event)
void deleteLater()
QString i18n(const char *text, const TYPE &arg...)
void addStretch(int stretch)
void result(KJob *job)
KIOCORE_EXPORT CopyJob * move(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
void destroyed(QObject *obj)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.