summaryrefslogtreecommitdiffstats
path: root/src/variant.cpp
blob: 10422cd02b97cfa99dfb224c69b50a892f2df2eb (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/***************************************************************************
 *   Copyright (C) 2003-2005 by David Saxton                               *
 *   [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 "colorcombo.h"
#include "cnitem.h"

#include <cmath>
#include <klocale.h>

Variant::Variant( Type::Value type )
	: QObject()
{
	m_type = type;
	m_bSetDefault = false;
	m_bHidden = false;
	m_bAdvanced = false; 
	m_minValue = 1e-6;
	m_maxValue = 1e9;
	m_minAbsValue = 1e-6;
	m_colorScheme = ColorCombo::QtStandard;
	if ( type == Type::Color )
	{
		// this value is taken from ColorCombo and should ideally be put somewhere...
		m_defaultValue = "#f62a2a";
		m_value = "#f62a2a";
	}
}


Variant::~Variant()
{
}


void Variant::setType( Type::Value type )
{
	m_type = type;
}


void Variant::appendAllowed(QString string)
{
	if ( !m_allowed.contains(string) ) {
		m_allowed.append(string);
	}
}


void Variant::setAllowed(QStringList stringList)
{
	// Ideally, we should check for duplicates in whatever is setting the
	// allowed strings, but it is a lot easier and permanent to do it here
	m_allowed.clear();
	const QStringList::iterator end = stringList.end();
	for ( QStringList::iterator it = stringList.begin(); it != end; ++it )
	{
		if ( !m_allowed.contains(*it) ) {
			m_allowed.append(*it);
		}
	}
}


void Variant::setMinValue( const double value )
{
	m_minValue = value;
	if ( std::abs(value) < m_minAbsValue && value != 0.  )
	{
		m_minAbsValue = std::abs(value);
	}
}

void Variant::setMaxValue( const double value )
{
	m_maxValue = value;
	if ( std::abs(value) < m_minAbsValue && value != 0. )
	{
		m_minAbsValue = std::abs(value);
	}
}

QString Variant::displayString() const
{
	switch(type())
	{
		case Variant::Type::Double:
		{
			double numValue = m_value.toDouble();
			return QString::number( numValue / CNItem::getMultiplier(numValue) ) + " " + CNItem::getNumberMag(numValue) + m_unit;
		}
		
		case Variant::Type::Int:
			return m_value.toString()+" "+m_unit;
			
		case Variant::Type::Bool:
			return m_value.toBool() ? i18n("True") : i18n("False");
			
		default:
			return m_value.toString();
	}
}

void Variant::setValue( const QVariant& val )
{
	if (!m_bSetDefault)
		setDefaultValue(val);
	
	if ( m_value == val )
		return;
	
	const QVariant old = m_value;
	m_value = val;
	emit( valueChanged( val, old ) );
}

void Variant::setDefaultValue( QVariant val )
{
	m_defaultValue = val;
	m_bSetDefault = true;
}

void Variant::resetToDefault()
{
	setValue( defaultValue() );
}

void Variant::setMinAbsValue( double val )
{
	m_minAbsValue = val;
}

#include "variant.moc"