2
Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
4
This library is free software; you can redistribute it and/or modify it
5
under the terms of the GNU Library General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or (at your
7
option) any later version.
9
This library is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12
License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to the
16
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
#include "editorlocation.h"
22
#include "locationmodel.h"
23
#include "ui_editorlocation.h"
25
#include <KDE/KABC/Addressee>
26
#include <KDE/KMessageBox>
28
#include <QtGui/QCheckBox>
29
#include <QtGui/QDataWidgetMapper>
30
#include <QtGui/QGroupBox>
32
class AddressTypeDialog : public KDialog
35
AddressTypeDialog( QWidget *parent = 0 )
38
setCaption( i18nc( "street/postal", "New Address" ) );
40
QWidget *page = new QWidget(this);
41
setMainWidget( page );
42
QVBoxLayout *layout = new QVBoxLayout( page );
43
layout->setSpacing( KDialog::spacingHint() );
44
layout->setMargin( 0 );
46
QGroupBox *box = new QGroupBox( i18nc( "street/postal", "Address Types" ), page );
47
layout->addWidget( box );
48
mGroup = new QButtonGroup( box );
49
mGroup->setExclusive ( false );
51
QGridLayout *buttonLayout = new QGridLayout( box );
53
mTypeList = KABC::Address::typeList();
54
mTypeList.removeAll( KABC::Address::Pref );
56
KABC::Address::TypeList::ConstIterator it;
59
for ( it = mTypeList.constBegin(); it != mTypeList.constEnd(); ++it, ++i ) {
60
QCheckBox *checkBox = new QCheckBox( KABC::Address::typeLabel( *it ), box );
61
buttonLayout->addWidget( checkBox, row, i % 3 );
66
mGroup->addButton( checkBox );
70
KABC::Address::Type type() const
72
KABC::Address::Type type;
73
for ( int i = 0; i < mGroup->buttons().count(); ++i ) {
74
QCheckBox *box = dynamic_cast<QCheckBox*>( mGroup->buttons().at( i ) );
75
if ( box && box->isChecked() )
76
type |= mTypeList[ i ];
85
KABC::Address::TypeList mTypeList;
89
class EditorLocation::Private
91
EditorLocation *const q;
94
explicit Private( EditorLocation *parent ) : q( parent )
96
mUi.setupUi( parent );
97
mModel = new LocationModel( q );
99
mUi.addressSelectionCombo->setModel( mModel );
100
mUi.addressSelectionCombo->setModelColumn( 0 );
102
mMapper = new QDataWidgetMapper( q );
103
mMapper->setModel( mModel );
104
mMapper->addMapping( mUi.streetLineEdit, 1, "plainText" );
105
mMapper->addMapping( mUi.postOfficeBoxLineEdit, 2 );
106
mMapper->addMapping( mUi.localityLineEdit, 3 );
107
mMapper->addMapping( mUi.regionLineEdit, 4 );
108
mMapper->addMapping( mUi.postalCodeLineEdit, 5 );
109
mMapper->addMapping( mUi.countryLineEdit, 6 );
110
mMapper->addMapping( mUi.editLabelLineEdit, 7 );
113
q->connect( mUi.addressSelectionCombo, SIGNAL( activated( int ) ),
114
mMapper, SLOT( setCurrentIndex( int ) ) );
115
q->connect( mUi.addAddressButton, SIGNAL( clicked() ),
116
SLOT( addAddress() ) );
117
q->connect( mUi.deleteAddressButton, SIGNAL( clicked() ),
118
SLOT( removeAddress() ) );
119
q->connect( mModel, SIGNAL( rowsInserted( const QModelIndex&, int, int ) ),
120
SLOT( addressCountChanged() ) );
121
q->connect( mModel, SIGNAL( rowsRemoved( const QModelIndex&, int, int ) ),
122
SLOT( addressCountChanged() ) );
124
addressCountChanged();
129
AddressTypeDialog dlg;
133
const KABC::Address::Type addressType = dlg.type();
135
if ( mModel->insertRows( 0, 1 ) ) {
136
mModel->setData( mModel->index( 0, 0 ), QVariant::fromValue( static_cast<int>( addressType ) ) );
137
mUi.addressSelectionCombo->setCurrentIndex( 0 );
138
mMapper->setCurrentIndex( 0 );
144
const int answer = KMessageBox::questionYesNo( 0, i18n( "Do you really want to delete this address?" ),
145
i18n( "Delete Address" ),
146
KGuiItem( i18n("Delete") ) );
147
if ( answer == KMessageBox::No )
150
const int index = mMapper->currentIndex();
151
mModel->removeRows( index, 1 );
152
if ( index >= mModel->rowCount() )
153
mMapper->setCurrentIndex( mModel->rowCount() - 1 );
155
mMapper->setCurrentIndex( index );
157
if ( mModel->rowCount() == 0 ) {
158
// We have to cleanup the fields ourself in this case,
159
// QDataWidgetMapper does not handle a non-existing index
160
for ( int column = 1; column < 8; ++column ) {
161
QLineEdit *lineEdit = qobject_cast<QLineEdit*>( mMapper->mappedWidgetAt( column ) );
165
QTextEdit *textEdit = qobject_cast<QTextEdit*>( mMapper->mappedWidgetAt( column ) );
173
void addressCountChanged()
175
const bool enabled = (mModel->rowCount() > 0);
177
mUi.addressSelectionCombo->setEnabled( enabled );
178
mUi.deleteAddressButton->setEnabled( enabled );
179
for ( int column = 1; column < 8; ++column ) {
180
QWidget *widget = mMapper->mappedWidgetAt( column );
182
widget->setEnabled( enabled );
187
Ui::EditorLocation mUi;
189
KABC::Addressee mContact;
190
LocationModel *mModel;
191
QDataWidgetMapper *mMapper;
195
EditorLocation::EditorLocation( QWidget *parent )
196
: EditorBase( parent ), d( new Private( this ) )
200
EditorLocation::~EditorLocation()
205
void EditorLocation::loadContact( const KABC::Addressee &contact, const Akonadi::ContactMetaData& )
207
d->mModel->setLocations( contact.addresses() );
208
d->mUi.addressSelectionCombo->setCurrentIndex( 0 );
209
d->mMapper->setCurrentIndex( 0 );
210
d->addressCountChanged();
213
void EditorLocation::saveContact( KABC::Addressee &contact, Akonadi::ContactMetaData& ) const
215
const KABC::Address::List oldAddresses = contact.addresses();
216
foreach ( const KABC::Address &oldAddress, oldAddresses )
217
contact.removeAddress( oldAddress );
219
foreach ( const KABC::Address &newAddress, d->mModel->locations() )
220
contact.insertAddress( newAddress );
223
#include "editorlocation.moc"