1 | /****************************************************************************
|
---|
2 | ** $Id: scrollview.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
5 | **
|
---|
6 | ** This file is part of an example program for Qt. This example
|
---|
7 | ** program may be used, distributed and modified without limitation.
|
---|
8 | **
|
---|
9 | *****************************************************************************/
|
---|
10 |
|
---|
11 | #include <qscrollview.h>
|
---|
12 | #include <qapplication.h>
|
---|
13 | #include <qmenubar.h>
|
---|
14 | #include <qpopupmenu.h>
|
---|
15 | #include <qpushbutton.h>
|
---|
16 | #include <qpainter.h>
|
---|
17 | #include <qpixmap.h>
|
---|
18 | #include <qmessagebox.h>
|
---|
19 | #include <qlayout.h>
|
---|
20 | #include <qlabel.h>
|
---|
21 | #include <qmultilineedit.h>
|
---|
22 | #include <qsizegrip.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 |
|
---|
25 |
|
---|
26 | static const int style_id = 0x1000;
|
---|
27 | static const int lw_id = 0x2000;
|
---|
28 | static const int mlw_id = 0x4000;
|
---|
29 | static const int mw_id = 0x8000;
|
---|
30 | static const int max_lw = 16;
|
---|
31 | static const int max_mlw = 5;
|
---|
32 | static const int max_mw = 10;
|
---|
33 |
|
---|
34 |
|
---|
35 | class BigShrinker : public QFrame {
|
---|
36 | Q_OBJECT
|
---|
37 | public:
|
---|
38 | BigShrinker(QWidget* parent) :
|
---|
39 | QFrame(parent)
|
---|
40 | {
|
---|
41 | setFrameStyle(QFrame::Box|QFrame::Sunken);
|
---|
42 | int h=35;
|
---|
43 | int b=0;
|
---|
44 | for (int y=0; y<2000-h; y+=h+10) {
|
---|
45 | if (y == 0) {
|
---|
46 | QButton* q=new QPushButton("Quit", this);
|
---|
47 | connect(q, SIGNAL(clicked()), qApp, SLOT(quit()));
|
---|
48 | } else {
|
---|
49 | QString str;
|
---|
50 | if ( b > 0 ) {
|
---|
51 | str.sprintf("Button %d", b++);
|
---|
52 | } else {
|
---|
53 | str = "I'm shrinking!";
|
---|
54 | ++b;
|
---|
55 | }
|
---|
56 | (new QPushButton(str, this))->move(y/2,y);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | resize(1000,2000);
|
---|
60 |
|
---|
61 | startTimer(250);
|
---|
62 | }
|
---|
63 |
|
---|
64 | void timerEvent(QTimerEvent*)
|
---|
65 | {
|
---|
66 | int w=width();
|
---|
67 | int h=height();
|
---|
68 | if ( w > 50 ) w -= 1;
|
---|
69 | if ( h > 50 ) h -= 2;
|
---|
70 | resize(w,h);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void mouseReleaseEvent(QMouseEvent* e)
|
---|
74 | {
|
---|
75 | emit clicked(e->x(), e->y());
|
---|
76 | }
|
---|
77 |
|
---|
78 | signals:
|
---|
79 | void clicked(int,int);
|
---|
80 | };
|
---|
81 |
|
---|
82 | class BigMatrix : public QScrollView {
|
---|
83 | QMultiLineEdit *dragging;
|
---|
84 | public:
|
---|
85 | BigMatrix(QWidget* parent) :
|
---|
86 | QScrollView(parent,"matrix", WStaticContents),
|
---|
87 | bg("bg.ppm")
|
---|
88 | {
|
---|
89 | bg.load("bg.ppm");
|
---|
90 | resizeContents(400000,300000);
|
---|
91 |
|
---|
92 | dragging = 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void viewportMousePressEvent(QMouseEvent* e)
|
---|
96 | {
|
---|
97 | int x, y;
|
---|
98 | viewportToContents( e->x(), e->y(), x, y );
|
---|
99 | dragging = new QMultiLineEdit(viewport(),"Another");
|
---|
100 | dragging->setText("Thanks!");
|
---|
101 | dragging->resize(100,100);
|
---|
102 | addChild(dragging, x, y);
|
---|
103 | showChild(dragging);
|
---|
104 | }
|
---|
105 |
|
---|
106 | void viewportMouseReleaseEvent(QMouseEvent*)
|
---|
107 | {
|
---|
108 | dragging = 0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void viewportMouseMoveEvent(QMouseEvent* e)
|
---|
112 | {
|
---|
113 | if ( dragging ) {
|
---|
114 | int mx, my;
|
---|
115 | viewportToContents( e->x(), e->y(), mx, my );
|
---|
116 | int cx = childX(dragging);
|
---|
117 | int cy = childY(dragging);
|
---|
118 | int w = mx - cx + 1;
|
---|
119 | int h = my - cy + 1;
|
---|
120 | QString msg;
|
---|
121 | msg.sprintf("at (%d,%d) %d by %d",cx,cy,w,h);
|
---|
122 | dragging->setText(msg);
|
---|
123 | dragging->resize(w,h);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | protected:
|
---|
128 | void drawContents(QPainter* p, int cx, int cy, int cw, int ch)
|
---|
129 | {
|
---|
130 | // The Background
|
---|
131 | if ( !bg.isNull() ) {
|
---|
132 | int rowheight=bg.height();
|
---|
133 | int toprow=cy/rowheight;
|
---|
134 | int bottomrow=(cy+ch+rowheight-1)/rowheight;
|
---|
135 | int colwidth=bg.width();
|
---|
136 | int leftcol=cx/colwidth;
|
---|
137 | int rightcol=(cx+cw+colwidth-1)/colwidth;
|
---|
138 | for (int r=toprow; r<=bottomrow; r++) {
|
---|
139 | int py=r*rowheight;
|
---|
140 | for (int c=leftcol; c<=rightcol; c++) {
|
---|
141 | int px=c*colwidth;
|
---|
142 | p->drawPixmap(px, py, bg);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | } else {
|
---|
146 | p->fillRect(cx, cy, cw, ch, QColor(240,222,208));
|
---|
147 | }
|
---|
148 |
|
---|
149 | // The Numbers
|
---|
150 | {
|
---|
151 | QFontMetrics fm=p->fontMetrics();
|
---|
152 | int rowheight=fm.lineSpacing();
|
---|
153 | int toprow=cy/rowheight;
|
---|
154 | int bottomrow=(cy+ch+rowheight-1)/rowheight;
|
---|
155 | int colwidth=fm.width("00000,000000 ")+3;
|
---|
156 | int leftcol=cx/colwidth;
|
---|
157 | int rightcol=(cx+cw+colwidth-1)/colwidth;
|
---|
158 | QString str;
|
---|
159 | for (int r=toprow; r<=bottomrow; r++) {
|
---|
160 | int py=r*rowheight;
|
---|
161 | for (int c=leftcol; c<=rightcol; c++) {
|
---|
162 | int px=c*colwidth;
|
---|
163 | str.sprintf("%d,%d",c,r);
|
---|
164 | p->drawText(px+3, py+fm.ascent(), str);
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | // The Big Hint
|
---|
169 | if (leftcol<10 && toprow<5) {
|
---|
170 | p->setFont(QFont("Charter",30));
|
---|
171 | p->setPen(red);
|
---|
172 | QString text;
|
---|
173 | text.sprintf("HINT: Look at %d,%d",215000/colwidth,115000/rowheight);
|
---|
174 | p->drawText(100,50,text);
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | // The Big X
|
---|
179 | {
|
---|
180 | if (cx+cw>200000 && cy+ch>100000 && cx<230000 && cy<130000) {
|
---|
181 | // Note that some X server cannot even handle co-ordinates
|
---|
182 | // beyond about 4000, so you might not see this.
|
---|
183 | p->drawLine(200000,100000,229999,129999);
|
---|
184 | p->drawLine(229999,100000,200000,129999);
|
---|
185 |
|
---|
186 | // X marks the spot!
|
---|
187 | p->setFont(QFont("Charter",100));
|
---|
188 | p->setPen(blue);
|
---|
189 | p->drawText(215000-500,115000-100,1000,200,AlignCenter,"YOU WIN!!!!!");
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | private:
|
---|
195 | QPixmap bg;
|
---|
196 | };
|
---|
197 |
|
---|
198 | class ScrollViewExample : public QWidget {
|
---|
199 | Q_OBJECT
|
---|
200 |
|
---|
201 | public:
|
---|
202 | ScrollViewExample(int technique, QWidget* parent=0, const char* name=0) :
|
---|
203 | QWidget(parent,name)
|
---|
204 | {
|
---|
205 | QMenuBar* menubar = new QMenuBar(this);
|
---|
206 | Q_CHECK_PTR( menubar );
|
---|
207 |
|
---|
208 | QPopupMenu* file = new QPopupMenu( menubar );
|
---|
209 | Q_CHECK_PTR( file );
|
---|
210 | menubar->insertItem( "&File", file );
|
---|
211 | file->insertItem( "Quit", qApp, SLOT(quit()) );
|
---|
212 |
|
---|
213 | vp_options = new QPopupMenu( menubar );
|
---|
214 | Q_CHECK_PTR( vp_options );
|
---|
215 | vp_options->setCheckable( TRUE );
|
---|
216 | menubar->insertItem( "&ScrollView", vp_options );
|
---|
217 | connect( vp_options, SIGNAL(activated(int)),
|
---|
218 | this, SLOT(doVPMenuItem(int)) );
|
---|
219 |
|
---|
220 | vauto_id = vp_options->insertItem( "Vertical Auto" );
|
---|
221 | vaoff_id = vp_options->insertItem( "Vertical AlwaysOff" );
|
---|
222 | vaon_id = vp_options->insertItem( "Vertical AlwaysOn" );
|
---|
223 | vp_options->insertSeparator();
|
---|
224 | hauto_id = vp_options->insertItem( "Horizontal Auto" );
|
---|
225 | haoff_id = vp_options->insertItem( "Horizontal AlwaysOff" );
|
---|
226 | haon_id = vp_options->insertItem( "Horizontal AlwaysOn" );
|
---|
227 | vp_options->insertSeparator();
|
---|
228 | corn_id = vp_options->insertItem( "cornerWidget" );
|
---|
229 |
|
---|
230 | if (technique == 1) {
|
---|
231 | vp = new QScrollView(this);
|
---|
232 | BigShrinker *bs = new BigShrinker(0);//(vp->viewport());
|
---|
233 | vp->addChild(bs);
|
---|
234 | bs->setAcceptDrops(TRUE);
|
---|
235 | QObject::connect(bs, SIGNAL(clicked(int,int)),
|
---|
236 | vp, SLOT(center(int,int)));
|
---|
237 | } else {
|
---|
238 | vp = new BigMatrix(this);
|
---|
239 | if ( technique == 3 )
|
---|
240 | vp->enableClipper(TRUE);
|
---|
241 | srand(1);
|
---|
242 | for (int i=0; i<30; i++) {
|
---|
243 | QMultiLineEdit *l = new QMultiLineEdit(vp->viewport(),"First");
|
---|
244 | l->setText("Drag out more of these.");
|
---|
245 | l->resize(100,100);
|
---|
246 | vp->addChild(l, rand()%800, rand()%10000);
|
---|
247 | }
|
---|
248 | vp->viewport()->setBackgroundMode(NoBackground);
|
---|
249 | }
|
---|
250 |
|
---|
251 | f_options = new QPopupMenu( menubar );
|
---|
252 | Q_CHECK_PTR( f_options );
|
---|
253 | f_options->setCheckable( TRUE );
|
---|
254 | menubar->insertItem( "F&rame", f_options );
|
---|
255 | connect( f_options, SIGNAL(activated(int)),
|
---|
256 | this, SLOT(doFMenuItem(int)) );
|
---|
257 |
|
---|
258 | f_options->insertItem( "No Frame", style_id );
|
---|
259 | f_options->insertItem( "Box", style_id|QFrame::Box );
|
---|
260 | f_options->insertItem( "Panel", style_id|QFrame::Panel );
|
---|
261 | f_options->insertItem( "WinPanel", style_id|QFrame::WinPanel );
|
---|
262 | f_options->insertSeparator();
|
---|
263 | f_options->insertItem( "Plain", style_id|QFrame::Plain );
|
---|
264 | f_options->insertItem( "Raised", style_id|QFrame::Raised );
|
---|
265 | f_laststyle = f_options->indexOf(
|
---|
266 | f_options->insertItem( "Sunken", style_id|QFrame::Sunken ));
|
---|
267 | f_options->insertSeparator();
|
---|
268 | lw_options = new QPopupMenu( menubar );
|
---|
269 | Q_CHECK_PTR( lw_options );
|
---|
270 | lw_options->setCheckable( TRUE );
|
---|
271 | for (int lw = 1; lw <= max_lw; lw++) {
|
---|
272 | QString str;
|
---|
273 | str.sprintf("%d Pixels", lw);
|
---|
274 | lw_options->insertItem( str, lw_id | lw );
|
---|
275 | }
|
---|
276 | f_options->insertItem( "Line Width", lw_options );
|
---|
277 | connect( lw_options, SIGNAL(activated(int)),
|
---|
278 | this, SLOT(doFMenuItem(int)) );
|
---|
279 | mlw_options = new QPopupMenu( menubar );
|
---|
280 | Q_CHECK_PTR( mlw_options );
|
---|
281 | mlw_options->setCheckable( TRUE );
|
---|
282 | for (int mlw = 0; mlw <= max_mlw; mlw++) {
|
---|
283 | QString str;
|
---|
284 | str.sprintf("%d Pixels", mlw);
|
---|
285 | mlw_options->insertItem( str, mlw_id | mlw );
|
---|
286 | }
|
---|
287 | f_options->insertItem( "Midline Width", mlw_options );
|
---|
288 | connect( mlw_options, SIGNAL(activated(int)),
|
---|
289 | this, SLOT(doFMenuItem(int)) );
|
---|
290 | mw_options = new QPopupMenu( menubar );
|
---|
291 | Q_CHECK_PTR( mw_options );
|
---|
292 | mw_options->setCheckable( TRUE );
|
---|
293 | for (int mw = 0; mw <= max_mw; mw++) {
|
---|
294 | QString str;
|
---|
295 | str.sprintf("%d Pixels", mw);
|
---|
296 | mw_options->insertItem( str, mw_id | mw );
|
---|
297 | }
|
---|
298 | f_options->insertItem( "Margin Width", mw_options );
|
---|
299 | connect( mw_options, SIGNAL(activated(int)),
|
---|
300 | this, SLOT(doFMenuItem(int)) );
|
---|
301 |
|
---|
302 | setVPMenuItems();
|
---|
303 | setFMenuItems();
|
---|
304 |
|
---|
305 | QVBoxLayout* vbox = new QVBoxLayout(this);
|
---|
306 | vbox->setMenuBar(menubar);
|
---|
307 | menubar->setSeparator(QMenuBar::InWindowsStyle);
|
---|
308 | vbox->addWidget(vp);
|
---|
309 | vbox->activate();
|
---|
310 |
|
---|
311 | corner = new QSizeGrip(this);
|
---|
312 | corner->hide();
|
---|
313 | }
|
---|
314 |
|
---|
315 | private slots:
|
---|
316 | void doVPMenuItem(int id)
|
---|
317 | {
|
---|
318 | if (id == vauto_id ) {
|
---|
319 | vp->setVScrollBarMode(QScrollView::Auto);
|
---|
320 | } else if (id == vaoff_id) {
|
---|
321 | vp->setVScrollBarMode(QScrollView::AlwaysOff);
|
---|
322 | } else if (id == vaon_id) {
|
---|
323 | vp->setVScrollBarMode(QScrollView::AlwaysOn);
|
---|
324 | } else if (id == hauto_id) {
|
---|
325 | vp->setHScrollBarMode(QScrollView::Auto);
|
---|
326 | } else if (id == haoff_id) {
|
---|
327 | vp->setHScrollBarMode(QScrollView::AlwaysOff);
|
---|
328 | } else if (id == haon_id) {
|
---|
329 | vp->setHScrollBarMode(QScrollView::AlwaysOn);
|
---|
330 | } else if (id == corn_id) {
|
---|
331 | bool corn = !vp->cornerWidget();
|
---|
332 | vp->setCornerWidget(corn ? corner : 0);
|
---|
333 | } else {
|
---|
334 | return; // Not for us to process.
|
---|
335 | }
|
---|
336 | setVPMenuItems();
|
---|
337 | }
|
---|
338 |
|
---|
339 | void setVPMenuItems()
|
---|
340 | {
|
---|
341 | QScrollView::ScrollBarMode vm = vp->vScrollBarMode();
|
---|
342 | vp_options->setItemChecked( vauto_id, vm == QScrollView::Auto );
|
---|
343 | vp_options->setItemChecked( vaoff_id, vm == QScrollView::AlwaysOff );
|
---|
344 | vp_options->setItemChecked( vaon_id, vm == QScrollView::AlwaysOn );
|
---|
345 |
|
---|
346 | QScrollView::ScrollBarMode hm = vp->hScrollBarMode();
|
---|
347 | vp_options->setItemChecked( hauto_id, hm == QScrollView::Auto );
|
---|
348 | vp_options->setItemChecked( haoff_id, hm == QScrollView::AlwaysOff );
|
---|
349 | vp_options->setItemChecked( haon_id, hm == QScrollView::AlwaysOn );
|
---|
350 |
|
---|
351 | vp_options->setItemChecked( corn_id, !!vp->cornerWidget() );
|
---|
352 | }
|
---|
353 |
|
---|
354 | void doFMenuItem(int id)
|
---|
355 | {
|
---|
356 | if (id & style_id) {
|
---|
357 | int sty;
|
---|
358 |
|
---|
359 | if (id == style_id) {
|
---|
360 | sty = 0;
|
---|
361 | } else if (id & QFrame::MShape) {
|
---|
362 | sty = vp->frameStyle()&QFrame::MShadow;
|
---|
363 | sty = (sty ? sty : QFrame::Plain) | (id&QFrame::MShape);
|
---|
364 | } else {
|
---|
365 | sty = vp->frameStyle()&QFrame::MShape;
|
---|
366 | sty = (sty ? sty : QFrame::Box) | (id&QFrame::MShadow);
|
---|
367 | }
|
---|
368 | vp->setFrameStyle(sty);
|
---|
369 | } else if (id & lw_id) {
|
---|
370 | vp->setLineWidth(id&~lw_id);
|
---|
371 | } else if (id & mlw_id) {
|
---|
372 | vp->setMidLineWidth(id&~mlw_id);
|
---|
373 | } else {
|
---|
374 | vp->setMargin(id&~mw_id);
|
---|
375 | }
|
---|
376 |
|
---|
377 | vp->update();
|
---|
378 | setFMenuItems();
|
---|
379 | }
|
---|
380 |
|
---|
381 | void setFMenuItems()
|
---|
382 | {
|
---|
383 | int sty = vp->frameStyle();
|
---|
384 |
|
---|
385 | f_options->setItemChecked( style_id, !sty );
|
---|
386 |
|
---|
387 | for (int i=1; i <= f_laststyle; i++) {
|
---|
388 | int id = f_options->idAt(i);
|
---|
389 | if (id & QFrame::MShape)
|
---|
390 | f_options->setItemChecked( id,
|
---|
391 | ((id&QFrame::MShape) == (sty&QFrame::MShape)) );
|
---|
392 | else
|
---|
393 | f_options->setItemChecked( id,
|
---|
394 | ((id&QFrame::MShadow) == (sty&QFrame::MShadow)) );
|
---|
395 | }
|
---|
396 |
|
---|
397 | for (int lw=1; lw<=max_lw; lw++)
|
---|
398 | lw_options->setItemChecked( lw_id|lw, vp->lineWidth() == lw );
|
---|
399 |
|
---|
400 | for (int mlw=0; mlw<=max_mlw; mlw++)
|
---|
401 | mlw_options->setItemChecked( mlw_id|mlw, vp->midLineWidth() == mlw );
|
---|
402 |
|
---|
403 | for (int mw=0; mw<=max_mw; mw++)
|
---|
404 | mw_options->setItemChecked( mw_id|mw, vp->margin() == mw );
|
---|
405 | }
|
---|
406 |
|
---|
407 | private:
|
---|
408 | QScrollView* vp;
|
---|
409 | QPopupMenu* vp_options;
|
---|
410 | QPopupMenu* f_options;
|
---|
411 | QPopupMenu* lw_options;
|
---|
412 | QPopupMenu* mlw_options;
|
---|
413 | QPopupMenu* mw_options;
|
---|
414 | QSizeGrip* corner;
|
---|
415 |
|
---|
416 | int vauto_id, vaoff_id, vaon_id,
|
---|
417 | hauto_id, haoff_id, haon_id,
|
---|
418 | corn_id;
|
---|
419 |
|
---|
420 | int f_laststyle;
|
---|
421 | };
|
---|
422 |
|
---|
423 | int main( int argc, char **argv )
|
---|
424 | {
|
---|
425 | QApplication a( argc, argv );
|
---|
426 |
|
---|
427 | ScrollViewExample ve1(1,0,"ve1");
|
---|
428 | ScrollViewExample ve2(2,0,"ve2");
|
---|
429 | ScrollViewExample ve3(3,0,"ve3");
|
---|
430 | ve1.setCaption("Qt Example - Scrollviews");
|
---|
431 | ve1.show();
|
---|
432 | ve2.setCaption("Qt Example - Scrollviews");
|
---|
433 | ve2.show();
|
---|
434 | ve3.setCaption("Qt Example - Scrollviews");
|
---|
435 | ve3.show();
|
---|
436 |
|
---|
437 | QObject::connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));
|
---|
438 |
|
---|
439 | return a.exec();
|
---|
440 | }
|
---|
441 |
|
---|
442 | #include "scrollview.moc"
|
---|