Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #20 from camrein/image_scale
Browse files Browse the repository at this point in the history
Image scale
  • Loading branch information
camrein authored Apr 23, 2017
2 parents a15d19f + d836dc6 commit ad06ab1
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 36 deletions.
40 changes: 32 additions & 8 deletions EzGraverUi/imagelabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "ezgraver.h"

ImageLabel::ImageLabel(QWidget* parent) : ClickLabel{parent}, _image{}, _flags{Qt::DiffuseDither},
_grayscale{false}, _layer{0}, _layerCount{3}, _keepAspectRatio{false} {}
_grayscale{false}, _layer{0}, _layerCount{3}, _keepAspectRatio{false}, _scaled{false}, _imageScale{1.0} {}

ImageLabel::~ImageLabel() {}

Expand Down Expand Up @@ -72,6 +72,26 @@ void ImageLabel::setKeepAspectRatio(bool const& keepAspectRatio) {
emit keepAspectRatioChanged(keepAspectRatio);
}

bool ImageLabel::scaled() const {
return _imageScale;
}

void ImageLabel::setScaled(bool const& scaled) {
_scaled = scaled;
updateDisplayedImage();
emit scaledChanged(scaled);
}

float ImageLabel::imageScale() const {
return _imageScale;
}

void ImageLabel::setImageScale(float const& imageScale) {
_imageScale = imageScale;
updateDisplayedImage();
emit imageScaleChanged(imageScale);
}

