mirror of
https://github.com/fergalmoran/flameshot.git
synced 2026-02-10 20:44:00 +00:00
Improve Colorpicker (#2403)
* Fix * update preset functionality added * dynamic radius * added drag to swap * Refactor * Fix Bug
This commit is contained in:
@@ -15,4 +15,5 @@ target_sources(
|
||||
visualseditor.cpp
|
||||
shortcutswidget.cpp
|
||||
setshortcutwidget.cpp
|
||||
colorpickereditmode.cpp
|
||||
)
|
||||
|
||||
106
src/config/colorpickereditmode.cpp
Normal file
106
src/config/colorpickereditmode.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2022 Dearsh Oberoi
|
||||
|
||||
#include "colorpickereditmode.h"
|
||||
|
||||
#include <QMouseEvent>
|
||||
#include <QPainter>
|
||||
|
||||
ColorPickerEditMode::ColorPickerEditMode(QWidget* parent)
|
||||
: ColorPickerWidget(parent)
|
||||
{
|
||||
m_isPressing = false;
|
||||
m_isDragging = false;
|
||||
installEventFilter(this);
|
||||
}
|
||||
|
||||
bool ColorPickerEditMode::eventFilter(QObject* obj, QEvent* event)
|
||||
{
|
||||
auto widget = static_cast<QWidget*>(obj);
|
||||
|
||||
switch (event->type()) {
|
||||
case QEvent::MouseButtonPress: {
|
||||
auto mouseEvent = static_cast<QMouseEvent*>(event);
|
||||
|
||||
if (mouseEvent->button() == Qt::LeftButton) {
|
||||
m_mousePressPos = mouseEvent->pos();
|
||||
m_mouseMovePos = m_mousePressPos;
|
||||
|
||||
for (int i = 1; i < m_colorList.size(); ++i) {
|
||||
if (m_colorAreaList.at(i).contains(m_mousePressPos)) {
|
||||
m_isPressing = true;
|
||||
m_draggedPresetInitialPos =
|
||||
m_colorAreaList[i].topLeft();
|
||||
m_selectedIndex = i;
|
||||
update(m_colorAreaList.at(i) +
|
||||
QMargins(10, 10, 10, 10));
|
||||
update(m_colorAreaList.at(m_lastIndex) +
|
||||
QMargins(10, 10, 10, 10));
|
||||
m_lastIndex = i;
|
||||
emit colorSelected(m_selectedIndex);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} break;
|
||||
case QEvent::MouseMove: {
|
||||
auto mouseEvent = static_cast<QMouseEvent*>(event);
|
||||
|
||||
if (m_isPressing) {
|
||||
QPoint eventPos = mouseEvent->pos();
|
||||
QPoint diff = eventPos - m_mouseMovePos;
|
||||
m_colorAreaList[m_selectedIndex].translate(diff);
|
||||
widget->update();
|
||||
|
||||
if (!m_isDragging) {
|
||||
QPoint totalMovedDiff = eventPos - m_mousePressPos;
|
||||
if (totalMovedDiff.manhattanLength() > 3) {
|
||||
m_isDragging = true;
|
||||
}
|
||||
}
|
||||
|
||||
m_mouseMovePos = eventPos;
|
||||
}
|
||||
} break;
|
||||
case QEvent::MouseButtonRelease: {
|
||||
m_isPressing = false;
|
||||
if (m_isDragging) {
|
||||
QPoint draggedPresetCenter =
|
||||
m_colorAreaList[m_selectedIndex].center();
|
||||
m_isDragging = false;
|
||||
|
||||
bool swapped = false;
|
||||
|
||||
for (int i = 1; i < m_colorList.size(); ++i) {
|
||||
if (i != m_selectedIndex &&
|
||||
m_colorAreaList.at(i).contains(draggedPresetCenter)) {
|
||||
// swap colors
|
||||
QColor temp = m_colorList[i];
|
||||
m_colorList[i] = m_colorList[m_selectedIndex];
|
||||
m_colorList[m_selectedIndex] = temp;
|
||||
m_config.setUserColors(m_colorList);
|
||||
|
||||
m_colorAreaList[m_selectedIndex].moveTo(
|
||||
m_draggedPresetInitialPos);
|
||||
m_selectedIndex = i;
|
||||
widget->update();
|
||||
m_lastIndex = i;
|
||||
emit presetsSwapped(m_selectedIndex);
|
||||
swapped = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!swapped) {
|
||||
m_colorAreaList[m_selectedIndex].moveTo(
|
||||
m_draggedPresetInitialPos);
|
||||
widget->update();
|
||||
}
|
||||
}
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return QObject::eventFilter(obj, event);
|
||||
}
|
||||
28
src/config/colorpickereditmode.h
Normal file
28
src/config/colorpickereditmode.h
Normal file
@@ -0,0 +1,28 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2022 Dearsh Oberoi
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "src/utils/confighandler.h"
|
||||
#include "src/widgets/colorpickerwidget.h"
|
||||
|
||||
class ColorPickerEditMode : public ColorPickerWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ColorPickerEditMode(QWidget* parent = nullptr);
|
||||
|
||||
signals:
|
||||
void colorSelected(int index);
|
||||
void presetsSwapped(int index);
|
||||
|
||||
private:
|
||||
bool eventFilter(QObject* obj, QEvent* event) override;
|
||||
|
||||
bool m_isPressing = false;
|
||||
bool m_isDragging = false;
|
||||
QPoint m_mouseMovePos;
|
||||
QPoint m_mousePressPos;
|
||||
QPoint m_draggedPresetInitialPos;
|
||||
ConfigHandler m_config;
|
||||
};
|
||||
@@ -2,10 +2,8 @@
|
||||
// SPDX-FileCopyrightText: 2022 Dearsh Oberoi
|
||||
|
||||
#include "colorpickereditor.h"
|
||||
#include "src/utils/confighandler.h"
|
||||
#include "colorpickereditmode.h"
|
||||
#include "src/utils/globalvalues.h"
|
||||
#include "src/widgets/colorpickerwidget.h"
|
||||
#include "src/widgets/colorspinbox.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QColor>
|
||||
@@ -22,12 +20,12 @@ ColorPickerEditor::ColorPickerEditor(QWidget* parent)
|
||||
: QWidget(parent)
|
||||
, m_selectedIndex(1)
|
||||
{
|
||||
ConfigHandler config;
|
||||
m_color = config.drawColor();
|
||||
m_color = m_config.drawColor();
|
||||
m_colorList = m_config.userColors();
|
||||
|
||||
m_gLayout = new QGridLayout(this);
|
||||
|
||||
m_colorpicker = new ColorPickerWidget(this);
|
||||
m_colorpicker = new ColorPickerEditMode(this);
|
||||
m_gLayout->addWidget(m_colorpicker, 0, 0);
|
||||
|
||||
m_colorWheel = new color_widgets::ColorWheel(this);
|
||||
@@ -39,19 +37,39 @@ ColorPickerEditor::ColorPickerEditor(QWidget* parent)
|
||||
auto* m_vLocalLayout1 = new QVBoxLayout();
|
||||
m_vLocalLayout1->addStretch();
|
||||
|
||||
m_colorSpinboxLabel = new QLabel(tr("Select Preset:"), this);
|
||||
m_vLocalLayout1->addWidget(m_colorSpinboxLabel);
|
||||
m_colorEditLabel = new QLabel(tr("Edit Preset:"), this);
|
||||
m_vLocalLayout1->addWidget(m_colorEditLabel);
|
||||
|
||||
m_colorSpinbox = new ColorSpinBox(this);
|
||||
connect(m_colorSpinbox,
|
||||
QOverload<int>::of(&QSpinBox::valueChanged),
|
||||
m_colorpicker,
|
||||
[=](int val) {
|
||||
m_selectedIndex = val;
|
||||
m_colorpicker->updateSelection(val);
|
||||
m_colorEdit = new QLineEdit(this);
|
||||
m_colorEdit->setText(m_colorList[m_selectedIndex].name(QColor::HexRgb));
|
||||
m_colorEdit->setToolTip(tr("Enter color to update preset"));
|
||||
connect(m_colorpicker,
|
||||
&ColorPickerEditMode::colorSelected,
|
||||
this,
|
||||
[this](int index) {
|
||||
m_selectedIndex = index;
|
||||
m_colorEdit->setText(
|
||||
m_colorList[m_selectedIndex].name(QColor::HexRgb));
|
||||
});
|
||||
m_colorSpinbox->setToolTip(tr("Select preset using the spinbox"));
|
||||
m_vLocalLayout1->addWidget(m_colorSpinbox);
|
||||
connect(m_colorpicker,
|
||||
&ColorPickerEditMode::presetsSwapped,
|
||||
this,
|
||||
[this](int index) {
|
||||
m_selectedIndex = index;
|
||||
m_colorList = m_config.userColors();
|
||||
m_colorEdit->setText(
|
||||
m_colorList[m_selectedIndex].name(QColor::HexRgb));
|
||||
});
|
||||
m_vLocalLayout1->addWidget(m_colorEdit);
|
||||
|
||||
m_updatePresetButton = new QPushButton(tr("Update"), this);
|
||||
m_updatePresetButton->setToolTip(
|
||||
tr("Press button to update the selected preset"));
|
||||
connect(m_updatePresetButton,
|
||||
&QPushButton::pressed,
|
||||
this,
|
||||
&ColorPickerEditor::onUpdatePreset);
|
||||
m_vLocalLayout1->addWidget(m_updatePresetButton);
|
||||
|
||||
m_deletePresetButton = new QPushButton(tr("Delete"), this);
|
||||
m_deletePresetButton->setToolTip(
|
||||
@@ -100,18 +118,13 @@ ColorPickerEditor::ColorPickerEditor(QWidget* parent)
|
||||
|
||||
void ColorPickerEditor::addPreset()
|
||||
{
|
||||
ConfigHandler config;
|
||||
QVector<QColor> colors = config.userColors();
|
||||
|
||||
if (colors.contains(m_color)) {
|
||||
if (m_colorList.contains(m_color)) {
|
||||
return;
|
||||
}
|
||||
|
||||
colors << m_color;
|
||||
|
||||
const int maxPresetsAllowed = 17;
|
||||
|
||||
if (colors.size() > maxPresetsAllowed) {
|
||||
if (m_colorList.size() >= maxPresetsAllowed) {
|
||||
QMessageBox::critical(
|
||||
this,
|
||||
tr("Error"),
|
||||
@@ -119,19 +132,16 @@ void ColorPickerEditor::addPreset()
|
||||
return;
|
||||
}
|
||||
|
||||
config.setUserColors(colors);
|
||||
m_colorList << m_color;
|
||||
|
||||
m_config.setUserColors(m_colorList);
|
||||
}
|
||||
|
||||
void ColorPickerEditor::deletePreset()
|
||||
{
|
||||
ConfigHandler config;
|
||||
QVector<QColor> colors = config.userColors();
|
||||
|
||||
colors.remove(m_selectedIndex);
|
||||
|
||||
const int minPresetsAllowed = 3;
|
||||
|
||||
if (colors.size() < minPresetsAllowed) {
|
||||
if (m_colorList.size() <= minPresetsAllowed) {
|
||||
QMessageBox::critical(
|
||||
this,
|
||||
tr("Error"),
|
||||
@@ -139,7 +149,23 @@ void ColorPickerEditor::deletePreset()
|
||||
return;
|
||||
}
|
||||
|
||||
config.setUserColors(colors);
|
||||
m_colorList.remove(m_selectedIndex);
|
||||
|
||||
m_config.setUserColors(m_colorList);
|
||||
}
|
||||
|
||||
void ColorPickerEditor::updatePreset()
|
||||
{
|
||||
QColor c = QColor(m_colorEdit->text());
|
||||
|
||||
if (m_colorList.contains(c)) {
|
||||
m_colorEdit->setText(m_colorList[m_selectedIndex].name(QColor::HexRgb));
|
||||
return;
|
||||
}
|
||||
|
||||
m_colorList[m_selectedIndex] = c;
|
||||
|
||||
m_config.setUserColors(m_colorList);
|
||||
}
|
||||
|
||||
void ColorPickerEditor::onAddPreset()
|
||||
@@ -153,15 +179,31 @@ void ColorPickerEditor::onAddPreset()
|
||||
}
|
||||
|
||||
addPreset();
|
||||
m_colorSpinbox->setValue(1);
|
||||
m_colorpicker->updateWidget();
|
||||
m_colorSpinbox->updateWidget();
|
||||
m_selectedIndex = 1;
|
||||
m_colorpicker->updateSelection(m_selectedIndex);
|
||||
m_colorEdit->setText(m_colorList[m_selectedIndex].name(QColor::HexRgb));
|
||||
}
|
||||
|
||||
void ColorPickerEditor::onDeletePreset()
|
||||
{
|
||||
deletePreset();
|
||||
m_colorSpinbox->setValue(1);
|
||||
m_colorpicker->updateWidget();
|
||||
m_colorSpinbox->updateWidget();
|
||||
m_selectedIndex = 1;
|
||||
m_colorpicker->updateSelection(m_selectedIndex);
|
||||
m_colorEdit->setText(m_colorList[m_selectedIndex].name(QColor::HexRgb));
|
||||
}
|
||||
|
||||
void ColorPickerEditor::onUpdatePreset()
|
||||
{
|
||||
if (QColor::isValidColor(m_colorEdit->text())) {
|
||||
QColor c = QColor(m_colorEdit->text());
|
||||
m_colorEdit->setText(c.name(QColor::HexRgb));
|
||||
} else {
|
||||
m_colorEdit->setText(m_colorList[m_selectedIndex].name(QColor::HexRgb));
|
||||
return;
|
||||
}
|
||||
|
||||
updatePreset();
|
||||
m_colorpicker->updateWidget();
|
||||
}
|
||||
@@ -4,10 +4,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "QtColorWidgets/color_wheel.hpp"
|
||||
#include "src/utils/confighandler.h"
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class ColorSpinBox;
|
||||
class ColorPickerWidget;
|
||||
class ColorPickerEditMode;
|
||||
class QLabel;
|
||||
class QPushButton;
|
||||
class QLineEdit;
|
||||
@@ -23,17 +24,20 @@ public:
|
||||
private slots:
|
||||
void onAddPreset();
|
||||
void onDeletePreset();
|
||||
void onUpdatePreset();
|
||||
|
||||
private:
|
||||
void addPreset();
|
||||
void deletePreset();
|
||||
void updatePreset();
|
||||
|
||||
ColorPickerWidget* m_colorpicker;
|
||||
ColorPickerEditMode* m_colorpicker;
|
||||
color_widgets::ColorWheel* m_colorWheel;
|
||||
|
||||
QLabel* m_colorSpinboxLabel;
|
||||
ColorSpinBox* m_colorSpinbox;
|
||||
QLabel* m_colorEditLabel;
|
||||
QLineEdit* m_colorEdit;
|
||||
QPushButton* m_deletePresetButton;
|
||||
QPushButton* m_updatePresetButton;
|
||||
|
||||
QLineEdit* m_colorInput;
|
||||
QLabel* m_addPresetLabel;
|
||||
@@ -41,6 +45,8 @@ private:
|
||||
|
||||
QColor m_color;
|
||||
int m_selectedIndex;
|
||||
QVector<QColor> m_colorList;
|
||||
ConfigHandler m_config;
|
||||
|
||||
QGridLayout* m_gLayout;
|
||||
};
|
||||
|
||||
@@ -21,7 +21,6 @@ target_sources(
|
||||
uploadlineitem.h
|
||||
updatenotificationwidget.h
|
||||
colorpickerwidget.h
|
||||
colorspinbox.h
|
||||
imguploaddialog.h
|
||||
capture/capturetoolobjects.h
|
||||
)
|
||||
@@ -39,7 +38,6 @@ target_sources(
|
||||
uploadlineitem.cpp
|
||||
updatenotificationwidget.cpp
|
||||
colorpickerwidget.cpp
|
||||
colorspinbox.cpp
|
||||
imguploaddialog.cpp
|
||||
capture/capturetoolobjects.cpp
|
||||
)
|
||||
|
||||
@@ -122,7 +122,8 @@ void ColorPickerWidget::initColorPicker()
|
||||
// extraSize represents the extra space needed for the highlight of the
|
||||
// selected color.
|
||||
const int extraSize = 6;
|
||||
double radius = GlobalValues::buttonBaseSize() * 2;
|
||||
const double slope = 3;
|
||||
double radius = slope * m_colorList.size() + GlobalValues::buttonBaseSize();
|
||||
setMinimumSize(radius * 2 + m_colorAreaSize + extraSize,
|
||||
radius * 2 + m_colorAreaSize + extraSize);
|
||||
resize(radius * 2 + m_colorAreaSize + extraSize,
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2022 Dearsh Oberoi
|
||||
|
||||
#include "src/widgets/colorspinbox.h"
|
||||
#include "src/utils/confighandler.h"
|
||||
|
||||
ColorSpinBox::ColorSpinBox(QWidget* parent)
|
||||
: QSpinBox(parent)
|
||||
{
|
||||
initColorSpinbox();
|
||||
}
|
||||
|
||||
int ColorSpinBox::valueFromText(const QString& text) const
|
||||
{
|
||||
if (!QColor::isValidColor(text)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const QColor color = QColor(text);
|
||||
|
||||
for (int i = 1; i < m_colorList.size(); ++i) {
|
||||
if (m_colorList.at(i) == color) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
QString ColorSpinBox::textFromValue(int value) const
|
||||
{
|
||||
return m_colorList[value].name(QColor::HexRgb);
|
||||
}
|
||||
|
||||
void ColorSpinBox::initColorSpinbox()
|
||||
{
|
||||
ConfigHandler config;
|
||||
m_colorList = config.userColors();
|
||||
|
||||
setRange(1, m_colorList.size() - 1);
|
||||
setWrapping(true);
|
||||
}
|
||||
|
||||
void ColorSpinBox::updateWidget()
|
||||
{
|
||||
initColorSpinbox();
|
||||
update();
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2022 Dearsh Oberoi
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QColor>
|
||||
#include <QSpinBox>
|
||||
|
||||
class ColorSpinBox : public QSpinBox
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ColorSpinBox(QWidget* parent = nullptr);
|
||||
void updateWidget();
|
||||
|
||||
protected:
|
||||
int valueFromText(const QString& text) const override;
|
||||
QString textFromValue(int value) const override;
|
||||
|
||||
private:
|
||||
void initColorSpinbox();
|
||||
|
||||
QVector<QColor> m_colorList;
|
||||
};
|
||||
Reference in New Issue
Block a user