2022-08-29 07:01:48 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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-10-09 02:41:24 +05:30
|
|
|
|
2013-10-12 18:49:49 +05:30
|
|
|
#include <QMessageBox>
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
#include "InfoFrame.h"
|
|
|
|
#include "ui_InfoFrame.h"
|
2021-11-22 08:25:16 +05:30
|
|
|
|
|
|
|
#include "ui/dialogs/CustomMessageBox.h"
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
InfoFrame::InfoFrame(QWidget *parent) :
|
|
|
|
QFrame(parent),
|
|
|
|
ui(new Ui::InfoFrame)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
ui->descriptionLabel->setHidden(true);
|
|
|
|
ui->nameLabel->setHidden(true);
|
|
|
|
updateHiddenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
InfoFrame::~InfoFrame()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoFrame::updateWithMod(Mod const& m)
|
2013-10-09 02:15:48 +05:30
|
|
|
{
|
2022-08-09 10:23:50 +05:30
|
|
|
if (m.type() == ResourceType::FOLDER)
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
2013-10-09 02:55:56 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QString text = "";
|
|
|
|
QString name = "";
|
|
|
|
if (m.name().isEmpty())
|
2022-04-16 06:37:35 +05:30
|
|
|
name = m.internal_id();
|
2018-07-15 18:21:05 +05:30
|
|
|
else
|
|
|
|
name = m.name();
|
2013-10-09 07:40:56 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
if (m.homeurl().isEmpty())
|
|
|
|
text = name;
|
|
|
|
else
|
|
|
|
text = "<a href=\"" + m.homeurl() + "\">" + name + "</a>";
|
|
|
|
if (!m.authors().isEmpty())
|
2019-08-03 09:00:46 +05:30
|
|
|
text += " by " + m.authors().join(", ");
|
2013-10-09 02:15:48 +05:30
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
setName(text);
|
2013-10-09 02:15:48 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
if (m.description().isEmpty())
|
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
setDescription(QString());
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
setDescription(m.description());
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2022-08-29 17:44:15 +05:30
|
|
|
|
|
|
|
setImage();
|
2013-10-09 05:33:02 +05:30
|
|
|
}
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
void InfoFrame::updateWithResource(const Resource& resource)
|
2013-10-09 02:15:48 +05:30
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
setName(resource.name());
|
2022-08-29 17:44:15 +05:30
|
|
|
setImage();
|
2013-10-09 02:15:48 +05:30
|
|
|
}
|
|
|
|
|
2022-08-29 07:01:48 +05:30
|
|
|
// https://www.sportskeeda.com/minecraft-wiki/color-codes
|
|
|
|
static const QMap<QChar, QString> s_value_to_color = {
|
|
|
|
{'0', "#000000"}, {'1', "#0000AA"}, {'2', "#00AA00"}, {'3', "#00AAAA"}, {'4', "#AA0000"},
|
|
|
|
{'5', "#AA00AA"}, {'6', "#FFAA00"}, {'7', "#AAAAAA"}, {'8', "#555555"}, {'9', "#5555FF"},
|
|
|
|
{'a', "#55FF55"}, {'b', "#55FFFF"}, {'c', "#FF5555"}, {'d', "#FF55FF"}, {'e', "#FFFF55"},
|
|
|
|
{'f', "#FFFFFF"}
|
|
|
|
};
|
|
|
|
|
2022-08-31 02:36:13 +05:30
|
|
|
void InfoFrame::updateWithResourcePack(ResourcePack& resource_pack)
|
2022-08-29 07:01:48 +05:30
|
|
|
{
|
|
|
|
setName(resource_pack.name());
|
|
|
|
|
|
|
|
// We have to manually set the colors for use.
|
|
|
|
//
|
|
|
|
// A color is set using §x, with x = a hex number from 0 to f.
|
|
|
|
//
|
|
|
|
// We traverse the description and, when one of those is found, we create
|
|
|
|
// a span element with that color set.
|
|
|
|
//
|
|
|
|
// TODO: Make the same logic for font formatting too.
|
|
|
|
// TODO: Wrap links inside <a> tags
|
|
|
|
|
|
|
|
auto description = resource_pack.description();
|
|
|
|
|
|
|
|
QString description_parsed("<html>");
|
|
|
|
bool in_div = false;
|
|
|
|
|
|
|
|
auto desc_it = description.constBegin();
|
|
|
|
while (desc_it != description.constEnd()) {
|
|
|
|
if (*desc_it == u'§') {
|
|
|
|
if (in_div)
|
|
|
|
description_parsed += "</span>";
|
|
|
|
|
|
|
|
auto const& num = *(++desc_it);
|
|
|
|
description_parsed += QString("<span style=\"color: %1;\">").arg(s_value_to_color.constFind(num).value());
|
|
|
|
|
|
|
|
in_div = true;
|
|
|
|
|
|
|
|
desc_it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
description_parsed += *desc_it;
|
|
|
|
desc_it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_div)
|
|
|
|
description_parsed += "</span>";
|
|
|
|
description_parsed += "</html>";
|
|
|
|
|
|
|
|
description_parsed.replace("\n", "<br>");
|
|
|
|
|
|
|
|
setDescription(description_parsed);
|
2022-08-29 17:44:15 +05:30
|
|
|
setImage(resource_pack.image({64, 64}));
|
2022-08-29 07:01:48 +05:30
|
|
|
}
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
void InfoFrame::clear()
|
2013-10-09 02:15:48 +05:30
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
setName();
|
|
|
|
setDescription();
|
2022-08-29 17:44:15 +05:30
|
|
|
setImage();
|
2013-10-09 02:15:48 +05:30
|
|
|
}
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
void InfoFrame::updateHiddenState()
|
2016-01-02 08:31:00 +05:30
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
if(ui->descriptionLabel->isHidden() && ui->nameLabel->isHidden())
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
|
|
|
setHidden(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setHidden(false);
|
|
|
|
}
|
2016-01-02 08:31:00 +05:30
|
|
|
}
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
void InfoFrame::setName(QString text)
|
2013-10-09 02:15:48 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(text.isEmpty())
|
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
ui->nameLabel->setHidden(true);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
ui->nameLabel->setText(text);
|
|
|
|
ui->nameLabel->setHidden(false);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
updateHiddenState();
|
2013-10-09 02:15:48 +05:30
|
|
|
}
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
void InfoFrame::setDescription(QString text)
|
2013-10-09 02:15:48 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(text.isEmpty())
|
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
ui->descriptionLabel->setHidden(true);
|
2018-07-15 18:21:05 +05:30
|
|
|
updateHiddenState();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
ui->descriptionLabel->setHidden(false);
|
2018-07-15 18:21:05 +05:30
|
|
|
updateHiddenState();
|
|
|
|
}
|
2022-08-10 23:18:34 +05:30
|
|
|
ui->descriptionLabel->setToolTip("");
|
2018-07-15 18:21:05 +05:30
|
|
|
QString intermediatetext = text.trimmed();
|
|
|
|
bool prev(false);
|
|
|
|
QChar rem('\n');
|
|
|
|
QString finaltext;
|
|
|
|
finaltext.reserve(intermediatetext.size());
|
|
|
|
foreach(const QChar& c, intermediatetext)
|
|
|
|
{
|
|
|
|
if(c == rem && prev){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
prev = c == rem;
|
|
|
|
finaltext += c;
|
|
|
|
}
|
|
|
|
QString labeltext;
|
|
|
|
labeltext.reserve(300);
|
|
|
|
if(finaltext.length() > 290)
|
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
ui->descriptionLabel->setOpenExternalLinks(false);
|
|
|
|
ui->descriptionLabel->setTextFormat(Qt::TextFormat::RichText);
|
|
|
|
m_description = text;
|
2020-11-03 02:58:07 +05:30
|
|
|
// This allows injecting HTML here.
|
2018-07-15 18:21:05 +05:30
|
|
|
labeltext.append("<html><body>" + finaltext.left(287) + "<a href=\"#mod_desc\">...</a></body></html>");
|
2022-08-10 23:18:34 +05:30
|
|
|
QObject::connect(ui->descriptionLabel, &QLabel::linkActivated, this, &InfoFrame::descriptionEllipsisHandler);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-29 07:01:48 +05:30
|
|
|
ui->descriptionLabel->setTextFormat(Qt::TextFormat::AutoText);
|
2018-07-15 18:21:05 +05:30
|
|
|
labeltext.append(finaltext);
|
|
|
|
}
|
2022-08-10 23:18:34 +05:30
|
|
|
ui->descriptionLabel->setText(labeltext);
|
2013-10-09 02:15:48 +05:30
|
|
|
}
|
2015-11-13 05:20:38 +05:30
|
|
|
|
2022-08-29 17:44:15 +05:30
|
|
|
void InfoFrame::setImage(QPixmap img)
|
|
|
|
{
|
|
|
|
if (img.isNull()) {
|
|
|
|
ui->iconLabel->setHidden(true);
|
|
|
|
} else {
|
|
|
|
ui->iconLabel->setHidden(false);
|
|
|
|
ui->iconLabel->setPixmap(img);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
void InfoFrame::descriptionEllipsisHandler(QString link)
|
2013-10-12 18:49:49 +05:30
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
if(!m_current_box)
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
m_current_box = CustomMessageBox::selectable(this, "", m_description);
|
|
|
|
connect(m_current_box, &QMessageBox::finished, this, &InfoFrame::boxClosed);
|
|
|
|
m_current_box->show();
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
m_current_box->setText(m_description);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2015-11-13 05:20:38 +05:30
|
|
|
}
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
void InfoFrame::boxClosed(int result)
|
2015-11-13 05:20:38 +05:30
|
|
|
{
|
2022-08-10 23:18:34 +05:30
|
|
|
m_current_box = nullptr;
|
2013-10-12 18:49:49 +05:30
|
|
|
}
|