Skip to content

Commit

Permalink
Добавлена пипетка для захвата цвета с экрана
Browse files Browse the repository at this point in the history
  • Loading branch information
Legotckoi authored and Evgenii committed Jan 31, 2016
1 parent f8fc9da commit 279d5f3
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/images.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<file>images/myappico.ico</file>
<file>images/close-circle-black.png</file>
<file>images/close-circle-outline-black.png</file>
<file>images/eyedropper-black.png</file>
</qresource>
</RCC>
Binary file added src/images/eyedropper-black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 76 additions & 31 deletions src/popupcolor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <QGraphicsDropShadowEffect>
#include <cmath>
#include "popupmessage.h"
#include "qmessagebox.h"

PopUpColor::PopUpColor(QWidget *parent) : QWidget(parent)
{
Expand All @@ -30,19 +29,29 @@ PopUpColor::PopUpColor(QWidget *parent) : QWidget(parent)
animation.setPropertyName("popupOpacity");

setLayout(&layout);

label.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
layout.addWidget(&label,0,0,1,2);
layout.addWidget(&closeButton,0,2);
layout.addWidget(&pickerButton,1,0);
comboBox.addItems(QStringList() << "HEX" << "RGB" << "CMYK" << "HSV" << "HSL");
comboBox.setCurrentIndex(0);
layout.addWidget(&comboBox,1,0,1,2);
layout.addWidget(&closeButton,0,1);
label.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
layout.addWidget(&label,0,0);
layout.addWidget(&comboBox,1,1,1,2);

reloadSettings();
setPopupColor(QColor(Qt::white));
setCurrentColor(QColor(Qt::white));

connect(&comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeIndexComboBoxColor(int)));
connect(&comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &PopUpColor::changeIndexComboBoxColor);
connect(&label, &CodeLabel::setPos, this, &PopUpColor::showPos);
connect(&closeButton, &QToolButton::clicked, this, &PopUpColor::hide);
connect(&closeButton, &QToolButton::clicked, &dummyTransparentWindow, &TransparentWindow::hide);
connect(&closeButton, &QToolButton::clicked, this, &PopUpColor::backColor);
connect(&pickerButton, &QToolButton::clicked, this, &PopUpColor::pickerButtonClicked);
connect(this, &PopUpColor::currentColorChanged, this, &PopUpColor::changeLabelText);
connect(this, &PopUpColor::currentColorChanged, this, &PopUpColor::changeStyleSheets);
connect(&dummyTransparentWindow, &TransparentWindow::changeColor, this, &PopUpColor::setCurrentColor);
connect(&dummyTransparentWindow, &TransparentWindow::backColor, this, &PopUpColor::backColor);
connect(&dummyTransparentWindow, &TransparentWindow::saveColor, this, &PopUpColor::slotCopyBuffer);
}

PopUpColor::~PopUpColor()
Expand Down Expand Up @@ -81,21 +90,22 @@ void PopUpColor::paintEvent(QPaintEvent *event)

painter.drawRoundedRect(roundedRect, 2, 2);

QPolygonF triangle;
/* QPolygonF triangle;
triangle << QPoint(rect().x() + rect().width() / 2 - 10, rect().y() + rect().height() - 10)
<< QPoint(rect().x() + rect().width() / 2, rect().y() + rect().height())
<< QPoint(rect().x() + rect().width() / 2 + 10, rect().y() + rect().height() - 10);
QPainterPath trianglePath;
trianglePath.addPolygon(triangle);
painter.drawPath(trianglePath);
painter.drawPath(trianglePath);*/
}

bool PopUpColor::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(eventType)
Q_UNUSED(result)
#ifdef Q_OS_WIN32
MSG* msg = reinterpret_cast<MSG*>(message);

if(msg->message == WM_HOTKEY){
Expand All @@ -104,10 +114,11 @@ bool PopUpColor::nativeEvent(const QByteArray &eventType, void *message, long *r
case 100: {
QScreen *screen = QApplication::primaryScreen();
QImage img = screen->grabWindow(0).toImage();
QRgb b = img.pixel(QCursor::pos());
QColor c;
c.setRgb(b);
setColor(c);
QColor color;
color.setRgb(img.pixel(QCursor::pos()));;
setCurrentColor(color);
(followCursor) ? showPos(QCursor::pos()) : show();
slotCopyBuffer();
return true;
break;
}
Expand All @@ -125,17 +136,10 @@ bool PopUpColor::nativeEvent(const QByteArray &eventType, void *message, long *r
break;
}
}
#endif
return false;
}

void PopUpColor::setPopupColor(const QColor &color)
{
currentColor = color;
changeStyleSheets();
setLabelText();
this->repaint();
}

