summaryrefslogtreecommitdiffstats
path: root/quanta/dialogs/tagdialogs/colorcombo.cpp
blob: ebe9a7fdb2b9855b5325483d2c256cbd2f2fac82 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* This file is part of the KDE libraries
    Copyright (C) 1997 Martin Jones (mjones@kde.org)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/
//-----------------------------------------------------------------------------
// KDE color selection dialog.
//
// 1999-09-27 Espen Sand <espensa@online.no>
// KColorDialog is now subclassed from KDialogBase. I have also extended
// KColorDialog::getColor() so that in contains a parent argument. This
// improves centering capability.
//
// layout management added Oct 1997 by Mario Weilguni
// <mweilguni@sime.com>
//


#include <stdio.h>
#include <stdlib.h>

#include <tqdrawutil.h>
#include <tqevent.h>
#include <tqfile.h>
#include <tqimage.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <klineedit.h>
#include <tqvalidator.h>
#include <tqpainter.h>
#include <tqpushbutton.h>
#include <tqtimer.h>

#include <kcolordialog.h>
#include <tdeconfig.h>
#include <kglobal.h>
#include <kglobalsettings.h>
#include <kiconloader.h>
#include <klistbox.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kseparator.h>
#include <kpalette.h>
#include <kimageeffect.h>

#include "colorcombo.h"
#include "colorcombo.moc"

// This is repeated from the KColorDlg, but I didn't
// want to make it public BL.
// We define it out when compiling with --enable-final in which case
// we use the version defined in KColorDlg

#define STANDARD_PAL_SIZE 17

ColorCombo::ColorCombo( TQWidget *parent, const char *name )
  : TQComboBox( parent, name )
{
  customColor.setRgb( 255, 255, 255 );
  internalcolor.setRgb( 255, 255, 255 );

  hascolor = false;

  createStandardPalette();

  addColors();

  connect( this, TQT_SIGNAL( activated(int) ), TQT_SLOT( slotActivated(int) ) );
  connect( this, TQT_SIGNAL( highlighted(int) ), TQT_SLOT( slotHighlighted(int) ) );
}

ColorCombo::~ColorCombo()
{
  delete []standardPalette;
  delete []standardPaletteNames;
}

/**
   Sets the current color
 */
void ColorCombo::setColor( const TQColor &col )
{
  internalcolor = col;
  hascolor = true;

  addColors();
}


/**
   Returns the currently selected color
 */
TQColor ColorCombo::color() const {
  return internalcolor;
}


bool ColorCombo::hasColor() const{
  return hascolor;
}


void ColorCombo::setColorName( const TQString &color )
{

  TQColor c(color);

  if ( c.isValid() && !color.isEmpty() ) {
    setColor( c );
  }
  else {
    hascolor = false;
    addColors();
  }

}

TQString ColorCombo::colorName()
{
  if ( hascolor ) {
    int i;

    for ( i = 0; i < STANDARD_PAL_SIZE; i++ )
      if ( standardPalette[i] == internalcolor )
          return standardPaletteNames[i];

    return internalcolor.name();

  }
  else
    return "";
}


void ColorCombo::resizeEvent( TQResizeEvent *re )
{
  TQComboBox::resizeEvent( re );

  addColors();
}

void ColorCombo::slotActivated( int index )
{
  hascolor = ( index != 0);

  if ( index == 1 )
  {
    if ( KColorDialog::getColor( customColor ) == TQDialog::Accepted )
    {
      TQRect rect( 0, 0, width(), 20 );
      TQPixmap pixmap( rect.width(), rect.height() );
      TQPainter painter;
      TQPen pen;

      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, painter.fontMetrics().height(),
            i18n("Custom...") );
      painter.end();

      changeItem( pixmap, 1 );
      pixmap.detach();
    }

    internalcolor = customColor;
  }
  else
    if ( index > 1 )
      internalcolor = standardPalette[ index - 2 ];

  emit activated( internalcolor );
}

void ColorCombo::slotHighlighted( int index )
{
  if ( index < 2 )
    internalcolor = customColor;
  else
    internalcolor = standardPalette[ index - 2 ];

  emit highlighted( internalcolor );
}

void ColorCombo::addColors()
{
  TQRect rect( 0, 0, width(), 20 );
  TQPixmap pixmap( rect.width(), rect.height() );
  TQPainter painter;
  TQPen pen;
  int i;

  clear();

  for ( i = 0; i < STANDARD_PAL_SIZE; i++ )
    if ( standardPalette[i] == internalcolor ) break;

  if ( i == STANDARD_PAL_SIZE )
    customColor = internalcolor;

  insertItem( i18n("None") );

  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, painter.fontMetrics().height(), i18n("Custom...") );
  painter.end();

  insertItem( pixmap );
  pixmap.detach();

  bool findColor = false;

  for ( i = 0; i < STANDARD_PAL_SIZE; i++ )
  {
    painter.begin( &pixmap );
    TQBrush brush( standardPalette[i] );
    painter.fillRect( rect, brush );
    painter.end();

    insertItem( pixmap );
    pixmap.detach();

    if ( standardPalette[i] == internalcolor ) {
      setCurrentItem( i + 2 );
      findColor = true;
    }
  }

  if ( !findColor )
    setCurrentItem(1);



  if ( !hascolor )
    setCurrentItem(0);

}

void ColorCombo::createStandardPalette()
{

    standardPalette = new TQColor [STANDARD_PAL_SIZE];

    int i = 0;

    standardPalette[i++] = TQt::red;
    standardPalette[i++] = TQt::green;
    standardPalette[i++] = TQt::blue;
    standardPalette[i++] = TQt::cyan;
    standardPalette[i++] = TQt::magenta;
    standardPalette[i++] = TQt::yellow;
    standardPalette[i++] = TQt::darkRed;
    standardPalette[i++] = TQt::darkGreen;
    standardPalette[i++] = TQt::darkBlue;
    standardPalette[i++] = TQt::darkCyan;
    standardPalette[i++] = TQt::darkMagenta;
    standardPalette[i++] = TQt::darkYellow;
    standardPalette[i++] = TQt::white;
    standardPalette[i++] = TQt::lightGray;
    standardPalette[i++] = TQt::gray;
    standardPalette[i++] = TQt::darkGray;
    standardPalette[i++] = TQt::black;


    standardPaletteNames = new TQString [STANDARD_PAL_SIZE];

    i = 0;

    standardPaletteNames[i++] = "red";
    standardPaletteNames[i++] = "green";
    standardPaletteNames[i++] = "blue";
    standardPaletteNames[i++] = "cyan";
    standardPaletteNames[i++] = "magenta";
    standardPaletteNames[i++] = "yellow";
    standardPaletteNames[i++] = "darkRed";
    standardPaletteNames[i++] = "darkGreen";
    standardPaletteNames[i++] = "darkBlue";
    standardPaletteNames[i++] = "darkCyan";
    standardPaletteNames[i++] = "darkMagenta";
    standardPaletteNames[i++] = "darkYellow";
    standardPaletteNames[i++] = "white";
    standardPaletteNames[i++] = "lightGray";
    standardPaletteNames[i++] = "gray";
    standardPaletteNames[i++] = "darkGray";
    standardPaletteNames[i++] = "black";
}