summaryrefslogtreecommitdiffstats
blob: eadc64fb8a7ea75fcaeb8bb91b1e0e1796547e1f (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
/*
 * Copyright (c) 2016-2017 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/QtEmojisGrid.h>

#include <string>

#include <QString>
#include <QVector>

#include <SwifTools/EmojiMapper.h>

#include <Swift/QtUI/QtEmojiCell.h>
#include <Swift/QtUI/QtRecentEmojisGrid.h>
#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {
QtEmojisGrid::QtEmojisGrid() {

}

QtEmojisGrid::QtEmojisGrid(QString categoryName) {
        auto category = EmojiMapper::categoryNameToEmojis(Q2PSTRING(categoryName));

        QVector<QString> categoryEmojis;

        for (const auto& emoji : category) {
            categoryEmojis.push_back(P2QSTRING(emoji));
        }

        setEmojis(categoryEmojis);
    }

    void QtEmojisGrid::setEmojis(const QVector<QString>& emojis) {
        clearEmojis();

        int iEmoji = 0;
        for (const auto& unicodeEmoji : emojis) {
            QString shortname = QString::fromStdString(EmojiMapper::unicodeToShortname(Q2PSTRING(unicodeEmoji)));
            QtEmojiCell* emoji = new QtEmojiCell(shortname, unicodeEmoji);
            this->addWidget(emoji, iEmoji/6, iEmoji%6);
            connect(emoji, SIGNAL(emojiClicked(QString)), this, SIGNAL(onEmojiSelected(QString)));
            iEmoji++;
        }
        for (int index = 0; index < columnCount(); index++) {
            auto layoutItem = itemAtPosition(0, index);
            if (layoutItem) {
                auto cellWidget = layoutItem->widget();
                if (cellWidget) {
                    setColumnMinimumWidth(index, cellWidget->width());
                }
            }
        }
        setSpacing(5);
    }

    void QtEmojisGrid::clearEmojis() {
        QLayoutItem* child = nullptr;
        while ((child = this->takeAt(0)) != 0) {
            if (child->widget()) {
                child->widget()->hide();
                removeWidget(child->widget());
            }
            else {
                removeItem(child);
            }
        }
    }
}