kontact

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., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, 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     mFilter( false ),
00047     mView( parent ),
00048     mNoteIVI( 0 ),
00049     mPreview( new QTextEdit( this ) )
00050 {
00051   mPreview->setReadOnly( true );
00052   mPreview->setHScrollBarMode( QScrollView::AlwaysOff );
00053   mPreview->setVScrollBarMode( QScrollView::AlwaysOff );
00054 
00055   QBoxLayout *layout = new QVBoxLayout( this );
00056   layout->addWidget( mPreview );
00057 
00058   setPalette( QToolTip::palette() );
00059   setMargin( 1 );
00060   setFrameStyle( QFrame::Plain | QFrame::Box );
00061   hide();
00062 }
00063 
00064 KNoteTip::~KNoteTip()
00065 {
00066   delete mPreview;
00067   mPreview = 0;
00068 }
00069 
00070 void KNoteTip::setNote( KNotesIconViewItem *item )
00071 {
00072   if ( mNoteIVI == item )
00073     return;
00074 
00075   mNoteIVI = item;
00076 
00077   if ( !mNoteIVI ) {
00078     killTimers();
00079     if ( isVisible() ) {
00080       setFilter( false );
00081       hide();
00082     }
00083   } else {
00084     KCal::Journal *journal = item->journal();
00085     if ( journal->customProperty( "KNotes", "RichText" ) == "true" )
00086       mPreview->setTextFormat( Qt::RichText );
00087     else
00088       mPreview->setTextFormat( Qt::PlainText );
00089 
00090     QColor fg( journal->customProperty( "KNotes", "FgColor" ) );
00091     QColor bg( journal->customProperty( "KNotes", "BgColor" ) );
00092     setColor( fg, bg );
00093 
00094     mPreview->setText( journal->description() );
00095     mPreview->zoomTo( 8 );
00096     mPreview->sync();
00097 
00098     int w = 400;
00099     int h = mPreview->heightForWidth( w );
00100     while ( w > 60 && h == mPreview->heightForWidth( w - 20 ) )
00101         w -= 20;
00102 
00103     QRect desk = KGlobalSettings::desktopGeometry( mNoteIVI->rect().center() );
00104     resize( w, QMIN( h, desk.height() / 2 - 20 ) );
00105 
00106     hide();
00107     killTimers();
00108     setFilter( true );
00109     startTimer( 600 );  // delay showing the tooltip for 0.7 sec
00110   }
00111 }
00112 
00113 
00114 // protected, virtual methods
00115 
00116 void KNoteTip::resizeEvent( QResizeEvent *ev )
00117 {
00118   QFrame::resizeEvent( ev );
00119   reposition();
00120 }
00121 
00122 void KNoteTip::timerEvent( QTimerEvent * )
00123 {
00124   killTimers();
00125 
00126   if ( !isVisible() ) {
00127     startTimer( 15000 ); // show the tooltip for 15 sec
00128     reposition();
00129     show();
00130   } else {
00131     setFilter( false );
00132     hide();
00133   }
00134 }
00135 
00136 bool KNoteTip::eventFilter( QObject *, QEvent *e )
00137 {
00138   switch ( e->type() ) {
00139     case QEvent::Leave:
00140     case QEvent::MouseButtonPress:
00141     case QEvent::MouseButtonRelease:
00142     case QEvent::KeyPress:
00143     case QEvent::KeyRelease:
00144     case QEvent::FocusIn:
00145     case QEvent::FocusOut:
00146     case QEvent::Wheel:
00147       killTimers();
00148       setFilter( false );
00149       hide();
00150     default:
00151       break;
00152   }
00153 
00154   return false;
00155 }
00156 
00157 
00158 // private stuff
00159 
00160 void KNoteTip::setColor( const QColor &fg, const QColor &bg )
00161 {
00162   QPalette newpalette = palette();
00163   newpalette.setColor( QColorGroup::Background, bg );
00164   newpalette.setColor( QColorGroup::Foreground, fg );
00165   newpalette.setColor( QColorGroup::Base,       bg ); // text background
00166   newpalette.setColor( QColorGroup::Text,       fg ); // text color
00167   newpalette.setColor( QColorGroup::Button,     bg );
00168 
00169   // the shadow
00170   newpalette.setColor( QColorGroup::Midlight, bg.light(110) );
00171   newpalette.setColor( QColorGroup::Shadow, bg.dark(116) );
00172   newpalette.setColor( QColorGroup::Light, bg.light(180) );
00173   newpalette.setColor( QColorGroup::Dark, bg.dark(108) );
00174   setPalette( newpalette );
00175 
00176   // set the text color
00177   mPreview->setColor( fg );
00178 }
00179 
00180 
00181 void KNoteTip::setFilter( bool enable )
00182 {
00183   if ( enable == mFilter )
00184     return;
00185 
00186   if ( enable ) {
00187     kapp->installEventFilter( this );
00188     QApplication::setGlobalMouseTracking( true );
00189   } else {
00190     QApplication::setGlobalMouseTracking( false );
00191     kapp->removeEventFilter( this );
00192   }
00193 
00194   mFilter = enable;
00195 }
00196 
00197 void KNoteTip::reposition()
00198 {
00199   if ( !mNoteIVI )
00200     return;
00201 
00202   QRect rect = mNoteIVI->rect();
00203   QPoint off = mView->mapToGlobal( mView->contentsToViewport( QPoint( 0, 0 ) ) );
00204   rect.moveBy( off.x(), off.y() );
00205 
00206   QPoint pos = rect.center();
00207 
00208   // should the tooltip be shown to the left or to the right of the ivi?
00209   QRect desk = KGlobalSettings::desktopGeometry( pos );
00210   if ( rect.center().x() + width() > desk.right() ) {
00211     // to the left
00212     if ( pos.x() - width() < 0 )
00213         pos.setX( 0 );
00214     else
00215         pos.setX( pos.x() - width() );
00216   }
00217 
00218   // should the tooltip be shown above or below the ivi ?
00219   if ( rect.bottom() + height() > desk.bottom() ) {
00220     // above
00221     pos.setY( rect.top() - height() );
00222   } else
00223     pos.setY( rect.bottom() );
00224 
00225   move( pos );
00226   update();
00227 }
KDE Home | KDE Accessibility Home | Description of Access Keys