mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-22 22:53:55 -07:00
[TabDeckEditor] Generalize visibility filter and extract it to a separate file (#6526)
* create class * use new class in old code
This commit is contained in:
parent
1b71519ec6
commit
84483c56d7
5 changed files with 67 additions and 35 deletions
|
|
@ -0,0 +1,26 @@
|
|||
#include "visibility_change_listener.h"
|
||||
|
||||
#include <QEvent>
|
||||
#include <QWidget>
|
||||
|
||||
VisibilityChangeListener::VisibilityChangeListener(QWidget *targetWidget)
|
||||
: QObject(targetWidget), targetWidget(targetWidget)
|
||||
{
|
||||
if (targetWidget) {
|
||||
targetWidget->installEventFilter(this);
|
||||
}
|
||||
}
|
||||
|
||||
bool VisibilityChangeListener::eventFilter(QObject *o, QEvent *e)
|
||||
{
|
||||
if (o == targetWidget && !e->spontaneous()) {
|
||||
if (e->type() == QEvent::Show) {
|
||||
emit visibilityChanged(true);
|
||||
}
|
||||
|
||||
if (e->type() == QEvent::Hide) {
|
||||
emit visibilityChanged(false);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef COCKATRICE_VISIBILITY_LISTENER_H
|
||||
#define COCKATRICE_VISIBILITY_LISTENER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
/**
|
||||
* @brief This filter listens to the visibility changes of a target widget, emitting signals whenever the visibility of
|
||||
* that widget changes.
|
||||
*/
|
||||
class VisibilityChangeListener : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
QWidget *targetWidget;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Creates a new instance of this class, watching the targetWidget.
|
||||
* This class automatically installs itself as an eventFilter to the targetWidget.
|
||||
*
|
||||
* @param targetWidget The widget to watch. Sets that widget as this object's parent.
|
||||
*/
|
||||
explicit VisibilityChangeListener(QWidget *targetWidget);
|
||||
|
||||
bool eventFilter(QObject *o, QEvent *e) override;
|
||||
|
||||
signals:
|
||||
/**
|
||||
* Emitted whenever the target widget's visibility changes
|
||||
* @param visible The widget's new visibility
|
||||
*/
|
||||
void visibilityChanged(bool visible);
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_VISIBILITY_LISTENER_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue