Skip to content

Commit

Permalink
gui / popup and overlay: update on parent resize
Browse files Browse the repository at this point in the history
- added function to automatically center on parent widget

Signed-off-by: Andrei Popa <[email protected]>
  • Loading branch information
andrei47w committed Nov 28, 2024
1 parent af4fe93 commit 790bc48
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/src/licenseoverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ LicenseOverlay::LicenseOverlay(QWidget *parent)
m_popupWidget->enableTitleBar(false);
m_popupWidget->enableTintedOverlay(true);
m_popupWidget->setDescription(getLicense());
m_popupWidget->enableCenterOnParent(true);

connect(m_popupWidget->getContinueBtn(), &QAbstractButton::clicked, [&]() {
Preferences::GetInstance()->set("general_first_run", false);
Expand All @@ -55,7 +56,6 @@ void LicenseOverlay::showOverlay()
{
raise();
show();
m_popupWidget->move(parent->rect().center() - m_popupWidget->rect().center());
}

QPushButton *LicenseOverlay::getContinueBtn() { return m_popupWidget->getContinueBtn(); }
Expand Down
1 change: 1 addition & 0 deletions gui/include/gui/tintedoverlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SCOPY_GUI_EXPORT TintedOverlay : public QWidget
const QList<QWidget *> &getHoles() const;
void setHoles(const QList<QWidget *> &newHoles);
void clearHoles();
bool eventFilter(QObject *watched, QEvent *event) override;

protected:
void paintEvent(QPaintEvent *ev) override;
Expand Down
3 changes: 3 additions & 0 deletions gui/include/gui/widgets/popupwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class SCOPY_GUI_EXPORT PopupWidget : public QWidget
void enableTitleBar(bool enable = true);
void enableTintedOverlay(bool enable = true);
void setEnableExternalLinks(bool enable = true);
void enableCenterOnParent(bool enable = true);

QString getDescription();
void setDescription(const QString &description);
Expand All @@ -58,6 +59,8 @@ class SCOPY_GUI_EXPORT PopupWidget : public QWidget

void enableCloseButton(bool en);

bool eventFilter(QObject *watched, QEvent *event) override;

Q_SIGNALS:
void continueButtonClicked();
void exitButtonClicked();
Expand Down
14 changes: 12 additions & 2 deletions gui/src/tintedoverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ TintedOverlay::TintedOverlay(QWidget *parent, QColor color)
this->parent = parent;
holes.clear();

QRect geo = parent->rect();
setGeometry(geo);
setGeometry(parent->rect());
setAttribute(Qt::WA_TransparentForMouseEvents, false);

parent->installEventFilter(this);
}

TintedOverlay::~TintedOverlay() {}
Expand Down Expand Up @@ -65,6 +66,15 @@ void TintedOverlay::paintEvent(QPaintEvent *ev)
painter.fillRect(rect(), color);
}

bool TintedOverlay::eventFilter(QObject *watched, QEvent *event)
{
if(event->type() == QEvent::Resize) {
setGeometry(parent->rect());
}

return QObject::eventFilter(watched, event);
}

void TintedOverlay::mousePressEvent(QMouseEvent *ev)
{
// prevent clicks from being sent to parents
Expand Down
19 changes: 19 additions & 0 deletions gui/src/widgets/popupwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,23 @@ void PopupWidget::setEnableExternalLinks(bool enable)
m_descriptionTextBrowser->setProperty("openExternalLinks", enable);
}

bool PopupWidget::eventFilter(QObject *watched, QEvent *event)
{
if(event->type() == QEvent::Resize) {
move(parentWidget()->rect().center() - rect().center());
}

return QObject::eventFilter(watched, event);
}

void PopupWidget::enableCenterOnParent(bool enable)
{
if(enable) {
move(parentWidget()->rect().center() - rect().center());
parentWidget()->installEventFilter(this);
} else {
parentWidget()->removeEventFilter(this);
}
}

#include "moc_popupwidget.cpp"
2 changes: 1 addition & 1 deletion gui/src/widgets/toolbuttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ bool InfoBtn::hasTutorial() { return m_hasTutorial; }
void InfoBtn::generateInfoPopup(QWidget *parent)
{
m_popupWidget = new PopupWidget(parent);
m_popupWidget->move(parent->rect().center() - m_popupWidget->rect().center());
m_popupWidget->enableCenterOnParent(true);
m_popupWidget->setTitle("Plugin Information");
m_popupWidget->setDescription(
"To learn more about this plugin, check out the tutorial or read the online documentation.");
Expand Down

0 comments on commit 790bc48

Please sign in to comment.