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:
parent
d83400bcdc
commit
c640007775
|
@ -40,7 +40,9 @@ QVariant CustomTextDocument::loadResource(int type, const QUrl& name)
|
|||
Settings::getInstance().getEmojiFontPointSize());
|
||||
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);
|
||||
|
|
|
@ -21,15 +21,23 @@
|
|||
#define CUSTOMTEXTDOCUMENT_H
|
||||
|
||||
#include <QTextDocument>
|
||||
#include <QList>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class QIcon;
|
||||
|
||||
class CustomTextDocument : public QTextDocument
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit CustomTextDocument(QObject* parent = 0);
|
||||
explicit CustomTextDocument(QObject* parent = nullptr);
|
||||
|
||||
protected:
|
||||
virtual QVariant loadResource(int type, const QUrl& name);
|
||||
|
||||
private:
|
||||
QList<std::shared_ptr<QIcon>> emoticonIcons;
|
||||
};
|
||||
|
||||
#endif // CUSTOMTEXTDOCUMENT_H
|
||||
|
|
|
@ -204,7 +204,6 @@ bool SmileyPack::load(const QString& filename)
|
|||
emoticons.clear();
|
||||
emoticonToIcon.clear();
|
||||
emoticonToPath.clear();
|
||||
icons.clear();
|
||||
|
||||
for (int i = 0; i < iconsCount; ++i) {
|
||||
QDomNode node = emoticonElements.at(i);
|
||||
|
@ -267,19 +266,21 @@ QList<QStringList> SmileyPack::getEmoticons() const
|
|||
* @param emoticon Passed 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);
|
||||
if (emoticonToIcon.contains(emoticon))
|
||||
return *(emoticonToIcon[emoticon]);
|
||||
return emoticonToIcon[emoticon];
|
||||
|
||||
if (!emoticonToPath.contains(emoticon))
|
||||
return QIcon{};
|
||||
const auto iconPathIt = emoticonToPath.find(emoticon);
|
||||
if (iconPathIt == emoticonToPath.end()) {
|
||||
return std::make_shared<QIcon>();
|
||||
}
|
||||
|
||||
const QString& iconPath = emoticonToPath[emoticon];
|
||||
icons.append(QIcon{iconPath});
|
||||
emoticonToIcon.insert(emoticon, &icons.last());
|
||||
return icons.last();
|
||||
const QString& iconPath = iconPathIt.value();
|
||||
auto icon = std::make_shared<QIcon>(iconPath);
|
||||
emoticonToIcon.insert(emoticon, icon);
|
||||
return icon;
|
||||
}
|
||||
|
||||
void SmileyPack::onSmileyPackChanged()
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
#include <QMap>
|
||||
#include <QMutex>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class SmileyPack : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
@ -35,7 +37,7 @@ public:
|
|||
|
||||
QString smileyfied(const QString& msg);
|
||||
QList<QStringList> getEmoticons() const;
|
||||
QIcon getAsIcon(const QString& key);
|
||||
std::shared_ptr<QIcon> getAsIcon(const QString& key);
|
||||
|
||||
private slots:
|
||||
void onSmileyPackChanged();
|
||||
|
@ -47,9 +49,8 @@ private:
|
|||
|
||||
bool load(const QString& filename);
|
||||
|
||||
QVector<QIcon> icons;
|
||||
QMap<QString, const QIcon*> emoticonToIcon;
|
||||
QMap<QString, QString> emoticonToPath;
|
||||
QMap<QString, std::shared_ptr<QIcon>> emoticonToIcon;
|
||||
QHash<QString, QString> emoticonToPath;
|
||||
QList<QStringList> emoticons;
|
||||
QString path;
|
||||
mutable QMutex loadingMutex;
|
||||
|
|
|
@ -86,9 +86,12 @@ EmoticonsWidget::EmoticonsWidget(QWidget* parent)
|
|||
}
|
||||
buttonLayout->addStretch();
|
||||
|
||||
SmileyPack& smileyPack = SmileyPack::getInstance();
|
||||
for (const QStringList& set : emoticons) {
|
||||
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->setProperty("sequence", set[0]);
|
||||
button->setCursor(Qt::PointingHandCursor);
|
||||
|
|
|
@ -23,6 +23,11 @@
|
|||
#include <QMenu>
|
||||
#include <QStackedWidget>
|
||||
#include <QVBoxLayout>
|
||||
#include <QVector>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class QIcon;
|
||||
|
||||
class EmoticonsWidget : public QMenu
|
||||
{
|
||||
|
@ -47,6 +52,7 @@ protected:
|
|||
private:
|
||||
QStackedWidget stack;
|
||||
QVBoxLayout layout;
|
||||
QList<std::shared_ptr<QIcon>> emoticonsIcons;
|
||||
|
||||
public:
|
||||
QSize sizeHint() const override;
|
||||
|
|
|
@ -239,10 +239,12 @@ void UserInterfaceForm::reloadSmileys()
|
|||
for (int i = 0; i < emoticons.size(); ++i)
|
||||
smileys.push_front(emoticons.at(i).first());
|
||||
|
||||
emoticonsIcons.clear();
|
||||
const QSize size(18, 18);
|
||||
for (int i = 0; i < smileLabels.size(); ++i) {
|
||||
QIcon icon = SmileyPack::getInstance().getAsIcon(smileys[i]);
|
||||
smileLabels[i]->setPixmap(icon.pixmap(size));
|
||||
std::shared_ptr<QIcon> icon = SmileyPack::getInstance().getAsIcon(smileys[i]);
|
||||
emoticonsIcons.append(icon);
|
||||
smileLabels[i]->setPixmap(icon->pixmap(size));
|
||||
smileLabels[i]->setToolTip(smileys[i]);
|
||||
}
|
||||
|
||||
|
@ -253,8 +255,7 @@ void UserInterfaceForm::reloadSmileys()
|
|||
int maxSide = qMin(desktop.geometry().height() / sideSize, desktop.geometry().width() / sideSize);
|
||||
QSize maxSize(maxSide, maxSide);
|
||||
|
||||
QIcon icon = SmileyPack::getInstance().getAsIcon(smileys[0]);
|
||||
QSize actualSize = icon.actualSize(maxSize);
|
||||
QSize actualSize = emoticonsIcons.first()->actualSize(maxSize);
|
||||
bodyUI->emoticonSize->setMaximum(actualSize.width());
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
#include "genericsettings.h"
|
||||
#include "src/widget/form/settingswidget.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace Ui {
|
||||
class UserInterfaceSettings;
|
||||
}
|
||||
|
@ -65,6 +67,7 @@ private:
|
|||
|
||||
private:
|
||||
QList<QLabel*> smileLabels;
|
||||
QList<std::shared_ptr<QIcon>> emoticonsIcons;
|
||||
SettingsWidget* parent;
|
||||
Ui::UserInterfaceSettings* bodyUI;
|
||||
const int MAX_FORMAT_LENGTH = 128;
|
||||
|
|
Loading…
Reference in New Issue
Block a user