Play-/Source/ui_qt/BootableModel.cpp

149 lines
3.7 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"
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-17 12:26:47 +00:00
int BootableModel::rowCount(const QModelIndex& parent) 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-17 12:26:47 +00:00
int BootableModel::columnCount(const QModelIndex& parent) 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-01-20 02:56:00 +00:00
auto bootable = m_bootables.at(static_cast<unsigned int>(pos));
return QVariant::fromValue(BootableCoverQVarient(bootable.discId, bootable.title));
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-01-20 02:51:16 +00:00
/* start of BootImageItemDelegate */
BootableCoverQVarient::BootableCoverQVarient(std::string key, std::string title)
2019-01-17 12:26:47 +00:00
: m_key(key)
, m_title(title)
2019-01-16 20:00:10 +00:00
{
}
2019-01-17 12:26:47 +00:00
void BootableCoverQVarient::paint(QPainter* painter, const QRect& rect, const QPalette& palette, int mode) 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);
}
2019-01-16 20:00:10 +00:00
painter->drawPixmap(rect.x() + 5, rect.y() + 5, pixmap);
painter->restore();
}
QSize BootableCoverQVarient::sizeHint() const
{
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;
}
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
{
2019-01-17 12:26:47 +00:00
if(index.data().canConvert<BootableCoverQVarient>())
2019-01-16 20:00:10 +00:00
{
BootableCoverQVarient bootablecover = qvariant_cast<BootableCoverQVarient>(index.data());
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
{
2019-01-17 12:26:47 +00:00
if(index.data().canConvert<BootableCoverQVarient>())
2019-01-16 20:00:10 +00:00
{
BootableCoverQVarient bootablecover = qvariant_cast<BootableCoverQVarient>(index.data());
return bootablecover.sizeHint();
}
else
{
return QStyledItemDelegate::sizeHint(option, index);
}
}