~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 *     Copyright (C) 1999 by Espen Sand <espensa@online.no>
 *                   2000 by Klarälvdalens Datakonsult AB <kalle@dalheimer.de>
 *                   2000 by Jesper Pedersen <blackie@kde.org>
 *                   2007 the ktimetracker developers
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the
 *      Free Software Foundation, Inc.
 *      51 Franklin Street, Fifth Floor
 *      Boston, MA  02110-1301  USA.
 *
 */

#include "edittaskdialog.h"

#include <QCheckBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QRadioButton>
#include <QString>
#include <QVBoxLayout>
#include <QWidget>

#include <KComboBox>
#include <KDebug>
#include <KLineEdit>
#include <KLocale>            // i18n
#include <KWindowSystem>

#include "ktimewidget.h"

EditTaskDialog::EditTaskDialog( QWidget *parent, const QString &caption, 
                                bool editDlg, DesktopList* desktopList)
  : KDialog( parent ),
    origTime( 0 ), origSession( 0 )
{
  setWindowTitle( caption );
  setObjectName( "EditTaskDialog" );
  QWidget *page = new QWidget( this ); 
  setMainWidget( page );

  QVBoxLayout *mainLayout = new QVBoxLayout;
  mainLayout->setMargin( 0 );
  mainLayout->setSpacing( KDialog::spacingHint() );

  /* Task name */
  QWidget *taskNameWidget = new QWidget( page );
  QHBoxLayout *layout = new QHBoxLayout;
  layout->setMargin( 0 );
  layout->setSpacing( KDialog::spacingHint() );
  QLabel *label = new QLabel( i18n( "Task &name:" ), taskNameWidget );
  _name = new KLineEdit( taskNameWidget );
  label->setBuddy( _name );
  layout->addWidget( label );
  layout->addWidget( _name );
  taskNameWidget->setLayout( layout );

  /* The "Edit Absolut" radio button */
  QWidget *timeWidget = new QWidget( page );
  QGridLayout *gridLayout = new QGridLayout;
  gridLayout->setMargin( 0 );
  gridLayout->setSpacing( 0 );
  gridLayout->setColumnMinimumWidth( 0, 20 );
  gridLayout->setColumnMinimumWidth( 2, KDialog::spacingHint() );
  _absoluteRB = new QRadioButton( i18n( "Edit &absolute" ), timeWidget );
  connect( _absoluteRB, SIGNAL( clicked() ),
           this, SLOT( slotAbsolutePressed() ) );
  _timeLA = new QLabel( i18n( "&Time:" ), timeWidget );
  _timeTW = new KArmTimeWidget( timeWidget );
  _timeLA->setBuddy( _timeTW );
  _sessionLA = new QLabel( i18n( "&Session time:" ), timeWidget );
  _sessionTW = new KArmTimeWidget( timeWidget );
  _sessionLA->setBuddy( _sessionTW );

  /* The "Edit relative" radio button */
  _relativeRB = new QRadioButton( i18n( "Edit &relative (apply to both time and"
      " session time)" ), timeWidget );
  connect( _relativeRB, SIGNAL( clicked() ), 
           this, SLOT( slotRelativePressed() ) );
  _operator = new KComboBox( timeWidget );
  _operator->addItem( QString::fromLatin1( "+" ) );
  _operator->addItem( QString::fromLatin1( "-" ) );

  _diffTW = new KArmTimeWidget( timeWidget );
  gridLayout->addWidget(_absoluteRB, 0, 0, 1, 4 );
  gridLayout->addWidget( _timeLA, 1, 1 );
  gridLayout->addWidget( _timeTW, 1, 3 );
  gridLayout->addWidget( _sessionLA, 2, 1 );
  gridLayout->addWidget( _sessionTW, 2, 3 );
  gridLayout->addWidget( _relativeRB, 3, 0, 1, 4 );
  gridLayout->addWidget( _operator, 4, 1, 1, 2 );
  gridLayout->addWidget( _diffTW, 4, 3 );
  timeWidget->setLayout( gridLayout );

  /* Auto Tracking */
#ifdef Q_WS_X11
  desktopCount = KWindowSystem::numberOfDesktops();
#else
#ifdef __GNUC__
#warning non-X11 support missing
#endif
  desktopCount = 1;
#endif

  if ( desktopList && (desktopList->size() > 0) ) {
    DesktopList::iterator rit = desktopList->begin();
    while ( *rit < desktopCount && rit!=desktopList->end() ) {
      ++rit;
    }
    desktopList->erase( rit, desktopList->end() );
  }

  int lines = (int)(desktopCount/2);
  if (lines*2 != desktopCount) lines++; 

  QGroupBox *groupBox = new QGroupBox( i18n( "A&uto Tracking" ), page );
  groupBox->setCheckable( true );
  groupBox->setChecked( false );
  gridLayout = new QGridLayout;
  gridLayout->setMargin( KDialog::marginHint() );
  gridLayout->setSpacing( KDialog::spacingHint() );
  label = new QLabel( i18n( "In Desktop" ) );
  gridLayout->addWidget( label, 0, 0, 1, lines );

  for ( int i = 0; i < desktopCount; ++i ) {
    QCheckBox *tmpBx = new QCheckBox( groupBox );
    tmpBx->setObjectName( QString::number( i ).toLatin1() );
    _deskBox.append( tmpBx );
#ifdef Q_WS_X11
    tmpBx->setText( KWindowSystem::desktopName( i + 1 ) );
#endif
    tmpBx->setChecked( false );

    gridLayout->addWidget( tmpBx, i / lines + 1, i % lines );
  }

  groupBox->setLayout( gridLayout );

  // check specified Desktop Check Boxes
  bool enableDesktops = false;

  if ( desktopList && ( desktopList->size() > 0 ) ) {
    DesktopList::iterator it = desktopList->begin();
    while ( it != desktopList->end() ) {
      _deskBox[*it]->setChecked( true );
      ++it;
    }
    enableDesktops = true;
  }
  // if some desktops were specified, then enable the parent box
  groupBox->setChecked( enableDesktops );

  connect(groupBox, SIGNAL( clicked( bool ) ),
          this, SLOT( slotAutoTrackingPressed( bool ) ) );

  mainLayout->addWidget( taskNameWidget );
  mainLayout->addWidget( timeWidget );
  mainLayout->addWidget( groupBox );
  page->setLayout( mainLayout );

  if ( editDlg ) {
    // This is an edit dialog.
    _operator->setFocus();
  }
  else {
    // This is an initial dialog
    _name->setFocus();
  }

  slotRelativePressed();

  // Whats this help.
  _name->setWhatsThis(
                   i18n( "Enter the name of the task here. "
                         "This name is for your eyes only."));
  _absoluteRB->setWhatsThis(
                   i18n( "Use this option to set the time spent on this task "
                         "to an absolute value.\n\nFor example, if you have "
                         "worked exactly four hours on this task during the current "
                         "session, you would set the Session time to 4 hr." ) );
  _relativeRB->setWhatsThis(
                   i18n( "Use this option to change the time spent on this task "
                         "relative to its current value.\n\nFor example, if you worked "
                         "on this task for one hour without the timer running, you "
                         "would add 1 hr." ) );
  _timeTW->setWhatsThis(
                   i18n( "This is the time the task has been "
                         "running since all times were reset."));
  _sessionTW->setWhatsThis(
                   i18n( "This is the time the task has been running this "
                         "session."));
  _diffTW->setWhatsThis( 
                   i18n( "Specify how much time to add or subtract "
                         "to the overall and session time"));
  groupBox->setWhatsThis( 
                   i18n( "Use this option to automatically start the timer "
                         "on this task when you switch to the specified desktop(s). "
                         "Select the desktop(s) that will automatically start the "
                         "timer on this task." ) );
}

