krop's commit fixes my problem in a better way, reverting
[kdepim.git] / libkdepim / designerfields.cpp
bloba2da71a775de0d571ac8900c85af3813f4039a17
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
23 #include "designerfields.h"
25 #include <KDatePicker>
26 #include <KDateTimeWidget>
27 #include <KDebug>
28 #include <KDialog>
29 #include <KLineEdit>
30 #include <KStandardDirs>
32 #include <QCheckBox>
33 #include <QComboBox>
34 #include <QLayout>
35 #include <QObject>
36 #include <QSpinBox>
37 #include <QRegExp>
38 #include <QTextEdit>
39 #include <QFile>
40 #include <QtDesigner/QFormBuilder>
41 #include <QVBoxLayout>
42 #include <Q3DateTimeEdit>
44 using namespace KPIM;
46 DesignerFields::DesignerFields( const QString &uiFile, QWidget *parent,
47 const char *name )
48 : QWidget( parent )
50 setObjectName(name);
51 initGUI( uiFile );
54 void DesignerFields::initGUI( const QString &uiFile )
56 QVBoxLayout *layout = new QVBoxLayout( this );
58 QFile f( uiFile );
59 f.open(QFile::ReadOnly);
60 QFormBuilder builder;
61 QWidget *wdg = builder.load( &f, this );
62 f.close();
63 if ( !wdg ) {
64 kError() <<"No ui file found";
65 return;
68 mTitle = wdg->windowTitle();
69 mIdentifier = wdg->objectName();
71 layout->addWidget( wdg );
73 QStringList allowedTypes;
74 allowedTypes << "QLineEdit"
75 << "QTextEdit"
76 << "QSpinBox"
77 << "QCheckBox"
78 << "QComboBox"
79 << "QDateTimeEdit"
80 << "KLineEdit"
81 << "KDateTimeWidget"
82 << "KDatePicker";
84 QList<QWidget*> list = wdg->findChildren<QWidget*>();
85 QWidget *it;
86 Q_FOREACH( it, list ) {
87 if ( allowedTypes.contains( it->metaObject()->className() ) ) {
88 QString name = it->objectName();
89 if ( name.startsWith( "X_" ) ) {
90 name = name.mid( 2 );
92 QWidget *widget = it;
93 if ( !name.isEmpty() )
94 mWidgets.insert( name, widget );
96 if ( it->inherits( "QLineEdit" ) )
97 connect( it, SIGNAL( textChanged( const QString& ) ),
98 SIGNAL( modified() ) );
99 else if ( it->inherits( "QSpinBox" ) )
100 connect( it, SIGNAL( valueChanged( int ) ),
101 SIGNAL( modified() ) );
102 else if ( it->inherits( "QCheckBox" ) )
103 connect( it, SIGNAL( toggled( bool ) ),
104 SIGNAL( modified() ) );
105 else if ( it->inherits( "QComboBox" ) )
106 connect( it, SIGNAL( activated( const QString& ) ),
107 SIGNAL( modified() ) );
108 else if ( it->inherits( "QDateTimeEdit" ) )
109 connect( it, SIGNAL( valueChanged( const QDateTime& ) ),
110 SIGNAL( modified() ) );
111 else if ( it->inherits( "KDateTimeWidget" ) )
112 connect( it, SIGNAL( valueChanged( const QDateTime& ) ),
113 SIGNAL( modified() ) );
114 else if ( it->inherits( "KDatePicker" ) )
115 connect( it, SIGNAL( dateChanged( QDate ) ),
116 SIGNAL( modified() ) );
117 else if ( it->inherits( "QTextEdit" ) )
118 connect( it, SIGNAL( textChanged() ),
119 SIGNAL( modified() ) );
121 if ( !widget->isEnabled() )
122 mDisabledWidgets.append( widget );
128 QString DesignerFields::identifier() const
130 return mIdentifier;
133 QString DesignerFields::title() const
135 return mTitle;
138 void DesignerFields::load( DesignerFields::Storage *storage )
140 QStringList keys = storage->keys();
142 // clear all custom page widgets
143 // we can't do this in the following loop, as it works on the
144 // custom fields of the vcard, which may not be set.
145 QMap<QString, QWidget *>::ConstIterator widIt;
146 for ( widIt = mWidgets.constBegin(); widIt != mWidgets.constEnd(); ++widIt ) {
147 QString value;
148 if ( widIt.value()->inherits( "QLineEdit" ) ) {
149 QLineEdit *wdg = static_cast<QLineEdit*>( widIt.value() );
150 wdg->setText( QString() );
151 } else if ( widIt.value()->inherits( "QSpinBox" ) ) {
152 QSpinBox *wdg = static_cast<QSpinBox*>( widIt.value() );
153 wdg->setValue( wdg->minimum() );
154 } else if ( widIt.value()->inherits( "QCheckBox" ) ) {
155 QCheckBox *wdg = static_cast<QCheckBox*>( widIt.value() );
156 wdg->setChecked( false );
157 } else if ( widIt.value()->inherits( "QDateTimeEdit" ) ) {
158 Q3DateTimeEdit *wdg = static_cast<Q3DateTimeEdit*>( widIt.value() );
159 wdg->setDateTime( QDateTime::currentDateTime() );
160 } else if ( widIt.value()->inherits( "KDateTimeWidget" ) ) {
161 KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( widIt.value() );
162 wdg->setDateTime( QDateTime::currentDateTime() );
163 } else if ( widIt.value()->inherits( "KDatePicker" ) ) {
164 KDatePicker *wdg = static_cast<KDatePicker*>( widIt.value() );
165 wdg->setDate( QDate::currentDate() );
166 } else if ( widIt.value()->inherits( "QComboBox" ) ) {
167 QComboBox *wdg = static_cast<QComboBox*>( widIt.value() );
168 wdg->setCurrentIndex( 0 );
169 } else if ( widIt.value()->inherits( "QTextEdit" ) ) {
170 QTextEdit *wdg = static_cast<QTextEdit*>( widIt.value() );
171 wdg->setPlainText( QString() );
175 QStringList::ConstIterator it2;
176 for ( it2 = keys.constBegin(); it2 != keys.constEnd(); ++it2 ) {
177 QString value = storage->read( *it2 );
179 QMap<QString, QWidget *>::ConstIterator it = mWidgets.constFind( *it2 );
180 if ( it != mWidgets.constEnd() ) {
181 if ( it.value()->inherits( "QLineEdit" ) ) {
182 QLineEdit *wdg = static_cast<QLineEdit*>( it.value() );
183 wdg->setText( value );
184 } else if ( it.value()->inherits( "QSpinBox" ) ) {
185 QSpinBox *wdg = static_cast<QSpinBox*>( it.value() );
186 wdg->setValue( value.toInt() );
187 } else if ( it.value()->inherits( "QCheckBox" ) ) {
188 QCheckBox *wdg = static_cast<QCheckBox*>( it.value() );
189 wdg->setChecked( value == "true" || value == "1" );
190 } else if ( it.value()->inherits( "QDateTimeEdit" ) ) {
191 Q3DateTimeEdit *wdg = static_cast<Q3DateTimeEdit*>( it.value() );
192 wdg->setDateTime( QDateTime::fromString( value, Qt::ISODate ) );
193 } else if ( it.value()->inherits( "KDateTimeWidget" ) ) {
194 KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.value() );
195 wdg->setDateTime( QDateTime::fromString( value, Qt::ISODate ) );
196 } else if ( it.value()->inherits( "KDatePicker" ) ) {
197 KDatePicker *wdg = static_cast<KDatePicker*>( it.value() );
198 wdg->setDate( QDate::fromString( value, Qt::ISODate ) );
199 } else if ( it.value()->inherits( "QComboBox" ) ) {
200 QComboBox *wdg = static_cast<QComboBox*>( it.value() );
201 wdg->setItemText( wdg->currentIndex(), value );
202 } else if ( it.value()->inherits( "QTextEdit" ) ) {
203 QTextEdit *wdg = static_cast<QTextEdit*>( it.value() );
204 wdg->setPlainText( value );
210 void DesignerFields::save( DesignerFields::Storage *storage )
212 QMap<QString, QWidget*>::Iterator it;
213 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
214 QString value;
215 if ( it.value()->inherits( "QLineEdit" ) ) {
216 QLineEdit *wdg = static_cast<QLineEdit*>( it.value() );
217 value = wdg->text();
218 } else if ( it.value()->inherits( "QSpinBox" ) ) {
219 QSpinBox *wdg = static_cast<QSpinBox*>( it.value() );
220 value = QString::number( wdg->value() );
221 } else if ( it.value()->inherits( "QCheckBox" ) ) {
222 QCheckBox *wdg = static_cast<QCheckBox*>( it.value() );
223 value = ( wdg->isChecked() ? "true" : "false" );
224 } else if ( it.value()->inherits( "QDateTimeEdit" ) ) {
225 Q3DateTimeEdit *wdg = static_cast<Q3DateTimeEdit*>( it.value() );
226 value = wdg->dateTime().toString( Qt::ISODate );
227 } else if ( it.value()->inherits( "KDateTimeWidget" ) ) {
228 KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.value() );
229 value = wdg->dateTime().toString( Qt::ISODate );
230 } else if ( it.value()->inherits( "KDatePicker" ) ) {
231 KDatePicker *wdg = static_cast<KDatePicker*>( it.value() );
232 value = wdg->date().toString( Qt::ISODate );
233 } else if ( it.value()->inherits( "QComboBox" ) ) {
234 QComboBox *wdg = static_cast<QComboBox*>( it.value() );
235 value = wdg->currentText();
236 } else if ( it.value()->inherits( "QTextEdit" ) ) {
237 QTextEdit *wdg = static_cast<QTextEdit*>( it.value() );
238 value = wdg->toPlainText();
241 storage->write( it.key(), value );
245 void DesignerFields::setReadOnly( bool readOnly )
247 QMap<QString, QWidget*>::Iterator it;
248 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it )
249 if ( !mDisabledWidgets.contains( it.value() ) )
250 it.value()->setEnabled( !readOnly );
253 #include "designerfields.moc"