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

perf(smileys): Use shared_ptr to automaticaly count references

This commit is contained in:
Diadlo 2017-10-31 16:48:46 +03:00
parent d83400bcdc
commit c640007775
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
8 changed files with 45 additions and 20 deletions

View File

@ -40,7 +40,9 @@ QVariant CustomTextDocument::loadResource(int type, const QUrl& name)
Settings::getInstance().getEmojiFontPointSize()); Settings::getInstance().getEmojiFontPointSize());
QString fileName = QUrl::fromPercentEncoding(name.toEncoded()).mid(4).toHtmlEscaped(); QString fileName = QUrl::fromPercentEncoding(name.toEncoded()).mid(4).toHtmlEscaped();
return SmileyPack::getInstance().getAsIcon(fileName).pixmap(size); std::shared_ptr<QIcon> icon = SmileyPack::getInstance().getAsIcon(fileName);
emoticonIcons.append(icon);
return icon->pixmap(size);
} }
return QTextDocument::loadResource(type, name); return QTextDocument::loadResource(type, name);

View File

@ -21,15 +21,23 @@
#define CUSTOMTEXTDOCUMENT_H #define CUSTOMTEXTDOCUMENT_H
#include <QTextDocument> #include <QTextDocument>
#include <QList>
#include <memory>
class QIcon;
class CustomTextDocument : public QTextDocument class CustomTextDocument : public QTextDocument
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit CustomTextDocument(QObject* parent = 0); explicit CustomTextDocument(QObject* parent = nullptr);
protected: protected:
virtual QVariant loadResource(int type, const QUrl& name); virtual QVariant loadResource(int type, const QUrl& name);
private:
QList<std::shared_ptr<QIcon>> emoticonIcons;
}; };
#endif // CUSTOMTEXTDOCUMENT_H #endif // CUSTOMTEXTDOCUMENT_H

View File

