summaryrefslogtreecommitdiffstats
path: root/examples/xform/xform.cpp
blob: d9632d6e01862f9987aa0ea86cf2a7f60ff69d3e (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <ntqapplication.h>

#include <ntqdialog.h>
#include <ntqlabel.h>
#include <ntqlineedit.h>
#include <ntqpushbutton.h>
#include <ntqcheckbox.h>
#include <ntqradiobutton.h>
#include <ntqbuttongroup.h>
#include <ntqlcdnumber.h>
#include <ntqslider.h>
#include <ntqmenubar.h>
#include <ntqfontdialog.h>
#include <ntqlayout.h>
#include <ntqvbox.h>
#include <ntqwidgetstack.h>

#include <ntqpainter.h>
#include <ntqpixmap.h>
#include <ntqpicture.h>

#include <stdlib.h>


class ModeNames {
public:
    enum Mode { Text, Image, Picture };
};


class XFormControl : public TQVBox, public ModeNames
{
    TQ_OBJECT
public:
    XFormControl( const TQFont &initialFont, TQWidget *parent=0, const char *name=0 );
   ~XFormControl() {}

    TQWMatrix matrix();

signals:
    void newMatrix( TQWMatrix );
    void newText( const TQString& );
    void newFont( const TQFont & );
    void newMode( int );
private slots:
    void newMtx();
    void newTxt(const TQString&);
    void selectFont();
    void fontSelected( const TQFont & );
    void changeMode(int);
    void timerEvent(TQTimerEvent*);
private:
    Mode mode;
    TQSlider	 *rotS;		       // Rotation angle scroll bar
    TQSlider	 *shearS;	       // Shear value scroll bar
    TQSlider	 *magS;		       // Magnification value scroll bar
    TQLCDNumber	 *rotLCD;	       // Rotation angle LCD display
    TQLCDNumber	 *shearLCD;	       // Shear value LCD display
    TQLCDNumber	 *magLCD;	       // Magnification value LCD display
    TQCheckBox	 *mirror;	       // Checkbox for mirror image on/of
    TQWidgetStack* optionals;
    TQLineEdit	 *textEd;	       // Inp[ut field for xForm text
    TQPushButton  *fpb;		       // Select font push button
    TQRadioButton *rb_txt;	       // Radio button for text
    TQRadioButton *rb_img;	       // Radio button for image
    TQRadioButton *rb_pic;	       // Radio button for picture
    TQFont currentFont;
};

/*
  ShowXForm displays a text or a pixmap (TQPixmap) using a coordinate
  transformation matrix (TQWMatrix)
*/

class ShowXForm : public TQWidget, public ModeNames
{
    TQ_OBJECT
public:
    ShowXForm( const TQFont &f, TQWidget *parent=0, const char *name=0 );
   ~ShowXForm() {}
    void showIt();			// (Re)displays text or pixmap

    Mode mode() const { return m; }
public slots:
    void setText( const TQString& );
    void setMatrix( TQWMatrix );
    void setFont( const TQFont &f );
    void setPixmap( TQPixmap );
    void setPicture( const TQPicture& );
    void setMode( int );
private:
    TQSizePolicy sizePolicy() const;
    TQSize sizeHint() const;
    void paintEvent( TQPaintEvent * );
    void resizeEvent( TQResizeEvent * );
    TQWMatrix  mtx;			// coordinate transform matrix
    TQString   text;			// text to be displayed
    TQPixmap   pix;			// pixmap to be displayed
    TQPicture  picture;			// text to be displayed
    TQRect     eraseRect;		// covers last displayed text/pixmap
    Mode      m;
};

XFormControl::XFormControl( const TQFont &initialFont,
			    TQWidget *parent, const char *name )
	: TQVBox( parent, name )
{
    setSpacing(6);
    setMargin(6);
    currentFont = initialFont;
    mode = Image;

    rotLCD	= new TQLCDNumber( 4, this, "rotateLCD" );
    rotS	= new TQSlider( TQSlider::Horizontal, this,
				  "rotateSlider" );
    shearLCD	= new TQLCDNumber( 5,this, "shearLCD" );
    shearS	= new TQSlider( TQSlider::Horizontal, this,
				  "shearSlider" );
    mirror	= new TQCheckBox( this, "mirrorCheckBox" );
    rb_txt = new TQRadioButton( this, "text" );
    rb_img = new TQRadioButton( this, "image" );
    rb_pic = new TQRadioButton( this, "picture" );
    optionals = new TQWidgetStack(this);
    TQVBox* optionals_text = new TQVBox(optionals);
    optionals_text->setSpacing(6);
    TQVBox* optionals_other = new TQVBox(optionals);
    optionals_other->setSpacing(6);
    optionals->addWidget(optionals_text,0);
    optionals->addWidget(optionals_other,1);
    fpb		= new TQPushButton( optionals_text, "text" );
    textEd	= new TQLineEdit( optionals_text, "text" );
    textEd->setFocus();

    rotLCD->display( "  0'" );

    rotS->setRange( -180, 180 );
    rotS->setValue( 0 );
    connect( rotS, SIGNAL(valueChanged(int)), SLOT(newMtx()) );

    shearLCD->display( "0.00" );

    shearS->setRange( -25, 25 );
    shearS->setValue( 0 );
    connect( shearS, SIGNAL(valueChanged(int)), SLOT(newMtx()) );

    mirror->setText( tr("Mirror") );
    connect( mirror, SIGNAL(clicked()), SLOT(newMtx()) );

    TQButtonGroup *bg = new TQButtonGroup(this);
    bg->hide();
    bg->insert(rb_txt,0);
    bg->insert(rb_img,1);
    bg->insert(rb_pic,2);
    rb_txt->setText( tr("Text") );
    rb_img->setText( tr("Image") );
    rb_img->setChecked(TRUE);
    rb_pic->setText( tr("Picture") );
    connect( bg, SIGNAL(clicked(int)), SLOT(changeMode(int)) );

    fpb->setText( tr("Select font...") );
    connect( fpb, SIGNAL(clicked()), SLOT(selectFont()) );

    textEd->setText( "Troll" );
    connect( textEd, SIGNAL(textChanged(const TQString&)),
		     SLOT(newTxt(const TQString&)) );

    magLCD = new TQLCDNumber( 4,optionals_other, "magLCD" );
    magLCD->display( "100" );
    magS = new TQSlider( TQSlider::Horizontal, optionals_other,
			   "magnifySlider" );
    magS->setRange( 0, 800 );
    connect( magS, SIGNAL(valueChanged(int)), SLOT(newMtx()) );
    magS->setValue( 0 );
    connect( magS, SIGNAL(valueChanged(int)), magLCD, SLOT(display(int)));

    optionals_text->adjustSize();
    optionals_other->adjustSize();
    changeMode(Image);

    startTimer(20); // start an initial animation
}

void XFormControl::timerEvent(TQTimerEvent*)
{
    int v = magS->value();
    v = (v+2)+v/10;
    if ( v >= 200 ) {
	v = 200;
	killTimers();
    }
    magS->setValue(v);
}



/*
    Called whenever the user has changed one of the matrix parameters
    (i.e. rotate, shear or magnification)
*/
void XFormControl::newMtx()
{
    emit newMatrix( matrix() );
}

void XFormControl::newTxt(const TQString& s)
{
    emit newText(s);
    changeMode(Text);
}

/*
    Calculates the matrix appropriate for the current controls,
    and updates the displays.
*/
TQWMatrix XFormControl::matrix()
{
    TQWMatrix m;
    if (mode != Text) {
	double magVal = 1.0*magS->value()/100;
	m.scale( magVal, magVal );
    }
    double shearVal = 1.0*shearS->value()/25;
    m.shear( shearVal, shearVal );
    m.rotate( rotS->value() );
    if ( mirror->isChecked() ) {
	m.scale( 1, -1 );
	m.rotate( 180 );
    }

    TQString tmp;
    tmp.sprintf( "%1.2f", shearVal  );
    if ( shearVal >= 0 )
	tmp.insert( 0, " " );
    shearLCD->display( tmp );

    int rot = rotS->value();
    if ( rot < 0 )
	rot = rot + 360;
    tmp.sprintf( "%3i'", rot );
    rotLCD->display( tmp );
    return m;
}


void XFormControl::selectFont()
{
    bool ok;
    TQFont f = TQFontDialog::getFont( &ok, currentFont );
    if ( ok ) {
	currentFont = f;
	fontSelected( f );
    }
}

void XFormControl::fontSelected( const TQFont &font )
{
    emit newFont( font );
    changeMode(Text);
}

/*
    Sets the mode - Text, Image, or Picture.
*/

void XFormControl::changeMode(int m)
{
    mode = (Mode)m;

    emit newMode( m );
    newMtx();
    if ( mode == Text ) {
	optionals->raiseWidget(0);
	rb_txt->setChecked(TRUE);
    } else {
	optionals->raiseWidget(1);
	if ( mode == Image )
	    rb_img->setChecked(TRUE);
	else
	    rb_pic->setChecked(TRUE);
    }
    tqApp->flushX();
}

ShowXForm::ShowXForm( const TQFont &initialFont,
		      TQWidget *parent, const char *name )
	: TQWidget( parent, name, WResizeNoErase )
{
    setFont( initialFont );
    setBackgroundColor( white );
    m = Text;
    eraseRect = TQRect( 0, 0, 0, 0 );
}

TQSizePolicy ShowXForm::sizePolicy() const
{
    return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding );
}

