summaryrefslogtreecommitdiffstats
blob: 6064a6688be51e00e0d44eee191be0f08eed35c4 (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
/*
 * 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 {
    static const int emojiCellSpacing = 2;

    QtEmojisGrid::QtEmojisGrid() : FlowLayout(0, emojiCellSpacing, emojiCellSpacing) {

    }

    QtEmojisGrid::QtEmojisGrid(QString categoryName) : FlowLayout(0, emojiCellSpacing, emojiCellSpacing) {
        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();

        for (const auto& unicodeEmoji : emojis) {
            QString shortname = QString::fromStdString(EmojiMapper::unicodeToShortname(Q2PSTRING(unicodeEmoji)));
            auto emoji = new QtEmojiCell(shortname, unicodeEmoji);
            connect(emoji, SIGNAL(emojiClicked(QString)), this, SIGNAL(onEmojiSelected(QString)));
            addItem(new QWidgetItem(emoji));
        }
    }

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