Play-/Source/ui_qt/BootableModel.cpp

217 lines
5.2 KiB
C++
Raw Normal View History

2019-01-16 20:00:10 +00:00
#include <QPainter>
#include <QPixmap>
#include "ui_shared/BootablesProcesses.h"
#include "http/HttpClientFactory.h"
#include "BootableModel.h"
#include "CoverUtils.h"
int g_viewwidth;
2019-01-17 21:52:23 +00:00
BootableModel::BootableModel(QObject* parent, std::vector<BootablesDb::Bootable>& bootables)
2019-01-16 20:00:10 +00:00
: QAbstractTableModel(parent)
2019-01-17 21:52:23 +00:00
, m_bootables(bootables)
2019-01-16 20:00:10 +00:00
{
}
2019-01-20 02:58:13 +00:00
int BootableModel::rowCount(const QModelIndex&) const
2019-01-16 20:00:10 +00:00
{
2019-01-20 02:56:00 +00:00
return static_cast<int>(m_bootables.size());
2019-01-16 20:00:10 +00:00
}
2019-01-20 02:58:13 +00:00
int BootableModel::columnCount(const QModelIndex&) const
2019-01-16 20:00:10 +00:00
{
return 1;
}
2019-01-17 12:26:47 +00:00
QVariant BootableModel::data(const QModelIndex& index, int role) const
2019-01-16 20:00:10 +00:00
{
if(role == Qt::DisplayRole)
{
int pos = index.row() + index.column();
2019-03-30 00:34:26 +00:00
auto bootable = m_bootables.at(static_cast<unsigned int>(pos));
return QVariant::fromValue(BootableCoverQVariant(bootable.discId, bootable.title, bootable.path, bootable.states));
2019-01-16 20:00:10 +00:00
}
return QVariant();
}
QSize BootableModel::SizeHint()
{
static QSize size;
if(size.isEmpty())
{
QPixmap pixmap = CoverUtils::find("PH");
size = pixmap.size();
size.setHeight(size.height() + 10);
size.setWidth(size.width() + 10);
}
return size;
}
BootablesDb::Bootable BootableModel::GetBootable(const QModelIndex& index)
{
2019-01-20 02:56:00 +00:00
int pos = index.row() + index.column();
return m_bootables.at(static_cast<unsigned int>(pos));
2019-01-16 20:00:10 +00:00
}
void BootableModel::removeItem(const QModelIndex& index)
{
2019-01-20 02:56:00 +00:00
int pos = index.row() + index.column();
2019-01-16 20:00:10 +00:00
emit QAbstractTableModel::beginRemoveRows(QModelIndex(), pos, pos);
2019-01-17 21:50:25 +00:00
m_bootables.erase(m_bootables.begin() + pos);
2019-01-16 20:00:10 +00:00
emit QAbstractTableModel::endRemoveRows();
}
void BootableModel::SetWidth(int width)
{
g_viewwidth = width;
}
2019-01-20 02:51:16 +00:00
/* start of BootImageItemDelegate */
BootableCoverQVariant::BootableCoverQVariant(std::string key, std::string title, fs::path path, BootablesDb::BootableStateList states)
2019-01-17 12:26:47 +00:00
: m_key(key)
, m_title(title)
, m_path(path)
2022-02-19 00:03:53 +00:00
, m_states(states)
2019-01-16 20:00:10 +00:00
{
2022-02-19 00:40:29 +00:00
for(auto state : states)
{
if(state.name.find("state") != std::string::npos)
{
m_statusColor = "#" + state.color;
}
}
2019-01-16 20:00:10 +00:00
}
2022-01-18 16:47:28 +00:00
void BootableCoverQVariant::paint(QPainter* painter, const QRect& rect, const QPalette&, int) const
2019-01-16 20:00:10 +00:00
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(Qt::NoPen);
QPixmap pixmap = CoverUtils::find(m_key.c_str());
if(pixmap.isNull())
{
pixmap = CoverUtils::find("PH");
QPainter painter(&pixmap);
painter.setFont(QFont("Arial"));
QTextOption opts(Qt::AlignCenter);
opts.setWrapMode(QTextOption::WordWrap);
QRect pix_rect = pixmap.rect();
pix_rect.setTop(pixmap.height() * 70 / 100);
std::string text;
if(!m_key.empty())
{
text = m_key + "\n";
}
text += m_title;
painter.drawText(pix_rect, text.c_str(), opts);
}
2022-02-19 00:40:29 +00:00
if(!m_statusColor.empty())
{
QPainter painter(&pixmap);
auto radius = pixmap.height() - (pixmap.height() * 92.5 / 100);
QRect pix_rect = QRect(pixmap.width() - radius - 5, pixmap.height() - radius - 5, radius, radius);
QColor color(m_statusColor.c_str());
Qt::BrushStyle style = Qt::SolidPattern;
QBrush brush(color, style);
painter.setBrush(brush);
painter.drawEllipse(pix_rect);
}
painter->drawPixmap(rect.x() + 5 + (GetPadding() / 2), rect.y() + 5, pixmap);
2019-01-16 20:00:10 +00:00
painter->restore();
}
2022-01-18 16:47:28 +00:00
int BootableCoverQVariant::GetPadding() const
{
QPixmap pixmap = CoverUtils::find("PH");
int cover_width = pixmap.size().width() + 10;
int cover_count = (g_viewwidth / cover_width);
int reminder = (g_viewwidth % cover_width);
if(reminder > 0 && cover_count > 0)
return reminder / cover_count;
return 0;
}
2022-01-18 16:47:28 +00:00
QSize BootableCoverQVariant::sizeHint() const
2019-01-16 20:00:10 +00:00
{
QSize size;
2019-01-16 20:00:10 +00:00
if(size.isEmpty())
{
QPixmap pixmap = CoverUtils::find("PH");
size = pixmap.size();
size.setHeight(size.height() + 10);
size.setWidth(size.width() + 10);
size.rwidth() += GetPadding();
2019-01-16 20:00:10 +00:00
}
return size;
}
std::string BootableCoverQVariant::GetKey() const
{
return m_key;
}
std::string BootableCoverQVariant::GetTitle() const
{
return m_title;
}
fs::path BootableCoverQVariant::GetPath() const
{
return m_path;
2022-02-19 00:03:53 +00:00
}
bool BootableCoverQVariant::HasState(std::string state)
{
auto itr = std::find_if(std::begin(m_states), std::end(m_states), [state](BootablesDb::BootableState bootState) {
return bootState.name == state;
});
return itr != std::end(m_states);
2019-01-16 20:00:10 +00:00
}
2019-01-20 02:51:16 +00:00
/* start of BootImageItemDelegate */
BootImageItemDelegate::BootImageItemDelegate(QWidget* parent)
: QStyledItemDelegate(parent)
{
}
2019-01-17 12:26:47 +00:00
void BootImageItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
2019-01-16 20:00:10 +00:00
{
2022-01-18 16:47:28 +00:00
if(index.data().canConvert<BootableCoverQVariant>())
2019-01-16 20:00:10 +00:00
{
2022-01-18 16:47:28 +00:00
BootableCoverQVariant bootablecover = qvariant_cast<BootableCoverQVariant>(index.data());
2019-01-16 20:00:10 +00:00
2019-01-17 12:26:47 +00:00
if(option.state & QStyle::State_Selected)
2019-01-16 20:00:10 +00:00
painter->fillRect(option.rect, option.palette.highlight());
bootablecover.paint(painter, option.rect, option.palette, 0);
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
2019-01-17 12:26:47 +00:00
QSize BootImageItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
2019-01-16 20:00:10 +00:00
{
2022-01-18 16:47:28 +00:00
if(index.data().canConvert<BootableCoverQVariant>())
2019-01-16 20:00:10 +00:00
{
2022-01-18 16:47:28 +00:00
BootableCoverQVariant bootablecover = qvariant_cast<BootableCoverQVariant>(index.data());
2019-01-16 20:00:10 +00:00
return bootablecover.sizeHint();
}
else
{
return QStyledItemDelegate::sizeHint(option, index);
}
}