knotes

knote.cpp
1 /*******************************************************************
2  KNotes -- Notes for the KDE project
3 
4  Copyright (c) 1997-2006, The KNotes Developers
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 2
9  of the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************/
20 
21 #include <tqlabel.h>
22 #include <tqdrawutil.h>
23 #include <tqsize.h>
24 #include <tqsizegrip.h>
25 #include <tqbitmap.h>
26 #include <tqcursor.h>
27 #include <tqpainter.h>
28 #include <tqpaintdevicemetrics.h>
29 #include <tqsimplerichtext.h>
30 #include <tqobjectlist.h>
31 #include <tqfile.h>
32 #include <tqcheckbox.h>
33 #include <tqtimer.h>
34 
35 #include <tdeapplication.h>
36 #include <kdebug.h>
37 #include <tdeaction.h>
38 #include <kstdaction.h>
39 #include <kcombobox.h>
40 #include <tdetoolbar.h>
41 #include <tdepopupmenu.h>
42 #include <kxmlguibuilder.h>
43 #include <kxmlguifactory.h>
44 #include <kcolordrag.h>
45 #include <kiconeffect.h>
46 #include <tdelocale.h>
47 #include <kstandarddirs.h>
48 #include <tdemessagebox.h>
49 #include <kfind.h>
50 #include <kprocess.h>
51 #include <kinputdialog.h>
52 #include <kmdcodec.h>
53 #include <tdeglobalsettings.h>
54 #include <tdefiledialog.h>
55 #include <tdeio/netaccess.h>
56 
57 #include <libkcal/journal.h>
58 
59 #include "knote.h"
60 #include "knotebutton.h"
61 #include "knoteedit.h"
62 #include "knoteconfig.h"
63 #include "knotesglobalconfig.h"
64 #include "knoteconfigdlg.h"
65 #include "knotealarmdlg.h"
66 #include "knotehostdlg.h"
67 #include "knotesnetsend.h"
68 #include "knoteprinter.h"
69 #include "version.h"
70 
71 #include "pushpin.xpm"
72 
73 #include <twin.h>
74 #include <netwm.h>
75 
76 #include <fixx11h.h>
77 
78 using namespace KCal;
79 
80 int KNote::s_ppOffset = 0;
81 
82 KNote::KNote( TQDomDocument buildDoc, Journal *j, TQWidget *parent, const char *name )
83  : TQFrame( parent, name, WStyle_Customize | WStyle_NoBorder | WDestructiveClose ),
84  m_label( 0 ), m_pushpin( 0 ), m_fold( 0 ), m_button( 0 ), m_tool( 0 ), m_editor( 0 ),
85  m_config( 0 ), m_journal( j ), m_find( 0 ),
86  m_twinConf( TDESharedConfig::openConfig( "twinrc", true ) ),
87  m_busy( 0 ), m_deleteWhenIdle( false ), m_blockEmitDataChanged( false )
88 {
89  setAcceptDrops( true );
90  actionCollection()->setWidget( this );
91 
92  setDOMDocument( buildDoc );
93 
94  // just set the name of the file to save the actions to, do NOT reparse it
95  setXMLFile( instance()->instanceName() + "ui.rc", false, false );
96 
97  // if there is no title yet, use the start date if valid
98  // (KOrganizer's journals don't have titles but a valid start date)
99  if ( m_journal->summary().isNull() && m_journal->dtStart().isValid() )
100  {
101  TQString s = TDEGlobal::locale()->formatDateTime( m_journal->dtStart() );
102  m_journal->setSummary( s );
103  }
104 
105  // create the menu items for the note - not the editor...
106  // rename, mail, print, save as, insert date, alarm, close, delete, new note
107  new TDEAction( i18n("New"), "document-new", 0,
108  TQT_TQOBJECT(this),TQT_SLOT(slotRequestNewNote()) , actionCollection(), "new_note" );
109  new TDEAction( i18n("Rename..."), "text", 0,
110  TQT_TQOBJECT(this), TQT_SLOT(slotRename()), actionCollection(), "rename_note" );
111  m_readOnly = new TDEToggleAction( i18n("Lock"), "system-lock-screen" , 0,
112  TQT_TQOBJECT(this), TQT_SLOT(slotUpdateReadOnly()), actionCollection(), "lock_note" );
113  m_readOnly->setCheckedState( KGuiItem( i18n("Unlock"), "unlock" ) );
114  new TDEAction( i18n("Hide"), "window-close" , Key_Escape,
115  TQT_TQOBJECT(this), TQT_SLOT(slotClose()), actionCollection(), "hide_note" );
116  new TDEAction( i18n("Delete"), "knotes_delete", 0,
117  TQT_TQOBJECT(this), TQT_SLOT(slotKill()), actionCollection(), "delete_note" );
118 
119  new TDEAction( i18n("Insert Date"), "knotes_date", 0 ,
120  TQT_TQOBJECT(this), TQT_SLOT(slotInsDate()), actionCollection(), "insert_date" );
121  new TDEAction( i18n("Set Alarm..."), "knotes_alarm", 0 ,
122  TQT_TQOBJECT(this), TQT_SLOT(slotSetAlarm()), actionCollection(), "set_alarm" );
123 
124  new TDEAction( i18n("Send..."), "network", 0,
125  TQT_TQOBJECT(this), TQT_SLOT(slotSend()), actionCollection(), "send_note" );
126  new TDEAction( i18n("Mail..."), "mail-send", 0,
127  TQT_TQOBJECT(this), TQT_SLOT(slotMail()), actionCollection(), "mail_note" );
128  new TDEAction( i18n("Save As..."), "document-save-as", 0,
129  TQT_TQOBJECT(this), TQT_SLOT(slotSaveAs()), actionCollection(), "save_note" );
130  KStdAction::print( TQT_TQOBJECT(this), TQT_SLOT(slotPrint()), actionCollection(), "print_note" );
131  new TDEAction( i18n("Preferences..."), "configure", 0,
132  TQT_TQOBJECT(this), TQT_SLOT(slotPreferences()), actionCollection(), "configure_note" );
133 
134  m_keepAbove = new TDEToggleAction( i18n("Keep Above Others"), "go-up", 0,
135  TQT_TQOBJECT(this), TQT_SLOT(slotUpdateKeepAboveBelow()), actionCollection(), "keep_above" );
136  m_keepAbove->setExclusiveGroup( "keepAB" );
137 
138  m_keepBelow = new TDEToggleAction( i18n("Keep Below Others"), "go-down", 0,
139  TQT_TQOBJECT(this), TQT_SLOT(slotUpdateKeepAboveBelow()), actionCollection(), "keep_below" );
140  m_keepBelow->setExclusiveGroup( "keepAB" );
141 
142  m_toDesktop = new TDEListAction( i18n("To Desktop"), 0,
143  TQT_TQOBJECT(this), TQT_SLOT(slotPopupActionToDesktop(int)), actionCollection(), "to_desktop" );
144  connect( m_toDesktop->popupMenu(), TQT_SIGNAL(aboutToShow()), TQT_TQOBJECT(this), TQT_SLOT(slotUpdateDesktopActions()) );
145 
146  // invisible action to walk through the notes to make this configurable
147  new TDEAction( i18n("Walk Through Notes"), 0, SHIFT+Key_BackTab,
148  TQT_TQOBJECT(this), TQT_SIGNAL(sigShowNextNote()), actionCollection(), "walk_notes" );
149 
150  // create the note header, button and label...
151  m_label = new TQLabel( this );
152  m_label->setFrameStyle( NoFrame );
153  m_label->setLineWidth( 0 );
154  m_label->installEventFilter( this ); // receive events (for dragging & action menu)
155  setName( m_journal->summary() ); // don't worry, no signals are connected at this stage yet
156 
157  m_button = new KNoteButton( "knotes_close", this );
158  connect( m_button, TQT_SIGNAL(clicked()), TQT_TQOBJECT(this), TQT_SLOT(slotClose()) );
159 
160  // create the note editor
161  m_editor = new KNoteEdit( actionCollection(), this );
162  m_editor->setNote( this );
163  m_editor->installEventFilter( this ); // receive events (for modified)
164  m_editor->viewport()->installEventFilter( this );
165  connect( m_editor, TQT_SIGNAL(contentsMoving( int, int )), TQT_TQOBJECT(this), TQT_SLOT(slotUpdateViewport( int, int )));
166 
167  KXMLGUIBuilder builder( this );
168  KXMLGUIFactory factory( &builder, TQT_TQOBJECT(this) );
169  factory.addClient( this );
170 
171  m_menu = dynamic_cast<TDEPopupMenu*>(factory.container( "note_context", this ));
172  m_edit_menu = dynamic_cast<TDEPopupMenu*>(factory.container( "note_edit", this ));
173  m_tool = dynamic_cast<TDEToolBar*>(factory.container( "note_tool", this ));
174 
175  if ( m_tool ) {
176  m_tool->setIconSize( 10 );
177  m_tool->setFixedHeight( 16 );
178  m_tool->setIconText( TDEToolBar::IconOnly );
179 
180  // if there was just a way of making KComboBox adhere the toolbar height...
181  TQObjectList *list = m_tool->queryList( "KComboBox" );
182  TQObjectListIt it( *list );
183  while ( it.current() != 0 )
184  {
185  KComboBox *combo = (KComboBox *)it.current();
186  TQFont font = combo->font();
187  font.setPointSize( 7 );
188  combo->setFont( font );
189  combo->setFixedHeight( 14 );
190  ++it;
191  }
192  delete list;
193 
194  m_tool->hide();
195  }
196 
197  setFocusProxy( m_editor );
198 
199  // create the resize handle
200  m_editor->setCornerWidget( new TQSizeGrip( this ) );
201  uint width = m_editor->cornerWidget()->width();
202  uint height = m_editor->cornerWidget()->height();
203  TQBitmap mask;
204  mask.resize( width, height );
205  mask.fill( color0 );
206  TQPointArray array;
207  array.setPoints( 3, 0, height, width, height, width, 0 );
208  TQPainter p;
209  p.begin( &mask );
210  p.setBrush( color1 );
211  p.drawPolygon( array );
212  p.end();
213  m_editor->cornerWidget()->setMask( mask );
214  m_editor->cornerWidget()->setBackgroundMode( PaletteBase );
215 
216  // the config file location
217  TQString configFile = TDEGlobal::dirs()->saveLocation( "appdata", "notes/" );
218  configFile += m_journal->uid();
219 
220  // no config file yet? -> use the default display config if available
221  // we want to write to configFile, so use "false"
222  bool newNote = !TDEIO::NetAccess::exists( KURL::fromPathOrURL( configFile ), false, 0 );
223 
224  m_config = new KNoteConfig( TDESharedConfig::openConfig( configFile, false, false ) );
225  m_config->readConfig();
226  m_config->setVersion( KNOTES_VERSION );
227 
228  if ( newNote )
229  {
230  // until tdelibs provides copying of TDEConfigSkeletons (KDE 3.4)
231  KNotesGlobalConfig *globalConfig = KNotesGlobalConfig::self();
232  m_config->setBgColor( globalConfig->bgColor() );
233  m_config->setFgColor( globalConfig->fgColor() );
234  m_config->setWidth( globalConfig->width() );
235  m_config->setHeight( globalConfig->height() );
236 
237  m_config->setFont( globalConfig->font() );
238  m_config->setTitleFont( globalConfig->titleFont() );
239  m_config->setAutoIndent( globalConfig->autoIndent() );
240  m_config->setRichText( globalConfig->richText() );
241  m_config->setTabSize( globalConfig->tabSize() );
242  m_config->setReadOnly( globalConfig->readOnly() );
243 
244  m_config->setDesktop( globalConfig->desktop() );
245  m_config->setHideNote( globalConfig->hideNote() );
246  m_config->setPosition( globalConfig->position() );
247  m_config->setShowInTaskbar( globalConfig->showInTaskbar() );
248  m_config->setKeepAbove( globalConfig->keepAbove() );
249  m_config->setKeepBelow( globalConfig->keepBelow() );
250 
251  m_config->writeConfig();
252  }
253 
254  // set up the look&feel of the note
255  setMinimumSize( 20, 20 );
256  setLineWidth( 1 );
257  setMargin( 0 );
258 
259  m_editor->setMargin( 0 );
260  m_editor->setFrameStyle( NoFrame );
261  m_editor->setBackgroundOrigin( WindowOrigin );
262 
263  // can be done here since this doesn't pick up changes while KNotes is running anyway
264  bool closeLeft = false;
265  m_twinConf->setGroup( "Style" );
266  if ( m_twinConf->readBoolEntry( "CustomButtonPositions" ) )
267  closeLeft = m_twinConf->readEntry( "ButtonsOnLeft" ).find( 'X' ) > -1;
268 
269  TQPixmap pushpin_pix;
270  if ( closeLeft )
271  pushpin_pix = TQPixmap( TQPixmap( pushpin_xpm ).convertToImage().mirror( true, false ) );
272  else
273  pushpin_pix = TQPixmap( pushpin_xpm );
274 
275  // the pushpin label at the top left or right corner
276  m_pushpin = new TQLabel( this );
277  m_pushpin->setScaledContents( true );
278  m_pushpin->setBackgroundMode( NoBackground );
279  m_pushpin->setPixmap( pushpin_pix );
280  m_pushpin->resize( pushpin_pix.size() );
281 
282  // fold label at bottom right corner
283  m_fold = new TQLabel( this );
284  m_fold->setScaledContents( true );
285  m_fold->setBackgroundMode( NoBackground );
286 
287  // load the display configuration of the note
288  width = m_config->width();
289  height = m_config->height();
290  resize( width, height );
291 
292  // let KWin do the placement if the position is illegal--at least 10 pixels
293  // of a note need to be visible
294  const TQPoint& position = m_config->position();
295  TQRect desk = kapp->desktop()->rect();
296  desk.addCoords( 10, 10, -10, -10 );
297  if ( desk.intersects( TQRect( position, TQSize( width, height ) ) ) )
298  move( position ); // do before calling show() to avoid flicker
299 
300  // config items in the journal have priority
301  TQString property = m_journal->customProperty( "KNotes", "FgColor" );
302  if ( !property.isNull() )
303  m_config->setFgColor( TQColor( property ) );
304  else
305  m_journal->setCustomProperty( "KNotes", "FgColor", m_config->fgColor().name() );
306 
307  property = m_journal->customProperty( "KNotes", "BgColor" );
308  if ( !property.isNull() )
309  m_config->setBgColor( TQColor( property ) );
310  else
311  m_journal->setCustomProperty( "KNotes", "BgColor", m_config->bgColor().name() );
312 
313  property = m_journal->customProperty( "KNotes", "RichText" );
314  if ( !property.isNull() )
315  m_config->setRichText( property == "true" ? true : false );
316  else
317  m_journal->setCustomProperty( "KNotes", "RichText", m_config->richText() ? "true" : "false" );
318 
319  // read configuration settings...
320  slotApplyConfig();
321 
322  // create the mask for the fold---to be done after slotApplyConfig(),
323  // which calls createFold()
324  m_fold->setMask( TQRegion( m_fold->pixmap()->createHeuristicMask() ) );
325 
326  // if this is a new note put on current desktop - we can't use defaults
327  // in TDEConfig XT since only _changes_ will be stored in the config file
328  int desktop = m_config->desktop();
329  if ( desktop < 0 && desktop != NETWinInfo::OnAllDesktops )
330  desktop = KWin::currentDesktop();
331 
332  // show the note if desired
333  if ( desktop != 0 && !m_config->hideNote() )
334  {
335  // to avoid flicker, call this before show()
336  toDesktop( desktop );
337  show();
338 
339  // because KWin forgets about that for hidden windows
340  if ( desktop == NETWinInfo::OnAllDesktops )
341  toDesktop( desktop );
342  }
343 
344  m_editor->setText( m_journal->description() );
345  m_editor->setModified( false );
346 
347  m_readOnly->setChecked( m_config->readOnly() );
348  slotUpdateReadOnly();
349 
350  if ( m_config->keepAbove() )
351  m_keepAbove->setChecked( true );
352  else if ( m_config->keepBelow() )
353  m_keepBelow->setChecked( true );
354  else
355  {
356  m_keepAbove->setChecked( false );
357  m_keepBelow->setChecked( false );
358  }
359  slotUpdateKeepAboveBelow();
360 
361  // HACK: update the icon color - again after showing the note, to make kicker aware of the new colors
362  TDEIconEffect effect;
363  TQPixmap icon = effect.apply( kapp->icon(), TDEIconEffect::Colorize, 1, m_config->bgColor(), false );
364  TQPixmap miniIcon = effect.apply( kapp->miniIcon(), TDEIconEffect::Colorize, 1, m_config->bgColor(), false );
365  KWin::setIcons( winId(), icon, miniIcon );
366 }
367 
368 KNote::~KNote()
369 {
370  delete m_config;
371 }
372 
373 void KNote::slotRequestNewNote()
374 {
375  //Be sure to save before to request a new note
376  saveConfig();
377  saveData();
378  emit sigRequestNewNote();
379 }
380 
381 void KNote::changeJournal(KCal::Journal *journal)
382 {
383  m_journal = journal;
384  m_editor->setText( m_journal->description() );
385  m_label->setText( m_journal->summary() );
386  updateLabelAlignment();
387 }
388 
389 // -------------------- public slots -------------------- //
390 
391 void KNote::slotKill( bool force )
392 {
393  m_blockEmitDataChanged = true;
394  if ( !force &&
395  KMessageBox::warningContinueCancel( this,
396  i18n("<qt>Do you really want to delete note <b>%1</b>?</qt>").arg( m_label->text() ),
397  i18n("Confirm Delete"), KGuiItem( i18n("&Delete"), "edit-delete" ),
398  "ConfirmDeleteNote"
399  )
400  != KMessageBox::Continue )
401  {
402  m_blockEmitDataChanged = false;
403  return;
404  }
405  aboutToEnterEventLoop();
406  // delete the configuration first, then the corresponding file
407  delete m_config;
408  m_config = 0;
409 
410  TQString configFile = TDEGlobal::dirs()->saveLocation( "appdata", "notes/" );
411  configFile += m_journal->uid();
412 
413  if ( !TDEIO::NetAccess::del( KURL::fromPathOrURL( configFile ), this ) )
414  kdError(5500) << "Can't remove the note config: " << configFile << endl;
415 
416  emit sigKillNote( m_journal );
417  eventLoopLeft();
418 
419 }
420 
421 
422 // -------------------- public member functions -------------------- //
423 
424 void KNote::saveData(bool update)
425 {
426  m_journal->setSummary( m_label->text() );
427  m_journal->setDescription( m_editor->text() );
428  m_journal->setCustomProperty( "KNotes", "FgColor", m_config->fgColor().name() );
429  m_journal->setCustomProperty( "KNotes", "BgColor", m_config->bgColor().name() );
430  m_journal->setCustomProperty( "KNotes", "RichText", m_config->richText() ? "true" : "false" );
431  if(update) {
432  emit sigDataChanged( noteId() );
433  m_editor->setModified( false );
434  }
435 }
436 
437 void KNote::saveConfig() const
438 {
439  m_config->setWidth( width() );
440  m_config->setHeight( height() );
441  m_config->setPosition( pos() );
442 
443  NETWinInfo wm_client( tqt_xdisplay(), winId(), tqt_xrootwin(), NET::WMDesktop );
444  if ( wm_client.desktop() == NETWinInfo::OnAllDesktops || wm_client.desktop() > 0 )
445  m_config->setDesktop( wm_client.desktop() );
446 
447  // actually store the config on disk
448  m_config->writeConfig();
449 }
450 
451 TQString KNote::noteId() const
452 {
453  return m_journal->uid();
454 }
455 
456 TQString KNote::name() const
457 {
458  return m_label->text();
459 }
460 
461 TQString KNote::text() const
462 {
463  return m_editor->text();
464 }
465 
466 TQString KNote::plainText() const
467 {
468  if ( m_editor->textFormat() == RichText )
469  {
470  TQTextEdit conv;
471  conv.setTextFormat( RichText );
472  conv.setText( m_editor->text() );
473  conv.setTextFormat( PlainText );
474  return conv.text();
475  }
476  else
477  return m_editor->text();
478 }
479 
480 void KNote::setName( const TQString& name )
481 {
482  m_label->setText( name );
483  updateLabelAlignment();
484 
485  if ( m_editor ) // not called from CTOR?
486  saveData();
487 
488  // set the window's name for the taskbar entry to be more helpful (#58338)
489  NETWinInfo note_win( tqt_xdisplay(), winId(), tqt_xrootwin(), NET::WMDesktop );
490  note_win.setName( name.utf8() );
491 
492  emit sigNameChanged();
493 }
494 
495 void KNote::setText( const TQString& text )
496 {
497  m_editor->setText( text );
498  saveData();
499 }
500 
501 TQColor KNote::fgColor() const
502 {
503  return m_config->fgColor();
504 }
505 
506 TQColor KNote::bgColor() const
507 {
508  return m_config->bgColor();
509 }
510 
511 void KNote::setColor( const TQColor& fg, const TQColor& bg )
512 {
513  m_journal->setCustomProperty( "KNotes", "FgColor", fg.name() );
514  m_journal->setCustomProperty( "KNotes", "BgColor", bg.name() );
515  m_config->setFgColor( fg );
516  m_config->setBgColor( bg );
517 
518  m_journal->updated(); // because setCustomProperty() doesn't call it!!
519  emit sigDataChanged(noteId());
520  m_config->writeConfig();
521 
522  TQPalette newpalette = palette();
523  newpalette.setColor( TQColorGroup::Background, bg );
524  newpalette.setColor( TQColorGroup::Foreground, fg );
525  newpalette.setColor( TQColorGroup::Base, bg ); // text background
526  newpalette.setColor( TQColorGroup::Text, fg ); // text color
527  newpalette.setColor( TQColorGroup::Button, bg );
528  newpalette.setColor( TQColorGroup::ButtonText, fg );
529 
530 // newpalette.setColor( TQColorGroup::Highlight, bg );
531 // newpalette.setColor( TQColorGroup::HighlightedText, fg );
532 
533  // the shadow
534  newpalette.setColor( TQColorGroup::Midlight, bg.light(150) );
535  newpalette.setColor( TQColorGroup::Shadow, bg.dark(116) );
536  newpalette.setColor( TQColorGroup::Light, bg.light(180) );
537  if ( s_ppOffset )
538  newpalette.setColor( TQColorGroup::Dark, bg.dark(200) );
539  else
540  newpalette.setColor( TQColorGroup::Dark, bg.dark(108) );
541  setPalette( newpalette );
542 
543  // set the text color
544  m_editor->setTextColor( fg );
545 
546  // set the background color or gradient
547  updateBackground();
548 
549  // set darker value for the hide button...
550  TQPalette darker = palette();
551  darker.setColor( TQColorGroup::Button, bg.dark(116) );
552  m_button->setPalette( darker );
553 
554  // update the icon color
555  TDEIconEffect effect;
556  TQPixmap icon = effect.apply( kapp->icon(), TDEIconEffect::Colorize, 1, bg, false );
557  TQPixmap miniIcon = effect.apply( kapp->miniIcon(), TDEIconEffect::Colorize, 1, bg, false );
558  KWin::setIcons( winId(), icon, miniIcon );
559 
560  // set the color for the selection used to highlight the find stuff
561  TQColor sel = palette().color( TQPalette::Active, TQColorGroup::Base ).dark();
562  if ( sel == TQt::black )
563  sel = palette().color( TQPalette::Active, TQColorGroup::Base ).light();
564 
565  m_editor->setSelectionAttributes( 1, sel, true );
566 
567  // update the color of the fold
568  createFold();
569 
570  // update the color of the title
571  updateFocus();
572  emit sigColorChanged();
573 }
574 
575 void KNote::find( const TQString& pattern, long options )
576 {
577  delete m_find;
578  m_find = new KFind( pattern, options, this );
579 
580  connect( m_find, TQT_SIGNAL(highlight( const TQString &, int, int )),
581  TQT_TQOBJECT(this), TQT_SLOT(slotHighlight( const TQString &, int, int )) );
582  connect( m_find, TQT_SIGNAL(findNext()), TQT_TQOBJECT(this), TQT_SLOT(slotFindNext()) );
583 
584  m_find->setData( plainText() );
585  slotFindNext();
586 }
587 
588 void KNote::slotFindNext()
589 {
590  // TODO: honor FindBackwards
591  // TODO: dialogClosed() -> delete m_find
592 
593  // Let KFind inspect the text fragment, and display a dialog if a match is found
594  KFind::Result res = m_find->find();
595 
596  if ( res == KFind::NoMatch ) // i.e. at end-pos
597  {
598  m_editor->removeSelection( 1 );
599  emit sigFindFinished();
600  delete m_find;
601  m_find = 0;
602  }
603  else
604  {
605  show();
606  KWin::setCurrentDesktop( KWin::windowInfo( winId() ).desktop() );
607  }
608 }
609 
610 void KNote::slotHighlight( const TQString& str, int idx, int len )
611 {
612  int paraFrom = 0, idxFrom = 0, p = 0;
613  for ( ; p < idx; ++p )
614  if ( str[p] == '\n' )
615  {
616  ++paraFrom;
617  idxFrom = 0;
618  }
619  else
620  ++idxFrom;
621 
622  int paraTo = paraFrom, idxTo = idxFrom;
623 
624  for ( ; p < idx + len; ++p )
625  {
626  if ( str[p] == '\n' )
627  {
628  ++paraTo;
629  idxTo = 0;
630  }
631  else
632  ++idxTo;
633  }
634 
635  m_editor->setSelection( paraFrom, idxFrom, paraTo, idxTo, 1 );
636 }
637 
638 bool KNote::isModified() const
639 {
640  return m_editor->isModified();
641 }
642 
643 // FIXME KDE 4.0: remove sync(), isNew() and isModified()
644 void KNote::sync( const TQString& app )
645 {
646  TQByteArray sep( 1 );
647  sep[0] = '\0';
648 
649  KMD5 hash;
650  TQCString result;
651 
652  hash.update( m_label->text().utf8() );
653  hash.update( sep );
654  hash.update( m_editor->text().utf8() );
655  hash.hexDigest( result );
656 
657  // hacky... not possible with TDEConfig XT
658  TDEConfig *config = m_config->config();
659  config->setGroup( "Synchronisation" );
660  config->writeEntry( app, result.data() );
661 }
662 
663 bool KNote::isNew( const TQString& app ) const
664 {
665  TDEConfig *config = m_config->config();
666  config->setGroup( "Synchronisation" );
667  TQString hash = config->readEntry( app );
668  return hash.isEmpty();
669 }
670 
671 bool KNote::isModified( const TQString& app ) const
672 {
673  TQByteArray sep( 1 );
674  sep[0] = '\0';
675 
676  KMD5 hash;
677  hash.update( m_label->text().utf8() );
678  hash.update( sep );
679  hash.update( m_editor->text().utf8() );
680  hash.hexDigest();
681 
682  TDEConfig *config = m_config->config();
683  config->setGroup( "Synchronisation" );
684  TQString orig = config->readEntry( app );
685 
686  if ( hash.verify( orig.utf8() ) ) // returns false on error!
687  return false;
688  else
689  return true;
690 }
691 
692 void KNote::setStyle( int style )
693 {
694  if ( style == KNotesGlobalConfig::EnumStyle::Plain )
695  s_ppOffset = 0;
696  else
697  s_ppOffset = 12;
698 }
699 
700 
701 // ------------------ private slots (menu actions) ------------------ //
702 
703 void KNote::slotRename()
704 {
705  m_blockEmitDataChanged = true;
706  // pop up dialog to get the new name
707  bool ok;
708  aboutToEnterEventLoop();
709  TQString oldName = m_label->text();
710  TQString newName = KInputDialog::getText( TQString(),
711  i18n("Please enter the new name:"), m_label->text(), &ok, this );
712  eventLoopLeft();
713  m_blockEmitDataChanged = false;
714  if ( !ok || ( oldName == newName) ) // handle cancel
715  return;
716 
717  setName( newName );
718 }
719 
720 void KNote::slotUpdateReadOnly()
721 {
722  const bool readOnly = m_readOnly->isChecked();
723 
724  m_editor->setReadOnly( readOnly );
725  m_config->setReadOnly( readOnly );
726 
727  // Enable/disable actions accordingly
728  actionCollection()->action( "configure_note" )->setEnabled( !readOnly );
729  actionCollection()->action( "insert_date" )->setEnabled( !readOnly );
730  actionCollection()->action( "delete_note" )->setEnabled( !readOnly );
731 
732  actionCollection()->action( "edit_undo" )->setEnabled( !readOnly && m_editor->isUndoAvailable() );
733  actionCollection()->action( "edit_redo" )->setEnabled( !readOnly && m_editor->isRedoAvailable() );
734  actionCollection()->action( "edit_cut" )->setEnabled( !readOnly && m_editor->hasSelectedText() );
735  actionCollection()->action( "edit_paste" )->setEnabled( !readOnly );
736  actionCollection()->action( "edit_clear" )->setEnabled( !readOnly );
737  actionCollection()->action( "rename_note" )->setEnabled( !readOnly );
738 
739  actionCollection()->action( "format_bold" )->setEnabled( !readOnly );
740  actionCollection()->action( "format_italic" )->setEnabled( !readOnly );
741  actionCollection()->action( "format_underline" )->setEnabled( !readOnly );
742  actionCollection()->action( "format_strikeout" )->setEnabled( !readOnly );
743  actionCollection()->action( "format_alignleft" )->setEnabled( !readOnly );
744  actionCollection()->action( "format_aligncenter" )->setEnabled( !readOnly );
745  actionCollection()->action( "format_alignright" )->setEnabled( !readOnly );
746  actionCollection()->action( "format_alignblock" )->setEnabled( !readOnly );
747  actionCollection()->action( "format_list" )->setEnabled( !readOnly );
748  actionCollection()->action( "format_super" )->setEnabled( !readOnly );
749  actionCollection()->action( "format_sub" )->setEnabled( !readOnly );
750  actionCollection()->action( "format_size" )->setEnabled( !readOnly );
751  actionCollection()->action( "format_color" )->setEnabled( !readOnly );
752 
753  updateFocus();
754 }
755 
756 void KNote::slotClose()
757 {
758  NETWinInfo wm_client( tqt_xdisplay(), winId(), tqt_xrootwin(), NET::WMDesktop );
759  if ( wm_client.desktop() == NETWinInfo::OnAllDesktops || wm_client.desktop() > 0 )
760  m_config->setDesktop( wm_client.desktop() );
761 
762  m_editor->clearFocus();
763  m_config->setHideNote( true );
764  m_config->setPosition( pos() );
765 
766  // just hide the note so it's still available from the dock window
767  hide();
768 }
769 
770 void KNote::slotInsDate()
771 {
772  m_editor->insert( TDEGlobal::locale()->formatDateTime(TQDateTime::currentDateTime()) );
773 }
774 
775 void KNote::slotSetAlarm()
776 {
777  m_blockEmitDataChanged = true;
778  KNoteAlarmDlg dlg( name(), this );
779  dlg.setIncidence( m_journal );
780 
781  aboutToEnterEventLoop();
782  if ( dlg.exec() == TQDialog::Accepted )
783  emit sigDataChanged(noteId());
784  eventLoopLeft();
785  m_blockEmitDataChanged = false;
786 }
787 
788 void KNote::slotPreferences()
789 {
790  // reuse if possible
791  if ( KNoteConfigDlg::showDialog( noteId().utf8() ) )
792  return;
793 
794  // create a new preferences dialog...
795  KNoteConfigDlg *dialog = new KNoteConfigDlg( m_config, name(), this, noteId().utf8() );
796  connect( dialog, TQT_SIGNAL(settingsChanged()), TQT_TQOBJECT(this), TQT_SLOT(slotApplyConfig()) );
797  connect( this, TQT_SIGNAL(sigNameChanged()), dialog, TQT_SLOT(slotUpdateCaption()) );
798  dialog->show();
799 }
800 
801 void KNote::slotSend()
802 {
803  // pop up dialog to get the IP
804  KNoteHostDlg hostDlg( i18n("Send \"%1\"").arg( name() ), this );
805  aboutToEnterEventLoop();
806  bool ok = (hostDlg.exec() == TQDialog::Accepted);
807  eventLoopLeft();
808  if ( !ok ) // handle cancel
809  return;
810  TQString host = hostDlg.host();
811 
812  if ( host.isEmpty() )
813  {
814  KMessageBox::sorry( this, i18n("The host cannot be empty.") );
815  return;
816  }
817 
818  // Send the note
819  KNotesNetworkSender *sender = new KNotesNetworkSender( host, KNotesGlobalConfig::port() );
820  sender->setSenderId( KNotesGlobalConfig::senderID() );
821  sender->setNote( name(), text() );
822  sender->connect();
823 }
824 
825 void KNote::slotMail()
826 {
827  // get the mail action command
828  const TQStringList cmd_list = TQStringList::split( TQChar(' '), KNotesGlobalConfig::mailAction() );
829 
830  TDEProcess mail;
831  for ( TQStringList::ConstIterator it = cmd_list.constBegin();
832  it != cmd_list.constEnd(); ++it )
833  {
834  if ( *it == "%f" )
835  mail << plainText().local8Bit(); // convert rich text to plain text
836  else if ( *it == "%t" )
837  mail << m_label->text().local8Bit();
838  else
839  mail << (*it).local8Bit();
840  }
841 
842  if ( !mail.start( TDEProcess::DontCare ) )
843  KMessageBox::sorry( this, i18n("Unable to start the mail process.") );
844 }
845 
846 void KNote::slotPrint()
847 {
848  TQString content;
849  if ( m_editor->textFormat() == PlainText )
850  content = TQStyleSheet::convertFromPlainText( m_editor->text() );
851  else
852  content = m_editor->text();
853 
854  KNotePrinter printer;
855  printer.setMimeSourceFactory( m_editor->mimeSourceFactory() );
856  printer.setFont( m_config->font() );
857  printer.setContext( m_editor->context() );
858  printer.setStyleSheet( m_editor->styleSheet() );
859  printer.setColorGroup( colorGroup() );
860  printer.printNote( TQString(), content );
861 }
862 
863 void KNote::slotSaveAs()
864 {
865  m_blockEmitDataChanged = true;
866  TQCheckBox *convert = 0;
867 
868  if ( m_editor->textFormat() == RichText )
869  {
870  convert = new TQCheckBox( 0 );
871  convert->setText( i18n("Save note as plain text") );
872  }
873 
874  KFileDialog dlg( TQString(), TQString(), this, "filedialog", true, convert );
875  dlg.setOperationMode( KFileDialog::Saving );
876  dlg.setCaption( i18n("Save As") );
877  aboutToEnterEventLoop();
878  dlg.exec();
879  eventLoopLeft();
880 
881  TQString fileName = dlg.selectedFile();
882  if ( fileName.isEmpty() )
883  {
884  m_blockEmitDataChanged = false;
885  return;
886  }
887  TQFile file( fileName );
888 
889  if ( file.exists() &&
890  KMessageBox::warningContinueCancel( this, i18n("<qt>A file named <b>%1</b> already exists.<br>"
891  "Are you sure you want to overwrite it?</qt>").arg( TQFileInfo(file).fileName() ) )
892  != KMessageBox::Continue )
893  {
894  m_blockEmitDataChanged = false;
895  return;
896  }
897 
898  if ( file.open( IO_WriteOnly ) )
899  {
900  TQTextStream stream( &file );
901  // convert rich text to plain text first
902  if ( convert && convert->isChecked() )
903  stream << plainText();
904  else
905  stream << text();
906  }
907  m_blockEmitDataChanged = false;
908 }
909 
910 void KNote::slotPopupActionToDesktop( int id )
911 {
912  toDesktop( id - 1 ); // compensate for the menu separator, -1 == all desktops
913 }
914 
915 
916 // ------------------ private slots (configuration) ------------------ //
917 
918 void KNote::slotApplyConfig()
919 {
920  if ( m_config->richText() )
921  m_editor->setTextFormat( RichText );
922  else
923  m_editor->setTextFormat( PlainText );
924 
925  m_label->setFont( m_config->titleFont() );
926  m_editor->setTextFont( m_config->font() );
927  m_editor->setTabStop( m_config->tabSize() );
928  m_editor->setAutoIndentMode( m_config->autoIndent() );
929 
930  // if called as a slot, save the text, we might have changed the
931  // text format - otherwise the journal will not be updated
932  if ( sender() )
933  saveData();
934 
935  setColor( m_config->fgColor(), m_config->bgColor() );
936 
937  updateLabelAlignment();
938  slotUpdateShowInTaskbar();
939 }
940 
941 void KNote::slotUpdateKeepAboveBelow()
942 {
943  KWin::WindowInfo info( KWin::windowInfo( winId() ) );
944 
945  if ( m_keepAbove->isChecked() )
946  {
947  m_config->setKeepAbove( true );
948  m_config->setKeepBelow( false );
949  KWin::setState( winId(), info.state() | NET::KeepAbove );
950  }
951  else if ( m_keepBelow->isChecked() )
952  {
953  m_config->setKeepAbove( false );
954  m_config->setKeepBelow( true );
955  KWin::setState( winId(), info.state() | NET::KeepBelow );
956  }
957  else
958  {
959  m_config->setKeepAbove( false );
960  KWin::clearState( winId(), NET::KeepAbove );
961 
962  m_config->setKeepBelow( false );
963  KWin::clearState( winId(), NET::KeepBelow );
964  }
965 }
966 
967 void KNote::slotUpdateShowInTaskbar()
968 {
969  if ( !m_config->showInTaskbar() )
970  KWin::setState( winId(), KWin::windowInfo(winId()).state() | NET::SkipTaskbar );
971  else
972  KWin::clearState( winId(), NET::SkipTaskbar );
973 }
974 
975 void KNote::slotUpdateDesktopActions()
976 {
977  NETRootInfo wm_root( tqt_xdisplay(), NET::NumberOfDesktops | NET::DesktopNames );
978  NETWinInfo wm_client( tqt_xdisplay(), winId(), tqt_xrootwin(), NET::WMDesktop );
979 
980  TQStringList desktops;
981  desktops.append( i18n("&All Desktops") );
982  desktops.append( TQString() ); // Separator
983 
984  int count = wm_root.numberOfDesktops();
985  for ( int n = 1; n <= count; n++ )
986  desktops.append( TQString("&%1 %2").arg( n ).arg( TQString::fromUtf8(wm_root.desktopName( n )) ) );
987 
988  m_toDesktop->setItems( desktops );
989 
990  if ( wm_client.desktop() == NETWinInfo::OnAllDesktops )
991  m_toDesktop->setCurrentItem( 0 );
992  else
993  m_toDesktop->setCurrentItem( wm_client.desktop() + 1 ); // compensate for separator (+1)
994 }
995 
996 void KNote::slotUpdateViewport( int /*x*/, int y )
997 {
998  if ( s_ppOffset )
999  updateBackground( y );
1000 }
1001 
1002 // -------------------- private methods -------------------- //
1003 
1004 void KNote::toDesktop( int desktop )
1005 {
1006  if ( desktop == 0 )
1007  return;
1008 
1009  if ( desktop == NETWinInfo::OnAllDesktops )
1010  KWin::setOnAllDesktops( winId(), true );
1011  else
1012  KWin::setOnDesktop( winId(), desktop );
1013 }
1014 
1015 void KNote::createFold()
1016 {
1017  TQPixmap fold( 15, 15 );
1018  TQPainter foldp( &fold );
1019  foldp.setPen( TQt::NoPen );
1020  foldp.setBrush( palette().active().dark() );
1021  TQPointArray foldpoints( 3 );
1022  foldpoints.putPoints( 0, 3, 0, 0, 14, 0, 0, 14 );
1023  foldp.drawPolygon( foldpoints );
1024  foldp.end();
1025  m_fold->setPixmap( fold );
1026 }
1027 
1028 void KNote::updateLabelAlignment()
1029 {
1030  // if the name is too long to fit, left-align it, otherwise center it (#59028)
1031  TQString labelText = m_label->text();
1032  if ( m_label->fontMetrics().boundingRect( labelText ).width() > m_label->width() )
1033  m_label->setAlignment( AlignLeft );
1034  else
1035  m_label->setAlignment( AlignHCenter );
1036 }
1037 
1038 void KNote::updateFocus()
1039 {
1040  if ( hasFocus() )
1041  {
1042  m_label->setBackgroundColor( palette().active().shadow() );
1043  m_button->show();
1044 
1045  if ( !m_editor->isReadOnly() )
1046  {
1047  if ( m_tool && m_tool->isHidden() && m_editor->textFormat() == TQTextEdit::RichText )
1048  {
1049  m_tool->show();
1050  updateLayout(); // to update the editor height
1051  }
1052  m_editor->cornerWidget()->show();
1053  }
1054  else
1055  {
1056  if ( m_tool && !m_tool->isHidden() )
1057  {
1058  m_tool->hide();
1059  updateLayout(); // to update the minimum height
1060  }
1061  m_editor->cornerWidget()->hide();
1062  }
1063 
1064  m_fold->hide();
1065  }
1066  else
1067  {
1068  m_button->hide();
1069  m_editor->cornerWidget()->hide();
1070 
1071  if ( m_tool && !m_tool->isHidden() )
1072  {
1073  m_tool->hide();
1074  updateLayout(); // to update the minimum height
1075  }
1076 
1077  if ( s_ppOffset )
1078  {
1079  m_label->setBackgroundColor( palette().active().midlight() );
1080  m_fold->show();
1081  }
1082  else
1083  m_label->setBackgroundColor( palette().active().background() );
1084  }
1085 }
1086 
1087 void KNote::updateMask()
1088 {
1089  if ( !s_ppOffset )
1090  {
1091  clearMask();
1092  return;
1093  }
1094 
1095  int w = width();
1096  int h = height();
1097  TQRegion reg( 0, s_ppOffset, w, h - s_ppOffset );
1098 
1099  const TQBitmap *pushpin_bitmap = m_pushpin->pixmap()->mask();
1100  TQRegion pushpin_reg( *pushpin_bitmap );
1101  m_pushpin->setMask( pushpin_reg );
1102  pushpin_reg.translate( m_pushpin->x(), m_pushpin->y() );
1103 
1104  if ( !hasFocus() )
1105  {
1106  TQPointArray foldpoints( 3 );
1107  foldpoints.putPoints( 0, 3, w-15, h, w, h-15, w, h );
1108  TQRegion fold( foldpoints, false );
1109  setMask( reg.unite( pushpin_reg ).subtract( fold ) );
1110  }
1111  else
1112  setMask( reg.unite( pushpin_reg ) );
1113 }
1114 
1115 void KNote::updateBackground( int y_offset )
1116 {
1117  if ( !s_ppOffset )
1118  {
1119  m_editor->setPaper( TQBrush( colorGroup().background() ) );
1120  return;
1121  }
1122 
1123  int w = m_editor->visibleWidth();
1124  int h = m_editor->visibleHeight();
1125 
1126  // in case y_offset is not set, calculate y_offset as the content
1127  // y-coordinate of the top-left point of the viewport - which is essentially
1128  // the vertical scroll amount
1129  if ( y_offset == -1 )
1130  y_offset = m_editor->contentsY();
1131 
1132  y_offset = y_offset % h;
1133 
1134  TQImage grad_img( w, h, 32 );
1135  TQRgb rgbcol;
1136  TQColor bg = palette().active().background();
1137 
1138  for ( int i = 0; i < h; ++i )
1139  {
1140  // if the scrollbar has moved, then adjust the gradient by the amount the
1141  // scrollbar moved -- so that the background gradient looks ok when tiled
1142 
1143  // the lightness is calculated as follows:
1144  // if i >= y, then lightness = 150 - (i-y)*75/h;
1145  // if i < y, then lightness = 150 - (i+h-y)*75/h
1146 
1147  int i_1 = 150 - 75 * ((i - y_offset + h) % h) / h;
1148  rgbcol = bg.light( i_1 ).rgb();
1149  for ( int j = 0; j < w; ++j )
1150  grad_img.setPixel( j, i, rgbcol );
1151  }
1152 
1153  // setPaletteBackgroundPixmap makes TQTextEdit::color() stop working!!
1154  m_editor->setPaper( TQBrush( TQt::black, TQPixmap( grad_img ) ) );
1155 }
1156 
1157 void KNote::updateLayout()
1158 {
1159  const int headerHeight = m_label->sizeHint().height();
1160  const int margin = m_editor->margin();
1161  bool closeLeft = false;
1162 
1163  m_twinConf->setGroup( "Style" );
1164  if ( m_twinConf->readBoolEntry( "CustomButtonPositions" ) )
1165  closeLeft = m_twinConf->readEntry( "ButtonsOnLeft" ).find( 'X' ) > -1;
1166 
1167  if ( s_ppOffset )
1168  {
1169  if ( !m_editor->paper().pixmap() ) // just changed the style
1170  setColor( palette().active().foreground(), palette().active().background() );
1171 
1172  m_pushpin->show();
1173  setFrameStyle( Panel | Raised );
1174 
1175  if ( closeLeft )
1176  m_pushpin->move( width() - m_pushpin->width(), 0 );
1177  else
1178  m_pushpin->move( 0, 0 );
1179  }
1180  else
1181  {
1182  if ( m_editor->paper().pixmap() ) // just changed the style
1183  setColor( palette().active().foreground(), palette().active().background() );
1184 
1185  setFrameStyle( WinPanel | Raised );
1186  m_pushpin->hide();
1187  m_fold->hide();
1188  }
1189 
1190  m_button->setGeometry(
1191  closeLeft ? contentsRect().x() : contentsRect().width() - headerHeight,
1192  contentsRect().y() + s_ppOffset,
1193  headerHeight,
1194  headerHeight
1195  );
1196 
1197  m_label->setGeometry(
1198  contentsRect().x(), contentsRect().y() + s_ppOffset,
1199  contentsRect().width(), headerHeight
1200  );
1201 
1202  m_editor->setGeometry( TQRect(
1203  TQPoint( contentsRect().x(),
1204  contentsRect().y() + headerHeight + s_ppOffset ),
1205  TQPoint( contentsRect().right(),
1206  contentsRect().bottom() - ( m_tool ? (m_tool->isHidden() ? 0 : m_tool->height()) : 0 ) )
1207  ) );
1208 
1209  if( m_tool ) {
1210  m_tool->setGeometry(
1211  contentsRect().x(),
1212  contentsRect().bottom() - m_tool->height() + 1,
1213  contentsRect().width(),
1214  m_tool->height()
1215  );
1216  }
1217 
1218  if ( s_ppOffset )
1219  m_fold->move( width() - 15, height() - 15 );
1220 
1221  setMinimumSize(
1222  m_editor->cornerWidget()->width() + margin*2,
1223  headerHeight + s_ppOffset + ( m_tool ? m_tool->height() : 0 ) +
1224  m_editor->cornerWidget()->height() + margin*2
1225  );
1226 
1227  updateLabelAlignment();
1228  updateMask();
1229  updateBackground();
1230 }
1231 
1232 // -------------------- protected methods -------------------- //
1233 
1234 void KNote::drawFrame( TQPainter *p )
1235 {
1236  TQRect r = frameRect();
1237  r.setTop( s_ppOffset );
1238  if ( s_ppOffset )
1239  qDrawShadePanel( p, r, colorGroup(), false, lineWidth() );
1240  else
1241  qDrawWinPanel( p, r, colorGroup(), false );
1242 }
1243 
1244 void KNote::showEvent( TQShowEvent * )
1245 {
1246  if ( m_config->hideNote() )
1247  {
1248  // KWin does not preserve these properties for hidden windows
1249  slotUpdateKeepAboveBelow();
1250  slotUpdateShowInTaskbar();
1251  toDesktop( m_config->desktop() );
1252  move( m_config->position() );
1253  m_config->setHideNote( false );
1254  }
1255 }
1256 
1257 void KNote::resizeEvent( TQResizeEvent *qre )
1258 {
1259  TQFrame::resizeEvent( qre );
1260  updateLayout();
1261 }
1262 
1263 void KNote::closeEvent( TQCloseEvent *event )
1264 {
1265  if(kapp->sessionSaving())
1266  return;
1267  event->ignore(); //We don't want to close (and delete the widget). Just hide it
1268  slotClose();
1269 }
1270 
1271 void KNote::dragEnterEvent( TQDragEnterEvent *e )
1272 {
1273  if ( !m_config->readOnly() )
1274  e->accept( KColorDrag::canDecode( e ) );
1275 }
1276 
1277 void KNote::dropEvent( TQDropEvent *e )
1278 {
1279  if ( m_config->readOnly() )
1280  return;
1281 
1282  TQColor bg;
1283  if ( KColorDrag::decode( e, bg ) )
1284  setColor( paletteForegroundColor(), bg );
1285 }
1286 
1287 bool KNote::focusNextPrevChild( bool )
1288 {
1289  return true;
1290 }
1291 
1292 bool KNote::event( TQEvent *ev )
1293 {
1294  if ( ev->type() == TQEvent::LayoutHint )
1295  {
1296  updateLayout();
1297  return true;
1298  }
1299  else
1300  return TQFrame::event( ev );
1301 }
1302 
1303 bool KNote::eventFilter( TQObject *o, TQEvent *ev )
1304 {
1305  if ( ev->type() == TQEvent::DragEnter &&
1306  KColorDrag::canDecode( static_cast<TQDragEnterEvent *>(ev) ) )
1307  {
1308  dragEnterEvent( static_cast<TQDragEnterEvent *>(ev) );
1309  return true;
1310  }
1311 
1312  if ( ev->type() == TQEvent::Drop &&
1313  KColorDrag::canDecode( static_cast<TQDropEvent *>(ev) ) )
1314  {
1315  dropEvent( static_cast<TQDropEvent *>(ev) );
1316  return true;
1317  }
1318 
1319  if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(m_label) )
1320  {
1321  TQMouseEvent *e = (TQMouseEvent *)ev;
1322 
1323  if ( ev->type() == TQEvent::MouseButtonDblClick )
1324  {
1325  if( !m_editor->isReadOnly())
1326  slotRename();
1327  }
1328  if ( ev->type() == TQEvent::MouseButtonPress &&
1329  (e->button() == Qt::LeftButton || e->button() == Qt::MidButton))
1330  {
1331  e->button() == Qt::LeftButton ? KWin::raiseWindow( winId() )
1332  : KWin::lowerWindow( winId() );
1333 
1334  XUngrabPointer( tqt_xdisplay(), GET_QT_X_TIME() );
1335  NETRootInfo wm_root( tqt_xdisplay(), NET::WMMoveResize );
1336  wm_root.moveResizeRequest( winId(), e->globalX(), e->globalY(), NET::Move );
1337  return true;
1338  }
1339 
1340 #if KDE_IS_VERSION( 3, 5, 1 )
1341  if ( ev->type() == TQEvent::MouseButtonRelease )
1342  {
1343  NETRootInfo wm_root( tqt_xdisplay(), NET::WMMoveResize );
1344  wm_root.moveResizeRequest( winId(), e->globalX(), e->globalY(), NET::MoveResizeCancel );
1345  return false;
1346  }
1347 #endif
1348 
1349  if ( m_menu && ( ev->type() == TQEvent::MouseButtonPress )
1350  && ( e->button() == Qt::RightButton ) )
1351  {
1352  m_menu->popup( TQCursor::pos() );
1353  return true;
1354  }
1355 
1356  return false;
1357  }
1358 
1359  if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(m_editor) ) {
1360  if ( ev->type() == TQEvent::FocusOut ) {
1361  TQFocusEvent *fe = TQT_TQFOCUSEVENT(ev);
1362  if ( fe->reason() != TQFocusEvent::Popup &&
1363  fe->reason() != TQFocusEvent::Mouse ) {
1364  updateFocus();
1365  if ( isModified() ) {
1366  saveConfig();
1367  if ( !m_blockEmitDataChanged )
1368  saveData();
1369  }
1370  }
1371  } else if ( ev->type() == TQEvent::FocusIn ) {
1372  updateFocus();
1373  }
1374 
1375  return false;
1376  }
1377 
1378  if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(m_editor->viewport()) )
1379  {
1380  if ( m_edit_menu &&
1381  ev->type() == TQEvent::MouseButtonPress &&
1382  ((TQMouseEvent *)ev)->button() == Qt::RightButton )
1383  {
1384  m_edit_menu->popup( TQCursor::pos() );
1385  return true;
1386  }
1387  }
1388 
1389  return false;
1390 }
1391 
1392 void KNote::slotSaveData()
1393 {
1394  saveData();
1395 }
1396 
1397 void KNote::deleteWhenIdle()
1398 {
1399  if ( m_busy <= 0 )
1400  deleteLater();
1401  else
1402  m_deleteWhenIdle = true;
1403 }
1404 
1405 void KNote::aboutToEnterEventLoop()
1406 {
1407  ++m_busy;
1408 }
1409 
1410 void KNote::eventLoopLeft()
1411 {
1412  --m_busy;
1413  if ( m_busy <= 0 && m_deleteWhenIdle )
1414  deleteLater();
1415 }
1416 
1417 
1418 #include "knote.moc"
1419 #include "knotebutton.moc"