File indexing completed on 2024-04-21 04:52:31

0001 /*
0002     SPDX-FileCopyrightText: 2010 Till Theato <root@ttill.de>
0003 
0004 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "doublewidget.h"
0008 #include "dragvalue.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QHBoxLayout>
0012 #include <QToolButton>
0013 
0014 DoubleWidget::DoubleWidget(const QString &name, double value, double min, double max, double factor, double defaultValue, const QString &comment, int id,
0015                            const QString &suffix, int decimals, bool oddOnly, QWidget *parent)
0016     : QWidget(parent)
0017     , m_factor(factor)
0018 {
0019     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
0020     auto *layout = new QHBoxLayout(this);
0021     layout->setContentsMargins(0, 0, 0, 0);
0022     layout->setSpacing(0);
0023     m_dragVal = new DragValue(name, defaultValue * m_factor, decimals, min, max, id, suffix, true, oddOnly, this);
0024     if (suffix == QStringLiteral("°")) {
0025         QToolButton *rotationTb = new QToolButton(this);
0026         rotationTb->setIcon(QIcon::fromTheme("object-rotate-right"));
0027         rotationTb->setToolTip(i18n("Rotate by 90 ° steps"));
0028         rotationTb->setAutoRaise(true);
0029         layout->addWidget(rotationTb);
0030         connect(rotationTb, &QToolButton::clicked, this, [this, min, max]() {
0031             int val = m_dragVal->value() / 90;
0032             val = (val + 1) * 90;
0033             if (val > max) {
0034                 val = min;
0035             }
0036             m_dragVal->setValue(val);
0037         });
0038     }
0039     layout->addWidget(m_dragVal);
0040     setMinimumHeight(m_dragVal->height());
0041 
0042     if (!comment.isEmpty()) {
0043         setToolTip(comment);
0044     }
0045     m_dragVal->setValue(value * factor, false);
0046     connect(m_dragVal, &DragValue::valueChanged, this, &DoubleWidget::slotSetValue);
0047 }
0048 
0049 void DoubleWidget::setDragObjectName(const QString &name)
0050 {
0051     m_dragVal->setObjectName(name);
0052 }
0053 
0054 bool DoubleWidget::hasEditFocus() const
0055 {
0056     return m_dragVal->hasEditFocus();
0057 }
0058 
0059 DoubleWidget::~DoubleWidget()
0060 {
0061     delete m_dragVal;
0062 }
0063 
0064 int DoubleWidget::spinSize()
0065 {
0066     return m_dragVal->spinSize();
0067 }
0068 
0069 void DoubleWidget::setSpinSize(int width)
0070 {
0071     m_dragVal->setSpinSize(width);
0072 }
0073 
0074 void DoubleWidget::setValue(double value)
0075 {
0076     QSignalBlocker bk(m_dragVal);
0077     m_dragVal->setValue(value * m_factor);
0078 }
0079 
0080 void DoubleWidget::enableEdit(bool enable)
0081 {
0082     m_dragVal->setEnabled(enable);
0083 }
0084 
0085 void DoubleWidget::slotSetValue(double value, bool final)
0086 {
0087     if (final) {
0088         Q_EMIT valueChanged(value / m_factor);
0089     }
0090 }
0091 
0092 double DoubleWidget::getValue()
0093 {
0094     return m_dragVal->value();
0095 }
0096 
0097 void DoubleWidget::slotReset()
0098 {
0099     m_dragVal->slotReset();
0100 }
0101 
0102 void DoubleWidget::slotShowComment(bool show)
0103 {
0104     Q_UNUSED(show)
0105 }