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 TQt library by Trolltech AS, Norway (or with modified versions 00023 of TQt that use the same license as TQt), 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 TQt. 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 <tqtooltip.h> 00033 #include <tqlayout.h> 00034 #include <tqtextedit.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 : TQFrame( 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 TQTextEdit( this ) ) 00050 { 00051 mPreview->setReadOnly( true ); 00052 mPreview->setHScrollBarMode( TQScrollView::AlwaysOff ); 00053 mPreview->setVScrollBarMode( TQScrollView::AlwaysOff ); 00054 00055 TQBoxLayout *layout = new TQVBoxLayout( this ); 00056 layout->addWidget( mPreview ); 00057 00058 setPalette( TQToolTip::palette() ); 00059 setMargin( 1 ); 00060 setFrameStyle( TQFrame::Plain | TQFrame::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 TQT_TQOBJECT(this)->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( TQt::RichText ); 00087 else 00088 mPreview->setTextFormat( TQt::PlainText ); 00089 00090 TQColor fg( journal->customProperty( "KNotes", "FgColor" ) ); 00091 TQColor 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 TQRect desk = KGlobalSettings::desktopGeometry( mNoteIVI->rect().center() ); 00104 resize( w, TQMIN( h, desk.height() / 2 - 20 ) ); 00105 00106 hide(); 00107 TQT_TQOBJECT(this)->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( TQResizeEvent *ev ) 00117 { 00118 TQFrame::resizeEvent( ev ); 00119 reposition(); 00120 } 00121 00122 void KNoteTip::timerEvent( TQTimerEvent * ) 00123 { 00124 TQT_TQOBJECT(this)->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( TQObject *, TQEvent *e ) 00137 { 00138 switch ( e->type() ) { 00139 case TQEvent::Leave: 00140 case TQEvent::MouseButtonPress: 00141 case TQEvent::MouseButtonRelease: 00142 case TQEvent::KeyPress: 00143 case TQEvent::KeyRelease: 00144 case TQEvent::FocusIn: 00145 case TQEvent::FocusOut: 00146 case TQEvent::Wheel: 00147 TQT_TQOBJECT(this)->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 TQColor &fg, const TQColor &bg ) 00161 { 00162 TQPalette newpalette = palette(); 00163 newpalette.setColor( TQColorGroup::Background, bg ); 00164 newpalette.setColor( TQColorGroup::Foreground, fg ); 00165 newpalette.setColor( TQColorGroup::Base, bg ); // text background 00166 newpalette.setColor( TQColorGroup::Text, fg ); // text color 00167 newpalette.setColor( TQColorGroup::Button, bg ); 00168 00169 // the shadow 00170 newpalette.setColor( TQColorGroup::Midlight, bg.light(110) ); 00171 newpalette.setColor( TQColorGroup::Shadow, bg.dark(116) ); 00172 newpalette.setColor( TQColorGroup::Light, bg.light(180) ); 00173 newpalette.setColor( TQColorGroup::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 TQApplication::setGlobalMouseTracking( true ); 00189 } else { 00190 TQApplication::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 TQRect rect = mNoteIVI->rect(); 00203 TQPoint off = mView->mapToGlobal( mView->contentsToViewport( TQPoint( 0, 0 ) ) ); 00204 rect.moveBy( off.x(), off.y() ); 00205 00206 TQPoint pos = rect.center(); 00207 00208 // should the tooltip be shown to the left or to the right of the ivi? 00209 TQRect 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 }