Skip to content

Commit

Permalink
Improve Colorpicker (flameshot-org#2403)
Browse files Browse the repository at this point in the history
* Fix

* update preset functionality added

* dynamic radius

* added drag to swap

* Refactor

* Fix Bug

(cherry picked from commit b4188f7)
  • Loading branch information
deo002 authored and Yuriy Puchkov committed May 13, 2022
1 parent bb951c5 commit 721b14f
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 116 deletions.
1 change: 1 addition & 0 deletions src/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ target_sources(
visualseditor.cpp
shortcutswidget.cpp
setshortcutwidget.cpp
colorpickereditmode.cpp
)
106 changes: 106 additions & 0 deletions src/config/colorpickereditmode.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 28 additions & 0 deletions src/config/colorpickereditmode.h
Original file line number Diff line number Diff line change
@@ -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;
};
114 changes: 78 additions & 36 deletions src/config/colorpickereditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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);
Expand All @@ -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));
});
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_colorSpinbox->setToolTip(tr("Select preset using the spinbox"));
m_vLocalLayout1->addWidget(m_colorSpinbox);
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(
Expand Down Expand Up @@ -100,46 +118,54 @@ 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"),
tr("Unable to add preset. Maximum limit reached."));
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"),
tr("Unable to remove preset. Minimum limit reached."));
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()
Expand All @@ -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();
}
16 changes: 11 additions & 5 deletions src/config/colorpickereditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,24 +24,29 @@ class ColorPickerEditor : public QWidget
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;
QPushButton* m_addPresetButton;

QColor m_color;
int m_selectedIndex;
QVector<QColor> m_colorList;
ConfigHandler m_config;

QGridLayout* m_gLayout;
};
2 changes: 0 additions & 2 deletions src/widgets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ target_sources(
uploadlineitem.h
updatenotificationwidget.h
colorpickerwidget.h
colorspinbox.h
imguploaddialog.h
capture/capturetoolobjects.h
)
Expand All @@ -39,7 +38,6 @@ target_sources(
uploadlineitem.cpp
updatenotificationwidget.cpp
colorpickerwidget.cpp
colorspinbox.cpp
imguploaddialog.cpp
capture/capturetoolobjects.cpp
)
Loading

0 comments on commit 721b14f

Please sign in to comment.