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.
24 #include <KApplication>
27 #include <q3simplerichtext.h>
28 #include <Q3ValueList>
29 #include <QApplication>
37 class Amarok::ToolTip::Manager
: public QObject
40 Manager( QObject
*parent
): QObject( parent
) { kapp
->installEventFilter( this ); }
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
)
51 case QEvent::KeyPress
:
52 case QEvent::KeyRelease
:
53 case QEvent::MouseButtonPress
:
54 case QEvent::MouseButtonRelease
:
55 //case QEvent::MouseMove:
62 case QEvent::FocusOut
:
63 if( !dynamic_cast<Amarok::ToolTip
*>( kapp
->widgetAt( QCursor::pos() ) ) )
64 Amarok::ToolTip::hideTips();
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
);
83 // s_manager = new Amarok::ToolTip::Manager( kapp );
84 //new ToolTip( client, parent );
87 void Amarok::ToolTip::remove( QWidget
*widget
) //static
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();
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 );*/
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 )
124 s_tooltips[i]->showTip();
130 Amarok::ToolTip::ToolTip( ToolTipClient
*client
, QWidget
*parent
)
131 : QFrame(0, 0, Qt::FramelessWindowHint
| Qt::Tool
| Qt::WindowStaysOnTopHint
132 | Qt::X11BypassWindowManagerHint
),
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 );
153 if( QToolTip::parentWidget() && !s_text.isEmpty() )
163 void Amarok::ToolTip::showTip()
165 m_timer
.start( 15000, true );
166 if( !isVisible() || sizeHint() != size() )
168 resize( sizeHint() );
177 void Amarok::ToolTip::hideTip()
182 //QToolTip::parentWidget()->update();
187 void Amarok::ToolTip::drawContents( QPainter
*painter
)
189 /*QPixmap buf( width(), height() );
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();
214 //Q3SimpleRichText text( s_text, QToolTip::parentWidget()->font() );
215 //text.setWidth( 500 ); //is this reasonable?
216 //return QSize( text.widthUsed() - 2, text.height() );
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();
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 ) );
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 ) );
248 #include "tooltip.moc"