kontact Library API Documentation

knotetip.cpp

00001 /*
00002    This file is part of the KDE project
00003    Copyright (C) 2004 Michael Brade <brade@kde.org>
00004 
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; see the file COPYING.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 
00020    In addition, as a special exception, the copyright holders give
00021    permission to link the code of this program with any edition of
00022    the Qt library by Trolltech AS, Norway (or with modified versions
00023    of Qt that use the same license as Qt), and distribute linked
00024    combinations including the two.  You must obey the GNU General
00025    Public License in all respects for all of the code used other than
00026    Qt.  If you modify this file, you may extend this exception to
00027    your version of the file, but you are not obligated to do so.  If
00028    you do not wish to do so, delete this exception statement from
00029    your version.
00030 */
00031 
00032 #include <qtooltip.h>
00033 #include <qlayout.h>
00034 #include <qtextedit.h>
00035 
00036 #include <kapplication.h>
00037 #include <kglobalsettings.h>
00038 
00039 #include "knotetip.h"
00040 #include "knotes_part_p.h"
00041 
00042 
00043 KNoteTip::KNoteTip( KIconView *parent )
00044     : QFrame( 0, 0, WX11BypassWM |   // this will make Seli happy >:-P
00045               WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WStyle_StaysOnTop ),
00046       m_filter( false ),
00047       m_view( parent ),
00048       m_noteIVI( 0 ),
00049       m_preview( new QTextEdit( this ) )
00050 {
00051     m_preview->setReadOnly( true );
00052     m_preview->setHScrollBarMode( QScrollView::AlwaysOff );
00053     m_preview->setVScrollBarMode( QScrollView::AlwaysOff );
00054 
00055     QBoxLayout *layout = new QVBoxLayout( this );
00056     layout->addWidget( m_preview );
00057 
00058     setPalette( QToolTip::palette() );
00059     setMargin( 1 );
00060     setFrameStyle( QFrame::Plain | QFrame::Box );
00061     hide();
00062 }
00063 
00064 KNoteTip::~KNoteTip()
00065 {
00066     delete m_preview;
00067 }
00068 
00069 void KNoteTip::setNote( KNotesIconViewItem *item, TextFormat format )
00070 {
00071     if ( m_noteIVI == item && m_format == format )
00072         return;
00073 
00074     m_noteIVI = item;
00075     m_format = format;
00076 
00077     if ( !m_noteIVI )
00078     {
00079         killTimers();
00080         if ( isVisible() )
00081         {
00082             setFilter( false );
00083             hide();
00084         }
00085     }
00086     else
00087     {
00088         m_preview->setTextFormat( format );
00089         m_preview->setText( item->journal()->description() );
00090         m_preview->zoomTo( 6 );
00091         m_preview->sync();
00092 
00093         int w = 400;
00094         int h = m_preview->heightForWidth( w );
00095         while ( w > 60 && h == m_preview->heightForWidth( w - 20 ) )
00096             w -= 20;
00097 
00098         QRect desk = KGlobalSettings::desktopGeometry( m_noteIVI->rect().center() );
00099         resize( w, QMIN(h, desk.height()/2 - 20) );
00100 
00101         hide();
00102         killTimers();
00103         setFilter( true );
00104         startTimer( 700 );  // delay showing the tooltip for 0.7 sec
00105     }
00106 }
00107 
00108 
00109 // protected, virtual methods
00110 
00111 void KNoteTip::resizeEvent( QResizeEvent *ev )
00112 {
00113     QFrame::resizeEvent( ev );
00114     reposition();
00115 }
00116 
00117 void KNoteTip::timerEvent( QTimerEvent * )
00118 {
00119     killTimers();
00120     if ( !isVisible() )
00121     {
00122         startTimer( 15000 ); // show the tooltip for 15 sec
00123         reposition();
00124         show();
00125     }
00126     else
00127     {
00128         setFilter( false );
00129         hide();
00130     }
00131 }
00132 
00133 bool KNoteTip::eventFilter( QObject *, QEvent *e )
00134 {
00135     switch ( e->type() )
00136     {
00137     case QEvent::Leave:
00138     case QEvent::MouseButtonPress:
00139     case QEvent::MouseButtonRelease:
00140     case QEvent::KeyPress:
00141     case QEvent::KeyRelease:
00142     case QEvent::FocusIn:
00143     case QEvent::FocusOut:
00144     case QEvent::Wheel:
00145         killTimers();
00146         setFilter( false );
00147         hide();
00148     default:
00149         break;
00150     }
00151 
00152     return false;
00153 }
00154 
00155 
00156 // private stuff
00157 
00158 void KNoteTip::setFilter( bool enable )
00159 {
00160     if ( enable == m_filter )
00161         return;
00162 
00163     if ( enable )
00164     {
00165         kapp->installEventFilter( this );
00166         QApplication::setGlobalMouseTracking( true );
00167     }
00168     else
00169     {
00170         QApplication::setGlobalMouseTracking( false );
00171         kapp->removeEventFilter( this );
00172     }
00173 
00174     m_filter = enable;
00175 }
00176 
00177 void KNoteTip::reposition()
00178 {
00179     if ( !m_noteIVI )
00180         return;
00181 
00182     QRect rect = m_noteIVI->rect();
00183     QPoint off = m_view->mapToGlobal( m_view->contentsToViewport( QPoint( 0, 0 ) ) );
00184     rect.moveBy( off.x(), off.y() );
00185 
00186     QPoint pos = rect.center();
00187 
00188     // should the tooltip be shown to the left or to the right of the ivi?
00189     QRect desk = KGlobalSettings::desktopGeometry( pos );
00190     if ( rect.center().x() + width() > desk.right() )
00191     {
00192         // to the left
00193         if ( pos.x() - width() < 0 )
00194             pos.setX( 0 );
00195         else
00196             pos.setX( pos.x() - width() );
00197     }
00198 
00199     // should the tooltip be shown above or below the ivi ?
00200     if ( rect.bottom() + height() > desk.bottom() )
00201     {
00202         // above
00203         pos.setY( rect.top() - height() );
00204     }
00205     else
00206         pos.setY( rect.bottom() );
00207 
00208     move( pos );
00209     update();
00210 }
KDE Logo
This file is part of the documentation for kontact Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Apr 4 04:50:12 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003