From 721b14fc96dd46325567988648c4c628b1e81d9a Mon Sep 17 00:00:00 2001 From: Dearsh Oberoi <59907159+deo002@users.noreply.github.com> Date: Sun, 13 Feb 2022 05:34:20 +0530 Subject: [PATCH] Improve Colorpicker (#2403) * Fix * update preset functionality added * dynamic radius * added drag to swap * Refactor * Fix Bug (cherry picked from commit b4188f7ec8a35bedcda7bb06cd5f64168ae844bd) --- src/config/CMakeLists.txt | 1 + src/config/colorpickereditmode.cpp | 106 +++++++++++++++++++++++++++ src/config/colorpickereditmode.h | 28 +++++++ src/config/colorpickereditor.cpp | 114 ++++++++++++++++++++--------- src/config/colorpickereditor.h | 16 ++-- src/widgets/CMakeLists.txt | 2 - src/widgets/colorpickerwidget.cpp | 3 +- src/widgets/colorspinbox.cpp | 48 ------------ src/widgets/colorspinbox.h | 24 ------ 9 files changed, 226 insertions(+), 116 deletions(-) create mode 100644 src/config/colorpickereditmode.cpp create mode 100644 src/config/colorpickereditmode.h delete mode 100644 src/widgets/colorspinbox.cpp delete mode 100644 src/widgets/colorspinbox.h diff --git a/src/config/CMakeLists.txt b/src/config/CMakeLists.txt index f0ce1b635b..6e0c25070e 100644 --- a/src/config/CMakeLists.txt +++ b/src/config/CMakeLists.txt @@ -15,4 +15,5 @@ target_sources( visualseditor.cpp shortcutswidget.cpp setshortcutwidget.cpp + colorpickereditmode.cpp ) diff --git a/src/config/colorpickereditmode.cpp b/src/config/colorpickereditmode.cpp new file mode 100644 index 0000000000..3a39382b0a --- /dev/null +++ b/src/config/colorpickereditmode.cpp @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-FileCopyrightText: 2022 Dearsh Oberoi + +#include "colorpickereditmode.h" + +#include +#include + +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(obj); + + switch (event->type()) { + case QEvent::MouseButtonPress: { + auto mouseEvent = static_cast(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(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); +} diff --git a/src/config/colorpickereditmode.h b/src/config/colorpickereditmode.h new file mode 100644 index 0000000000..9d53e9c84a --- /dev/null +++ b/src/config/colorpickereditmode.h @@ -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; +}; diff --git a/src/config/colorpickereditor.cpp b/src/config/colorpickereditor.cpp index 756b7e38db..2af6c49198 100644 --- a/src/config/colorpickereditor.cpp +++ b/src/config/colorpickereditor.cpp @@ -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 #include @@ -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::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( @@ -100,18 +118,13 @@ ColorPickerEditor::ColorPickerEditor(QWidget* parent) void ColorPickerEditor::addPreset() { - ConfigHandler config; - QVector 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 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(); } \ No newline at end of file diff --git a/src/config/colorpickereditor.h b/src/config/colorpickereditor.h index b42a35b15d..6a20558c80 100644 --- a/src/config/colorpickereditor.h +++ b/src/config/colorpickereditor.h @@ -4,10 +4,11 @@ #pragma once #include "QtColorWidgets/color_wheel.hpp" +#include "src/utils/confighandler.h" + #include -class ColorSpinBox; -class ColorPickerWidget; +class ColorPickerEditMode; class QLabel; class QPushButton; class QLineEdit; @@ -23,17 +24,20 @@ 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; @@ -41,6 +45,8 @@ private slots: QColor m_color; int m_selectedIndex; + QVector m_colorList; + ConfigHandler m_config; QGridLayout* m_gLayout; }; diff --git a/src/widgets/CMakeLists.txt b/src/widgets/CMakeLists.txt index b848c624b3..4fb3afd620 100644 --- a/src/widgets/CMakeLists.txt +++ b/src/widgets/CMakeLists.txt @@ -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 ) diff --git a/src/widgets/colorpickerwidget.cpp b/src/widgets/colorpickerwidget.cpp index 072e884c08..c8db7220e5 100644 --- a/src/widgets/colorpickerwidget.cpp +++ b/src/widgets/colorpickerwidget.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, diff --git a/src/widgets/colorspinbox.cpp b/src/widgets/colorspinbox.cpp deleted file mode 100644 index 20ab5207ef..0000000000 --- a/src/widgets/colorspinbox.cpp +++ /dev/null @@ -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(); -} diff --git a/src/widgets/colorspinbox.h b/src/widgets/colorspinbox.h deleted file mode 100644 index 65adbe745d..0000000000 --- a/src/widgets/colorspinbox.h +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -// SPDX-FileCopyrightText: 2022 Dearsh Oberoi - -#pragma once - -#include -#include - -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 m_colorList; -}; \ No newline at end of file