summaryrefslogtreecommitdiffstats
path: root/kicker-applets/kolourpicker/kolourpicker.cpp
blob: 77e4ff817525d3c0cda2fe822608d211f524e880 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* This file is part of KolourPicker
   Copyright (c) 2001 Malte Starostik <malte@kde.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.

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.

$Id$
*/

#include <tqfile.h>
#include <tqtextstream.h>
#include <tqlayout.h>
#include <tqimage.h>
#include <tqclipboard.h>
#include <tqregexp.h>
#include <tqbitmap.h>
#include <tqpainter.h>
#include <tqtooltip.h>
#include <tqcursor.h>

#include <tdeglobal.h>
#include <tdelocale.h>
#include <kiconloader.h>
#include <kinstance.h>
#include <tdeconfig.h>
#include <tdeaboutdata.h>
#include <tdeaboutapplication.h>
#include <tdemessagebox.h>
#include <tdepopupmenu.h>

#include "kolourpicker.h"
#include "kolourpicker.moc"

#include <X11/Xlib.h>

// Applet initialization function
extern "C"
{
	KDE_EXPORT KPanelApplet* init(TQWidget *parent, const TQString& configFile)
	{
		TDEGlobal::locale()->insertCatalogue("kolourpicker");
		return new KolourPicker(configFile, KPanelApplet::Normal,
			KPanelApplet::About,
			parent, "kolourpicker");
	}
}

KolourPicker::KolourPicker(const TQString& configFile, Type type,
	int actions, TQWidget *parent, const char *name)
	: KPanelApplet(configFile, type, actions, parent, name),
	  m_picking(0)
{
	TDEAboutData *about = new TDEAboutData("kolourpicker",
									   I18N_NOOP("Color Picker"),
									   "v0.1",
									   I18N_NOOP("An applet to pick color values from anywhere on the screen"),
									   TDEAboutData::License_GPL_V2,
									   "(c) 2001 Malte Starostik");
	about->addAuthor("Malte Starostik", I18N_NOOP("Original Author"), "malte@kde.org");
	m_instance = new TDEInstance(about);

	TDEConfig *conf = config();
	conf->setGroup("General");
	TQStringList history = conf->readListEntry("History");
	for (TQStringList::ConstIterator it = history.begin(); it != history.end(); ++it)
		m_history.append(TQColor(*it));

	setBackgroundOrigin(AncestorOrigin);

	m_colourButton = new SimpleButton(this);
	m_colourButton->setPixmap(SmallIcon("colorpicker"));
	m_colourButton->setFixedSize(20, 20);
	TQToolTip::add(m_colourButton, i18n("Pick a color"));
	connect(m_colourButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotPick()));

	m_historyButton = new SimpleButton(this);
	m_historyButton->setFixedSize(20, 20);
	if (m_history.count())
		m_historyButton->setPixmap(colorPixmap(m_history.last()));
	else
	{
		m_historyButton->setPixmap(colorPixmap(TQColor()));
		m_historyButton->setEnabled(false);
	}
	TQToolTip::add(m_historyButton, i18n("History"));
	connect(m_historyButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotHistory()));
}

KolourPicker::~KolourPicker()
{
    TDEGlobal::locale()->removeCatalogue("kolourpicker");
}


int KolourPicker::heightForWidth(int width) const
{
	return (width > 40) ? 22 : 44;
}

int KolourPicker::widthForHeight(int height) const
{
	return (height > 40) ? 22 : 44;
}

void KolourPicker::about()
{
	TDEAboutApplication dlg(m_instance->aboutData());
	dlg.exec();
}

void KolourPicker::slotPick()
{
	m_picking = true;
	grabMouse(crossCursor);
	grabKeyboard();
}

