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>
25 #include <q3simplerichtext.h>
27 #include <Q3ValueList>
30 #include <kapplication.h>
35 class Amarok::ToolTip::Manager
: public QObject
38 Manager( QObject
*parent
): QObject( parent
) { kapp
->installEventFilter( this ); }
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
)
49 case QEvent::KeyPress
:
50 case QEvent::KeyRelease
:
51 case QEvent::MouseButtonPress
:
52 case QEvent::MouseButtonRelease
:
53 //case QEvent::MouseMove:
60 case QEvent::FocusOut
:
61 if( !dynamic_cast<Amarok::ToolTip
*>( kapp
->widgetAt( QCursor::pos(), true ) ) )
62 Amarok::ToolTip::hideTips();
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
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();
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 );*/
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 )
119 s_tooltips[i]->showTip();
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
),
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 );
147 if( QToolTip::parentWidget() && !s_text.isEmpty() )
157 void Amarok::ToolTip::showTip()
159 m_timer
.start( 15000, true );
160 if( !isVisible() || sizeHint() != size() )
162 resize( sizeHint() );
171 void Amarok::ToolTip::hideTip()
176 //QToolTip::parentWidget()->update();
181 void Amarok::ToolTip::drawContents( QPainter
*painter
)
183 /*QPixmap buf( width(), height() );
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();
208 //Q3SimpleRichText text( s_text, QToolTip::parentWidget()->font() );
209 //text.setWidth( 500 ); //is this reasonable?
210 //return QSize( text.widthUsed() - 2, text.height() );
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();
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 ) );
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 ) );
242 #include "tooltip.moc"