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
|
/***************************************************************************
* $Id$
**
* Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
**
* This file is part of an example program for Qt. This example
* program may be used, distributed and modified without limitation.
**
****************************************************************************/
import org.kde.qt.*;
class MagWidget extends TQWidget
{
private TQComboBox zoom;
private TQComboBox refresh;
private TQPushButton saveButton;
private TQPushButton multiSaveButton;
private TQPushButton quitButton;
private TQPixmap pm; // pixmap, magnified
private TQPixmap p; // pixmap
private TQImage image; // image of pixmap (for RGB)
private TQLabel rgb;
private int yoffset; // pixels in addition to the actual picture
private int z; // magnification factor
private int r; // autorefresh rate (index into refreshrates)
private boolean grabbing; // true if qmag is currently grabbing
private int grabx, graby;
private String multifn; // filename for multisave
static String zoomfactors[] = {
"100%", "200%", "300%", "400%", "500%",
"600%", "700%", "800%", "1600%" };
static String refreshrates[] = {
"No autorefresh", "50 per second", "4 per second", "3 per second", "2 per second",
"Every second", "Every two seconds", "Every three seconds",
"Every five seconds", "Every ten seconds" };
static int timer[] = {
0, 20, 250, 333, 500, 1000, 2000, 3000, 5000, 10000 };
MagWidget( )
{
this(null, null);
}
MagWidget( TQWidget parent, String name )
{
super( parent, name);
z = 1; // default zoom (100%)
r = 0; // default refresh (none)
int w=0, x=0, n;
zoom = new TQComboBox( false, this );
zoom.insertStrList( zoomfactors, 9 );
connect( zoom, SIGNAL("activated(int)"), SLOT("setZoom(int)") );
refresh = new TQComboBox( false, this );
refresh.insertStrList( refreshrates, 9 );
connect( refresh, SIGNAL("activated(int)"), SLOT("setRefresh(int)") );
for( n=0; n<9; n++) {
int w2 = zoom.fontMetrics().width( zoomfactors[n] );
w = w2 > w ? w2 : w;
}
zoom.setGeometry( 2, 2, w+30, 20 );
x = w+34;
w = 0;
for( n=0; n<9; n++) {
int w2 = refresh.fontMetrics().width( refreshrates[n] );
w = w2 > w ? w2 : w;
}
refresh.setGeometry( x, 2, w+30, 20 );
saveButton = new TQPushButton( this );
connect( saveButton, SIGNAL("clicked()"), this, SLOT("save()") );
saveButton.setText( "Save" );
saveButton.setGeometry( x+w+30+2, 2,
10+saveButton.fontMetrics().width("Save"), 20 );
multiSaveButton = new TQPushButton( this );
multiSaveButton.setToggleButton(true);
connect( multiSaveButton, SIGNAL("clicked()"), this, SLOT("multiSave()") );
multiSaveButton.setText( "MultiSave" );
multiSaveButton.setGeometry( saveButton.geometry().right() + 2, 2,
10+multiSaveButton.fontMetrics().width("MultiSave"), 20 );
quitButton = new TQPushButton( this );
connect( quitButton, SIGNAL("clicked()"), qApp(), SLOT("quit()") );
quitButton.setText( "Quit" );
quitButton.setGeometry( multiSaveButton.geometry().right() + 2, 2,
10+quitButton.fontMetrics().width("Quit"), 20 );
// #else
// zoom = null;
// multiSaveButton = null;
setRefresh(1);
setZoom(5);
rgb = new TQLabel( this );
rgb.setText( "" );
rgb.setAlignment( AlignVCenter );
rgb.resize( width(), rgb.fontMetrics().height() + 4 );
yoffset = zoom.height() // top buttons
+ 4 // space around top buttons
+ rgb.height(); // color-value text height
setMinimumSize( quitButton.pos().x(), yoffset+20 );
resize( quitButton.geometry().topRight().x() + 2, yoffset+60 );
// #else
yoffset = 0;
resize(350,350);
grabx = graby = -1;
grabbing = false;
setMouseTracking( true ); // and do let me know what pixel I'm at, eh?
grabAround( new TQPoint(grabx=qApp().desktop().width()/2, graby=qApp().desktop().height()/2) );
}
void setZoom( int index )
{
if (index == 8)
z = 16;
else
z = index+1;
grab();
}
void setRefresh( int index )
{
r = index;
killTimers();
if (index != 0 && grabbing)
startTimer( timer[r] );
}
void save()
{
if ( p != null ) {
killTimers();
String fn = TQFileDialog.getSaveFileName();
if ( !fn.equals("") )
p.save( fn, "BMP" );
if ( r != 0)
startTimer( timer[r] );
}
}
void multiSave()
{
if ( p != null ) {
multifn = ""; // stops saving
multifn = TQFileDialog.getSaveFileName();
if ( multifn.equals("") )
multiSaveButton.setOn(false);
if ( r == 0)
p.save( multifn, "BMP" );
} else {
multiSaveButton.setOn(false);
}
}
void grab()
{
if ( !isVisible() )
return; // don't eat resources when iconified
if ( grabx < 0 || graby < 0 )
return; // don't grab until the user has said to
int x,y, w,h;
w = (width()+z-1)/z;
h = (height()+z-1-yoffset)/z;
if ( w<1 || h<1 )
return; // don't ask too much from the window system :)
x = grabx-w/2; // find a suitable position to grab from
y = graby-h/2;
if ( x + w > TQApplication.desktop().width() )
x = TQApplication.desktop().width()-w;
else if ( x < 0 )
x = 0;
if ( y + h > TQApplication.desktop().height() )
y = TQApplication.desktop().height()-h;
else if ( y < 0 )
y = 0;
p = TQPixmap.grabWindow( TQApplication.desktop().winId(), x, y, w, h );
image = p.convertToImage();
TQWMatrix m = new TQWMatrix(); // after getting it, scale it
m.scale( (double)z, (double)z );
// A cast from Object to TQPixmap is needed in java. The xForm methods
// have several different return types in C++, and must all be of type
// 'Object' for java
pm = (TQPixmap) p.xForm( m );
if ( multiSaveButton == null || !multiSaveButton.isOn() )
repaint( false ); // and finally repaint, flicker-free
}
protected void paintEvent( TQPaintEvent e )
{
if ( pm != null ) {
TQPainter paint = new TQPainter( this );
paint.drawPixmap( 0, zoom != null ? zoom.height()+4 : 0, pm,
0,0, width(), height()-yoffset );
}
}
protected void mousePressEvent( TQMouseEvent e )
{
if ( !grabbing ) { // prepare to grab...
grabbing = true;
killTimers();
grabMouse( crossCursor() );
grabx = -1;
graby = -1;
} else { // REALLY prepare to grab
grabx = mapToGlobal(e.pos()).x();
graby = mapToGlobal(e.pos()).y();
}
}
protected void mouseReleaseEvent( TQMouseEvent e )
{
if ( grabbing && grabx >= 0 && graby >= 0 ) {
grabbing = false;
grabAround(e.pos());
releaseMouse();
}
}
void grabAround(TQPoint pos)
{
int rx, ry;
rx = mapToGlobal(pos).x();
ry = mapToGlobal(pos).y();
int w = Math.abs(rx-grabx);
int h = Math.abs(ry-graby);
if ( w > 10 && h > 10 ) {
int pz;
pz = 1;
while ( w*pz*h*pz < width()*(height()-yoffset) &&
w*pz < TQApplication.desktop().width() &&
h*pz < TQApplication.desktop().height() )
pz++;
if ( (w*pz*h*pz - width()*(height()-yoffset)) >
(width()*(height()-yoffset) - w*(pz-1)*h*(pz-1)) )
pz--;
if ( pz < 1 )
pz = 1;
if ( pz > 8 )
pz = 8;
if ( zoom != null)
zoom.setCurrentItem( pz-1 );
z = pz;
grabx = (rx < grabx ? rx : grabx) + w/2;
graby = (ry < graby ? ry : graby) + h/2;
resize( w*z, h*z+yoffset );
}
grab();
if ( r != 0 )
startTimer( timer[r] );
}
protected void mouseMoveEvent( TQMouseEvent e )
{
if ( grabbing || pm.isNull() ||
e.pos().y() > height() - (zoom != null ? zoom.fontMetrics().height() - 4 : 0) ||
e.pos().y() < (zoom != null ? zoom.height()+4 : 4) ) {
rgb.setText( "" );
} else {
int x,y;
x = e.pos().x() / z;
y = (e.pos().y() - ( zoom != null ? zoom.height() : 0 ) - 4) / z;
String pixelinfo = "";
if ( image.valid(x,y) )
{
int px = image.pixel(x,y);
pixelinfo= " " + qRed(px) + "," + qGreen(px) + "," + qBlue(px) + " ";
pixelinfo += "#" + Integer.toHexString(qRed(px))
+ Integer.toHexString(qGreen(px))
+ Integer.toHexString(qBlue(px)) + " ";
}
String label = "x=" + (x+grabx) + ", y=" + (y+graby) + " " + pixelinfo;
rgb.setText( label );
}
}
protected void focusOutEvent( TQFocusEvent e )
{
rgb.setText( "" );
}
protected void timerEvent( TQTimerEvent e )
{
grab();
/*
if ( multiSaveButton.isOn() && !multifn.equals("") ) {
TQRegExp num("[0-9][0-9]");
int start;
int len;
if ((start=num.match(multifn,0,&len))>=0)
multifn.replace(num,
String().setNum(multifn.mid(start,len).toInt()+1)
);
p.save( multifn, "BMP" );
}
*/
}
protected void resizeEvent( TQResizeEvent e )
{
rgb.setGeometry( 0, height() - rgb.height(), width(), rgb.height() );
grab();
}
public static void main(String[] args)
{
TQApplication a = new TQApplication( args );
MagWidget m = new MagWidget();
a.setMainWidget( m );
m.show();
a.exec();
return;
}
static {
qtjava.initialize();
}
}
|