diff --git a/autotests/unicodeemoticonparsertest.cpp b/autotests/unicodeemoticonparsertest.cpp index c470ca67..aefe5f91 100644 --- a/autotests/unicodeemoticonparsertest.cpp +++ b/autotests/unicodeemoticonparsertest.cpp @@ -1,29 +1,32 @@ /* Copyright (c) 2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "unicodeemoticonparsertest.h" +#include "emoticons/unicodeemoticonparser.h" #include QTEST_GUILESS_MAIN(UnicodeEmoticonParserTest) UnicodeEmoticonParserTest::UnicodeEmoticonParserTest(QObject *parent) : QObject(parent) { } + +//TODO add test parser diff --git a/autotests/unicodeemoticontest.cpp b/autotests/unicodeemoticontest.cpp index a27e2ada..83646876 100644 --- a/autotests/unicodeemoticontest.cpp +++ b/autotests/unicodeemoticontest.cpp @@ -1,39 +1,40 @@ /* Copyright (c) 2018-2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "unicodeemoticontest.h" #include "emoticons/unicodeemoticon.h" #include QTEST_GUILESS_MAIN(UnicodeEmoticonTest) UnicodeEmoticonTest::UnicodeEmoticonTest(QObject *parent) : QObject(parent) { } void UnicodeEmoticonTest::shouldHaveDefaultValue() { UnicodeEmoticon w; QVERIFY(w.identifier().isEmpty()); QVERIFY(w.category().isEmpty()); QVERIFY(w.unicode().isEmpty()); QVERIFY(w.aliases().isEmpty()); + QVERIFY(w.key().isEmpty()); QVERIFY(!w.isValid()); } diff --git a/src/ruqolacore/emoticons/unicodeemoticon.cpp b/src/ruqolacore/emoticons/unicodeemoticon.cpp index 55a3ab0d..fdc0200a 100644 --- a/src/ruqolacore/emoticons/unicodeemoticon.cpp +++ b/src/ruqolacore/emoticons/unicodeemoticon.cpp @@ -1,144 +1,155 @@ /* Copyright (c) 2018-2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "emoticons/unicodeemoticon.h" #include #include #include UnicodeEmoticon::UnicodeEmoticon() { } bool UnicodeEmoticon::isValid() const { return !mIdentifier.isEmpty() && !mUnicode.isEmpty(); } QString UnicodeEmoticon::identifier() const { return mIdentifier; } void UnicodeEmoticon::setIdentifier(const QString &name) { mIdentifier = name; } QString UnicodeEmoticon::unicode() const { return mUnicode; } //Code from fairchat //Reimplement it ? QString UnicodeEmoticon::escapeUnicodeEmoji(const QString &pString) { static const QRegularExpression reg{ QStringLiteral("(\\b[A-Fa-f0-9]{2,6}\\b)") }; QRegularExpressionMatchIterator iter = reg.globalMatch(pString); QString retString; if (pString.contains(QLatin1Char('-'))) { const QStringList parts = pString.split(QLatin1Char('-')); for (const auto &item : parts) { int part; std::stringstream ss; ss << std::hex << item.toStdString(); ss >> part; if (part >= 0x10000 && part <= 0x10FFFF) { const int hi = ((part - 0x10000) / 0x400) + 0xD800; const int lo = ((part - 0x10000) % 0x400) + 0xDC00; retString += QChar(hi); retString += QChar(lo); } else { retString = QChar(part); } } } else { int part; std::stringstream ss; ss << std::hex << pString.toStdString(); ss >> part; if (part >= 0x10000 && part <= 0x10FFFF) { const int hi = ((part - 0x10000) / 0x400) + 0xD800; const int lo = ((part - 0x10000) % 0x400) + 0xDC00; retString += QChar(hi); retString += QChar(lo); } else { retString = QChar(part); } } return retString; } +QString UnicodeEmoticon::key() const +{ + return mKey; +} + +void UnicodeEmoticon::setKey(const QString &key) +{ + mKey = key; +} + int UnicodeEmoticon::order() const { return mOrder; } void UnicodeEmoticon::setOrder(int order) { mOrder = order; } void UnicodeEmoticon::setUnicode(const QString &unicode) { mUnicode = escapeUnicodeEmoji(unicode); } QString UnicodeEmoticon::category() const { return mCategory; } void UnicodeEmoticon::setCategory(const QString &category) { mCategory = category; } QStringList UnicodeEmoticon::aliases() const { return mAliases; } void UnicodeEmoticon::setAliases(const QStringList &aliases) { mAliases = aliases; } bool UnicodeEmoticon::hasEmoji(const QString &identifier) const { return (mIdentifier == identifier) || mAliases.contains(identifier); } QDebug operator <<(QDebug d, const UnicodeEmoticon &t) { d << "Identifier : " << t.identifier(); d << "Unicode: " << t.unicode(); d << "Category: " << t.category(); d << "Aliases: " << t.aliases(); d << "Order: " << t.order(); + d << "Key:" << t.key(); return d; } diff --git a/src/ruqolacore/emoticons/unicodeemoticon.h b/src/ruqolacore/emoticons/unicodeemoticon.h index 5643c838..1e8c8d32 100644 --- a/src/ruqolacore/emoticons/unicodeemoticon.h +++ b/src/ruqolacore/emoticons/unicodeemoticon.h @@ -1,65 +1,69 @@ /* Copyright (c) 2018-2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef UNICODEEMOTICON_H #define UNICODEEMOTICON_H #include #include #include #include "libruqola_private_export.h" class LIBRUQOLACORE_TESTS_EXPORT UnicodeEmoticon { Q_GADGET public: UnicodeEmoticon(); Q_REQUIRED_RESULT QString identifier() const; void setIdentifier(const QString &identifier); Q_REQUIRED_RESULT QString unicode() const; void setUnicode(const QString &unicode); Q_REQUIRED_RESULT QString category() const; void setCategory(const QString &category); Q_REQUIRED_RESULT QStringList aliases() const; void setAliases(const QStringList &aliases); Q_REQUIRED_RESULT bool hasEmoji(const QString &identifier) const; Q_REQUIRED_RESULT bool isValid() const; Q_REQUIRED_RESULT int order() const; void setOrder(int order); + Q_REQUIRED_RESULT QString key() const; + void setKey(const QString &key); + private: QString escapeUnicodeEmoji(const QString &pString); QString mIdentifier; QString mUnicode; QString mCategory; + QString mKey; QStringList mAliases; int mOrder = -1; }; Q_DECLARE_METATYPE(UnicodeEmoticon) LIBRUQOLACORE_EXPORT QDebug operator <<(QDebug d, const UnicodeEmoticon &t); #endif // UNICODEEMOTICON_H diff --git a/src/ruqolacore/emoticons/unicodeemoticonparser.cpp b/src/ruqolacore/emoticons/unicodeemoticonparser.cpp index 838f9bf6..518c6ec0 100644 --- a/src/ruqolacore/emoticons/unicodeemoticonparser.cpp +++ b/src/ruqolacore/emoticons/unicodeemoticonparser.cpp @@ -1,58 +1,59 @@ /* Copyright (c) 2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "unicodeemoticonparser.h" #include #include UnicodeEmoticonParser::UnicodeEmoticonParser() { } UnicodeEmoticonParser::~UnicodeEmoticonParser() { } QVector UnicodeEmoticonParser::parse(const QJsonObject &o) const { QVector lstEmoticons; for (const QString &key: o.keys()) { UnicodeEmoticon emoticon; QJsonObject emojiObj = o[key].toObject(); + emoticon.setKey(key); emoticon.setUnicode(emojiObj[QStringLiteral("unicode")].toString()); emoticon.setCategory(emojiObj[QStringLiteral("category")].toString()); emoticon.setIdentifier(emojiObj[QStringLiteral("shortname")].toString()); - emoticon.setOrder(emojiObj[QStringLiteral("emoji_order")].toInt()); + emoticon.setOrder(emojiObj[QStringLiteral("emoji_order")].toString().toInt()); const QJsonArray aliasArray = emojiObj[QStringLiteral("aliases_ascii")].toArray(); if (!aliasArray.isEmpty()) { QStringList lst; const int aliasArrayCount = aliasArray.count(); lst.reserve(aliasArrayCount); for (int i = 0; i < aliasArrayCount; ++i) { lst.append(aliasArray.at(i).toString()); } emoticon.setAliases(lst); } if (emoticon.isValid()) { lstEmoticons.append(emoticon); } } return lstEmoticons; } diff --git a/tests/unicodeemoticongui.cpp b/tests/unicodeemoticongui.cpp index d59a0d7c..d040229b 100644 --- a/tests/unicodeemoticongui.cpp +++ b/tests/unicodeemoticongui.cpp @@ -1,81 +1,160 @@ /* Copyright (c) 2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "unicodeemoticongui.h" #include #include #include #include #include #include #include #include #include +#include +#include #include UnicodeEmoticonGui::UnicodeEmoticonGui(QWidget *parent) : QWidget(parent) { - QHBoxLayout *mainLayout = new QHBoxLayout(this); + QVBoxLayout *mainLayout = new QVBoxLayout(this); + QHBoxLayout *hboxLayout = new QHBoxLayout; + hboxLayout->setContentsMargins(0, 0, 0, 0); + mainLayout->addLayout(hboxLayout); mListWidget = new QListWidget(this); - mainLayout->addWidget(mListWidget); + hboxLayout->addWidget(mListWidget); + mWidgetInfo = new UnicodeEmoticonInfo(this); + hboxLayout->addWidget(mWidgetInfo); + + QPushButton *save = new QPushButton(QStringLiteral("Save"), this); + mainLayout->addWidget(save); + connect(save, &QPushButton::clicked, this, &UnicodeEmoticonGui::save); + + connect(mListWidget, &QListWidget::itemClicked, this, &UnicodeEmoticonGui::slotItemChanged); load(); } UnicodeEmoticonGui::~UnicodeEmoticonGui() { } +void UnicodeEmoticonGui::slotItemChanged(QListWidgetItem *item) +{ + if (item) { + UnicodeEmoticonListWidgetItem *itemResult = static_cast(item); + UnicodeEmoticon info = itemResult->info(); + mWidgetInfo->setInfo(info); + } +} void UnicodeEmoticonGui::load() { UnicodeEmoticonParser unicodeParser; QFile file(QStringLiteral(":/emoji.json")); if (!file.open(QFile::ReadOnly)) { qWarning() << "Impossible to open file: " << file.errorString(); return; } const QJsonDocument doc = QJsonDocument::fromJson(file.readAll()); const QJsonObject obj = doc.object(); const QVector unicodeEmojiList = unicodeParser.parse(obj); for (int i = 0; i < unicodeEmojiList.count(); ++i) { - new QListWidgetItem(unicodeEmojiList.at(i).identifier(), mListWidget); + UnicodeEmoticonListWidgetItem *item = new UnicodeEmoticonListWidgetItem(unicodeEmojiList.at(i).identifier(), mListWidget); + item->setInfo(unicodeEmojiList.at(i)); + //Allow to update it. } } void UnicodeEmoticonGui::save() { + QJsonDocument doc; + QJsonObject o; + doc.setObject(o); //TODO } int main(int argc, char *argv[]) { QApplication app(argc, argv); QStandardPaths::setTestModeEnabled(true); UnicodeEmoticonGui w; w.show(); return app.exec(); } + +UnicodeEmoticonInfo::UnicodeEmoticonInfo(QWidget *parent) + : QWidget(parent) +{ + QFormLayout *mainLayout = new QFormLayout(this); + mainLayout->setContentsMargins(0, 0, 0, 0); + mIdentifier = new QLineEdit(this); + mainLayout->addRow(QStringLiteral("identifier:"), mIdentifier); + mUnicode = new QLineEdit(this); + mainLayout->addRow(QStringLiteral("unicode:"), mUnicode); + mAliases = new QLineEdit(this); + mainLayout->addRow(QStringLiteral("aliases:"), mAliases); + mCategory = new QLineEdit(this); + mainLayout->addRow(QStringLiteral("category:"), mCategory); + mOrder = new QLineEdit(this); + mainLayout->addRow(QStringLiteral("order:"), mOrder); +} + +UnicodeEmoticonInfo::~UnicodeEmoticonInfo() +{ + +} + +UnicodeEmoticon UnicodeEmoticonInfo::info() const +{ + return mInfo; +} + +void UnicodeEmoticonInfo::setInfo(const UnicodeEmoticon &info) +{ + mIdentifier->setText(info.identifier()); + mUnicode->setText(info.unicode()); + mAliases->setText(info.aliases().join(QLatin1Char(','))); + mCategory->setText(info.category()); + mOrder->setText(QString::number(info.order())); + mInfo = info; +} + +UnicodeEmoticonListWidgetItem::UnicodeEmoticonListWidgetItem(const QString &str, QListWidget *parent) + : QListWidgetItem (str, parent) +{ + +} + +UnicodeEmoticon UnicodeEmoticonListWidgetItem::info() const +{ + return mInfo; +} + +void UnicodeEmoticonListWidgetItem::setInfo(const UnicodeEmoticon &info) +{ + mInfo = info; +} diff --git a/tests/unicodeemoticongui.h b/tests/unicodeemoticongui.h index 76c4d318..8c664cac 100644 --- a/tests/unicodeemoticongui.h +++ b/tests/unicodeemoticongui.h @@ -1,39 +1,77 @@ /* Copyright (c) 2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef UNICODEEMOTICONGUI_H #define UNICODEEMOTICONGUI_H +#include #include + +#include + +class QLineEdit; +class UnicodeEmoticonInfo : public QWidget +{ + Q_OBJECT +public: + explicit UnicodeEmoticonInfo(QWidget *parent = nullptr); + ~UnicodeEmoticonInfo(); + + Q_REQUIRED_RESULT UnicodeEmoticon info() const; + void setInfo(const UnicodeEmoticon &info); + +private: + QLineEdit *mIdentifier = nullptr; + QLineEdit *mUnicode = nullptr; + QLineEdit *mAliases = nullptr; + QLineEdit *mCategory = nullptr; + QLineEdit *mOrder = nullptr; + UnicodeEmoticon mInfo; +}; + +class UnicodeEmoticonListWidgetItem : public QListWidgetItem +{ +public: + explicit UnicodeEmoticonListWidgetItem(const QString &str, QListWidget *parent); + + Q_REQUIRED_RESULT UnicodeEmoticon info() const; + void setInfo(const UnicodeEmoticon &info); + +private: + UnicodeEmoticon mInfo; +}; + class QListWidget; class UnicodeEmoticonGui : public QWidget { Q_OBJECT public: explicit UnicodeEmoticonGui(QWidget *parent = nullptr); ~UnicodeEmoticonGui(); private: void save(); void load(); + void slotItemChanged(QListWidgetItem *item); QListWidget *mListWidget = nullptr; + UnicodeEmoticonInfo *mWidgetInfo = nullptr; }; #endif // UnicodeEmoticonGui_H