Files
qt6-migration-pipeline/Lts/Common/LtsWidgets/tests/lts_picture_editing.cpp
T

222 lines
6.9 KiB
C++
Executable File

#include "lts_picture_editing.h"
#include "ui_lts_picture_editing.h"
#include "lts_picture_view.h"
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>
#include <QImage>
#include <QGraphicsPixmapItem>
#include <QDebug>
#include <QTimer>
LtsPictureEditing::LtsPictureEditing(QWidget *parent)
: QMainWindow(parent)
, m_gvPicture(nullptr)
, m_ui(new Ui::PictureEditing)
{
m_ui->setupUi(this);
connect(m_ui->actionOpen, &QAction::triggered, this, &LtsPictureEditing::onOpenImage);
connect(m_ui->actionSave, &QAction::triggered, this, &LtsPictureEditing::onSavePicture);
connect(m_ui->actionQuit, &QAction::triggered, this, &LtsPictureEditing::close);
connect(m_ui->pbSetSize, &QPushButton::clicked, this, &LtsPictureEditing::onSetSize);
connect(m_ui->sbHeight, QOverload<int>::of(&QSpinBox::valueChanged), this, &LtsPictureEditing::onRatioChanged);
connect(m_ui->sbWidth, QOverload<int>::of(&QSpinBox::valueChanged), this, &LtsPictureEditing::onRatioChanged);
connect(m_ui->chbRatio, &QPushButton::clicked, this, &LtsPictureEditing::onRatioClicked);
connect(m_ui->sbXMin, QOverload<int>::of(&QSpinBox::valueChanged), this, &LtsPictureEditing::onLeftChanged);
connect(m_ui->sbXMax, QOverload<int>::of(&QSpinBox::valueChanged), this, &LtsPictureEditing::onRightChanged);
connect(m_ui->sbYMin, QOverload<int>::of(&QSpinBox::valueChanged), this, &LtsPictureEditing::onTopChanged);
connect(m_ui->sbYMax, QOverload<int>::of(&QSpinBox::valueChanged), this, &LtsPictureEditing::onBottomChanged);
addViewToFrame();
connect(this, &LtsPictureEditing::leftChanged, m_gvPicture, &LtsWidgets::LtsPictureView::leftChanged);
connect(this, &LtsPictureEditing::rightChanged, m_gvPicture, &LtsWidgets::LtsPictureView::rightChanged);
connect(this, &LtsPictureEditing::topChanged, m_gvPicture, &LtsWidgets::LtsPictureView::topChanged);
connect(this, &LtsPictureEditing::bottomChanged, m_gvPicture, &LtsWidgets::LtsPictureView::bottomChanged);
connect(this, &LtsPictureEditing::setAspectRatio, m_gvPicture, &LtsWidgets::LtsPictureView::setAspectRatio);
connect(this, &LtsPictureEditing::ratioClicked, m_gvPicture, &LtsWidgets::LtsPictureView::ratioClicked);
connect(m_gvPicture, &LtsWidgets::LtsPictureView::mouseMoved, this, &LtsPictureEditing::setSpinboxValues);
connect(m_gvPicture, &LtsWidgets::LtsPictureView::setLimits, this, &LtsPictureEditing::setSpinboxLimits);
connect(this, &LtsPictureEditing::setPicture, m_gvPicture, &LtsWidgets::LtsPictureView::setPicture);
onRatioChanged();
}
LtsPictureEditing::~LtsPictureEditing()
{
delete m_ui;
}
void LtsPictureEditing::addViewToFrame()
{
QHBoxLayout *layout(new QHBoxLayout(m_ui->frPicFrame));
m_gvPicture = new LtsWidgets::LtsPictureView(m_ui->frPicFrame);
layout->addWidget(m_gvPicture);
m_gvPicture->setEnabled(false);
}
void LtsPictureEditing::onOpenImage()
{
const QStringList filter ({
tr("Images (*.jpg *.jpeg *.png)")
});
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
QStringLiteral("../picture_editing"),
filter.join(QStringLiteral(";;")));
if (fileName.isEmpty())
return;
QFileInfo fi(fileName);
if (!fi.isReadable())
{
QMessageBox::warning(this, tr("Error!"), tr("An error occurred while loading the file.\n"
"The loaded file is not readable."));
return;
}
QImage pic;
pic.load(fileName);
if (pic.isNull())
{
QMessageBox::warning(this, tr("Error!"), tr("An error occurred while loading the file.\n"
"The loaded file is no picture file.\n"
"Please load *.jpg, *.jpeg, *.png files only."));
return;
}
m_image = pic;
emit setPicture(pic, QPoint(0, 0), true);
m_gvPicture->setEnabled(true);
m_gvPicture->newRubberBand();
}
void LtsPictureEditing::onSetSize()
{
if (m_image.isNull())
return;
m_gvPicture->hideRubberband();
int xMin = m_ui->sbXMin->value() - 1;
int yMin = m_ui->sbYMin->value() - 1;
int xMax = m_ui->sbXMax->value() - 1;
int yMax = m_ui->sbYMax->value() - 1;
if (xMax < xMin || yMax < yMin || xMax == xMin || yMax == yMin)
{
clear();
return;
}
QRect picRect(xMin,
yMin,
xMax - xMin,
yMax - yMin);
QImage newPic = m_image.copy(picRect);
emit setPicture(newPic, QPoint(xMin, yMin), false);
}
void LtsPictureEditing::onSavePicture()
{
if (m_gvPicture->getImage()->pixmap().isNull())
{
QMessageBox::warning(this, tr("Error!"), tr("An error occurred while saving the picture\n"
"Please load a picture before saving it."));
return;
}
else
{
QString filename = QFileDialog::getSaveFileName(this, tr("Open Image"), QLatin1String("../picture_editing"),
tr("Images (*.jpg *.jpeg *.png)"));
if (filename.isEmpty())
{
QMessageBox::warning(this, tr("Error!"), tr("An error occurred while saving the picture.\n"
"Please choose a valid path to save the picture."));
return;
}
m_gvPicture->getImage()->pixmap().save(filename, "PNG");
}
}
void LtsPictureEditing::setSpinboxLimits(const QSize &picSize, bool setValues)
{
m_ui->sbXMin->setMaximum(picSize.width());
m_ui->sbXMax->setMaximum(picSize.width());
m_ui->sbYMin->setMaximum(picSize.height());
m_ui->sbYMax->setMaximum(picSize.height());
if (setValues)
{
m_ui->sbXMin->setValue(0);
m_ui->sbYMin->setValue(0);
m_ui->sbXMax->setValue(picSize.width() - 1);
m_ui->sbYMax->setValue(picSize.height() - 1);
}
}
void LtsPictureEditing::setSpinboxValues(const QRect &picRect)
{
m_ui->sbXMin->setValue(picRect.left());
m_ui->sbYMin->setValue(picRect.top());
m_ui->sbXMax->setValue(picRect.right());
m_ui->sbYMax->setValue(picRect.bottom());
}
void LtsPictureEditing::onRatioChanged()
{
double ratio = double(m_ui->sbWidth->value()) / double(m_ui->sbHeight->value());
emit setAspectRatio(ratio);
}
void LtsPictureEditing::onRatioClicked()
{
if (m_ui->chbRatio->isChecked())
{
m_ui->sbHeight->setEnabled(true);
m_ui->sbWidth->setEnabled(true);
emit ratioClicked(true);
}
else
{
m_ui->sbHeight->setEnabled(false);
m_ui->sbWidth->setEnabled(false);
emit ratioClicked(false);
}
}
void LtsPictureEditing::onLeftChanged(int left)
{
emit leftChanged(left);
}
void LtsPictureEditing::onRightChanged(int right)
{
emit rightChanged(right);
}
void LtsPictureEditing::onTopChanged(int top)
{
emit topChanged(top);
}
void LtsPictureEditing::onBottomChanged(int bottom)
{
emit bottomChanged(bottom);
}
void LtsPictureEditing::clear()
{
emit setPicture(QImage(), QPoint(), false);
}