mirror of
https://github.com/fergalmoran/flameshot.git
synced 2025-12-27 20:30:52 +00:00
Add basic logic in colorPicker
This commit is contained in:
@@ -16,22 +16,20 @@
|
|||||||
// along with Flameshot. If not, see <http://www.gnu.org/licenses/>.
|
// along with Flameshot. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#include "capturemodification.h"
|
#include "capturemodification.h"
|
||||||
#include <QSettings>
|
|
||||||
#include <QColor>
|
#include <QColor>
|
||||||
|
|
||||||
// CaptureModification is a single modification in the screenshot drawn
|
// CaptureModification is a single modification in the screenshot drawn
|
||||||
// by the user.
|
// by the user.
|
||||||
|
|
||||||
CaptureModification::CaptureModification(
|
CaptureModification::CaptureModification(
|
||||||
const Button::Type t, QPoint p) : m_type(t) {
|
const Button::Type t, QPoint p, const QColor c) : m_type(t) {
|
||||||
m_coords.append(p);
|
m_coords.append(p);
|
||||||
if (m_type == Button::Type::circle || m_type == Button::Type::rectangle
|
if (m_type == Button::Type::circle || m_type == Button::Type::rectangle
|
||||||
|| m_type == Button::Type::arrow || m_type == Button::Type::line ||
|
|| m_type == Button::Type::arrow || m_type == Button::Type::line ||
|
||||||
m_type == Button::Type::marker) {
|
m_type == Button::Type::marker) {
|
||||||
m_coords.append(p);
|
m_coords.append(p);
|
||||||
}
|
}
|
||||||
QSettings settings;
|
m_color = c;
|
||||||
m_color = settings.value("drawColor").value<QColor>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CaptureModification::CaptureModification() {
|
CaptureModification::CaptureModification() {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class QPoint;
|
|||||||
class CaptureModification {
|
class CaptureModification {
|
||||||
public:
|
public:
|
||||||
CaptureModification();
|
CaptureModification();
|
||||||
CaptureModification(const Button::Type, const QPoint);
|
CaptureModification(const Button::Type, const QPoint, const QColor);
|
||||||
Button::Type getType() const;
|
Button::Type getType() const;
|
||||||
QColor getColor() const;
|
QColor getColor() const;
|
||||||
QVector<QPoint> getPoints() const;
|
QVector<QPoint> getPoints() const;
|
||||||
|
|||||||
@@ -53,8 +53,8 @@ namespace {
|
|||||||
|
|
||||||
CaptureWidget::CaptureWidget(QWidget *parent) :
|
CaptureWidget::CaptureWidget(QWidget *parent) :
|
||||||
QWidget(parent), m_mouseOverHandle(0), m_mouseIsClicked(false),
|
QWidget(parent), m_mouseOverHandle(0), m_mouseIsClicked(false),
|
||||||
m_newSelection(false), m_grabbing(false), m_onButton(false),
|
m_rightClick(false), m_newSelection(false), m_grabbing(false),
|
||||||
m_state(Button::Type::move) {
|
m_onButton(false), m_state(Button::Type::move) {
|
||||||
|
|
||||||
setAttribute(Qt::WA_DeleteOnClose);
|
setAttribute(Qt::WA_DeleteOnClose);
|
||||||
// create selection handlers
|
// create selection handlers
|
||||||
@@ -151,6 +151,8 @@ void CaptureWidget::paintEvent(QPaintEvent *) {
|
|||||||
|
|
||||||
void CaptureWidget::mousePressEvent(QMouseEvent *e) {
|
void CaptureWidget::mousePressEvent(QMouseEvent *e) {
|
||||||
if (e->button() == Qt::RightButton) {
|
if (e->button() == Qt::RightButton) {
|
||||||
|
m_rightClick = true;
|
||||||
|
setCursor(Qt::ArrowCursor);
|
||||||
m_colorPicker->move(e->pos().x()-m_colorPicker->width()/2,
|
m_colorPicker->move(e->pos().x()-m_colorPicker->width()/2,
|
||||||
e->pos().y()-m_colorPicker->height()/2);
|
e->pos().y()-m_colorPicker->height()/2);
|
||||||
m_colorPicker->show();
|
m_colorPicker->show();
|
||||||
@@ -160,7 +162,8 @@ void CaptureWidget::mousePressEvent(QMouseEvent *e) {
|
|||||||
if (e->button() == Qt::LeftButton) {
|
if (e->button() == Qt::LeftButton) {
|
||||||
m_mouseIsClicked = true;
|
m_mouseIsClicked = true;
|
||||||
if (m_state != Button::Type::move) {
|
if (m_state != Button::Type::move) {
|
||||||
m_modifications.append(CaptureModification(m_state, e->pos()));
|
m_modifications.append(CaptureModification(m_state, e->pos(),
|
||||||
|
m_colorPicker->getDrawColor()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_dragStartPoint = e->pos();
|
m_dragStartPoint = e->pos();
|
||||||
@@ -256,7 +259,10 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
|
|||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
m_mouseOverHandle = 0;
|
m_mouseOverHandle = 0;
|
||||||
if (m_selection.contains(e->pos()) && !m_onButton &&
|
|
||||||
|
if (m_rightClick) {
|
||||||
|
setCursor(Qt::ArrowCursor);
|
||||||
|
} else if (m_selection.contains(e->pos()) && !m_onButton &&
|
||||||
m_state == Button::Type::move) {
|
m_state == Button::Type::move) {
|
||||||
setCursor(Qt::OpenHandCursor);
|
setCursor(Qt::OpenHandCursor);
|
||||||
} else if (m_onButton) {
|
} else if (m_onButton) {
|
||||||
@@ -283,6 +289,7 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
|
|||||||
void CaptureWidget::mouseReleaseEvent(QMouseEvent *e) {
|
void CaptureWidget::mouseReleaseEvent(QMouseEvent *e) {
|
||||||
if (e->button() == Qt::RightButton) {
|
if (e->button() == Qt::RightButton) {
|
||||||
m_colorPicker->hide();
|
m_colorPicker->hide();
|
||||||
|
m_rightClick = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ protected:
|
|||||||
QRect m_selectionBeforeDrag;
|
QRect m_selectionBeforeDrag;
|
||||||
// utility flags
|
// utility flags
|
||||||
bool m_mouseIsClicked;
|
bool m_mouseIsClicked;
|
||||||
|
bool m_rightClick;
|
||||||
bool m_newSelection;
|
bool m_newSelection;
|
||||||
bool m_grabbing;
|
bool m_grabbing;
|
||||||
bool m_onButton;
|
bool m_onButton;
|
||||||
|
|||||||
@@ -1,17 +1,25 @@
|
|||||||
#include "colorpicker.h"
|
#include "colorpicker.h"
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <QMouseEvent>
|
||||||
|
#include <QSettings>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
ColorPicker::ColorPicker(QWidget *parent) : QWidget(parent),
|
ColorPicker::ColorPicker(QWidget *parent) : QWidget(parent),
|
||||||
m_colorAreaSize(18) {
|
m_colorAreaSize(18) {
|
||||||
setMouseTracking(true);
|
setMouseTracking(true);
|
||||||
|
|
||||||
|
QSettings settings;
|
||||||
|
m_uiColor = settings.value("uiColor").value<QColor>();
|
||||||
|
m_drawColor = settings.value("drawColor").value<QColor>();
|
||||||
|
const int extraSize = 6;
|
||||||
double radius = (colorList.size()*m_colorAreaSize/1.3)/(3.141592);
|
double radius = (colorList.size()*m_colorAreaSize/1.3)/(3.141592);
|
||||||
resize(radius*2 + m_colorAreaSize, radius*2 + m_colorAreaSize);
|
resize(radius*2 + m_colorAreaSize + extraSize,
|
||||||
|
radius*2 + m_colorAreaSize+ extraSize);
|
||||||
double degree = 360 / (colorList.size());
|
double degree = 360 / (colorList.size());
|
||||||
double degreeAcum = degree;
|
double degreeAcum = degree;
|
||||||
|
|
||||||
QLineF baseLine = QLineF(QPoint(radius, radius), QPoint(radius*2, radius));
|
QLineF baseLine = QLineF(QPoint(radius+extraSize/2, radius+extraSize/2),
|
||||||
|
QPoint(radius*2, radius));
|
||||||
|
|
||||||
for (int i = 0; i<colorList.size(); ++i) {
|
for (int i = 0; i<colorList.size(); ++i) {
|
||||||
m_colorAreaList.append(QRect(baseLine.x2(), baseLine.y2(),
|
m_colorAreaList.append(QRect(baseLine.x2(), baseLine.y2(),
|
||||||
@@ -21,6 +29,14 @@ ColorPicker::ColorPicker(QWidget *parent) : QWidget(parent),
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ColorPicker::~ColorPicker() {
|
||||||
|
QSettings().setValue("drawColor", m_drawColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
QColor ColorPicker::getDrawColor() {
|
||||||
|
return m_drawColor;
|
||||||
|
}
|
||||||
|
|
||||||
void ColorPicker::paintEvent(QPaintEvent *) {
|
void ColorPicker::paintEvent(QPaintEvent *) {
|
||||||
QPainter painter(this);
|
QPainter painter(this);
|
||||||
painter.setRenderHint(QPainter::Antialiasing);
|
painter.setRenderHint(QPainter::Antialiasing);
|
||||||
@@ -28,11 +44,34 @@ void ColorPicker::paintEvent(QPaintEvent *) {
|
|||||||
QVector<QRect> rects = handleMask();
|
QVector<QRect> rects = handleMask();
|
||||||
painter.setPen(QColor(Qt::black));
|
painter.setPen(QColor(Qt::black));
|
||||||
for(int i = 0; i < rects.size(); ++i) {
|
for(int i = 0; i < rects.size(); ++i) {
|
||||||
|
if (m_drawColor == QColor(colorList.at(i))) {
|
||||||
|
QColor c = QColor(m_uiColor);
|
||||||
|
c.setAlpha(155);
|
||||||
|
painter.setBrush(c);
|
||||||
|
c.setAlpha(100);
|
||||||
|
painter.setPen(c);
|
||||||
|
QRect highlight = rects.at(i);
|
||||||
|
highlight.moveTo(highlight.x() - 3, highlight.y() - 3);
|
||||||
|
highlight.setHeight(highlight.height() + 6);
|
||||||
|
highlight.setWidth(highlight.width() + 6);
|
||||||
|
painter.drawRoundRect(highlight, 100, 100);
|
||||||
|
painter.setPen(QColor(Qt::black));
|
||||||
|
}
|
||||||
painter.setBrush(QColor(colorList.at(i)));
|
painter.setBrush(QColor(colorList.at(i)));
|
||||||
painter.drawRoundRect(rects.at(i), 100, 100);
|
painter.drawRoundRect(rects.at(i), 100, 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ColorPicker::mouseMoveEvent(QMouseEvent *e) {
|
||||||
|
for(int i = 0; i < colorList.size(); ++i) {
|
||||||
|
if (m_colorAreaList.at(i).contains(e->pos())) {
|
||||||
|
m_drawColor = colorList.at(i);
|
||||||
|
update();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QVector<QRect> ColorPicker::handleMask() const {
|
QVector<QRect> ColorPicker::handleMask() const {
|
||||||
QVector<QRect> areas;
|
QVector<QRect> areas;
|
||||||
for(QRect rect: m_colorAreaList) {
|
for(QRect rect: m_colorAreaList) {
|
||||||
@@ -48,9 +87,8 @@ QVector<Qt::GlobalColor> ColorPicker::colorList = {
|
|||||||
Qt::yellow,
|
Qt::yellow,
|
||||||
Qt::green,
|
Qt::green,
|
||||||
Qt::darkGreen,
|
Qt::darkGreen,
|
||||||
Qt::darkCyan,
|
|
||||||
Qt::blue,
|
|
||||||
Qt::cyan,
|
Qt::cyan,
|
||||||
|
Qt::blue,
|
||||||
Qt::magenta,
|
Qt::magenta,
|
||||||
Qt::darkMagenta
|
Qt::darkMagenta
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,10 +8,15 @@ class ColorPicker : public QWidget
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit ColorPicker(QWidget *parent = 0);
|
explicit ColorPicker(QWidget *parent = 0);
|
||||||
|
~ColorPicker();
|
||||||
static QVector<Qt::GlobalColor> colorList;
|
static QVector<Qt::GlobalColor> colorList;
|
||||||
|
|
||||||
|
QColor getDrawColor();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void paintEvent(QPaintEvent *);
|
void paintEvent(QPaintEvent *);
|
||||||
|
void mouseMoveEvent(QMouseEvent *);
|
||||||
|
|
||||||
QVector<QRect> handleMask() const;
|
QVector<QRect> handleMask() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
@@ -21,6 +26,8 @@ public slots:
|
|||||||
private:
|
private:
|
||||||
const int m_colorAreaSize;
|
const int m_colorAreaSize;
|
||||||
QVector<QRect> m_colorAreaList;
|
QVector<QRect> m_colorAreaList;
|
||||||
|
|
||||||
|
QColor m_uiColor, m_drawColor;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // COLORPICKER_H
|
#endif // COLORPICKER_H
|
||||||
|
|||||||
Reference in New Issue
Block a user