knotes

knote.cpp

00001 /*******************************************************************
00002  KNotes -- Notes for the KDE project
00003 
00004  Copyright (c) 1997-2006, The KNotes Developers
00005 
00006  This program is free software; you can redistribute it and/or
00007  modify it under the terms of the GNU General Public License
00008  as published by the Free Software Foundation; either version 2
00009  of the License, or (at your option) any later version.
00010 
00011  This program is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with this program; if not, write to the Free Software
00018  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 *******************************************************************/
00020 
00021 #include <qlabel.h>
00022 #include <qdrawutil.h>
00023 #include <qsize.h>
00024 #include <qsizegrip.h>
00025 #include <qbitmap.h>
00026 #include <qcursor.h>
00027 #include <qpainter.h>
00028 #include <qpaintdevicemetrics.h>
00029 #include <qsimplerichtext.h>
00030 #include <qobjectlist.h>
00031 #include <qfile.h>
00032 #include <qcheckbox.h>
00033 #include <qtimer.h>
00034 
00035 #include <kapplication.h>
00036 #include <kdebug.h>
00037 #include <kaction.h>
00038 #include <kstdaction.h>
00039 #include <kcombobox.h>
00040 #include <ktoolbar.h>
00041 #include <kpopupmenu.h>
00042 #include <kxmlguibuilder.h>
00043 #include <kxmlguifactory.h>
00044 #include <kcolordrag.h>
00045 #include <kiconeffect.h>
00046 #include <klocale.h>
00047 #include <kstandarddirs.h>
00048 #include <kmessagebox.h>
00049 #include <kfind.h>
00050 #include <kprocess.h>
00051 #include <kinputdialog.h>
00052 #include <kmdcodec.h>
00053 #include <kglobalsettings.h>
00054 #include <kfiledialog.h>
00055 #include <kio/netaccess.h>
00056 
00057 #include <libkcal/journal.h>
00058 
00059 #include "knote.h"
00060 #include "knotebutton.h"
00061 #include "knoteedit.h"
00062 #include "knoteconfig.h"
00063 #include "knotesglobalconfig.h"
00064 #include "knoteconfigdlg.h"
00065 #include "knotealarmdlg.h"
00066 #include "knotehostdlg.h"
00067 #include "knotesnetsend.h"
00068 #include "knoteprinter.h"
00069 #include "version.h"
00070 
00071 #include "pushpin.xpm"
00072 
00073 #include <kwin.h>
00074 #include <netwm.h>
00075 
00076 #include <fixx11h.h>
00077 
00078 using namespace KCal;
00079 
00080 extern Time qt_x_time;
00081 
00082 int KNote::s_ppOffset = 0;
00083 
00084 KNote::KNote( QDomDocument buildDoc, Journal *j, QWidget *parent, const char *name )
00085   : QFrame( parent, name, WStyle_Customize | WStyle_NoBorder | WDestructiveClose ),
00086     m_label( 0 ), m_pushpin( 0 ), m_fold( 0 ), m_button( 0 ), m_tool( 0 ), m_editor( 0 ),
00087     m_config( 0 ), m_journal( j ), m_find( 0 ),
00088     m_kwinConf( KSharedConfig::openConfig( "kwinrc", true ) ),
00089     m_busy( 0 ), m_deleteWhenIdle( false ), m_blockEmitDataChanged( false )
00090 {
00091     setAcceptDrops( true );
00092     actionCollection()->setWidget( this );
00093 
00094     setDOMDocument( buildDoc );
00095 
00096     // just set the name of the file to save the actions to, do NOT reparse it
00097     setXMLFile( instance()->instanceName() + "ui.rc", false, false );
00098 
00099     // if there is no title yet, use the start date if valid
00100     // (KOrganizer's journals don't have titles but a valid start date)
00101     if ( m_journal->summary().isNull() && m_journal->dtStart().isValid() )
00102     {
00103         QString s = KGlobal::locale()->formatDateTime( m_journal->dtStart() );
00104         m_journal->setSummary( s );
00105     }
00106 
00107     // create the menu items for the note - not the editor...
00108     // rename, mail, print, save as, insert date, alarm, close, delete, new note
00109     new KAction( i18n("New"), "filenew", 0,
00110         this,SLOT(slotRequestNewNote()) , actionCollection(), "new_note" );
00111     new KAction( i18n("Rename..."), "text", 0,
00112         this, SLOT(slotRename()), actionCollection(), "rename_note" );
00113     m_readOnly = new KToggleAction( i18n("Lock"), "lock" , 0,
00114         this, SLOT(slotUpdateReadOnly()), actionCollection(), "lock_note" );
00115     m_readOnly->setCheckedState( KGuiItem( i18n("Unlock"), "unlock" ) );
00116     new KAction( i18n("Hide"), "fileclose" , Key_Escape,
00117         this, SLOT(slotClose()), actionCollection(), "hide_note" );
00118     new KAction( i18n("Delete"), "knotes_delete", 0,
00119         this, SLOT(slotKill()), actionCollection(), "delete_note" );
00120 
00121     new KAction( i18n("Insert Date"), "knotes_date", 0 ,
00122         this, SLOT(slotInsDate()), actionCollection(), "insert_date" );
00123     new KAction( i18n("Set Alarm..."), "knotes_alarm", 0 ,
00124         this, SLOT(slotSetAlarm()), actionCollection(), "set_alarm" );
00125 
00126     new KAction( i18n("Send..."), "network", 0,
00127         this, SLOT(slotSend()), actionCollection(), "send_note" );
00128     new KAction( i18n("Mail..."), "mail_send", 0,
00129         this, SLOT(slotMail()), actionCollection(), "mail_note" );
00130     new KAction( i18n("Save As..."), "filesaveas", 0,
00131         this, SLOT(slotSaveAs()), actionCollection(), "save_note" );
00132     KStdAction::print( this, SLOT(slotPrint()), actionCollection(), "print_note" );
00133     new KAction( i18n("Preferences..."), "configure", 0,
00134         this, SLOT(slotPreferences()), actionCollection(), "configure_note" );
00135 
00136     m_keepAbove = new KToggleAction( i18n("Keep Above Others"), "up", 0,
00137         this, SLOT(slotUpdateKeepAboveBelow()), actionCollection(), "keep_above" );
00138     m_keepAbove->setExclusiveGroup( "keepAB" );
00139 
00140     m_keepBelow = new KToggleAction( i18n("Keep Below Others"), "down", 0,
00141         this, SLOT(slotUpdateKeepAboveBelow()), actionCollection(), "keep_below" );
00142     m_keepBelow->setExclusiveGroup( "keepAB" );
00143 
00144     m_toDesktop = new KListAction( i18n("To Desktop"), 0,
00145         this, SLOT(slotPopupActionToDesktop(int)), actionCollection(), "to_desktop" );
00146     connect( m_toDesktop->popupMenu(), SIGNAL(aboutToShow()), this, SLOT(slotUpdateDesktopActions()) );
00147 
00148     // invisible action to walk through the notes to make this configurable
00149     new KAction( i18n("Walk Through Notes"), 0, SHIFT+Key_BackTab,
00150                  this, SIGNAL(sigShowNextNote()), actionCollection(), "walk_notes" );
00151 
00152     // create the note header, button and label...
00153     m_label = new QLabel( this );
00154     m_label->setFrameStyle( NoFrame );
00155     m_label->setLineWidth( 0 );
00156     m_label->installEventFilter( this );  // receive events (for dragging & action menu)
00157     setName( m_journal->summary() );      // don't worry, no signals are connected at this stage yet
00158 
00159     m_button = new KNoteButton( "knotes_close", this );
00160     connect( m_button, SIGNAL(clicked()), this, SLOT(slotClose()) );
00161 
00162     // create the note editor
00163     m_editor = new KNoteEdit( actionCollection(), this );
00164     m_editor->setNote( this );
00165     m_editor->installEventFilter( this ); // receive events (for modified)
00166     m_editor->viewport()->installEventFilter( this );
00167     connect( m_editor, SIGNAL(contentsMoving( int, int )), this, SLOT(slotUpdateViewport( int, int )));
00168 
00169     KXMLGUIBuilder builder( this );
00170     KXMLGUIFactory factory( &builder, this );
00171     factory.addClient( this );
00172 
00173     m_menu = dynamic_cast<KPopupMenu*>(factory.container( "note_context", this ));
00174     m_edit_menu = dynamic_cast<KPopupMenu*>(factory.container( "note_edit", this ));
00175     m_tool = dynamic_cast<KToolBar*>(factory.container( "note_tool", this ));
00176 
00177     if ( m_tool ) {
00178       m_tool->setIconSize( 10 );
00179       m_tool->setFixedHeight( 16 );
00180       m_tool->setIconText( KToolBar::IconOnly );
00181 
00182       // if there was just a way of making KComboBox adhere the toolbar height...
00183       QObjectList *list = m_tool->queryList( "KComboBox" );
00184       QObjectListIt it( *list );
00185       while ( it.current() != 0 )
00186       {
00187           KComboBox *combo = (KComboBox *)it.current();
00188           QFont font = combo->font();
00189           font.setPointSize( 7 );
00190           combo->setFont( font );
00191           combo->setFixedHeight( 14 );
00192           ++it;
00193       }
00194       delete list;
00195 
00196       m_tool->hide();
00197     }
00198 
00199     setFocusProxy( m_editor );
00200 
00201     // create the resize handle
00202     m_editor->setCornerWidget( new QSizeGrip( this ) );
00203     uint width = m_editor->cornerWidget()->width();
00204     uint height = m_editor->cornerWidget()->height();
00205     QBitmap mask;
00206     mask.resize( width, height );
00207     mask.fill( color0 );
00208     QPointArray array;
00209     array.setPoints( 3, 0, height, width, height, width, 0 );
00210     QPainter p;
00211     p.begin( &mask );
00212     p.setBrush( color1 );
00213     p.drawPolygon( array );
00214     p.end();
00215     m_editor->cornerWidget()->setMask( mask );
00216     m_editor->cornerWidget()->setBackgroundMode( PaletteBase );
00217 
00218     // the config file location
00219     QString configFile = KGlobal::dirs()->saveLocation( "appdata", "notes/" );
00220     configFile += m_journal->uid();
00221 
00222     // no config file yet? -> use the default display config if available
00223     // we want to write to configFile, so use "false"
00224     bool newNote = !KIO::NetAccess::exists( KURL::fromPathOrURL( configFile ), false, 0 );
00225 
00226     m_config = new KNoteConfig( KSharedConfig::openConfig( configFile, false, false ) );
00227     m_config->readConfig();
00228     m_config->setVersion( KNOTES_VERSION );
00229 
00230     if ( newNote )
00231     {
00232         // until kdelibs provides copying of KConfigSkeletons (KDE 3.4)
00233         KNotesGlobalConfig *globalConfig = KNotesGlobalConfig::self();
00234         m_config->setBgColor( globalConfig->bgColor() );
00235         m_config->setFgColor( globalConfig->fgColor() );
00236         m_config->setWidth( globalConfig->width() );
00237         m_config->setHeight( globalConfig->height() );
00238 
00239         m_config->setFont( globalConfig->font() );
00240         m_config->setTitleFont( globalConfig->titleFont() );
00241         m_config->setAutoIndent( globalConfig->autoIndent() );
00242         m_config->setRichText( globalConfig->richText() );
00243         m_config->setTabSize( globalConfig->tabSize() );
00244         m_config->setReadOnly( globalConfig->readOnly() );
00245 
00246         m_config->setDesktop( globalConfig->desktop() );
00247         m_config->setHideNote( globalConfig->hideNote() );
00248         m_config->setPosition( globalConfig->position() );
00249         m_config->setShowInTaskbar( globalConfig->showInTaskbar() );
00250         m_config->setKeepAbove( globalConfig->keepAbove() );
00251         m_config->setKeepBelow( globalConfig->keepBelow() );
00252 
00253         m_config->writeConfig();
00254     }
00255 
00256     // set up the look&feel of the note
00257     setMinimumSize( 20, 20 );
00258     setLineWidth( 1 );
00259     setMargin( 0 );
00260 
00261     m_editor->setMargin( 0 );
00262     m_editor->setFrameStyle( NoFrame );
00263     m_editor->setBackgroundOrigin( WindowOrigin );
00264 
00265     // can be done here since this doesn't pick up changes while KNotes is running anyway
00266     bool closeLeft = false;
00267     m_kwinConf->setGroup( "Style" );
00268     if ( m_kwinConf->readBoolEntry( "CustomButtonPositions" ) )
00269         closeLeft = m_kwinConf->readEntry( "ButtonsOnLeft" ).find( 'X' ) > -1;
00270 
00271     QPixmap pushpin_pix;
00272     if ( closeLeft )
00273         pushpin_pix = QPixmap( QPixmap( pushpin_xpm ).convertToImage().mirror( true, false ) );
00274     else
00275         pushpin_pix = QPixmap( pushpin_xpm );
00276 
00277     // the pushpin label at the top left or right corner
00278     m_pushpin = new QLabel( this );
00279     m_pushpin->setScaledContents( true );
00280     m_pushpin->setBackgroundMode( NoBackground );
00281     m_pushpin->setPixmap( pushpin_pix );
00282     m_pushpin->resize( pushpin_pix.size() );
00283 
00284     // fold label at bottom right corner
00285     m_fold = new QLabel( this );
00286     m_fold->setScaledContents( true );
00287     m_fold->setBackgroundMode( NoBackground );
00288 
00289     // load the display configuration of the note
00290     width = m_config->width();
00291     height = m_config->height();
00292     resize( width, height );
00293 
00294     // let KWin do the placement if the position is illegal--at least 10 pixels
00295     // of a note need to be visible
00296     const QPoint& position = m_config->position();
00297     QRect desk = kapp->desktop()->rect();
00298     desk.addCoords( 10, 10, -10, -10 );
00299     if ( desk.intersects( QRect( position, QSize( width, height ) ) ) )
00300         move( position );           // do before calling show() to avoid flicker
00301 
00302     // config items in the journal have priority
00303     QString property = m_journal->customProperty( "KNotes", "FgColor" );
00304     if ( !property.isNull() )
00305         m_config->setFgColor( QColor( property ) );
00306     else
00307         m_journal->setCustomProperty( "KNotes", "FgColor", m_config->fgColor().name() );
00308 
00309     property = m_journal->customProperty( "KNotes", "BgColor" );
00310     if ( !property.isNull() )
00311         m_config->setBgColor( QColor( property ) );
00312     else
00313         m_journal->setCustomProperty( "KNotes", "BgColor", m_config->bgColor().name() );
00314 
00315     property = m_journal->customProperty( "KNotes", "RichText" );
00316     if ( !property.isNull() )
00317         m_config->setRichText( property == "true" ? true : false );
00318     else
00319         m_journal->setCustomProperty( "KNotes", "RichText", m_config->richText() ? "true" : "false" );
00320 
00321     // read configuration settings...
00322     slotApplyConfig();
00323 
00324     // create the mask for the fold---to be done after slotApplyConfig(),
00325     // which calls createFold()
00326     m_fold->setMask( QRegion( m_fold->pixmap()->createHeuristicMask() ) );
00327 
00328     // if this is a new note put on current desktop - we can't use defaults
00329     // in KConfig XT since only _changes_ will be stored in the config file
00330     int desktop = m_config->desktop();
00331     if ( desktop < 0 && desktop != NETWinInfo::OnAllDesktops )
00332         desktop = KWin::currentDesktop();
00333 
00334     // show the note if desired
00335     if ( desktop != 0 && !m_config->hideNote() )
00336     {
00337         // to avoid flicker, call this before show()
00338         toDesktop( desktop );
00339         show();
00340 
00341         // because KWin forgets about that for hidden windows
00342         if ( desktop == NETWinInfo::OnAllDesktops )
00343             toDesktop( desktop );
00344     }
00345 
00346     m_editor->setText( m_journal->description() );
00347     m_editor->setModified( false );
00348 
00349     m_readOnly->setChecked( m_config->readOnly() );
00350     slotUpdateReadOnly();
00351 
00352     if ( m_config->keepAbove() )
00353         m_keepAbove->setChecked( true );
00354     else if ( m_config->keepBelow() )
00355         m_keepBelow->setChecked( true );
00356     else
00357     {
00358         m_keepAbove->setChecked( false );
00359         m_keepBelow->setChecked( false );
00360     }
00361     slotUpdateKeepAboveBelow();
00362 
00363     // HACK: update the icon color - again after showing the note, to make kicker aware of the new colors
00364     KIconEffect effect;
00365     QPixmap icon = effect.apply( kapp->icon(), KIconEffect::Colorize, 1, m_config->bgColor(), false );
00366     QPixmap miniIcon = effect.apply( kapp->miniIcon(), KIconEffect::Colorize, 1, m_config->bgColor(), false );
00367     KWin::setIcons( winId(), icon, miniIcon );
00368 }
00369 
00370 KNote::~KNote()
00371 {
00372     delete m_config;
00373 }
00374 
00375 void KNote::slotRequestNewNote()
00376 {
00377     //Be sure to save before to request a new note
00378     saveConfig();
00379     saveData();
00380     emit sigRequestNewNote();
00381 }
00382 
00383 void KNote::changeJournal(KCal::Journal *journal)
00384 {
00385    m_journal = journal;
00386    m_editor->setText( m_journal->description() );
00387    m_label->setText( m_journal->summary() );
00388    updateLabelAlignment();
00389 }
00390 
00391 // -------------------- public slots -------------------- //
00392 
00393 void KNote::slotKill( bool force )
00394 {
00395     m_blockEmitDataChanged = true;
00396     if ( !force &&
00397          KMessageBox::warningContinueCancel( this,
00398              i18n("<qt>Do you really want to delete note <b>%1</b>?</qt>").arg( m_label->text() ),
00399              i18n("Confirm Delete"), KGuiItem( i18n("&Delete"), "editdelete" ),
00400              "ConfirmDeleteNote"
00401          )
00402          != KMessageBox::Continue )
00403     {
00404     m_blockEmitDataChanged = false;
00405         return;
00406     }
00407     aboutToEnterEventLoop();
00408     // delete the configuration first, then the corresponding file
00409     delete m_config;
00410     m_config = 0;
00411 
00412     QString configFile = KGlobal::dirs()->saveLocation( "appdata", "notes/" );
00413     configFile += m_journal->uid();
00414 
00415     if ( !KIO::NetAccess::del( KURL::fromPathOrURL( configFile ), this ) )
00416         kdError(5500) << "Can't remove the note config: " << configFile << endl;
00417 
00418     emit sigKillNote( m_journal );
00419     eventLoopLeft();
00420 
00421 }
00422 
00423 
00424 // -------------------- public member functions -------------------- //
00425 
00426 void KNote::saveData(bool update)
00427 {
00428     m_journal->setSummary( m_label->text() );
00429     m_journal->setDescription( m_editor->text() );
00430     m_journal->setCustomProperty( "KNotes", "FgColor", m_config->fgColor().name() );
00431     m_journal->setCustomProperty( "KNotes", "BgColor", m_config->bgColor().name() );
00432     m_journal->setCustomProperty( "KNotes", "RichText", m_config->richText() ? "true" : "false" );
00433     if(update) {
00434     emit sigDataChanged( noteId() );
00435     m_editor->setModified( false );
00436     }
00437 }
00438 
00439 void KNote::saveConfig() const
00440 {
00441     m_config->setWidth( width() );
00442     if ( m_tool )
00443       m_config->setHeight( height() - (m_tool->isHidden() ? 0 : m_tool->height()) );
00444     else
00445       m_config->setHeight( 0 );
00446     m_config->setPosition( pos() );
00447 
00448     NETWinInfo wm_client( qt_xdisplay(), winId(), qt_xrootwin(), NET::WMDesktop );
00449     if ( wm_client.desktop() == NETWinInfo::OnAllDesktops || wm_client.desktop() > 0 )
00450         m_config->setDesktop( wm_client.desktop() );
00451 
00452     // actually store the config on disk
00453     m_config->writeConfig();
00454 }
00455 
00456 QString KNote::noteId() const
00457 {
00458     return m_journal->uid();
00459 }
00460 
00461 QString KNote::name() const
00462 {
00463     return m_label->text();
00464 }
00465 
00466 QString KNote::text() const
00467 {
00468     return m_editor->text();
00469 }
00470 
00471 QString KNote::plainText() const
00472 {
00473     if ( m_editor->textFormat() == RichText )
00474     {
00475         QTextEdit conv;
00476         conv.setTextFormat( RichText );
00477         conv.setText( m_editor->text() );
00478         conv.setTextFormat( PlainText );
00479         return conv.text();
00480     }
00481     else
00482         return m_editor->text();
00483 }
00484 
00485 void KNote::setName( const QString& name )
00486 {
00487     m_label->setText( name );
00488     updateLabelAlignment();
00489 
00490     if ( m_editor )    // not called from CTOR?
00491         saveData();
00492 
00493     // set the window's name for the taskbar entry to be more helpful (#58338)
00494     NETWinInfo note_win( qt_xdisplay(), winId(), qt_xrootwin(), NET::WMDesktop );
00495     note_win.setName( name.utf8() );
00496 
00497     emit sigNameChanged();
00498 }
00499 
00500 void KNote::setText( const QString& text )
00501 {
00502     m_editor->setText( text );
00503     saveData();
00504 }
00505 
00506 QColor KNote::fgColor() const
00507 {
00508     return m_config->fgColor();
00509 }
00510 
00511 QColor KNote::bgColor() const
00512 {
00513     return m_config->bgColor();
00514 }
00515 
00516 void KNote::setColor( const QColor& fg, const QColor& bg )
00517 {
00518     m_journal->setCustomProperty( "KNotes", "FgColor", fg.name() );
00519     m_journal->setCustomProperty( "KNotes", "BgColor", bg.name() );
00520     m_config->setFgColor( fg );
00521     m_config->setBgColor( bg );
00522 
00523     m_journal->updated();  // because setCustomProperty() doesn't call it!!
00524     emit sigDataChanged(noteId());
00525     m_config->writeConfig();
00526 
00527     QPalette newpalette = palette();
00528     newpalette.setColor( QColorGroup::Background, bg );
00529     newpalette.setColor( QColorGroup::Foreground, fg );
00530     newpalette.setColor( QColorGroup::Base,       bg ); // text background
00531     newpalette.setColor( QColorGroup::Text,       fg ); // text color
00532     newpalette.setColor( QColorGroup::Button,     bg );
00533     newpalette.setColor( QColorGroup::ButtonText, fg );
00534 
00535 //    newpalette.setColor( QColorGroup::Highlight,  bg );
00536 //    newpalette.setColor( QColorGroup::HighlightedText, fg );
00537 
00538     // the shadow
00539     newpalette.setColor( QColorGroup::Midlight, bg.light(150) );
00540     newpalette.setColor( QColorGroup::Shadow, bg.dark(116) );
00541     newpalette.setColor( QColorGroup::Light, bg.light(180) );
00542     if ( s_ppOffset )
00543         newpalette.setColor( QColorGroup::Dark, bg.dark(200) );
00544     else
00545         newpalette.setColor( QColorGroup::Dark, bg.dark(108) );
00546     setPalette( newpalette );
00547 
00548     // set the text color
00549     m_editor->setTextColor( fg );
00550 
00551     // set the background color or gradient
00552     updateBackground();
00553 
00554     // set darker value for the hide button...
00555     QPalette darker = palette();
00556     darker.setColor( QColorGroup::Button, bg.dark(116) );
00557     m_button->setPalette( darker );
00558 
00559     // update the icon color
00560     KIconEffect effect;
00561     QPixmap icon = effect.apply( kapp->icon(), KIconEffect::Colorize, 1, bg, false );
00562     QPixmap miniIcon = effect.apply( kapp->miniIcon(), KIconEffect::Colorize, 1, bg, false );
00563     KWin::setIcons( winId(), icon, miniIcon );
00564 
00565     // set the color for the selection used to highlight the find stuff
00566     QColor sel = palette().color( QPalette::Active, QColorGroup::Base ).dark();
00567     if ( sel == Qt::black )
00568         sel = palette().color( QPalette::Active, QColorGroup::Base ).light();
00569 
00570     m_editor->setSelectionAttributes( 1, sel, true );
00571 
00572     // update the color of the fold
00573     createFold();
00574 
00575     // update the color of the title
00576     updateFocus();
00577     emit sigColorChanged();
00578 }
00579 
00580 void KNote::find( const QString& pattern, long options )
00581 {
00582     delete m_find;
00583     m_find = new KFind( pattern, options, this );
00584 
00585     connect( m_find, SIGNAL(highlight( const QString &, int, int )),
00586              this, SLOT(slotHighlight( const QString &, int, int )) );
00587     connect( m_find, SIGNAL(findNext()), this, SLOT(slotFindNext()) );
00588 
00589     m_find->setData( plainText() );
00590     slotFindNext();
00591 }
00592 
00593 void KNote::slotFindNext()
00594 {
00595     // TODO: honor FindBackwards
00596     // TODO: dialogClosed() -> delete m_find
00597 
00598     // Let KFind inspect the text fragment, and display a dialog if a match is found
00599     KFind::Result res = m_find->find();
00600 
00601     if ( res == KFind::NoMatch ) // i.e. at end-pos
00602     {
00603         m_editor->removeSelection( 1 );
00604         emit sigFindFinished();
00605         delete m_find;
00606         m_find = 0;
00607     }
00608     else
00609     {
00610         show();
00611         KWin::setCurrentDesktop( KWin::windowInfo( winId() ).desktop() );
00612     }
00613 }
00614 
00615 void KNote::slotHighlight( const QString& str, int idx, int len )
00616 {
00617     int paraFrom = 0, idxFrom = 0, p = 0;
00618     for ( ; p < idx; ++p )
00619         if ( str[p] == '\n' )
00620         {
00621             ++paraFrom;
00622             idxFrom = 0;
00623         }
00624         else
00625             ++idxFrom;
00626 
00627     int paraTo = paraFrom, idxTo = idxFrom;
00628 
00629     for ( ; p < idx + len; ++p )
00630     {
00631         if ( str[p] == '\n' )
00632         {
00633             ++paraTo;
00634             idxTo = 0;
00635         }
00636         else
00637             ++idxTo;
00638     }
00639 
00640     m_editor->setSelection( paraFrom, idxFrom, paraTo, idxTo, 1 );
00641 }
00642 
00643 bool KNote::isModified() const
00644 {
00645     return m_editor->isModified();
00646 }
00647 
00648 // FIXME KDE 4.0: remove sync(), isNew() and isModified()
00649 void KNote::sync( const QString& app )
00650 {
00651     QByteArray sep( 1 );
00652     sep[0] = '\0';
00653 
00654     KMD5 hash;
00655     QCString result;
00656 
00657     hash.update( m_label->text().utf8() );
00658     hash.update( sep );
00659     hash.update( m_editor->text().utf8() );
00660     hash.hexDigest( result );
00661 
00662     // hacky... not possible with KConfig XT
00663     KConfig *config = m_config->config();
00664     config->setGroup( "Synchronisation" );
00665     config->writeEntry( app, result.data() );
00666 }
00667 
00668 bool KNote::isNew( const QString& app ) const
00669 {
00670     KConfig *config = m_config->config();
00671     config->setGroup( "Synchronisation" );
00672     QString hash = config->readEntry( app );
00673     return hash.isEmpty();
00674 }
00675 
00676 bool KNote::isModified( const QString& app ) const
00677 {
00678     QByteArray sep( 1 );
00679     sep[0] = '\0';
00680 
00681     KMD5 hash;
00682     hash.update( m_label->text().utf8() );
00683     hash.update( sep );
00684     hash.update( m_editor->text().utf8() );
00685     hash.hexDigest();
00686 
00687     KConfig *config = m_config->config();
00688     config->setGroup( "Synchronisation" );
00689     QString orig = config->readEntry( app );
00690 
00691     if ( hash.verify( orig.utf8() ) )   // returns false on error!
00692         return false;
00693     else
00694         return true;
00695 }
00696 
00697 void KNote::setStyle( int style )
00698 {
00699     if ( style == KNotesGlobalConfig::EnumStyle::Plain )
00700         s_ppOffset = 0;
00701     else
00702         s_ppOffset = 12;
00703 }
00704 
00705 
00706 // ------------------ private slots (menu actions) ------------------ //
00707 
00708 void KNote::slotRename()
00709 {
00710     m_blockEmitDataChanged = true;
00711     // pop up dialog to get the new name
00712     bool ok;
00713     aboutToEnterEventLoop();
00714     QString oldName = m_label->text();
00715     QString newName = KInputDialog::getText( QString::null,
00716         i18n("Please enter the new name:"), m_label->text(), &ok, this );
00717     eventLoopLeft();
00718     m_blockEmitDataChanged = false;
00719     if ( !ok || ( oldName == newName) ) // handle cancel
00720         return;
00721 
00722     setName( newName );
00723 }
00724 
00725 void KNote::slotUpdateReadOnly()
00726 {
00727     const bool readOnly = m_readOnly->isChecked();
00728 
00729     m_editor->setReadOnly( readOnly );
00730     m_config->setReadOnly( readOnly );
00731 
00732     // Enable/disable actions accordingly
00733     actionCollection()->action( "configure_note" )->setEnabled( !readOnly );
00734     actionCollection()->action( "insert_date" )->setEnabled( !readOnly );
00735     actionCollection()->action( "delete_note" )->setEnabled( !readOnly );
00736 
00737     actionCollection()->action( "edit_undo" )->setEnabled( !readOnly && m_editor->isUndoAvailable() );
00738     actionCollection()->action( "edit_redo" )->setEnabled( !readOnly && m_editor->isRedoAvailable() );
00739     actionCollection()->action( "edit_cut" )->setEnabled( !readOnly && m_editor->hasSelectedText() );
00740     actionCollection()->action( "edit_paste" )->setEnabled( !readOnly );
00741     actionCollection()->action( "edit_clear" )->setEnabled( !readOnly );
00742     actionCollection()->action( "rename_note" )->setEnabled( !readOnly );
00743 
00744     actionCollection()->action( "format_bold" )->setEnabled( !readOnly );
00745     actionCollection()->action( "format_italic" )->setEnabled( !readOnly );
00746     actionCollection()->action( "format_underline" )->setEnabled( !readOnly );
00747     actionCollection()->action( "format_strikeout" )->setEnabled( !readOnly );
00748     actionCollection()->action( "format_alignleft" )->setEnabled( !readOnly );
00749     actionCollection()->action( "format_aligncenter" )->setEnabled( !readOnly );
00750     actionCollection()->action( "format_alignright" )->setEnabled( !readOnly );
00751     actionCollection()->action( "format_alignblock" )->setEnabled( !readOnly );
00752     actionCollection()->action( "format_list" )->setEnabled( !readOnly );
00753     actionCollection()->action( "format_super" )->setEnabled( !readOnly );
00754     actionCollection()->action( "format_sub" )->setEnabled( !readOnly );
00755     actionCollection()->action( "format_size" )->setEnabled( !readOnly );
00756     actionCollection()->action( "format_color" )->setEnabled( !readOnly );
00757 
00758     updateFocus();
00759 }
00760 
00761 void KNote::slotClose()
00762 {
00763     NETWinInfo wm_client( qt_xdisplay(), winId(), qt_xrootwin(), NET::WMDesktop );
00764     if ( wm_client.desktop() == NETWinInfo::OnAllDesktops || wm_client.desktop() > 0 )
00765         m_config->setDesktop( wm_client.desktop() );
00766 
00767     m_editor->clearFocus();
00768     m_config->setHideNote( true );
00769     m_config->setPosition( pos() );
00770     m_config->writeConfig();
00771     // just hide the note so it's still available from the dock window
00772     hide();
00773 }
00774 
00775 void KNote::slotInsDate()
00776 {
00777     m_editor->insert( KGlobal::locale()->formatDateTime(QDateTime::currentDateTime()) );
00778 }
00779 
00780 void KNote::slotSetAlarm()
00781 {
00782     m_blockEmitDataChanged = true;
00783     KNoteAlarmDlg dlg( name(), this );
00784     dlg.setIncidence( m_journal );
00785 
00786     aboutToEnterEventLoop();
00787     if ( dlg.exec() == QDialog::Accepted )
00788         emit sigDataChanged(noteId());
00789     eventLoopLeft();
00790     m_blockEmitDataChanged = false;
00791 }
00792 
00793 void KNote::slotPreferences()
00794 {
00795     // reuse if possible
00796     if ( KNoteConfigDlg::showDialog( noteId().utf8() ) )
00797         return;
00798 
00799     // create a new preferences dialog...
00800     KNoteConfigDlg *dialog = new KNoteConfigDlg( m_config, name(), this, noteId().utf8() );
00801     connect( dialog, SIGNAL(settingsChanged()), this, SLOT(slotApplyConfig()) );
00802     connect( this, SIGNAL(sigNameChanged()), dialog, SLOT(slotUpdateCaption()) );
00803     dialog->show();
00804 }
00805 
00806 void KNote::slotSend()
00807 {
00808     // pop up dialog to get the IP
00809     KNoteHostDlg hostDlg( i18n("Send \"%1\"").arg( name() ), this );
00810     aboutToEnterEventLoop();
00811     bool ok = (hostDlg.exec() == QDialog::Accepted);
00812     eventLoopLeft();
00813     if ( !ok ) // handle cancel
00814         return;
00815     QString host = hostDlg.host();
00816 
00817     if ( host.isEmpty() )
00818     {
00819         KMessageBox::sorry( this, i18n("The host cannot be empty.") );
00820         return;
00821     }
00822 
00823     // Send the note
00824     KNotesNetworkSender *sender = new KNotesNetworkSender( host, KNotesGlobalConfig::port() );
00825     sender->setSenderId( KNotesGlobalConfig::senderID() );
00826     sender->setNote( name(), text() );
00827     sender->connect();
00828 }
00829 
00830 void KNote::slotMail()
00831 {
00832     // get the mail action command
00833     const QStringList cmd_list = QStringList::split( QChar(' '), KNotesGlobalConfig::mailAction() );
00834 
00835     KProcess mail;
00836     for ( QStringList::ConstIterator it = cmd_list.constBegin();
00837         it != cmd_list.constEnd(); ++it )
00838     {
00839         if ( *it == "%f" )
00840             mail << plainText().local8Bit();  // convert rich text to plain text
00841         else if ( *it == "%t" )
00842             mail << m_label->text().local8Bit();
00843         else
00844             mail << (*it).local8Bit();
00845     }
00846 
00847     if ( !mail.start( KProcess::DontCare ) )
00848         KMessageBox::sorry( this, i18n("Unable to start the mail process.") );
00849 }
00850 
00851 void KNote::slotPrint()
00852 {
00853     QString content;
00854     if ( m_editor->textFormat() == PlainText )
00855         content = QStyleSheet::convertFromPlainText( m_editor->text() );
00856     else
00857         content = m_editor->text();
00858 
00859     KNotePrinter printer;
00860     printer.setMimeSourceFactory( m_editor->mimeSourceFactory() );
00861     printer.setFont( m_config->font() );
00862     printer.setContext( m_editor->context() );
00863     printer.setStyleSheet( m_editor->styleSheet() );
00864     printer.setColorGroup( colorGroup() );
00865     printer.printNote( QString(), content );
00866 }
00867 
00868 void KNote::slotSaveAs()
00869 {
00870     m_blockEmitDataChanged = true;
00871     QCheckBox *convert = 0;
00872 
00873     if ( m_editor->textFormat() == RichText )
00874     {
00875         convert = new QCheckBox( 0 );
00876         convert->setText( i18n("Save note as plain text") );
00877     }
00878 
00879     KFileDialog dlg( QString::null, QString::null, this, "filedialog", true, convert );
00880     dlg.setOperationMode( KFileDialog::Saving );
00881     dlg.setCaption( i18n("Save As") );
00882     aboutToEnterEventLoop();
00883     dlg.exec();
00884     eventLoopLeft();
00885 
00886     QString fileName = dlg.selectedFile();
00887     if ( fileName.isEmpty() )
00888     {
00889     m_blockEmitDataChanged = false;
00890         return;
00891     }
00892     QFile file( fileName );
00893 
00894     if ( file.exists() &&
00895          KMessageBox::warningContinueCancel( this, i18n("<qt>A file named <b>%1</b> already exists.<br>"
00896                            "Are you sure you want to overwrite it?</qt>").arg( QFileInfo(file).fileName() ) )
00897          != KMessageBox::Continue )
00898     {
00899     m_blockEmitDataChanged = false;
00900         return;
00901     }
00902 
00903     if ( file.open( IO_WriteOnly ) )
00904     {
00905         QTextStream stream( &file );
00906         // convert rich text to plain text first
00907         if ( convert && convert->isChecked() )
00908             stream << plainText();
00909         else
00910             stream << text();
00911     }
00912     m_blockEmitDataChanged = false;
00913 }
00914 
00915 void KNote::slotPopupActionToDesktop( int id )
00916 {
00917     toDesktop( id - 1 ); // compensate for the menu separator, -1 == all desktops
00918 }
00919 
00920 
00921 // ------------------ private slots (configuration) ------------------ //
00922 
00923 void KNote::slotApplyConfig()
00924 {
00925     if ( m_config->richText() )
00926         m_editor->setTextFormat( RichText );
00927     else
00928         m_editor->setTextFormat( PlainText );
00929 
00930     m_label->setFont( m_config->titleFont() );
00931     m_editor->setTextFont( m_config->font() );
00932     m_editor->setTabStop( m_config->tabSize() );
00933     m_editor->setAutoIndentMode( m_config->autoIndent() );
00934 
00935     // if called as a slot, save the text, we might have changed the
00936     // text format - otherwise the journal will not be updated
00937     if ( sender() )
00938         saveData();
00939 
00940     setColor( m_config->fgColor(), m_config->bgColor() );
00941 
00942     updateLabelAlignment();
00943     slotUpdateShowInTaskbar();
00944 }
00945 
00946 void KNote::slotUpdateKeepAboveBelow()
00947 {
00948     KWin::WindowInfo info( KWin::windowInfo( winId() ) );
00949 
00950     if ( m_keepAbove->isChecked() )
00951     {
00952         m_config->setKeepAbove( true );
00953         m_config->setKeepBelow( false );
00954         KWin::setState( winId(), info.state() | NET::KeepAbove );
00955     }
00956     else if ( m_keepBelow->isChecked() )
00957     {
00958         m_config->setKeepAbove( false );
00959         m_config->setKeepBelow( true );
00960         KWin::setState( winId(), info.state() | NET::KeepBelow );
00961     }
00962     else
00963     {
00964         m_config->setKeepAbove( false );
00965         KWin::clearState( winId(), NET::KeepAbove );
00966 
00967         m_config->setKeepBelow( false );
00968         KWin::clearState( winId(), NET::KeepBelow );
00969     }
00970 }
00971 
00972 void KNote::slotUpdateShowInTaskbar()
00973 {
00974     if ( !m_config->showInTaskbar() )
00975         KWin::setState( winId(), KWin::windowInfo(winId()).state() | NET::SkipTaskbar );
00976     else
00977         KWin::clearState( winId(), NET::SkipTaskbar );
00978 }
00979 
00980 void KNote::slotUpdateDesktopActions()
00981 {
00982     NETRootInfo wm_root( qt_xdisplay(), NET::NumberOfDesktops | NET::DesktopNames );
00983     NETWinInfo wm_client( qt_xdisplay(), winId(), qt_xrootwin(), NET::WMDesktop );
00984 
00985     QStringList desktops;
00986     desktops.append( i18n("&All Desktops") );
00987     desktops.append( QString::null );           // Separator
00988 
00989     int count = wm_root.numberOfDesktops();
00990     for ( int n = 1; n <= count; n++ )
00991         desktops.append( QString("&%1 %2").arg( n ).arg( QString::fromUtf8(wm_root.desktopName( n )) ) );
00992 
00993     m_toDesktop->setItems( desktops );
00994 
00995     if ( wm_client.desktop() == NETWinInfo::OnAllDesktops )
00996         m_toDesktop->setCurrentItem( 0 );
00997     else
00998         m_toDesktop->setCurrentItem( wm_client.desktop() + 1 ); // compensate for separator (+1)
00999 }
01000 
01001 void KNote::slotUpdateViewport( int /*x*/, int y )
01002 {
01003     if ( s_ppOffset )
01004         updateBackground( y );
01005 }
01006 
01007 // -------------------- private methods -------------------- //
01008 
01009 void KNote::toDesktop( int desktop )
01010 {
01011     if ( desktop == 0 )
01012         return;
01013 
01014     if ( desktop == NETWinInfo::OnAllDesktops )
01015         KWin::setOnAllDesktops( winId(), true );
01016     else
01017         KWin::setOnDesktop( winId(), desktop );
01018 }
01019 
01020 void KNote::createFold()
01021 {
01022     QPixmap fold( 15, 15 );
01023     QPainter foldp( &fold );
01024     foldp.setPen( Qt::NoPen );
01025     foldp.setBrush( palette().active().dark() );
01026     QPointArray foldpoints( 3 );
01027     foldpoints.putPoints( 0, 3, 0, 0, 14, 0, 0, 14 );
01028     foldp.drawPolygon( foldpoints );
01029     foldp.end();
01030     m_fold->setPixmap( fold );
01031 }
01032 
01033 void KNote::updateLabelAlignment()
01034 {
01035     // if the name is too long to fit, left-align it, otherwise center it (#59028)
01036     QString labelText = m_label->text();
01037     if ( m_label->fontMetrics().boundingRect( labelText ).width() > m_label->width() )
01038         m_label->setAlignment( AlignLeft );
01039     else
01040         m_label->setAlignment( AlignHCenter );
01041 }
01042 
01043 void KNote::updateFocus()
01044 {
01045     if ( hasFocus() )
01046     {
01047         m_label->setBackgroundColor( palette().active().shadow() );
01048         m_button->show();
01049 
01050         if ( !m_editor->isReadOnly() )
01051         {
01052             if ( m_tool && m_tool->isHidden() && m_editor->textFormat() == QTextEdit::RichText )
01053             {
01054                 m_tool->show();
01055         m_editor->cornerWidget()->show();
01056                 setGeometry( x(), y(), width(), height() + m_tool->height() );
01057             }
01058         }
01059         else if ( m_tool && !m_tool->isHidden() )
01060         {
01061             m_tool->hide();
01062         m_editor->cornerWidget()->hide();
01063             setGeometry( x(), y(), width(), height() - m_tool->height() );
01064             updateLayout();     // to update the minimum height
01065         }
01066 
01067         m_fold->hide();
01068     }
01069     else
01070     {
01071         m_button->hide();
01072         m_editor->cornerWidget()->hide();
01073 
01074         if ( m_tool && !m_tool->isHidden() )
01075         {
01076             m_tool->hide();
01077             setGeometry( x(), y(), width(), height() - m_tool->height() );
01078             updateLayout();     // to update the minimum height
01079         }
01080 
01081         if ( s_ppOffset )
01082         {
01083             m_label->setBackgroundColor( palette().active().midlight() );
01084             m_fold->show();
01085         }
01086         else
01087             m_label->setBackgroundColor( palette().active().background() );
01088     }
01089 }
01090 
01091 void KNote::updateMask()
01092 {
01093     if ( !s_ppOffset )
01094     {
01095         clearMask();
01096         return;
01097     }
01098 
01099     int w = width();
01100     int h = height();
01101     QRegion reg( 0, s_ppOffset, w, h - s_ppOffset );
01102 
01103     const QBitmap *pushpin_bitmap = m_pushpin->pixmap()->mask();
01104     QRegion pushpin_reg( *pushpin_bitmap );
01105     m_pushpin->setMask( pushpin_reg );
01106     pushpin_reg.translate( m_pushpin->x(), m_pushpin->y() );
01107 
01108     if ( !hasFocus() )
01109     {
01110         QPointArray foldpoints( 3 );
01111         foldpoints.putPoints( 0, 3, w-15, h, w, h-15, w, h );
01112         QRegion fold( foldpoints, false );
01113         setMask( reg.unite( pushpin_reg ).subtract( fold ) );
01114     }
01115     else
01116         setMask( reg.unite( pushpin_reg ) );
01117 }
01118 
01119 void KNote::updateBackground( int y_offset )
01120 {
01121     if ( !s_ppOffset )
01122     {
01123         m_editor->setPaper( QBrush( colorGroup().background() ) );
01124         return;
01125     }
01126 
01127     int w = m_editor->visibleWidth();
01128     int h = m_editor->visibleHeight();
01129 
01130     // in case y_offset is not set, calculate y_offset as the content
01131     // y-coordinate of the top-left point of the viewport - which is essentially
01132     // the vertical scroll amount
01133     if ( y_offset == -1 )
01134         y_offset = m_editor->contentsY();
01135 
01136     y_offset = y_offset % h;
01137 
01138     QImage grad_img( w, h, 32 );
01139     QRgb rgbcol;
01140     QColor bg = palette().active().background();
01141 
01142     for ( int i = 0; i < h; ++i )
01143     {
01144         // if the scrollbar has moved, then adjust the gradient by the amount the
01145         // scrollbar moved -- so that the background gradient looks ok when tiled
01146 
01147         // the lightness is calculated as follows:
01148         // if i >= y, then lightness = 150 - (i-y)*75/h;
01149         // if i < y, then lightness = 150 - (i+h-y)*75/h
01150 
01151         int i_1 = 150 - 75 * ((i - y_offset + h) % h) / h;
01152         rgbcol = bg.light( i_1 ).rgb();
01153         for ( int j = 0; j < w; ++j )
01154             grad_img.setPixel( j, i, rgbcol );
01155     }
01156 
01157     // setPaletteBackgroundPixmap makes QTextEdit::color() stop working!!
01158     m_editor->setPaper( QBrush( Qt::black, QPixmap( grad_img ) ) );
01159 }
01160 
01161 void KNote::updateLayout()
01162 {
01163     const int headerHeight = m_label->sizeHint().height();
01164     const int margin = m_editor->margin();
01165     bool closeLeft = false;
01166 
01167     m_kwinConf->setGroup( "Style" );
01168     if ( m_kwinConf->readBoolEntry( "CustomButtonPositions" ) )
01169         closeLeft = m_kwinConf->readEntry( "ButtonsOnLeft" ).find( 'X' ) > -1;
01170 
01171     if ( s_ppOffset )
01172     {
01173         if ( !m_editor->paper().pixmap() )  // just changed the style
01174             setColor( palette().active().foreground(), palette().active().background() );
01175 
01176         m_pushpin->show();
01177         setFrameStyle( Panel | Raised );
01178 
01179         if ( closeLeft )
01180             m_pushpin->move( width() - m_pushpin->width(), 0 );
01181         else
01182             m_pushpin->move( 0, 0 );
01183     }
01184     else
01185     {
01186         if ( m_editor->paper().pixmap() )  // just changed the style
01187             setColor( palette().active().foreground(), palette().active().background() );
01188 
01189         setFrameStyle( WinPanel | Raised );
01190         m_pushpin->hide();
01191         m_fold->hide();
01192     }
01193 
01194     m_button->setGeometry(
01195         closeLeft ? contentsRect().x() : contentsRect().width() - headerHeight,
01196         contentsRect().y() + s_ppOffset,
01197         headerHeight,
01198         headerHeight
01199     );
01200 
01201     m_label->setGeometry(
01202         contentsRect().x(), contentsRect().y() + s_ppOffset,
01203         contentsRect().width(), headerHeight
01204     );
01205 
01206     m_editor->setGeometry( QRect(
01207         QPoint( contentsRect().x(),
01208                 contentsRect().y() + headerHeight + s_ppOffset ),
01209         QPoint( contentsRect().right(),
01210                 contentsRect().bottom() - ( m_tool ? (m_tool->isHidden() ? 0 : m_tool->height()) : 0 ) )
01211     ) );
01212 
01213     if( m_tool ) {
01214       m_tool->setGeometry(
01215           contentsRect().x(),
01216           contentsRect().bottom() - m_tool->height() + 1,
01217           contentsRect().width(),
01218           m_tool->height()
01219       );
01220     }
01221 
01222     if ( s_ppOffset )
01223         m_fold->move( width() - 15, height() - 15 );
01224 
01225     setMinimumSize(
01226         m_editor->cornerWidget()->width() + margin*2,
01227         headerHeight + s_ppOffset + ( m_tool ? (m_tool->isHidden() ? 0 : m_tool->height() ) : 0 ) +
01228                 m_editor->cornerWidget()->height() + margin*2
01229     );
01230 
01231     updateLabelAlignment();
01232     updateMask();
01233     updateBackground();
01234 }
01235 
01236 // -------------------- protected methods -------------------- //
01237 
01238 void KNote::drawFrame( QPainter *p )
01239 {
01240     QRect r = frameRect();
01241     r.setTop( s_ppOffset );
01242     if ( s_ppOffset )
01243         qDrawShadePanel( p, r, colorGroup(), false, lineWidth() );
01244     else
01245         qDrawWinPanel( p, r, colorGroup(), false );
01246 }
01247 
01248 void KNote::showEvent( QShowEvent * )
01249 {
01250     if ( m_config->hideNote() )
01251     {
01252         // KWin does not preserve these properties for hidden windows
01253         slotUpdateKeepAboveBelow();
01254         slotUpdateShowInTaskbar();
01255         toDesktop( m_config->desktop() );
01256         move( m_config->position() );
01257         m_config->setHideNote( false );
01258     }
01259 }
01260 
01261 void KNote::resizeEvent( QResizeEvent *qre )
01262 {
01263     QFrame::resizeEvent( qre );
01264     updateLayout();
01265 }
01266 
01267 void KNote::closeEvent( QCloseEvent *event )
01268 {
01269     event->ignore(); //We don't want to close (and delete the widget). Just hide it
01270     slotClose();
01271 }
01272 
01273 void KNote::dragEnterEvent( QDragEnterEvent *e )
01274 {
01275     if ( !m_config->readOnly() )
01276         e->accept( KColorDrag::canDecode( e ) );
01277 }
01278 
01279 void KNote::dropEvent( QDropEvent *e )
01280 {
01281     if ( m_config->readOnly() )
01282         return;
01283 
01284     QColor bg;
01285     if ( KColorDrag::decode( e, bg ) )
01286         setColor( paletteForegroundColor(), bg );
01287 }
01288 
01289 bool KNote::focusNextPrevChild( bool )
01290 {
01291     return true;
01292 }
01293 
01294 bool KNote::event( QEvent *ev )
01295 {
01296     if ( ev->type() == QEvent::LayoutHint )
01297     {
01298         updateLayout();
01299         return true;
01300     }
01301     else
01302         return QFrame::event( ev );
01303 }
01304 
01305 bool KNote::eventFilter( QObject *o, QEvent *ev )
01306 {
01307     if ( ev->type() == QEvent::DragEnter &&
01308          KColorDrag::canDecode( static_cast<QDragEnterEvent *>(ev) ) )
01309     {
01310         dragEnterEvent( static_cast<QDragEnterEvent *>(ev) );
01311         return true;
01312     }
01313 
01314     if ( ev->type() == QEvent::Drop &&
01315          KColorDrag::canDecode( static_cast<QDropEvent *>(ev) ) )
01316     {
01317         dropEvent( static_cast<QDropEvent *>(ev) );
01318         return true;
01319     }
01320 
01321     if ( o == m_label )
01322     {
01323         QMouseEvent *e = (QMouseEvent *)ev;
01324 
01325         if ( ev->type() == QEvent::MouseButtonDblClick )
01326     {
01327         if( !m_editor->isReadOnly())
01328                slotRename();
01329         }
01330         if ( ev->type() == QEvent::MouseButtonPress &&
01331              (e->button() == LeftButton || e->button() == MidButton))
01332         {
01333             e->button() == LeftButton ? KWin::raiseWindow( winId() )
01334                                       : KWin::lowerWindow( winId() );
01335 
01336             XUngrabPointer( qt_xdisplay(), qt_x_time );
01337             NETRootInfo wm_root( qt_xdisplay(), NET::WMMoveResize );
01338             wm_root.moveResizeRequest( winId(), e->globalX(), e->globalY(), NET::Move );
01339             return true;
01340         }
01341 
01342 #if KDE_IS_VERSION( 3, 5, 1 )
01343         if ( ev->type() == QEvent::MouseButtonRelease )
01344         {
01345             NETRootInfo wm_root( qt_xdisplay(), NET::WMMoveResize );
01346             wm_root.moveResizeRequest( winId(), e->globalX(), e->globalY(), NET::MoveResizeCancel );
01347             return false;
01348         }
01349 #endif
01350 
01351         if ( m_menu && ( ev->type() == QEvent::MouseButtonPress )
01352             && ( e->button() == RightButton ) )
01353         {
01354             m_menu->popup( QCursor::pos() );
01355             return true;
01356         }
01357 
01358         return false;
01359     }
01360 
01361     if ( o == m_editor ) {
01362         if ( ev->type() == QEvent::FocusOut ) {
01363             QFocusEvent *fe = static_cast<QFocusEvent *>(ev);
01364             if ( fe->reason() != QFocusEvent::Popup &&
01365                  fe->reason() != QFocusEvent::Mouse ) {
01366                 updateFocus();
01367                 if ( isModified() ) {
01368             saveConfig();
01369                         if ( !m_blockEmitDataChanged )
01370                             saveData();
01371         }
01372             }
01373         } else if ( ev->type() == QEvent::FocusIn ) {
01374             updateFocus();
01375         }
01376 
01377         return false;
01378     }
01379 
01380     if ( o == m_editor->viewport() )
01381     {
01382         if ( m_edit_menu &&
01383              ev->type() == QEvent::MouseButtonPress &&
01384              ((QMouseEvent *)ev)->button() == RightButton )
01385         {
01386             m_edit_menu->popup( QCursor::pos() );
01387             return true;
01388         }
01389     }
01390 
01391     return false;
01392 }
01393 
01394 void KNote::slotSaveData()
01395 {
01396     saveData();
01397 }
01398 
01399 void KNote::deleteWhenIdle()
01400 {
01401   if ( m_busy <= 0 )
01402     deleteLater();
01403   else
01404     m_deleteWhenIdle = true;
01405 }
01406 
01407 void KNote::aboutToEnterEventLoop()
01408 {
01409   ++m_busy;
01410 }
01411 
01412 void KNote::eventLoopLeft()
01413 {
01414   --m_busy;
01415   if ( m_busy <= 0 && m_deleteWhenIdle )
01416     deleteLater();
01417 }
01418 
01419 
01420 #include "knote.moc"
01421 #include "knotebutton.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys