KTutorial  0.5.1
WindowVisibilitySpy.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00003  *   Copyright (C) 2012 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; If not, see <http://www.gnu.org/licenses/>.  *
00017  ***************************************************************************/
00018 
00019 #include "WindowVisibilitySpy.h"
00020 
00021 #include <QEvent>
00022 #include <QWidget>
00023 
00024 namespace ktutorial {
00025 namespace common {
00026 
00027 //public:
00028 
00029 WindowVisibilitySpy::WindowVisibilitySpy(QObject* parent /*= 0*/):
00030         QObject(parent) {
00031 }
00032 
00033 void WindowVisibilitySpy::addWidgetToSpy(QWidget* widget) {
00034     Q_ASSERT(widget);
00035 
00036     widget->installEventFilter(this);
00037 
00038     foreach (QObject* childObject, widget->children()) {
00039         if (childObject->isWidgetType()) {
00040             addWidgetToSpy(static_cast<QWidget*>(childObject));
00041         }
00042     }
00043 }
00044 
00045 //protected:
00046 
00047 bool WindowVisibilitySpy::eventFilter(QObject* object, QEvent* event) {
00048     if (event->type() == QEvent::ChildAdded) {
00049         QChildEvent* childEvent = static_cast<QChildEvent*>(event);
00050         if (childEvent->child()->isWidgetType()) {
00051             addWidgetToSpy(static_cast<QWidget*>(childEvent->child()));
00052         }
00053         return false;
00054     }
00055 
00056     QWidget* widget = static_cast<QWidget*>(object);
00057 
00058     if (!(widget->windowFlags() & (Qt::Window | Qt::Dialog))) {
00059         return false;
00060     }
00061 
00062     if (event->type() == QEvent::Show) {
00063         emit windowShown(widget);
00064     } else if (event->type() == QEvent::Hide) {
00065         emit windowHidden(widget);
00066     }
00067 
00068     return false;
00069 }
00070 
00071 }
00072 }