Header cleanup
[amarok.git] / src / tooltip.cpp
blobee27d464ef28f8711a30be89ad68b9d31ac925a9
1 /*
2 Copyright (c) 2006 Gábor Lehel <illissius@gmail.com>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include <QApplication>
21 #include <QCursor>
22 #include <QFrame>
23 #include <QPainter>
24 #include <QPixmap>
25 #include <q3simplerichtext.h>
26 //Added by qt3to4:
27 #include <Q3ValueList>
28 #include <QEvent>
29 #include <kglobal.h>
30 #include <kapplication.h>
32 #include "debug.h"
33 #include "tooltip.h"
35 class Amarok::ToolTip::Manager: public QObject
37 public:
38 Manager( QObject *parent ): QObject( parent ) { kapp->installEventFilter( this ); }
39 virtual ~Manager()
41 for( int n = Amarok::ToolTip::s_tooltips.count() - 1; n >= 0; --n )
42 delete Amarok::ToolTip::s_tooltips[n];
45 bool eventFilter( QObject*, QEvent *e )
47 switch ( e->type() )
49 case QEvent::KeyPress:
50 case QEvent::KeyRelease:
51 case QEvent::MouseButtonPress:
52 case QEvent::MouseButtonRelease:
53 //case QEvent::MouseMove:
54 case QEvent::Wheel:
55 ToolTip::hideTips();
56 break;
57 case QEvent::FocusIn:
58 case QEvent::Enter:
59 case QEvent::Leave:
60 case QEvent::FocusOut:
61 if( !dynamic_cast<Amarok::ToolTip*>( kapp->widgetAt( QCursor::pos(), true ) ) )
62 Amarok::ToolTip::hideTips();
63 default: break;
66 return false;
70 Amarok::ToolTip::Manager* Amarok::ToolTip::s_manager = 0;
71 QPoint Amarok::ToolTip::s_pos;
72 QRect Amarok::ToolTip::s_rect;
73 QString Amarok::ToolTip::s_text;
74 Q3ValueList<Amarok::ToolTip*> Amarok::ToolTip::s_tooltips;
75 int Amarok::ToolTip::s_hack = 0;
77 void Amarok::ToolTip::add( ToolTipClient *client, QWidget *parent ) //static
79 //if( !s_manager )
80 // s_manager = new Amarok::ToolTip::Manager( kapp );
81 //new ToolTip( client, parent );
84 void Amarok::ToolTip::remove( QWidget *widget ) //static
86 //for( int i = s_tooltips.count() - 1; i >= 0; --i )
87 // if( s_tooltips[i]->QToolTip::parentWidget() == widget )
88 // delete s_tooltips[i];
91 void Amarok::ToolTip::hideTips() //static
93 //for( int i = 0, n = s_tooltips.count(); i < n; ++i )
94 // s_tooltips[i]->hideTip();
95 //QToolTip::hide();
98 QString Amarok::ToolTip::textFor( QWidget *widget, const QPoint &pos ) //static
100 /*for( int i = 0, n = s_tooltips.count(); i < n; ++i )
101 if( s_tooltips[i]->QToolTip::parentWidget() == widget )
102 return s_tooltips[i]->m_client->toolTipText( widget, pos ).first;
103 return QToolTip::textFor( widget, pos );*/
104 return QString();
107 void Amarok::ToolTip::updateTip() //static
109 for( int i = 0, n = s_tooltips.count(); i < n; ++i )
110 if( s_tooltips[i]->isVisible() )
112 /*QWidget* const w = s_tooltips[i]->QToolTip::parentWidget();
113 QPair<QString, QRect> p = s_tooltips[i]->m_client->toolTipText( w, w->mapFromGlobal( s_pos ) );
114 QString prev = s_text;
115 if( prev != p.first )
117 s_text = p.first;
118 s_rect = p.second;
119 s_tooltips[i]->showTip();
121 break;
125 Amarok::ToolTip::ToolTip( ToolTipClient *client, QWidget *parent )
126 : QFrame(0, 0, Qt::WStyle_Customize | Qt::WStyle_NoBorder | Qt::WStyle_Tool | Qt::WStyle_StaysOnTop | Qt::WX11BypassWM | Qt::WNoAutoErase ),
127 // QToolTip( ),
128 m_client( client )
130 s_tooltips.append( this );
131 QFrame::setPalette( QToolTip::palette() );
132 connect( &m_timer, SIGNAL( timeout() ), this, SLOT( hideTip() ) );
135 Amarok::ToolTip::~ToolTip()
137 s_tooltips.remove( this );
140 void Amarok::ToolTip::maybeTip( const QPoint &pos )
142 /*s_pos = QToolTip::parentWidget()->mapToGlobal( pos );
143 QString prev = s_text;
144 QPair<QString, QRect> p = m_client->toolTipText( QToolTip::parentWidget(), pos );
145 s_text = p.first;
146 s_rect = p.second;
147 if( QToolTip::parentWidget() && !s_text.isEmpty() )
149 if( s_text != prev )
150 hideTips();
151 showTip();
153 else
154 hideTips();*/
157 void Amarok::ToolTip::showTip()
159 m_timer.start( 15000, true );
160 if( !isVisible() || sizeHint() != size() )
162 resize( sizeHint() );
163 position();
165 if( !isVisible() )
166 show();
167 else
168 update();
171 void Amarok::ToolTip::hideTip()
173 if( !isVisible() )
174 return;
175 QFrame::hide();
176 //QToolTip::parentWidget()->update();
177 m_timer.stop();
178 s_hack = 0;
181 void Amarok::ToolTip::drawContents( QPainter *painter )
183 /*QPixmap buf( width(), height() );
184 QPainter p( &buf );
185 buf.fill( colorGroup().background() );
187 p.setPen( colorGroup().foreground() );
188 p.drawRect( buf.rect() );
190 Q3SimpleRichText text( s_text, QToolTip::parentWidget()->font() );
191 text.setWidth( width() );
192 p.translate( 0, height() / 2 - text.height() / 2);
193 QPoint pos = s_rect.isNull() ? QPoint(2, -1)
194 : s_hack == 1 ? QPoint(4, -2)
195 : QPoint(2, -2); //HACK positioning
196 p.setFont( QToolTip::parentWidget()->font() );
197 text.draw( &p, pos.x(), pos.y(), rect(), colorGroup() );
199 painter->drawPixmap( 0, 0, buf );*/
202 QSize Amarok::ToolTip::sizeHint() const
204 if( !s_rect.isNull() )
205 return s_rect.size();
206 else
208 //Q3SimpleRichText text( s_text, QToolTip::parentWidget()->font() );
209 //text.setWidth( 500 ); //is this reasonable?
210 //return QSize( text.widthUsed() - 2, text.height() );
211 return QSize(0,0);
215 void Amarok::ToolTip::position()
217 /*const QRect drect = QApplication::desktop()->availableGeometry( QToolTip::parentWidget() );
218 const QSize size = sizeHint();
219 const int width = size.width(), height = size.height();
220 QPoint pos;
221 if( !s_rect.isNull() )
223 pos = s_rect.topLeft();
224 if( pos.y() + height > drect.bottom() )
225 pos.setY( qMax( drect.top(), drect.bottom() - height ) );
226 if( pos.x() + width > drect.right() )
227 pos.setX( qMax( drect.left(), drect.right() - width ) );
229 else
231 const QRect r = QRect( QToolTip::parentWidget()->mapToGlobal( QToolTip::parentWidget()->pos() ), QToolTip::parentWidget()->size() );
232 pos = r.bottomRight();
233 if( pos.y() + height > drect.bottom() )
234 pos.setY( qMax( drect.top(), r.top() - height ) );
235 if( pos.x() + width > drect.right() )
236 pos.setX( qMax( drect.left(), r.left() - width ) );
239 move( pos );*/
242 #include "tooltip.moc"