libtdepim

designerfields.cpp
1 /*
2  This file is part of libtdepim.
3 
4  Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
5  Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library 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 GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include <tqcheckbox.h>
24 #include <tqcombobox.h>
25 #include <tqdatetimeedit.h>
26 #include <tqlayout.h>
27 #include <tqobjectlist.h>
28 #include <tqspinbox.h>
29 #include <tqregexp.h>
30 #include <tqtextedit.h>
31 #include <tqwidgetfactory.h>
32 
33 #include <kdatepicker.h>
34 #include <kdatetimewidget.h>
35 #include <kdialog.h>
36 #include <klineedit.h>
37 #include <kstandarddirs.h>
38 #include <kdebug.h>
39 
40 #include "designerfields.h"
41 
42 using namespace KPIM;
43 
44 DesignerFields::DesignerFields( const TQString &uiFile, TQWidget *parent,
45  const char *name )
46  : TQWidget( parent, name )
47 {
48  initGUI( uiFile );
49 }
50 
51 void DesignerFields::initGUI( const TQString &uiFile )
52 {
53  TQVBoxLayout *layout = new TQVBoxLayout( this );
54 
55  TQWidget *wdg = TQWidgetFactory::create( uiFile, 0, this );
56  if ( !wdg ) {
57  kdError() << "No ui file found" << endl;
58  return;
59  }
60 
61  mTitle = wdg->caption();
62  mIdentifier = wdg->name();
63 
64  layout->addWidget( wdg );
65 
66  TQObjectList *list = wdg->queryList( "TQWidget" );
67  TQObjectListIt it( *list );
68 
69  TQStringList allowedTypes;
70  allowedTypes << "TQLineEdit"
71  << "TQTextEdit"
72  << "TQSpinBox"
73  << "TQCheckBox"
74  << "TQComboBox"
75  << "TQDateTimeEdit"
76  << "KLineEdit"
77  << "KDateTimeWidget"
78  << "KDatePicker";
79 
80  while ( it.current() ) {
81  if ( allowedTypes.contains( it.current()->className() ) ) {
82  TQString name = it.current()->name();
83  if ( name.startsWith( "X_" ) ) {
84  name = name.mid( 2 );
85 
86  TQWidget *widget = static_cast<TQWidget*>( it.current() );
87  if ( !name.isEmpty() )
88  mWidgets.insert( name, widget );
89 
90  if ( it.current()->inherits( "TQLineEdit" ) )
91  connect( it.current(), TQ_SIGNAL( textChanged( const TQString& ) ),
92  TQ_SIGNAL( modified() ) );
93  else if ( it.current()->inherits( "TQSpinBox" ) )
94  connect( it.current(), TQ_SIGNAL( valueChanged( int ) ),
95  TQ_SIGNAL( modified() ) );
96  else if ( it.current()->inherits( "TQCheckBox" ) )
97  connect( it.current(), TQ_SIGNAL( toggled( bool ) ),
98  TQ_SIGNAL( modified() ) );
99  else if ( it.current()->inherits( "TQComboBox" ) )
100  connect( it.current(), TQ_SIGNAL( activated( const TQString& ) ),
101  TQ_SIGNAL( modified() ) );
102  else if ( it.current()->inherits( "TQDateTimeEdit" ) )
103  connect( it.current(), TQ_SIGNAL( valueChanged( const TQDateTime& ) ),
104  TQ_SIGNAL( modified() ) );
105  else if ( it.current()->inherits( "KDateTimeWidget" ) )
106  connect( it.current(), TQ_SIGNAL( valueChanged( const TQDateTime& ) ),
107  TQ_SIGNAL( modified() ) );
108  else if ( it.current()->inherits( "KDatePicker" ) )
109  connect( it.current(), TQ_SIGNAL( dateChanged( TQDate ) ),
110  TQ_SIGNAL( modified() ) );
111  else if ( it.current()->inherits( "TQTextEdit" ) )
112  connect( it.current(), TQ_SIGNAL( textChanged() ),
113  TQ_SIGNAL( modified() ) );
114 
115  if ( !widget->isEnabled() )
116  mDisabledWidgets.append( widget );
117  }
118  }
119 
120  ++it;
121  }
122 
123  delete list;
124 }
125 
126 TQString DesignerFields::identifier() const
127 {
128  return mIdentifier;
129 }
130 
131 TQString DesignerFields::title() const
132 {
133  return mTitle;
134 }
135 
136 void DesignerFields::load( DesignerFields::Storage *storage )
137 {
138  TQStringList keys = storage->keys();
139 
140  // clear all custom page widgets
141  // we can't do this in the following loop, as it works on the
142  // custom fields of the vcard, which may not be set.
143  TQMap<TQString, TQWidget *>::ConstIterator widIt;
144  for ( widIt = mWidgets.begin(); widIt != mWidgets.end(); ++widIt ) {
145  TQString value;
146  if ( widIt.data()->inherits( "TQLineEdit" ) ) {
147  TQLineEdit *wdg = static_cast<TQLineEdit*>( widIt.data() );
148  wdg->setText( TQString() );
149  } else if ( widIt.data()->inherits( "TQSpinBox" ) ) {
150  TQSpinBox *wdg = static_cast<TQSpinBox*>( widIt.data() );
151  wdg->setValue( wdg->minValue() );
152  } else if ( widIt.data()->inherits( "TQCheckBox" ) ) {
153  TQCheckBox *wdg = static_cast<TQCheckBox*>( widIt.data() );
154  wdg->setChecked( false );
155  } else if ( widIt.data()->inherits( "TQDateTimeEdit" ) ) {
156  TQDateTimeEdit *wdg = static_cast<TQDateTimeEdit*>( widIt.data() );
157  wdg->setDateTime( TQDateTime::currentDateTime() );
158  } else if ( widIt.data()->inherits( "KDateTimeWidget" ) ) {
159  KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( widIt.data() );
160  wdg->setDateTime( TQDateTime::currentDateTime() );
161  } else if ( widIt.data()->inherits( "KDatePicker" ) ) {
162  KDatePicker *wdg = static_cast<KDatePicker*>( widIt.data() );
163  wdg->setDate( TQDate::currentDate() );
164  } else if ( widIt.data()->inherits( "TQComboBox" ) ) {
165  TQComboBox *wdg = static_cast<TQComboBox*>( widIt.data() );
166  wdg->setCurrentItem( 0 );
167  } else if ( widIt.data()->inherits( "TQTextEdit" ) ) {
168  TQTextEdit *wdg = static_cast<TQTextEdit*>( widIt.data() );
169  wdg->setText( TQString() );
170  }
171  }
172 
173  TQStringList::ConstIterator it2;
174  for ( it2 = keys.begin(); it2 != keys.end(); ++it2 ) {
175  TQString value = storage->read( *it2 );
176 
177  TQMap<TQString, TQWidget *>::ConstIterator it = mWidgets.find( *it2 );
178  if ( it != mWidgets.end() ) {
179  if ( it.data()->inherits( "TQLineEdit" ) ) {
180  TQLineEdit *wdg = static_cast<TQLineEdit*>( it.data() );
181  wdg->setText( value );
182  } else if ( it.data()->inherits( "TQSpinBox" ) ) {
183  TQSpinBox *wdg = static_cast<TQSpinBox*>( it.data() );
184  wdg->setValue( value.toInt() );
185  } else if ( it.data()->inherits( "TQCheckBox" ) ) {
186  TQCheckBox *wdg = static_cast<TQCheckBox*>( it.data() );
187  wdg->setChecked( value == "true" || value == "1" );
188  } else if ( it.data()->inherits( "TQDateTimeEdit" ) ) {
189  TQDateTimeEdit *wdg = static_cast<TQDateTimeEdit*>( it.data() );
190  wdg->setDateTime( TQDateTime::fromString( value, TQt::ISODate ) );
191  } else if ( it.data()->inherits( "KDateTimeWidget" ) ) {
192  KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() );
193  wdg->setDateTime( TQDateTime::fromString( value, TQt::ISODate ) );
194  } else if ( it.data()->inherits( "KDatePicker" ) ) {
195  KDatePicker *wdg = static_cast<KDatePicker*>( it.data() );
196  wdg->setDate( TQDate::fromString( value, TQt::ISODate ) );
197  } else if ( it.data()->inherits( "TQComboBox" ) ) {
198  TQComboBox *wdg = static_cast<TQComboBox*>( it.data() );
199  wdg->setCurrentText( value );
200  } else if ( it.data()->inherits( "TQTextEdit" ) ) {
201  TQTextEdit *wdg = static_cast<TQTextEdit*>( it.data() );
202  wdg->setText( value );
203  }
204  }
205  }
206 }
207 
208 void DesignerFields::save( DesignerFields::Storage *storage )
209 {
210  TQMap<TQString, TQWidget*>::Iterator it;
211  for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
212  TQString value;
213  if ( it.data()->inherits( "TQLineEdit" ) ) {
214  TQLineEdit *wdg = static_cast<TQLineEdit*>( it.data() );
215  value = wdg->text();
216  } else if ( it.data()->inherits( "TQSpinBox" ) ) {
217  TQSpinBox *wdg = static_cast<TQSpinBox*>( it.data() );
218  value = TQString::number( wdg->value() );
219  } else if ( it.data()->inherits( "TQCheckBox" ) ) {
220  TQCheckBox *wdg = static_cast<TQCheckBox*>( it.data() );
221  value = ( wdg->isChecked() ? "true" : "false" );
222  } else if ( it.data()->inherits( "TQDateTimeEdit" ) ) {
223  TQDateTimeEdit *wdg = static_cast<TQDateTimeEdit*>( it.data() );
224  value = wdg->dateTime().toString( TQt::ISODate );
225  } else if ( it.data()->inherits( "KDateTimeWidget" ) ) {
226  KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() );
227  value = wdg->dateTime().toString( TQt::ISODate );
228  } else if ( it.data()->inherits( "KDatePicker" ) ) {
229  KDatePicker *wdg = static_cast<KDatePicker*>( it.data() );
230  value = wdg->date().toString( TQt::ISODate );
231  } else if ( it.data()->inherits( "TQComboBox" ) ) {
232  TQComboBox *wdg = static_cast<TQComboBox*>( it.data() );
233  value = wdg->currentText();
234  } else if ( it.data()->inherits( "TQTextEdit" ) ) {
235  TQTextEdit *wdg = static_cast<TQTextEdit*>( it.data() );
236  value = wdg->text();
237  }
238 
239  storage->write( it.key(), value );
240  }
241 }
242 
243 void DesignerFields::setReadOnly( bool readOnly )
244 {
245  TQMap<TQString, TQWidget*>::Iterator it;
246  for ( it = mWidgets.begin(); it != mWidgets.end(); ++it )
247  if ( mDisabledWidgets.find( it.data() ) == mDisabledWidgets.end() )
248  it.data()->setEnabled( !readOnly );
249 }
250 
251 #include "designerfields.moc"
TDEPIM classes for drag and drop of mails.