krop's commit fixes my problem in a better way, reverting
[kdepim.git] / ktimetracker / edittaskdialog.cpp
blobe9e9ea3cf719a98c06b2cf65c897043878002073
1 /*
2 * Copyright (C) 1999 by Espen Sand <espensa@online.no>
3 * 2000 by Klarälvdalens Datakonsult AB <kalle@dalheimer.de>
4 * 2000 by Jesper Pedersen <blackie@kde.org>
5 * 2007 the ktimetracker developers
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the
19 * Free Software Foundation, Inc.
20 * 51 Franklin Street, Fifth Floor
21 * Boston, MA 02110-1301 USA.
25 #include "edittaskdialog.h"
27 #include <QCheckBox>
28 #include <QGridLayout>
29 #include <QGroupBox>
30 #include <QHBoxLayout>
31 #include <QLabel>
32 #include <QRadioButton>
33 #include <QString>
34 #include <QVBoxLayout>
35 #include <QWidget>
37 #include <KComboBox>
38 #include <KDebug>
39 #include <KLineEdit>
40 #include <KLocale> // i18n
41 #include <KWindowSystem>
43 #include "ktimewidget.h"
45 EditTaskDialog::EditTaskDialog( QWidget *parent, const QString &caption,
46 bool editDlg, DesktopList* desktopList)
47 : KDialog( parent ),
48 origTime( 0 ), origSession( 0 )
50 setWindowTitle( caption );
51 setObjectName( "EditTaskDialog" );
52 QWidget *page = new QWidget( this );
53 setMainWidget( page );
55 QVBoxLayout *mainLayout = new QVBoxLayout;
56 mainLayout->setMargin( 0 );
57 mainLayout->setSpacing( KDialog::spacingHint() );
59 /* Task name */
60 QWidget *taskNameWidget = new QWidget( page );
61 QHBoxLayout *layout = new QHBoxLayout;
62 layout->setMargin( 0 );
63 layout->setSpacing( KDialog::spacingHint() );
64 QLabel *label = new QLabel( i18n( "Task &name:" ), taskNameWidget );
65 _name = new KLineEdit( taskNameWidget );
66 label->setBuddy( _name );
67 layout->addWidget( label );
68 layout->addWidget( _name );
69 taskNameWidget->setLayout( layout );
71 /* The "Edit Absolut" radio button */
72 QWidget *timeWidget = new QWidget( page );
73 QGridLayout *gridLayout = new QGridLayout;
74 gridLayout->setMargin( 0 );
75 gridLayout->setSpacing( 0 );
76 gridLayout->setColumnMinimumWidth( 0, 20 );
77 gridLayout->setColumnMinimumWidth( 2, KDialog::spacingHint() );
78 _absoluteRB = new QRadioButton( i18n( "Edit &absolute" ), timeWidget );
79 connect( _absoluteRB, SIGNAL( clicked() ),
80 this, SLOT( slotAbsolutePressed() ) );
81 _timeLA = new QLabel( i18n( "&Time:" ), timeWidget );
82 _timeTW = new KArmTimeWidget( timeWidget );
83 _timeLA->setBuddy( _timeTW );
84 _sessionLA = new QLabel( i18n( "&Session time:" ), timeWidget );
85 _sessionTW = new KArmTimeWidget( timeWidget );
86 _sessionLA->setBuddy( _sessionTW );
88 /* The "Edit relative" radio button */
89 _relativeRB = new QRadioButton( i18n( "Edit &relative (apply to both time and"
90 " session time)" ), timeWidget );
91 connect( _relativeRB, SIGNAL( clicked() ),
92 this, SLOT( slotRelativePressed() ) );
93 _operator = new KComboBox( timeWidget );
94 _operator->addItem( QString::fromLatin1( "+" ) );
95 _operator->addItem( QString::fromLatin1( "-" ) );
97 _diffTW = new KArmTimeWidget( timeWidget );
98 gridLayout->addWidget(_absoluteRB, 0, 0, 1, 4 );
99 gridLayout->addWidget( _timeLA, 1, 1 );
100 gridLayout->addWidget( _timeTW, 1, 3 );
101 gridLayout->addWidget( _sessionLA, 2, 1 );
102 gridLayout->addWidget( _sessionTW, 2, 3 );
103 gridLayout->addWidget( _relativeRB, 3, 0, 1, 4 );
104 gridLayout->addWidget( _operator, 4, 1, 1, 2 );
105 gridLayout->addWidget( _diffTW, 4, 3 );
106 timeWidget->setLayout( gridLayout );
108 /* Auto Tracking */
109 #ifdef Q_WS_X11
110 desktopCount = KWindowSystem::numberOfDesktops();
111 #else
112 #ifdef __GNUC__
113 #warning non-X11 support missing
114 #endif
115 desktopCount = 1;
116 #endif
118 if ( desktopList && (desktopList->size() > 0) ) {
119 DesktopList::iterator rit = desktopList->begin();
120 while ( *rit < desktopCount && rit!=desktopList->end() ) {
121 ++rit;
123 desktopList->erase( rit, desktopList->end() );
126 int lines = (int)(desktopCount/2);
127 if (lines*2 != desktopCount) lines++;
129 QGroupBox *groupBox = new QGroupBox( i18n( "A&uto Tracking" ), page );
130 groupBox->setCheckable( true );
131 groupBox->setChecked( false );
132 gridLayout = new QGridLayout;
133 gridLayout->setMargin( KDialog::marginHint() );
134 gridLayout->setSpacing( KDialog::spacingHint() );
135 label = new QLabel( i18n( "In Desktop" ) );
136 gridLayout->addWidget( label, 0, 0, 1, lines );
138 for ( int i = 0; i < desktopCount; ++i ) {
139 QCheckBox *tmpBx = new QCheckBox( groupBox );
140 tmpBx->setObjectName( QString::number( i ).toLatin1() );
141 _deskBox.append( tmpBx );
142 #ifdef Q_WS_X11
143 tmpBx->setText( KWindowSystem::desktopName( i + 1 ) );
144 #endif
145 tmpBx->setChecked( false );
147 gridLayout->addWidget( tmpBx, i / lines + 1, i % lines );
150 groupBox->setLayout( gridLayout );
152 // check specified Desktop Check Boxes
153 bool enableDesktops = false;
155 if ( desktopList && ( desktopList->size() > 0 ) ) {
156 DesktopList::iterator it = desktopList->begin();
157 while ( it != desktopList->end() ) {
158 _deskBox[*it]->setChecked( true );
159 ++it;
161 enableDesktops = true;
163 // if some desktops were specified, then enable the parent box
164 groupBox->setChecked( enableDesktops );
166 connect(groupBox, SIGNAL( clicked( bool ) ),
167 this, SLOT( slotAutoTrackingPressed( bool ) ) );
169 mainLayout->addWidget( taskNameWidget );
170 mainLayout->addWidget( timeWidget );
171 mainLayout->addWidget( groupBox );
172 page->setLayout( mainLayout );
174 if ( editDlg ) {
175 // This is an edit dialog.
176 _operator->setFocus();
178 else {
179 // This is an initial dialog
180 _name->setFocus();
183 slotRelativePressed();
185 // Whats this help.
186 _name->setWhatsThis(
187 i18n( "Enter the name of the task here. "
188 "This name is for your eyes only."));
189 _absoluteRB->setWhatsThis(
190 i18n( "Use this option to set the time spent on this task "
191 "to an absolute value.\n\nFor example, if you have "
192 "worked exactly four hours on this task during the current "
193 "session, you would set the Session time to 4 hr." ) );
194 _relativeRB->setWhatsThis(
195 i18n( "Use this option to change the time spent on this task "
196 "relative to its current value.\n\nFor example, if you worked "
197 "on this task for one hour without the timer running, you "
198 "would add 1 hr." ) );
199 _timeTW->setWhatsThis(
200 i18n( "This is the time the task has been "
201 "running since all times were reset."));
202 _sessionTW->setWhatsThis(
203 i18n( "This is the time the task has been running this "
204 "session."));
205 _diffTW->setWhatsThis(
206 i18n( "Specify how much time to add or subtract "
207 "to the overall and session time"));
208 groupBox->setWhatsThis(
209 i18n( "Use this option to automatically start the timer "
210 "on this task when you switch to the specified desktop(s). "
211 "Select the desktop(s) that will automatically start the "
212 "timer on this task." ) );
215 void EditTaskDialog::slotAbsolutePressed()
217 _relativeRB->setChecked( false );
218 _absoluteRB->setChecked( true );
220 _operator->setEnabled( false );
221 _diffTW->setEnabled( false );
223 _timeLA->setEnabled( true );
224 _sessionLA->setEnabled( true );
225 _timeTW->setEnabled( true );
226 _sessionTW->setEnabled( true );
229 void EditTaskDialog::slotRelativePressed()
231 _relativeRB->setChecked( true );
232 _absoluteRB->setChecked( false );
234 _operator->setEnabled( true );
235 _diffTW->setEnabled( true );
237 _timeLA->setEnabled( false );
238 _sessionLA->setEnabled( false );
239 _timeTW->setEnabled( false );
240 _sessionTW->setEnabled( false );
243 void EditTaskDialog::slotAutoTrackingPressed( bool checked )
245 if (!checked) // uncheck all desktop boxes
246 for ( int i = 0; i < desktopCount; ++i )
247 _deskBox[i]->setChecked( false );
250 void EditTaskDialog::setTask( const QString &name, long time, long session )
252 _name->setText( name );
254 _timeTW->setTime( time / 60, time % 60 );
255 _sessionTW->setTime( session / 60, session % 60 );
256 origTime = time;
257 origSession = session;
260 QString EditTaskDialog::taskName() const
262 return( _name->text() );
265 void EditTaskDialog::status(long *time, long *timeDiff, long *session,
266 long *sessionDiff, DesktopList *desktopList) const
268 if ( _absoluteRB->isChecked() ) {
269 *time = _timeTW->time();
270 *session = _sessionTW->time();
271 } else {
272 int diff = _diffTW->time();
273 if ( _operator->currentIndex() == 1) {
274 diff = -diff;
276 *time = origTime + diff;
277 *session = origSession + diff;
280 *timeDiff = *time - origTime;
281 *sessionDiff = *session - origSession;
283 for ( int i = 0; i < _deskBox.count(); i++ ) {
284 if ( _deskBox[i]->isChecked() )
285 desktopList->append( i );
289 #include "edittaskdialog.moc"