void KolourPicker::slotHistory()
{
	TDEPopupMenu popup;
	popup.insertTitle(SmallIcon("colorize"), i18n("History"));
	TQPtrList<TQPopupMenu> subMenus;
	subMenus.setAutoDelete(true);
	for (TQValueList<TQColor>::ConstIterator it = m_history.fromLast();
		it != m_history.end();
		--it)
	{
		TQPopupMenu *sub = copyPopup(*it, false);
		subMenus.append(sub);
		popup.insertItem(colorPixmap(*it),
			TQString("%1, %2, %3").arg((*it).red()).arg((*it).green()).arg((*it).blue()),
			sub);
	}
	popup.insertSeparator();
	int clear = popup.insertItem(SmallIcon("history_clear"), i18n("&Clear History"));
	int id = popup.exec(TQCursor::pos());
	if (id == clear)
	{
		m_history.clear();
		m_historyButton->setEnabled(false);
		arrangeButtons();
		TDEConfig *conf = config();
		conf->setGroup("General");
		conf->writeEntry("History", TQStringList());
		conf->sync();
	}
	else if (id != -1)
		setClipboard(popup.findItem(id)->text());
}

void KolourPicker::mouseReleaseEvent(TQMouseEvent *e)
{
	if (m_picking)
	{
		m_picking = false;
		releaseMouse();
		releaseKeyboard();
		TQWidget *desktop = TQT_TQWIDGET(TQApplication::desktop());
		TQPixmap pm = TQPixmap::grabWindow(desktop->winId(),
			e->globalPos().x(), e->globalPos().y(), 1, 1);
		TQImage img = pm.convertToImage();
		TQColor color(img.pixel(0, 0));

		// eventually remove a dupe
		TQValueListIterator<TQColor> dupe = m_history.find(color);
		if (dupe != m_history.end())
			m_history.remove(dupe);

		m_history.append(color);
		while (m_history.count() >= 10)
			m_history.remove(m_history.begin());
		m_historyButton->setEnabled(true);
		arrangeButtons();
		TQStringList history;
		for (TQValueList<TQColor>::ConstIterator it = m_history.begin();
			it != m_history.end();
			++it)
		{
			history.append((*it).name());
		}
		TDEConfig *conf = config();
		conf->setGroup("General");
		conf->writeEntry("History", history);
		conf->sync();
		m_historyButton->setPixmap(colorPixmap(color));
		TQPopupMenu *popup = copyPopup(color, true);
		int id = popup->exec(e->globalPos());
		if (id != -1)
			setClipboard( popup->findItem(id)->text() );
		delete popup;
	}
	else
		KPanelApplet::mouseReleaseEvent(e);
}

// set both clipboard and selection
void KolourPicker::setClipboard(const TQString& text)
{
	TQClipboard *clip = TQApplication::clipboard();
	bool oldMode = clip->selectionModeEnabled();
	clip->setSelectionMode(true);
	clip->setText(text);
	clip->setSelectionMode(false);
	clip->setText(text);
	clip->setSelectionMode( oldMode );
}

void KolourPicker::keyPressEvent(TQKeyEvent *e)
{
	if (m_picking)
	{
		if (e->key() == Key_Escape)
		{
			m_picking = false;
			releaseMouse();
			releaseKeyboard();
		}
		e->accept();
		return;
	}
	KPanelApplet::keyPressEvent(e);
}

void KolourPicker::resizeEvent(TQResizeEvent *)
{
	arrangeButtons();
}

void KolourPicker::arrangeButtons()
{
	int h, w, p;

	if (orientation() ==Qt::Horizontal)
	{
		h = height();
		if (h > 40)
		{
			// vertical layout
			p = (h - 40)/3;
			m_colourButton->setGeometry(2, p, 20, 20);
			m_historyButton->setGeometry(2, 2*p+20, 20, 20);
		}
		else
		{
			// horizontal layout
			p = (h - 20)/2;
			m_colourButton->setGeometry(2, p, 20, 20);
			m_historyButton->setGeometry(24, p, 20, 20);
		}
	}
	else
	{
		w = width();
		if (w > 40)
		{
			// horizontal layout
			p = (w - 40)/3;
			m_colourButton->setGeometry(p, 2, 20, 20);
			m_historyButton->setGeometry(2*p+20, 2, 20, 20);
		}
		else
		{
			// vertical layout
			p = (w - 20)/2;
			m_colourButton->setGeometry(p, 2, 20, 20);
			m_historyButton->setGeometry(p, 24, 20, 20);
		}
	}

	updateGeometry();
}

