Build, if that was not necessary blame cartman who told me "sure" :-D
[kdepim.git] / libkdepim / designerfields.cpp
blobea28bb13f3ca8de260afeeb9d889719769ec89b8
1 /*
2 This file is part of libkdepim.
4 Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
5 Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
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.
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.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
23 #include <qcheckbox.h>
24 #include <qcombobox.h>
25 #include <qdatetimeedit.h>
26 #include <qlayout.h>
27 #include <qobjectlist.h>
28 #include <qspinbox.h>
29 #include <qregexp.h>
30 #include <qtextedit.h>
31 #include <qwidgetfactory.h>
33 #include <kdatepicker.h>
34 #include <kdatetimewidget.h>
35 #include <kdialog.h>
36 #include <klineedit.h>
37 #include <kstandarddirs.h>
38 #include <kdebug.h>
40 #include "designerfields.h"
42 using namespace KPIM;
44 DesignerFields::DesignerFields( const QString &uiFile, QWidget *parent,
45 const char *name )
46 : QWidget( parent, name )
48 initGUI( uiFile );
51 void DesignerFields::initGUI( const QString &uiFile )
53 QVBoxLayout *layout = new QVBoxLayout( this );
55 QWidget *wdg = QWidgetFactory::create( uiFile, 0, this );
56 if ( !wdg ) {
57 kdError() << "No ui file found" << endl;
58 return;
61 mTitle = wdg->caption();
62 mIdentifier = wdg->name();
64 layout->addWidget( wdg );
66 QObjectList *list = wdg->queryList( "QWidget" );
67 QObjectListIt it( *list );
69 QStringList allowedTypes;
70 allowedTypes << "QLineEdit"
71 << "QTextEdit"
72 << "QSpinBox"
73 << "QCheckBox"
74 << "QComboBox"
75 << "QDateTimeEdit"
76 << "KLineEdit"
77 << "KDateTimeWidget"
78 << "KDatePicker";
80 while ( it.current() ) {
81 if ( allowedTypes.contains( it.current()->className() ) ) {
82 QString name = it.current()->name();
83 if ( name.startsWith( "X_" ) ) {
84 name = name.mid( 2 );
86 QWidget *widget = static_cast<QWidget*>( it.current() );
87 if ( !name.isEmpty() )
88 mWidgets.insert( name, widget );
90 if ( it.current()->inherits( "QLineEdit" ) )
91 connect( it.current(), SIGNAL( textChanged( const QString& ) ),
92 SIGNAL( modified() ) );
93 else if ( it.current()->inherits( "QSpinBox" ) )
94 connect( it.current(), SIGNAL( valueChanged( int ) ),
95 SIGNAL( modified() ) );
96 else if ( it.current()->inherits( "QCheckBox" ) )
97 connect( it.current(), SIGNAL( toggled( bool ) ),
98 SIGNAL( modified() ) );
99 else if ( it.current()->inherits( "QComboBox" ) )
100 connect( it.current(), SIGNAL( activated( const QString& ) ),
101 SIGNAL( modified() ) );
102 else if ( it.current()->inherits( "QDateTimeEdit" ) )
103 connect( it.current(), SIGNAL( valueChanged( const QDateTime& ) ),
104 SIGNAL( modified() ) );
105 else if ( it.current()->inherits( "KDateTimeWidget" ) )
106 connect( it.current(), SIGNAL( valueChanged( const QDateTime& ) ),
107 SIGNAL( modified() ) );
108 else if ( it.current()->inherits( "KDatePicker" ) )
109 connect( it.current(), SIGNAL( dateChanged( QDate ) ),
110 SIGNAL( modified() ) );
111 else if ( it.current()->inherits( "QTextEdit" ) )
112 connect( it.current(), SIGNAL( textChanged() ),
113 SIGNAL( modified() ) );
115 if ( !widget->isEnabled() )
116 mDisabledWidgets.append( widget );
120 ++it;
123 delete list;
126 QString DesignerFields::identifier() const
128 return mIdentifier;
131 QString DesignerFields::title() const
133 return mTitle;
136 void DesignerFields::load( DesignerFields::Storage *storage )
138 QStringList keys = storage->keys();
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 QMap<QString, QWidget *>::ConstIterator widIt;
144 for ( widIt = mWidgets.begin(); widIt != mWidgets.end(); ++widIt ) {
145 QString value;
146 if ( widIt.data()->inherits( "QLineEdit" ) ) {
147 QLineEdit *wdg = static_cast<QLineEdit*>( widIt.data() );
148 wdg->setText( QString::null );
149 } else if ( widIt.data()->inherits( "QSpinBox" ) ) {
150 QSpinBox *wdg = static_cast<QSpinBox*>( widIt.data() );
151 wdg->setValue( wdg->minValue() );
152 } else if ( widIt.data()->inherits( "QCheckBox" ) ) {
153 QCheckBox *wdg = static_cast<QCheckBox*>( widIt.data() );
154 wdg->setChecked( false );
155 } else if ( widIt.data()->inherits( "QDateTimeEdit" ) ) {
156 QDateTimeEdit *wdg = static_cast<QDateTimeEdit*>( widIt.data() );
157 wdg->setDateTime( QDateTime::currentDateTime() );
158 } else if ( widIt.data()->inherits( "KDateTimeWidget" ) ) {
159 KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( widIt.data() );
160 wdg->setDateTime( QDateTime::currentDateTime() );
161 } else if ( widIt.data()->inherits( "KDatePicker" ) ) {
162 KDatePicker *wdg = static_cast<KDatePicker*>( widIt.data() );
163 wdg->setDate( QDate::currentDate() );
164 } else if ( widIt.data()->inherits( "QComboBox" ) ) {
165 QComboBox *wdg = static_cast<QComboBox*>( widIt.data() );
166 wdg->setCurrentItem( 0 );
167 } else if ( widIt.data()->inherits( "QTextEdit" ) ) {
168 QTextEdit *wdg = static_cast<QTextEdit*>( widIt.data() );
169 wdg->setText( QString::null );
173 QStringList::ConstIterator it2;
174 for ( it2 = keys.begin(); it2 != keys.end(); ++it2 ) {
175 QString value = storage->read( *it2 );
177 QMap<QString, QWidget *>::ConstIterator it = mWidgets.find( *it2 );
178 if ( it != mWidgets.end() ) {
179 if ( it.data()->inherits( "QLineEdit" ) ) {
180 QLineEdit *wdg = static_cast<QLineEdit*>( it.data() );
181 wdg->setText( value );
182 } else if ( it.data()->inherits( "QSpinBox" ) ) {
183 QSpinBox *wdg = static_cast<QSpinBox*>( it.data() );
184 wdg->setValue( value.toInt() );
185 } else if ( it.data()->inherits( "QCheckBox" ) ) {
186 QCheckBox *wdg = static_cast<QCheckBox*>( it.data() );
187 wdg->setChecked( value == "true" || value == "1" );
188 } else if ( it.data()->inherits( "QDateTimeEdit" ) ) {
189 QDateTimeEdit *wdg = static_cast<QDateTimeEdit*>( it.data() );
190 wdg->setDateTime( QDateTime::fromString( value, Qt::ISODate ) );
191 } else if ( it.data()->inherits( "KDateTimeWidget" ) ) {
192 KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() );
193 wdg->setDateTime( QDateTime::fromString( value, Qt::ISODate ) );
194 } else if ( it.data()->inherits( "KDatePicker" ) ) {
195 KDatePicker *wdg = static_cast<KDatePicker*>( it.data() );
196 wdg->setDate( QDate::fromString( value, Qt::ISODate ) );
197 } else if ( it.data()->inherits( "QComboBox" ) ) {
198 QComboBox *wdg = static_cast<QComboBox*>( it.data() );
199 wdg->setCurrentText( value );
200 } else if ( it.data()->inherits( "QTextEdit" ) ) {
201 QTextEdit *wdg = static_cast<QTextEdit*>( it.data() );
202 wdg->setText( value );
208 void DesignerFields::save( DesignerFields::Storage *storage )
210 QMap<QString, QWidget*>::Iterator it;
211 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
212 QString value;
213 if ( it.data()->inherits( "QLineEdit" ) ) {
214 QLineEdit *wdg = static_cast<QLineEdit*>( it.data() );
215 value = wdg->text();
216 } else if ( it.data()->inherits( "QSpinBox" ) ) {
217 QSpinBox *wdg = static_cast<QSpinBox*>( it.data() );
218 value = QString::number( wdg->value() );
219 } else if ( it.data()->inherits( "QCheckBox" ) ) {
220 QCheckBox *wdg = static_cast<QCheckBox*>( it.data() );
221 value = ( wdg->isChecked() ? "true" : "false" );
222 } else if ( it.data()->inherits( "QDateTimeEdit" ) ) {
223 QDateTimeEdit *wdg = static_cast<QDateTimeEdit*>( it.data() );
224 value = wdg->dateTime().toString( Qt::ISODate );
225 } else if ( it.data()->inherits( "KDateTimeWidget" ) ) {
226 KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() );
227 value = wdg->dateTime().toString( Qt::ISODate );
228 } else if ( it.data()->inherits( "KDatePicker" ) ) {
229 KDatePicker *wdg = static_cast<KDatePicker*>( it.data() );
230 value = wdg->date().toString( Qt::ISODate );
231 } else if ( it.data()->inherits( "QComboBox" ) ) {
232 QComboBox *wdg = static_cast<QComboBox*>( it.data() );
233 value = wdg->currentText();
234 } else if ( it.data()->inherits( "QTextEdit" ) ) {
235 QTextEdit *wdg = static_cast<QTextEdit*>( it.data() );
236 value = wdg->text();
239 storage->write( it.key(), value );
243 void DesignerFields::setReadOnly( bool readOnly )
245 QMap<QString, QWidget*>::Iterator it;
246 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it )
247 if ( mDisabledWidgets.find( it.data() ) == mDisabledWidgets.end() )
248 it.data()->setEnabled( !readOnly );
251 #include "designerfields.moc"