korganizer

journalentry.cpp
1 /*
2  This file is part of KOrganizer.
3  Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 //
26 // Journal Entry
27 
28 #include <tqlabel.h>
29 #include <tqlayout.h>
30 #include <tqcheckbox.h>
31 #include <tqwhatsthis.h>
32 #include <tqtooltip.h>
33 #include <tqtoolbutton.h>
34 
35 #include <kdebug.h>
36 #include <kdialog.h>
37 #include <tdeglobal.h>
38 #include <tdelocale.h>
39 #include <ktextedit.h>
40 #include <ktimeedit.h>
41 #include <klineedit.h>
42 #include <kactivelabel.h>
43 #include <kstdguiitem.h>
44 #include <tdemessagebox.h>
45 
46 #include <libkcal/journal.h>
47 #include <libkcal/calendar.h>
48 
49 #include "kodialogmanager.h"
50 #include "incidencechanger.h"
51 #include "koglobals.h"
52 
53 #include "journalentry.h"
54 #include "journalentry.moc"
55 #ifndef KORG_NOPRINTER
56 #include "kocorehelper.h"
57 #include "calprinter.h"
58 #endif
59 
60 class JournalTitleLable : public KActiveLabel
61 {
62 public:
63  JournalTitleLable( TQWidget *parent, const char *name=0 ) : KActiveLabel( parent, name ) {}
64 
65  void openLink( const TQString &/*link*/ ) {}
66 };
67 
68 
69 JournalDateEntry::JournalDateEntry( Calendar *calendar, TQWidget *parent ) :
70  TQVBox( parent ), mCalendar( calendar )
71 {
72 //kdDebug(5850)<<"JournalEntry::JournalEntry, parent="<<parent<<endl;
73  mChanger = 0;
74 
75  mTitle = new JournalTitleLable( this );
76  mTitle->setMargin(2);
77  mTitle->setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed );
78  connect( mTitle, TQ_SIGNAL( linkClicked( const TQString & ) ),
79  this, TQ_SLOT( emitNewJournal() ) );
80 }
81 
82 JournalDateEntry::~JournalDateEntry()
83 {
84 }
85 
86 void JournalDateEntry::setDate(const TQDate &date)
87 {
88  TQString dtstring = TQString( "<qt><center><b><i>%1</i></b> " )
89  .arg( TDEGlobal::locale()->formatDate(date) );
90 
91  dtstring += " <font size=\"-1\"><a href=\"#\">" +
92  i18n("[Add Journal Entry]") +
93  "</a></font></center></qt>";
94 
95  mTitle->setText( dtstring );
96  mDate = date;
97  emit setDateSignal( date );
98 }
99 
100 void JournalDateEntry::clear()
101 {
102  TQValueList<JournalEntry*> values( mEntries.values() );
103 
104  TQValueList<JournalEntry*>::Iterator it = values.begin();
105  for ( ; it != values.end(); ++it ) {
106  delete (*it);
107  }
108  mEntries.clear();
109 }
110 
111 // should only be called by the KOJournalView now.
112 void JournalDateEntry::addJournal( Journal *j )
113 {
114  TQMap<Journal*,JournalEntry*>::Iterator pos = mEntries.find( j );
115  if ( pos != mEntries.end() ) return;
116 
117  JournalEntry *entry = new JournalEntry( j, this );
118  entry->show();
119  entry->setDate( mDate );
120  entry->setIncidenceChanger( mChanger );
121 
122  mEntries.insert( j, entry );
123  connect( this, TQ_SIGNAL( setIncidenceChangerSignal( IncidenceChangerBase * ) ),
124  entry, TQ_SLOT( setIncidenceChanger( IncidenceChangerBase * ) ) );
125  connect( this, TQ_SIGNAL( setDateSignal( const TQDate & ) ),
126  entry, TQ_SLOT( setDate( const TQDate & ) ) );
127  connect( this, TQ_SIGNAL( flushEntries() ),
128  entry, TQ_SLOT( flushEntry() ) );
129  connect( entry, TQ_SIGNAL( deleteIncidence( Incidence* ) ),
130  this, TQ_SIGNAL( deleteIncidence( Incidence* ) ) );
131  connect( entry, TQ_SIGNAL( editIncidence( Incidence*, const TQDate& ) ),
132  this, TQ_SIGNAL( editIncidence( Incidence*, const TQDate& ) ) );
133 }
134 
135 Journal::List JournalDateEntry::journals() const
136 {
137  TQValueList<Journal*> jList( mEntries.keys() );
138  Journal::List l;
139  TQValueList<Journal*>::Iterator it = jList.begin();
140  for ( ; it != jList.end(); ++it ) {
141  l.append( *it );
142  }
143  return l;
144 }
145 
146 void JournalDateEntry::setIncidenceChanger( IncidenceChangerBase *changer )
147 {
148  mChanger = changer;
149  emit setIncidenceChangerSignal( changer );
150 }
151 
152 void JournalDateEntry::emitNewJournal()
153 {
154  emit newJournal( 0/*ResourceCalendar*/, TQString()/*subResource*/, mDate );
155 }
156 
157 void JournalDateEntry::journalEdited( Journal *journal )
158 {
159  TQMap<Journal*,JournalEntry*>::Iterator pos = mEntries.find( journal );
160  if ( pos == mEntries.end() ) return;
161 
162  pos.data()->setJournal( journal );
163 
164 }
165 
166 void JournalDateEntry::journalDeleted( Journal *journal )
167 {
168  TQMap<Journal*,JournalEntry*>::Iterator pos = mEntries.find( journal );
169  if ( pos == mEntries.end() ) return;
170 
171  delete pos.data();
172 }
173 
174 
175 
176 
177 
178 JournalEntry::JournalEntry( Journal* j, TQWidget *parent ) :
179  TQWidget( parent ), mJournal( j )
180 {
181 //kdDebug(5850)<<"JournalEntry::JournalEntry, parent="<<parent<<endl;
182  mDirty = false;
183  mWriteInProgress = false;
184  mChanger = 0;
185  mReadOnly = false;
186 
187  mLayout = new TQGridLayout( this );
188  mLayout->setSpacing( KDialog::spacingHint() );
189  mLayout->setMargin( KDialog::marginHint() );
190 
191  TQString whatsThis = i18n("Sets the Title of this journal entry.");
192 
193  mTitleLabel = new TQLabel( i18n("&Title: "), this );
194  mLayout->addWidget( mTitleLabel, 0, 0 );
195  mTitleEdit = new KLineEdit( this );
196  mLayout->addWidget( mTitleEdit, 0, 1 );
197  mTitleLabel->setBuddy( mTitleEdit );
198 
199  TQWhatsThis::add( mTitleLabel, whatsThis );
200  TQWhatsThis::add( mTitleEdit, whatsThis );
201 
202  mTimeCheck = new TQCheckBox( i18n("Ti&me: "), this );
203  mLayout->addWidget( mTimeCheck, 0, 2 );
204  mTimeEdit = new KTimeEdit( this );
205  mLayout->addWidget( mTimeEdit, 0, 3 );
206  connect( mTimeCheck, TQ_SIGNAL(toggled(bool)),
207  this, TQ_SLOT(timeCheckBoxToggled(bool)) );
208  TQWhatsThis::add( mTimeCheck, i18n("Determines whether this journal entry has "
209  "a time associated with it") );
210  TQWhatsThis::add( mTimeEdit, i18n( "Sets the time associated with this journal "
211  "entry" ) );
212 
213  mDeleteButton = new TQToolButton( this, "deleteButton" );
214  TQPixmap pix = KOGlobals::self()->smallIcon( "edit-delete" );
215  mDeleteButton->setPixmap( pix );
216  mDeleteButton->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
217  TQToolTip::add( mDeleteButton, i18n("Delete this journal entry") );
218  TQWhatsThis::add( mDeleteButton, i18n("Delete this journal entry") );
219  mLayout->addWidget( mDeleteButton, 0, 4 );
220  connect( mDeleteButton, TQ_SIGNAL(pressed()), this, TQ_SLOT(deleteItem()) );
221 
222  mEditButton = new TQToolButton( this, "editButton" );
223  mEditButton->setPixmap( KOGlobals::self()->smallIcon( "edit" ) );
224  mEditButton->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
225  TQToolTip::add( mEditButton, i18n("Edit this journal entry") );
226  TQWhatsThis::add( mEditButton, i18n("Opens an editor dialog for this journal entry") );
227  mLayout->addWidget( mEditButton, 0, 5 );
228  connect( mEditButton, TQ_SIGNAL(clicked()), this, TQ_SLOT( editItem() ) );
229 
230 #ifndef KORG_NOPRINTER
231  mPrintButton = new TQToolButton( this, "printButton" );
232  mPrintButton->setPixmap( KOGlobals::self()->smallIcon( "printer" ) );
233  mPrintButton->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
234  TQToolTip::add( mPrintButton, i18n("Print this journal entry") );
235  TQWhatsThis::add( mPrintButton, i18n("Opens the print dialog for this journal entry") );
236  mLayout->addWidget( mPrintButton, 0, 6 );
237  connect( mPrintButton, TQ_SIGNAL(clicked()), this, TQ_SLOT( printItem() ) );
238 #endif
239  mEditor = new KTextEdit(this);
240  mLayout->addMultiCellWidget( mEditor, 1, 2, 0, 6 );
241 
242  connect( mTitleEdit, TQ_SIGNAL(textChanged( const TQString& )), TQ_SLOT(setDirty()) );
243  connect( mTimeCheck, TQ_SIGNAL(toggled(bool)), TQ_SLOT(setDirty()) );
244  connect( mTimeEdit, TQ_SIGNAL(timeChanged(TQTime)), TQ_SLOT(setDirty()) );
245  connect( mEditor, TQ_SIGNAL(textChanged()), TQ_SLOT(setDirty()) );
246 
247  mEditor->installEventFilter(this);
248 
249  readJournal( mJournal );
250  mDirty = false;
251 }
252 
253 JournalEntry::~JournalEntry()
254 {
255  writeJournal();
256 }
257 
258 void JournalEntry::deleteItem()
259 {
260 /* KMessageBox::ButtonCode *code = KMessageBox::warningContinueCancel(this,
261  i18n("The journal \"%1\" on %2 will be permanently deleted.")
262  .arg( mJournal->summary() )
263  .arg( mJournal->dtStartStr() ),
264  i18n("KOrganizer Confirmation"), KStdGuiItem::del() );
265  if ( code == KMessageBox::Yes ) {*/
266  if ( mJournal )
267  emit deleteIncidence( mJournal );
268 // }
269 }
270 
271 void JournalEntry::editItem()
272 {
273  writeJournal();
274  if ( mJournal ) {
275  emit editIncidence( mJournal, mJournal->dtStart().date() );
276  }
277 }
278 
279 void JournalEntry::printItem()
280 {
281 #ifndef KORG_NOPRINTER
282  writeJournal();
283  if ( mJournal ) {
284  KOCoreHelper helper;
285  CalPrinter printer( this, 0, &helper );
286  connect( this, TQ_SIGNAL(configChanged()), &printer, TQ_SLOT(updateConfig()) );
287 
288  Incidence::List selectedIncidences;
289  selectedIncidences.append( mJournal );
290 
291  printer.print( KOrg::CalPrinterBase::Incidence,
292  TQDate(), TQDate(), selectedIncidences );
293  }
294 #endif
295 }
296 
297 void JournalEntry::setReadOnly( bool readonly )
298 {
299  mReadOnly = readonly;
300  mTitleEdit->setReadOnly( mReadOnly );
301  mEditor->setReadOnly( mReadOnly );
302  mTimeCheck->setEnabled( !mReadOnly );
303  mTimeEdit->setEnabled( !mReadOnly && mTimeCheck->isChecked() );
304  mDeleteButton->setEnabled( !mReadOnly );
305 }
306 
307 
308 void JournalEntry::setDate(const TQDate &date)
309 {
310  writeJournal();
311  mDate = date;
312 }
313 
314 void JournalEntry::setJournal(Journal *journal)
315 {
316  if ( !mWriteInProgress )
317  writeJournal();
318  if ( !journal ) return;
319 
320  mJournal = journal;
321  readJournal( journal );
322 
323  mDirty = false;
324 }
325 
326 void JournalEntry::setDirty()
327 {
328  mDirty = true;
329  kdDebug(5850) << "JournalEntry::setDirty()" << endl;
330 }
331 
332 bool JournalEntry::eventFilter( TQObject *o, TQEvent *e )
333 {
334 // kdDebug(5850) << "JournalEntry::event received " << e->type() << endl;
335 
336  if ( e->type() == TQEvent::FocusOut || e->type() == TQEvent::Hide ||
337  e->type() == TQEvent::Close ) {
338  writeJournal();
339  }
340  return TQWidget::eventFilter( o, e ); // standard event processing
341 }
342 
343 
344 void JournalEntry::readJournal( Journal *j )
345 {
346  mJournal = j;
347  mTitleEdit->setText( mJournal->summary() );
348  bool hasTime = !mJournal->doesFloat();
349  mTimeCheck->setChecked( hasTime );
350  mTimeEdit->setEnabled( hasTime );
351  if ( hasTime ) {
352  mTimeEdit->setTime( mJournal->dtStart().time() );
353  }
354  mEditor->setText( mJournal->description() );
355  setReadOnly( mJournal->isReadOnly() );
356 }
357 
358 void JournalEntry::writeJournalPrivate( Journal *j )
359 {
360  j->setSummary( mTitleEdit->text() );
361  bool hasTime = mTimeCheck->isChecked();
362  TQTime tm( mTimeEdit->getTime() );
363  j->setDtStart( TQDateTime( mDate, hasTime?tm:TQTime(0,0,0) ) );
364  j->setFloats( !hasTime );
365  j->setDescription( mEditor->text() );
366 }
367 
368 void JournalEntry::writeJournal()
369 {
370 // kdDebug(5850) << "JournalEntry::writeJournal()" << endl;
371 
372  if ( mReadOnly || !mDirty || !mChanger ) {
373  kdDebug(5850)<<"Journal either read-only, unchanged or no changer object available"<<endl;
374  return;
375  }
376  bool newJournal = false;
377  mWriteInProgress = true;
378 
379  Journal *oldJournal = 0;
380 
381  if ( !mJournal ) {
382  newJournal = true;
383  mJournal = new Journal;
384  writeJournalPrivate( mJournal );
385  if ( !mChanger->addIncidence( mJournal, 0, TQString(), this ) ) {
386  KODialogManager::errorSaveIncidence( this, mJournal );
387  delete mJournal;
388  mJournal = 0;
389  }
390  } else {
391  oldJournal = mJournal->clone();
392  if ( mChanger->beginChange( mJournal, 0, TQString() ) ) {
393  writeJournalPrivate( mJournal );
394  mChanger->changeIncidence( oldJournal, mJournal, KOGlobals::DESCRIPTION_MODIFIED, this );
395  mChanger->endChange( mJournal, 0, TQString() );
396  }
397  delete oldJournal;
398  }
399  mDirty = false;
400  mWriteInProgress = false;
401 }
402 
403 void JournalEntry::flushEntry()
404 {
405  if (!mDirty) return;
406 
407  writeJournal();
408 }
409 
410 void JournalEntry::timeCheckBoxToggled(bool on)
411 {
412  mTimeEdit->setEnabled(on);
413  if(on)
414  mTimeEdit->setFocus();
415 }
CalPrinter is a class for printing Calendars.
Definition: calprinter.h:54
bool doesFloat() const
void setSummary(const TQString &summary)
void setDescription(const TQString &description)
void setFloats(bool f)
virtual void setDtStart(const TQDateTime &dtStart)
Journal * clone()