summaryrefslogtreecommitdiffstats
blob: fbc9685da4f40a4e1da8f8d646bdb8bc098b3bd6 (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
/*
 * Copyright (c) 2010 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "QtContactEditWidget.h"

#include <algorithm>

#include <QScrollArea>
#include <QBoxLayout>
#include <QLabel>
#include <QCheckBox>
#include <QLineEdit>

#include "Swift/QtUI/QtSwiftUtil.h"

namespace Swift {

QtContactEditWidget::QtContactEditWidget(const std::set<std::string>& allGroups, QWidget* parent) : QWidget(parent), groups_(NULL) {
	QBoxLayout* layout = new QVBoxLayout(this);
	setContentsMargins(0,0,0,0);
	layout->setContentsMargins(0,0,0,0);

	QHBoxLayout* nameLayout = new QHBoxLayout();
	
	QLabel* label = new QLabel(tr("Name:"), this);
	nameLayout->addWidget(label);
	name_ = new QLineEdit(this);
	nameLayout->addWidget(name_);
	layout->addLayout(nameLayout);

	layout->addWidget(new QLabel(tr("Groups:"), this));

	QScrollArea* groupsArea = new QScrollArea(this);
	layout->addWidget(groupsArea);
	groupsArea->setWidgetResizable(true);
	groupsArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	groupsArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);

	QWidget* groups = new QWidget(groupsArea);
	groupsArea->setWidget(groups);
	QVBoxLayout* scrollLayout = new QVBoxLayout(groups);

	foreach (std::string group, allGroups) {
		QCheckBox* check = new QCheckBox(groups);
		check->setText(P2QSTRING(group));
		check->setCheckState(Qt::Unchecked);
		checkBoxes_[group] = check;
		scrollLayout->addWidget(check);
	}

	QHBoxLayout* newGroupLayout = new QHBoxLayout();
	newGroup_ = new QCheckBox(groups);
	newGroup_->setText(tr("New Group:"));
	newGroup_->setCheckState(Qt::Unchecked);
	newGroupLayout->addWidget(newGroup_);
	newGroupName_ = new QLineEdit(groups);
	newGroupLayout->addWidget(newGroupName_);
	scrollLayout->addLayout(newGroupLayout);

	scrollLayout->addItem(new QSpacerItem(20, 73, QSizePolicy::Minimum, QSizePolicy::Expanding));
}

void QtContactEditWidget::setName(const std::string& name) {
	name_->setText(P2QSTRING(name));
}

std::string QtContactEditWidget::getName() const {
	return Q2PSTRING(name_->text());
}

void QtContactEditWidget::setSelectedGroups(const std::vector<std::string>& groups) {
	foreach (std::string group, groups) {
		checkBoxes_[group]->setCheckState(Qt::Checked);
	}
}

std::set<std::string> QtContactEditWidget::getSelectedGroups() const {
	std::set<std::string> groups;
	foreach(const CheckBoxMap::value_type& group, checkBoxes_) {
		if (group.second->checkState() == Qt::Checked) {
			groups.insert(group.first);
		}
	}
	if (newGroup_->checkState() == Qt::Checked && !newGroupName_->text().isEmpty()) {
		groups.insert(Q2PSTRING(newGroupName_->text()));
	}
	return groups;
}

}