void PopUpColor::show()
{
adjustSize();
Expand Down Expand Up @@ -164,10 +168,7 @@ void PopUpColor::show()
void PopUpColor::showPos(QPoint point)
{
adjustSize();
int cursorX = (QApplication::desktop()->width() - point.x() < width()) ? (point.x() - width()) : point.x();
int cursorY = (QApplication::desktop()->height() - point.y() < height()) ? (point.y() - height()) : point.y();

setGeometry(cursorX, cursorY, width(), height());
setGeometry(point.x(), point.y(), width(), height());

if(!isVisible()){
setWindowOpacity(0.0);
Expand All @@ -193,19 +194,24 @@ void PopUpColor::reloadSettings()
copyBuffer = settings.value(SETTINGS_COPY_BUFF, false).toBool();
typeCopyBuffer = settings.value(SETTINGS_TYPE_BUFF, 0).toInt();
followCursor = settings.value(SETTINGS_FOLLOW_CURSOR, false).toBool();
#ifdef Q_OS_WIN32
UnregisterHotKey((HWND) PopUpColor::winId(), 101);
if(settings.value(SETTINGS_ALLOW_SCREENSHOTS, false).toBool()){
RegisterHotKey((HWND)PopUpColor::winId(), 101, 0, VK_SNAPSHOT);
}
#endif
keys = QKeySequence(settings.value(KEY_SEQUENCE_PIXEL, QVariant()).toString());
#ifdef Q_OS_WIN32
UnregisterHotKey((HWND)PopUpColor::winId(), 100);
RegisterHotKey((HWND)PopUpColor::winId(), 100, winKeyModificator(keys), winHotKey(keys));
#endif
comboBox.setCurrentIndex(typeCopyBuffer);
}

void PopUpColor::dropperbuttonClicked()
void PopUpColor::pickerButtonClicked()
{

tempCurrentColor = getCurrentColor();
dummyTransparentWindow.showFullScreen();
}

void PopUpColor::hideAnimation()
Expand All @@ -219,15 +225,19 @@ void PopUpColor::hideAnimation()
void PopUpColor::changeIndexComboBoxColor(int index)
{
typeCopyBuffer = index;
setLabelText();
adjustSize();
changeLabelText();
slotCopyBuffer();
this->repaint();
}

void PopUpColor::backColor()
{
setCurrentColor(tempCurrentColor);
}

void PopUpColor::setColor(const QColor &color)
{
setPopupColor(color);
setCurrentColor(color);
(followCursor) ? showPos(QCursor::pos()) : show();
slotCopyBuffer();
}
Expand Down Expand Up @@ -272,6 +282,16 @@ void PopUpColor::changeStyleSheets()
"margin-left: 6px; "
"margin-bottom: 0px;"
"font-size: 16px; }");
pickerButton.setStyleSheet("QToolButton { image: url(:/images/eyedropper-black.png);"
"icon-size: 16px;"
"height: 16px;"
"width: 16px;"
"margin: 6px;"
"padding: 6px;"
"border: none;"
"border-radius: 2px;"
"background-color: " + strColor + "; }"
"QToolButton:pressed { background-color: transparent; }");
closeButton.setStyleSheet("QToolButton { image: url(:/images/close-circle-outline-black.png);"
"icon-size: 16px;"
"height: 16px;"
Expand All @@ -289,6 +309,16 @@ void PopUpColor::changeStyleSheets()
"margin-left: 6px; "
"margin-bottom: 0px;"
"font-size: 16px; }");
pickerButton.setStyleSheet("QToolButton { image: url(:/images/eyedropper.png);"
"icon-size: 16px;"
"height: 16px;"
"width: 16px;"
"margin: 6px;"
"padding: 6px;"
"border: none;"
"border-radius: 2px;"
"background-color: " + strColor + "; }"
"QToolButton:pressed { background-color: transparent; }");
closeButton.setStyleSheet("QToolButton { image: url(:/images/close-circle-outline.png);"
"icon-size: 16px;"
"height: 16px;"
Expand All @@ -300,9 +330,10 @@ void PopUpColor::changeStyleSheets()
"border: none;}"
"QToolButton:pressed { image: url(:/images/close-circle.png);}");
}
this->repaint();
}

void PopUpColor::setLabelText()
void PopUpColor::changeLabelText()
{
switch (typeCopyBuffer) {
case 0:
Expand Down Expand Up @@ -336,6 +367,7 @@ void PopUpColor::setLabelText()
default:
break;
}
adjustSize();
}

void PopUpColor::slotCopyBuffer()
Expand Down Expand Up @@ -373,6 +405,7 @@ void PopUpColor::slotCopyBuffer()
}
}

