summaryrefslogtreecommitdiffstats
path: root/karm/edittaskdialog.cpp
blob: 37d215d50df0cff552e548cbbe0635c7d9090bba (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 *   karm
 *   This file only: Copyright (C) 1999  Espen Sand, espensa@online.no
 *   Modifications (see CVS log) Copyright (C) 2000 Klarälvdalens
 *   Datakonsult AB <kalle@dalheimer.de>, Jesper Pedersen <blackie@kde.org>
 *
 *
 *   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 <tqbuttongroup.h>
#include <tqcombobox.h>
#include <tqgroupbox.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqradiobutton.h>
#include <tqsizepolicy.h>
#include <tqstring.h>
#include <tqwidget.h>
#include <tqwhatsthis.h>

#include <tdelocale.h>            // i18n
#include <twinmodule.h>

#include "edittaskdialog.h"
#include "ktimewidget.h"
#include "kdebug.h"

EditTaskDialog::EditTaskDialog( TQString caption, bool editDlg,
                                DesktopList* desktopList)
  : KDialogBase(0, "EditTaskDialog", true, caption, Ok|Cancel, Ok, true ),
    origTime( 0 ), origSession( 0 )
{
  TQWidget *page = new TQWidget( this ); 
  setMainWidget(page);
  KWinModule twinmodule(0, KWinModule::INFO_DESKTOP);

  TQVBoxLayout *lay1 = new TQVBoxLayout(page);
  
  TQHBoxLayout *lay2 = new TQHBoxLayout();
  lay1->addLayout(lay2);
  
  // The name of the widget
  TQLabel *label = new TQLabel( i18n("Task &name:"), page, "name" );
  lay2->addWidget( label );
  lay2->addSpacing(5);
  
  
  _name = new TQLineEdit( page, "lineedit" );
  
  _name->setMinimumWidth(fontMetrics().maxWidth()*15);
  lay2->addWidget( _name );
  label->setBuddy( _name );


  // The "Edit Absolut" radio button
  lay1->addSpacing(10);lay1->addStretch(1); 
  _absoluteRB = new TQRadioButton( i18n( "Edit &absolute" ), page,
                                  "_absoluteRB" );
  lay1->addWidget( _absoluteRB );
  connect( _absoluteRB, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotAbsolutePressed() ) );
  

  // Absolute times
  TQHBoxLayout *lay5 = new TQHBoxLayout();
  lay1->addLayout(lay5);
  lay5->addSpacing(20);
  TQGridLayout *lay3 = new TQGridLayout( 2, 2, -1, "lay3" );
  lay5->addLayout(lay3);
  
  _sessionLA = new TQLabel( i18n("&Session time: "), page, "session time" );

  // Time
  _timeLA = new TQLabel( i18n("&Time:"), page, "time" );
  lay3->addWidget( _timeLA, 0, 0 );
  _timeLA->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, 
                                         (TQSizePolicy::SizeType)0, 
                                         0, 
                                         0, 
                                         _timeLA->sizePolicy().hasHeightForWidth()) );

  // Based on measuring pixels in a screenshot, it looks like the fontmetrics
  // call includes the ampersand when calculating the width.  To be sure
  // things will line up (no matter what language or widget style), set all
  // three date entry label controls to the same width.
  _timeLA->setMinimumWidth( fontMetrics().width( _sessionLA->text() ) );

  _timeTW = new KArmTimeWidget( page, "_timeTW" );
  lay3->addWidget( _timeTW, 0, 1 );
  _timeLA->setBuddy( _timeTW );
  

  // Session
  lay3->addWidget( _sessionLA, 1, 0 );

  _sessionTW = new KArmTimeWidget( page, "_sessionTW" );
  lay3->addWidget( _sessionTW, 1, 1 );
  _sessionLA->setBuddy( _sessionTW );
  _sessionLA->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, 
                                         (TQSizePolicy::SizeType)0, 
                                         0, 
                                         0, 
                                         _sessionLA->sizePolicy().hasHeightForWidth()) );
  _sessionLA->setMinimumWidth( fontMetrics().width( _sessionLA->text() ) );


  // The "Edit relative" radio button
  lay1->addSpacing(10);
  lay1->addStretch(1);
  _relativeRB = new TQRadioButton( i18n( "Edit &relative (apply to both time and"
                                        " session time)" ), page, "_relativeRB" );
  lay1->addWidget( _relativeRB );
  connect( _relativeRB, TQ_SIGNAL( clicked() ), this, TQ_SLOT(slotRelativePressed()) );
  
  // The relative times
  TQHBoxLayout *lay4 = new TQHBoxLayout();
  lay1->addLayout( lay4 );
  lay4->addSpacing(20);
  
  _operator = new TQComboBox(page);
  _operator->insertItem( TQString::fromLatin1( "+" ) );
  _operator->insertItem( TQString::fromLatin1( "-" ) );
  _operator->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, 
                                         (TQSizePolicy::SizeType)0, 
                                         0, 
                                         0, 
                                         _operator->sizePolicy().hasHeightForWidth()) );
  //kdDebug() << "text width=" << fontMetrics().width( _sessionLA->text() ) << endl;
  _operator->setMinimumWidth( fontMetrics().width( _sessionLA->text() ) );
  lay4->addWidget( _operator );

  _diffTW = new KArmTimeWidget( page, "_sessionAddTW" );
  lay4->addWidget( _diffTW );

  desktopCount = twinmodule.numberOfDesktops();
  
  // If desktopList contains higher numbered desktops than desktopCount then
  // delete those from desktopList. This may be the case if the user has
  // configured virtual desktops. The values in desktopList are sorted.
  if ( (desktopList != 0) && (desktopList->size() > 0) ) 
  {
    DesktopList::iterator rit = desktopList->begin();
    while (*rit < desktopCount && rit!=desktopList->end()) 
    {
      ++rit;
    }
    desktopList->erase(rit, desktopList->end());
  }

  // The "Choose Desktop" checkbox
  lay1->addSpacing(10);
  lay1->addStretch(1);
  
  _desktopCB = new TQCheckBox(i18n("A&uto tracking"), page);
  _desktopCB->setEnabled(true);
  lay1->addWidget(_desktopCB);
  
  TQGroupBox* groupBox;
  {
    int lines = (int)(desktopCount/2);
    if (lines*2 != desktopCount) lines++; 
      groupBox = new TQButtonGroup( lines, TQt::Horizontal,
                                   i18n("In Desktop"), page, "_desktopsGB");
  }
  lay1->addWidget(groupBox);

  TQHBoxLayout *lay6 = new TQHBoxLayout();

  lay1->addLayout(lay6);
  for (int i=0; i<desktopCount; i++) {
    _deskBox.push_back(new TQCheckBox(groupBox,TQString::number(i).latin1()));
    _deskBox[i]->setText(twinmodule.desktopName(i+1));
    _deskBox[i]->setChecked(false);

    lay6->addWidget(_deskBox[i]);
  }
  // check specified Desktop Check Boxes
  bool enableDesktops = false;

  if ( (desktopList != 0) && (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
  _desktopCB->setChecked(enableDesktops);

  for (int i=0; i<desktopCount; i++)
    _deskBox[i]->setEnabled(enableDesktops);
  
  connect(_desktopCB, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotAutoTrackingPressed()));

  lay1->addStretch(1);


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

  slotRelativePressed();

  // Whats this help.
  TQWhatsThis::add( _name,
                   i18n( "Enter the name of the task here. "
                         "This name is for your eyes only."));
  TQWhatsThis::add( _absoluteRB,
                   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." ) );
  TQWhatsThis::add( _relativeRB,
                   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." ) );
  TQWhatsThis::add( _timeTW,
                   i18n( "This is the time the task has been "
                         "running since all times were reset."));
  TQWhatsThis::add( _sessionTW,
                   i18n( "This is the time the task has been running this "
                         "session."));
  TQWhatsThis::add( _diffTW, i18n( "Specify how much time to add or subtract "
                                  "to the overall and session time"));

  TQWhatsThis::add( _desktopCB, 
                   i18n( "Use this option to automatically start the timer "
                         "on this task when you switch to the specified desktop(s)." ) );
  TQWhatsThis::add( groupBox, 
                   i18n( "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 = _desktopCB->isChecked();
  for (unsigned int i=0; i<_deskBox.size(); i++)
    _deskBox[i]->setEnabled(checked);

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

void EditTaskDialog::setTask( const TQString &name, long time, long session )
{
  _name->setText( name );
  
  _timeTW->setTime( time );
  _sessionTW->setTime( session );
  origTime = time;
  origSession = session;
}


TQString 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->currentItem() == 1) {
      diff = -diff;
    }
    *time = origTime + diff;
    *session = origSession + diff;
  }

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

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

#include "edittaskdialog.moc"