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 #7 from camrein/image_conversion_enhancements
Browse files Browse the repository at this point in the history
Image conversion enhancements
  • Loading branch information
camrein authored Aug 30, 2016
2 parents a57cd36 + 71fdeb4 commit fb145b9
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 55 deletions.
3 changes: 0 additions & 3 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ build:
- cp /mingw$$arch/bin/libicudt*.dll dependencies/
- cp /mingw$$arch/bin/libicuin*.dll dependencies/
- cp /mingw$$arch/bin/libicuuc*.dll dependencies/
- ls -al dependencies/
- for file in dependencies/lib* ; do mv "$file" "${file/lib/}" ; done
- ls -al dependencies/
- ls -al $PWD/dependencies/
- PATH=$PATH:$PWD/dependencies
# Deployment
- mkdir deploy
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ script:
- qmake -v
- qmake -config release EzGraver.pro
- make
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ls -alR ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then sudo make install ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then macdeployqt EzGraverUi/EzGraverUi.app -dmg ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then mv EzGraverUi/EzGraverUi.dmg EzGraverUi/EzGraver_osx.dmg; fi
Expand Down
6 changes: 4 additions & 2 deletions EzGraverUi/EzGraverUi.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ TEMPLATE = app

SOURCES += main.cpp\
mainwindow.cpp \
clicklabel.cpp
clicklabel.cpp \
imagelabel.cpp

HEADERS += mainwindow.h \
clicklabel.h
clicklabel.h \
imagelabel.h

FORMS += mainwindow.ui

Expand Down
2 changes: 1 addition & 1 deletion EzGraverUi/clicklabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <QMouseEvent>

ClickLabel::ClickLabel(QWidget* parent) : QLabel(parent) {}
ClickLabel::ClickLabel(QWidget* parent) : QLabel{parent} {}
ClickLabel::~ClickLabel() {}

