00001
00002
00003
00004
00005
00006
00007
00008 #include <numeric>
00009
00010 #include <qkeycode.h>
00011 #include <qpopupmenu.h>
00012 #include <qptrlist.h>
00013 #include <qstring.h>
00014
00015 #include <kaccel.h>
00016 #include <kaction.h>
00017 #include <kapplication.h>
00018 #include <kconfig.h>
00019 #include <kdebug.h>
00020 #include <kglobal.h>
00021 #include <kkeydialog.h>
00022 #include <klocale.h>
00023 #include <kmessagebox.h>
00024 #include <kstatusbar.h>
00025 #include <kstdaction.h>
00026
00027 #include "kaccelmenuwatch.h"
00028 #include "karmutility.h"
00029 #include "mainwindow.h"
00030 #include "preferences.h"
00031 #include "print.h"
00032 #include "timekard.h"
00033 #include "task.h"
00034 #include "taskview.h"
00035 #include "tray.h"
00036
00037 MainWindow::MainWindow()
00038 : KMainWindow(0),
00039 _accel( new KAccel( this ) ),
00040 _watcher( new KAccelMenuWatch( _accel, this ) ),
00041 _taskView( new TaskView( this ) ),
00042 _totalSum( 0 ),
00043 _sessionSum( 0 )
00044 {
00045 setCentralWidget( _taskView );
00046
00047 startStatusBar();
00048
00049
00050 _preferences = Preferences::instance();
00051
00052
00053 makeMenus();
00054 _watcher->updateMenus();
00055
00056
00057 connect( _taskView, SIGNAL( totalTimesChanged( long, long ) ),
00058 this, SLOT( updateTime( long, long ) ) );
00059 connect( _taskView, SIGNAL( selectionChanged ( QListViewItem * )),
00060 this, SLOT(slotSelectionChanged()));
00061 connect( _taskView, SIGNAL( updateButtons() ),
00062 this, SLOT(slotSelectionChanged()));
00063
00064 loadGeometry();
00065
00066
00067 connect( _taskView,
00068 SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int )),
00069 this,
00070 SLOT( contextMenuRequest( QListViewItem*, const QPoint&, int )));
00071
00072 _tray = new KarmTray( this );
00073
00074 connect( _tray, SIGNAL( quitSelected() ), SLOT( quit() ) );
00075
00076 connect( _taskView, SIGNAL( timersActive() ), _tray, SLOT( startClock() ) );
00077 connect( _taskView, SIGNAL( timersActive() ), this, SLOT( enableStopAll() ));
00078 connect( _taskView, SIGNAL( timersInactive() ), _tray, SLOT( stopClock() ) );
00079 connect( _taskView, SIGNAL( timersInactive() ), this, SLOT( disableStopAll()));
00080 connect( _taskView, SIGNAL( tasksChanged( QPtrList<Task> ) ),
00081 _tray, SLOT( updateToolTip( QPtrList<Task> ) ));
00082
00083 _taskView->load();
00084
00085 if ( _taskView->isReadOnly() )
00086 stateChanged( QString::fromLatin1( "readonly" ) );
00087
00088
00089
00090 _preferences->emitSignals();
00091 slotSelectionChanged();
00092
00093 }
00094
00095 void MainWindow::slotSelectionChanged()
00096 {
00097 Task* item= _taskView->current_item();
00098 if ( _taskView->isReadOnly() )
00099 item = 0;
00100 actionDelete->setEnabled(item);
00101 actionEdit->setEnabled(item);
00102 actionStart->setEnabled(item && !item->isRunning());
00103 actionStop->setEnabled(item && item->isRunning());
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113 void MainWindow::save()
00114 {
00115 kdDebug(5970) << i18n("Saving time data to disk.") << endl;
00116 _taskView->save();
00117 saveGeometry();
00118 }
00119
00120 void MainWindow::quit()
00121 {
00122 kapp->quit();
00123 }
00124
00125
00126 MainWindow::~MainWindow()
00127 {
00128 kdDebug(5970) << i18n("MainWindow::~MainWindows: Quitting karm.") << endl;
00129 _taskView->stopAllTimers();
00130 save();
00131 _taskView->closeStorage();
00132 }
00133
00134 void MainWindow::enableStopAll()
00135 {
00136 actionStopAll->setEnabled(true);
00137 }
00138
00139 void MainWindow::disableStopAll()
00140 {
00141 actionStopAll->setEnabled(false);
00142 }
00143
00144
00150 void MainWindow::updateTime( long sessionDiff, long totalDiff )
00151 {
00152 _sessionSum += sessionDiff;
00153 _totalSum += totalDiff;
00154
00155 updateStatusBar();
00156 }
00157
00158 void MainWindow::updateStatusBar( )
00159 {
00160 QString time;
00161
00162 time = formatTime( _sessionSum );
00163 statusBar()->changeItem( i18n("Session: %1").arg(time), 0 );
00164
00165 time = formatTime( _totalSum );
00166 statusBar()->changeItem( i18n("Total: %1" ).arg(time), 1);
00167 }
00168
00169 void MainWindow::startStatusBar()
00170 {
00171 statusBar()->insertItem( i18n("Session"), 0, 0, true );
00172 statusBar()->insertItem( i18n("Total" ), 1, 0, true );
00173 }
00174
00175 void MainWindow::saveProperties( KConfig* cfg )
00176 {
00177 _taskView->stopAllTimers();
00178 _taskView->save();
00179 cfg->writeEntry( "WindowShown", isVisible());
00180 }
00181
00182 void MainWindow::readProperties( KConfig* cfg )
00183 {
00184 if( cfg->readBoolEntry( "WindowShown", true ))
00185 show();
00186 }
00187
00188 void MainWindow::keyBindings()
00189 {
00190 KKeyDialog::configure( actionCollection(), this );
00191 }
00192
00193 void MainWindow::startNewSession()
00194 {
00195 _taskView->startNewSession();
00196 }
00197
00198 void MainWindow::resetAllTimes()
00199 {
00200 if ( KMessageBox::warningContinueCancel( this, i18n( "Do you really want to reset the time to zero for all tasks?" ),
00201 i18n( "Confirmation Required" ), KGuiItem( i18n( "Reset All Times" ) ) ) == KMessageBox::Continue )
00202 _taskView->resetTimeForAllTasks();
00203 }
00204
00205 void MainWindow::makeMenus()
00206 {
00207 KAction
00208 *actionKeyBindings,
00209 *actionNew,
00210 *actionNewSub;
00211
00212 (void) KStdAction::quit( this, SLOT( quit() ), actionCollection());
00213 (void) KStdAction::print( this, SLOT( print() ), actionCollection());
00214 actionKeyBindings = KStdAction::keyBindings( this, SLOT( keyBindings() ),
00215 actionCollection() );
00216 actionPreferences = KStdAction::preferences(_preferences,
00217 SLOT(showDialog()),
00218 actionCollection() );
00219 (void) KStdAction::save( this, SLOT( save() ), actionCollection() );
00220 KAction* actionStartNewSession = new KAction( i18n("Start &New Session"),
00221 0,
00222 this,
00223 SLOT( startNewSession() ),
00224 actionCollection(),
00225 "start_new_session");
00226 KAction* actionResetAll = new KAction( i18n("&Reset All Times"),
00227 0,
00228 this,
00229 SLOT( resetAllTimes() ),
00230 actionCollection(),
00231 "reset_all_times");
00232 actionStart = new KAction( i18n("&Start"),
00233 QString::fromLatin1("1rightarrow"), Key_S,
00234 _taskView,
00235 SLOT( startCurrentTimer() ), actionCollection(),
00236 "start");
00237 actionStop = new KAction( i18n("S&top"),
00238 QString::fromLatin1("stop"), 0,
00239 _taskView,
00240 SLOT( stopCurrentTimer() ), actionCollection(),
00241 "stop");
00242 actionStopAll = new KAction( i18n("Stop &All Timers"),
00243 Key_Escape,
00244 _taskView,
00245 SLOT( stopAllTimers() ), actionCollection(),
00246 "stopAll");
00247 actionStopAll->setEnabled(false);
00248
00249 actionNew = new KAction( i18n("&New..."),
00250 QString::fromLatin1("filenew"), CTRL+Key_N,
00251 _taskView,
00252 SLOT( newTask() ), actionCollection(),
00253 "new_task");
00254 actionNewSub = new KAction( i18n("New &Subtask..."),
00255 QString::fromLatin1("kmultiple"), CTRL+ALT+Key_N,
00256 _taskView,
00257 SLOT( newSubTask() ), actionCollection(),
00258 "new_sub_task");
00259 actionDelete = new KAction( i18n("&Delete"),
00260 QString::fromLatin1("editdelete"), Key_Delete,
00261 _taskView,
00262 SLOT( deleteTask() ), actionCollection(),
00263 "delete_task");
00264 actionEdit = new KAction( i18n("&Edit..."),
00265 QString::fromLatin1("edit"), CTRL + Key_E,
00266 _taskView,
00267 SLOT( editTask() ), actionCollection(),
00268 "edit_task");
00269
00270
00271
00272
00273
00274
00275
00276 actionMarkAsComplete = new KAction( i18n("&Mark as Complete"),
00277 QString::fromLatin1("document"),
00278 CTRL+Key_M,
00279 _taskView,
00280 SLOT( markTaskAsComplete() ),
00281 actionCollection(),
00282 "mark_as_complete");
00283 actionClipTotals = new KAction( i18n("&Copy Totals to Clipboard"),
00284 QString::fromLatin1("klipper"),
00285 CTRL+Key_C,
00286 _taskView,
00287 SLOT( clipTotals() ),
00288 actionCollection(),
00289 "clip_totals");
00290 actionClipHistory = new KAction( i18n("Copy &History to Clipboard"),
00291 QString::fromLatin1("klipper"),
00292 CTRL+ALT+Key_C,
00293 _taskView,
00294 SLOT( clipHistory() ),
00295 actionCollection(),
00296 "clip_history");
00297
00298 new KAction( i18n("Import &Legacy Flat File..."), 0,
00299 _taskView, SLOT(loadFromFlatFile()), actionCollection(),
00300 "import_flatfile");
00301 new KAction( i18n("&Export to CSV File..."), 0,
00302 _taskView, SLOT(exportcsvFile()), actionCollection(),
00303 "export_csvfile");
00304 new KAction( i18n("Export &History to CSV File..."), 0,
00305 _taskView, SLOT(exportcsvHistory()), actionCollection(),
00306 "export_csvhistory");
00307
00308
00309
00310
00311
00312
00313
00314 createGUI( QString::fromLatin1("karmui.rc") );
00315
00316
00317 actionKeyBindings->setToolTip( i18n("Configure key bindings") );
00318 actionKeyBindings->setWhatsThis( i18n("This will let you configure key"
00319 "bindings which is specific to karm") );
00320
00321 actionStartNewSession->setToolTip( i18n("Start a new session") );
00322 actionStartNewSession->setWhatsThis( i18n("This will reset the session time "
00323 "to 0 for all tasks, to start a "
00324 "new session, without affecting "
00325 "the totals.") );
00326 actionResetAll->setToolTip( i18n("Reset all times") );
00327 actionResetAll->setWhatsThis( i18n("This will reset the session and total "
00328 "time to 0 for all tasks, to restart from "
00329 "scratch.") );
00330
00331 actionStart->setToolTip( i18n("Start timing for selected task") );
00332 actionStart->setWhatsThis( i18n("This will start timing for the selected "
00333 "task.\n"
00334 "It is even possible to time several tasks "
00335 "simultaneously.\n\n"
00336 "You may also start timing of a tasks by "
00337 "double clicking the left mouse "
00338 "button on a given task. This will, however, "
00339 "stop timing of other tasks."));
00340
00341 actionStop->setToolTip( i18n("Stop timing of the selected task") );
00342 actionStop->setWhatsThis( i18n("Stop timing of the selected task") );
00343
00344 actionStopAll->setToolTip( i18n("Stop all of the active timers") );
00345 actionStopAll->setWhatsThis( i18n("Stop all of the active timers") );
00346
00347 actionNew->setToolTip( i18n("Create new top level task") );
00348 actionNew->setWhatsThis( i18n("This will create a new top level task.") );
00349
00350 actionDelete->setToolTip( i18n("Delete selected task") );
00351 actionDelete->setWhatsThis( i18n("This will delete the selected task and "
00352 "all its subtasks.") );
00353
00354 actionEdit->setToolTip( i18n("Edit name or times for selected task") );
00355 actionEdit->setWhatsThis( i18n("This will bring up a dialog box where you "
00356 "may edit the parameters for the selected "
00357 "task."));
00358
00359
00360
00361
00362
00363
00364 actionClipTotals->setToolTip(i18n("Copy task totals to clipboard"));
00365 actionClipHistory->setToolTip(i18n("Copy time card history to clipboard."));
00366
00367 slotSelectionChanged();
00368 }
00369
00370 void MainWindow::print()
00371 {
00372 MyPrinter printer(_taskView);
00373 printer.print();
00374 }
00375
00376 void MainWindow::loadGeometry()
00377 {
00378 KConfig &config = *kapp->config();
00379
00380 config.setGroup( QString::fromLatin1("Main Window Geometry") );
00381 int w = config.readNumEntry( QString::fromLatin1("Width"), 100 );
00382 int h = config.readNumEntry( QString::fromLatin1("Height"), 100 );
00383 w = QMAX( w, sizeHint().width() );
00384 h = QMAX( h, sizeHint().height() );
00385 resize(w, h);
00386 }
00387
00388
00389 void MainWindow::saveGeometry()
00390 {
00391 KConfig &config = *KGlobal::config();
00392 config.setGroup( QString::fromLatin1("Main Window Geometry"));
00393 config.writeEntry( QString::fromLatin1("Width"), width());
00394 config.writeEntry( QString::fromLatin1("Height"), height());
00395 config.sync();
00396 }
00397
00398 bool MainWindow::queryClose()
00399 {
00400 if ( !kapp->sessionSaving() ) {
00401 hide();
00402 return false;
00403 }
00404 return KMainWindow::queryClose();
00405 }
00406
00407 void MainWindow::contextMenuRequest( QListViewItem*, const QPoint& point, int )
00408 {
00409 QPopupMenu* pop = dynamic_cast<QPopupMenu*>(
00410 factory()->container( i18n( "task_popup" ), this ) );
00411 if ( pop )
00412 pop->popup( point );
00413 }
00414
00415 #include "mainwindow.moc"