1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor: Remove SmileyPack singleton

This commit is contained in:
Anthony Bilinski 2022-03-11 06:20:42 -08:00
parent 7d40bcf43d
commit 47328cc6bf
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
25 changed files with 82 additions and 62 deletions

View File

@ -51,7 +51,7 @@ ChatMessage::~ChatMessage() = default;
ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QString& rawMessage,
MessageType type, bool isMe, MessageState state,
const QDateTime& date, DocumentCache& documentCache,
bool colorizeName)
SmileyPack& smileyPack, bool colorizeName)
{
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(documentCache));
@ -61,7 +61,7 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QSt
auto textType = Text::NORMAL;
// smileys
if (Settings::getInstance().getUseEmoticons())
text = SmileyPack::getInstance().smileyfied(text);
text = smileyPack.smileyfied(text);
// quotes (green text)
text = detectQuotes(text, type);

View File

@ -28,6 +28,7 @@
class CoreFile;
class QGraphicsScene;
class DocumentCache;
class SmileyPack;
class ChatMessage : public ChatLine
{
@ -56,7 +57,7 @@ public:
static ChatMessage::Ptr createChatMessage(const QString& sender, const QString& rawMessage,
MessageType type, bool isMe, MessageState state,
const QDateTime& date, DocumentCache&,
bool colorizeName = false);
SmileyPack& smileyPack, bool colorizeName = false);
static ChatMessage::Ptr createChatInfoMessage(const QString& rawMessage, SystemMessageType type,
const QDateTime& date, DocumentCache&);
static ChatMessage::Ptr createFileTransferMessage(const QString& sender, CoreFile& coreFile,

View File

@ -71,7 +71,8 @@ ChatMessage::Ptr createDateMessage(QDateTime timestamp, DocumentCache& documentC
}
ChatMessage::Ptr createMessage(const QString& displayName, bool isSelf, bool colorizeNames,
const ChatLogMessage& chatLogMessage, DocumentCache& documentCache)
const ChatLogMessage& chatLogMessage, DocumentCache& documentCache,
SmileyPack& smileyPack)
{
auto messageType = chatLogMessage.message.isAction ? ChatMessage::MessageType::ACTION
: ChatMessage::MessageType::NORMAL;
@ -89,12 +90,12 @@ ChatMessage::Ptr createMessage(const QString& displayName, bool isSelf, bool col
const auto timestamp = chatLogMessage.message.timestamp;
return ChatMessage::createChatMessage(displayName, chatLogMessage.message.content, messageType,
isSelf, chatLogMessage.state, timestamp, documentCache,
colorizeNames);
smileyPack, colorizeNames);
}
void renderMessageRaw(const QString& displayName, bool isSelf, bool colorizeNames,
const ChatLogMessage& chatLogMessage, ChatLine::Ptr& chatLine,
DocumentCache& documentCache)
DocumentCache& documentCache, SmileyPack& smileyPack)
{
// HACK: This is kind of gross, but there's not an easy way to fit this into
// the existing architecture. This shouldn't ever fail since we should only
@ -110,7 +111,8 @@ void renderMessageRaw(const QString& displayName, bool isSelf, bool colorizeName
chatMessage->markAsBroken();
}
} else {
chatLine = createMessage(displayName, isSelf, colorizeNames, chatLogMessage, documentCache);
chatLine = createMessage(displayName, isSelf, colorizeNames, chatLogMessage,
documentCache, smileyPack);
}
}
@ -207,12 +209,13 @@ ChatLogIdx clampedAdd(ChatLogIdx idx, int val, IChatLog& chatLog)
ChatWidget::ChatWidget(IChatLog& chatLog_, const Core& core_, DocumentCache& documentCache_,
QWidget* parent)
SmileyPack& smileyPack_, QWidget* parent)
: QGraphicsView(parent)
, chatLog(chatLog_)
, core(core_)
, chatLineStorage(new ChatLineStorage())
, documentCache(documentCache_)
, smileyPack{smileyPack_}
{
// Create the scene
busyScene = new QGraphicsScene(this);
@ -1414,7 +1417,7 @@ void ChatWidget::renderItem(const ChatLogItem& item, bool hideName, bool coloriz
const auto& chatLogMessage = item.getContentAsMessage();
renderMessageRaw(item.getDisplayName(), isSelf, colorizeNames_, chatLogMessage,
chatMessage, documentCache);
chatMessage, documentCache, smileyPack);
break;
}

View File

@ -34,6 +34,7 @@ class QMouseEvent;
class QTimer;
class ChatLineContent;
struct ToxFile;
class SmileyPack;
class ChatLineStorage;
@ -42,7 +43,7 @@ class ChatWidget : public QGraphicsView
{
Q_OBJECT
public:
ChatWidget(IChatLog& chatLog_, const Core& core_, DocumentCache&,
ChatWidget(IChatLog& chatLog_, const Core& core_, DocumentCache&, SmileyPack&,
QWidget* parent = nullptr);
virtual ~ChatWidget();
@ -216,4 +217,5 @@ private:
std::vector<std::function<void(void)>> renderCompletionFns;
DocumentCache& documentCache;
SmileyPack& smileyPack;
};

View File

@ -26,8 +26,9 @@
#include <QIcon>
#include <QUrl>
CustomTextDocument::CustomTextDocument(QObject* parent)
CustomTextDocument::CustomTextDocument(SmileyPack& smileyPack_, QObject* parent)
: QTextDocument(parent)
, smileyPack(smileyPack_)
{
setUndoRedoEnabled(false);
setUseDesignMetrics(false);
@ -40,7 +41,7 @@ QVariant CustomTextDocument::loadResource(int type, const QUrl& name)
Settings::getInstance().getEmojiFontPointSize());
QString fileName = QUrl::fromPercentEncoding(name.toEncoded()).mid(4).toHtmlEscaped();
std::shared_ptr<QIcon> icon = SmileyPack::getInstance().getAsIcon(fileName);
std::shared_ptr<QIcon> icon = smileyPack.getAsIcon(fileName);
emoticonIcons.append(icon);
return icon->pixmap(size);
}

View File

@ -25,16 +25,18 @@
#include <memory>
class QIcon;
class SmileyPack;
class CustomTextDocument : public QTextDocument
{
Q_OBJECT
public:
explicit CustomTextDocument(QObject* parent = nullptr);
explicit CustomTextDocument(SmileyPack&, QObject* parent = nullptr);
protected:
virtual QVariant loadResource(int type, const QUrl& name);
private:
QList<std::shared_ptr<QIcon>> emoticonIcons;
SmileyPack& smileyPack;
};

View File

@ -20,6 +20,10 @@
#include "documentcache.h"
#include "customtextdocument.h"
DocumentCache::DocumentCache(SmileyPack& smileyPack_)
: smileyPack{smileyPack_}
{
}
DocumentCache::~DocumentCache()
{
while (!documents.isEmpty())
@ -29,7 +33,7 @@ DocumentCache::~DocumentCache()
QTextDocument* DocumentCache::pop()
{
if (documents.empty())
documents.push(new CustomTextDocument);
documents.push(new CustomTextDocument(smileyPack));
return documents.pop();
}

View File

@ -22,11 +22,12 @@
#include <QStack>
class QTextDocument;
class SmileyPack;
class DocumentCache
{
public:
DocumentCache() = default;
explicit DocumentCache(SmileyPack& smileyPack);
~DocumentCache();
DocumentCache(DocumentCache&) = delete;
DocumentCache& operator=(const DocumentCache&) = delete;
@ -35,4 +36,5 @@ public:
void push(QTextDocument* doc);
private:
QStack<QTextDocument*> documents;
SmileyPack& smileyPack;
};

View File

@ -144,15 +144,6 @@ void SmileyPack::cleanupIconsCache()
}
}
/**
* @brief Returns the singleton instance.
*/
SmileyPack& SmileyPack::getInstance()
{
static SmileyPack smileyPack;
return smileyPack;
}
/**
* @brief Does the same as listSmileyPaths, but with default paths
*/

View File

@ -33,7 +33,11 @@ class SmileyPack : public QObject
Q_OBJECT
public:
static SmileyPack& getInstance();
SmileyPack();
SmileyPack(SmileyPack&) = delete;
SmileyPack& operator=(const SmileyPack&) = delete;
~SmileyPack() override;
static QList<QPair<QString, QString>> listSmileyPacks(const QStringList& paths);
static QList<QPair<QString, QString>> listSmileyPacks();
@ -47,11 +51,6 @@ private slots:
void cleanupIconsCache();
private:
SmileyPack();
SmileyPack(SmileyPack&) = delete;
SmileyPack& operator=(const SmileyPack&) = delete;
~SmileyPack() override;
bool load(const QString& filename);
void constructRegex();

View File

@ -31,7 +31,7 @@
#include <math.h>
EmoticonsWidget::EmoticonsWidget(QWidget* parent)
EmoticonsWidget::EmoticonsWidget(SmileyPack& smileyPack, QWidget* parent)
: QMenu(parent)
{
setStyleSheet(Style::getStylesheet("emoticonWidget/emoticonWidget.css"));
@ -48,7 +48,7 @@ EmoticonsWidget::EmoticonsWidget(QWidget* parent)
const int maxRows = 8;
const int itemsPerPage = maxRows * maxCols;
const QList<QStringList>& emoticons = SmileyPack::getInstance().getEmoticons();
const QList<QStringList>& emoticons = smileyPack.getEmoticons();
int itemCount = emoticons.size();
int pageCount = ceil(float(itemCount) / float(itemsPerPage));
int currPage = 0;
@ -86,7 +86,6 @@ EmoticonsWidget::EmoticonsWidget(QWidget* parent)
}
buttonLayout->addStretch();
SmileyPack& smileyPack = SmileyPack::getInstance();
for (const QStringList& set : emoticons) {
QPushButton* button = new QPushButton;
std::shared_ptr<QIcon> icon = smileyPack.getAsIcon(set[0]);

View File

@ -27,12 +27,13 @@
#include <memory>
class QIcon;
class SmileyPack;
class EmoticonsWidget : public QMenu
{
Q_OBJECT
public:
explicit EmoticonsWidget(QWidget* parent = nullptr);
explicit EmoticonsWidget(SmileyPack&, QWidget* parent = nullptr);
signals:
void insertEmoticon(QString str);

View File

@ -107,9 +107,9 @@ QString secondsToDHMS(quint32 duration)
} // namespace
ChatForm::ChatForm(Profile& profile, Friend* chatFriend, IChatLog& chatLog_,
IMessageDispatcher& messageDispatcher_, DocumentCache& documentCache_)
IMessageDispatcher& messageDispatcher_, DocumentCache& documentCache_, SmileyPack& smileyPack_)
: GenericChatForm(profile.getCore(), chatFriend, chatLog_, messageDispatcher_,
documentCache_)
documentCache_, smileyPack_)
, core{profile.getCore()}
, f(chatFriend)
, isTyping{false}

View File

@ -43,13 +43,14 @@ class QHideEvent;
class QMoveEvent;
class ImagePreviewButton;
class DocumentCache;
class SmileyPack;
class ChatForm : public GenericChatForm
{
Q_OBJECT
public:
ChatForm(Profile& profile, Friend* chatFriend, IChatLog& chatLog_,
IMessageDispatcher& messageDispatcher_, DocumentCache&);
IMessageDispatcher& messageDispatcher_, DocumentCache&, SmileyPack&);
~ChatForm() override;
void setStatusMessage(const QString& newMessage);

View File

