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
|
/***************************************************************************
* Copyright (C) 2005 by David Saxton *
* david@bluehaze.org *
* *
* 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 <kcolordialog.h>
#include <klocale.h>
#include <tqpainter.h>
bool ColorCombo::createdPalettes = false;
TQColor * ColorCombo::palette[ NumberOfSchemes ];
int ColorCombo::paletteSize[ NumberOfSchemes ];
ColorCombo::ColorCombo( ColorScheme colorScheme, TQWidget *parent, const char *name )
: TQComboBox( parent, name )
{
m_colorScheme = colorScheme;
customColor.setRgb( 255, 255, 255 );
internalColor.setRgb( 255, 255, 255 );
createPalettes();
addColors();
connect( this, TQT_SIGNAL( activated(int) ), TQT_SLOT( slotActivated(int) ) );
connect( this, TQT_SIGNAL( highlighted(int) ), TQT_SLOT( slotHighlighted(int) ) );
}
ColorCombo::~ColorCombo()
{
}
void ColorCombo::createPalettes()
{
if ( createdPalettes )
return;
createdPalettes = true;
paletteSize[ TQtStandard ] = 17;
palette[ TQtStandard ] = new TQColor[ paletteSize[ TQtStandard ] ];
int i = 0;
palette[ TQtStandard ][i++] = TQt::red;
palette[ TQtStandard ][i++] = TQt::green;
palette[ TQtStandard ][i++] = TQt::blue;
palette[ TQtStandard ][i++] = TQt::cyan;
palette[ TQtStandard ][i++] = TQt::magenta;
palette[ TQtStandard ][i++] = TQt::yellow;
palette[ TQtStandard ][i++] = TQt::darkRed;
palette[ TQtStandard ][i++] = TQt::darkGreen;
palette[ TQtStandard ][i++] = TQt::darkBlue;
palette[ TQtStandard ][i++] = TQt::darkCyan;
palette[ TQtStandard ][i++] = TQt::darkMagenta;
palette[ TQtStandard ][i++] = TQt::darkYellow;
palette[ TQtStandard ][i++] = TQt::white;
palette[ TQtStandard ][i++] = TQt::lightGray;
palette[ TQtStandard ][i++] = TQt::gray;
palette[ TQtStandard ][i++] = TQt::darkGray;
palette[ TQtStandard ][i++] = TQt::black;
paletteSize[ LED ] = 6;
palette[ LED ] = new TQColor[ paletteSize[ LED ] ];
i = 0;
palette[ LED ][i++] = "#f62a2a";
palette[ LED ][i++] = "#ff7733";
palette[ LED ][i++] = "#ffbb33";
palette[ LED ][i++] = "#eeee22";
palette[ LED ][i++] = "#4cc308";
palette[ LED ][i++] = "#22aaee";
}
void ColorCombo::setColor( const TQColor &col )
{
internalColor = col;
addColors();
}
void ColorCombo::resizeEvent( TQResizeEvent *re )
{
TQComboBox::resizeEvent( re );
addColors();
}
void ColorCombo::slotActivated( int index )
{
if ( index == 0 )
{
if ( KColorDialog::getColor( customColor, this ) == TQDialog::Accepted )
{
TQPainter painter;
TQPen pen;
TQRect rect( 0, 0, width(), TQFontMetrics(painter.font()).height()+4);
TQPixmap pixmap( rect.width(), rect.height() );
if ( tqGray( customColor.rgb() ) < 128 )
pen.setColor( white );
else
pen.setColor( black );
painter.begin( &pixmap );
TQBrush brush( customColor );
painter.fillRect( rect, brush );
painter.setPen( pen );
painter.drawText( 2, TQFontMetrics(painter.font()).ascent()+2, i18n("Custom...") );
painter.end();
changeItem( pixmap, 0 );
pixmap.detach();
}
internalColor = customColor;
}
else
internalColor = palette[ m_colorScheme ][ index - 1 ];
emit activated( internalColor );
}
void ColorCombo::slotHighlighted( int index )
{
if ( index == 0 )
internalColor = customColor;
else
internalColor = palette[ m_colorScheme ][ index - 1 ];
emit highlighted( internalColor );
}
void ColorCombo::addColors()
{
TQPainter painter;
TQPen pen;
TQRect rect( 0, 0, width(), TQFontMetrics(painter.font()).height()+4 );
TQPixmap pixmap( rect.width(), rect.height() );
int i;
clear();
createPalettes();
for ( i = 0; i < paletteSize[ m_colorScheme ]; i++ )
if ( palette[ m_colorScheme ][i] == internalColor ) break;
if ( i == paletteSize[ m_colorScheme ] )
customColor = internalColor;
if ( tqGray( customColor.rgb() ) < 128 )
pen.setColor( white );
else
pen.setColor( black );
painter.begin( &pixmap );
TQBrush brush( customColor );
painter.fillRect( rect, brush );
painter.setPen( pen );
painter.drawText( 2, TQFontMetrics(painter.font()).ascent()+2, i18n("Custom...") );
painter.end();
insertItem( pixmap );
pixmap.detach();
for ( i = 0; i < paletteSize[ m_colorScheme ]; i++ )
{
painter.begin( &pixmap );
TQBrush brush( palette[ m_colorScheme ][i] );
painter.fillRect( rect, brush );
painter.end();
insertItem( pixmap );
pixmap.detach();
if ( palette[ m_colorScheme ][i] == internalColor )
setCurrentItem( i + 1 );
}
}
#include "colorcombo.moc"
|