1
//////////////////////////////////////////////////////////////////////////////
2
// oxygenwidgetexplorer.cpp
3
// print widget's and parent's information on mouse click
6
// Copyright (c) 2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
8
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
// of this software and associated documentation files (the "Software"), to
10
// deal in the Software without restriction, including without limitation the
11
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
// sell copies of the Software, and to permit persons to whom the Software is
13
// furnished to do so, subject to the following conditions:
15
// The above copyright notice and this permission notice shall be included in
16
// all copies or substantial portions of the Software.
18
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25
//////////////////////////////////////////////////////////////////////////////
27
#include "oxygenwidgetexplorer.h"
28
#include "oxygenwidgetexplorer.moc"
30
#include <QtCore/QTextStream>
31
#include <QtGui/QApplication>
32
#include <QtGui/QMouseEvent>
33
#include <QtGui/QPainter>
38
//________________________________________________
39
WidgetExplorer::WidgetExplorer( QObject* parent ):
42
_drawWidgetRects( false )
45
_eventTypes.insert( QEvent::Enter, "Enter" );
46
_eventTypes.insert( QEvent::Leave, "Leave" );
48
_eventTypes.insert( QEvent::HoverMove, "HoverMove" );
49
_eventTypes.insert( QEvent::HoverEnter, "HoverEnter" );
50
_eventTypes.insert( QEvent::HoverLeave, "HoverLeave" );
52
_eventTypes.insert( QEvent::MouseMove, "MouseMove" );
53
_eventTypes.insert( QEvent::MouseButtonPress, "MouseButtonPress" );
54
_eventTypes.insert( QEvent::MouseButtonRelease, "MouseButtonRelease" );
56
_eventTypes.insert( QEvent::FocusIn, "FocusIn" );
57
_eventTypes.insert( QEvent::FocusOut, "FocusOut" );
59
// _eventTypes.insert( QEvent::Paint, "Paint" );
63
//________________________________________________
64
void WidgetExplorer::setEnabled( bool value )
66
if( value == _enabled ) return;
69
qApp->removeEventFilter( this );
70
if( _enabled ) qApp->installEventFilter( this );
73
//________________________________________________
74
bool WidgetExplorer::eventFilter( QObject* object, QEvent* event )
77
if( object->isWidgetType() )
79
QString type( _eventTypes[event->type()] );
82
QTextStream( stdout ) << "Oxygen::WidgetExplorer::eventFilter - widget: " << object << " (" << object->metaObject()->className() << ")";
83
QTextStream( stdout ) << " type: " << type << endl;
87
switch( event->type() )
90
if( _drawWidgetRects )
92
QWidget* widget( qobject_cast<QWidget*>( object ) );
93
if( !widget ) return false;
95
QPainter painter( widget );
96
painter.setRenderHints(QPainter::Antialiasing);
97
painter.setBrush( Qt::NoBrush );
98
painter.setPen( Qt::red );
99
painter.drawRect( widget->rect() );
104
case QEvent::MouseButtonPress:
107
// cast event and check button
108
QMouseEvent* mouseEvent( static_cast<QMouseEvent*>( event ) );
109
if( mouseEvent->button() != Qt::LeftButton ) break;
111
// case widget and check (should not be necessary)
112
QWidget* widget( qobject_cast<QWidget*>(object) );
113
if( !widget ) return false;
115
QTextStream( stdout )
116
<< "Oxygen::WidgetExplorer::eventFilter -"
117
<< " event: " << event << " type: " << eventType( event->type() )
118
<< " widget: " << widgetInformation( widget )
121
// print parent information
122
QWidget* parent( widget->parentWidget() );
125
QTextStream( stdout ) << " parent: " << widgetInformation( parent ) << endl;
126
parent = parent->parentWidget();
128
QTextStream( stdout ) << "" << endl;
137
// always return false to go on with normal chain
142
//________________________________________________
143
QString WidgetExplorer::eventType( const QEvent::Type& type ) const
147
case QEvent::MouseButtonPress: return "MouseButtonPress";
148
case QEvent::MouseButtonRelease: return "MouseButtonRelease";
149
case QEvent::MouseMove: return "MouseMove";
150
default: return "Unknown";
154
//________________________________________________
155
QString WidgetExplorer::widgetInformation( const QWidget* widget ) const
158
QRect r( widget->geometry() );
159
QString className( widget->metaObject()->className() );
161
QTextStream( &out ) << widget << " (" << className << ")"
162
<< " position: " << r.x() << "," << r.y()
163
<< " size: " << r.width() << "," << r.height()
164
<< " hover: " << widget->testAttribute( Qt::WA_Hover );