@ -138,19 +138,20 @@ QPushButton* createButton(const QString& name, T* self, Fun onClickSlot)
GenericChatForm::GenericChatForm(const Core& core_, const Contact* contact, IChatLog& chatLog_,
IMessageDispatcher& messageDispatcher_, DocumentCache& documentCache,
QWidget* parent_)
SmileyPack& smileyPack_, QWidget* parent_)
: QWidget(parent_, Qt::Window)
, core{core_}
, audioInputFlag(false)
, audioOutputFlag(false)
, chatLog(chatLog_)
, messageDispatcher(messageDispatcher_)
, smileyPack{smileyPack_}
{
curRow = 0;
headWidget = new ChatFormHeader();
searchForm = new SearchForm();
dateInfo = new QLabel(this);
chatWidget = new ChatWidget(chatLog_, core, documentCache, this);
chatWidget = new ChatWidget(chatLog_, core, documentCache, smileyPack, this);
searchForm->hide();
dateInfo->setAlignment(Qt::AlignHCenter);
dateInfo->setVisible(false);
@ -452,10 +453,10 @@ void GenericChatForm::onSendTriggered()
void GenericChatForm::onEmoteButtonClicked()
{
// don't show the smiley selection widget if there are no smileys available
if (SmileyPack::getInstance().getEmoticons().empty())
if (smileyPack.getEmoticons().empty())
return;
EmoticonsWidget widget;
EmoticonsWidget widget(smileyPack);
connect(&widget, SIGNAL(insertEmoticon(QString)), this, SLOT(onEmoteInsertRequested(QString)));
widget.installEventFilter(this);

View File

@ -54,6 +54,7 @@ class QVBoxLayout;
class IMessageDispatcher;
struct Message;
class DocumentCache;
class SmileyPack;
namespace Ui {
class MainWindow;
@ -71,7 +72,7 @@ class GenericChatForm : public QWidget
public:
GenericChatForm(const Core& core_, const Contact* contact, IChatLog& chatLog_,
IMessageDispatcher& messageDispatcher_, DocumentCache&,
QWidget* parent_ = nullptr);
SmileyPack&, QWidget* parent_ = nullptr);
~GenericChatForm() override;
void setName(const QString& newName);
@ -167,4 +168,5 @@ protected:
IChatLog& chatLog;
IMessageDispatcher& messageDispatcher;
SmileyPack& smileyPack;
};

View File

@ -83,8 +83,9 @@ QString editName(const QString& name)
*/
GroupChatForm::GroupChatForm(Core& core_, Group* chatGroup, IChatLog& chatLog_,
IMessageDispatcher& messageDispatcher_, IGroupSettings& settings_, DocumentCache& documentCache_)
: GenericChatForm(core_, chatGroup, chatLog_, messageDispatcher_, documentCache_)
IMessageDispatcher& messageDispatcher_, IGroupSettings& settings_, DocumentCache& documentCache_,
SmileyPack& smileyPack_)
: GenericChatForm(core_, chatGroup, chatLog_, messageDispatcher_, documentCache_, smileyPack_)
, core{core_}
, group(chatGroup)
, inCall(false)

View File

