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