korganizer

journalentry.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (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     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 //
00026 // Journal Entry
00027 
00028 #include <qlabel.h>
00029 #include <qlayout.h>
00030 #include <qcheckbox.h>
00031 #include <qwhatsthis.h>
00032 #include <qtooltip.h>
00033 #include <qtoolbutton.h>
00034 
00035 #include <kdebug.h>
00036 #include <kdialog.h>
00037 #include <kglobal.h>
00038 #include <klocale.h>
00039 #include <ktextedit.h>
00040 #include <ktimeedit.h>
00041 #include <klineedit.h>
00042 #include <kactivelabel.h>
00043 #include <kstdguiitem.h>
00044 #include <kmessagebox.h>
00045 
00046 #include <libkcal/journal.h>
00047 #include <libkcal/calendar.h>
00048 
00049 #include "kodialogmanager.h"
00050 #include "incidencechanger.h"
00051 #include "koglobals.h"
00052 
00053 #include "journalentry.h"
00054 #include "journalentry.moc"
00055 #ifndef KORG_NOPRINTER
00056 #include "kocorehelper.h"
00057 #include "calprinter.h"
00058 #endif
00059 
00060 class JournalTitleLable : public KActiveLabel
00061 {
00062 public:
00063   JournalTitleLable( QWidget *parent, const char *name=0 ) : KActiveLabel( parent, name ) {}
00064 
00065   void openLink( const QString &/*link*/ ) {}
00066 };
00067 
00068 
00069 JournalDateEntry::JournalDateEntry( Calendar *calendar, QWidget *parent ) :
00070   QVBox( parent ), mCalendar( calendar )
00071 {
00072 //kdDebug(5850)<<"JournalEntry::JournalEntry, parent="<<parent<<endl;
00073   mChanger = 0;
00074 
00075   mTitle = new JournalTitleLable( this );
00076   mTitle->setMargin(2);
00077   mTitle->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00078   connect( mTitle, SIGNAL( linkClicked( const QString & ) ),
00079            this, SLOT( emitNewJournal() ) );
00080 }
00081 
00082 JournalDateEntry::~JournalDateEntry()
00083 {
00084 }
00085 
00086 void JournalDateEntry::setDate(const QDate &date)
00087 {
00088   QString dtstring = QString( "<qt><center><b><i>%1</i></b>  " )
00089                      .arg( KGlobal::locale()->formatDate(date) );
00090 
00091   dtstring += " <font size=\"-1\"><a href=\"#\">" +
00092               i18n("[Add Journal Entry]") +
00093               "</a></font></center></qt>";
00094 
00095   mTitle->setText( dtstring );
00096   mDate = date;
00097   emit setDateSignal( date );
00098 }
00099 
00100 void JournalDateEntry::clear()
00101 {
00102   QValueList<JournalEntry*> values( mEntries.values() );
00103 
00104   QValueList<JournalEntry*>::Iterator it = values.begin();
00105   for ( ; it != values.end(); ++it ) {
00106     delete (*it);
00107   }
00108   mEntries.clear();
00109 }
00110 
00111 // should only be called by the KOJournalView now.
00112 void JournalDateEntry::addJournal( Journal *j )
00113 {
00114   QMap<Journal*,JournalEntry*>::Iterator pos = mEntries.find( j );
00115   if ( pos != mEntries.end() ) return;
00116 
00117   JournalEntry *entry = new JournalEntry( j, this );
00118   entry->show();
00119   entry->setDate( mDate );
00120   entry->setIncidenceChanger( mChanger );
00121 
00122   mEntries.insert( j, entry );
00123   connect( this, SIGNAL( setIncidenceChangerSignal( IncidenceChangerBase * ) ),
00124            entry, SLOT( setIncidenceChanger( IncidenceChangerBase * ) ) );
00125   connect( this, SIGNAL( setDateSignal( const QDate & ) ),
00126            entry, SLOT( setDate( const QDate & ) ) );
00127   connect( this, SIGNAL( flushEntries() ),
00128            entry, SLOT( flushEntry() ) );
00129   connect( entry, SIGNAL( deleteIncidence( Incidence* ) ),
00130            this, SIGNAL( deleteIncidence( Incidence* ) ) );
00131   connect( entry, SIGNAL( editIncidence( Incidence* ) ),
00132            this, SIGNAL( editIncidence( Incidence* ) ) );
00133 }
00134 
00135 Journal::List JournalDateEntry::journals() const
00136 {
00137   QValueList<Journal*> jList( mEntries.keys() );
00138   Journal::List l;
00139   QValueList<Journal*>::Iterator it = jList.begin();
00140   for ( ; it != jList.end(); ++it ) {
00141     l.append( *it );
00142   }
00143   return l;
00144 }
00145 
00146 void JournalDateEntry::setIncidenceChanger( IncidenceChangerBase *changer )
00147 {
00148   mChanger = changer;
00149   emit setIncidenceChangerSignal( changer );
00150 }
00151 
00152 void JournalDateEntry::emitNewJournal()
00153 {
00154   emit newJournal( mDate );
00155 }
00156 
00157 void JournalDateEntry::journalEdited( Journal *journal )
00158 {
00159   QMap<Journal*,JournalEntry*>::Iterator pos = mEntries.find( journal );
00160   if ( pos == mEntries.end() ) return;
00161 
00162   pos.data()->setJournal( journal );
00163 
00164 }
00165 
00166 void JournalDateEntry::journalDeleted( Journal *journal )
00167 {
00168   QMap<Journal*,JournalEntry*>::Iterator pos = mEntries.find( journal );
00169   if ( pos == mEntries.end() ) return;
00170 
00171   delete pos.data();
00172 }
00173 
00174 
00175 
00176 
00177 
00178 JournalEntry::JournalEntry( Journal* j, QWidget *parent ) :
00179   QWidget( parent ), mJournal( j )
00180 {
00181 //kdDebug(5850)<<"JournalEntry::JournalEntry, parent="<<parent<<endl;
00182   mDirty = false;
00183   mWriteInProgress = false;
00184   mChanger = 0;
00185   mReadOnly = false;
00186 
00187   mLayout = new QGridLayout( this );
00188   mLayout->setSpacing( KDialog::spacingHint() );
00189   mLayout->setMargin( KDialog::marginHint() );
00190 
00191   QString whatsThis = i18n("Sets the Title of this journal entry.");
00192 
00193   mTitleLabel = new QLabel( i18n("&Title: "), this );
00194   mLayout->addWidget( mTitleLabel, 0, 0 );
00195   mTitleEdit = new KLineEdit( this );
00196   mLayout->addWidget( mTitleEdit, 0, 1 );
00197   mTitleLabel->setBuddy( mTitleEdit );
00198 
00199   QWhatsThis::add( mTitleLabel, whatsThis );
00200   QWhatsThis::add( mTitleEdit, whatsThis );
00201 
00202   mTimeCheck = new QCheckBox( i18n("Ti&me: "), this );
00203   mLayout->addWidget( mTimeCheck, 0, 2 );
00204   mTimeEdit = new KTimeEdit( this );
00205   mLayout->addWidget( mTimeEdit, 0, 3 );
00206   connect( mTimeCheck, SIGNAL(toggled(bool)),
00207            this, SLOT(timeCheckBoxToggled(bool)) );
00208   QWhatsThis::add( mTimeCheck, i18n("Determines whether this journal entry has "
00209                                     "a time associated with it") );
00210   QWhatsThis::add( mTimeEdit, i18n( "Sets the time associated with this journal "
00211                                     "entry" ) );
00212 
00213   mDeleteButton = new QToolButton( this, "deleteButton" );
00214   QPixmap pix = KOGlobals::self()->smallIcon( "editdelete" );
00215   mDeleteButton->setPixmap( pix );
00216   mDeleteButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00217   QToolTip::add( mDeleteButton, i18n("Delete this journal entry") );
00218   QWhatsThis::add( mDeleteButton, i18n("Delete this journal entry") );
00219   mLayout->addWidget( mDeleteButton, 0, 4 );
00220   connect( mDeleteButton, SIGNAL(pressed()), this, SLOT(deleteItem()) );
00221 
00222   mEditButton = new QToolButton( this, "editButton" );
00223   mEditButton->setPixmap( KOGlobals::self()->smallIcon( "edit" ) );
00224   mEditButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00225   QToolTip::add( mEditButton, i18n("Edit this journal entry") );
00226   QWhatsThis::add( mEditButton, i18n("Opens an editor dialog for this journal entry") );
00227   mLayout->addWidget( mEditButton, 0, 5 );
00228   connect( mEditButton, SIGNAL(clicked()), this, SLOT( editItem() ) );
00229 
00230 #ifndef KORG_NOPRINTER
00231   mPrintButton = new QToolButton( this, "printButton" );
00232   mPrintButton->setPixmap( KOGlobals::self()->smallIcon( "printer1" ) );
00233   mPrintButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00234   QToolTip::add( mPrintButton, i18n("Print this journal entry") );
00235   QWhatsThis::add( mPrintButton, i18n("Opens the print dialog for this journal entry") );
00236   mLayout->addWidget( mPrintButton, 0, 6 );
00237   connect( mPrintButton, SIGNAL(clicked()), this, SLOT( printItem() ) );
00238 #endif
00239   mEditor = new KTextEdit(this);
00240   mLayout->addMultiCellWidget( mEditor, 1, 2, 0, 6 );
00241 
00242   connect( mTitleEdit, SIGNAL(textChanged( const QString& )), SLOT(setDirty()) );
00243   connect( mTimeCheck, SIGNAL(toggled(bool)), SLOT(setDirty()) );
00244   connect( mTimeEdit, SIGNAL(timeChanged(QTime)), SLOT(setDirty()) );
00245   connect( mEditor, SIGNAL(textChanged()), SLOT(setDirty()) );
00246 
00247   mEditor->installEventFilter(this);
00248 
00249   readJournal( mJournal );
00250   mDirty = false;
00251 }
00252 
00253 JournalEntry::~JournalEntry()
00254 {
00255   writeJournal();
00256 }
00257 
00258 void JournalEntry::deleteItem()
00259 {
00260 /*  KMessageBox::ButtonCode *code = KMessageBox::warningContinueCancel(this,
00261       i18n("The journal \"%1\" on %2 will be permanently deleted.")
00262                .arg( mJournal->summary() )
00263                .arg( mJournal->dtStartStr() ),
00264   i18n("KOrganizer Confirmation"), KStdGuiItem::del() );
00265   if ( code == KMessageBox::Yes ) {*/
00266     if ( mJournal )
00267       emit deleteIncidence( mJournal );
00268 //   }
00269 }
00270 
00271 void JournalEntry::editItem()
00272 {
00273   writeJournal();
00274   if ( mJournal )
00275     emit editIncidence( mJournal );
00276 }
00277 
00278 void JournalEntry::printItem()
00279 {
00280 #ifndef KORG_NOPRINTER
00281   writeJournal();
00282   if ( mJournal ) {
00283     KOCoreHelper helper;
00284     CalPrinter printer( this, 0, &helper );
00285     connect( this, SIGNAL(configChanged()), &printer, SLOT(updateConfig()) );
00286 
00287     Incidence::List selectedIncidences;
00288     selectedIncidences.append( mJournal );
00289 
00290     printer.print( KOrg::CalPrinterBase::Incidence,
00291                  QDate(), QDate(), selectedIncidences );
00292   }
00293 #endif
00294 }
00295 
00296 void JournalEntry::setReadOnly( bool readonly )
00297 {
00298   mReadOnly = readonly;
00299   mTitleEdit->setReadOnly( mReadOnly );
00300   mEditor->setReadOnly( mReadOnly );
00301   mTimeCheck->setEnabled( !mReadOnly );
00302   mTimeEdit->setEnabled( !mReadOnly && mTimeCheck->isChecked() );
00303   mDeleteButton->setEnabled( !mReadOnly );
00304 }
00305 
00306 
00307 void JournalEntry::setDate(const QDate &date)
00308 {
00309   writeJournal();
00310   mDate = date;
00311 }
00312 
00313 void JournalEntry::setJournal(Journal *journal)
00314 {
00315   if ( !mWriteInProgress )
00316     writeJournal();
00317   if ( !journal ) return;
00318 
00319   mJournal = journal;
00320   readJournal( journal );
00321 
00322   mDirty = false;
00323 }
00324 
00325 void JournalEntry::setDirty()
00326 {
00327   mDirty = true;
00328   kdDebug(5850) << "JournalEntry::setDirty()" << endl;
00329 }
00330 
00331 bool JournalEntry::eventFilter( QObject *o, QEvent *e )
00332 {
00333 //  kdDebug(5850) << "JournalEntry::event received " << e->type() << endl;
00334 
00335   if ( e->type() == QEvent::FocusOut || e->type() == QEvent::Hide ||
00336        e->type() == QEvent::Close ) {
00337     writeJournal();
00338   }
00339   return QWidget::eventFilter( o, e );    // standard event processing
00340 }
00341 
00342 
00343 void JournalEntry::readJournal( Journal *j )
00344 {
00345   mJournal = j;
00346   mTitleEdit->setText( mJournal->summary() );
00347   bool hasTime = !mJournal->doesFloat();
00348   mTimeCheck->setChecked( hasTime );
00349   mTimeEdit->setEnabled( hasTime );
00350   if ( hasTime ) {
00351     mTimeEdit->setTime( mJournal->dtStart().time() );
00352   }
00353   mEditor->setText( mJournal->description() );
00354   setReadOnly( mJournal->isReadOnly() );
00355 }
00356 
00357 void JournalEntry::writeJournalPrivate( Journal *j )
00358 {
00359   j->setSummary( mTitleEdit->text() );
00360   bool hasTime = mTimeCheck->isChecked();
00361   QTime tm( mTimeEdit->getTime() );
00362   j->setDtStart( QDateTime( mDate, hasTime?tm:QTime(0,0,0) ) );
00363   j->setFloats( !hasTime );
00364   j->setDescription( mEditor->text() );
00365 }
00366 
00367 void JournalEntry::writeJournal()
00368 {
00369 //  kdDebug(5850) << "JournalEntry::writeJournal()" << endl;
00370 
00371   if ( mReadOnly || !mDirty || !mChanger ) {
00372     kdDebug(5850)<<"Journal either read-only, unchanged or no changer object available"<<endl;
00373     return;
00374   }
00375   bool newJournal = false;
00376   mWriteInProgress = true;
00377 
00378   Journal *oldJournal = 0;
00379 
00380   if ( !mJournal ) {
00381     newJournal = true;
00382     mJournal = new Journal;
00383     writeJournalPrivate( mJournal );
00384     if ( !mChanger->addIncidence( mJournal, this ) ) {
00385       KODialogManager::errorSaveIncidence( this, mJournal );
00386       delete mJournal;
00387       mJournal = 0;
00388     }
00389   } else {
00390     oldJournal = mJournal->clone();
00391     if ( mChanger->beginChange( mJournal ) ) {
00392       writeJournalPrivate( mJournal );
00393       mChanger->changeIncidence( oldJournal, mJournal, KOGlobals::DESCRIPTION_MODIFIED );
00394       mChanger->endChange( mJournal );
00395     }
00396     delete oldJournal;
00397   }
00398   mDirty = false;
00399   mWriteInProgress = false;
00400 }
00401 
00402 void JournalEntry::flushEntry()
00403 {
00404   if (!mDirty) return;
00405 
00406   writeJournal();
00407 }
00408 
00409 void JournalEntry::timeCheckBoxToggled(bool on)
00410 {
00411   mTimeEdit->setEnabled(on);
00412   if(on)
00413     mTimeEdit->setFocus();
00414 }
KDE Home | KDE Accessibility Home | Description of Access Keys