@ -204,7 +204,6 @@ bool SmileyPack::load(const QString& filename)
emoticons.clear(); emoticons.clear();
emoticonToIcon.clear(); emoticonToIcon.clear();
emoticonToPath.clear(); emoticonToPath.clear();
icons.clear();
for (int i = 0; i < iconsCount; ++i) { for (int i = 0; i < iconsCount; ++i) {
QDomNode node = emoticonElements.at(i); QDomNode node = emoticonElements.at(i);
@ -267,19 +266,21 @@ QList<QStringList> SmileyPack::getEmoticons() const
* @param emoticon Passed emoticon * @param emoticon Passed emoticon
* @return Returns cached icon according to passed emoticon, null if no icon mapped to this emoticon * @return Returns cached icon according to passed emoticon, null if no icon mapped to this emoticon
*/ */
QIcon SmileyPack::getAsIcon(const QString& emoticon) std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon)
{ {
QMutexLocker locker(&loadingMutex); QMutexLocker locker(&loadingMutex);
if (emoticonToIcon.contains(emoticon)) if (emoticonToIcon.contains(emoticon))
return *(emoticonToIcon[emoticon]); return emoticonToIcon[emoticon];
if (!emoticonToPath.contains(emoticon)) const auto iconPathIt = emoticonToPath.find(emoticon);
return QIcon{}; if (iconPathIt == emoticonToPath.end()) {
return std::make_shared<QIcon>();
}
const QString& iconPath = emoticonToPath[emoticon]; const QString& iconPath = iconPathIt.value();
icons.append(QIcon{iconPath}); auto icon = std::make_shared<QIcon>(iconPath);
emoticonToIcon.insert(emoticon, &icons.last()); emoticonToIcon.insert(emoticon, icon);
return icons.last(); return icon;
} }
void SmileyPack::onSmileyPackChanged() void SmileyPack::onSmileyPackChanged()

View File

@ -24,6 +24,8 @@
#include <QMap> #include <QMap>
#include <QMutex> #include <QMutex>
#include <memory>
class SmileyPack : public QObject class SmileyPack : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -35,7 +37,7 @@ public:
QString smileyfied(const QString& msg); QString smileyfied(const QString& msg);
QList<QStringList> getEmoticons() const; QList<QStringList> getEmoticons() const;
QIcon getAsIcon(const QString& key); std::shared_ptr<QIcon> getAsIcon(const QString& key);
private slots: private slots:
void onSmileyPackChanged(); void onSmileyPackChanged();
@ -47,9 +49,8 @@ private:
bool load(const QString& filename); bool load(const QString& filename);
QVector<QIcon> icons; QMap<QString, std::shared_ptr<QIcon>> emoticonToIcon;
QMap<QString, const QIcon*> emoticonToIcon; QHash<QString, QString> emoticonToPath;
QMap<QString, QString> emoticonToPath;
QList<QStringList> emoticons; QList<QStringList> emoticons;
QString path; QString path;
mutable QMutex loadingMutex; mutable QMutex loadingMutex;

View File

@ -86,9 +86,12 @@ EmoticonsWidget::EmoticonsWidget(QWidget* parent)
} }
buttonLayout->addStretch(); buttonLayout->addStretch();
SmileyPack& smileyPack = SmileyPack::getInstance();
for (const QStringList& set : emoticons) { for (const QStringList& set : emoticons) {
QPushButton* button = new QPushButton; QPushButton* button = new QPushButton;
button->setIcon(SmileyPack::getInstance().getAsIcon(set[0]).pixmap(size)); std::shared_ptr<QIcon> icon = smileyPack.getAsIcon(set[0]);
emoticonsIcons.append(icon);
button->setIcon(icon->pixmap(size));
button->setToolTip(set.join(" ")); button->setToolTip(set.join(" "));
button->setProperty("sequence", set[0]); button->setProperty("sequence", set[0]);
button->setCursor(Qt::PointingHandCursor); button->setCursor(Qt::PointingHandCursor);

View File

@ -23,6 +23,11 @@
#include <QMenu> #include <QMenu>
#include <QStackedWidget> #include <QStackedWidget>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QVector>
#include <memory>
class QIcon;
class EmoticonsWidget : public QMenu class EmoticonsWidget : public QMenu
{ {
@ -47,6 +52,7 @@ protected:
private: private:
QStackedWidget stack; QStackedWidget stack;
QVBoxLayout layout; QVBoxLayout layout;
QList<std::shared_ptr<QIcon>> emoticonsIcons;
public: public:
QSize sizeHint() const override; QSize sizeHint() const override;

View File

@ -239,10 +239,12 @@ void UserInterfaceForm::reloadSmileys()
for (int i = 0; i < emoticons.size(); ++i) for (int i = 0; i < emoticons.size(); ++i)
smileys.push_front(emoticons.at(i).first()); smileys.push_front(emoticons.at(i).first());
emoticonsIcons.clear();
const QSize size(18, 18); const QSize size(18, 18);
for (int i = 0; i < smileLabels.size(); ++i) { for (int i = 0; i < smileLabels.size(); ++i) {
QIcon icon = SmileyPack::getInstance().getAsIcon(smileys[i]); std::shared_ptr<QIcon> icon = SmileyPack::getInstance().getAsIcon(smileys[i]);
smileLabels[i]->setPixmap(icon.pixmap(size)); emoticonsIcons.append(icon);
smileLabels[i]->setPixmap(icon->pixmap(size));
smileLabels[i]->setToolTip(smileys[i]); smileLabels[i]->setToolTip(smileys[i]);
} }
@ -253,8 +255,7 @@ void UserInterfaceForm::reloadSmileys()
int maxSide = qMin(desktop.geometry().height() / sideSize, desktop.geometry().width() / sideSize); int maxSide = qMin(desktop.geometry().height() / sideSize, desktop.geometry().width() / sideSize);
QSize maxSize(maxSide, maxSide); QSize maxSize(maxSide, maxSide);
QIcon icon = SmileyPack::getInstance().getAsIcon(smileys[0]); QSize actualSize = emoticonsIcons.first()->actualSize(maxSize);
QSize actualSize = icon.actualSize(maxSize);
bodyUI->emoticonSize->setMaximum(actualSize.width()); bodyUI->emoticonSize->setMaximum(actualSize.width());
} }

View File

@ -23,6 +23,8 @@
#include "genericsettings.h" #include "genericsettings.h"
#include "src/widget/form/settingswidget.h" #include "src/widget/form/settingswidget.h"
#include <memory>
namespace Ui { namespace Ui {
class UserInterfaceSettings; class UserInterfaceSettings;
} }
@ -65,6 +67,7 @@ private:
private: private:
QList<QLabel*> smileLabels; QList<QLabel*> smileLabels;
QList<std::shared_ptr<QIcon>> emoticonsIcons;
SettingsWidget* parent; SettingsWidget* parent;
Ui::UserInterfaceSettings* bodyUI; Ui::UserInterfaceSettings* bodyUI;
const int MAX_FORMAT_LENGTH = 128; const int MAX_FORMAT_LENGTH = 128;