mirror of https://github.com/cutefishos/fishui
Add new IconItem
parent
e9fbab7f9e
commit
a1319b7d40
@ -0,0 +1,137 @@
|
||||
#include "newiconitem.h"
|
||||
|
||||
#include <QQuickWindow>
|
||||
#include <QApplication>
|
||||
#include <QImageReader>
|
||||
#include <QPainter>
|
||||
#include <QIcon>
|
||||
|
||||
NewIconItem::NewIconItem(QQuickItem *parent)
|
||||
: QQuickPaintedItem(parent)
|
||||
{
|
||||
setFlag(ItemHasContents, true);
|
||||
setSmooth(true);
|
||||
}
|
||||
|
||||
void NewIconItem::setSource(const QVariant &source)
|
||||
{
|
||||
if (source == m_source) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_source = source;
|
||||
QString sourceString = source.toString();
|
||||
|
||||
if (source.canConvert<QIcon>() && !source.value<QIcon>().name().isEmpty()) {
|
||||
sourceString = source.value<QIcon>().name();
|
||||
}
|
||||
|
||||
QString localFile;
|
||||
if (sourceString.startsWith(QLatin1String("file:"))) {
|
||||
localFile = QUrl(sourceString).toLocalFile();
|
||||
} else if (sourceString.startsWith(QLatin1Char('/'))) {
|
||||
localFile = sourceString;
|
||||
} else if (sourceString.startsWith("qrc:/")) {
|
||||
localFile = sourceString.remove(0, 3);
|
||||
} else if (sourceString.startsWith(":/")) {
|
||||
localFile = sourceString;
|
||||
}
|
||||
|
||||
if (!localFile.isEmpty()) {
|
||||
if (sourceString.endsWith(QLatin1String(".svg"))
|
||||
|| sourceString.endsWith(QLatin1String(".svgz"))
|
||||
|| sourceString.endsWith(QLatin1String(".ico"))) {
|
||||
m_icon = QIcon(localFile);
|
||||
m_iconName.clear();
|
||||
m_image = QImage();
|
||||
} else {
|
||||
m_image = QImage(localFile);
|
||||
m_iconName.clear();
|
||||
m_icon = QIcon();
|
||||
}
|
||||
} else if (source.canConvert<QIcon>()) {
|
||||
m_icon = source.value<QIcon>();
|
||||
m_iconName.clear();
|
||||
m_image = QImage();
|
||||
} else if (source.canConvert<QImage>()) {
|
||||
m_image = source.value<QImage>();
|
||||
m_iconName.clear();
|
||||
m_icon = QIcon();
|
||||
} else {
|
||||
// From icon theme.
|
||||
m_icon = QIcon();
|
||||
m_image = QImage();
|
||||
m_iconName = sourceString;
|
||||
}
|
||||
|
||||
if (width() > 0 && height() > 0) {
|
||||
loadPixmap();
|
||||
}
|
||||
|
||||
emit sourceChanged();
|
||||
}
|
||||
|
||||
QVariant NewIconItem::source() const
|
||||
{
|
||||
return m_source;
|
||||
}
|
||||
|
||||
void NewIconItem::paint(QPainter *painter)
|
||||
{
|
||||
if (m_iconPixmap.isNull())
|
||||
return;
|
||||
|
||||
painter->setRenderHints(QPainter::SmoothPixmapTransform);
|
||||
painter->drawPixmap(contentsBoundingRect().toRect(), m_iconPixmap);
|
||||
}
|
||||
|
||||
void NewIconItem::updateIcon()
|
||||
{
|
||||
loadPixmap();
|
||||
}
|
||||
|
||||
void NewIconItem::loadPixmap()
|
||||
{
|
||||
if (!isComponentComplete()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QSize size = QSize(width(), height());
|
||||
QPixmap result;
|
||||
|
||||
if (size.width() < 0 ||
|
||||
size.height() < 0) {
|
||||
m_iconPixmap = QPixmap();
|
||||
update();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_iconName.isEmpty()) {
|
||||
QIcon icon = QIcon::fromTheme(m_iconName);
|
||||
|
||||
if (icon.isNull())
|
||||
icon = QIcon::fromTheme("application-x-desktop");
|
||||
|
||||
result = icon.pixmap(size * qApp->devicePixelRatio());
|
||||
} else if (!m_icon.isNull()) {
|
||||
result = m_icon.pixmap(window(), size * qApp->devicePixelRatio());
|
||||
} else if (!m_image.isNull()) {
|
||||
result = QPixmap::fromImage(m_image);
|
||||
} else {
|
||||
m_iconPixmap = QPixmap();
|
||||
update();
|
||||
return;
|
||||
}
|
||||
|
||||
m_iconPixmap = result;
|
||||
update();
|
||||
}
|
||||
|
||||
void NewIconItem::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
|
||||
{
|
||||
QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
|
||||
|
||||
if (newGeometry.width() > 0 && newGeometry.height() > 0) {
|
||||
loadPixmap();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
#ifndef NEWICONITEM_H
|
||||
#define NEWICONITEM_H
|
||||
|
||||
#include <QQuickPaintedItem>
|
||||
#include <QPixmap>
|
||||
#include <QPixmapCache>
|
||||
#include <QIcon>
|
||||
|
||||
class IconItemSource;
|
||||
class NewIconItem : public QQuickPaintedItem
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QVariant source READ source WRITE setSource NOTIFY sourceChanged)
|
||||
|
||||
public:
|
||||
NewIconItem(QQuickItem *parent = nullptr);
|
||||
|
||||
void setSource(const QVariant &source);
|
||||
QVariant source() const;
|
||||
|
||||
void paint(QPainter *painter) override;
|
||||
|
||||
Q_INVOKABLE void updateIcon();
|
||||
|
||||
signals:
|
||||
void sourceChanged();
|
||||
|
||||
protected:
|
||||
void loadPixmap();
|
||||
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
|
||||
|
||||
private:
|
||||
QVariant m_source;
|
||||
|
||||
QIcon m_icon;
|
||||
QImage m_image;
|
||||
|
||||
QString m_iconName;
|
||||
QPixmap m_iconPixmap;
|
||||
};
|
||||
|
||||
#endif // NEWICONITEM_H
|
||||
Loading…
Reference in New Issue