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
|
/*
* Copyright (c) 2012 Tobias Markmann
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include "QtVCardPhotoAndNameFields.h"
#include "ui_QtVCardPhotoAndNameFields.h"
#include <QMenu>
namespace Swift {
QtVCardPhotoAndNameFields::QtVCardPhotoAndNameFields(QWidget* parent) :
QWidget(parent),
ui(new Ui::QtVCardPhotoAndNameFields) {
ui->setupUi(this);
ui->lineEditPREFIX->hide();
ui->lineEditMIDDLE->hide();
ui->lineEditSUFFIX->hide();
ui->lineEditFN->hide();
ui->lineEditNICKNAME->hide();
ui->labelFULLNAME->hide();
#if QT_VERSION >= 0x040700
ui->lineEditFN->setPlaceholderText(tr("Formatted Name"));
ui->lineEditNICKNAME->setPlaceholderText(tr("Nickname"));
ui->lineEditPREFIX->setPlaceholderText(tr("Prefix"));
ui->lineEditGIVEN->setPlaceholderText(tr("Given Name"));
ui->lineEditMIDDLE->setPlaceholderText(tr("Middle Name"));
ui->lineEditFAMILY->setPlaceholderText(tr("Last Name"));
ui->lineEditSUFFIX->setPlaceholderText(tr("Suffix"));
#endif
addFieldMenu = new QMenu("Name", this);
actionSignalMapper = new QSignalMapper(this);
connect(actionSignalMapper, SIGNAL(mapped(const QString &)), this, SLOT(showField(const QString &)));
prepareAddFieldMenu();
}
QtVCardPhotoAndNameFields::~QtVCardPhotoAndNameFields() {
delete ui;
delete actionSignalMapper;
}
bool QtVCardPhotoAndNameFields::isEditable() const {
return editable;
}
void QtVCardPhotoAndNameFields::setEditable(bool editable) {
this->editable = editable;
ui->avatarWidget->setEditable(editable);
ui->lineEditFN->setVisible(editable ? true : !ui->lineEditFN->text().isEmpty());
ui->lineEditFN->setEditable(editable);
ui->lineEditFN->setStyleSheet(editable ? "" : "QLineEdit {border: none; background-color: transparent;}");
ui->lineEditNICKNAME->setVisible(editable ? true : !ui->lineEditNICKNAME->text().isEmpty());
ui->lineEditNICKNAME->setEditable(editable);
ui->lineEditNICKNAME->setStyleSheet(editable ? "" : "QLineEdit {border: none; background-color: transparent;}");
// prefix given middle last suffix
ui->lineEditPREFIX->setVisible(editable);
ui->lineEditGIVEN->setVisible(editable);
ui->lineEditMIDDLE->setVisible(editable);
ui->lineEditFAMILY->setVisible(editable);
ui->lineEditSUFFIX->setVisible(editable);
ui->labelFULLNAME->setVisible(!editable);
ui->labelFULLNAME->setText( ui->lineEditPREFIX->text() + " " + ui->lineEditGIVEN->text() + " " +
ui->lineEditMIDDLE->text() + " " + ui->lineEditFAMILY->text() + " " + ui->lineEditSUFFIX->text());
prepareAddFieldMenu();
}
QMenu* QtVCardPhotoAndNameFields::getAddFieldMenu() const {
return addFieldMenu;
}
void QtVCardPhotoAndNameFields::setAvatar(const ByteArray &data, const std::string &type) {
ui->avatarWidget->setAvatar(data, type);
}
ByteArray QtVCardPhotoAndNameFields::getAvatarData() const {
return ui->avatarWidget->getAvatarData();
}
std::string QtVCardPhotoAndNameFields::getAvatarType() const {
return ui->avatarWidget->getAvatarType();
}
void QtVCardPhotoAndNameFields::setFormattedName(const QString formattedName) {
ui->lineEditFN->setText(formattedName);
}
QString QtVCardPhotoAndNameFields::getFormattedName() const {
return ui->lineEditFN->text();
}
void QtVCardPhotoAndNameFields::setNickname(const QString nickname) {
ui->lineEditNICKNAME->setText(nickname);
}
QString QtVCardPhotoAndNameFields::getNickname() const {
return ui->lineEditNICKNAME->text();
}
void QtVCardPhotoAndNameFields::setPrefix(const QString prefix) {
ui->lineEditPREFIX->setText(prefix);
}
QString QtVCardPhotoAndNameFields::getPrefix() const {
return ui->lineEditPREFIX->text();
}
void QtVCardPhotoAndNameFields::setGivenName(const QString givenName) {
ui->lineEditGIVEN->setText(givenName);
}
QString QtVCardPhotoAndNameFields::getGivenName() const {
return ui->lineEditGIVEN->text();
}
void QtVCardPhotoAndNameFields::setMiddleName(const QString middleName) {
ui->lineEditMIDDLE->setText(middleName);
}
QString QtVCardPhotoAndNameFields::getMiddleName() const {
return ui->lineEditMIDDLE->text();
}
void QtVCardPhotoAndNameFields::setFamilyName(const QString familyName) {
ui->lineEditFAMILY->setText(familyName);
}
QString QtVCardPhotoAndNameFields::getFamilyName() const {
return ui->lineEditFAMILY->text();
}
void QtVCardPhotoAndNameFields::setSuffix(const QString suffix) {
ui->lineEditSUFFIX->setText(suffix);
}
QString QtVCardPhotoAndNameFields::getSuffix() const {
return ui->lineEditSUFFIX->text();
}
void QtVCardPhotoAndNameFields::prepareAddFieldMenu() {
foreach(QAction* action, addFieldMenu->actions()) {
actionSignalMapper->removeMappings(action);
}
addFieldMenu->clear();
foreach(QObject* obj, children()) {
QLineEdit* lineEdit = 0;
if (!(lineEdit = dynamic_cast<QLineEdit*>(obj))) continue;
if (lineEdit->isHidden()) {
#if QT_VERSION >= 0x040700
QAction* action = addFieldMenu->addAction(QString("Add ") + lineEdit->placeholderText(), actionSignalMapper, SLOT(map()));
#else
QAction* action = addFieldMenu->addAction(QString("Add ") + lineEdit->toolTip(), actionSignalMapper, SLOT(map()));
#endif
actionSignalMapper->setMapping(action, lineEdit->objectName());
}
}
}
void QtVCardPhotoAndNameFields::showField(const QString& widgetName) {
QLineEdit* lineEditToShow = findChild<QLineEdit*>(widgetName);
if (lineEditToShow) lineEditToShow->show();
prepareAddFieldMenu();
}
}
|