@ -36,14 +36,15 @@ struct Message;
class Settings;
class IGroupSettings;
class DocumentCache;
class SmileyPack;
class GroupChatForm : public GenericChatForm
{
Q_OBJECT
public:
explicit GroupChatForm(Core& core_, Group* chatGroup, IChatLog& chatLog_,
GroupChatForm(Core& core_, Group* chatGroup, IChatLog& chatLog_,
IMessageDispatcher& messageDispatcher_, IGroupSettings& settings_,
DocumentCache&);
DocumentCache&, SmileyPack&);
~GroupChatForm();
void peerAudioPlaying(ToxPk peerPk);

View File

@ -54,8 +54,9 @@
*
* Restores all controls from the settings.
*/
UserInterfaceForm::UserInterfaceForm(SettingsWidget* myParent)
UserInterfaceForm::UserInterfaceForm(SmileyPack& smileyPack_, SettingsWidget* myParent)
: GenericForm(QPixmap(":/img/settings/general.png"))
, smileyPack{smileyPack_}
{
parent = myParent;
@ -237,7 +238,7 @@ void UserInterfaceForm::on_smileyPackBrowser_currentIndexChanged(int index)
*/
void UserInterfaceForm::reloadSmileys()
{
QList<QStringList> emoticons = SmileyPack::getInstance().getEmoticons();
QList<QStringList> emoticons = smileyPack.getEmoticons();
// sometimes there are no emoticons available, don't crash in this case
if (emoticons.isEmpty()) {
@ -252,7 +253,7 @@ void UserInterfaceForm::reloadSmileys()
emoticonsIcons.clear();
const QSize size(18, 18);
for (int i = 0; i < smileLabels.size(); ++i) {
std::shared_ptr<QIcon> icon = SmileyPack::getInstance().getAsIcon(smileys[i]);
std::shared_ptr<QIcon> icon = smileyPack.getAsIcon(smileys[i]);
emoticonsIcons.append(icon);
smileLabels[i]->setPixmap(icon->pixmap(size));
smileLabels[i]->setToolTip(smileys[i]);

View File

@ -28,11 +28,13 @@ namespace Ui {
class UserInterfaceSettings;
}
class SmileyPack;
class UserInterfaceForm : public GenericForm
{
Q_OBJECT
public:
explicit UserInterfaceForm(SettingsWidget* myParent);
UserInterfaceForm(SmileyPack&, SettingsWidget* myParent);
~UserInterfaceForm();
QString getFormName() final
{
@ -74,4 +76,5 @@ private:
SettingsWidget* parent;
Ui::UserInterfaceSettings* bodyUI;
const int MAX_FORMAT_LENGTH = 128;
SmileyPack& smileyPack;
};

View File

@ -41,7 +41,8 @@
#include <memory>
SettingsWidget::SettingsWidget(UpdateCheck* updateCheck, IAudioControl& audio, Core* core, Widget* parent)
SettingsWidget::SettingsWidget(UpdateCheck* updateCheck, IAudioControl& audio,
Core* core, SmileyPack& smileyPack, Widget* parent)
: QWidget(parent, Qt::Window)
{
CoreAV* coreAV = core->getAv();
@ -60,7 +61,7 @@ SettingsWidget::SettingsWidget(UpdateCheck* updateCheck, IAudioControl& audio, C
std::unique_ptr<GeneralForm> gfrm(new GeneralForm(this));
connect(gfrm.get(), &GeneralForm::updateIcons, parent, &Widget::updateIcons);
std::unique_ptr<UserInterfaceForm> uifrm(new UserInterfaceForm(this));
std::unique_ptr<UserInterfaceForm> uifrm(new UserInterfaceForm(smileyPack, this));
std::unique_ptr<PrivacyForm> pfrm(new PrivacyForm(core));
connect(pfrm.get(), &PrivacyForm::clearAllReceipts, parent, &Widget::clearAllReceipts);

View File

@ -38,12 +38,13 @@ class QTabWidget;
class ContentLayout;
class UpdateCheck;
class Widget;
class SmileyPack;
class SettingsWidget : public QWidget
{
Q_OBJECT
public:
SettingsWidget(UpdateCheck* updateCheck, IAudioControl& audio, Core *core, Widget* parent = nullptr);
SettingsWidget(UpdateCheck* updateCheck, IAudioControl& audio, Core *core, SmileyPack&, Widget* parent = nullptr);
~SettingsWidget();
bool isShown() const;

View File

@ -80,6 +80,7 @@
#include "src/widget/style.h"
#include "src/widget/translator.h"
#include "tool/removefrienddialog.h"
#include "src/persistence/smileypack.h"
bool toxActivateEventHandler(const QByteArray&)
{
@ -148,7 +149,8 @@ Widget::Widget(Profile &profile_, IAudioControl& audio_, QWidget* parent)
, eventIcon(false)
, audio(audio_)
, settings(Settings::getInstance())
, documentCache(new DocumentCache())
, smileyPack(new SmileyPack())
, documentCache(new DocumentCache(*smileyPack))
{
installEventFilter(this);
QString locale = settings.getTranslation();
@ -290,7 +292,7 @@ void Widget::init()
updateCheck = std::unique_ptr<UpdateCheck>(new UpdateCheck(settings));
connect(updateCheck.get(), &UpdateCheck::updateAvailable, this, &Widget::onUpdateAvailable);
#endif
settingsWidget = new SettingsWidget(updateCheck.get(), audio, core, this);
settingsWidget = new SettingsWidget(updateCheck.get(), audio, core, *smileyPack, this);
#if UPDATE_CHECK_ENABLED
updateCheck->checkForUpdate();
#endif
@ -1188,7 +1190,7 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk)
std::make_shared<ChatHistory>(*newfriend, history, *core, settings,
*friendMessageDispatcher);
auto friendForm = new ChatForm(profile, newfriend, *chatHistory,
*friendMessageDispatcher, *documentCache);
*friendMessageDispatcher, *documentCache, *smileyPack);
connect(friendForm, &ChatForm::updateFriendActivity, this, &Widget::updateFriendActivity);
friendMessageDispatchers[friendPk] = friendMessageDispatcher;
@ -2171,7 +2173,8 @@ Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId)
connect(messageDispatcher.get(), &IMessageDispatcher::messageReceived, notifyReceivedCallback);
groupAlertConnections.insert(groupId, notifyReceivedConnection);
auto form = new GroupChatForm(*core, newgroup, *groupChatLog, *messageDispatcher, settings, *documentCache);
auto form = new GroupChatForm(*core, newgroup, *groupChatLog, *messageDispatcher,
settings, *documentCache, *smileyPack);
connect(&settings, &Settings::nameColorsChanged, form, &GenericChatForm::setColorizedNames);
form->setColorizedNames(settings.getEnableGroupChatsColor());
groupMessageDispatchers[groupId] = messageDispatcher;

View File

@ -83,8 +83,7 @@ class UpdateCheck;
class Settings;
class IChatLog;
class ChatHistory;
class DocumentCache;
class SmileyPack;
class Widget final : public QMainWindow
{
Q_OBJECT
@ -382,6 +381,7 @@ private:
QAction* nextConversationAction;
QAction* previousConversationAction;
#endif
std::unique_ptr<SmileyPack> smileyPack;
std::unique_ptr<DocumentCache> documentCache;
};

View File

@ -58,7 +58,7 @@ TestSmileyPack::TestSmileyPack()
*/
void TestSmileyPack::testSmilifySingleCharEmoji()
{
auto& smileyPack = SmileyPack::getInstance();
SmileyPack smileyPack{};
auto result = smileyPack.smileyfied("😊");
QVERIFY(result == SmileyPack::getAsRichText("😊"));
@ -73,7 +73,7 @@ void TestSmileyPack::testSmilifySingleCharEmoji()
*/
void TestSmileyPack::testSmilifyMultiCharEmoji()
{
auto& smileyPack = SmileyPack::getInstance();
SmileyPack smileyPack{};
auto result = smileyPack.smileyfied("🇬🇧");
QVERIFY(result == SmileyPack::getAsRichText("🇬🇧"));
@ -94,7 +94,7 @@ void TestSmileyPack::testSmilifyMultiCharEmoji()
*/
void TestSmileyPack::testSmilifyAsciiEmoticon()
{
auto& smileyPack = SmileyPack::getInstance();
SmileyPack smileyPack{};
auto result = smileyPack.smileyfied(":-)");
QVERIFY(result == SmileyPack::getAsRichText(":-)"));