summaryrefslogtreecommitdiffstats
blob: 4a599ea119e96866f26dfff679741cff9c46c8a5 (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
/*
 * Copyright (c) 2015 Daniel Baczynski
 * Licensed under the Simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include "QtEmoticonsGrid.h"

#include <set>

#include <QPushButton>

#include <Swiften/Base/foreach.h>

#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {

typedef std::map<std::string, std::string> EmoticonsMap; // Without this typedef compiler complains when using foreach

QtEmoticonsGrid::QtEmoticonsGrid(const std::map<std::string, std::string>& emoticons, QWidget* parent) : QGridLayout(parent) {
	makeUniqueEmoticonsMap(emoticons);

	// Create grid: 3 columns, [uniqueEmoticons_.size()/3] rows
	int row = 0;
	int column = 0;

	foreach(EmoticonsMap::value_type emoticon, uniqueEmoticons_) {
		QtEmoticonCell* newCell = new QtEmoticonCell(P2QSTRING(emoticon.first), P2QSTRING(emoticon.second));
		addWidget(newCell, row, column);
		connect(newCell, SIGNAL(emoticonClicked(QString)), this, SLOT(emoticonClickedSlot(QString)));

		column++;
		if (column >= 3) {
			column = 0;
			row++;
		}
	}
}

QtEmoticonsGrid::~QtEmoticonsGrid() {

}

void QtEmoticonsGrid::makeUniqueEmoticonsMap(const std::map<std::string, std::string>& emoticons) {
	std::set<std::string> paths;
	reverse_foreach(EmoticonsMap::value_type emoticon, emoticons) {
		if (paths.find(emoticon.second) == paths.end()) {
			uniqueEmoticons_.insert(emoticon);
			paths.insert(emoticon.second);
		}
	}
}

void QtEmoticonsGrid::emoticonClickedSlot(QString emoticonAsText) {
	emit emoticonClicked(emoticonAsText);
}

}