source: trunk/examples/demo/opengl/glworkspace.cpp

Last change on this file was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1#include "glworkspace.h"
2#include "glbox.h"
3#include "glgear.h"
4#include "gltexobj.h"
5
6#include <qworkspace.h>
7#include <qdialog.h>
8#include <qtoolbar.h>
9#include <qpopupmenu.h>
10#include <qmenubar.h>
11#include <qaction.h>
12#include <qprinter.h>
13#include <qpainter.h>
14#include <qcheckbox.h>
15#include <qvbox.h>
16#include <qimage.h>
17#include "printpreview.h"
18
19
20GLWorkspace::GLWorkspace( QWidget *parent, const char *name, WFlags f )
21: QMainWindow( parent, name, f ), printer( 0 )
22{
23 setupSceneActions();
24
25 QVBox *vbox = new QVBox( this );
26 vbox->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
27 vbox->setMargin( 1 );
28 vbox->setLineWidth( 1 );
29
30 workspace = new QWorkspace( vbox );
31 workspace->setBackgroundMode( PaletteMid );
32 setCentralWidget( vbox );
33}
34
35GLWorkspace::~GLWorkspace()
36{
37}
38
39void GLWorkspace::setupSceneActions()
40{
41 QToolBar *tb = new QToolBar( "Scene", this );
42 QPopupMenu *menu = new QPopupMenu( this );
43 menuBar()->insertItem( tr( "&Scene" ), menu );
44
45 QAction *a;
46 QActionGroup *newGroup = new QActionGroup( this );
47 newGroup->setMenuText( tr( "&New" ) );
48 newGroup->setText( tr( "New" ) );
49 newGroup->setUsesDropDown( TRUE );
50 newGroup->setExclusive( FALSE );
51 newGroup->setIconSet( QPixmap( "textdrawing/filenew.png" ) );
52 a = new QAction( tr( "Wirebox" ), QPixmap( "opengl/wirebox.xpm" ), tr( "&Wirebox" ), 0, newGroup );
53 connect( a, SIGNAL( activated() ), this, SLOT( newWirebox() ) );
54 a = new QAction( tr( "Gear" ), QPixmap( "opengl/gear.xpm" ), tr( "&Gears" ), 0, newGroup );
55 connect( a, SIGNAL( activated() ), this, SLOT( newGear() ) );
56 a = new QAction( tr( "Texture" ), QPixmap( "opengl/texture.xpm" ), tr( "&Texture" ), 0, newGroup );
57 connect( a, SIGNAL( activated() ), this, SLOT( newTexture() ) );
58/* a = new QAction( tr( "Nurbs" ), QPixmap( "opengl/nurbs.xpm" ), tr( "&Nurbs" ), 0, newGroup );
59 connect( a, SIGNAL( activated() ), this, SLOT( newNurbs() ) );*/
60 newGroup->addTo( tb );
61 newGroup->addTo( menu );
62
63 menu->insertSeparator();
64 QActionGroup *printGroup = new QActionGroup( this );
65 printGroup->setMenuText( tr( "&Print" ) );
66 printGroup->setText( tr( "Print" ) );
67 printGroup->setUsesDropDown( TRUE );
68 printGroup->setExclusive( FALSE );
69 printGroup->setIconSet( QPixmap( "textdrawing/print.png" ) );
70 QAction *da = new QAction( tr( "Window Size" ), QPixmap( "textdrawing/print.png" ), tr( "&Window Size" ), CTRL + Key_P, printGroup );
71 connect( da, SIGNAL( activated() ), this, SLOT( filePrintWindowRes() ) );
72 a = new QAction( tr( "Low Resolution" ), tr( "&Low Resolution" ), 0, printGroup );
73 connect( a, SIGNAL( activated() ), this, SLOT( filePrintLowRes() ) );
74 a = new QAction( tr( "Medium Resolution" ), tr( "&Medium Resolution" ), 0, printGroup );
75 connect( a, SIGNAL( activated() ), this, SLOT( filePrintMedRes() ) );
76 a = new QAction( tr( "High Resolution" ), tr( "&High Resolution" ), 0, printGroup );
77 connect( a, SIGNAL( activated() ), this, SLOT( filePrintHighRes() ) );
78 printGroup->addSeparator();
79 a = new QAction( tr( "Setup" ), tr( "&Setup..." ), 0, printGroup );
80 connect( a, SIGNAL( activated() ), this, SLOT( filePrintSetup() ) );
81 da->addTo( tb );
82 printGroup->addTo( menu );
83
84 a = new QAction( tr( "Close" ), QPixmap(), tr( "&Close" ), 0, this );
85 connect( a, SIGNAL( activated() ), this, SLOT( fileClose() ) );
86 a->addTo( menu );
87}
88
89void GLWorkspace::newWirebox()
90{
91 GLBox *gl = new GLBox( workspace, 0, WDestructiveClose );
92 gl->setIcon( QPixmap( "opengl/wirebox.xpm" ) );
93 gl->setCaption( tr( "Wirebox" ) );
94 gl->resize( 320, 240 );
95 gl->show();
96}
97
98void GLWorkspace::newGear()
99{
100 GLGear *gl = new GLGear( workspace, 0, WDestructiveClose );
101 gl->setIcon( QPixmap( "opengl/gear.xpm" ) );
102 gl->setCaption( tr( "Gear" ) );
103 gl->resize( 320, 240 );
104 gl->show();
105}
106
107void GLWorkspace::newTexture()
108{
109 GLTexobj *gl = new GLTexobj( workspace, 0, WDestructiveClose );
110 gl->setIcon( QPixmap( "opengl/texture.xpm" ) );
111 gl->setCaption( tr( "Texture" ) );
112 gl->resize( 320, 240 );
113 gl->show();
114}
115
116void GLWorkspace::newNurbs()
117{
118 GLGear *gl = new GLGear ( workspace, 0, WDestructiveClose );
119 gl->setIcon( QPixmap( "opengl/nurbs.xpm" ) );
120 gl->setCaption( tr( "Nurbs" ) );
121 gl->resize( 320, 240 );
122 gl->show();
123}
124
125void GLWorkspace::filePrint( int x, int y )
126{
127 bool print = printer || filePrintSetup();
128 if ( !print || !printer )
129 return;
130
131 QWidget *widget = workspace->activeWindow();
132 if ( !widget || !widget->inherits( "QGLWidget" ) )
133 return;
134 QGLWidget *gl = (QGLWidget *)widget;
135 QPixmap pm = gl->renderPixmap( x, y );
136
137 PrintPreview view( this );
138 QImage temp = pm.convertToImage();
139 temp = temp.smoothScale( 400, 300 );
140 QPixmap temppix;
141 temppix.convertFromImage( temp );
142 view.setPixmap( temppix );
143 view.setIcon( QPixmap( "opengl/snapshot.xpm" ) );
144 view.setCaption( gl->caption() + " - Print preview" );
145 if ( view.exec() ) {
146 QImage img = pm.convertToImage();
147 if ( view.checkInvert->isChecked() ) {
148 img.invertPixels();
149 }
150 if ( view.checkMirror->isChecked() ) {
151 img = img.mirror( TRUE, FALSE );
152 }
153 if ( view.checkFlip->isChecked() ) {
154 img = img.mirror( FALSE, TRUE );
155 }
156 if ( view.checkLeft->isEnabled() && view.checkLeft->isChecked() ) {
157 }
158 if ( view.checkRight->isEnabled() && view.checkRight->isChecked() ) {
159 }
160 pm.convertFromImage( img );
161
162 QPainter painter;
163 if ( !painter.begin( printer ) )
164 return;
165
166 painter.drawPixmap( QPoint( 0, 0 ), pm );
167
168 painter.end();
169 }
170}
171
172void GLWorkspace::filePrintWindowRes()
173{
174 filePrint( 0, 0 );
175}
176
177void GLWorkspace::filePrintLowRes()
178{
179 filePrint( 640, 480 );
180}
181
182void GLWorkspace::filePrintMedRes()
183{
184 filePrint( 1024, 768 );
185}
186
187void GLWorkspace::filePrintHighRes()
188{
189 filePrint( 2048, 1536 );
190}
191
192bool GLWorkspace::filePrintSetup()
193{
194 bool newPrinter = !printer;
195
196 if ( !printer )
197 printer = new QPrinter;
198 if ( printer->setup() ) {
199 return TRUE;
200 } else {
201 if ( newPrinter ) {
202 delete printer;
203 printer = 0;
204 }
205 return FALSE;
206 }
207}
208
209void GLWorkspace::fileClose()
210{
211 workspace->closeActiveWindow();
212}
Note: See TracBrowser for help on using the repository browser.