Skip to content

Commit

Permalink
Fix: Rendering issues, Added pick cancellation with ESC or right click
Browse files Browse the repository at this point in the history
  • Loading branch information
rurigk committed Jul 14, 2020
1 parent 5fc7097 commit c371fc7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
6 changes: 6 additions & 0 deletions KonkorpColorPicker.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

VERSION = 0.3.0.0
QMAKE_TARGET_COMPANY = Rurigk
QMAKE_TARGET_PRODUCT = ColorPicker
QMAKE_TARGET_DESCRIPTION = Desktop color picker
QMAKE_TARGET_COPYRIGHT = LGPLv3

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
Expand Down
12 changes: 11 additions & 1 deletion colorpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ColorPanel::ColorPanel(QWidget *parent)
{
ColorPicker *colorPicker = new ColorPicker();
connect(colorPicker, &ColorPicker::ColorPicked, this, &ColorPanel::ColorPicked);
connect(colorPicker, &ColorPicker::PickerCancelled, this, &ColorPanel::PickerCancelled);
colorPicker->setMouseTracking(true);
pickerWindows->push_back(colorPicker);
}
Expand Down Expand Up @@ -174,6 +175,15 @@ void ColorPanel::ColorPicked(QColor color)
ShowNotification("Color copied to clipboard", color.name(QColor::HexRgb));
}

void ColorPanel::PickerCancelled()
{
for(int wIndex = 0; wIndex < pickerWindows->count(); wIndex++)
{
ColorPicker *picker = pickerWindows->at(wIndex);
picker->hide();
}
}

void ColorPanel::ColorPickedFromHistory(QColor color)
{
//qDebug() << "Picked from history: " << color.name(QColor::HexRgb);
Expand All @@ -186,7 +196,7 @@ void ColorPanel::ColorPickedFromToolbar(int index)
{
if(colorPickerHistory->history->count() - 1 >= index)
{
qDebug() << (colorPickerHistory->history->count()-1) - index;
//qDebug() << (colorPickerHistory->history->count()-1) - index;
QColor color = colorPickerHistory->history->at((colorPickerHistory->history->count()-1) - index);
QClipboard *clipboard = QGuiApplication::clipboard();
clipboard->setText(color.name(QColor::HexRgb).toUpper());
Expand Down
1 change: 1 addition & 0 deletions colorpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private slots:
void ColorHoveredFromHistory(QColor color);
void ColorUnhoveredFromHistory();
void ColorPickedFromToolbar(int index);
void PickerCancelled();

private:
Ui::ColorPanel *ui;
Expand Down
31 changes: 23 additions & 8 deletions colorpicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ ColorPicker::ColorPicker(QWidget *parent) :
//setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint | Qt::WindowStaysOnTopHint | Qt::ToolTip);
setMouseTracking(true);
mousePos = new QPoint(-1, -1);
shortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
connect(shortcut, &QShortcut::activated, this, &ColorPicker::EscapeKeyPressed);
}

ColorPicker::~ColorPicker()
Expand Down Expand Up @@ -72,19 +74,26 @@ void ColorPicker::mouseMoveEvent(QMouseEvent * event)
else
{
mousePos = mousePoint;
update((mousePos->x() - (rectSize / 2)) - 30,
(mousePos->y() - (rectSize / 2)) - 30,
rectSize + 60, rectSize + 60);
update((mousePos->x() - (rectSize / 2)) - 50,
(mousePos->y() - (rectSize / 2)) - 50,
rectSize + 100, rectSize + 100);
}
}

void ColorPicker::mousePressEvent(QMouseEvent *event)
{
QImage image = backgroundPixmap.toImage();
QColor color = image.pixel(event->pos().x(), event->pos().y());
ColorPicked(color);
backgroundPixmap = emptyPixmap;
mousePos = new QPoint(-1, -1);
if(event->button() == Qt::LeftButton)
{
QImage image = backgroundPixmap.toImage();
QColor color = image.pixel(event->pos().x(), event->pos().y());
ColorPicked(color);
backgroundPixmap = emptyPixmap;
mousePos = new QPoint(-1, -1);
}
else
{
PickerCancelled();
}
}

void ColorPicker::enterEvent(QEvent *)
Expand All @@ -98,3 +107,9 @@ void ColorPicker::leaveEvent(QEvent *)
drawLens = false;
repaint();
}

void ColorPicker::EscapeKeyPressed()
{
qDebug() << "Escpressed";
PickerCancelled();
}
4 changes: 4 additions & 0 deletions colorpicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QMouseEvent>
#include <QDebug>
#include <QColor>
#include <QShortcut>

namespace Ui {
class ColorPicker;
Expand All @@ -24,14 +25,17 @@ class ColorPicker : public QWidget

signals:
void ColorPicked(QColor color);
void PickerCancelled();

private:
Ui::ColorPicker *ui;
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
void EscapeKeyPressed();
QPoint *mousePos;
QShortcut *shortcut;
bool drawLens = false;
int rectSize = 100;
int lensRectSize = 20;
Expand Down

0 comments on commit c371fc7

Please sign in to comment.