summaryrefslogtreecommitdiffstats
path: root/kcoloredit/texteditselection.cpp
blob: 4b1d6a56362c7f0c852adc73a54746c6bee7d706 (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
/***************************************************************************
                          texteditselection.cpp  -  description
                             -------------------
    begin                : Wed Jul 12 2000
    copyright            : (C) 2000 by Artur Rataj
    email                : art@zeus.polsl.gliwice.pl
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <tqlabel.h>
#include <tqvalidator.h>
#include <tqcolor.h>

#include <klocale.h>

#include "main.h"
#include "texteditselection.h"

TextEditSelection::TextEditSelection(TQWidget *parent, const char *name ) : TQWidget(parent,name) {
	inChangingComponents = false;
	TQVBoxLayout* topLayout = new TQVBoxLayout(this, 4);
	TQGridLayout* componentsLayout = new TQGridLayout(3, 5, 2);
	topLayout->addLayout(componentsLayout);
	componentsLayout->setColStretch(1, 10);
	componentsLayout->addColSpacing(2, 8);
	componentsLayout->setColStretch(4, 10);
	TQLineEdit* lineEdit;
	addComponent(H_INDEX, ( lineEdit = new TQLineEdit(this) ), HSV_MAX_H_VALUE, "H:", 0, 0, componentsLayout);
	connect(lineEdit, TQT_SIGNAL( textChanged(const TQString&) ), TQT_SLOT( slotHsvComponentChanged() ));
	addComponent(S_INDEX, ( lineEdit = new TQLineEdit(this) ), HSV_MAX_S_VALUE, "S:", 1, 0, componentsLayout);
	connect(lineEdit, TQT_SIGNAL( textChanged(const TQString&) ), TQT_SLOT( slotHsvComponentChanged() ));
	addComponent(V_INDEX, ( lineEdit = new TQLineEdit(this) ), HSV_MAX_V_VALUE, "V:", 2, 0, componentsLayout);
	connect(lineEdit, TQT_SIGNAL( textChanged(const TQString&) ), TQT_SLOT( slotHsvComponentChanged() ));
	addComponent(R_INDEX, ( lineEdit = new TQLineEdit(this) ), RGB_MAX_COMPONENT_VALUE, "R:", 0, 1, componentsLayout);
	connect(lineEdit, TQT_SIGNAL( textChanged(const TQString&) ), TQT_SLOT( slotRgbComponentChanged() ));
	addComponent(G_INDEX, ( lineEdit = new TQLineEdit(this) ), RGB_MAX_COMPONENT_VALUE, "G:", 1, 1, componentsLayout);
	connect(lineEdit, TQT_SIGNAL( textChanged(const TQString&) ), TQT_SLOT( slotRgbComponentChanged() ));
	addComponent(B_INDEX, ( lineEdit = new TQLineEdit(this) ), RGB_MAX_COMPONENT_VALUE, "B:", 2, 1, componentsLayout);
	connect(lineEdit, TQT_SIGNAL( textChanged(const TQString&) ), TQT_SLOT( slotRgbComponentChanged() ));
	TQHBoxLayout* rgbStringLayout = new TQHBoxLayout(2);
	TQLabel* rgbStringLabel = new TQLabel("RGB " + i18n( "hex." ) + ": ", this);
	rgbStringLayout->addWidget(rgbStringLabel);
	rgbStringLineEdit = new TQLineEdit(this);
	rgbStringLineEdit->setMinimumWidth(lineEdit->fontMetrics().width( TQString("8888888") ));
	rgbStringLineEdit->setMaximumWidth(lineEdit->fontMetrics().width( TQString("888888888") ));
	connect(rgbStringLineEdit, TQT_SIGNAL( textChanged(const TQString&) ), TQT_SLOT( slotRgbStringChanged() ));
	rgbStringLayout->addWidget(rgbStringLineEdit);
	topLayout->addLayout(rgbStringLayout);
}
TextEditSelection::~TextEditSelection(){
}

void TextEditSelection::addComponent(const int index, TQLineEdit* lineEdit, const int maxValue,
	const TQString& labelString, const int row, const int column, TQGridLayout* layout) {
	TQLabel* label = new TQLabel(labelString, this);
	lineEdit->setValidator(new TQIntValidator( 0, maxValue, lineEdit ));
	lineEditTable[index] = lineEdit;
	lineEdit->setMinimumWidth(lineEdit->fontMetrics().width( TQString("8888") ));
	lineEdit->setMaximumWidth(lineEdit->fontMetrics().width( TQString("8888888") ));
	layout->addWidget(label, row, column*3);
	layout->addWidget(lineEdit, row, column*3 + 1);
}

void TextEditSelection::setRgbString(const int red, const int green, const int blue) {
	TQString string;
	string.sprintf("%02x%02x%02x", red, green, blue);
	rgbStringLineEdit->setText(string);
}

void TextEditSelection::slotHsvComponentChanged() {
	if(!inChangingComponents) {
		inChangingComponents = true;
		int hComponent = lineEditTable[H_INDEX]->text().toInt();
		int sComponent = lineEditTable[S_INDEX]->text().toInt();
		int vComponent = lineEditTable[V_INDEX]->text().toInt();
		TQColor color;
		color.setHsv(hComponent, sComponent, vComponent);
		int rComponent = color.red();
		int gComponent = color.green();
		int bComponent = color.blue();
		TQString string;
		lineEditTable[R_INDEX]->setText(string.setNum( rComponent ));
		lineEditTable[G_INDEX]->setText(string.setNum( gComponent ));
		lineEditTable[B_INDEX]->setText(string.setNum( bComponent ));
		Color oldColor = this->color;
		this->color.setComponent(Color::RED_INDEX, rComponent);
		this->color.setComponent(Color::GREEN_INDEX, gComponent);
		this->color.setComponent(Color::BLUE_INDEX, bComponent);
		if(!this->color.equals( oldColor ))
			emit valueChanged(&this->color);
		setRgbString(rComponent, gComponent, bComponent);
		inChangingComponents = false;
	}
}

void TextEditSelection::slotRgbComponentChanged() {
	if(!inChangingComponents) {
		inChangingComponents = true;
		int rComponent = lineEditTable[R_INDEX]->text().toInt();
		int gComponent = lineEditTable[G_INDEX]->text().toInt();
		int bComponent = lineEditTable[B_INDEX]->text().toInt();
		TQColor color;
		color.setRgb(rComponent, gComponent, bComponent);
		int hComponent;
		int sComponent;
		int vComponent;
		color.hsv(&hComponent, &sComponent, &vComponent);
		TQString string;
		lineEditTable[H_INDEX]->setText(string.setNum( hComponent ));
		lineEditTable[S_INDEX]->setText(string.setNum( sComponent ));
		lineEditTable[V_INDEX]->setText(string.setNum( vComponent ));
		Color oldColor = this->color;
		this->color.setComponent(Color::RED_INDEX, rComponent);
		this->color.setComponent(Color::GREEN_INDEX, gComponent);
		this->color.setComponent(Color::BLUE_INDEX, bComponent);
		if(!this->color.equals( oldColor ))
			emit valueChanged(&this->color);
		setRgbString(rComponent, gComponent, bComponent);
		inChangingComponents = false;
	}
}

void TextEditSelection::slotRgbStringChanged() {
	if(!inChangingComponents) {
		inChangingComponents = true;
		TQString string = rgbStringLineEdit->text().stripWhiteSpace();
		bool result;
		int value = string.toInt(&result, 16);
		if(result) {
			int rComponent = (value >> 16)&0xff;
			int gComponent = (value >> 8)&0xff;
			int bComponent = (value >> 0)&0xff;
			lineEditTable[R_INDEX]->setText(string.setNum( rComponent ));
			lineEditTable[G_INDEX]->setText(string.setNum( gComponent ));
			lineEditTable[B_INDEX]->setText(string.setNum( bComponent ));
			int hComponent;
			int sComponent;
			int vComponent;
			TQColor hsvColor;
			hsvColor.hsv(&hComponent, &sComponent, &vComponent);
			lineEditTable[H_INDEX]->setText(string.setNum( hComponent ));
			lineEditTable[S_INDEX]->setText(string.setNum( sComponent ));
			lineEditTable[V_INDEX]->setText(string.setNum( vComponent ));
			Color oldColor = this->color;
			this->color.setComponent(Color::RED_INDEX, rComponent);
			this->color.setComponent(Color::GREEN_INDEX, gComponent);
			this->color.setComponent(Color::BLUE_INDEX, bComponent);
			if(!this->color.equals( oldColor ))
				emit valueChanged(&this->color);
		}
		inChangingComponents = false;
	}
}

void TextEditSelection::slotSetValue(Color* color) {
	if(!color->equals( this->color )) {
		inChangingComponents = true;
		this->color = *color;
		TQString string;
		int rComponent = this->color.component(Color::RED_INDEX);
		int gComponent = this->color.component(Color::GREEN_INDEX);
		int bComponent = this->color.component(Color::BLUE_INDEX);
		lineEditTable[R_INDEX]->setText(string.setNum( rComponent ));
		lineEditTable[G_INDEX]->setText(string.setNum( gComponent ));
		lineEditTable[B_INDEX]->setText(string.setNum( bComponent ));
		TQColor hsvColor(rComponent, gComponent, bComponent);
		int hComponent;
		int sComponent;
		int vComponent;
		hsvColor.hsv(&hComponent, &sComponent, &vComponent);
		lineEditTable[H_INDEX]->setText(string.setNum( hComponent ));
		lineEditTable[S_INDEX]->setText(string.setNum( sComponent ));
		lineEditTable[V_INDEX]->setText(string.setNum( vComponent ));
		setRgbString(rComponent, gComponent, bComponent);
		inChangingComponents = false;
	}
}

#include "texteditselection.moc"