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