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

#include <Swift/QtUI/QtEmojisSelector.h>

#include <QScrollArea>
#include <QSettings>
#include <QString>
#include <QTabBar>
#include <QWidget>

#include <Swiften/Base/Platform.h>

#include <SwifTools/EmojiMapper.h>

#include <Swift/QtUI/QtEmojisGrid.h>
#include <Swift/QtUI/QtEmojisScroll.h>
#include <Swift/QtUI/QtEmoticonsGrid.h>
#include <Swift/QtUI/QtRecentEmojisGrid.h>
#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {
    QtEmojisSelector::QtEmojisSelector(QSettings* settings, const std::map<std::string, std::string>& emoticonsMap, QWidget* parent) : QTabWidget(parent), settings_(settings), emoticonsMap_(emoticonsMap) {
#ifdef SWIFTEN_PLATFORM_MACOSX
        recentEmojisGrid_ = addRecentTab();
        connect(recentEmojisGrid_, SIGNAL(onEmojiSelected(QString)), this, SLOT(emojiClickedSlot(QString)));

        for (const auto& category : EmojiMapper::getCategories()) {
            if (category != "modifier") {
                QtEmojisGrid* grid = addTab(P2QSTRING(category));
                connect(grid, SIGNAL(onEmojiSelected(QString)), this, SLOT(emojiClickedSlot(QString)));
            }
        }

        loadSettings();
#else
        setupEmoticonsTab();
#endif
    }

    QtEmojisSelector::~QtEmojisSelector() {
#ifdef SWIFTEN_PLATFORM_MACOSX
        writeSettings();
#endif
    }

    QtRecentEmojisGrid* QtEmojisSelector::addRecentTab() {
        QtRecentEmojisGrid* recent = new QtRecentEmojisGrid(settings_);
        QtEmojisScroll* scroll = new QtEmojisScroll(recent);
        QTabWidget::addTab(scroll, QString::fromStdString(EmojiMapper::categoryToFlagshipUnicodeEmoji("recent")));

        setTabToolTip(count()-1, tr("Recent"));

        return recent;
    }

    QtEmojisGrid* QtEmojisSelector::addTab(QString categoryName) {
        QtEmojisGrid* grid = new QtEmojisGrid(categoryName);
        QtEmojisScroll* scroll = new QtEmojisScroll(grid);
        QTabWidget::addTab(scroll, QString::fromStdString(EmojiMapper::categoryToFlagshipUnicodeEmoji(Q2PSTRING(categoryName))));
        setTabToolTip(count()-1, categoryName.replace(0, 1, categoryName[0].toUpper()));

        return grid;
    }

    void QtEmojisSelector::loadSettings() {
        if (settings_->contains("currentEmojiTab")) {
            setCurrentIndex(settings_->value("currentEmojiTab").toInt());
        } else {
            setCurrentIndex(1); //index of people category
        }
    }

    void QtEmojisSelector::writeSettings() {
        settings_->setValue("currentEmojiTab", currentIndex());
    }

    void QtEmojisSelector::setupEmoticonsTab() {
        QtEmojisGrid* grid = new QtEmoticonsGrid(emoticonsMap_);
        QtEmojisScroll* scroll = new QtEmojisScroll(grid);
        QTabWidget::addTab(scroll, QIcon(":/emoticons/smile.png"),"");
        connect(grid, SIGNAL(onEmojiSelected(QString)), this, SLOT(emojiClickedSlot(QString)));
        tabBar()->hide();
    }

    void QtEmojisSelector::emojiClickedSlot(QString emoji) {
        if (recentEmojisGrid_) {
            recentEmojisGrid_->handleEmojiClicked(emoji);
        }
        emit emojiClicked(emoji);
    }
}