Show opacity slider value

This commit is contained in:
lupoDharkael
2018-01-12 17:07:57 +01:00
parent 45538387f8
commit 5f96f148d5
4 changed files with 134 additions and 23 deletions

View File

@@ -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 <http://www.gnu.org/licenses/>.
#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<qreal>(maximum() - minimum());
return min + (max - min) * progress;
}
void ExtendedSlider::setMapedValue(int min, int val, int max) {
qreal progress = ((val - min) + 1) / static_cast<qreal>(max - min);
int value = minimum() + (maximum() - minimum()) * progress;
setValue(value);
}
void ExtendedSlider::updateTooltip() {
setToolTip(QString::number(value()));
}
void ExtendedSlider::fireTimer() {
m_timer.start(500);
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
#ifndef EXTENDEDSLIDER_H
#define EXTENDEDSLIDER_H
#include <QSlider>
#include <QTimer>
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

View File

@@ -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 <http://www.gnu.org/licenses/>.
#include "visualseditor.h"
#include "src/config/buttonlistview.h"
#include "src/config/uicoloreditor.h"
#include "src/utils/confighandler.h"
#include <QSlider>
#include "src/config/extendedslider.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
@@ -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);
}

View File

@@ -3,7 +3,7 @@
#include <QWidget>
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();