edittaskdialog.cpp
00001 /* 00002 * karm 00003 * This file only: Copyright (C) 1999 Espen Sand, espensa@online.no 00004 * Modifications (see CVS log) Copyright (C) 2000 Klar�lvdalens 00005 * Datakonsult AB <kalle@dalheimer.de>, Jesper Pedersen <blackie@kde.org> 00006 * 00007 * 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00021 * 00022 */ 00023 #include <tqbuttongroup.h> 00024 #include <tqcombobox.h> 00025 #include <tqgroupbox.h> 00026 #include <tqhbox.h> 00027 #include <tqlabel.h> 00028 #include <tqlayout.h> 00029 #include <tqlineedit.h> 00030 #include <tqradiobutton.h> 00031 #include <tqsizepolicy.h> 00032 #include <tqstring.h> 00033 #include <tqwidget.h> 00034 #include <tqwhatsthis.h> 00035 00036 #include <klocale.h> // i18n 00037 #include <kwinmodule.h> 00038 00039 #include "edittaskdialog.h" 00040 #include "ktimewidget.h" 00041 #include "kdebug.h" 00042 00043 EditTaskDialog::EditTaskDialog( TQString caption, bool editDlg, 00044 DesktopList* desktopList) 00045 : KDialogBase(0, "EditTaskDialog", true, caption, Ok|Cancel, Ok, true ), 00046 origTime( 0 ), origSession( 0 ) 00047 { 00048 TQWidget *page = new TQWidget( this ); 00049 setMainWidget(page); 00050 KWinModule kwinmodule(0, KWinModule::INFO_DESKTOP); 00051 00052 TQVBoxLayout *lay1 = new TQVBoxLayout(page); 00053 00054 TQHBoxLayout *lay2 = new TQHBoxLayout(); 00055 lay1->addLayout(lay2); 00056 00057 // The name of the widget 00058 TQLabel *label = new TQLabel( i18n("Task &name:"), page, "name" ); 00059 lay2->addWidget( label ); 00060 lay2->addSpacing(5); 00061 00062 00063 _name = new TQLineEdit( page, "lineedit" ); 00064 00065 _name->setMinimumWidth(fontMetrics().maxWidth()*15); 00066 lay2->addWidget( _name ); 00067 label->setBuddy( _name ); 00068 00069 00070 // The "Edit Absolut" radio button 00071 lay1->addSpacing(10);lay1->addStretch(1); 00072 _absoluteRB = new TQRadioButton( i18n( "Edit &absolute" ), page, 00073 "_absoluteRB" ); 00074 lay1->addWidget( _absoluteRB ); 00075 connect( _absoluteRB, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotAbsolutePressed() ) ); 00076 00077 00078 // Absolute times 00079 TQHBoxLayout *lay5 = new TQHBoxLayout(); 00080 lay1->addLayout(lay5); 00081 lay5->addSpacing(20); 00082 TQGridLayout *lay3 = new TQGridLayout( 2, 2, -1, "lay3" ); 00083 lay5->addLayout(lay3); 00084 00085 _sessionLA = new TQLabel( i18n("&Session time: "), page, "session time" ); 00086 00087 // Time 00088 _timeLA = new TQLabel( i18n("&Time:"), page, "time" ); 00089 lay3->addWidget( _timeLA, 0, 0 ); 00090 _timeLA->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, 00091 (TQSizePolicy::SizeType)0, 00092 0, 00093 0, 00094 _timeLA->sizePolicy().hasHeightForWidth()) ); 00095 00096 // Based on measuring pixels in a screenshot, it looks like the fontmetrics 00097 // call includes the ampersand when calculating the width. To be sure 00098 // things will line up (no matter what language or widget style), set all 00099 // three date entry label controls to the same width. 00100 _timeLA->setMinimumWidth( fontMetrics().width( _sessionLA->text() ) ); 00101 00102 _timeTW = new KArmTimeWidget( page, "_timeTW" ); 00103 lay3->addWidget( _timeTW, 0, 1 ); 00104 _timeLA->setBuddy( _timeTW ); 00105 00106 00107 // Session 00108 lay3->addWidget( _sessionLA, 1, 0 ); 00109 00110 _sessionTW = new KArmTimeWidget( page, "_sessionTW" ); 00111 lay3->addWidget( _sessionTW, 1, 1 ); 00112 _sessionLA->setBuddy( _sessionTW ); 00113 _sessionLA->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, 00114 (TQSizePolicy::SizeType)0, 00115 0, 00116 0, 00117 _sessionLA->sizePolicy().hasHeightForWidth()) ); 00118 _sessionLA->setMinimumWidth( fontMetrics().width( _sessionLA->text() ) ); 00119 00120 00121 // The "Edit relative" radio button 00122 lay1->addSpacing(10); 00123 lay1->addStretch(1); 00124 _relativeRB = new TQRadioButton( i18n( "Edit &relative (apply to both time and" 00125 " session time)" ), page, "_relativeRB" ); 00126 lay1->addWidget( _relativeRB ); 00127 connect( _relativeRB, TQT_SIGNAL( clicked() ), this, TQT_SLOT(slotRelativePressed()) ); 00128 00129 // The relative times 00130 TQHBoxLayout *lay4 = new TQHBoxLayout(); 00131 lay1->addLayout( lay4 ); 00132 lay4->addSpacing(20); 00133 00134 _operator = new TQComboBox(page); 00135 _operator->insertItem( TQString::fromLatin1( "+" ) ); 00136 _operator->insertItem( TQString::fromLatin1( "-" ) ); 00137 _operator->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, 00138 (TQSizePolicy::SizeType)0, 00139 0, 00140 0, 00141 _operator->sizePolicy().hasHeightForWidth()) ); 00142 //kdDebug() << "text width=" << fontMetrics().width( _sessionLA->text() ) << endl; 00143 _operator->setMinimumWidth( fontMetrics().width( _sessionLA->text() ) ); 00144 lay4->addWidget( _operator ); 00145 00146 _diffTW = new KArmTimeWidget( page, "_sessionAddTW" ); 00147 lay4->addWidget( _diffTW ); 00148 00149 desktopCount = kwinmodule.numberOfDesktops(); 00150 00151 // If desktopList contains higher numbered desktops than desktopCount then 00152 // delete those from desktopList. This may be the case if the user has 00153 // configured virtual desktops. The values in desktopList are sorted. 00154 if ( (desktopList != 0) && (desktopList->size() > 0) ) 00155 { 00156 DesktopList::iterator rit = desktopList->begin(); 00157 while (*rit < desktopCount && rit!=desktopList->end()) 00158 { 00159 ++rit; 00160 } 00161 desktopList->erase(rit, desktopList->end()); 00162 } 00163 00164 // The "Choose Desktop" checkbox 00165 lay1->addSpacing(10); 00166 lay1->addStretch(1); 00167 00168 _desktopCB = new TQCheckBox(i18n("A&uto tracking"), page); 00169 _desktopCB->setEnabled(true); 00170 lay1->addWidget(_desktopCB); 00171 00172 TQGroupBox* groupBox; 00173 { 00174 int lines = (int)(desktopCount/2); 00175 if (lines*2 != desktopCount) lines++; 00176 groupBox = new TQButtonGroup( lines, Qt::Horizontal, 00177 i18n("In Desktop"), page, "_desktopsGB"); 00178 } 00179 lay1->addWidget(groupBox); 00180 00181 TQHBoxLayout *lay6 = new TQHBoxLayout(); 00182 00183 lay1->addLayout(lay6); 00184 for (int i=0; i<desktopCount; i++) { 00185 _deskBox.push_back(new TQCheckBox(groupBox,TQString::number(i).latin1())); 00186 _deskBox[i]->setText(kwinmodule.desktopName(i+1)); 00187 _deskBox[i]->setChecked(false); 00188 00189 lay6->addWidget(_deskBox[i]); 00190 } 00191 // check specified Desktop Check Boxes 00192 bool enableDesktops = false; 00193 00194 if ( (desktopList != 0) && (desktopList->size() > 0) ) 00195 { 00196 DesktopList::iterator it = desktopList->begin(); 00197 while (it != desktopList->end()) 00198 { 00199 _deskBox[*it]->setChecked(true); 00200 it++; 00201 } 00202 enableDesktops = true; 00203 } 00204 // if some desktops were specified, then enable the parent box 00205 _desktopCB->setChecked(enableDesktops); 00206 00207 for (int i=0; i<desktopCount; i++) 00208 _deskBox[i]->setEnabled(enableDesktops); 00209 00210 connect(_desktopCB, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotAutoTrackingPressed())); 00211 00212 lay1->addStretch(1); 00213 00214 00215 if ( editDlg ) { 00216 // This is an edit dialog. 00217 _operator->setFocus(); 00218 } 00219 else { 00220 // This is an initial dialog 00221 _name->setFocus(); 00222 } 00223 00224 slotRelativePressed(); 00225 00226 // Whats this help. 00227 TQWhatsThis::add( _name, 00228 i18n( "Enter the name of the task here. " 00229 "This name is for your eyes only.")); 00230 TQWhatsThis::add( _absoluteRB, 00231 i18n( "Use this option to set the time spent on this task " 00232 "to an absolute value.\n\nFor example, if you have " 00233 "worked exactly four hours on this task during the current " 00234 "session, you would set the Session time to 4 hr." ) ); 00235 TQWhatsThis::add( _relativeRB, 00236 i18n( "Use this option to change the time spent on this task " 00237 "relative to its current value.\n\nFor example, if you worked " 00238 "on this task for one hour without the timer running, you " 00239 "would add 1 hr." ) ); 00240 TQWhatsThis::add( _timeTW, 00241 i18n( "This is the time the task has been " 00242 "running since all times were reset.")); 00243 TQWhatsThis::add( _sessionTW, 00244 i18n( "This is the time the task has been running this " 00245 "session.")); 00246 TQWhatsThis::add( _diffTW, i18n( "Specify how much time to add or subtract " 00247 "to the overall and session time")); 00248 00249 TQWhatsThis::add( _desktopCB, 00250 i18n( "Use this option to automatically start the timer " 00251 "on this task when you switch to the specified desktop(s)." ) ); 00252 TQWhatsThis::add( groupBox, 00253 i18n( "Select the desktop(s) that will automatically start the " 00254 "timer on this task." ) ); 00255 } 00256 00257 00258 void EditTaskDialog::slotAbsolutePressed() 00259 { 00260 _relativeRB->setChecked( false ); 00261 _absoluteRB->setChecked( true ); 00262 00263 _operator->setEnabled( false ); 00264 _diffTW->setEnabled( false ); 00265 00266 _timeLA->setEnabled( true ); 00267 _sessionLA->setEnabled( true ); 00268 _timeTW->setEnabled( true ); 00269 _sessionTW->setEnabled( true ); 00270 } 00271 00272 void EditTaskDialog::slotRelativePressed() 00273 { 00274 _relativeRB->setChecked( true ); 00275 _absoluteRB->setChecked( false ); 00276 00277 _operator->setEnabled( true ); 00278 _diffTW->setEnabled( true ); 00279 00280 _timeLA->setEnabled( false ); 00281 _sessionLA->setEnabled( false ); 00282 _timeTW->setEnabled( false ); 00283 _sessionTW->setEnabled( false ); 00284 } 00285 00286 void EditTaskDialog::slotAutoTrackingPressed() 00287 { 00288 bool checked = _desktopCB->isChecked(); 00289 for (unsigned int i=0; i<_deskBox.size(); i++) 00290 _deskBox[i]->setEnabled(checked); 00291 00292 if (!checked) // uncheck all desktop boxes 00293 for (int i=0; i<desktopCount; i++) 00294 _deskBox[i]->setChecked(false); 00295 } 00296 00297 void EditTaskDialog::setTask( const TQString &name, long time, long session ) 00298 { 00299 _name->setText( name ); 00300 00301 _timeTW->setTime( time ); 00302 _sessionTW->setTime( session ); 00303 origTime = time; 00304 origSession = session; 00305 } 00306 00307 00308 TQString EditTaskDialog::taskName() const 00309 { 00310 return( _name->text() ); 00311 } 00312 00313 00314 void EditTaskDialog::status(long *time, long *timeDiff, long *session, 00315 long *sessionDiff, DesktopList *desktopList) const 00316 { 00317 if ( _absoluteRB->isChecked() ) { 00318 *time = _timeTW->time(); 00319 *session = _sessionTW->time(); 00320 } 00321 else { 00322 int diff = _diffTW->time(); 00323 if ( _operator->currentItem() == 1) { 00324 diff = -diff; 00325 } 00326 *time = origTime + diff; 00327 *session = origSession + diff; 00328 } 00329 00330 *timeDiff = *time - origTime; 00331 *sessionDiff = *session - origSession; 00332 00333 for (unsigned int i=0; i<_deskBox.size(); i++) { 00334 if (_deskBox[i]->isChecked()) 00335 desktopList->push_back(i); 00336 } 00337 } 00338 00339 #include "edittaskdialog.moc"