Skip to content

Commit

Permalink
Customizable ColorPicker (flameshot-org#2202)
Browse files Browse the repository at this point in the history
* Added spinbox, refactored colorpicker

* Added add preset functionality

* Added delete preset

* Refactored code

* Fix

(cherry picked from commit 259e438)
  • Loading branch information
deo002 authored and Yuriy Puchkov committed May 13, 2022
1 parent 92880ab commit 9c9730f
Show file tree
Hide file tree
Showing 19 changed files with 563 additions and 142 deletions.
5 changes: 4 additions & 1 deletion flameshot.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
;; The colors are arranged counter-clockwise with the first being set to the right of the cursor
;; Colors are any valid hex code or W3C color name
;; "picker" adds a custom color picker
;userColors=#800000, #ff0000, #ffff00, #00ff00, #008000, #00ffff, #0000ff, #ff00ff, #800080, picker
;userColors=picker, #800000, #ff0000, #ffff00, #00ff00, #008000, #00ffff, #0000ff, #ff00ff, #800080
;
;; Image Save Path
;savePath=/tmp
Expand Down Expand Up @@ -84,6 +84,9 @@
;; Upload to imgur without confirmation (bool)
;uploadWithoutConfirmation=false
;
;; Use larger color palette as the default one
; predefinedColorPaletteLarge=false
;
;; Shortcut Settings for all tools
;[Shortcuts]
;TYPE_ARROW=A
Expand Down
1 change: 1 addition & 0 deletions src/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ target_sources(
strftimechooserwidget.cpp
styleoverride.cpp
uicoloreditor.cpp
colorpickereditor.cpp
visualseditor.cpp
shortcutswidget.cpp
setshortcutwidget.cpp
Expand Down
166 changes: 166 additions & 0 deletions src/config/colorpickereditor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2022 Dearsh Oberoi

#include "colorpickereditor.h"
#include "src/utils/confighandler.h"
#include "src/utils/globalvalues.h"
#include "src/widgets/colorpickerwidget.h"
#include "src/widgets/colorspinbox.h"

#include <QApplication>
#include <QColor>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QString>
#include <QVector>

ColorPickerEditor::ColorPickerEditor(QWidget* parent)
: QWidget(parent)
, m_selectedIndex(1)
{
ConfigHandler config;
m_color = config.drawColor();

m_gLayout = new QGridLayout(this);

m_colorpicker = new ColorPickerWidget(this);
m_gLayout->addWidget(m_colorpicker, 0, 0);

m_colorWheel = new color_widgets::ColorWheel(this);
m_colorWheel->setColor(m_color);
const int size = GlobalValues::buttonBaseSize() * 3.5;
m_colorWheel->setMinimumSize(size, size);
m_gLayout->addWidget(m_colorWheel, 1, 0);

QVBoxLayout* m_vLocalLayout1 = new QVBoxLayout();
m_vLocalLayout1->addStretch();

m_colorSpinboxLabel = new QLabel(tr("Select Preset:"), this);
m_vLocalLayout1->addWidget(m_colorSpinboxLabel);

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_colorSpinbox->setToolTip(tr("Select preset using the spinbox"));
m_vLocalLayout1->addWidget(m_colorSpinbox);

m_deletePresetButton = new QPushButton(tr("Delete"), this);
m_deletePresetButton->setToolTip(
tr("Press button to delete the selected preset"));
connect(m_deletePresetButton,
&QPushButton::pressed,
this,
&ColorPickerEditor::onDeletePreset);
m_vLocalLayout1->addWidget(m_deletePresetButton);

m_vLocalLayout1->addStretch();

m_gLayout->addLayout(m_vLocalLayout1, 0, 1);

QVBoxLayout* m_vLocalLayout2 = new QVBoxLayout();
m_vLocalLayout2->addStretch();

m_addPresetLabel = new QLabel(tr("Add Preset:"), this);
m_vLocalLayout2->addWidget(m_addPresetLabel);

m_colorInput = new QLineEdit(this);
m_colorInput->setText(m_color.name(QColor::HexRgb));
m_colorInput->setToolTip(
tr("Enter color manually or select it using the color-wheel"));
connect(m_colorWheel,
&color_widgets::ColorWheel::colorSelected,
this,
[=](QColor c) {
m_color = c;
m_colorInput->setText(m_color.name(QColor::HexRgb));
});
m_vLocalLayout2->addWidget(m_colorInput);

m_addPresetButton = new QPushButton(tr("Add"), this);
m_addPresetButton->setToolTip(tr("Press button to add preset"));
connect(m_addPresetButton,
&QPushButton::pressed,
this,
&ColorPickerEditor::onAddPreset);
m_vLocalLayout2->addWidget(m_addPresetButton);

m_vLocalLayout2->addStretch();

m_gLayout->addLayout(m_vLocalLayout2, 1, 1);
}

void ColorPickerEditor::addPreset()
{
ConfigHandler config;
QVector<QColor> colors = config.userColors();

if (colors.contains(m_color))
return;

colors << m_color;

const int maxPresetsAllowed = 17;

if (colors.size() > maxPresetsAllowed) {
QMessageBox::critical(
this,
tr("Error"),
tr("Unable to add preset. Maximum limit reached."));
return;
}

config.setUserColors(colors);
}

void ColorPickerEditor::deletePreset()
{
ConfigHandler config;
QVector<QColor> colors = config.userColors();

colors.remove(m_selectedIndex);

const int minPresetsAllowed = 3;

if (colors.size() < minPresetsAllowed) {
QMessageBox::critical(
this,
tr("Error"),
tr("Unable to remove preset. Minimum limit reached."));
return;
}

config.setUserColors(colors);
}

void ColorPickerEditor::onAddPreset()
{
if (QColor::isValidColor(m_colorInput->text())) {
m_color = QColor(m_colorInput->text());
m_colorInput->setText(m_color.name(QColor::HexRgb));
} else {
m_colorInput->setText(m_color.name(QColor::HexRgb));
return;
}

addPreset();
m_colorSpinbox->setValue(1);
m_colorpicker->updateWidget();
m_colorSpinbox->updateWidget();
}

void ColorPickerEditor::onDeletePreset()
{
deletePreset();
m_colorSpinbox->setValue(1);
m_colorpicker->updateWidget();
m_colorSpinbox->updateWidget();
}
46 changes: 46 additions & 0 deletions src/config/colorpickereditor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2022 Dearsh Oberoi

#pragma once

#include "QtColorWidgets/color_wheel.hpp"
#include <QWidget>

class ColorSpinBox;
class ColorPickerWidget;
class QLabel;
class QPushButton;
class QLineEdit;
class QColor;
class QGridLayout;

class ColorPickerEditor : public QWidget
{
Q_OBJECT
public:
explicit ColorPickerEditor(QWidget* parent = nullptr);

private slots:
void onAddPreset();
void onDeletePreset();

private:
void addPreset();
void deletePreset();

ColorPickerWidget* m_colorpicker;
color_widgets::ColorWheel* m_colorWheel;

QLabel* m_colorSpinboxLabel;
ColorSpinBox* m_colorSpinbox;
QPushButton* m_deletePresetButton;

QLineEdit* m_colorInput;
QLabel* m_addPresetLabel;
QPushButton* m_addPresetButton;

QColor m_color;
int m_selectedIndex;

QGridLayout* m_gLayout;
};
2 changes: 2 additions & 0 deletions src/config/generalconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ void GeneralConf::_updateComponents(bool allowEmptySavePath)
m_autoCloseIdleDaemon->setChecked(config.autoCloseIdleDaemon());
#endif

m_predefinedColorPaletteLarge->setChecked(
config.predefinedColorPaletteLarge());
m_showStartupLaunchMessage->setChecked(config.showStartupLaunchMessage());
m_screenshotPathFixedCheck->setChecked(config.savePathFixed());
m_uploadHistoryMax->setValue(config.uploadHistoryMax());
Expand Down
3 changes: 1 addition & 2 deletions src/config/uicoloreditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
#include <QVBoxLayout>

UIcolorEditor::UIcolorEditor(QWidget* parent)
: QGroupBox(parent)
: QWidget(parent)
{
setTitle(tr("UI Color Editor"));
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_hLayout = new QHBoxLayout;
m_vLayout = new QVBoxLayout;
Expand Down
2 changes: 1 addition & 1 deletion src/config/uicoloreditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class QHBoxLayout;
class CaptureToolButton;
class ClickableLabel;

class UIcolorEditor : public QGroupBox
class UIcolorEditor : public QWidget
{
Q_OBJECT
public:
Expand Down
17 changes: 16 additions & 1 deletion src/config/visualseditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "visualseditor.h"
#include "src/config/buttonlistview.h"
#include "src/config/colorpickereditor.h"
#include "src/config/extendedslider.h"
#include "src/config/uicoloreditor.h"
#include "src/utils/confighandler.h"
Expand Down Expand Up @@ -56,8 +57,22 @@ void VisualsEditor::initOpacitySlider()

void VisualsEditor::initWidgets()
{
m_tabWidget = new QTabWidget();
m_layout->addWidget(m_tabWidget);

m_colorEditor = new UIcolorEditor();
m_layout->addWidget(m_colorEditor);
m_colorEditorTab = new QWidget();
QVBoxLayout* colorEditorLayout = new QVBoxLayout(m_colorEditorTab);
m_colorEditorTab->setLayout(colorEditorLayout);
colorEditorLayout->addWidget(m_colorEditor);
m_tabWidget->addTab(m_colorEditorTab, tr("UI Color Editor"));

m_colorpickerEditor = new ColorPickerEditor();
m_colorpickerEditorTab = new QWidget();
QVBoxLayout* colorpickerEditorLayout =
new QVBoxLayout(m_colorpickerEditorTab);
colorpickerEditorLayout->addWidget(m_colorpickerEditor);
m_tabWidget->addTab(m_colorpickerEditorTab, tr("Colorpicker Editor"));

initOpacitySlider();

Expand Down
12 changes: 11 additions & 1 deletion src/config/visualseditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

#pragma once

#include <QTabWidget>
#include <QWidget>

class ExtendedSlider;
class QVBoxLayout;
class ButtonListView;
class UIcolorEditor;
class ColorPickerEditor;

class VisualsEditor : public QWidget
{
Expand All @@ -21,8 +23,16 @@ public slots:

private:
QVBoxLayout* m_layout;
ButtonListView* m_buttonList;

QTabWidget* m_tabWidget;

UIcolorEditor* m_colorEditor;
QWidget* m_colorEditorTab;

ColorPickerEditor* m_colorpickerEditor;
QWidget* m_colorpickerEditorTab;

ButtonListView* m_buttonList;
ExtendedSlider* m_opacitySlider;

void initWidgets();
Expand Down
2 changes: 1 addition & 1 deletion src/utils/confighandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
OPTION("drawThickness" ,LowerBoundedInt (1 , 3 )),
OPTION("drawFontSize" ,LowerBoundedInt (1 , 8 )),
OPTION("drawColor" ,Color ( Qt::red )),
OPTION("userColors" ,UserColors ( )),
OPTION("userColors" ,UserColors(3, 17 )),
OPTION("ignoreUpdateToVersion" ,String ( "" )),
OPTION("keepOpenAppLauncher" ,Bool ( false )),
OPTION("fontFamily" ,String ( "" )),
Expand Down
Loading

0 comments on commit 9c9730f

Please sign in to comment.