summaryrefslogtreecommitdiffstats
blob: 596006aa036c7f375e98ebf930c465aee58ef47c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (c) 2012-2014 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2015-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/QtVCardWidget/QtVCardAddressField.h>

#include <QGridLayout>

#include <Swift/QtUI/QtElidingLabel.h>
#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {

QtVCardAddressField::QtVCardAddressField(QWidget* parent, QGridLayout *layout, bool editable) :
    QtVCardGeneralField(parent, layout, editable, layout->rowCount(), tr("Address")), streetLineEdit(nullptr), poboxLineEdit(nullptr), addressextLineEdit(nullptr), cityLineEdit(nullptr), pocodeLineEdit(nullptr), regionLineEdit(nullptr), countryLineEdit(nullptr), textFieldGridLayout(nullptr), textFieldGridLayoutItem(nullptr), deliveryTypeLabel(nullptr), domesticRadioButton(nullptr), internationalRadioButton(nullptr), buttonGroup(nullptr) {
    connect(this, SIGNAL(editableChanged(bool)), SLOT(handleEditibleChanged(bool)));
}

QtVCardAddressField::~QtVCardAddressField() {
    disconnect(this, SLOT(handleEditibleChanged(bool)));
}

void QtVCardAddressField::setupContentWidgets() {
    textFieldGridLayout = new QGridLayout();

    streetLineEdit = new QtResizableLineEdit(this);
    textFieldGridLayout->addWidget(streetLineEdit, 0, 0, Qt::AlignVCenter);

    poboxLineEdit = new QtResizableLineEdit(this);
    textFieldGridLayout->addWidget(poboxLineEdit, 0, 1, Qt::AlignVCenter);

    addressextLineEdit = new QtResizableLineEdit(this);
    textFieldGridLayout->addWidget(addressextLineEdit, 1, 0, Qt::AlignVCenter);

    cityLineEdit = new QtResizableLineEdit(this);
    textFieldGridLayout->addWidget(cityLineEdit, 2, 0, Qt::AlignVCenter);

    pocodeLineEdit = new QtResizableLineEdit(this);
    textFieldGridLayout->addWidget(pocodeLineEdit, 2, 1, Qt::AlignVCenter);

    regionLineEdit = new QtResizableLineEdit(this);
    textFieldGridLayout->addWidget(regionLineEdit, 3, 0, Qt::AlignVCenter);

    countryLineEdit = new QtResizableLineEdit(this);
    textFieldGridLayout->addWidget(countryLineEdit, 4, 0, Qt::AlignVCenter);
    textFieldGridLayout->setVerticalSpacing(2);
    getGridLayout()->addLayout(textFieldGridLayout, getGridLayout()->rowCount()-1, 2, 5, 2, Qt::AlignVCenter);
    textFieldGridLayoutItem = getGridLayout()->itemAtPosition(getGridLayout()->rowCount()-1, 2);

#if QT_VERSION >= 0x040700
    streetLineEdit->setPlaceholderText(tr("Street"));
    poboxLineEdit->setPlaceholderText(tr("PO Box"));
    addressextLineEdit->setPlaceholderText(tr("Address Extension"));
    cityLineEdit->setPlaceholderText(tr("City"));
    pocodeLineEdit->setPlaceholderText(tr("Postal Code"));
    regionLineEdit->setPlaceholderText(tr("Region"));
    countryLineEdit->setPlaceholderText(tr("Country"));
#endif

    deliveryTypeLabel = new QtElidingLabel(this);
    deliveryTypeLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
    getGridLayout()->addWidget(deliveryTypeLabel, getGridLayout()->rowCount()-3, 4, Qt::AlignVCenter);

    domesticRadioButton = new QRadioButton(tr("Domestic Delivery"), this);
    getGridLayout()->addWidget(domesticRadioButton, getGridLayout()->rowCount()-2, 4, Qt::AlignVCenter);

    internationalRadioButton = new QRadioButton(tr("International Delivery"), this);
    getGridLayout()->addWidget(internationalRadioButton, getGridLayout()->rowCount()-1, 4, Qt::AlignVCenter);

    buttonGroup = new QButtonGroup(this);
    buttonGroup->addButton(domesticRadioButton);
    buttonGroup->addButton(internationalRadioButton);

    setTabOrder(internationalRadioButton, getTagComboBox());
    getTagComboBox()->addTag("postal", tr("Postal"));
    getTagComboBox()->addTag("parcel", tr("Parcel"));

    QtVCardHomeWork::setTagComboBox(getTagComboBox());

    textFields << streetLineEdit << poboxLineEdit << addressextLineEdit << cityLineEdit << pocodeLineEdit << regionLineEdit << countryLineEdit;
    childWidgets << deliveryTypeLabel << domesticRadioButton << internationalRadioButton;
}

