File indexing completed on 2024-05-05 05:46:20

0001 /***************************************************************************
0002  *   Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>                     *
0003  *   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>            *
0004  *   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>                     *
0005  *   Copyright (C) 2006 David Saxton <david@bluehaze.org>                  *
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or modify  *
0008  *   it under the terms of the GNU General Public License as published by  *
0009  *   the Free Software Foundation; either version 2 of the License, or     *
0010  *   (at your option) any later version.                                   *
0011  ***************************************************************************/
0012 
0013 #include "propertyeditorinput.h"
0014 #include "doublespinbox.h"
0015 #include "iteminterface.h"
0016 #include "property.h"
0017 
0018 #include <KLineEdit>
0019 #include <KLocalizedString>
0020 
0021 #include <QDebug>
0022 //#include <qiconset.h>
0023 #include <QIcon>
0024 #include <QKeyEvent>
0025 #include <QToolButton>
0026 
0027 #include <limits.h>
0028 
0029 // BEGIN class PropertyEditorInput
0030 PropertyEditorInput::PropertyEditorInput(QWidget *parent, Property *property)
0031     : PropertySubEditor(parent, property)
0032 {
0033     m_lineedit = new KLineEdit(this);
0034     m_lineedit->resize(width(), height());
0035 
0036     m_lineedit->setText(property->value().toString());
0037     m_lineedit->show();
0038 
0039     setWidget(m_lineedit);
0040 
0041     connect(m_lineedit, &KLineEdit::textChanged, this, &PropertyEditorInput::slotTextChanged);
0042     connect(m_property, qOverload<const QString &>(&Property::valueChanged), m_lineedit, &KLineEdit::setText);
0043 }
0044 
0045 void PropertyEditorInput::slotTextChanged(const QString &text)
0046 {
0047     m_property->setValue(text);
0048     ItemInterface::self()->setProperty(m_property);
0049 }
0050 // END class PropertyEditorInput
0051 
0052 // BEGIN class PropIntSpinBox
0053 PropIntSpinBox::PropIntSpinBox(int lower, int upper, int step, int value, int base = 10, QWidget *parent = nullptr)
0054     : QSpinBox(parent)
0055 {
0056     setMinimum(lower);
0057     setMaximum(upper);
0058     setSingleStep(step);
0059     setValue(value);
0060     setDisplayIntegerBase(base);
0061     lineEdit()->setAlignment(Qt::AlignLeft);
0062 }
0063 
0064 bool PropIntSpinBox::eventFilter(QObject *o, QEvent *e)
0065 {
0066     if (o == lineEdit()) {
0067         if (e->type() == QEvent::KeyPress) {
0068             QKeyEvent *ev = static_cast<QKeyEvent *>(e);
0069             if ((ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) && ev->modifiers() != Qt::ControlModifier) {
0070                 parentWidget()->eventFilter(o, e);
0071                 return true;
0072             }
0073         }
0074     }
0075 
0076     return QSpinBox::eventFilter(o, e);
0077 }
0078 // END class PropIntSpinBox
0079 
0080 // BEGIN class PropertyEditorSpin
0081 PropertyEditorSpin::PropertyEditorSpin(QWidget *parent, Property *property)
0082     : PropertySubEditor(parent, property)
0083 {
0084     m_leaveTheSpaceForRevertButton = true;
0085 
0086     m_spinBox = new PropIntSpinBox(static_cast<int>(property->minValue()), static_cast<int>(property->maxValue()), 1, 0, 10, this);
0087 
0088     m_spinBox->resize(width(), height());
0089     m_spinBox->setValue(property->value().toInt());
0090     m_spinBox->show();
0091 
0092     setWidget(m_spinBox, m_spinBox->editor());
0093     connect(m_spinBox, qOverload<int>(&PropIntSpinBox::valueChanged), this, &PropertyEditorSpin::valueChange);
0094     connect(m_property, qOverload<int>(&Property::valueChanged), m_spinBox, &PropIntSpinBox::setValue);
0095 }
0096 
0097 void PropertyEditorSpin::valueChange(int value)
0098 {
0099     m_property->setValue(value);
0100     ItemInterface::self()->setProperty(m_property);
0101 }
0102 // END class PropertyEditorSpin
0103 
0104 // BEGIN class PropDoubleSpinBox
0105 PropDoubleSpinBox::PropDoubleSpinBox(double lower, double upper, double minAbs, double value, const QString &unit, QWidget *parent = nullptr)
0106     : DoubleSpinBox(lower, upper, minAbs, value, unit, parent)
0107 {
0108     lineEdit()->setAlignment(Qt::AlignLeft);
0109 }
0110 
0111 bool PropDoubleSpinBox::eventFilter(QObject *o, QEvent *e)
0112 {
0113     if (o == lineEdit()) {
0114         if (e->type() == QEvent::KeyPress) {
0115             QKeyEvent *ev = static_cast<QKeyEvent *>(e);
0116             if ((ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) && ev->modifiers() != Qt::ControlModifier) {
0117                 parentWidget()->eventFilter(o, e);
0118                 return true;
0119             }
0120         }
0121     }
0122 
0123     return DoubleSpinBox::eventFilter(o, e);
0124 }
0125 // END class PropDoubleSpinBox
0126 
0127 // BEGIN class PropertyEditorDblSpin
0128 PropertyEditorDblSpin::PropertyEditorDblSpin(QWidget *parent, Property *property)
0129     : PropertySubEditor(parent, property)
0130 {
0131     m_leaveTheSpaceForRevertButton = true;
0132     m_spinBox = new PropDoubleSpinBox(property->minValue(), property->maxValue(), property->minAbsValue(), property->value().toDouble(), property->unit(), this);
0133     m_spinBox->resize(width(), height());
0134     m_spinBox->show();
0135 
0136     setWidget(m_spinBox, m_spinBox->editor());
0137     connect(m_spinBox, qOverload<double>(&PropDoubleSpinBox::valueChanged), this, &PropertyEditorDblSpin::valueChange);
0138     connect(m_property, qOverload<double>(&Property::valueChanged), m_spinBox, &PropDoubleSpinBox::setValue);
0139 }
0140 
0141 void PropertyEditorDblSpin::valueChange(double value)
0142 {
0143     m_property->setValue(value);
0144     ItemInterface::self()->setProperty(m_property);
0145 }
0146 // END class PropertyEditorDblSpin
0147 
0148 // BEGIN class PropertyEditorBool
0149 PropertyEditorBool::PropertyEditorBool(QWidget *parent, Property *property)
0150     : PropertySubEditor(parent, property)
0151 {
0152     m_toggle = new QToolButton(this);
0153     m_toggle->setFocusPolicy(Qt::NoFocus);
0154     m_toggle->setCheckable(true);
0155     m_toggle->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
0156     // 2018.12.02: see above
0157     // m_toggle->setTextPosition(QToolButton::Right); //js BesideIcon -didnt work before qt3.2);
0158     m_toggle->resize(width(), height());
0159 
0160     connect(m_toggle, &QToolButton::toggled, this, &PropertyEditorBool::setState);
0161     connect(m_property, qOverload<bool>(&Property::valueChanged), m_toggle, &QToolButton::setChecked);
0162 
0163     if (property->value().toBool())
0164         m_toggle->setChecked(true);
0165     else {
0166         m_toggle->toggle();
0167         m_toggle->setChecked(false);
0168     }
0169 
0170     m_toggle->show();
0171     setWidget(m_toggle);
0172     installEventFilter(this);
0173 }
0174 
0175 bool PropertyEditorBool::eventFilter(QObject *watched, QEvent *e)
0176 {
0177     if (e->type() == QEvent::KeyPress) {
0178         QKeyEvent *ev = static_cast<QKeyEvent *>(e);
0179         if (ev->key() == Qt::Key_Space) {
0180             m_toggle->toggle();
0181             return true;
0182         }
0183     }
0184     return PropertySubEditor::eventFilter(watched, e);
0185 }
0186 
0187 void PropertyEditorBool::setState(bool state)
0188 {
0189     if (state) {
0190         m_toggle->setIcon(QIcon::fromTheme("dialog-ok"));
0191         m_toggle->setToolTip(i18n("Yes"));
0192     } else {
0193         m_toggle->setIcon(QIcon::fromTheme("dialog-cancel"));
0194         m_toggle->setToolTip(i18n("No"));
0195     }
0196 
0197     m_property->setValue(state);
0198     ItemInterface::self()->setProperty(m_property);
0199 }
0200 // END class PropertyEditorBool
0201 
0202 #include "moc_propertyeditorinput.cpp"