void ImageLabel::updateDisplayedImage() {
if(!imageLoaded()) {
return;
Expand All @@ -83,13 +103,17 @@ void ImageLabel::updateDisplayedImage() {
QPainter painter{&image};

// As at this time, the target image is quadratic, scaling according the larger dimension is sufficient.
auto scaled = _keepAspectRatio
? (_image.width() > _image.height() ? _image.scaledToWidth(image.width()) : _image.scaledToHeight(image.height()))
: _image.scaled(image.size());
auto position = _keepAspectRatio
? (_image.width() > _image.height() ? QPoint(0, (image.height() - scaled.height()) / 2) : QPoint((image.width() - scaled.width()) / 2, 0))
: QPoint(0, 0);
painter.drawImage(position, scaled);
if(_scaled) {
auto scaled = _image.scaled(_image.width() * _imageScale, _image.height() * _imageScale);
QPoint position{(image.width() - scaled.width()) / 2, (image.height() - scaled.height()) / 2};
painter.drawImage(position, scaled);
} else if(_keepAspectRatio) {
auto scaled = (_image.width() > _image.height() ? _image.scaledToWidth(image.width()) : _image.scaledToHeight(image.height()));
auto position = (_image.width() > _image.height() ? QPoint{0, (image.height() - scaled.height()) / 2} : QPoint{(image.width() - scaled.width()) / 2, 0});
painter.drawImage(position, scaled);
} else {
painter.drawImage(QPoint{0, 0}, _image.scaled(image.size()));
}

auto rendered = _grayscale
? QPixmap::fromImage(_createGrayscaleImage(image))
Expand Down
48 changes: 47 additions & 1 deletion EzGraverUi/imagelabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class ImageLabel : public ClickLabel {
Q_PROPERTY(int layer READ layer WRITE setLayer NOTIFY layerChanged)
Q_PROPERTY(int layerCount READ layerCount WRITE setLayerCount NOTIFY layerCountChanged)
Q_PROPERTY(bool keepAspectRatio READ keepAspectRatio WRITE setKeepAspectRatio NOTIFY keepAspectRatioChanged)
Q_PROPERTY(bool scaled READ scaled WRITE setScaled NOTIFY scaledChanged)
Q_PROPERTY(float imageScale READ imageScale WRITE setImageScale NOTIFY imageScaleChanged)
Q_PROPERTY(bool imageLoaded READ imageLoaded NOTIFY imageLoadedChanged)

public:
Expand Down Expand Up @@ -107,12 +109,40 @@ class ImageLabel : public ClickLabel {
bool keepAspectRatio() const;

/*!
* Sets the if the aspect ratio of the image should be kept.
* Sets the the aspect ratio of the image should be kept.
*
* \param keepAspectRatio \c true if the aspect ratio should be kept.
*/
void setKeepAspectRatio(bool const& keepAspectRatio);

/*!
* Gets if the image is being scaled.
*
* \return Returns \c true if the image is being scaled.
*/
bool scaled() const;

/*!
* Sets if the image is being scaled.
*
* \param scaled \c true if the iamge is being scaled.
*/
void setScaled(bool const& scaled);

/*!
* Gets the current image scale.
*
* \return The image scale. 1.0 equals to 100% (original size).
*/
float imageScale() const;

/*!
* Sets the image scale of the image.
*
* \param imageScale The image sacle. 1.0 equals to 100% (origina size).
*/
void setImageScale(float const& imageScale);

/*!
* Gets if an image has been loaded.
*
Expand Down Expand Up @@ -170,6 +200,20 @@ class ImageLabel : public ClickLabel {
*/
void keepAspectRatioChanged(bool const& keepAspectRatio);

/*!
* Fired as soon as the image is being scaled.
*
* \param scaled \c true if the image is being scaled.
*/
void scaledChanged(float const& imageScale);

/*!
* Fired as soon as the image scale changed.
*
* \param imageScale The current image scale. 1.0 equals to 100%.
*/
void imageScaleChanged(float const& imageScale);

/*!
* Fired as soon as an image has been loaded.
*
Expand All @@ -183,6 +227,8 @@ class ImageLabel : public ClickLabel {
int _layer;
int _layerCount;
bool _keepAspectRatio;
float _scaled;
float _imageScale;

void updateDisplayedImage();
QImage _createGrayscaleImage(QImage const& original) const;
Expand Down
8 changes: 8 additions & 0 deletions EzGraverUi/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ void MainWindow::_initBindings() {
connect(_ui->image, &ImageLabel::imageLoadedChanged, uploadEnabled);
connect(_ui->selectedLayer, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), uploadEnabled);
connect(_ui->layered, &QCheckBox::toggled, uploadEnabled);

connect(_ui->keepAspectRatio, &QCheckBox::toggled, _ui->image, &ImageLabel::setKeepAspectRatio);

connect(_ui->scaled, &QCheckBox::toggled, _ui->imageScale, &QSlider::setEnabled);
connect(_ui->scaled, &QCheckBox::toggled, _ui->resetImageScale, &QSlider::setEnabled);
connect(_ui->scaled, &QCheckBox::toggled, _ui->image, &ImageLabel::setScaled);
connect(_ui->imageScale, &QSlider::valueChanged, [this](int const& v) { _ui->imageScaleLabel->setText(QString::number(v)); });
connect(_ui->imageScale, &QSlider::valueChanged, [this](int const& v) { _ui->image->setImageScale(v / 100.0); });
connect(_ui->resetImageScale, &QPushButton::clicked, [this] { _ui->imageScale->setValue(100); });
}

void MainWindow::_initConversionFlags() {
Expand Down
100 changes: 73 additions & 27 deletions EzGraverUi/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
</item>
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Burn time</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_5">
Expand Down Expand Up @@ -114,10 +114,25 @@
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<item row="2" column="3">
<widget class="QLabel" name="burnTimeLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>25</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Conversion flags</string>
<string>60</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
Expand All @@ -131,7 +146,14 @@
</property>
</widget>
</item>
<item row="2" column="0">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Conversion flags</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="layered">
<property name="text">
<string>Layered</string>
Expand All @@ -145,50 +167,74 @@
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QLabel" name="burnTimeLabel">
<item row="2" column="1" colspan="2">
<widget class="QSlider" name="burnTime">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>25</width>
<height>0</height>
</size>
<property name="minimum">
<number>1</number>
</property>
<property name="text">
<string>60</string>
<property name="maximum">
<number>240</number>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
<property name="value">
<number>60</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QSlider" name="burnTime">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<item row="1" column="1">
<widget class="QSlider" name="imageScale">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>240</number>
<number>200</number>
</property>
<property name="value">
<number>60</number>
<number>100</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="resetImageScale">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Reset</string>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QLabel" name="imageScaleLabel">
<property name="text">
<string>100</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="scaled">
<property name="text">
<string>Scaled</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down

0 comments on commit ad06ab1

Please sign in to comment.