addresslocationwidget.cpp 6.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
    This file is part of Akonadi Contact.

    Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
    Copyright (C) 2016 Laurent Montel <laurent.montel@kdab.com>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/


#include "addresslocationwidget.h"
25
#include "selectaddresstypecombobox.h"
26

Laurent Montel's avatar
Laurent Montel committed
27
#include <KComboBox>
28
29
30
31
32
33
#include <KLineEdit>
#include <KLocalizedString>

#include <QVBoxLayout>
#include <QLabel>
#include <QCheckBox>
Laurent Montel's avatar
Laurent Montel committed
34
#include <QPushButton>
35
#include <QDebug>
36
37
38
39
40
41
42

AddressLocationWidget::AddressLocationWidget(QWidget *parent)
    : QWidget(parent)
{
    QGridLayout *topLayout = new QGridLayout;
    setLayout(topLayout);

43
44
45
46
    mTypeCombo = new SelectAddressTypeComboBox(this);
    mTypeCombo->setObjectName(QStringLiteral("typeaddress"));
    topLayout->addWidget(mTypeCombo, 0, 0, 1, 2);

Laurent Montel's avatar
Laurent Montel committed
47
48
    QLabel *label = new QLabel(KContacts::Address::streetLabel(), this);
    label->setObjectName(QStringLiteral("streetlabel"));
Laurent Montel's avatar
Laurent Montel committed
49
    topLayout->addWidget(label, 1, 0);
Laurent Montel's avatar
Laurent Montel committed
50

Laurent Montel's avatar
Laurent Montel committed
51
52
53
54
    mStreetEdit = new KLineEdit(this);
    mStreetEdit->setPlaceholderText(i18n("Add Street"));
    mStreetEdit->setObjectName(QStringLiteral("streetlineedit"));
    mStreetEdit->setTrapReturnKey(true);
Laurent Montel's avatar
Laurent Montel committed
55
    topLayout->addWidget(mStreetEdit, 2, 0);
Laurent Montel's avatar
Laurent Montel committed
56
57


Laurent Montel's avatar
Laurent Montel committed
58
59

    label = new QLabel(KContacts::Address::postOfficeBoxLabel(), this);
Laurent Montel's avatar
Laurent Montel committed
60
    label->setObjectName(QStringLiteral("postofficeboxlabel"));
Laurent Montel's avatar
Laurent Montel committed
61
    topLayout->addWidget(label, 1, 1);
Laurent Montel's avatar
Laurent Montel committed
62
63

    mPOBoxEdit = new KLineEdit(this);
Laurent Montel's avatar
Laurent Montel committed
64
    mPOBoxEdit->setPlaceholderText(i18n("Add Post Office Box"));
Laurent Montel's avatar
Laurent Montel committed
65
66
    mPOBoxEdit->setObjectName(QStringLiteral("postofficeboxlineedit"));
    mPOBoxEdit->setTrapReturnKey(true);
Laurent Montel's avatar
Laurent Montel committed
67
68
69
70
71
72
73
74
75
76
    topLayout->addWidget(mPOBoxEdit, 2, 1);

    label = new QLabel(KContacts::Address::postalCodeLabel(), this);
    label->setObjectName(QStringLiteral("postalcodelabel"));
    topLayout->addWidget(label, 3, 0);
    mPostalCodeEdit = new KLineEdit(this);
    mPostalCodeEdit->setPlaceholderText(i18n("Add Postal Code"));
    mPostalCodeEdit->setObjectName(QStringLiteral("postalcodelineedit"));
    mPostalCodeEdit->setTrapReturnKey(true);
    topLayout->addWidget(mPostalCodeEdit, 4, 0);
Laurent Montel's avatar
Laurent Montel committed
77
78
79
80


    label = new QLabel(KContacts::Address::localityLabel(), this);
    label->setObjectName(QStringLiteral("localitylabel"));
Laurent Montel's avatar
Laurent Montel committed
81
    topLayout->addWidget(label, 3, 1);
Laurent Montel's avatar
Laurent Montel committed
82
    mLocalityEdit = new KLineEdit(this);
Laurent Montel's avatar
Laurent Montel committed
83
    mLocalityEdit->setPlaceholderText(i18n("Add Locality"));
Laurent Montel's avatar
Laurent Montel committed
84
85
    mLocalityEdit->setObjectName(QStringLiteral("localitylineedit"));
    mLocalityEdit->setTrapReturnKey(true);
Laurent Montel's avatar
Laurent Montel committed
86
    topLayout->addWidget(mLocalityEdit, 4, 1);
Laurent Montel's avatar
Laurent Montel committed
87
88
89

    label = new QLabel(KContacts::Address::regionLabel(), this);
    label->setObjectName(QStringLiteral("regionlabel"));
Laurent Montel's avatar
Laurent Montel committed
90
    topLayout->addWidget(label, 5, 0);
Laurent Montel's avatar
Laurent Montel committed
91
    mRegionEdit = new KLineEdit(this);
Laurent Montel's avatar
Laurent Montel committed
92
    mRegionEdit->setPlaceholderText(i18n("Add Region"));
Laurent Montel's avatar
Laurent Montel committed
93
94
    mRegionEdit->setObjectName(QStringLiteral("regionlineedit"));
    mRegionEdit->setTrapReturnKey(true);
Laurent Montel's avatar
Laurent Montel committed
95
    topLayout->addWidget(mRegionEdit, 6, 0);
Laurent Montel's avatar
Laurent Montel committed
96
97
98
99


    label = new QLabel(KContacts::Address::countryLabel(), this);
    label->setObjectName(QStringLiteral("countrylabel"));
Laurent Montel's avatar
Laurent Montel committed
100
    topLayout->addWidget(label, 5, 1);
Laurent Montel's avatar
Laurent Montel committed
101
102
103
    mCountryCombo = new KComboBox(this);
    mCountryCombo->setObjectName(QStringLiteral("countrycombobox"));
    mCountryCombo->setEditable(true);
Laurent Montel's avatar
Laurent Montel committed
104
    mCountryCombo->lineEdit()->setPlaceholderText(i18n("Add a Country"));
Laurent Montel's avatar
Laurent Montel committed
105
    mCountryCombo->setDuplicatesEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
106
    topLayout->addWidget(mCountryCombo, 6, 1);
Laurent Montel's avatar
Laurent Montel committed
107

108
109
    mPreferredCheckBox = new QCheckBox(i18nc("street/postal", "This is the preferred address"), this);
    mPreferredCheckBox->setObjectName(QStringLiteral("preferredcheckbox"));
Laurent Montel's avatar
Laurent Montel committed
110
    topLayout->addWidget(mPreferredCheckBox, 7, 0, 1, 2);
Laurent Montel's avatar
Laurent Montel committed
111

Laurent Montel's avatar
Laurent Montel committed
112
113
    mAddAddress = new QPushButton(i18n("Add Address"), this);
    mAddAddress->setObjectName(QStringLiteral("addbuttonaddress"));
Laurent Montel's avatar
Laurent Montel committed
114
    connect(mAddAddress, &QPushButton::clicked, this, &AddressLocationWidget::slotAddAddress);
Laurent Montel's avatar
Laurent Montel committed
115
    topLayout->addWidget(mAddAddress, 8, 0);
Laurent Montel's avatar
Laurent Montel committed
116

117
118
119
120
121
122
123
}

