summaryrefslogtreecommitdiffstats
path: root/examples/demo/opengl/glworkspace.cpp
blob: 6a2d7fe893aad2a6f3fe5f8299fe2d82a48faf05 (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
#include "glworkspace.h"
#include "glbox.h"
#include "glgear.h"
#include "gltexobj.h"

#include <tqworkspace.h>
#include <ntqdialog.h>
#include <tqtoolbar.h>
#include <tqpopupmenu.h>
#include <tqmenubar.h>
#include <tqaction.h>
#include <tqprinter.h>
#include <tqpainter.h>
#include <ntqcheckbox.h>
#include <ntqvbox.h>
#include <tqimage.h>
#include "printpreview.h"


GLWorkspace::GLWorkspace( TQWidget *parent, const char *name, WFlags f )
: TQMainWindow( parent, name, f ), printer( 0 )
{
    setupSceneActions();

    TQVBox *vbox = new TQVBox( this );
    vbox->setFrameStyle( TQFrame::StyledPanel | TQFrame::Sunken );
    vbox->setMargin( 1 );
    vbox->setLineWidth( 1 );

    workspace = new TQWorkspace( vbox );
    workspace->setBackgroundMode( PaletteMid );
    setCentralWidget( vbox );
}

GLWorkspace::~GLWorkspace()
{
}

void GLWorkspace::setupSceneActions()
{
    TQToolBar *tb = new TQToolBar( "Scene", this );
    TQPopupMenu *menu = new TQPopupMenu( this );
    menuBar()->insertItem( tr( "&Scene" ), menu );

    TQAction *a;
    TQActionGroup *newGroup = new TQActionGroup( this );
    newGroup->setMenuText( tr( "&New" ) );
    newGroup->setText( tr( "New" ) );
    newGroup->setUsesDropDown( TRUE );
    newGroup->setExclusive( FALSE );
    newGroup->setIconSet( TQPixmap( "textdrawing/filenew.png" ) );
    a = new TQAction( tr( "Wirebox" ), TQPixmap( "opengl/wirebox.xpm" ), tr( "&Wirebox" ), 0, newGroup );
    connect( a, TQ_SIGNAL( activated() ), this, TQ_SLOT( newWirebox() ) );
    a = new TQAction( tr( "Gear" ), TQPixmap( "opengl/gear.xpm" ), tr( "&Gears" ), 0, newGroup );
    connect( a, TQ_SIGNAL( activated() ), this, TQ_SLOT( newGear() ) );
    a = new TQAction( tr( "Texture" ), TQPixmap( "opengl/texture.xpm" ), tr( "&Texture" ), 0, newGroup );
    connect( a, TQ_SIGNAL( activated() ), this, TQ_SLOT( newTexture() ) );
/*    a = new TQAction( tr( "Nurbs" ), TQPixmap( "opengl/nurbs.xpm" ), tr( "&Nurbs" ), 0, newGroup );
    connect( a, TQ_SIGNAL( activated() ), this, TQ_SLOT( newNurbs() ) );*/
    newGroup->addTo( tb );
    newGroup->addTo( menu );

    menu->insertSeparator();
    TQActionGroup *printGroup = new TQActionGroup( this );
    printGroup->setMenuText( tr( "&Print" ) );
    printGroup->setText( tr( "Print" ) );
    printGroup->setUsesDropDown( TRUE );
    printGroup->setExclusive( FALSE );
    printGroup->setIconSet( TQPixmap( "textdrawing/print.png" ) );
    TQAction *da = new TQAction( tr( "Window Size" ), TQPixmap( "textdrawing/print.png" ), tr( "&Window Size" ), CTRL + Key_P, printGroup );
    connect( da, TQ_SIGNAL( activated() ), this, TQ_SLOT( filePrintWindowRes() ) );
    a = new TQAction( tr( "Low Resolution" ), tr( "&Low Resolution" ), 0, printGroup );
    connect( a, TQ_SIGNAL( activated() ), this, TQ_SLOT( filePrintLowRes() ) );
    a = new TQAction( tr( "Medium Resolution" ), tr( "&Medium Resolution" ), 0, printGroup );
    connect( a, TQ_SIGNAL( activated() ), this, TQ_SLOT( filePrintMedRes() ) );
    a = new TQAction( tr( "High Resolution" ), tr( "&High Resolution" ), 0, printGroup );
    connect( a, TQ_SIGNAL( activated() ), this, TQ_SLOT( filePrintHighRes() ) );
    printGroup->addSeparator();
    a = new TQAction( tr( "Setup" ), tr( "&Setup..." ), 0, printGroup );
    connect( a, TQ_SIGNAL( activated() ), this, TQ_SLOT( filePrintSetup() ) );
    da->addTo( tb );
    printGroup->addTo( menu );

    a = new TQAction( tr( "Close" ), TQPixmap(), tr( "&Close" ), 0, this );
    connect( a, TQ_SIGNAL( activated() ), this, TQ_SLOT( fileClose() ) );
    a->addTo( menu );
}

void GLWorkspace::newWirebox()
{
    GLBox *gl = new GLBox( workspace, 0, WDestructiveClose );
    gl->setIcon( TQPixmap( "opengl/wirebox.xpm" ) );
    gl->setCaption( tr( "Wirebox" ) );
    gl->resize( 320, 240 );
    gl->show();
}

void GLWorkspace::newGear()
{
    GLGear *gl = new GLGear( workspace, 0, WDestructiveClose );
    gl->setIcon( TQPixmap( "opengl/gear.xpm" ) );
    gl->setCaption( tr( "Gear" ) );
    gl->resize( 320, 240 );
    gl->show();
}

void GLWorkspace::newTexture()
{
    GLTexobj *gl = new GLTexobj( workspace, 0, WDestructiveClose );
    gl->setIcon( TQPixmap( "opengl/texture.xpm" ) );
    gl->setCaption( tr( "Texture" ) );
    gl->resize( 320, 240 );
    gl->show();
}

void GLWorkspace::newNurbs()
{
    GLGear *gl = new GLGear ( workspace, 0, WDestructiveClose );
    gl->setIcon( TQPixmap( "opengl/nurbs.xpm" ) );
    gl->setCaption( tr( "Nurbs" ) );
    gl->resize( 320, 240 );
    gl->show();
}

void GLWorkspace::filePrint( int x, int y )
{
    bool print = printer || filePrintSetup();
    if ( !print || !printer )
	return;

    TQWidget *widget = workspace->activeWindow();
    if ( !widget || !widget->inherits( "TQGLWidget" ) )
	return;
    TQGLWidget *gl = (TQGLWidget *)widget;
    TQPixmap pm = gl->renderPixmap( x, y );

    PrintPreview view( this );
    TQImage temp = pm.convertToImage();
    temp = temp.smoothScale( 400, 300 );
    TQPixmap temppix;
    temppix.convertFromImage( temp );
    view.setPixmap( temppix );
    view.setIcon( TQPixmap( "opengl/snapshot.xpm" ) );
    view.setCaption( gl->caption() + " - Print preview" );
    if ( view.exec() ) {
	TQImage img = pm.convertToImage();
	if ( view.checkInvert->isChecked() ) {
	    img.invertPixels();
	}
	if ( view.checkMirror->isChecked() ) {
	    img = img.mirror( TRUE, FALSE );
	}
	if ( view.checkFlip->isChecked() ) {
	    img = img.mirror( FALSE, TRUE );
	}
	if ( view.checkLeft->isEnabled() && view.checkLeft->isChecked() ) {
	}
	if ( view.checkRight->isEnabled() && view.checkRight->isChecked() ) {
	}
	pm.convertFromImage( img );

	TQPainter painter;
	if ( !painter.begin( printer ) )
	    return;

	painter.drawPixmap( TQPoint( 0, 0 ), pm );

	painter.end();
    }
}

void GLWorkspace::filePrintWindowRes()
{
    filePrint( 0, 0 );
}

void GLWorkspace::filePrintLowRes()
{
    filePrint( 640, 480 );
}

void GLWorkspace::filePrintMedRes()
{
    filePrint( 1024, 768 );
}

void GLWorkspace::filePrintHighRes()
{
    filePrint( 2048, 1536 );
}

bool GLWorkspace::filePrintSetup()
{
    bool newPrinter = !printer;

    if ( !printer )
	printer = new TQPrinter;
    if ( printer->setup() ) {
	return TRUE;
    } else {
	if ( newPrinter ) {
	    delete printer;
	    printer = 0;
	}
	return FALSE;
    }
}

void GLWorkspace::fileClose()
{
    workspace->closeActiveWindow();
}