Skip to content

Commit

Permalink
#24 ColorSelectionWidget mouse events
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrussmill committed Feb 7, 2019
1 parent 1906f59 commit 3cf44e0
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 22 deletions.
127 changes: 111 additions & 16 deletions src/colorselectionwidget.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
#include "colorselectionwidget.h"
#include "mousewheeleatereventfilter.h"
#include "ui_colorselectionwidget.h"
#include <QPixmap>
#include <QImage>
#include <QPainter>
#include <QMouseEvent>
#include <QPoint>

#include <QDebug>

ColorSelectionWidget::ColorSelectionWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ColorSelectionWidget)
{
ui->setupUi(this);
MouseWheelEaterEventFilter *wheelFilter = new MouseWheelEaterEventFilter(this);
ui->verticalSlider_ValueSelect->installEventFilter(wheelFilter);

//setup pixmaps via populating color scanlines - 7 distinct colors in Qt HSV Color Wheel (http://doc.qt.io/qt-5/qcolor.html)
QColor initColor(Qt::lightGray);
QImage paletteDisplay = QImage(7, 10, QImage::Format_RGB888);
selectedColor_m = QColor(Qt::lightGray);
valueDisplay_m = QImage(1, 10, QImage::Format_RGB888);
colorPreview_m.fill(Qt::lightGray);

int hue;
for(int x = 0; x < 7; x++)
Expand All @@ -25,39 +31,128 @@ ColorSelectionWidget::ColorSelectionWidget(QWidget *parent) :
{
//saturation & value set 10 y intensities to yield smoother interpolation results opposed to 2
paletteDisplay.setPixelColor(x, y, QColor::fromHsv(hue, 255 - 255 * (y + 1) / 10.0, 255 - 40 * (y + 1) / 10.0));
valueDisplay_m.setPixelColor(0, y, QColor::fromHsv(75, 0, 255 - 255 * (y + 1) / 10.0));
}
}