void ClickLabel::mouseReleaseEvent(QMouseEvent*) {
Expand Down
4 changes: 4 additions & 0 deletions EzGraverUi/clicklabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class ClickLabel : public QLabel {
* \param parent The parent of the label.
*/
explicit ClickLabel(QWidget* parent=NULL);

/*!
* Frees all required resources upon deconstruction.
*/
virtual ~ClickLabel();

signals:
Expand Down
45 changes: 45 additions & 0 deletions EzGraverUi/imagelabel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "imagelabel.h"

#include <QPainter>

ImageLabel::ImageLabel(QWidget* parent) : ClickLabel{parent}, _image{}, _flags{Qt::DiffuseDither} {}
ImageLabel::~ImageLabel() {}

QImage ImageLabel::image() const {
return _image;
}

void ImageLabel::setImage(QImage const& image) {
_image = image;
updateDisplayedImage();
emit imageLoadedChanged(true);
emit imageChanged(image);
}

Qt::ImageConversionFlags ImageLabel::conversionFlags() const {
return _flags;
}

void ImageLabel::setConversionFlags(Qt::ImageConversionFlags const& flags) {
_flags = flags;
updateDisplayedImage();
emit conversionFlagsChanged(flags);
}

void ImageLabel::updateDisplayedImage() {
if(!imageLoaded()) {
return;
}

// Draw white background, otherwise transparency is converted to black.
QImage image{QSize{512, 512}, QImage::Format_ARGB32};
image.fill(QColor{Qt::white});
QPainter painter{&image};
painter.drawImage(0, 0, _image.scaled(image.size()));

setPixmap(QPixmap::fromImage(image.convertToFormat(QImage::Format_Mono, _flags)));
}

bool ImageLabel::imageLoaded() const {
return !_image.isNull();
}
90 changes: 90 additions & 0 deletions EzGraverUi/imagelabel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef IMAGELABEL_H
#define IMAGELABEL_H

#include "clicklabel.h"

class ImageLabel : public ClickLabel {
Q_OBJECT
Q_PROPERTY(QImage image READ image WRITE setImage NOTIFY imageChanged)
Q_PROPERTY(Qt::ImageConversionFlags conversionFlags READ conversionFlags WRITE setConversionFlags NOTIFY conversionFlagsChanged)
Q_PROPERTY(bool imageLoaded READ imageLoaded NOTIFY imageLoadedChanged)

public:
/*!
* Creates a new instance with the given \a parent.
*
* \param parent The parent of the label.
*/
explicit ImageLabel(QWidget* parent=NULL);

/*!
* Frees all required resources upon deconstruction.
*/
virtual ~ImageLabel();

/*!
* Gets the currently loaded image without the application of
* any conversion.
*
* \return The currently loaded image.
*/
QImage image() const;

/*!
* Changes the currently displayed image to the given image
* and applies the selected conversion method.
*
* \param image The image to load.
*/
void setImage(QImage const& image);

/*!
* Gets the currently selected conversion flags.
*
* \return The currently selected conversion flags.
*/
Qt::ImageConversionFlags conversionFlags() const;

/*!
* Changes the applied conversion flags to the given one and
* updates the currently displayed image.
*
* \param flags The conversion flags to use.
*/
void setConversionFlags(Qt::ImageConversionFlags const& flags);

/*!
* Gets if an image has been loaded.
*
* \return Returns \c true if an image is loaded.
*/
bool imageLoaded() const;
signals:
/*!
* Fired as soon as the image has been changed.
*
* \param image The new image in its original state (unconverted).
*/
void imageChanged(QImage const& image);

/*!
* Fired as soon as the conversion flags hav been changed.
*
* \param flags The newly applied conversion flags.
*/
void conversionFlagsChanged(Qt::ImageConversionFlags const& flags);

/*!
* Fired as soon as an image has been loaded.
*
* \param imageLoaded \c true if an image is loaded.
*/
void imageLoadedChanged(bool imageLoaded);
private:
QImage _image;
Qt::ImageConversionFlags _flags;

void updateDisplayedImage();
};

#endif // IMAGELABEL_H
3 changes: 1 addition & 2 deletions EzGraverUi/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include <QApplication>
#include <QString>

int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
Expand Down
40 changes: 21 additions & 19 deletions EzGraverUi/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@

MainWindow::MainWindow(QWidget* parent)
: QMainWindow{parent}, _ui{new Ui::MainWindow},
_portTimer{}, _ezGraver{}, _connected{false}, _imageLoaded{false} {
_portTimer{}, _image{}, _ezGraver{}, _connected{false} {
_ui->setupUi(this);
setAcceptDrops(true);

connect(&_portTimer, &QTimer::timeout, this, &MainWindow::updatePorts);
_portTimer.start(1000);

setupBindings();
initBindings();
initConversionFlags();
setConnected(false);
}

MainWindow::~MainWindow() {
delete _ui;
}

void MainWindow::setupBindings() {
void MainWindow::initBindings() {
connect(_ui->burnTime, &QSlider::valueChanged, [this](int const& v) { _ui->burnTimeLabel->setText(QString::number(v)); });

connect(this, &MainWindow::connectedChanged, _ui->ports, &QComboBox::setDisabled);
Expand All @@ -45,10 +46,20 @@ void MainWindow::setupBindings() {
connect(this, &MainWindow::connectedChanged, _ui->start, &QPushButton::setEnabled);
connect(this, &MainWindow::connectedChanged, _ui->pause, &QPushButton::setEnabled);
connect(this, &MainWindow::connectedChanged, _ui->reset, &QPushButton::setEnabled);
connect(_ui->conversionFlags, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) {
_ui->image->setConversionFlags(static_cast<Qt::ImageConversionFlags>(_ui->conversionFlags->itemData(index).toInt()));
});

auto uploadEnabled = [this]() { _ui->upload->setEnabled(_imageLoaded && _connected); };
auto uploadEnabled = [this]() { _ui->upload->setEnabled(_ui->image->imageLoaded() && _connected); };
connect(this, &MainWindow::connectedChanged, uploadEnabled);
connect(this, &MainWindow::imageLoadedChanged, uploadEnabled);
connect(_ui->image, &ImageLabel::imageLoadedChanged, uploadEnabled);
}

void MainWindow::initConversionFlags() {
_ui->conversionFlags->addItem("DiffuseDither", Qt::DiffuseDither);
_ui->conversionFlags->addItem("OrderedDither", Qt::OrderedDither);
_ui->conversionFlags->addItem("ThresholdDither", Qt::ThresholdDither);
_ui->conversionFlags->setCurrentIndex(0);
}

void MainWindow::printVerbose(QString const& verbose) {
Expand Down Expand Up @@ -76,10 +87,8 @@ void MainWindow::loadImage(QString const& fileName) {
printVerbose("failed to load image");
return;
}
image = image.scaled(512, 512).convertToFormat(QImage::Format_Mono);

_ui->image->setPixmap(QPixmap::fromImage(image));
setImageLoaded(true);
_ui->image->setImage(image);
}

bool MainWindow::connected() const {
Expand All @@ -91,15 +100,6 @@ void MainWindow::setConnected(bool connected) {
emit connectedChanged(connected);
}

bool MainWindow::imageLoaded() const {
return _imageLoaded;
}

void MainWindow::setImageLoaded(bool imageLoaded) {
_imageLoaded = imageLoaded;
emit imageLoadedChanged(imageLoaded);
}

void MainWindow::on_connect_clicked() {
try {
printVerbose(QString("connecting to port %1").arg(_ui->ports->currentText()));
Expand Down Expand Up @@ -178,8 +178,10 @@ void MainWindow::on_disconnect_clicked() {
}

void MainWindow::on_image_clicked() {
auto fileName = QFileDialog::getOpenFileName(this, "Open Image", "", "Image Files (*.png *.jpeg *.jpg *.bmp)");
loadImage(fileName);
auto fileName = QFileDialog::getOpenFileName(this, "Open Image", "", "Images (*.png *.jpeg *.jpg *.bmp)");
if(!fileName.isNull()) {
loadImage(fileName);
}
}

void MainWindow::dragEnterEvent(QDragEnterEvent* event) {
Expand Down
10 changes: 4 additions & 6 deletions EzGraverUi/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ class MainWindow;
class MainWindow : public QMainWindow {
Q_OBJECT
Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged)
Q_PROPERTY(bool imageLoaded READ imageLoaded NOTIFY imageLoadedChanged)

public:
explicit MainWindow(QWidget* parent = NULL);
~MainWindow();

bool connected() const;
bool imageLoaded() const;
signals:
void connectedChanged(bool connected);
void imageLoadedChanged(bool connected);

private slots:
void on_connect_clicked();
Expand All @@ -52,14 +49,15 @@ private slots:
private:
Ui::MainWindow* _ui;
QTimer _portTimer;
QImage _image;

std::shared_ptr<EzGraver> _ezGraver;
bool _connected;
bool _imageLoaded;

void setupBindings();
void initBindings();
void initConversionFlags();

void setConnected(bool connected);
void setImageLoaded(bool imageLoaded);
void printVerbose(QString const& verbose);
void loadImage(QString const& fileName);
};
Expand Down
Loading

0 comments on commit fb145b9

Please sign in to comment.