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* )
00176 {
00177 _taskView->
stopAllTimers();
00178 _taskView->
save();
00179 }
00180
00181
void MainWindow::keyBindings()
00182 {
00183 KKeyDialog::configure( actionCollection(),
this );
00184 }
00185
00186
void MainWindow::startNewSession()
00187 {
00188 _taskView->
startNewSession();
00189 }
00190
00191
void MainWindow::resetAllTimes()
00192 {
00193
if ( KMessageBox::warningContinueCancel(
this, i18n(
"Do you really want to reset the time to zero for all tasks?" ),
00194 i18n(
"Confirmation Required" ), KGuiItem( i18n(
"Reset All Times" ) ) ) == KMessageBox::Continue )
00195 _taskView->
resetTimeForAllTasks();
00196 }
00197
00198
void MainWindow::makeMenus()
00199 {
00200 KAction
00201 *actionKeyBindings,
00202 *actionNew,
00203 *actionNewSub;
00204
00205 (
void) KStdAction::quit(
this, SLOT( quit() ), actionCollection());
00206 (
void) KStdAction::print(
this, SLOT( print() ), actionCollection());
00207 actionKeyBindings = KStdAction::keyBindings(
this, SLOT( keyBindings() ),
00208 actionCollection() );
00209 actionPreferences = KStdAction::preferences(_preferences,
00210 SLOT(showDialog()),
00211 actionCollection() );
00212 (
void) KStdAction::save(
this, SLOT( save() ), actionCollection() );
00213 KAction* actionStartNewSession =
new KAction( i18n(
"Start &New Session"),
00214 0,
00215
this,
00216 SLOT( startNewSession() ),
00217 actionCollection(),
00218
"start_new_session");
00219 KAction* actionResetAll =
new KAction( i18n(
"&Reset All Times"),
00220 0,
00221
this,
00222 SLOT( resetAllTimes() ),
00223 actionCollection(),
00224
"reset_all_times");
00225 actionStart =
new KAction( i18n(
"&Start"),
00226 QString::fromLatin1(
"1rightarrow"), Key_S,
00227 _taskView,
00228 SLOT( startCurrentTimer() ), actionCollection(),
00229
"start");
00230 actionStop =
new KAction( i18n(
"S&top"),
00231 QString::fromLatin1(
"stop"), 0,
00232 _taskView,
00233 SLOT( stopCurrentTimer() ), actionCollection(),
00234
"stop");
00235 actionStopAll =
new KAction( i18n(
"Stop &All Timers"),
00236 Key_Escape,
00237 _taskView,
00238 SLOT( stopAllTimers() ), actionCollection(),
00239
"stopAll");
00240 actionStopAll->setEnabled(
false);
00241
00242 actionNew =
new KAction( i18n(
"&New..."),
00243 QString::fromLatin1(
"filenew"), CTRL+Key_N,
00244 _taskView,
00245 SLOT( newTask() ), actionCollection(),
00246
"new_task");
00247 actionNewSub =
new KAction( i18n(
"New &Subtask..."),
00248 QString::fromLatin1(
"kmultiple"), CTRL+ALT+Key_N,
00249 _taskView,
00250 SLOT( newSubTask() ), actionCollection(),
00251
"new_sub_task");
00252 actionDelete =
new KAction( i18n(
"&Delete"),
00253 QString::fromLatin1(
"editdelete"), Key_Delete,
00254 _taskView,
00255 SLOT( deleteTask() ), actionCollection(),
00256
"delete_task");
00257 actionEdit =
new KAction( i18n(
"&Edit..."),
00258 QString::fromLatin1(
"edit"), CTRL + Key_E,
00259 _taskView,
00260 SLOT( editTask() ), actionCollection(),
00261
"edit_task");
00262
00263
00264
00265
00266
00267
00268
00269 actionMarkAsComplete =
new KAction( i18n(
"&Mark as Complete"),
00270 QString::fromLatin1(
"document"),
00271 CTRL+Key_M,
00272 _taskView,
00273 SLOT( markTaskAsComplete() ),
00274 actionCollection(),
00275
"mark_as_complete");
00276 actionClipTotals =
new KAction( i18n(
"&Copy Totals to Clipboard"),
00277 QString::fromLatin1(
"klipper"),
00278 CTRL+Key_C,
00279 _taskView,
00280 SLOT( clipTotals() ),
00281 actionCollection(),
00282
"clip_totals");
00283 actionClipHistory =
new KAction( i18n(
"Copy &History to Clipboard"),
00284 QString::fromLatin1(
"klipper"),
00285 CTRL+ALT+Key_C,
00286 _taskView,
00287 SLOT( clipHistory() ),
00288 actionCollection(),
00289
"clip_history");
00290
00291
new KAction( i18n(
"Import &Legacy Flat File..."), 0,
00292 _taskView, SLOT(loadFromFlatFile()), actionCollection(),
00293
"import_flatfile");
00294
new KAction( i18n(
"&Export to CSV File..."), 0,
00295 _taskView, SLOT(exportcsvFile()), actionCollection(),
00296
"export_csvfile");
00297
new KAction( i18n(
"Export &History to CSV File..."), 0,
00298 _taskView, SLOT(exportcsvHistory()), actionCollection(),
00299
"export_csvhistory");
00300
00301
00302
00303
00304
00305
00306
00307 createGUI( QString::fromLatin1(
"karmui.rc") );
00308
00309
00310 actionKeyBindings->setToolTip( i18n(
"Configure key bindings") );
00311 actionKeyBindings->setWhatsThis( i18n(
"This will let you configure key"
00312
"bindings which is specific to karm") );
00313
00314 actionStartNewSession->setToolTip( i18n(
"Start a new session") );
00315 actionStartNewSession->setWhatsThis( i18n(
"This will reset the session time "
00316
"to 0 for all tasks, to start a "
00317
"new session, without affecting "
00318
"the totals.") );
00319 actionResetAll->setToolTip( i18n(
"Reset all times") );
00320 actionResetAll->setWhatsThis( i18n(
"This will reset the session and total "
00321
"time to 0 for all tasks, to restart from "
00322
"scratch.") );
00323
00324 actionStart->setToolTip( i18n(
"Start timing for selected task") );
00325 actionStart->setWhatsThis( i18n(
"This will start timing for the selected "
00326
"task.\n"
00327
"It is even possible to time several tasks "
00328
"simultaneously.\n\n"
00329
"You may also start timing of a tasks by "
00330
"double clicking the left mouse "
00331
"button on a given task. This will, however, "
00332
"stop timing of other tasks."));
00333
00334 actionStop->setToolTip( i18n(
"Stop timing of the selected task") );
00335 actionStop->setWhatsThis( i18n(
"Stop timing of the selected task") );
00336
00337 actionStopAll->setToolTip( i18n(
"Stop all of the active timers") );
00338 actionStopAll->setWhatsThis( i18n(
"Stop all of the active timers") );
00339
00340 actionNew->setToolTip( i18n(
"Create new top level task") );
00341 actionNew->setWhatsThis( i18n(
"This will create a new top level task.") );
00342
00343 actionDelete->setToolTip( i18n(
"Delete selected task") );
00344 actionDelete->setWhatsThis( i18n(
"This will delete the selected task and "
00345
"all its subtasks.") );
00346
00347 actionEdit->setToolTip( i18n(
"Edit name or times for selected task") );
00348 actionEdit->setWhatsThis( i18n(
"This will bring up a dialog box where you "
00349
"may edit the parameters for the selected "
00350
"task."));
00351
00352
00353
00354
00355
00356
00357 actionClipTotals->setToolTip(i18n(
"Copy task totals to clipboard"));
00358 actionClipHistory->setToolTip(i18n(
"Copy time card history to clipboard."));
00359
00360 slotSelectionChanged();
00361 }
00362
00363
void MainWindow::print()
00364 {
00365
MyPrinter printer(_taskView);
00366 printer.
print();
00367 }
00368
00369
void MainWindow::loadGeometry()
00370 {
00371 KConfig &config = *kapp->config();
00372
00373 config.setGroup( QString::fromLatin1(
"Main Window Geometry") );
00374
int w = config.readNumEntry( QString::fromLatin1(
"Width"), 100 );
00375
int h = config.readNumEntry( QString::fromLatin1(
"Height"), 100 );
00376 w = QMAX( w, sizeHint().width() );
00377 h = QMAX( h, sizeHint().height() );
00378 resize(w, h);
00379 }
00380
00381
00382
void MainWindow::saveGeometry()
00383 {
00384 KConfig &config = *KGlobal::config();
00385 config.setGroup( QString::fromLatin1(
"Main Window Geometry"));
00386 config.writeEntry( QString::fromLatin1(
"Width"), width());
00387 config.writeEntry( QString::fromLatin1(
"Height"), height());
00388 config.sync();
00389 }
00390
00391
bool MainWindow::queryClose()
00392 {
00393
if ( !kapp->sessionSaving() ) {
00394 hide();
00395
return false;
00396 }
00397
return KMainWindow::queryClose();
00398 }
00399
00400
void MainWindow::contextMenuRequest( QListViewItem*,
const QPoint& point,
int )
00401 {
00402
QPopupMenu* pop = dynamic_cast<QPopupMenu*>(
00403 factory()->container( i18n(
"task_popup" ),
this ) );
00404
if ( pop )
00405 pop->popup( point );
00406 }
00407
00408
#include "mainwindow.moc"