void QtVCardAddressField::customCleanup() {
    for (auto widget : textFields) {
        widget->hide();
        textFieldGridLayout->removeWidget(widget);
    }
    getGridLayout()->removeItem(textFieldGridLayoutItem);
}



bool QtVCardAddressField::isEmpty() const {
    return streetLineEdit->text().isEmpty() &&
            poboxLineEdit->text().isEmpty() &&
            addressextLineEdit->text().isEmpty() &&
            cityLineEdit->text().isEmpty() &&
            pocodeLineEdit->text().isEmpty() &&
            regionLineEdit->text().isEmpty() &&
            countryLineEdit->text().isEmpty();
}

void QtVCardAddressField::setAddress(const VCard::Address& address) {
    setPreferred(address.isPreferred);
    setHome(address.isHome);
    setWork(address.isWork);
    getTagComboBox()->setTag("postal", address.isPostal);
    getTagComboBox()->setTag("parcel", address.isParcel);
    domesticRadioButton->setChecked(address.deliveryType == VCard::DomesticDelivery);
    internationalRadioButton->setChecked(address.deliveryType == VCard::InternationalDelivery);
    streetLineEdit->setText(P2QSTRING(address.street));
    poboxLineEdit->setText(P2QSTRING(address.poBox));
    addressextLineEdit->setText(P2QSTRING(address.addressExtension));
    cityLineEdit->setText(P2QSTRING(address.locality));
    pocodeLineEdit->setText(P2QSTRING(address.postalCode));
    regionLineEdit->setText(P2QSTRING(address.region));
    countryLineEdit->setText(P2QSTRING(address.country));
}

VCard::Address QtVCardAddressField::getAddress() const {
    VCard::Address address;
    address.isPreferred = getPreferred();
    address.isHome = getHome();
    address.isWork = getWork();
    address.deliveryType = domesticRadioButton->isChecked() ? VCard::DomesticDelivery : (internationalRadioButton->isChecked() ? VCard::InternationalDelivery : VCard::None);
    address.isPostal = getTagComboBox()->isTagSet("postal");
    address.isParcel = getTagComboBox()->isTagSet("parcel");
    address.street = Q2PSTRING(streetLineEdit->text());
    address.poBox = Q2PSTRING(poboxLineEdit->text());
    address.addressExtension = Q2PSTRING(addressextLineEdit->text());
    address.locality = Q2PSTRING(cityLineEdit->text());
    address.postalCode = Q2PSTRING(pocodeLineEdit->text());
    address.region = Q2PSTRING(regionLineEdit->text());
    address.country = Q2PSTRING(countryLineEdit->text());
    return address;
}

void QtVCardAddressField::handleEditibleChanged(bool isEditable) {
    assert(streetLineEdit);
    assert(poboxLineEdit);
    assert(addressextLineEdit);
    assert(cityLineEdit);
    assert(pocodeLineEdit);
    assert(regionLineEdit);
    assert(countryLineEdit);
    assert(deliveryTypeLabel);
    assert(domesticRadioButton);
    assert(internationalRadioButton);

    streetLineEdit->setEditable(isEditable);
    poboxLineEdit->setEditable(isEditable);
    addressextLineEdit->setEditable(isEditable);
    cityLineEdit->setEditable(isEditable);
    pocodeLineEdit->setEditable(isEditable);
    regionLineEdit->setEditable(isEditable);
    countryLineEdit->setEditable(isEditable);

    deliveryTypeLabel->setText(buttonGroup->checkedButton() == nullptr ? "" : buttonGroup->checkedButton()->text());
    deliveryTypeLabel->setVisible(!isEditable);

    domesticRadioButton->setVisible(isEditable);
    internationalRadioButton->setVisible(isEditable);

    for (auto widget : textFields) {
        QtResizableLineEdit* lineEdit;
        if ((lineEdit = dynamic_cast<QtResizableLineEdit*>(widget))) {
            lineEdit->setVisible(isEditable ? true : !lineEdit->text().isEmpty());
            lineEdit->setStyleSheet(isEditable ? "" : "QLineEdit { border: none; background: transparent; }");
        }
    }
}

}