TQSize ShowXForm::sizeHint() const
{
    return TQSize(400,400);
}

void ShowXForm::paintEvent( TQPaintEvent * )
{
    showIt();
}

void ShowXForm::resizeEvent( TQResizeEvent * )
{
    eraseRect = TQRect( width()/2, height()/2, 0, 0 );
    repaint(rect());
}

void ShowXForm::setText( const TQString& s )
{
    text = s;
    showIt();
}

void ShowXForm::setMatrix( TQWMatrix w )
{
    mtx = w;
    showIt();
}

void ShowXForm::setFont( const TQFont &f )
{
    m = Text;
    TQWidget::setFont( f );
}

void ShowXForm::setPixmap( TQPixmap pm )
{
    pix	 = pm;
    m    = Image;
    showIt();
}

void ShowXForm::setPicture( const TQPicture& p )
{
    picture = p;
    m = Picture;
    showIt();
}

void ShowXForm::setMode( int mode )
{
    m = (Mode)mode;
}

void ShowXForm::showIt()
{
    TQPainter p;
    TQRect r;	  // rectangle covering new text/pixmap in virtual coordinates
    TQWMatrix um;  // copy user specified transform
    int textYPos = 0; // distance from boundingRect y pos to baseline
    int textXPos = 0; // distance from boundingRect x pos to text start
    TQRect br;
    TQFontMetrics fm( fontMetrics() );	// get widget font metrics
    switch ( mode() ) {
      case Text:
	br = fm.boundingRect( text );	// rectangle covering text
	r  = br;
	textYPos = -r.y();
	textXPos = -r.x();
	br.moveTopLeft( TQPoint( -br.width()/2, -br.height()/2 ) );
        break;
      case Image:
	r = TQRect(0, 0, pix.width()+1, pix.height()+1);
        break;
      case Picture:
	// ### need TQPicture::boundingRect()
	r = TQRect(0,0,1000,1000);
        break;
    }
    r.moveTopLeft( TQPoint(-r.width()/2, -r.height()/2) );
    r.moveBy( -1, -1 ); // add border for matrix round off
    r.setSize( TQSize( r.width() + 2,r.height() + 2 ) );
	  // compute union of new and old rect
	  // the resulting rectangle will cover what is already displayed
	  // and have room for the new text/pixmap
    eraseRect = eraseRect.unite( mtx.mapRect(r) );
    int pw = TQMIN(eraseRect.width(),width());
    int ph = TQMIN(eraseRect.height(),height());
    TQPixmap pm( pw, ph );		// off-screen drawing pixmap
    pm.fill( backgroundColor() );

    p.begin( &pm );
    um.translate( pw/2, ph/2 );	// 0,0 is center
    um = mtx * um;
    p.setWorldMatrix( um );
    switch ( mode() ) {
      case Text:
	p.setFont( font() );		// use widget font
	p.drawText( r.left() + textXPos, r.top() + textYPos, text );
#if 0
	p.setPen( red );
	p.drawRect( br );
#endif
	break;
    case Image:
	p.drawPixmap( -pix.width()/2, -pix.height()/2, pix );
	break;
      case Picture:
	// ### need TQPicture::boundingRect()
	p.scale(0.25,0.25);
	p.translate(-230,-180);
	p.drawPicture( picture );
    }
    p.end();

    int xpos = width()/2  - pw/2;
    int ypos = height()/2 - ph/2;
    bitBlt( this, xpos, ypos,			// copy pixmap to widget
	    &pm, 0, 0, -1, -1 );
    eraseRect =	 mtx.map( r );
}


