1 /***************************************************************************
2  *   Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>                     *
3  *   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>            *
4  *   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>                     *
5  *   Copyright (C) 2006 David Saxton <david@bluehaze.org>                  *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  ***************************************************************************/
12 
13 #include "doublespinbox.h"
14 #include "iteminterface.h"
15 #include "propertyeditorinput.h"
16 #include "property.h"
17 
18 
19 #include <KLineEdit>
20 #include <KLocalizedString>
21 
22 #include <QDebug>
23 //#include <qiconset.h>
24 #include <QIcon>
25 #include <QToolButton>
26 #include <QKeyEvent>
27 
28 #include <limits.h>
29 
30 //BEGIN class PropertyEditorInput
PropertyEditorInput(QWidget * parent,Property * property,const char * name)31 PropertyEditorInput::PropertyEditorInput( QWidget * parent, Property * property, const char * name )
32 	: PropertySubEditor( parent, property, name )
33 {
34 	m_lineedit = new KLineEdit(this);
35 	m_lineedit->resize(width(), height());
36 
37 	m_lineedit->setText(property->value().toString());
38 	m_lineedit->show();
39 
40 	setWidget(m_lineedit);
41 
42 	connect( m_lineedit, SIGNAL(textChanged(const QString &)), this, SLOT(slotTextChanged(const QString &)));
43 	connect( m_property, SIGNAL(valueChanged( const QString& )), m_lineedit, SLOT(setText(const QString &)) );
44 }
45 
46 
slotTextChanged(const QString & text)47 void PropertyEditorInput::slotTextChanged( const QString & text )
48 {
49 	m_property->setValue( text );
50 	ItemInterface::self()->setProperty( m_property );
51 }
52 //END class PropertyEditorInput
53 
54 
55 
56 //BEGIN class PropIntSpinBox
PropIntSpinBox(int lower,int upper,int step,int value,int base=10,QWidget * parent=nullptr,const char * name=nullptr)57 PropIntSpinBox::PropIntSpinBox( int lower, int upper, int step, int value, int base=10, QWidget *parent = nullptr, const char *name = nullptr)
58 : QSpinBox(parent /*, name */)
59 {
60     setMinimum(lower);
61     setMaximum(upper);
62     setSingleStep(step);
63     setValue(value);
64     setDisplayIntegerBase(base);
65     setObjectName(name);
66 	lineEdit()->setAlignment(Qt::AlignLeft);
67 }
68 
69 
eventFilter(QObject * o,QEvent * e)70 bool PropIntSpinBox::eventFilter(QObject *o, QEvent *e)
71 {
72 	if(o == lineEdit())
73 	{
74 		if(e->type() == QEvent::KeyPress)
75 		{
76 			QKeyEvent* ev = static_cast<QKeyEvent*>(e);
77 			if((ev->key()==Qt::Key_Up || ev->key()==Qt::Key_Down) && ev->modifiers()!=Qt::ControlModifier)
78 			{
79 				parentWidget()->eventFilter(o, e);
80 				return true;
81 			}
82 		}
83 	}
84 
85 	return QSpinBox::eventFilter(o, e);
86 }
87 //END class PropIntSpinBox
88 
89 
90 
91 //BEGIN class PropertyEditorSpin
PropertyEditorSpin(QWidget * parent,Property * property,const char * name)92 PropertyEditorSpin::PropertyEditorSpin( QWidget * parent, Property * property, const char * name )
93  : PropertySubEditor(parent,property, name)
94 {
95 	m_leaveTheSpaceForRevertButton = true;
96 
97 	m_spinBox = new PropIntSpinBox( (int)property->minValue(), (int)property->maxValue(), 1, 0, 10, this );
98 
99 	m_spinBox->resize(width(), height());
100 	m_spinBox->setValue(property->value().toInt());
101 	m_spinBox->show();
102 
103 	setWidget( m_spinBox, m_spinBox->editor() );
104 	connect( m_spinBox, SIGNAL(valueChanged( int )), this, SLOT(valueChange( int )));
105 	connect( m_property, SIGNAL(valueChanged( int )), m_spinBox, SLOT(setValue( int )) );
106 }
107 
108 
valueChange(int value)109 void PropertyEditorSpin::valueChange( int value )
110 {
111 	m_property->setValue( value );
112 	ItemInterface::self()->setProperty( m_property );
113 }
114 //END class PropertyEditorSpin
115 
116 
117 
118 //BEGIN class PropDoubleSpinBox
PropDoubleSpinBox(double lower,double upper,double minAbs,double value,const QString & unit,QWidget * parent=nullptr)119 PropDoubleSpinBox::PropDoubleSpinBox(double lower, double upper, double minAbs, double value, const QString &unit, QWidget *parent = nullptr)
120 	: DoubleSpinBox( lower, upper, minAbs, value, unit, parent )
121 {
122 	lineEdit()->setAlignment(Qt::AlignLeft);
123 }
124 
125 
eventFilter(QObject * o,QEvent * e)126 bool PropDoubleSpinBox::eventFilter(QObject *o, QEvent *e)
127 {
128 	if(o == lineEdit())
129 	{
130 		if(e->type() == QEvent::KeyPress)
131 		{
132 			QKeyEvent* ev = static_cast<QKeyEvent*>(e);
133 			if((ev->key()==Qt::Key_Up || ev->key()==Qt::Key_Down) && ev->modifiers()!=Qt::ControlModifier)
134 			{
135 				parentWidget()->eventFilter(o, e);
136 				return true;
137 			}
138 		}
139 	}
140 
141 	return DoubleSpinBox::eventFilter(o, e);
142 }
143 //END class PropDoubleSpinBox
144 
145 
146 
147 //BEGIN class PropertyEditorDblSpin
PropertyEditorDblSpin(QWidget * parent,Property * property,const char * name)148 PropertyEditorDblSpin::PropertyEditorDblSpin( QWidget * parent, Property * property, const char * name )
149 	: PropertySubEditor( parent, property, name )
150 {
151 	m_leaveTheSpaceForRevertButton = true;
152 	m_spinBox = new PropDoubleSpinBox( property->minValue(), property->maxValue(), property->minAbsValue(), property->value().toDouble(), property->unit(), this );
153 	m_spinBox->resize(width(), height());
154 	m_spinBox->show();
155 
156 	setWidget( m_spinBox, m_spinBox->editor());
157 	connect( m_spinBox, SIGNAL(valueChanged(double)), this, SLOT(valueChange(double)));
158 	connect( m_property, SIGNAL(valueChanged( double )), m_spinBox, SLOT(setValue( double )) );
159 }
160 
161 
valueChange(double value)162 void PropertyEditorDblSpin::valueChange( double value )
163 {
164 	m_property->setValue( value );
165 	ItemInterface::self()->setProperty( m_property );
166 }
167 //END class PropertyEditorDblSpin
168 
169 
170 
171 //BEGIN class PropertyEditorBool
PropertyEditorBool(QWidget * parent,Property * property,const char * name)172 PropertyEditorBool::PropertyEditorBool( QWidget * parent, Property * property, const char * name )
173 	: PropertySubEditor( parent, property, name )
174 {
175 	m_toggle = new QToolButton(this);
176 	m_toggle->setFocusPolicy(Qt::NoFocus);
177 	m_toggle->setCheckable(true);
178 	m_toggle->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
179     // 2018.12.02: see above
180 	//m_toggle->setTextPosition(QToolButton::Right); //js BesideIcon -didnt work before qt3.2);
181 	m_toggle->resize(width(), height());
182 
183 	connect( m_toggle, SIGNAL(toggled(bool)), this, SLOT(setState(bool)));
184 	connect( m_property, SIGNAL(valueChanged( bool )), m_toggle, SLOT(setChecked(bool)) );
185 
186 	if(property->value().toBool())
187 		m_toggle->setChecked(true);
188 	else
189 	{
190 		m_toggle->toggle();
191 		m_toggle->setChecked(false);
192 	}
193 
194 	m_toggle->show();
195 	setWidget(m_toggle);
196 	installEventFilter(this);
197 }
198 
199 
eventFilter(QObject * watched,QEvent * e)200 bool PropertyEditorBool::eventFilter(QObject* watched, QEvent* e)
201 {
202 	if(e->type() == QEvent::KeyPress)
203 	{
204 		QKeyEvent* ev = static_cast<QKeyEvent*>(e);
205 		if(ev->key() == Qt::Key_Space)
206 		{
207 			m_toggle->toggle();
208 			return true;
209 		}
210 	}
211 	return PropertySubEditor::eventFilter(watched, e);
212 }
213 
214 
setState(bool state)215 void PropertyEditorBool::setState( bool state )
216 {
217 	if(state)
218 	{
219 		m_toggle->setIcon(QIcon::fromTheme("dialog-ok"));
220 		m_toggle->setToolTip(i18n("Yes"));
221 	}
222 	else
223 	{
224 		m_toggle->setIcon(QIcon::fromTheme("dialog-cancel"));
225 		m_toggle->setToolTip(i18n("No"));
226 	}
227 
228 	m_property->setValue( state );
229 	ItemInterface::self()->setProperty( m_property );
230 }
231 //END class PropertyEditorBool
232