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

#include "QtTagComboBox.h"

#include <QAbstractItemView>
#include <QtGui>

namespace Swift {

QtTagComboBox::QtTagComboBox(QWidget* parent) : QComboBox(parent) {
	setEditable(false);
	displayModel = new QStandardItemModel();
	displayItem = new QStandardItem();
	displayItem->setText("");
	displayModel->insertRow(0, displayItem);
	editMenu = new QMenu();
	this->setModel(displayModel);
	editable = true;
}

QtTagComboBox::~QtTagComboBox() {

}

bool QtTagComboBox::isEditable() const {
	return editable;
}

void QtTagComboBox::setEditable(const bool editable) {
	this->editable = editable;
}

void QtTagComboBox::addTag(const QString &id, const QString &label) {
	QAction* tagAction = new QAction(editMenu);
	tagAction->setText(label);
	tagAction->setCheckable(true);
	tagAction->setData(QString(id));
	editMenu->addAction(tagAction);
}

void QtTagComboBox::setTag(const QString &id, bool value) {
	QList<QAction*> tagActions = editMenu->actions();
	foreach(QAction* action, tagActions) {
		if (action->data() == id) {
			action->setChecked(value);
			updateDisplayItem();
			return;
		}
	}
}

bool QtTagComboBox::isTagSet(const QString &id) const {
	QList<QAction*> tagActions = editMenu->actions();
	foreach(QAction* action, tagActions) {
		if (action->data() == id) {
			return action->isChecked();
		}
	}
	return false;
}

void QtTagComboBox::showPopup() {

}

void QtTagComboBox::hidePopup() {

}

bool QtTagComboBox::event(QEvent* event) {
	if (event->type() == QEvent::MouseButtonPress ||
		event->type() == QEvent::KeyRelease) {
		if (!editable) return true;

		QPoint p = mapToGlobal(QPoint(0,0));
		p += QPoint(0, height());
		editMenu->exec(p);
		updateDisplayItem();
		return true;
	}
	return QComboBox::event(event);
}

void QtTagComboBox::updateDisplayItem() {
	QList<QAction*> tagActions = editMenu->actions();
	QString text = "";
	foreach(QAction* action, tagActions) {
		if (action->isChecked()) {
			if (text != "") {
				text += ", ";
			}
			text += action->text();
		}
	}
	setItemText(0, text);
}

}