summaryrefslogtreecommitdiffstats
path: root/src/sources/labelsource.cpp
blob: d4d218234585636042648c9d307e70760d14a16a (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
/***************************************************************************
 *   Copyright (C) 2007 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 "labelsource.h"
#include <tqlabel.h>
#include <kcolorbutton.h>
#include <kfontrequester.h>
#include <tqcombobox.h>
#include <tqcheckbox.h>
#include <klocale.h>

//#include "kdebug.h"

LabelSource::LabelSource(TQWidget* inParent):
		TriggeredSource(inParent),
		mParent(inParent),
		mLabelSourcePrefs(NULL){ 
}

LabelSource::~LabelSource(){
}

TQWidget* LabelSource::getWidget(){
	//kdDebug() << "LabelSource::getWidget called: " << mID << endl;
	return mLabel;
}

void LabelSource::createSubPrefs(TQWidget* inParent){
	if(!mLabelSourcePrefs){
		mLabelSourcePrefs = new LabelSourcePrefs(inParent, "labelsourceprefsui");
		// disable nameCheckBox if taskbarCheckBox is disabled
		connect(mSourcePrefs->taskbarCheckBox, TQT_SIGNAL(toggled(bool)), mLabelSourcePrefs->colorLabel, TQT_SLOT(setEnabled(bool)));
		connect(mSourcePrefs->taskbarCheckBox, TQT_SIGNAL(toggled(bool)), mLabelSourcePrefs->colorButton, TQT_SLOT(setEnabled(bool)));
		connect(mSourcePrefs->taskbarCheckBox, TQT_SIGNAL(toggled(bool)), mLabelSourcePrefs->fontLabel, TQT_SLOT(setEnabled(bool)));
		connect(mSourcePrefs->taskbarCheckBox, TQT_SIGNAL(toggled(bool)), mLabelSourcePrefs->fontRequester, TQT_SLOT(setEnabled(bool)));
		connect(mSourcePrefs->taskbarCheckBox, TQT_SIGNAL(toggled(bool)), mLabelSourcePrefs->alignmentLabel, TQT_SLOT(setEnabled(bool)));
		connect(mSourcePrefs->taskbarCheckBox, TQT_SIGNAL(toggled(bool)), mLabelSourcePrefs->alignmentComboBox, TQT_SLOT(setEnabled(bool)));
		addPrefs(mLabelSourcePrefs);
	}
}

void LabelSource::updatePrefsGUI(){
	TriggeredSource::updatePrefsGUI(); // update prefs of the super class
	mLabelSourcePrefs->colorButton->setColor(mLabel->paletteForegroundColor());
	mLabelSourcePrefs->fontRequester->setFont(mLabel->font());
	switch (mLabel->tqalignment()) {
		case TQt::AlignCenter:
			mLabelSourcePrefs->alignmentComboBox->setCurrentItem(1);
			break;
		case TQt::AlignRight:
			mLabelSourcePrefs->alignmentComboBox->setCurrentItem(2);
			break;
		default: // TQt::AlignLeft
			break;
			mLabelSourcePrefs->alignmentComboBox->setCurrentItem(0);
	}
}

void LabelSource::setPrefsWidgetsEnabled(bool isEnabled, bool isShownOnApplet){
	TriggeredSource::setPrefsWidgetsEnabled(isEnabled, isShownOnApplet);
	// disable/enable some widgets if source is disabled/enabled	if(isEnabled)
	mLabelSourcePrefs->colorLabel->setEnabled(isEnabled && isShownOnApplet);
	mLabelSourcePrefs->colorButton->setEnabled(isEnabled && isShownOnApplet);
	mLabelSourcePrefs->fontLabel->setEnabled(isEnabled && isShownOnApplet);
	mLabelSourcePrefs->fontRequester->setEnabled(isEnabled && isShownOnApplet);
	mLabelSourcePrefs->alignmentLabel->setEnabled(isEnabled && isShownOnApplet);
	mLabelSourcePrefs->alignmentComboBox->setEnabled(isEnabled && isShownOnApplet);
}

void LabelSource::applyPrefs(){
	TriggeredSource::applyPrefs(); // call apply prefs of the super class
	mLabel->setPaletteForegroundColor(mLabelSourcePrefs->colorButton->color());
	mLabel->setFont(mLabelSourcePrefs->fontRequester->font());
	int alignID = mLabelSourcePrefs->alignmentComboBox->currentItem();
	TQt::AlignmentFlags align = TQt::AlignCenter;
	if(alignID == 0){
		align = TQt::AlignLeft;
	}else if(alignID == 2){
		align = TQt::AlignRight;
	}
	mLabel->tqsetAlignment(align);
	updateLabel(mValue);
}

void LabelSource::savePrefs(KConfig* inKConfig){
	TriggeredSource::savePrefs(inKConfig);
	inKConfig->writeEntry(mID + "_color", mLabelSourcePrefs->colorButton->color());
	inKConfig->writeEntry(mID + "_font", mLabelSourcePrefs->fontRequester->font());
	inKConfig->writeEntry(mID + "_align", mLabel->tqalignment());
}

void LabelSource::loadPrefs(KConfig* inKConfig){
	TriggeredSource::loadPrefs(inKConfig);
	TQColor color = inKConfig->readColorEntry(mID + "_color");
	if(!color.isValid())
		color.setRgb(0,0,0);
	mLabel->setPaletteForegroundColor(color);
	mLabel->setFont(inKConfig->readFontEntry(mID + "_font"));
	mLabel->tqsetAlignment(inKConfig->readNumEntry(mID + "_align"));
}

void LabelSource::updateLabel(const TQString& inValue){
	// update the label text
	TQString text = (getName().isEmpty() || !showName()) ? inValue : getName() + ": " + inValue;
	//kdDebug() << "updateLabel " << getName() << ", value: " << text << endl;
	//if(mLabel->text() != text)
	mLabel->setText(text);
}

void LabelSource::realizeWidget(){
	Source::realizeWidget();
	// the prefs dialog is created in the addPrefs method
	mLabel = new TQLabel(i18n("n/a"), mParent);
    mLabel->setTextFormat(TQt::PlainText);
	connect(this, TQT_SIGNAL(valueUpdated(const TQString&)), this, TQT_SLOT(updateLabel(const TQString&)));
}