knotetip.cpp
1 /*
2  This file is part of the KDE project
3  Copyright (C) 2004 Michael Brade <brade@kde.org>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 
20  In addition, as a special exception, the copyright holders give
21  permission to link the code of this program with any edition of
22  the TQt library by Trolltech AS, Norway (or with modified versions
23  of TQt that use the same license as TQt), and distribute linked
24  combinations including the two. You must obey the GNU General
25  Public License in all respects for all of the code used other than
26  TQt. If you modify this file, you may extend this exception to
27  your version of the file, but you are not obligated to do so. If
28  you do not wish to do so, delete this exception statement from
29  your version.
30 */
31 
32 #include <tqtooltip.h>
33 #include <tqlayout.h>
34 #include <tqtextedit.h>
35 
36 #include <tdeapplication.h>
37 #include <tdeglobalsettings.h>
38 
39 #include "knotetip.h"
40 #include "knotes_part_p.h"
41 
42 
43 KNoteTip::KNoteTip( TDEIconView *parent )
44  : TQFrame( 0, 0, WX11BypassWM | // this will make Seli happy >:-P
45  WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WStyle_StaysOnTop ),
46  mFilter( false ),
47  mView( parent ),
48  mNoteIVI( 0 ),
49  mPreview( new TQTextEdit( this ) )
50 {
51  mPreview->setReadOnly( true );
52  mPreview->setHScrollBarMode( TQScrollView::AlwaysOff );
53  mPreview->setVScrollBarMode( TQScrollView::AlwaysOff );
54 
55  TQBoxLayout *layout = new TQVBoxLayout( this );
56  layout->addWidget( mPreview );
57 
58  setPalette( TQToolTip::palette() );
59  setMargin( 1 );
60  setFrameStyle( TQFrame::Plain | TQFrame::Box );
61  hide();
62 }
63 
64 KNoteTip::~KNoteTip()
65 {
66  delete mPreview;
67  mPreview = 0;
68 }
69 
70 void KNoteTip::setNote( KNotesIconViewItem *item )
71 {
72  if ( mNoteIVI == item )
73  return;
74 
75  mNoteIVI = item;
76 
77  if ( !mNoteIVI ) {
78  this->killTimers();
79  if ( isVisible() ) {
80  setFilter( false );
81  hide();
82  }
83  } else {
84  KCal::Journal *journal = item->journal();
85  if ( journal->customProperty( "KNotes", "RichText" ) == "true" )
86  mPreview->setTextFormat( TQt::RichText );
87  else
88  mPreview->setTextFormat( TQt::PlainText );
89 
90  TQColor fg( journal->customProperty( "KNotes", "FgColor" ) );
91  TQColor bg( journal->customProperty( "KNotes", "BgColor" ) );
92  setColor( fg, bg );
93 
94  mPreview->setText( journal->description() );
95  mPreview->zoomTo( 8 );
96  mPreview->sync();
97 
98  int w = 400;
99  int h = mPreview->heightForWidth( w );
100  while ( w > 60 && h == mPreview->heightForWidth( w - 20 ) )
101  w -= 20;
102 
103  TQRect desk = TDEGlobalSettings::desktopGeometry( mNoteIVI->rect().center() );
104  resize( w, TQMIN( h, desk.height() / 2 - 20 ) );
105 
106  hide();
107  this->killTimers();
108  setFilter( true );
109  startTimer( 600 ); // delay showing the tooltip for 0.7 sec
110  }
111 }
112 
113 
114 // protected, virtual methods
115 
116 void KNoteTip::resizeEvent( TQResizeEvent *ev )
117 {
118  TQFrame::resizeEvent( ev );
119  reposition();
120 }
121 
122 void KNoteTip::timerEvent( TQTimerEvent * )
123 {
124  this->killTimers();
125 
126  if ( !isVisible() ) {
127  startTimer( 15000 ); // show the tooltip for 15 sec
128  reposition();
129  show();
130  } else {
131  setFilter( false );
132  hide();
133  }
134 }
135 
136 bool KNoteTip::eventFilter( TQObject *, TQEvent *e )
137 {
138  switch ( e->type() ) {
139  case TQEvent::Leave:
140  case TQEvent::MouseButtonPress:
141  case TQEvent::MouseButtonRelease:
142  case TQEvent::KeyPress:
143  case TQEvent::KeyRelease:
144  case TQEvent::FocusIn:
145  case TQEvent::FocusOut:
146  case TQEvent::Wheel:
147  this->killTimers();
148  setFilter( false );
149  hide();
150  default:
151  break;
152  }
153 
154  return false;
155 }
156 
157 
158 // private stuff
159 
160 void KNoteTip::setColor( const TQColor &fg, const TQColor &bg )
161 {
162  TQPalette newpalette = palette();
163  newpalette.setColor( TQColorGroup::Background, bg );
164  newpalette.setColor( TQColorGroup::Foreground, fg );
165  newpalette.setColor( TQColorGroup::Base, bg ); // text background
166  newpalette.setColor( TQColorGroup::Text, fg ); // text color
167  newpalette.setColor( TQColorGroup::Button, bg );
168 
169  // the shadow
170  newpalette.setColor( TQColorGroup::Midlight, bg.light(110) );
171  newpalette.setColor( TQColorGroup::Shadow, bg.dark(116) );
172  newpalette.setColor( TQColorGroup::Light, bg.light(180) );
173  newpalette.setColor( TQColorGroup::Dark, bg.dark(108) );
174  setPalette( newpalette );
175 
176  // set the text color
177  mPreview->setColor( fg );
178 }
179 
180 
181 void KNoteTip::setFilter( bool enable )
182 {
183  if ( enable == mFilter )
184  return;
185 
186  if ( enable ) {
187  kapp->installEventFilter( this );
188  TQApplication::setGlobalMouseTracking( true );
189  } else {
190  TQApplication::setGlobalMouseTracking( false );
191  kapp->removeEventFilter( this );
192  }
193 
194  mFilter = enable;
195 }
196 
197 void KNoteTip::reposition()
198 {
199  if ( !mNoteIVI )
200  return;
201 
202  TQRect rect = mNoteIVI->rect();
203  TQPoint off = mView->mapToGlobal( mView->contentsToViewport( TQPoint( 0, 0 ) ) );
204  rect.moveBy( off.x(), off.y() );
205 
206  TQPoint pos = rect.center();
207 
208  // should the tooltip be shown to the left or to the right of the ivi?
209  TQRect desk = TDEGlobalSettings::desktopGeometry( pos );
210  if ( rect.center().x() + width() > desk.right() ) {
211  // to the left
212  if ( pos.x() - width() < 0 )
213  pos.setX( 0 );
214  else
215  pos.setX( pos.x() - width() );
216  }
217 
218  // should the tooltip be shown above or below the ivi ?
219  if ( rect.bottom() + height() > desk.bottom() ) {
220  // above
221  pos.setY( rect.top() - height() );
222  } else
223  pos.setY( rect.bottom() );
224 
225  move( pos );
226  update();
227 }
TQString customProperty(const TQCString &app, const TQCString &key) const
TQString description() const