karm

edittaskdialog.cpp
1/*
2 * karm
3 * This file only: Copyright (C) 1999 Espen Sand, espensa@online.no
4 * Modifications (see CVS log) Copyright (C) 2000 Klarälvdalens
5 * Datakonsult AB <kalle@dalheimer.de>, Jesper Pedersen <blackie@kde.org>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 *
22 */
23#include <tqbuttongroup.h>
24#include <tqcombobox.h>
25#include <tqgroupbox.h>
26#include <tqhbox.h>
27#include <tqlabel.h>
28#include <tqlayout.h>
29#include <tqlineedit.h>
30#include <tqradiobutton.h>
31#include <tqsizepolicy.h>
32#include <tqstring.h>
33#include <tqwidget.h>
34#include <tqwhatsthis.h>
35
36#include <tdelocale.h> // i18n
37#include <twinmodule.h>
38
39#include "edittaskdialog.h"
40#include "ktimewidget.h"
41#include "kdebug.h"
42
43EditTaskDialog::EditTaskDialog( TQString caption, bool editDlg,
44 DesktopList* desktopList)
45 : KDialogBase(0, "EditTaskDialog", true, caption, Ok|Cancel, Ok, true ),
46 origTime( 0 ), origSession( 0 )
47{
48 TQWidget *page = new TQWidget( this );
49 setMainWidget(page);
50 KWinModule twinmodule(0, KWinModule::INFO_DESKTOP);
51
52 TQVBoxLayout *lay1 = new TQVBoxLayout(page);
53
54 TQHBoxLayout *lay2 = new TQHBoxLayout();
55 lay1->addLayout(lay2);
56
57 // The name of the widget
58 TQLabel *label = new TQLabel( i18n("Task &name:"), page, "name" );
59 lay2->addWidget( label );
60 lay2->addSpacing(5);
61
62
63 _name = new TQLineEdit( page, "lineedit" );
64
65 _name->setMinimumWidth(fontMetrics().maxWidth()*15);
66 lay2->addWidget( _name );
67 label->setBuddy( _name );
68
69
70 // The "Edit Absolut" radio button
71 lay1->addSpacing(10);lay1->addStretch(1);
72 _absoluteRB = new TQRadioButton( i18n( "Edit &absolute" ), page,
73 "_absoluteRB" );
74 lay1->addWidget( _absoluteRB );
75 connect( _absoluteRB, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotAbsolutePressed() ) );
76
77
78 // Absolute times
79 TQHBoxLayout *lay5 = new TQHBoxLayout();
80 lay1->addLayout(lay5);
81 lay5->addSpacing(20);
82 TQGridLayout *lay3 = new TQGridLayout( 2, 2, -1, "lay3" );
83 lay5->addLayout(lay3);
84
85 _sessionLA = new TQLabel( i18n("&Session time: "), page, "session time" );
86
87 // Time
88 _timeLA = new TQLabel( i18n("&Time:"), page, "time" );
89 lay3->addWidget( _timeLA, 0, 0 );
90 _timeLA->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1,
91 (TQSizePolicy::SizeType)0,
92 0,
93 0,
94 _timeLA->sizePolicy().hasHeightForWidth()) );
95
96 // Based on measuring pixels in a screenshot, it looks like the fontmetrics
97 // call includes the ampersand when calculating the width. To be sure
98 // things will line up (no matter what language or widget style), set all
99 // three date entry label controls to the same width.
100 _timeLA->setMinimumWidth( fontMetrics().width( _sessionLA->text() ) );
101
102 _timeTW = new KArmTimeWidget( page, "_timeTW" );
103 lay3->addWidget( _timeTW, 0, 1 );
104 _timeLA->setBuddy( _timeTW );
105
106
107 // Session
108 lay3->addWidget( _sessionLA, 1, 0 );
109
110 _sessionTW = new KArmTimeWidget( page, "_sessionTW" );
111 lay3->addWidget( _sessionTW, 1, 1 );
112 _sessionLA->setBuddy( _sessionTW );
113 _sessionLA->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1,
114 (TQSizePolicy::SizeType)0,
115 0,
116 0,
117 _sessionLA->sizePolicy().hasHeightForWidth()) );
118 _sessionLA->setMinimumWidth( fontMetrics().width( _sessionLA->text() ) );
119
120
121 // The "Edit relative" radio button
122 lay1->addSpacing(10);
123 lay1->addStretch(1);
124 _relativeRB = new TQRadioButton( i18n( "Edit &relative (apply to both time and"
125 " session time)" ), page, "_relativeRB" );
126 lay1->addWidget( _relativeRB );
127 connect( _relativeRB, TQ_SIGNAL( clicked() ), this, TQ_SLOT(slotRelativePressed()) );
128
129 // The relative times
130 TQHBoxLayout *lay4 = new TQHBoxLayout();
131 lay1->addLayout( lay4 );
132 lay4->addSpacing(20);
133
134 _operator = new TQComboBox(page);
135 _operator->insertItem( TQString::fromLatin1( "+" ) );
136 _operator->insertItem( TQString::fromLatin1( "-" ) );
137 _operator->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1,
138 (TQSizePolicy::SizeType)0,
139 0,
140 0,
141 _operator->sizePolicy().hasHeightForWidth()) );
142 //kdDebug() << "text width=" << fontMetrics().width( _sessionLA->text() ) << endl;
143 _operator->setMinimumWidth( fontMetrics().width( _sessionLA->text() ) );
144 lay4->addWidget( _operator );
145
146 _diffTW = new KArmTimeWidget( page, "_sessionAddTW" );
147 lay4->addWidget( _diffTW );
148
149 desktopCount = twinmodule.numberOfDesktops();
150
151 // If desktopList contains higher numbered desktops than desktopCount then
152 // delete those from desktopList. This may be the case if the user has
153 // configured virtual desktops. The values in desktopList are sorted.
154 if ( (desktopList != 0) && (desktopList->size() > 0) )
155 {
156 DesktopList::iterator rit = desktopList->begin();
157 while (*rit < desktopCount && rit!=desktopList->end())
158 {
159 ++rit;
160 }
161 desktopList->erase(rit, desktopList->end());
162 }
163
164 // The "Choose Desktop" checkbox
165 lay1->addSpacing(10);
166 lay1->addStretch(1);
167
168 _desktopCB = new TQCheckBox(i18n("A&uto tracking"), page);
169 _desktopCB->setEnabled(true);
170 lay1->addWidget(_desktopCB);
171
172 TQGroupBox* groupBox;
173 {
174 int lines = (int)(desktopCount/2);
175 if (lines*2 != desktopCount) lines++;
176 groupBox = new TQButtonGroup( lines, TQt::Horizontal,
177 i18n("In Desktop"), page, "_desktopsGB");
178 }
179 lay1->addWidget(groupBox);
180
181 TQHBoxLayout *lay6 = new TQHBoxLayout();
182
183 lay1->addLayout(lay6);
184 for (int i=0; i<desktopCount; i++) {
185 _deskBox.push_back(new TQCheckBox(groupBox,TQString::number(i).latin1()));
186 _deskBox[i]->setText(twinmodule.desktopName(i+1));
187 _deskBox[i]->setChecked(false);
188
189 lay6->addWidget(_deskBox[i]);
190 }
191 // check specified Desktop Check Boxes
192 bool enableDesktops = false;
193
194 if ( (desktopList != 0) && (desktopList->size() > 0) )
195 {
196 DesktopList::iterator it = desktopList->begin();
197 while (it != desktopList->end())
198 {
199 _deskBox[*it]->setChecked(true);
200 it++;
201 }
202 enableDesktops = true;
203 }
204 // if some desktops were specified, then enable the parent box
205 _desktopCB->setChecked(enableDesktops);
206
207 for (int i=0; i<desktopCount; i++)
208 _deskBox[i]->setEnabled(enableDesktops);
209
210 connect(_desktopCB, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotAutoTrackingPressed()));
211
212 lay1->addStretch(1);
213
214
215 if ( editDlg ) {
216 // This is an edit dialog.
217 _operator->setFocus();
218 }
219 else {
220 // This is an initial dialog
221 _name->setFocus();
222 }
223
224 slotRelativePressed();
225
226 // Whats this help.
227 TQWhatsThis::add( _name,
228 i18n( "Enter the name of the task here. "
229 "This name is for your eyes only."));
230 TQWhatsThis::add( _absoluteRB,
231 i18n( "Use this option to set the time spent on this task "
232 "to an absolute value.\n\nFor example, if you have "
233 "worked exactly four hours on this task during the current "
234 "session, you would set the Session time to 4 hr." ) );
235 TQWhatsThis::add( _relativeRB,
236 i18n( "Use this option to change the time spent on this task "
237 "relative to its current value.\n\nFor example, if you worked "
238 "on this task for one hour without the timer running, you "
239 "would add 1 hr." ) );
240 TQWhatsThis::add( _timeTW,
241 i18n( "This is the time the task has been "
242 "running since all times were reset."));
243 TQWhatsThis::add( _sessionTW,
244 i18n( "This is the time the task has been running this "
245 "session."));
246 TQWhatsThis::add( _diffTW, i18n( "Specify how much time to add or subtract "
247 "to the overall and session time"));
248
249 TQWhatsThis::add( _desktopCB,
250 i18n( "Use this option to automatically start the timer "
251 "on this task when you switch to the specified desktop(s)." ) );
252 TQWhatsThis::add( groupBox,
253 i18n( "Select the desktop(s) that will automatically start the "
254 "timer on this task." ) );
255}
256
257
258void EditTaskDialog::slotAbsolutePressed()
259{
260 _relativeRB->setChecked( false );
261 _absoluteRB->setChecked( true );
262
263 _operator->setEnabled( false );
264 _diffTW->setEnabled( false );
265
266 _timeLA->setEnabled( true );
267 _sessionLA->setEnabled( true );
268 _timeTW->setEnabled( true );
269 _sessionTW->setEnabled( true );
270}
271
272void EditTaskDialog::slotRelativePressed()
273{
274 _relativeRB->setChecked( true );
275 _absoluteRB->setChecked( false );
276
277 _operator->setEnabled( true );
278 _diffTW->setEnabled( true );
279
280 _timeLA->setEnabled( false );
281 _sessionLA->setEnabled( false );
282 _timeTW->setEnabled( false );
283 _sessionTW->setEnabled( false );
284}
285
286void EditTaskDialog::slotAutoTrackingPressed()
287{
288 bool checked = _desktopCB->isChecked();
289 for (unsigned int i=0; i<_deskBox.size(); i++)
290 _deskBox[i]->setEnabled(checked);
291
292 if (!checked) // uncheck all desktop boxes
293 for (int i=0; i<desktopCount; i++)
294 _deskBox[i]->setChecked(false);
295}
296
297void EditTaskDialog::setTask( const TQString &name, long time, long session )
298{
299 _name->setText( name );
300
301 _timeTW->setTime( time );
302 _sessionTW->setTime( session );
303 origTime = time;
304 origSession = session;
305}
306
307
308TQString EditTaskDialog::taskName() const
309{
310 return( _name->text() );
311}
312
313
314void EditTaskDialog::status(long *time, long *timeDiff, long *session,
315 long *sessionDiff, DesktopList *desktopList) const
316{
317 if ( _absoluteRB->isChecked() ) {
318 *time = _timeTW->time();
319 *session = _sessionTW->time();
320 }
321 else {
322 int diff = _diffTW->time();
323 if ( _operator->currentItem() == 1) {
324 diff = -diff;
325 }
326 *time = origTime + diff;
327 *session = origSession + diff;
328 }
329
330 *timeDiff = *time - origTime;
331 *sessionDiff = *session - origSession;
332
333 for (unsigned int i=0; i<_deskBox.size(); i++) {
334 if (_deskBox[i]->isChecked())
335 desktopList->push_back(i);
336 }
337}
338
339#include "edittaskdialog.moc"
Widget used for entering minutes and seconds with validation.
Definition: ktimewidget.h:14