forked from qt-creator/qt-creator
Add IconDisplay class and expose it to lua
Change-Id: I5cc9725de1666e5e19d869f3249d3b287de8a4b9 Reviewed-by: Artur Twardy <atw@spyro-soft.com> Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
committed by
Marcus Tillmanns
parent
862590320d
commit
46af5ac7d6
@@ -83,6 +83,7 @@ add_qtc_library(Utils
|
||||
htmldocextractor.cpp htmldocextractor.h
|
||||
icon.cpp icon.h
|
||||
iconbutton.cpp iconbutton.h
|
||||
icondisplay.h icondisplay.cpp
|
||||
id.cpp id.h
|
||||
indexedcontainerproxyconstiterator.h
|
||||
infobar.cpp infobar.h
|
||||
|
64
src/libs/utils/icondisplay.cpp
Normal file
64
src/libs/utils/icondisplay.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
// Copyright (C) 2024 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "icondisplay.h"
|
||||
|
||||
#include "icon.h"
|
||||
|
||||
#include <QIcon>
|
||||
#include <QPainter>
|
||||
#include <QPixmap>
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
|
||||
class IconDisplayPrivate
|
||||
{
|
||||
public:
|
||||
QSize m_iconSize;
|
||||
QIcon m_icon;
|
||||
};
|
||||
|
||||
Utils::IconDisplay::IconDisplay(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
d(std::make_unique<IconDisplayPrivate>())
|
||||
{
|
||||
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
||||
}
|
||||
|
||||
IconDisplay::~IconDisplay() = default;
|
||||
|
||||
void IconDisplay::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
|
||||
QPainter painter(this);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
||||
|
||||
qreal dpr = devicePixelRatio();
|
||||
QSize actualSize = size();
|
||||
auto px = d->m_icon.pixmap(actualSize, dpr);
|
||||
px.setDevicePixelRatio(dpr);
|
||||
|
||||
QRect r(QPoint(0, 0), actualSize);
|
||||
r.moveCenter(rect().center());
|
||||
|
||||
painter.drawPixmap(r, px);
|
||||
}
|
||||
|
||||
QSize IconDisplay::sizeHint() const
|
||||
{
|
||||
if (d->m_icon.isNull())
|
||||
return {};
|
||||
|
||||
return d->m_icon.availableSizes().first();
|
||||
}
|
||||
|
||||
void IconDisplay::setIcon(const Icon &icon)
|
||||
{
|
||||
d->m_icon = icon.icon();
|
||||
update();
|
||||
}
|
||||
|
||||
} // namespace Utils
|
39
src/libs/utils/icondisplay.h
Normal file
39
src/libs/utils/icondisplay.h
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2024 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "utils_global.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QPaintEvent;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace Utils {
|
||||
|
||||
class Icon;
|
||||
class IconDisplayPrivate;
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT IconDisplay : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
IconDisplay(QWidget *parent = nullptr);
|
||||
~IconDisplay() override;
|
||||
|
||||
void setIcon(const Utils::Icon &);
|
||||
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
QSize sizeHint() const override;
|
||||
|
||||
private:
|
||||
friend class IconDisplayPrivates;
|
||||
|
||||
std::unique_ptr<IconDisplayPrivate> d;
|
||||
};
|
||||
|
||||
} // namespace Utils
|
@@ -6,6 +6,7 @@
|
||||
#include "fancylineedit.h"
|
||||
#include "filepath.h"
|
||||
#include "icon.h"
|
||||
#include "icondisplay.h"
|
||||
#include "markdownbrowser.h"
|
||||
#include "qtcassert.h"
|
||||
#include "spinner/spinner.h"
|
||||
@@ -1211,6 +1212,17 @@ void Spinner::setDecorated(bool on)
|
||||
access(this)->setDecorated(on);
|
||||
}
|
||||
|
||||
IconDisplay::IconDisplay(std::initializer_list<I> ps)
|
||||
{
|
||||
ptr = new Implementation;
|
||||
apply(this, ps);
|
||||
}
|
||||
|
||||
void IconDisplay::setIcon(const Utils::Icon &icon)
|
||||
{
|
||||
access(this)->setIcon(icon);
|
||||
}
|
||||
|
||||
// void createItem(LayoutItem *item, QWidget *t)
|
||||
// {
|
||||
// if (auto l = qobject_cast<QLabel *>(t))
|
||||
|
@@ -50,6 +50,8 @@ namespace Utils {
|
||||
class FancyLineEdit;
|
||||
class FilePath;
|
||||
class MarkdownBrowser;
|
||||
class Icon;
|
||||
class IconDisplay;
|
||||
} // namespace Utils
|
||||
|
||||
namespace Layouting {
|
||||
@@ -362,6 +364,16 @@ public:
|
||||
void setChildrenCollapsible(bool collapsible);
|
||||
};
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT IconDisplay : public Widget
|
||||
{
|
||||
public:
|
||||
using Implementation = Utils::IconDisplay;
|
||||
using I = Building::BuilderItem<IconDisplay>;
|
||||
|
||||
IconDisplay(std::initializer_list<I> ps);
|
||||
void setIcon(const Utils::Icon &icon);
|
||||
};
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT ScrollArea : public Widget
|
||||
{
|
||||
public:
|
||||
|
@@ -110,6 +110,7 @@ CREATE_HAS_FUNC(onRightSideIconClicked, nullptr, nullptr)
|
||||
CREATE_HAS_FUNC(setTextInteractionFlags, Qt::TextInteractionFlags())
|
||||
CREATE_HAS_FUNC(setFixedSize, QSize())
|
||||
CREATE_HAS_FUNC(setVisible, bool())
|
||||
CREATE_HAS_FUNC(setIcon, Utils::Icon());
|
||||
|
||||
template<class T>
|
||||
void setProperties(std::unique_ptr<T> &item, const sol::table &children, QObject *guard)
|
||||
@@ -120,6 +121,12 @@ void setProperties(std::unique_ptr<T> &item, const sol::table &children, QObject
|
||||
item->setVisible(*visible);
|
||||
}
|
||||
|
||||
if constexpr (has_setIcon<T>) {
|
||||
const auto icon = children.get<sol::optional<IconFilePathOrString>>("icon");
|
||||
if (icon)
|
||||
item->setIcon(*toIcon(*icon));
|
||||
}
|
||||
|
||||
if constexpr (has_setTextInteractionFlags<T>) {
|
||||
const auto interactionFlags = children.get<sol::optional<sol::table>>("interactionFlags");
|
||||
if (interactionFlags) {
|
||||
@@ -628,6 +635,15 @@ void setupGuiModule()
|
||||
sol::base_classes,
|
||||
sol::bases<Widget, Object, Thing>());
|
||||
|
||||
gui.new_usertype<Layouting::IconDisplay>(
|
||||
"IconDisplay",
|
||||
sol::call_constructor,
|
||||
sol::factories([guard](const sol::table &children) {
|
||||
return constructWidgetType<Layouting::IconDisplay>(children, guard);
|
||||
}),
|
||||
sol::base_classes,
|
||||
sol::bases<Widget, Object, Thing>());
|
||||
|
||||
gui["br"] = &br;
|
||||
gui["st"] = &st;
|
||||
gui["empty"] = ∅
|
||||
|
@@ -224,6 +224,17 @@ function gui.TabWidget(name, child) end
|
||||
---@field decorated bool Display spinner with custom styleSheet defined inside control (default true)
|
||||
local spinner = {}
|
||||
|
||||
---@class IconDisplay : Widget
|
||||
local IconDisplay = {}
|
||||
|
||||
---@class (exact) IconDisplayOptions : BaseWidgetOptions
|
||||
---@param icon? Utils.Icon|FilePath|string The icon to display
|
||||
gui.iconDisplayOptions = {}
|
||||
|
||||
---@param options IconDisplayOptions
|
||||
---@return IconDisplay
|
||||
function gui.IconDisplay(options) end
|
||||
|
||||
---A "Line break" in the gui.
|
||||
function gui.br() end
|
||||
|
||||
|
Reference in New Issue
Block a user