TQPopupMenu *KolourPicker::copyPopup(const TQColor &c, bool title) const
{
	TDEPopupMenu *popup = new TDEPopupMenu;
	if (title)
		popup->insertTitle(colorPixmap(c), i18n("Copy Color Value"));
	TQString value;
	// r, g, b
	value.sprintf("%u, %u, %u", c.red(), c.green(), c.blue());
	popup->insertItem(SmallIcon("text"), value);
	// HTML, lower case hex chars
	value.sprintf("#%.2x%.2x%.2x", c.red(), c.green(), c.blue());
	popup->insertItem(SmallIcon("text-html"), value);
	if (value.find(TQRegExp("[a-f]")) >= 0)
	{
		// HTML, upper case hex chars
		value.sprintf("#%.2X%.2X%.2X", c.red(), c.green(), c.blue());
    popup->insertItem(SmallIcon("text-html"), value);
  }
  // lower case hex chars 
  value.sprintf( "%.2x%.2x%.2x", c.red(), c.green(), c.blue() ); 
  popup->insertItem( SmallIcon( "text-html" ), value ); 
  if ( value.find( TQRegExp( "[a-f]" ) ) >= 0 ) 
  { 
    //  upper case hex chars 
    value.sprintf( "%.2X%.2X%.2X", c.red(), c.green(), c.blue() ); 
    popup->insertItem( SmallIcon( "text-html" ), value ); 
  } 
	// Color name
	TQStringList names = colorNames(c.red(), c.green(), c.blue());
	for (TQStringList::ConstIterator it = names.begin(); it != names.end(); ++it)
		popup->insertItem(SmallIcon("text"), *it);
	return popup;
}

#define AAFACTOR 4

TQPixmap KolourPicker::colorPixmap(const TQColor &c) const
{
    int x, y, dx, dy, d;
    
    TQImage img(16 * AAFACTOR, 16 * AAFACTOR, 32);
    img.setAlphaBuffer(true);
    img.fill(0);
    
    for (x = 0; x < img.width(); x++)
        for (y = 0; y < img.height(); y++)
        {
            dx = 1 + x - 15 * AAFACTOR / 2;
            dy = 1 + y - 15 * AAFACTOR / 2;
            d = dx * dx + dy * dy;
            
            if (d < (36 * AAFACTOR * AAFACTOR))
                img.setPixel(x, y, c.pixel());
            else if (d < (56.25 * AAFACTOR * AAFACTOR))
                img.setPixel(x, y, tqRgba(128, 128, 128, 255));
        }
    
    TQBitmap mask(16, 16);
    mask.fill(TQt::color0);
    TQPainter p(&mask);
    p.setPen(TQt::NoPen);
    p.setBrush(TQt::color1);
    p.drawEllipse(0, 0, 15, 15);
    p.end();
    
    TQPixmap pm = TQPixmap(img.smoothScale(16, 16));
    pm.setMask(mask);
    
    return pm;
}

const TQStringList &KolourPicker::colorNames(int r, int g, int b) const
{
	static TQStringList NullList;
	if (m_colorNames.isEmpty())
	{
		TQFile f("/usr/lib/X11/rgb.txt");
		if (!f.open(IO_ReadOnly))
			return NullList;
		TQTextStream str(&f);
		TQString red, green, blue;
		while (!str.atEnd())
		{
			str >> red;
			if (red.simplifyWhiteSpace()[0].latin1() == '!')
			{
				str.readLine();
				continue;
			}
			str >> green >> blue;
			const_cast<KolourPicker *>(this)->m_colorNames[(red.toInt() << 16) + (green.toInt() << 8) + blue.toInt()]
					.append(str.readLine().simplifyWhiteSpace());
		}
	}
	return m_colorNames[(r << 16) + (g << 8) + b];
}

// vim: ts=4 sw=4 noet