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