summaryrefslogtreecommitdiffstats
path: root/krecipes/src/widgets/fractioninput.cpp
blob: c6cd18680ee70669d65988847dcd05aafb30b403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/***************************************************************************
*   Copyright (C) 2003 by                                                 *
*   Jason Kivlighn ([email protected])                                  *
*                                                                         *
*   This program 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 2 of the License, or     *
*   (at your option) any later version.                                   *
***************************************************************************/

#include "fractioninput.h"

#include <ntqtimer.h>

#include <tdeglobalsettings.h>

#include "datablocks/ingredient.h"

FractionInput::FractionInput( TQWidget *parent, MixedNumber::Format format ) : KLineEdit( parent ),
	m_allowRange(false),
	m_validateTimer(new TQTimer(this)),
	m_format(format)
{
	setAlignment( TQt::AlignRight );

	connect( this, SIGNAL(textChanged(const TQString&)), this, SLOT(slotStartValidateTimer()) );
	connect( m_validateTimer, SIGNAL(timeout()), this, SLOT(validate()) );
}

FractionInput::~FractionInput()
{
	delete m_validateTimer;
}

void FractionInput::setValue( double d, double amount_offset )
{
	MixedNumber m( d );
	setValue( m, amount_offset );
}

void FractionInput::setValue( const MixedNumber &m, double amount_offset )
{
	TQString text = m.toString( m_format );
	if ( amount_offset > 0 ) {
		text += "-" + MixedNumber(m+amount_offset).toString( MixedNumber::MixedNumberFormat );
	}
	setText(text);
}

void FractionInput::value( MixedNumber &amount, double &amount_offset ) const
{
	Ingredient i; i.setAmount( text() );

	amount = MixedNumber(i.amount);
	amount_offset = i.amount_offset;
}

void FractionInput::value( double &amount, double &amount_offset ) const
{
	Ingredient i; i.setAmount( text() );

	amount = i.amount;
	amount_offset = i.amount_offset;
}

MixedNumber FractionInput::value() const
{
	Ingredient i; i.setAmount( text() );

	return MixedNumber(i.amount);
}

MixedNumber FractionInput::minValue() const
{
	Ingredient i; i.setAmount( text() );

	return MixedNumber(i.amount);
}

MixedNumber FractionInput::maxValue() const
{
	Ingredient i; i.setAmount( text() );

	return MixedNumber(i.amount_offset+i.amount);
}

bool FractionInput::isInputValid() const
{
	if ( !m_allowRange && text().contains("-") )
		return false;

	bool ok;
	Ingredient i; i.setAmount( text(), &ok );

	return ok;
}

void FractionInput::slotStartValidateTimer()
{
	if ( !m_validateTimer->isActive() )
		m_validateTimer->start( 1000, true );

	if ( isInputValid() )
		emit valueChanged( value() );
}

void FractionInput::validate()
{
	if ( isInputValid() ) {
		setPaletteForegroundColor( TDEGlobalSettings::textColor() );
	}
	else
		setPaletteForegroundColor( TQt::red );
}

bool FractionInput::isEmpty() const
{
	return text().isEmpty();
}

#include "fractioninput.moc"