//use hardware to interpolate inbetween colors
ui->label_PaletteVisual->setPixmap(QPixmap::fromImage(paletteDisplay)
.scaled(ui->label_PaletteVisual->width(), ui->label_PaletteVisual->height(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
ui->label_ValueVisual->setPixmap(QPixmap::fromImage(valueDisplay_m)
.scaled(ui->label_ValueVisual->width(), ui->label_ValueVisual->height(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
ui->label_Color->setPixmap(colorPreview_m);

//set initial menu items
ui->verticalSlider_ValueSelect->setValue(initColor.value());
ui->spinBox_Red->setValue(initColor.red());
ui->spinBox_Green->setValue(initColor.green());
ui->spinBox_Blue->setValue(initColor.blue());
ui->spinBox_Hue->setValue(initColor.hue());
ui->spinBox_Saturation->setValue(initColor.saturation());
ui->spinBox_Value->setValue(initColor.value());
ui->lineEdit_HTML->setText(initColor.name());
ui->verticalSlider_ValueSelect->setValue(selectedColor_m.value());
populateColorValues();

//connect signals and slots
connect(ui->verticalSlider_ValueSelect, SIGNAL(valueChanged(int)), this, SLOT(setColorFromPalette()));
connect(ui->verticalSlider_ValueSelect, SIGNAL(valueChanged(int)), this, SLOT(populateColorValues()));
}

//autogenerated destructor
ColorSelectionWidget::~ColorSelectionWidget()
{
delete ui;
}

void ColorSelectionWidget::setColor(QColor color)
void ColorSelectionWidget::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
QPoint mousePosition = ui->label_PaletteVisual->mapFromParent(event->pos());
if(mousePosition.x() >= 0 && mousePosition.y() >= 0
&& mousePosition.x() < ui->label_PaletteVisual->width()
&& mousePosition.y() < ui->label_PaletteVisual->height())
{
palettePoint_m = mousePosition;
//paint pixmap
setColorFromPalette();
populateColorValues();
//emit color
}
}
QWidget::mouseReleaseEvent(event);
}

void ColorSelectionWidget::mouseMoveEvent(QMouseEvent *event)
{
if(!event->pos().isNull())
{
QPoint mousePosition = ui->label_PaletteVisual->mapFromParent(event->pos());
if(mousePosition.x() >= 0 && mousePosition.y() >= 0
&& mousePosition.x() < ui->label_PaletteVisual->width()
&& mousePosition.y() < ui->label_PaletteVisual->height())
{
palettePoint_m = mousePosition;
//paint pixmap
setColorFromPalette();
populateColorValues();
}
}
QWidget::mouseMoveEvent(event);
}

void ColorSelectionWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
QPoint mousePosition = ui->label_PaletteVisual->mapFromParent(event->pos());
if(mousePosition.x() >= 0 && mousePosition.y() >= 0
&& mousePosition.x() < ui->label_PaletteVisual->width()
&& mousePosition.y() < ui->label_PaletteVisual->height())
{
palettePoint_m = mousePosition;
//paint pixmap
setColorFromPalette();
populateColorValues();
}
}
QWidget::mousePressEvent(event);
}

void ColorSelectionWidget::setColor(const QColor *color)
{

}

void ColorSelectionWidget::setColorFromPalette()
{
//remember y axes is inverted because qimage and x is because hue phase shift reversed
int hue = 359 - 359 * palettePoint_m.x() / static_cast<float>(ui->label_PaletteVisual->width() - 1);
int saturation = 255 - 255 * palettePoint_m.y() / static_cast<float>(ui->label_PaletteVisual->height() - 1);
selectedColor_m = QColor::fromHsv(hue, saturation, ui->verticalSlider_ValueSelect->value());
}

void ColorSelectionWidget::paintCursorOnPalette()
{
//need pixmap to be a member?
}

void ColorSelectionWidget::populateColorValues()
{
//see comments in constructor for value-bar generation
for(int y = 0; y < 10; y++)
{
valueDisplay_m.setPixelColor(0, y, QColor::fromHsv(selectedColor_m.hue(), 255, 255 - 255 * (y + 1) / 10.0));
}
ui->label_ValueVisual->setPixmap(QPixmap::fromImage(valueDisplay_m)
.scaled(ui->label_ValueVisual->width(), ui->label_ValueVisual->height(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation));

QImage color(1, 1, QImage::Format_RGB888);
color.setPixelColor(0, 0, selectedColor_m);
ui->label_Color->setPixmap(QPixmap::fromImage(color).
scaled(ui->label_Color->width(), ui->label_Color->height(),
Qt::IgnoreAspectRatio, Qt::FastTransformation));

//blockSignals(true);
ui->spinBox_Red->setValue(selectedColor_m.red());
ui->spinBox_Green->setValue(selectedColor_m.green());
ui->spinBox_Blue->setValue(selectedColor_m.blue());
ui->spinBox_Hue->setValue(selectedColor_m.hue());
ui->spinBox_Saturation->setValue(selectedColor_m.saturation());
ui->spinBox_Value->setValue(selectedColor_m.value());
ui->lineEdit_HTML->setText(selectedColor_m.name());
//blockSignals(false);
}

//make virt fns
15 changes: 13 additions & 2 deletions src/colorselectionwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
class QImage;
class QPixmap;
class QColor;
class QMouseEvent;
class QPoint;

namespace Ui {
class ColorSelectionWidget;
Expand All @@ -19,12 +21,21 @@ class ColorSelectionWidget : public QWidget
virtual ~ColorSelectionWidget();

public slots:
virtual void setColor(QColor color);
virtual void mouseReleaseEvent(QMouseEvent *event) override;
virtual void mousePressEvent(QMouseEvent *event) override;
virtual void mouseMoveEvent(QMouseEvent *event) override;
virtual void setColor(const QColor *color);

private:
void paintCursorOnPalette();
Ui::ColorSelectionWidget *ui;
QImage valueDisplay_m;
QPixmap colorPreview_m;
QPoint palettePoint_m;
QColor selectedColor_m;

private slots:
void setColorFromPalette();
void populateColorValues();
};

#endif // COLORSELECTIONWIDGET_H
35 changes: 34 additions & 1 deletion src/colorselectionwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
<property name="keyboardTracking">
<bool>true</bool>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
</item>
<item row="1" column="2">
Expand All @@ -190,6 +193,9 @@
<property name="keyboardTracking">
<bool>true</bool>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
</item>
<item row="0" column="0">
Expand Down Expand Up @@ -230,6 +236,9 @@
<property name="keyboardTracking">
<bool>true</bool>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
</item>
<item row="2" column="1">
Expand All @@ -240,6 +249,9 @@
<property name="keyboardTracking">
<bool>true</bool>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
</item>
<item row="0" column="1">
Expand All @@ -250,6 +262,9 @@
<property name="keyboardTracking">
<bool>true</bool>
</property>
<property name="maximum">
<number>359</number>
</property>
</widget>
</item>
<item row="1" column="1">
Expand All @@ -260,6 +275,9 @@
<property name="keyboardTracking">
<bool>true</bool>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
</item>
<item row="2" column="0">
Expand All @@ -275,11 +293,23 @@
<item row="4" column="3">
<widget class="QLabel" name="label_Color">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Preferred">
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>64</width>
<height>20</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>64</width>
<height>20</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
Expand Down Expand Up @@ -319,6 +349,9 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="placeholderText">
<string notr="true"/>
</property>
</widget>
</item>
</layout>
Expand Down
6 changes: 3 additions & 3 deletions src/imagewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ void ImageWidget::resizeEvent(QResizeEvent *event)
* selected, the coordinates under the mouse are emitted as a QPoint relative to the
* attachedImage_m's origin. If RectROI is selected, the region is finished being set, cropped
* and drawn before being emitted, else if DragROI the region is only cropped to fit and emitted. */
void ImageWidget::mouseReleaseEvent(QMouseEvent *event) //UPDATE THIS COMMENT
void ImageWidget::mouseReleaseEvent(QMouseEvent *event)
{
if(imageAttached())
{
Expand All @@ -408,7 +408,7 @@ void ImageWidget::mouseReleaseEvent(QMouseEvent *event) //UPDATE THIS COMMENT
{
region_m.setBottomRight(getPointInImage());
region_m = getAdjustedRegion();
selectRegionOnPixmap(); //do I need to draw again here? Check mouse movement in debug or can I just emit adjusted
selectRegionOnPixmap();
emit imageRectRegionSelected(region_m);
retrieveCoordinateMode_m = DragROI;
//qDebug() << region_m;
Expand Down Expand Up @@ -476,7 +476,7 @@ void ImageWidget::mouseMoveEvent(QMouseEvent *event)
{
if(imageAttached())
{
if(!event->pos().isNull() == Qt::LeftButton && retrieveCoordinateMode_m & 0x38)
if(!event->pos().isNull() && retrieveCoordinateMode_m & 0x38)
{
if(retrieveCoordinateMode_m == RectROI)
{
Expand Down
4 changes: 4 additions & 0 deletions src/transformmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

#include <QDebug>

/* Constructor installs the necessary filters for different objects, groups the buttons together
* to easily search for the selected button, and establishes all signals/slots necessary.*/
TransformMenu::TransformMenu(QWidget *parent) :
QScrollArea(parent),
ui(new Ui::TransformMenu)
Expand Down Expand Up @@ -118,11 +120,13 @@ TransformMenu::TransformMenu(QWidget *parent) :
//NOTE: rotate and warp will need silentEnable like in Filter
}

//autogenerated destructor
TransformMenu::~TransformMenu()
{
delete ui;
}

// Function initializes the necessary widget values to their starting values.
void TransformMenu::initializeMenu()
{
blockSignals(true);
Expand Down

0 comments on commit 3cf44e0

Please sign in to comment.