Revert previous commit, was incorrect
[amarok.git] / src / tooltip.cpp
blob037d61b24dfe6cf0f2091e0e9df27a956f2db12e
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 "tooltip.h"
22 #include "debug.h"
24 #include <KApplication>
25 #include <KGlobal>
27 #include <q3simplerichtext.h>
28 #include <Q3ValueList>
29 #include <QApplication>
30 #include <QCursor>
31 #include <QEvent>
32 #include <QFrame>
33 #include <QPainter>
34 #include <QPixmap>
37 class Amarok::ToolTip::Manager: public QObject
39 public:
40 Manager( QObject *parent ): QObject( parent ) { kapp->installEventFilter( this ); }
41 virtual ~Manager()
43 for( int n = Amarok::ToolTip::s_tooltips.count() - 1; n >= 0; --n )
44 delete Amarok::ToolTip::s_tooltips[n];
47 bool eventFilter( QObject*, QEvent *e )
49 switch ( e->type() )
51 case QEvent::KeyPress:
52 case QEvent::KeyRelease:
53 case QEvent::MouseButtonPress:
54 case QEvent::MouseButtonRelease:
55 //case QEvent::MouseMove:
56 case QEvent::Wheel:
57 ToolTip::hideTips();
58 break;
59 case QEvent::FocusIn:
60 case QEvent::Enter:
61 case QEvent::Leave:
62 case QEvent::FocusOut:
63 if( !dynamic_cast<Amarok::ToolTip*>( kapp->widgetAt( QCursor::pos() ) ) )
64 Amarok::ToolTip::hideTips();
65 default: break;
68 return false;
72 Amarok::ToolTip::Manager* Amarok::ToolTip::s_manager = 0;
73 QPoint Amarok::ToolTip::s_pos;
74 QRect Amarok::ToolTip::s_rect;
75 QString Amarok::ToolTip::s_text;
76 Q3ValueList<Amarok::ToolTip*> Amarok::ToolTip::s_tooltips;
77 int Amarok::ToolTip::s_hack = 0;
79 void Amarok::ToolTip::add( ToolTipClient *client, QWidget *parent ) //static
81 Q_UNUSED( client ); Q_UNUSED( parent );
82 //if( !s_manager )
83 // s_manager = new Amarok::ToolTip::Manager( kapp );
84 //new ToolTip( client, parent );
87 void Amarok::ToolTip::remove( QWidget *widget ) //static
89 Q_UNUSED( widget );
90 //for( int i = s_tooltips.count() - 1; i >= 0; --i )
91 // if( s_tooltips[i]->QToolTip::parentWidget() == widget )
92 // delete s_tooltips[i];
95 void Amarok::ToolTip::hideTips() //static
97 //for( int i = 0, n = s_tooltips.count(); i < n; ++i )
98 // s_tooltips[i]->hideTip();
99 //QToolTip::hide();
102 QString Amarok::ToolTip::textFor( QWidget *widget, const QPoint &pos ) //static
104 Q_UNUSED( widget ); Q_UNUSED( pos );
105 /*for( int i = 0, n = s_tooltips.count(); i < n; ++i )
106 if( s_tooltips[i]->QToolTip::parentWidget() == widget )
107 return s_tooltips[i]->m_client->toolTipText( widget, pos ).first;
108 return QToolTip::textFor( widget, pos );*/
109 return QString();
112 void Amarok::ToolTip::updateTip() //static
114 for( int i = 0, n = s_tooltips.count(); i < n; ++i )
115 if( s_tooltips[i]->isVisible() )
117 /*QWidget* const w = s_tooltips[i]->QToolTip::parentWidget();
118 QPair<QString, QRect> p = s_tooltips[i]->m_client->toolTipText( w, w->mapFromGlobal( s_pos ) );
119 QString prev = s_text;
120 if( prev != p.first )
122 s_text = p.first;
123 s_rect = p.second;
124 s_tooltips[i]->showTip();
126 break;
130 Amarok::ToolTip::ToolTip( ToolTipClient *client, QWidget *parent )
131 : QFrame(0, 0, Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint
132 | Qt::X11BypassWindowManagerHint ),
133 // QToolTip( ),
134 m_client( client )
136 s_tooltips.append( this );
137 QFrame::setPalette( QToolTip::palette() );
138 connect( &m_timer, SIGNAL( timeout() ), this, SLOT( hideTip() ) );
141 Amarok::ToolTip::~ToolTip()
143 s_tooltips.remove( this );
146 void Amarok::ToolTip::maybeTip( const QPoint &pos )
148 /*s_pos = QToolTip::parentWidget()->mapToGlobal( pos );
149 QString prev = s_text;
150 QPair<QString, QRect> p = m_client->toolTipText( QToolTip::parentWidget(), pos );
151 s_text = p.first;
152 s_rect = p.second;
153 if( QToolTip::parentWidget() && !s_text.isEmpty() )
155 if( s_text != prev )
156 hideTips();
157 showTip();
159 else
160 hideTips();*/
163 void Amarok::ToolTip::showTip()
165 m_timer.start( 15000, true );
166 if( !isVisible() || sizeHint() != size() )
168 resize( sizeHint() );
169 position();
171 if( !isVisible() )
172 show();
173 else
174 update();
177 void Amarok::ToolTip::hideTip()
179 if( !isVisible() )
180 return;
181 QFrame::hide();
182 //QToolTip::parentWidget()->update();
183 m_timer.stop();
184 s_hack = 0;
187 void Amarok::ToolTip::drawContents( QPainter *painter )
189 /*QPixmap buf( width(), height() );
190 QPainter p( &buf );
191 buf.fill( colorGroup().background() );
193 p.setPen( colorGroup().foreground() );
194 p.drawRect( buf.rect() );
196 Q3SimpleRichText text( s_text, QToolTip::parentWidget()->font() );
197 text.setWidth( width() );
198 p.translate( 0, height() / 2 - text.height() / 2);
199 QPoint pos = s_rect.isNull() ? QPoint(2, -1)
200 : s_hack == 1 ? QPoint(4, -2)
201 : QPoint(2, -2); //HACK positioning
202 p.setFont( QToolTip::parentWidget()->font() );
203 text.draw( &p, pos.x(), pos.y(), rect(), colorGroup() );
205 painter->drawPixmap( 0, 0, buf );*/
208 QSize Amarok::ToolTip::sizeHint() const
210 if( !s_rect.isNull() )
211 return s_rect.size();
212 else
214 //Q3SimpleRichText text( s_text, QToolTip::parentWidget()->font() );
215 //text.setWidth( 500 ); //is this reasonable?
216 //return QSize( text.widthUsed() - 2, text.height() );
217 return QSize(0,0);
221 void Amarok::ToolTip::position()
223 /*const QRect drect = QApplication::desktop()->availableGeometry( QToolTip::parentWidget() );
224 const QSize size = sizeHint();
225 const int width = size.width(), height = size.height();
226 QPoint pos;
227 if( !s_rect.isNull() )
229 pos = s_rect.topLeft();
230 if( pos.y() + height > drect.bottom() )
231 pos.setY( qMax( drect.top(), drect.bottom() - height ) );
232 if( pos.x() + width > drect.right() )
233 pos.setX( qMax( drect.left(), drect.right() - width ) );
235 else
237 const QRect r = QRect( QToolTip::parentWidget()->mapToGlobal( QToolTip::parentWidget()->pos() ), QToolTip::parentWidget()->size() );
238 pos = r.bottomRight();
239 if( pos.y() + height > drect.bottom() )
240 pos.setY( qMax( drect.top(), r.top() - height ) );
241 if( pos.x() + width > drect.right() )
242 pos.setX( qMax( drect.left(), r.left() - width ) );
245 move( pos );*/
248 #include "tooltip.moc"