#ifdef Q_OS_WIN32
unsigned int PopUpColor::winKeyModificator(QKeySequence sequence)
{
QStringList list = sequence.toString().split("+");
Expand Down Expand Up @@ -405,14 +438,26 @@ char PopUpColor::winHotKey(QKeySequence sequence)
}
return hotKey;
}
#endif

void PopUpColor::setPopupOpacity(float opacity)
{
popupOpacity = opacity;
setWindowOpacity(opacity);
}

void PopUpColor::setCurrentColor(QColor color)
{
currentColor = color;
emit currentColorChanged();
}

float PopUpColor::getPopupOpacity() const
{
return popupOpacity;
}

QColor PopUpColor::getCurrentColor() const
{
return currentColor;
}
29 changes: 20 additions & 9 deletions src/popupcolor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
#include <QComboBox>

#include "codelabel.h"
#include "transparentwindow.h"

class PopUpColor : public QWidget
{
Q_OBJECT

Q_PROPERTY(float popupOpacity READ getPopupOpacity WRITE setPopupOpacity)
Q_PROPERTY(QColor currentColor READ getCurrentColor WRITE setCurrentColor NOTIFY currentColorChanged)

void setPopupOpacity(float opacity);
float getPopupOpacity() const;
QColor getCurrentColor() const;

signals:
void currentColorChanged();

public:
explicit PopUpColor(QWidget *parent = 0);
Expand All @@ -32,26 +37,35 @@ class PopUpColor : public QWidget
bool nativeEvent(const QByteArray &eventType, void *message, long *result);

public slots:
void setPopupColor(const QColor& color);
void show();
void showPos(QPoint point);
void reloadSettings();
void setPopupOpacity(float opacity);
void setCurrentColor(QColor color);

private slots:
void dropperbuttonClicked();
void pickerButtonClicked();
void hideAnimation();
void changeIndexComboBoxColor(int index);
void backColor();
void changeStyleSheets();
void changeLabelText();
void slotCopyBuffer();

private:
// Properties
float popupOpacity;
QColor currentColor;
QColor tempCurrentColor;
// Interface
CodeLabel label;
QComboBox comboBox;
QToolButton dropperButton;
QToolButton pickerButton;
QToolButton closeButton;
QGridLayout layout;
QPropertyAnimation animation;
float popupOpacity;

QColor currentColor;
TransparentWindow dummyTransparentWindow;

// Переменные для работы с горячими клавишами
QKeySequence keys;
Expand All @@ -60,9 +74,6 @@ private slots:
bool followCursor;

void setColor(const QColor &color);
void changeStyleSheets();
void setLabelText();
void slotCopyBuffer();
unsigned int winKeyModificator(QKeySequence sequence);
char winHotKey(QKeySequence sequence);
};
Expand Down
6 changes: 4 additions & 2 deletions src/src.pro
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ SOURCES += main.cpp\
popupcolor.cpp \
ecolorcore.cpp \
popupmessage.cpp \
codelabel.cpp
codelabel.cpp \
transparentwindow.cpp

HEADERS += \
about.h \
Expand All @@ -56,7 +57,8 @@ HEADERS += \
popupcolor.h \
ecolorcore.h \
popupmessage.h \
codelabel.h
codelabel.h \
transparentwindow.h

FORMS += \
about.ui \
Expand Down
43 changes: 43 additions & 0 deletions src/transparentwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "transparentwindow.h"
#include <QDebug>
#include <QApplication>
#include <QScreen>

TransparentWindow::TransparentWindow(QWindow *parent) : QWindow(parent)
{
setFlags(Qt::Tool | Qt::FramelessWindowHint);
QPixmap imagePipette(":/images/eyedropper-black.png");
QPixmap pipette = imagePipette.scaled(24,24,Qt::KeepAspectRatio);
QCursor cursor = QCursor(pipette,4,20);
setCursor(cursor);
}

void TransparentWindow::mouseMoveEvent(QMouseEvent *event)
{
event->accept();
QImage img = QApplication::primaryScreen()->grabWindow(0).toImage();
QColor color;
color.setRgb(img.pixel(QCursor::pos()));
emit changeColor(color);
}

void TransparentWindow::mouseReleaseEvent(QMouseEvent *event)
{
event->accept();
if(event->button() == Qt::LeftButton){
emit saveColor();
} else {
emit backColor();
}
hide();
}

void TransparentWindow::keyPressEvent(QKeyEvent *event)
{
event->accept();
if(event->key() == Qt::Key_Escape) {
emit backColor();
hide();
}
}

Loading

0 comments on commit 279d5f3

Please sign in to comment.