void EditTaskDialog::slotAbsolutePressed()
{
  _relativeRB->setChecked( false );
  _absoluteRB->setChecked( true );

  _operator->setEnabled( false );
  _diffTW->setEnabled( false );

  _timeLA->setEnabled( true );
  _sessionLA->setEnabled( true );
  _timeTW->setEnabled( true );
  _sessionTW->setEnabled( true );
}

void EditTaskDialog::slotRelativePressed()
{
  _relativeRB->setChecked( true );
  _absoluteRB->setChecked( false );

  _operator->setEnabled( true );
  _diffTW->setEnabled( true );

  _timeLA->setEnabled( false );
  _sessionLA->setEnabled( false );
  _timeTW->setEnabled( false );
  _sessionTW->setEnabled( false );
}

void EditTaskDialog::slotAutoTrackingPressed( bool checked ) 
{
  if (!checked)  // uncheck all desktop boxes
    for ( int i = 0; i < desktopCount; ++i ) 
      _deskBox[i]->setChecked( false );
}

void EditTaskDialog::setTask( const QString &name, long time, long session )
{
  _name->setText( name );

  _timeTW->setTime( time / 60, time % 60 );
  _sessionTW->setTime( session / 60, session % 60 );
  origTime = time;
  origSession = session;
}

QString EditTaskDialog::taskName() const
{
  return( _name->text() );
}

void EditTaskDialog::status(long *time, long *timeDiff, long *session,
                            long *sessionDiff, DesktopList *desktopList) const
{
  if ( _absoluteRB->isChecked() ) {
    *time = _timeTW->time();
    *session = _sessionTW->time();
  } else {
    int diff = _diffTW->time();
    if ( _operator->currentIndex() == 1) {
      diff = -diff;
    }
    *time = origTime + diff;
    *session = origSession + diff;
  }

  *timeDiff = *time - origTime;
  *sessionDiff = *session - origSession;

  for ( int i = 0; i < _deskBox.count(); i++ ) {
    if ( _deskBox[i]->isChecked() )
      desktopList->append( i );
  }
}

#include "edittaskdialog.moc"