~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kstyles/oxygen/debug/oxygenwidgetexplorer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////////
 
2
// oxygenwidgetexplorer.cpp
 
3
// print widget's and parent's information on mouse click
 
4
// -------------------
 
5
//
 
6
// Copyright (c) 2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
 
7
//
 
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:
 
14
//
 
15
// The above copyright notice and this permission notice shall be included in
 
16
// all copies or substantial portions of the Software.
 
17
//
 
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
 
24
// IN THE SOFTWARE.
 
25
//////////////////////////////////////////////////////////////////////////////
 
26
 
 
27
#include "oxygenwidgetexplorer.h"
 
28
#include "oxygenwidgetexplorer.moc"
 
29
 
 
30
#include <QtCore/QTextStream>
 
31
#include <QtGui/QApplication>
 
32
#include <QtGui/QMouseEvent>
 
33
#include <QtGui/QPainter>
 
34
 
 
35
namespace Oxygen
 
36
{
 
37
 
 
38
    //________________________________________________
 
39
    WidgetExplorer::WidgetExplorer( QObject* parent ):
 
40
        QObject( parent ),
 
41
        _enabled( false ),
 
42
        _drawWidgetRects( false )
 
43
    {
 
44
 
 
45
        _eventTypes.insert( QEvent::Enter, "Enter" );
 
46
        _eventTypes.insert( QEvent::Leave, "Leave" );
 
47
 
 
48
        _eventTypes.insert( QEvent::HoverMove, "HoverMove" );
 
49
        _eventTypes.insert( QEvent::HoverEnter, "HoverEnter" );
 
50
        _eventTypes.insert( QEvent::HoverLeave, "HoverLeave" );
 
51
 
 
52
        _eventTypes.insert( QEvent::MouseMove, "MouseMove" );
 
53
        _eventTypes.insert( QEvent::MouseButtonPress, "MouseButtonPress" );
 
54
        _eventTypes.insert( QEvent::MouseButtonRelease, "MouseButtonRelease" );
 
55
 
 
56
        _eventTypes.insert( QEvent::FocusIn, "FocusIn" );
 
57
        _eventTypes.insert( QEvent::FocusOut, "FocusOut" );
 
58
 
 
59
        // _eventTypes.insert( QEvent::Paint, "Paint" );
 
60
 
 
61
    }
 
62
 
 
63
    //________________________________________________
 
64
    void WidgetExplorer::setEnabled( bool value )
 
65
    {
 
66
        if( value == _enabled ) return;
 
67
        _enabled = value;
 
68
 
 
69
        qApp->removeEventFilter( this );
 
70
        if( _enabled )  qApp->installEventFilter( this );
 
71
    }
 
72
 
 
73
    //________________________________________________
 
74
    bool WidgetExplorer::eventFilter( QObject* object, QEvent* event )
 
75
    {
 
76
 
 
77
        if( object->isWidgetType() )
 
78
        {
 
79
            QString type( _eventTypes[event->type()] );
 
80
            if( !type.isEmpty() )
 
81
            {
 
82
                QTextStream( stdout ) << "Oxygen::WidgetExplorer::eventFilter - widget: " << object << " (" << object->metaObject()->className() << ")";
 
83
                QTextStream( stdout ) << " type: " << type  << endl;
 
84
            }
 
85
        }
 
86
 
 
87
        switch( event->type() )
 
88
        {
 
89
            case QEvent::Paint:
 
90
            if( _drawWidgetRects )
 
91
            {
 
92
                QWidget* widget( qobject_cast<QWidget*>( object ) );
 
93
                if( !widget ) return false;
 
94
 
 
95
                QPainter painter( widget );
 
96
                painter.setRenderHints(QPainter::Antialiasing);
 
97
                painter.setBrush( Qt::NoBrush );
 
98
                painter.setPen( Qt::red );
 
99
                painter.drawRect( widget->rect() );
 
100
                painter.end();
 
101
            }
 
102
            break;
 
103
 
 
104
            case QEvent::MouseButtonPress:
 
105
            {
 
106
 
 
107
                // cast event and check button
 
108
                QMouseEvent* mouseEvent( static_cast<QMouseEvent*>( event ) );
 
109
                if( mouseEvent->button() != Qt::LeftButton ) break;
 
110
 
 
111
                // case widget and check (should not be necessary)
 
112
                QWidget* widget( qobject_cast<QWidget*>(object) );
 
113
                if( !widget ) return false;
 
114
 
 
115
                QTextStream( stdout )
 
116
                    << "Oxygen::WidgetExplorer::eventFilter -"
 
117
                    << " event: " << event << " type: " << eventType( event->type() )
 
118
                    << " widget: " << widgetInformation( widget )
 
119
                    << endl;
 
120
 
 
121
                // print parent information
 
122
                QWidget* parent( widget->parentWidget() );
 
123
                while( parent )
 
124
                {
 
125
                    QTextStream( stdout ) << "    parent: " << widgetInformation( parent ) << endl;
 
126
                    parent = parent->parentWidget();
 
127
                }
 
128
                QTextStream( stdout ) << "" << endl;
 
129
 
 
130
            }
 
131
            break;
 
132
 
 
133
            default: break;
 
134
 
 
135
        }
 
136
 
 
137
        // always return false to go on with normal chain
 
138
        return false;
 
139
 
 
140
    }
 
141
 
 
142
    //________________________________________________
 
143
    QString WidgetExplorer::eventType( const QEvent::Type& type ) const
 
144
    {
 
145
        switch( type )
 
146
        {
 
147
            case QEvent::MouseButtonPress: return "MouseButtonPress";
 
148
            case QEvent::MouseButtonRelease: return "MouseButtonRelease";
 
149
            case QEvent::MouseMove: return "MouseMove";
 
150
            default: return "Unknown";
 
151
        }
 
152
    }
 
153
 
 
154
    //________________________________________________
 
155
    QString WidgetExplorer::widgetInformation( const QWidget* widget ) const
 
156
    {
 
157
 
 
158
        QRect r( widget->geometry() );
 
159
        QString className( widget->metaObject()->className() );
 
160
        QString out;
 
161
        QTextStream( &out ) << widget << " (" << className << ")"
 
162
            << " position: " << r.x() << "," << r.y()
 
163
            << " size: " << r.width() << "," << r.height()
 
164
            << " hover: " << widget->testAttribute( Qt::WA_Hover );
 
165
        return out;
 
166
    }
 
167
 
 
168
}