From 7840c53effcb72b92ad83d62fcfe50dc372cde61 Mon Sep 17 00:00:00 2001 From: gxalpha Date: Sat, 25 Feb 2023 15:08:45 +0100 Subject: [PATCH] shared/qt: Add IconLabel widget --- shared/qt/icon-label/CMakeLists.txt | 11 ++++++ shared/qt/icon-label/icon-label.hpp | 56 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 shared/qt/icon-label/CMakeLists.txt create mode 100644 shared/qt/icon-label/icon-label.hpp diff --git a/shared/qt/icon-label/CMakeLists.txt b/shared/qt/icon-label/CMakeLists.txt new file mode 100644 index 000000000..ff16e3376 --- /dev/null +++ b/shared/qt/icon-label/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.22...3.25) + +find_package(Qt6 REQUIRED Core Widgets) + +add_library(qt-icon-label INTERFACE) +add_library(OBS::qt-icon-label ALIAS qt-icon-label) + +target_sources(qt-icon-label INTERFACE icon-label.hpp) +target_include_directories(qt-icon-label INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}") + +target_link_libraries(qt-icon-label INTERFACE Qt::Core Qt::Widgets) diff --git a/shared/qt/icon-label/icon-label.hpp b/shared/qt/icon-label/icon-label.hpp new file mode 100644 index 000000000..582a7c250 --- /dev/null +++ b/shared/qt/icon-label/icon-label.hpp @@ -0,0 +1,56 @@ +/****************************************************************************** + Copyright (C) 2024 by Sebastian Beckmann + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#pragma once + +#include +#include + +/** + * Widget to be used if a label is to be supplied a QIcon instead of a QPixmap, + * specifically so that qproperty-icon QSS styling (and as a result the OBS + * "themeID" property) works on it without having to first convert the icon to + * a fixed size PNG and then setting qproperty-pixmap in addition to the + * qproperty-icon statements. + */ +class IconLabel : public QLabel { + Q_OBJECT + Q_PROPERTY(QIcon icon READ icon WRITE setIcon) + Q_PROPERTY(int iconSize READ iconSize WRITE setIconSize) + +public: + inline IconLabel(QWidget *parent = nullptr) + : QLabel(parent), + m_icon(), + m_iconSize(16) + { + } + + inline QIcon icon() const { return m_icon; } + void setIcon(const QIcon &icon) + { + m_icon = icon; + QLabel::setPixmap(icon.pixmap(m_iconSize)); + } + + inline int iconSize() const { return m_iconSize; } + void setIconSize(int newSize) { m_iconSize = newSize; } + +private: + QIcon m_icon; + int m_iconSize; +};