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"
|
|
|
|
|
2019-05-19 16:16:24 +01:00
|
|
|
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));
|
2022-02-19 00:40:29 +00:00
|
|
|
return QVariant::fromValue(BootableCoverQVariant(bootable.discId, bootable.title, bootable.path.string(), 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();
|
|
|
|
}
|
|
|
|
|
2019-05-19 16:16:24 +01:00
|
|
|
void BootableModel::SetWidth(int width)
|
|
|
|
{
|
|
|
|
g_viewwidth = width;
|
|
|
|
}
|
|
|
|
|
2019-01-20 02:51:16 +00:00
|
|
|
/* start of BootImageItemDelegate */
|
2022-02-19 00:40:29 +00:00
|
|
|
BootableCoverQVariant::BootableCoverQVariant(std::string key, std::string title, std::string path, BootablesDb::BootableStateList states)
|
2019-01-17 12:26:47 +00:00
|
|
|
: m_key(key)
|
2019-01-20 00:21:21 +00:00
|
|
|
, m_title(title)
|
2022-01-15 00:17:41 +00:00
|
|
|
, m_path(path)
|
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());
|
2019-01-20 00:21:21 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-05-19 16:16:24 +01:00
|
|
|
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
|
2019-05-19 16:16:24 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2019-05-19 16:16:24 +01: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);
|
2019-05-19 16:16:24 +01:00
|
|
|
|
|
|
|
size.rwidth() += GetPadding();
|
2019-01-16 20:00:10 +00:00
|
|
|
}
|
|
|
|
return size;
|
2022-01-15 00:17:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 16:47:28 +00:00
|
|
|
std::string BootableCoverQVariant::GetKey()
|
2022-01-15 00:17:41 +00:00
|
|
|
{
|
|
|
|
return m_key;
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:47:28 +00:00
|
|
|
std::string BootableCoverQVariant::GetTitle()
|
2022-01-15 00:17:41 +00:00
|
|
|
{
|
|
|
|
return m_title;
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:47:28 +00:00
|
|
|
std::string BootableCoverQVariant::GetPath()
|
2022-01-15 00:17:41 +00:00
|
|
|
{
|
|
|
|
return m_path;
|
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);
|
|
|
|
}
|
|
|
|
}
|