mirror of
https://github.com/riuson/lcd-image-converter.git
synced 2026-03-04 07:14:12 +01:00
233 lines
6.6 KiB
C++
233 lines
6.6 KiB
C++
/*
|
|
* LCD Image Converter. Converts images and fonts for embedded applications.
|
|
* Copyright (C) 2019 riuson
|
|
* mailto: riuson@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, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* 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 <http://www.gnu.org/licenses/
|
|
*/
|
|
|
|
#include "canvasmodproxy.h"
|
|
#include "bitmaphelper.h"
|
|
#include "canvasmodinfo.h"
|
|
#include "imagesmodel.h"
|
|
#include <QColor>
|
|
|
|
namespace Data
|
|
{
|
|
namespace Models
|
|
{
|
|
|
|
CanvasModProxy::CanvasModProxy(QMap<QString, CanvasModInfo *> *canvasMods, QObject *parent)
|
|
: QSortFilterProxyModel(parent),
|
|
mCanvasMods(canvasMods)
|
|
{
|
|
}
|
|
|
|
QVariant CanvasModProxy::headerData(int section, Qt::Orientation orientation, int role) const
|
|
{
|
|
QVariant result = this->sourceModel()->headerData(section, orientation, role);
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
int sourceColumns = this->sourceModel()->columnCount();
|
|
|
|
if (orientation == Qt::Horizontal) {
|
|
if (section == sourceColumns + 0) {
|
|
result = tr("Original size");
|
|
} else if (section == sourceColumns + 1) {
|
|
result = tr("New size");
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
int CanvasModProxy::columnCount(const QModelIndex &parent) const
|
|
{
|
|
if (this->sourceModel() == nullptr) {
|
|
return 0;
|
|
}
|
|
|
|
int sourceColumns = this->sourceModel()->columnCount(parent);
|
|
return sourceColumns + 3;
|
|
}
|
|
|
|
QVariant CanvasModProxy::data(const QModelIndex &index, int role) const
|
|
{
|
|
QVariant result = this->sourceModel()->data(index, role);
|
|
|
|
if (!index.isValid()) {
|
|
return result;
|
|
}
|
|
|
|
int sourceColumns = this->sourceModel()->columnCount();
|
|
int columnIndex = index.column();
|
|
|
|
switch (role) {
|
|
case Qt::DecorationRole: {
|
|
if (columnIndex == 1) {
|
|
QImage imageSource = result.value<QImage>();
|
|
QColor backgroundColor = Parsing::Conversion::BitmapHelper::detectBackgroundColor(&imageSource);
|
|
|
|
QVariant varKey = this->sourceModel()->data(index, Data::Models::ImagesModel::KeyRole);
|
|
QString key = varKey.toString();
|
|
|
|
const Data::CanvasModInfo *mod = this->mCanvasMods->value(key);
|
|
qint16 left, top, right, bottom, offsetX, offsetY;
|
|
|
|
if (mod != nullptr) {
|
|
mod->resizeData(left, top, right, bottom);
|
|
mod->offsetData(offsetX, offsetY);
|
|
} else {
|
|
left = top = right = bottom = offsetX = offsetY = 0;
|
|
}
|
|
|
|
QImage imageScaled = Parsing::Conversion::BitmapHelper::crop(&imageSource, left, top, right, bottom, backgroundColor);
|
|
result = imageScaled;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case Qt::SizeHintRole: {
|
|
if (columnIndex == 1) {
|
|
QSize size = result.toSize();
|
|
|
|
QVariant varKey = this->sourceModel()->data(index, Data::Models::ImagesModel::KeyRole);
|
|
QString key = varKey.toString();
|
|
|
|
const Data::CanvasModInfo *mod = this->mCanvasMods->value(key);
|
|
qint16 left, top, right, bottom;
|
|
|
|
if (mod != nullptr) {
|
|
mod->resizeData(left, top, right, bottom);
|
|
} else {
|
|
left = top = right = bottom = 0;
|
|
}
|
|
|
|
size.rwidth() += left + right;
|
|
size.rheight() += top + bottom;
|
|
|
|
result = size;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case Qt::DisplayRole: {
|
|
if (columnIndex == sourceColumns + 0) {
|
|
QVariant varSize = this->sourceModel()->data(index, Data::Models::ImagesModel::ImageSizeRole);
|
|
QSize size = varSize.toSize();
|
|
result = QString("%1x%2").arg(size.width()).arg(size.height());
|
|
} else if (columnIndex == sourceColumns + 1) {
|
|
QVariant varSize = this->sourceModel()->data(index, Data::Models::ImagesModel::ImageSizeRole);
|
|
QSize size = varSize.toSize();
|
|
|
|
QVariant varKey = this->sourceModel()->data(index, Data::Models::ImagesModel::KeyRole);
|
|
QString key = varKey.toString();
|
|
|
|
const Data::CanvasModInfo *mod = this->mCanvasMods->value(key);
|
|
qint16 left, top, right, bottom;
|
|
|
|
if (mod != nullptr) {
|
|
mod->resizeData(left, top, right, bottom);
|
|
} else {
|
|
left = top = right = bottom = 0;
|
|
}
|
|
|
|
size.rwidth() += left + right;
|
|
size.rheight() += top + bottom;
|
|
|
|
result = QString("%1x%2").arg(size.width()).arg(size.height());
|
|
} else if (columnIndex == sourceColumns + 2) {
|
|
QVariant varKey = this->sourceModel()->data(index, Data::Models::ImagesModel::KeyRole);
|
|
QString key = varKey.toString();
|
|
|
|
const Data::CanvasModInfo *info = this->mCanvasMods->value(key);
|
|
qint16 left, top, right, bottom, offsetX, offsetY;
|
|
|
|
if (info != nullptr) {
|
|
info->resizeData(left, top, right, bottom);
|
|
info->offsetData(offsetX, offsetY);
|
|
} else {
|
|
left = top = right = bottom = offsetX = offsetY = 0;
|
|
}
|
|
|
|
result = QString("◄%1, ▲%2, ►%3, ▼%4, ↔%5, ↕%6")
|
|
.arg(left)
|
|
.arg(top)
|
|
.arg(right)
|
|
.arg(bottom)
|
|
.arg(offsetX)
|
|
.arg(offsetY);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
QModelIndex CanvasModProxy::index(int row, int column, const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return this->createIndex(row, column);
|
|
}
|
|
|
|
QModelIndex CanvasModProxy::parent(const QModelIndex &index) const
|
|
{
|
|
Q_UNUSED(index)
|
|
return QModelIndex();
|
|
}
|
|
|
|
QModelIndex CanvasModProxy::mapFromSource(const QModelIndex &sourceIndex) const
|
|
{
|
|
return this->index(sourceIndex.row(), sourceIndex.column(), sourceIndex.parent());
|
|
}
|
|
|
|
QModelIndex CanvasModProxy::mapToSource(const QModelIndex &proxyIndex) const
|
|
{
|
|
if (sourceModel() && proxyIndex.isValid()) {
|
|
return sourceModel()->index(proxyIndex.row(), proxyIndex.column(), proxyIndex.parent());
|
|
} else {
|
|
return QModelIndex();
|
|
}
|
|
}
|
|
|
|
const QSize CanvasModProxy::resized(const QSize &value, const CanvasModInfo &mod) const
|
|
{
|
|
QSize result = value;
|
|
qint16 left, top, right, bottom;
|
|
mod.resizeData(left, top, right, bottom);
|
|
result.rwidth() += left + right;
|
|
result.rheight() += top + bottom;
|
|
|
|
if (result.height() < 1) {
|
|
result.setHeight(1);
|
|
}
|
|
|
|
if (result.width() < 1) {
|
|
result.setWidth(1);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
} // namespace Models
|
|
} // namespace Data
|