blob: 37d35d90b20c78ec3e9c2ea7b0ad7d7814d7a63d (
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
|
/*
* Copyright (c) 2012 Tobias Markmann
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include "QtVCardOrganisationField.h"
#include "ui_QtVCardOrganisationField.h"
namespace Swift {
QtVCardOrganisationField::QtVCardOrganisationField(QWidget* parent, bool editable) :
QWidget(parent),
ui(new Ui::QtVCardOrganisationField) {
ui->setupUi(this);
connect(ui->lineEditORGNAME, SIGNAL(textChanged(QString)), SLOT(onTextChanged(QString)));
connect(ui->pushButtonAddUnit, SIGNAL(clicked()), SLOT(onAddUnitButtonPressed()));
connect(ui->pushButtonRemoveUnit, SIGNAL(clicked()), SLOT(onRemoveUnitButtonPressed()));
setEditable(editable);
}
QtVCardOrganisationField::~QtVCardOrganisationField() {
delete ui;
}
bool QtVCardOrganisationField::isEditable() const {
return editable;
}
void QtVCardOrganisationField::setEditable(bool editable) {
this->editable = editable;
if (this->editable) {
ui->lineEditORGNAME->show();
ui->labelORGNAME->hide();
ui->listWidgetORGUNIT->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed);
ui->listWidgetORGUNIT->setEnabled(true);
ui->pushButtonAddUnit->show();
ui->pushButtonRemoveUnit->show();
} else {
ui->lineEditORGNAME->hide();
ui->labelORGNAME->show();
if (ui->listWidgetORGUNIT->count() > 0) {
ui->listWidgetORGUNIT->setEditTriggers(0);
ui->listWidgetORGUNIT->show();
} else {
ui->listWidgetORGUNIT->hide();
}
ui->pushButtonAddUnit->hide();
ui->pushButtonRemoveUnit->hide();
}
}
bool QtVCardOrganisationField::isEmpty() const {
return ui->lineEditORGNAME->text().isEmpty() && getUnits().empty();
}
void QtVCardOrganisationField::setName(const QString name) {
ui->lineEditORGNAME->setText(name);
}
QString QtVCardOrganisationField::getName() const {
return ui->lineEditORGNAME->text();
}
void QtVCardOrganisationField::setUnits(const QList<QString> units) {
ui->listWidgetORGUNIT->clear();
foreach(const QString& unit, units) {
QListWidgetItem* item = new QListWidgetItem(unit);
item->setFlags(item->flags() | Qt::ItemIsEditable);
ui->listWidgetORGUNIT->addItem(item);
}
}
QList<QString> QtVCardOrganisationField::getUnits() const {
QList<QString> units;
for (int n = 0; n < ui->listWidgetORGUNIT->count(); n++) {
QListWidgetItem* item = ui->listWidgetORGUNIT->item(n);
units.push_back(item->text());
}
return units;
}
void QtVCardOrganisationField::onTextChanged(const QString& text) {
ui->labelORGNAME->setText(text);
}
void QtVCardOrganisationField::onAddUnitButtonPressed() {
QListWidgetItem* item = new QListWidgetItem("Doubleclick to edit.");
item->setFlags(item->flags() | Qt::ItemIsEditable);
ui->listWidgetORGUNIT->addItem(item);
}
void QtVCardOrganisationField::onRemoveUnitButtonPressed() {
foreach (QListWidgetItem* item, ui->listWidgetORGUNIT->selectedItems()) {
ui->listWidgetORGUNIT->removeItemWidget(item);
delete item;
}
}
}
|