Update ToolWindow to auto-hide itself when main window loses focus

This commit is contained in:
Ryan Huffman 2014-11-18 12:50:37 -08:00
parent ad3d7fbfb4
commit 2bfd3d213a
2 changed files with 40 additions and 2 deletions

View file

@ -19,6 +19,8 @@ ToolWindow::ToolWindow(QWidget* parent) :
QMainWindow(parent),
_hasShown(false),
_lastGeometry() {
Application::getInstance()->installEventFilter(this);
}
bool ToolWindow::event(QEvent* event) {
@ -47,8 +49,37 @@ bool ToolWindow::event(QEvent* event) {
return QMainWindow::event(event);
}
bool ToolWindow::eventFilter(QObject* sender, QEvent* event) {
switch (event->type()) {
case QEvent::WindowStateChange:
if (Application::getInstance()->getWindow()->isMinimized()) {
// If we are already visible, we are self-hiding
_selfHidden = isVisible();
setVisible(false);
} else {
if (_selfHidden) {
setVisible(true);
}
}
break;
case QEvent::ApplicationDeactivate:
_selfHidden = isVisible();
setVisible(false);
break;
case QEvent::ApplicationActivate:
if (_selfHidden) {
setVisible(true);
}
break;
default:
break;
}
return false;
}
void ToolWindow::onChildVisibilityUpdated(bool visible) {
if (visible) {
if (!_selfHidden && visible) {
setVisible(true);
} else {
bool hasVisible = false;
@ -59,7 +90,10 @@ void ToolWindow::onChildVisibilityUpdated(bool visible) {
break;
}
}
setVisible(hasVisible);
// If a child was hidden and we don't have any children still visible, hide ourself.
if (!hasVisible) {
setVisible(false);
}
}
}

View file

@ -28,11 +28,15 @@ public:
virtual void addDockWidget(Qt::DockWidgetArea area, QDockWidget* dockWidget, Qt::Orientation orientation);
virtual void removeDockWidget(QDockWidget* dockWidget);
virtual bool eventFilter(QObject* sender, QEvent* event);
public slots:
void onChildVisibilityUpdated(bool visible);
private:
// Indicates whether this window was hidden by itself (because the main window lost focus).
bool _selfHidden;
bool _hasShown;
QRect _lastGeometry;
};