diff --git a/src/config/extendedslider.cpp b/src/config/extendedslider.cpp new file mode 100644 index 00000000..c6e9579c --- /dev/null +++ b/src/config/extendedslider.cpp @@ -0,0 +1,50 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#include "extendedslider.h" + +ExtendedSlider::ExtendedSlider(QWidget *parent) + : QSlider(parent) +{ + connect(this, &ExtendedSlider::valueChanged, + this, &ExtendedSlider::updateTooltip); + connect(this, &ExtendedSlider::sliderMoved, + this, &ExtendedSlider::fireTimer); + m_timer.setSingleShot(true); + connect(&m_timer, &QTimer::timeout, + this, &ExtendedSlider::modificationsEnded); +} + +int ExtendedSlider::mappedValue(int min, int max) { + qreal progress = + ((value() - minimum())) / static_cast(maximum() - minimum()); + return min + (max - min) * progress; +} + +void ExtendedSlider::setMapedValue(int min, int val, int max) { + qreal progress = ((val - min) + 1) / static_cast(max - min); + int value = minimum() + (maximum() - minimum()) * progress; + setValue(value); +} + +void ExtendedSlider::updateTooltip() { + setToolTip(QString::number(value())); +} + +void ExtendedSlider::fireTimer() { + m_timer.start(500); +} diff --git a/src/config/extendedslider.h b/src/config/extendedslider.h new file mode 100644 index 00000000..c08b6ea8 --- /dev/null +++ b/src/config/extendedslider.h @@ -0,0 +1,45 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#ifndef EXTENDEDSLIDER_H +#define EXTENDEDSLIDER_H + +#include +#include + +class ExtendedSlider : public QSlider +{ + Q_OBJECT +public: + explicit ExtendedSlider(QWidget *parent = nullptr); + + int mappedValue(int min, int max); + void setMapedValue(int min, int val, int max); + +signals: + void modificationsEnded(); + +private slots: + void updateTooltip(); + void fireTimer(); + +private: + QTimer m_timer; + +}; + +#endif // EXTENDEDSLIDER_H diff --git a/src/config/visualseditor.cpp b/src/config/visualseditor.cpp index 3a25c789..6ce2ef6d 100644 --- a/src/config/visualseditor.cpp +++ b/src/config/visualseditor.cpp @@ -1,8 +1,25 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + #include "visualseditor.h" #include "src/config/buttonlistview.h" #include "src/config/uicoloreditor.h" #include "src/utils/confighandler.h" -#include +#include "src/config/extendedslider.h" #include #include #include @@ -17,35 +34,37 @@ VisualsEditor::VisualsEditor(QWidget *parent) : QWidget(parent) void VisualsEditor::updateComponents() { m_buttonList->updateComponents(); m_colorEditor->updateComponents(); - m_opacity = ConfigHandler().contrastOpacityValue(); - m_opacitySlider->setValue(m_opacity); + int opacity = ConfigHandler().contrastOpacityValue(); + m_opacitySlider->setMapedValue(0, opacity, 255); } void VisualsEditor::initOpacitySlider() { - QLabel *label = new QLabel(tr("Opacity of area outside selection:")); - m_layout->addWidget(label); - - m_opacitySlider = new QSlider(Qt::Horizontal); - m_opacitySlider->setRange(0, 255); - connect(m_opacitySlider, &QSlider::sliderMoved, - this, &VisualsEditor::updateOpacity); - connect(m_opacitySlider, &QSlider::sliderReleased, + m_opacitySlider = new ExtendedSlider(); + m_opacitySlider->setOrientation(Qt::Horizontal); + m_opacitySlider->setRange(0, 100); + connect(m_opacitySlider, &ExtendedSlider::modificationsEnded, this, &VisualsEditor::saveOpacity); QHBoxLayout *localLayout = new QHBoxLayout(); localLayout->addWidget(new QLabel("0%")); localLayout->addWidget(m_opacitySlider); localLayout->addWidget(new QLabel("100%")); - m_opacity = ConfigHandler().contrastOpacityValue(); - m_opacitySlider->setValue(m_opacity); - m_layout->addLayout(localLayout); -} -void VisualsEditor::updateOpacity(int opacity) { - m_opacity = opacity; + QLabel *label = new QLabel(); + QString labelMsg = tr("Opacity of area outside selection:") + " %1"; + connect(m_opacitySlider, &ExtendedSlider::valueChanged, + this, [labelMsg, label](int val){ + label->setText(labelMsg.arg(val)); + }); + m_layout->addWidget(label); + m_layout->addLayout(localLayout); + + int opacity = ConfigHandler().contrastOpacityValue(); + m_opacitySlider->setMapedValue(0, opacity, 255); } void VisualsEditor::saveOpacity() { - ConfigHandler().setContrastOpacity(m_opacity); + int value = m_opacitySlider->mappedValue(0, 255); + ConfigHandler().setContrastOpacity(value); } void VisualsEditor::initWidgets() { @@ -66,4 +85,3 @@ void VisualsEditor::initWidgets() { m_buttonList, &ButtonListView::selectAll); listLayout->addWidget(setAllButtons); } - diff --git a/src/config/visualseditor.h b/src/config/visualseditor.h index 05007bc2..6eaa9cf6 100644 --- a/src/config/visualseditor.h +++ b/src/config/visualseditor.h @@ -3,7 +3,7 @@ #include -class QSlider; +class ExtendedSlider; class QVBoxLayout; class ButtonListView; class UIcolorEditor; @@ -18,15 +18,13 @@ public slots: void updateComponents(); private slots: - void updateOpacity(int); void saveOpacity(); private: - int m_opacity; // Slider local value QVBoxLayout *m_layout; ButtonListView *m_buttonList; UIcolorEditor *m_colorEditor; - QSlider *m_opacitySlider; + ExtendedSlider *m_opacitySlider; void initWidgets(); void initOpacitySlider();