/*
    Grand unifying widget, putting ShowXForm and XFormControl
    together.
*/

class XFormCenter : public TQHBox, public ModeNames
{
    TQ_OBJECT
public:
    XFormCenter( TQWidget *parent=0, const char *name=0 );
public slots:
    void setFont( const TQFont &f ) { sx->setFont( f ); }
    void newMode( int );
private:
    ShowXForm	*sx;
    XFormControl *xc;
};

void XFormCenter::newMode( int m )
{
    static bool first_i = TRUE;
    static bool first_p = TRUE;

    if ( sx->mode() == m )
	return;
    if ( m == Image && first_i ) {
	first_i = FALSE;
	TQPixmap pm;
	if ( pm.load( "image.any" ) )
	    sx->setPixmap( pm );
	return;
    }
    if ( m == Picture && first_p ) {
	first_p = FALSE;
	TQPicture p;
	if (p.load( "picture.any" ))
	    sx->setPicture( p );
	return;
    }
    sx->setMode(m);
}

XFormCenter::XFormCenter( TQWidget *parent, const char *name )
    : TQHBox( parent, name )
{
    TQFont f( "Charter", 36, TQFont::Bold );

    xc = new XFormControl( f, this );
    sx = new ShowXForm( f, this );
    setStretchFactor(sx,1);
    xc->setFrameStyle( TQFrame::Panel | TQFrame::Raised );
    xc->setLineWidth( 2 );
    connect( xc, SIGNAL(newText(const TQString&)), sx,
		 SLOT(setText(const TQString&)) );
    connect( xc, SIGNAL(newMatrix(TQWMatrix)),
	     sx, SLOT(setMatrix(TQWMatrix)) );
    connect( xc, SIGNAL(newFont(const TQFont&)), sx,
		 SLOT(setFont(const TQFont&)) );
    connect( xc, SIGNAL(newMode(int)), SLOT(newMode(int)) );
    sx->setText( "Troll" );
    newMode( Image );
    sx->setMatrix(xc->matrix());
}


int main( int argc, char **argv )
{
    TQApplication a( argc, argv );

    XFormCenter *xfc = new XFormCenter;

    a.setMainWidget( xfc );
    xfc->setCaption("TQt Example - XForm");
    xfc->show();
    return a.exec();
}

#include "xform.moc"		      // include metadata generated by the moc