pollymc/launcher/ui/widgets/InfoFrame.cpp

173 lines
3.9 KiB
C++
Raw Normal View History

2021-01-18 12:58:54 +05:30
/* Copyright 2013-2021 MultiMC Contributors
2013-10-09 02:41:24 +05:30
*
* 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-12 18:49:49 +05:30
#include <QMessageBox>
#include "InfoFrame.h"
#include "ui_InfoFrame.h"
#include "ui/dialogs/CustomMessageBox.h"
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)
{
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())
name = m.internal_id();
2018-07-15 18:21:05 +05:30
else
name = m.name();
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())
text += " by " + m.authors().join(", ");
setName(text);
2018-07-15 18:21:05 +05:30
if (m.description().isEmpty())
{
setDescription(QString());
2018-07-15 18:21:05 +05:30
}
else
{
setDescription(m.description());
2018-07-15 18:21:05 +05:30
}
}
void InfoFrame::updateWithResource(const Resource& resource)
{
setName(resource.name());
}
void InfoFrame::clear()
{
setName();
setDescription();
}
void InfoFrame::updateHiddenState()
{
if(ui->descriptionLabel->isHidden() && ui->nameLabel->isHidden())
2018-07-15 18:21:05 +05:30
{
setHidden(true);
}
else
{
setHidden(false);
}
}
void InfoFrame::setName(QString text)
{
2018-07-15 18:21:05 +05:30
if(text.isEmpty())
{
ui->nameLabel->setHidden(true);
2018-07-15 18:21:05 +05:30
}
else
{
ui->nameLabel->setText(text);
ui->nameLabel->setHidden(false);
2018-07-15 18:21:05 +05:30
}
updateHiddenState();
}
void InfoFrame::setDescription(QString text)
{
2018-07-15 18:21:05 +05:30
if(text.isEmpty())
{
ui->descriptionLabel->setHidden(true);
2018-07-15 18:21:05 +05:30
updateHiddenState();
return;
}
else
{
ui->descriptionLabel->setHidden(false);
2018-07-15 18:21:05 +05:30
updateHiddenState();
}
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)
{
ui->descriptionLabel->setOpenExternalLinks(false);
ui->descriptionLabel->setTextFormat(Qt::TextFormat::RichText);
m_description = text;
// 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>");
QObject::connect(ui->descriptionLabel, &QLabel::linkActivated, this, &InfoFrame::descriptionEllipsisHandler);
2018-07-15 18:21:05 +05:30
}
else
{
ui->descriptionLabel->setTextFormat(Qt::TextFormat::PlainText);
2018-07-15 18:21:05 +05:30
labeltext.append(finaltext);
}
ui->descriptionLabel->setText(labeltext);
}
void InfoFrame::descriptionEllipsisHandler(QString link)
2013-10-12 18:49:49 +05:30
{
if(!m_current_box)
2018-07-15 18:21:05 +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
{
m_current_box->setText(m_description);
2018-07-15 18:21:05 +05:30
}
}
void InfoFrame::boxClosed(int result)
{
m_current_box = nullptr;
2013-10-12 18:49:49 +05:30
}