summaryrefslogtreecommitdiffstats
path: root/src/sources/source.cpp
blob: 89d0e9b8ed2bc4bf82b3c853a75f9eca16206892 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***************************************************************************
 *   Copyright (C) 2006 by Ken Werner                                      *
 *   [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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "source.h"
#include <math.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <klocale.h>
#include <kglobal.h>

//#include "kdebug.h"

Source::Source(TQWidget* inParent):	
		mID(""),
		mPosition(0),
		mName(""),
		mDescription(""),
		mEnabled(true),
		mMaybeEnabled(true),
		mShowOnApplet(true),
		mMaybeShowOnApplet(true),
		mShowName(true),
		mToolTipEnabled(true),
		mSourcePrefs(0) {
}

Source::~Source(){
}

const TQString& Source::getID() const{
	return mID;
}


int Source::getPosition() const{
	return mPosition;
}

void Source::setPosition(int inPosition, TDEConfig* inTDEConfig){
	mPosition = inPosition;
	inTDEConfig->writeEntry(mID + "_position", mPosition);
}

const TQString& Source::getName() const{
	return mName;
}

const TQString& Source::getDescription() const{
	return mDescription;
}

bool Source::isMetric() const{
	return mIsMetric;
}

bool Source::isEnabled() const{
	return mEnabled;
}

bool Source::showOnApplet() const{
	return mShowOnApplet;
}

bool Source::showName() const{
	return mShowName;
}

bool Source::isToolTipEnabled() const{
	return mToolTipEnabled;
}

void Source::setMaybeEnabled(bool inMaybeEnabled){
	if(inMaybeEnabled != mMaybeEnabled){
		mMaybeEnabled = inMaybeEnabled;
		// disable/enable some widgets if source is disabled/enabled
		setPrefsWidgetsEnabled(mMaybeEnabled, mSourcePrefs->taskbarCheckBox->isChecked());
	}
}

TQWidget* Source::createPrefs(TQWidget* inParent){
	if(!mSourcePrefs){
		mSourcePrefs = new SourcePrefs(inParent, "sourceprefsui");

		// disable nameCheckBox if taskbarCheckBox is disabled
		connect(mSourcePrefs->taskbarCheckBox, TQT_SIGNAL(toggled(bool)), mSourcePrefs->nameCheckBox, TQT_SLOT(setEnabled(bool)));
		
		// add prefs widgets from sub classes
		createSubPrefs(mSourcePrefs);
			
		// add bottom vspacer
		mSourcePrefs->layout()->addItem(new TQSpacerItem(0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding) );

		updatePrefsGUI(); // fill the prefs gui
	}
	return mSourcePrefs;
}

SourcePrefs* Source::getPrefs(){
	return mSourcePrefs;
}

void Source::updatePrefsGUI(){
	// set values
	mSourcePrefs->nameLineEdit->setText(mName);
	mSourcePrefs->descriptionLabel->setText(mDescription);
	mSourcePrefs->taskbarCheckBox->setChecked(mShowOnApplet);
	mSourcePrefs->nameCheckBox->setChecked(mShowName);
	mSourcePrefs->tooltipCheckBox->setChecked(mToolTipEnabled);

	// disable/enable some widgets if source is disabled/enabled
	setPrefsWidgetsEnabled(mEnabled, mShowOnApplet);
}

void Source::setPrefsWidgetsEnabled(bool isEnabled, bool isShownOnApplet){
	mSourcePrefs->nameLabel->setEnabled(isEnabled);
	mSourcePrefs->nameLineEdit->setEnabled(isEnabled);
	mSourcePrefs->taskbarCheckBox->setEnabled(isEnabled);
	mSourcePrefs->nameCheckBox->setEnabled(isEnabled && isShownOnApplet);
	mSourcePrefs->tooltipCheckBox->setEnabled(isEnabled);
}

void Source::addPrefs(TQWidget* inParent){
	if(inParent != NULL)
		mSourcePrefs->layout()->add(inParent);
}

void Source::applyPrefs(){
	mMaybeShowOnApplet = mSourcePrefs->taskbarCheckBox->isChecked();
	mShowName = mSourcePrefs->nameCheckBox->isChecked();
	mName = mSourcePrefs->nameLineEdit->text();
	mToolTipEnabled = mSourcePrefs->tooltipCheckBox->isChecked();

	//kdDebug() << "Source::applyPrefs() mEnabled: " << mEnabled << ", mMaybeEnabled: " << mMaybeEnabled << endl;
	if(mEnabled != mMaybeEnabled){
		mEnabled = mMaybeEnabled;
		//kdDebug() << "Source::applyPrefs() emit enabledChanged: " << mEnabled << endl;
		emit enabledChanged(mEnabled, this);
        // force hide/show on kicker. if the user just 
        // disabled/enabled the source, we want to show / hide
        // the source too, also if the "show on kicker" property
        // did not changed. so, force this here.
        mShowOnApplet = !mMaybeShowOnApplet;
	}

    if(!mEnabled)
        emit displaySource(false, this);
    else if(mMaybeShowOnApplet != mShowOnApplet) {
        emit displaySource(mMaybeShowOnApplet, this);
    }
    mShowOnApplet = mMaybeShowOnApplet;
}

void Source::savePrefs(TDEConfig* inTDEConfig){
	inTDEConfig->writeEntry(mID + "_position", mPosition);
	inTDEConfig->writeEntry(mID + "_enabled", mEnabled);
	inTDEConfig->writeEntry(mID + "_showOnApplet", mShowOnApplet);
	inTDEConfig->writeEntry(mID + "_showName", mShowName);
	inTDEConfig->writeEntry(mID + "_name", mName);
	inTDEConfig->writeEntry(mID + "_toolTipEnabled", mToolTipEnabled);
}

void Source::loadPrefs(TDEConfig* inTDEConfig){
	mPosition = inTDEConfig->readNumEntry(mID + "_position", mPosition);
	mEnabled = inTDEConfig->readBoolEntry(mID + "_enabled", mEnabled);
	mMaybeEnabled = mEnabled;
	mShowOnApplet = inTDEConfig->readBoolEntry(mID + "_showOnApplet", mShowOnApplet);
	mMaybeShowOnApplet = mShowOnApplet;
	mShowName = inTDEConfig->readBoolEntry(mID + "_showName", mShowName);
	mName = inTDEConfig->readEntry(mID + "_name", mName);
	mToolTipEnabled = inTDEConfig->readBoolEntry(mID + "_toolTipEnabled", mToolTipEnabled);

	// initializing
	// this signal is usually catched by the ThreadedTrigger who enables or disables the fetch loop
	emit enabledChanged(mEnabled, this);
}

// utility methods
TQString Source::formatTemperature(const TQString& temp) const {
	if(mIsMetric) {
		return temp + TQString::fromUtf8(" °C");
	} else {
		return TQString::number(celsiusToFahrenheit(temp.toInt())).append(TQString::fromUtf8(" °F"));
	}
}

TQString Source::KHzinHumanReadable( uint value ) const{
	if( value >= 1000000 )
		return TQString::number( round(value/1000000.0, 1) ) + " GHz";
	else if( value >= 1000 )
		return TQString::number( round(value/1000.0, -1) ) + " MHz";
	else
		return TQString::number( value ) + " KHz";
}
double Source::round(double inValue, int inDigits) const{
    return floor(inValue * pow( 10, inDigits) + 0.5) * pow(10, -inDigits);
}
int Source::celsiusToFahrenheit(int inCelsius) const{
	return tqRound(1.8 * inCelsius + 32);
}

void Source::realizeWidget(){
	mIsMetric = TDEGlobal::locale()->measureSystem() == KLocale::Metric;
}