summaryrefslogtreecommitdiffstats
blob: ba3d25ff91e894f6da1d66b6ec44635b4825bc54 (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
/*
 * Copyright (c) 2012-2014 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include "QtVCardAddressLabelField.h"

#include <QGridLayout>
#include <boost/algorithm/string.hpp>

#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {

QtVCardAddressLabelField::QtVCardAddressLabelField(QWidget* parent, QGridLayout *layout, bool editable) :
	QtVCardGeneralField(parent, layout, editable, layout->rowCount(), tr("Address Label")), addressLabelPlainTextEdit(NULL), deliveryTypeLabel(NULL), domesticRadioButton(NULL), internationalRadioButton(NULL), buttonGroup(NULL) {
	connect(this, SIGNAL(editableChanged(bool)), SLOT(handleEditibleChanged(bool)));
}

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

void QtVCardAddressLabelField::setupContentWidgets() {
	addressLabelPlainTextEdit = new QPlainTextEdit(this);
	addressLabelPlainTextEdit->setTabChangesFocus(true);
	getGridLayout()->addWidget(addressLabelPlainTextEdit, getGridLayout()->rowCount()-1, 2, 3, 2, Qt::AlignVCenter);

	deliveryTypeLabel = new QLabel(this);
	deliveryTypeLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
	getGridLayout()->addWidget(deliveryTypeLabel, getGridLayout()->rowCount()-2, 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());
	deliveryTypeLabel->hide();
	childWidgets << addressLabelPlainTextEdit << deliveryTypeLabel << domesticRadioButton << internationalRadioButton;
}

bool QtVCardAddressLabelField::isEmpty() const {
	return addressLabelPlainTextEdit->toPlainText().isEmpty();
}

void QtVCardAddressLabelField::setAddressLabel(const VCard::AddressLabel& addressLabel) {
	setPreferred(addressLabel.isPreferred);
	setHome(addressLabel.isHome);
	setWork(addressLabel.isWork);
	getTagComboBox()->setTag("postal", addressLabel.isPostal);
	getTagComboBox()->setTag("parcel", addressLabel.isParcel);
	domesticRadioButton->setChecked(addressLabel.deliveryType == VCard::DomesticDelivery);
	internationalRadioButton->setChecked(addressLabel.deliveryType == VCard::InternationalDelivery);
	std::string joinedLines = boost::algorithm::join(addressLabel.lines, "\n");
	addressLabelPlainTextEdit->setPlainText(P2QSTRING(joinedLines));
}

VCard::AddressLabel QtVCardAddressLabelField::getAddressLabel() const {
	VCard::AddressLabel addressLabel;
	addressLabel.isPreferred = getPreferred();
	addressLabel.isHome = getHome();
	addressLabel.isWork = getWork();
	addressLabel.deliveryType = domesticRadioButton->isChecked() ? VCard::DomesticDelivery : (internationalRadioButton->isChecked() ? VCard::InternationalDelivery : VCard::None);
	addressLabel.isPostal = getTagComboBox()->isTagSet("postal");
	addressLabel.isParcel = getTagComboBox()->isTagSet("parcel");

	std::string lines = Q2PSTRING(addressLabelPlainTextEdit->toPlainText());
	boost::split(addressLabel.lines, lines, boost::is_any_of("\n"));
	return addressLabel;
}

void QtVCardAddressLabelField::handleEditibleChanged(bool isEditable) {
	assert(addressLabelPlainTextEdit);
	assert(deliveryTypeLabel);
	assert(domesticRadioButton);
	assert(internationalRadioButton);

	addressLabelPlainTextEdit->setReadOnly(!isEditable);
	addressLabelPlainTextEdit->setStyleSheet(isEditable ? "" : "QPlainTextEdit { background: transparent; }");

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

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

}