mirror of
https://github.com/fergalmoran/flameshot.git
synced 2025-12-22 09:51:06 +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/>.
|
||||
|
||||
#include "capturemodification.h"
|
||||
#include <QSettings>
|
||||
#include <QColor>
|
||||
|
||||
// CaptureModification is a single modification in the screenshot drawn
|
||||
// by the user.
|
||||
|
||||
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);
|
||||
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::marker) {
|
||||
m_coords.append(p);
|
||||
}
|
||||
QSettings settings;
|
||||
m_color = settings.value("drawColor").value<QColor>();
|
||||
m_color = c;
|
||||
}
|
||||
|
||||
CaptureModification::CaptureModification() {
|
||||
|
||||
@@ -26,7 +26,7 @@ class QPoint;
|
||||
class CaptureModification {
|
||||
public:
|
||||
CaptureModification();
|
||||
CaptureModification(const Button::Type, const QPoint);
|
||||
CaptureModification(const Button::Type, const QPoint, const QColor);
|
||||
Button::Type getType() const;
|
||||
QColor getColor() const;
|
||||
QVector<QPoint> getPoints() const;
|
||||
|
||||
@@ -53,8 +53,8 @@ namespace {
|
||||
|
||||
CaptureWidget::CaptureWidget(QWidget *parent) :
|
||||
QWidget(parent), m_mouseOverHandle(0), m_mouseIsClicked(false),
|
||||
m_newSelection(false), m_grabbing(false), m_onButton(false),
|
||||
m_state(Button::Type::move) {
|
||||
m_rightClick(false), m_newSelection(false), m_grabbing(false),
|
||||
m_onButton(false), m_state(Button::Type::move) {
|
||||
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
// create selection handlers
|
||||
@@ -151,6 +151,8 @@ void CaptureWidget::paintEvent(QPaintEvent *) {
|
||||
|
||||
void CaptureWidget::mousePressEvent(QMouseEvent *e) {
|
||||
if (e->button() == Qt::RightButton) {
|
||||
m_rightClick = true;
|
||||
setCursor(Qt::ArrowCursor);
|
||||
m_colorPicker->move(e->pos().x()-m_colorPicker->width()/2,
|
||||
e->pos().y()-m_colorPicker->height()/2);
|
||||
m_colorPicker->show();
|
||||
@@ -160,7 +162,8 @@ void CaptureWidget::mousePressEvent(QMouseEvent *e) {
|
||||
if (e->button() == Qt::LeftButton) {
|
||||
m_mouseIsClicked = true;
|
||||
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;
|
||||
}
|
||||
m_dragStartPoint = e->pos();
|
||||
@@ -256,7 +259,10 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
|
||||
|
||||
if (!found) {
|
||||
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) {
|
||||
setCursor(Qt::OpenHandCursor);
|
||||
} else if (m_onButton) {
|
||||
@@ -283,6 +289,7 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
|
||||
void CaptureWidget::mouseReleaseEvent(QMouseEvent *e) {
|
||||
if (e->button() == Qt::RightButton) {
|
||||
m_colorPicker->hide();
|
||||
m_rightClick = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,7 @@ protected:
|
||||
QRect m_selectionBeforeDrag;
|
||||
// utility flags
|
||||
bool m_mouseIsClicked;
|
||||
bool m_rightClick;
|
||||
bool m_newSelection;
|
||||
bool m_grabbing;
|
||||
bool m_onButton;
|
||||
|
||||
@@ -1,17 +1,25 @@
|
||||
#include "colorpicker.h"
|
||||
#include "colorpicker.h"
|
||||
#include <QPainter>
|
||||
#include <QMouseEvent>
|
||||
#include <QSettings>
|
||||
#include <QDebug>
|
||||
|
||||
ColorPicker::ColorPicker(QWidget *parent) : QWidget(parent),
|
||||
m_colorAreaSize(18) {
|
||||
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);
|
||||
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 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) {
|
||||
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 *) {
|
||||
QPainter painter(this);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
@@ -28,11 +44,34 @@ void ColorPicker::paintEvent(QPaintEvent *) {
|
||||
QVector<QRect> rects = handleMask();
|
||||
painter.setPen(QColor(Qt::black));
|
||||
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.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> areas;
|
||||
for(QRect rect: m_colorAreaList) {
|
||||
@@ -48,9 +87,8 @@ QVector<Qt::GlobalColor> ColorPicker::colorList = {
|
||||
Qt::yellow,
|
||||
Qt::green,
|
||||
Qt::darkGreen,
|
||||
Qt::darkCyan,
|
||||
Qt::blue,
|
||||
Qt::cyan,
|
||||
Qt::blue,
|
||||
Qt::magenta,
|
||||
Qt::darkMagenta
|
||||
};
|
||||
|
||||
@@ -8,10 +8,15 @@ class ColorPicker : public QWidget
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ColorPicker(QWidget *parent = 0);
|
||||
~ColorPicker();
|
||||
static QVector<Qt::GlobalColor> colorList;
|
||||
|
||||
QColor getDrawColor();
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *);
|
||||
void mouseMoveEvent(QMouseEvent *);
|
||||
|
||||
QVector<QRect> handleMask() const;
|
||||
|
||||
signals:
|
||||
@@ -21,6 +26,8 @@ public slots:
|
||||
private:
|
||||
const int m_colorAreaSize;
|
||||
QVector<QRect> m_colorAreaList;
|
||||
|
||||
QColor m_uiColor, m_drawColor;
|
||||
};
|
||||
|
||||
#endif // COLORPICKER_H
|
||||
|
||||
Reference in New Issue
Block a user