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

#include "QtFormResultItemModel.h"

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

#include <Swift/QtUI/QtSwiftUtil.h>
#include <Swiften/Base/foreach.h>

namespace Swift {

QtFormResultItemModel::QtFormResultItemModel(QObject *parent) : QAbstractTableModel(parent) {

}

QtFormResultItemModel::QtFormResultItemModel(QObject* parent, Form::ref formResult) : QAbstractTableModel(parent), formResult_(formResult) {

}

void QtFormResultItemModel::setForm(Form::ref formResult) {
	emit layoutAboutToBeChanged();
	formResult_ = formResult;
	emit layoutChanged();
}

const Form::ref QtFormResultItemModel::getForm() const {
	return formResult_;
}

QVariant QtFormResultItemModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const {
	if (!formResult_) return QVariant();
	if (role != Qt::DisplayRole) return QVariant();
	if (static_cast<size_t>(section) >= formResult_->getReportedFields().size()) return QVariant();
	return QVariant(P2QSTRING(formResult_->getReportedFields().at(section)->getLabel()));
}

int QtFormResultItemModel::rowCount(const QModelIndex &/*parent*/) const {
	if (!formResult_) return 0;
	return formResult_->getItems().size();
}

int QtFormResultItemModel::columnCount(const QModelIndex &/*parent*/) const {
	if (!formResult_) return 0;
	return formResult_->getReportedFields().size();
}

QVariant QtFormResultItemModel::data(const QModelIndex &index, int role) const {
	if (!formResult_) return QVariant();
	if (role != Qt::DisplayRole || !index.isValid()) {
		return QVariant();
	}

	if (static_cast<size_t>(index.row()) >= formResult_->getItems().size()) return QVariant();
	if (static_cast<size_t>(index.column()) >= formResult_->getReportedFields().size()) return QVariant();

	Form::FormItem item = formResult_->getItems().at(index.row());

	return QVariant(P2QSTRING(getFieldValue(item, index.column())));
}

const std::string QtFormResultItemModel::getFieldValue(const Form::FormItem& item, const int column) const {
	// determine field name
	std::string name = formResult_->getReportedFields().at(column)->getName();

	foreach(FormField::ref field, item) {
		if (field->getName() == name) {
			std::string delimiter = "";
			if (boost::dynamic_pointer_cast<TextMultiFormField>(field)) {
				delimiter = "\n";
			} else if (boost::dynamic_pointer_cast<JIDMultiFormField>(field)) {
				delimiter = ", ";
			} else if (boost::dynamic_pointer_cast<ListMultiFormField>(field)) {
				delimiter = ", ";
			}

			return boost::algorithm::join(field->getRawValues(), delimiter);
		}
	}

	return "";
}

}