2022-05-26 22:18:54 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2023-08-04 18:41:47 +01:00
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-05-26 22:18:54 +01:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
*
|
|
|
|
* 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, version 3.
|
|
|
|
*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
2013-11-04 01:53:05 +00:00
|
|
|
*
|
2022-05-26 22:18:54 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-11-04 01:53:05 +00:00
|
|
|
*
|
2022-05-26 22:18:54 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-11-04 01:53:05 +00:00
|
|
|
*
|
2022-05-26 22:18:54 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-11-04 01:53:05 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-25 21:48:41 +01:00
|
|
|
#include "LabeledToolButton.h"
|
|
|
|
#include <QApplication>
|
2017-09-28 01:47:54 +01:00
|
|
|
#include <QDebug>
|
2023-08-14 17:16:53 +01:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QResizeEvent>
|
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QVBoxLayout>
|
2013-08-25 21:48:41 +01:00
|
|
|
|
|
|
|
/*
|
2023-08-14 17:16:53 +01:00
|
|
|
*
|
2013-08-25 21:48:41 +01:00
|
|
|
* Tool Button with a label on it, instead of the normal text rendering
|
2023-08-14 17:16:53 +01:00
|
|
|
*
|
2013-08-25 21:48:41 +01:00
|
|
|
*/
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
LabeledToolButton::LabeledToolButton(QWidget* parent) : QToolButton(parent), m_label(new QLabel(this))
|
2013-08-25 21:48:41 +01:00
|
|
|
{
|
2023-08-14 17:16:53 +01:00
|
|
|
// QToolButton::setText(" ");
|
2013-08-25 21:48:41 +01:00
|
|
|
m_label->setWordWrap(true);
|
|
|
|
m_label->setMouseTracking(false);
|
|
|
|
m_label->setAlignment(Qt::AlignCenter);
|
2013-10-29 12:47:14 +00:00
|
|
|
m_label->setTextInteractionFlags(Qt::NoTextInteraction);
|
2013-08-25 21:48:41 +01:00
|
|
|
// somehow, this makes word wrap work in the QLabel. yay.
|
2023-08-14 17:16:53 +01:00
|
|
|
// m_label->setMinimumWidth(100);
|
2013-08-25 21:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString LabeledToolButton::text() const
|
|
|
|
{
|
|
|
|
return m_label->text();
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void LabeledToolButton::setText(const QString& text)
|
2013-08-25 21:48:41 +01:00
|
|
|
{
|
|
|
|
m_label->setText(text);
|
|
|
|
}
|
|
|
|
|
2017-09-28 01:47:54 +01:00
|
|
|
void LabeledToolButton::setIcon(QIcon icon)
|
|
|
|
{
|
|
|
|
m_icon = icon;
|
|
|
|
resetIcon();
|
|
|
|
}
|
|
|
|
|
2013-08-25 21:48:41 +01:00
|
|
|
/*!
|
|
|
|
\reimp
|
|
|
|
*/
|
|
|
|
QSize LabeledToolButton::sizeHint() const
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Q_D(const QToolButton);
|
|
|
|
if (d->sizeHint.isValid())
|
|
|
|
return d->sizeHint;
|
|
|
|
*/
|
|
|
|
ensurePolished();
|
|
|
|
|
|
|
|
int w = 0, h = 0;
|
|
|
|
QStyleOptionToolButton opt;
|
|
|
|
initStyleOption(&opt);
|
2023-08-14 17:16:53 +01:00
|
|
|
QSize sz = m_label->sizeHint();
|
2013-08-25 21:48:41 +01:00
|
|
|
w = sz.width();
|
|
|
|
h = sz.height();
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height
|
2013-08-25 21:48:41 +01:00
|
|
|
if (popupMode() == MenuButtonPopup)
|
|
|
|
w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
|
2023-08-14 17:16:53 +01:00
|
|
|
|
2022-05-02 18:10:45 +01:00
|
|
|
return style()->sizeFromContents(QStyle::CT_ToolButton, &opt, QSize(w, h), this);
|
2013-08-25 21:48:41 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void LabeledToolButton::resizeEvent(QResizeEvent* event)
|
2013-08-25 21:48:41 +01:00
|
|
|
{
|
2023-08-14 17:16:53 +01:00
|
|
|
m_label->setGeometry(QRect(4, 4, width() - 8, height() - 8));
|
|
|
|
if (!m_icon.isNull()) {
|
2017-09-28 01:47:54 +01:00
|
|
|
resetIcon();
|
|
|
|
}
|
2013-08-25 21:48:41 +01:00
|
|
|
QWidget::resizeEvent(event);
|
2013-10-29 12:40:09 +00:00
|
|
|
}
|
2017-09-28 01:47:54 +01:00
|
|
|
|
|
|
|
void LabeledToolButton::resetIcon()
|
|
|
|
{
|
2017-11-14 00:03:32 +00:00
|
|
|
auto iconSz = m_icon.actualSize(QSize(160, 80));
|
|
|
|
float w = iconSz.width();
|
|
|
|
float h = iconSz.height();
|
2023-08-14 17:16:53 +01:00
|
|
|
float ar = w / h;
|
2017-11-14 00:03:32 +00:00
|
|
|
// FIXME: hardcoded max size of 160x80
|
|
|
|
int newW = 80 * ar;
|
2023-08-14 17:16:53 +01:00
|
|
|
if (newW > 160)
|
2017-11-14 00:03:32 +00:00
|
|
|
newW = 160;
|
2023-08-14 17:16:53 +01:00
|
|
|
QSize newSz(newW, 80);
|
2017-11-14 00:03:32 +00:00
|
|
|
auto pixmap = m_icon.pixmap(newSz);
|
|
|
|
m_label->setPixmap(pixmap);
|
|
|
|
m_label->setMinimumHeight(80);
|
2023-08-14 17:16:53 +01:00
|
|
|
m_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
|
2017-09-28 01:47:54 +01:00
|
|
|
}
|