AddressLocationWidget::~AddressLocationWidget()
{

}

Laurent Montel's avatar
Laurent Montel committed
124
125
void AddressLocationWidget::slotAddAddress()
{
Laurent Montel's avatar
Laurent Montel committed
126
127
128
    KContacts::Address addr = address();
    if (!addr.isEmpty()) {
        Q_EMIT addNewAddress(addr);
129
        clear();
Laurent Montel's avatar
Laurent Montel committed
130
    }
Laurent Montel's avatar
Laurent Montel committed
131
132
}

133
134
void AddressLocationWidget::setAddress(const KContacts::Address &address)
{
Laurent Montel's avatar
Laurent Montel committed
135
    mAddress = address;
136
    mTypeCombo->setType(mAddress.type());
Laurent Montel's avatar
Laurent Montel committed
137
    mStreetEdit->setText(mAddress.street());
Laurent Montel's avatar
Laurent Montel committed
138
139
140
141
142
143
144
145
146
147
148
149
150
    mRegionEdit->setText(address.region());
    mLocalityEdit->setText(address.locality());
    mPostalCodeEdit->setText(address.postalCode());
    mPOBoxEdit->setText(address.postOfficeBox());
    mPreferredCheckBox->setChecked(address.type() &KContacts::Address::Pref);
#if 0
    if (address.isEmpty()) {
        mCountryCombo->setItemText(mCountryCombo->currentIndex(),
                                   QLocale::countryToString(QLocale().country()));
    } else {
        mCountryCombo->setItemText(mCountryCombo->currentIndex(), mAddress.country());
    }
#endif
151
152
153
154
155
    //TODO
}

KContacts::Address AddressLocationWidget::address() const
{
Laurent Montel's avatar
Laurent Montel committed
156
157
    KContacts::Address address(mAddress);

158
    address.setType(mTypeCombo->type());
Laurent Montel's avatar
Laurent Montel committed
159
160
161
162
163
    address.setLocality(mLocalityEdit->text());
    address.setRegion(mRegionEdit->text());
    address.setPostalCode(mPostalCodeEdit->text());
    address.setCountry(mCountryCombo->currentText());
    address.setPostOfficeBox(mPOBoxEdit->text());
Laurent Montel's avatar
Laurent Montel committed
164
    address.setStreet(mStreetEdit->text());
Laurent Montel's avatar
Laurent Montel committed
165
166
167
168
169
170
    if (mPreferredCheckBox->isChecked()) {
        address.setType(address.type() | KContacts::Address::Pref);
    } else {
        address.setType(address.type() & ~(KContacts::Address::Pref));
    }
    return address;
171
}
Laurent Montel's avatar
Laurent Montel committed
172
173
174

void AddressLocationWidget::slotModifyAddress(const KContacts::Address &address, int currentIndex)
{
175
    qDebug()<<" void AddressLocationWidget::slotModifyAddress(const KContacts::Address &address, int currentIndex)"<<currentIndex;
176
177
    setAddress(address);
    //TODO store currentIndex.
Laurent Montel's avatar
Laurent Montel committed
178
179
    //TODO
}
Laurent Montel's avatar
Laurent Montel committed
180
181
182
183


void AddressLocationWidget::clear()
{
184
    setAddress(KContacts::Address());
Laurent Montel's avatar
Laurent Montel committed
185
186
    //TODO
}