File indexing completed on 2024-05-19 04:36:36

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2014 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "OpacityEditor.h"
0021 
0022 #include <QApplication>
0023 #include <QHBoxLayout>
0024 #include <QSpinBox>
0025 #include <QSlider>
0026 
0027 namespace tikz {
0028 namespace ui {
0029 
0030 class OpacityEditorPrivate
0031 {
0032 public:
0033     QSpinBox * spinBox;
0034     QSlider * slider;
0035     int intOpacity = 100;
0036 };
0037 
0038 OpacityEditor::OpacityEditor(QWidget * parent)
0039     : QWidget(parent)
0040     , d(new OpacityEditorPrivate())
0041 {
0042     auto hboxLayout = new QHBoxLayout(this);
0043     hboxLayout->setContentsMargins(0, 0, 0, 0);
0044 
0045     d->spinBox = new QSpinBox(this);
0046     d->slider = new QSlider(Qt::Horizontal, this);
0047 
0048     hboxLayout->addWidget(d->spinBox);
0049     hboxLayout->addWidget(d->slider);
0050 
0051     d->spinBox->setFrame(false);
0052     d->spinBox->setKeyboardTracking(false);
0053     d->spinBox->setRange(0, 100);
0054     d->spinBox->setSingleStep(10);
0055     d->spinBox->setSuffix("%");
0056     d->spinBox->setValue(100);
0057 
0058     d->slider->setRange(0, 100);
0059     d->slider->setPageStep(10);
0060     d->slider->setSingleStep(10);
0061     d->slider->setValue(100);
0062     d->slider->setToolTip("Hold down shift for fine adjustment.");
0063 
0064     setFocusProxy(d->spinBox);
0065 
0066     connect(d->spinBox, SIGNAL(valueChanged(int)), this, SLOT(slotSpinBoxChanged(int)));
0067     connect(d->slider, SIGNAL(valueChanged(int)), this, SLOT(slotSliderChanged(int)));
0068 }
0069 
0070 OpacityEditor::~OpacityEditor()
0071 {
0072     delete d;
0073 }
0074 
0075 void OpacityEditor::setValue(qreal opacity)
0076 {
0077     Q_ASSERT(opacity >= 0);
0078     Q_ASSERT(opacity <= 1.0);
0079 
0080     const int value = static_cast<int>(qRound(opacity * 100.0));
0081 
0082     if (d->intOpacity == value) {
0083         return;
0084     }
0085 
0086     d->intOpacity = value;
0087 
0088     const bool blockSpinBox = d->spinBox->blockSignals(true);
0089     const bool blockSlider = d->slider->blockSignals(true);
0090 
0091     d->spinBox->setValue(value);
0092     d->slider->setValue(value);
0093 
0094     d->spinBox->blockSignals(blockSpinBox);
0095     d->slider->blockSignals(blockSlider);
0096 }
0097 
0098 qreal OpacityEditor::value() const
0099 {
0100     return d->intOpacity / 100.0;
0101 }
0102 
0103 void OpacityEditor::setReadOnly(bool readOnly)
0104 {
0105     d->spinBox->setReadOnly(readOnly);
0106     d->slider->setDisabled(readOnly);
0107 }
0108 
0109 void OpacityEditor::slotSliderChanged(int opacity)
0110 {
0111     const bool snap = QApplication::keyboardModifiers() ^ Qt::ShiftModifier;
0112 
0113     if (snap) {
0114         opacity = 10 * qRound(opacity / 10.0);
0115     }
0116 
0117     if (d->intOpacity != opacity) {
0118         setValue(opacity / 100.0);
0119         Q_EMIT valueChanged(d->intOpacity / 100.0);
0120     } else if (d->slider->value() != opacity) {
0121         const bool blockSlider = d->slider->blockSignals(true);
0122         d->slider->setValue(opacity);
0123         d->slider->blockSignals(blockSlider);
0124     }
0125 }
0126 
0127 void OpacityEditor::slotSpinBoxChanged(int opacity)
0128 {
0129     if (opacity != d->intOpacity) {
0130         setValue(opacity / 100.0);
0131         Q_EMIT valueChanged(d->intOpacity / 100.0);
0132     }
0133 }
0134 
0135 }
0136 }
0137 
0138 // kate: indent-width 4; replace-tabs on;