1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the test suite of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
43
#include <QtTest/QtTest>
44
45
#include "q3scrollview.h"
46
#include <qapplication.h>
47
#include <qpainter.h>
48
#include <qlabel.h>
49
50
#include <q3scrollview.h>
51
52
//TESTED_CLASS=
53
//TESTED_FILES=compat/widgets/q3scrollview.h compat/widgets/q3scrollview.cpp
54
55
QT_FORWARD_DECLARE_CLASS(QTestScrollView)
56
Q_DECLARE_METATYPE(QTestScrollView *)
57
Q_DECLARE_METATYPE(QPixmap)
58
59
class tst_Q3ScrollView : public QObject
60
{
61
Q_OBJECT
62
63
public:
64
    tst_Q3ScrollView();
65
    virtual ~tst_Q3ScrollView();
66
67
68
public slots:
69
    void initTestCase();
70
    void cleanupTestCase();
71
    void init();
72
    void cleanup();
73
private slots:
74
    void resizePolicy();
75
    void setResizePolicy();
76
77
private:
78
    QTestScrollView *testWidget;
79
};
80
81
// *************************************************
82
// *************************************************
83
84
#include <q3scrollview.h>
85
#include <qapplication.h>
86
#include <qmenubar.h>
87
#include <q3popupmenu.h>
88
#include <qpushbutton.h>
89
#include <qpainter.h>
90
#include <qpixmap.h>
91
#include <qmessagebox.h>
92
#include <qlayout.h>
93
#include <qlabel.h>
94
#include <q3multilineedit.h>
95
#include <qsizegrip.h>
96
#include <stdlib.h>
97
98
99
100
static const int style_id	= 0x1000;
101
static const int lw_id		= 0x2000;
102
static const int mlw_id		= 0x4000;
103
static const int mw_id		= 0x8000;
104
static const int max_lw		= 16;
105
static const int max_mlw	= 5;
106
static const int max_mw		= 10;
107
108
109
class BigShrinker : public QFrame
110
{
111
    Q_OBJECT
112
public:
113
    BigShrinker(QWidget* parent) : QFrame(parent)
114
    {
115
	setFrameStyle(QFrame::Box | QFrame::Sunken);
116
	int h=35;
117
	int b=0;
118
	for (int y=0; y<2000-h; y+=h+10) {
119
	    QString str;
120
	    str.sprintf("Button %d", b++);
121
	    (new QPushButton(str, this))->move(y/2,y);
122
	}
123
	resize(1000,2000);
124
    }
125
};
126
127
class BigMatrix : public Q3ScrollView
128
{
129
    Q3MultiLineEdit *dragging;
130
public:
131
    BigMatrix(QWidget* parent) : Q3ScrollView(parent,"matrix", Qt::WStaticContents),
132
	bg("bg.ppm")
133
    {
134
	bg.load("bg.ppm");
135
	resizeContents(400000,300000);
136
137
	dragging = 0;
138
    }
139
140
    void viewportMousePressEvent(QMouseEvent* e)
141
    {
142
	int x, y;
143
	viewportToContents( e->x(),  e->y(), x, y );
144
	dragging = new Q3MultiLineEdit(viewport(),"Another");
145
	dragging->setText("Thanks!");
146
	dragging->resize(100,100);
147
	addChild(dragging, x, y);
148
	showChild(dragging);
149
    }
150
151
    void viewportMouseReleaseEvent(QMouseEvent*)
152
    {
153
	dragging = 0;
154
    }
155
156
    void viewportMouseMoveEvent(QMouseEvent* e)
157
    {
158
	if ( dragging ) {
159
	    int mx, my;
160
	    viewportToContents( e->x(),  e->y(), mx, my );
161
	    int cx = childX(dragging);
162
	    int cy = childY(dragging);
163
	    int w = mx - cx + 1;
164
	    int h = my - cy + 1;
165
	    QString msg;
166
	    msg.sprintf("at (%d,%d) %d by %d",cx,cy,w,h);
167
	    dragging->setText(msg);
168
	    dragging->resize(w,h);
169
	}
170
    }
171
172
protected:
173
    void drawContents(QPainter* p, int cx, int cy, int cw, int ch)
174
    {
175
	// The Background
176
	if ( !bg.isNull() ) {
177
	    int rowheight=bg.height();
178
	    int toprow=cy/rowheight;
179
	    int bottomrow=(cy+ch+rowheight-1)/rowheight;
180
	    int colwidth=bg.width();
181
	    int leftcol=cx/colwidth;
182
	    int rightcol=(cx+cw+colwidth-1)/colwidth;
183
	    for (int r=toprow; r<=bottomrow; r++) {
184
		int py=r*rowheight;
185
		for (int c=leftcol; c<=rightcol; c++) {
186
		    int px=c*colwidth;
187
		    p->drawPixmap(px, py, bg);
188
		}
189
	    }
190
	} else {
191
	    p->fillRect(cx, cy, cw, ch, QColor(240,222,208));
192
	}
193
194
	// The Numbers
195
	{
196
	    QFontMetrics fm=p->fontMetrics();
197
	    int rowheight=fm.lineSpacing();
198
	    int toprow=cy/rowheight;
199
	    int bottomrow=(cy+ch+rowheight-1)/rowheight;
200
	    int colwidth=fm.width("00000,000000 ")+3;
201
	    int leftcol=cx/colwidth;
202
	    int rightcol=(cx+cw+colwidth-1)/colwidth;
203
	    QString str;
204
	    for (int r=toprow; r<=bottomrow; r++) {
205
		int py=r*rowheight;
206
		for (int c=leftcol; c<=rightcol; c++) {
207
		    int px=c*colwidth;
208
		    str.sprintf("%d,%d",c,r);
209
		    p->drawText(px+3, py+fm.ascent(), str);
210
		}
211
	    }
212
213
/*
214
	    // The Big Hint
215
	    if (leftcol<10 && toprow<5) {
216
		p->setFont(QFont("Charter",30));
217
		p->setPen(red);
218
		QString text;
219
		text.sprintf("HINT:  Look at %d,%d",215000/colwidth,115000/rowheight);
220
		p->drawText(100,50,text);
221
	    }
222
*/
223
        }
224
225
	// The Big X
226
	{
227
	    if (cx+cw>200000 && cy+ch>100000 && cx<230000 && cy<130000) {
228
		// Note that some X server cannot even handle co-ordinates
229
		// beyond about 4000, so you might not see this.
230
		p->drawLine(200000,100000,229999,129999);
231
		p->drawLine(229999,100000,200000,129999);
232
233
		// X marks the spot!
234
		p->setFont(QFont("Charter",100));
235
		p->setPen(Qt::blue);
236
		p->drawText(215000-500,115000-100,1000,200,Qt::AlignCenter,"YOU WIN!!!!!");
237
	    }
238
	}
239
    }
240
241
private:
242
    QPixmap bg;
243
};
244
245
class ScrollViewExample : public QWidget
246
{
247
    Q_OBJECT
248
249
public:
250
    ScrollViewExample( int technique, QWidget* parent=0, const char* name=0) : QWidget(parent,name)
251
    {
252
/*
253
	QMenuBar* menubar = new QMenuBar(this);
254
	Q_CHECK_PTR( menubar );
255
256
	QPopupMenu* file = new QPopupMenu( menubar );
257
	Q_CHECK_PTR( file );
258
	menubar->insertItem( "&File", file );
259
	file->insertItem( "Quit", qApp,  SLOT(quit()) );
260
261
	vp_options = new QPopupMenu( menubar );
262
	Q_CHECK_PTR( vp_options );
263
	vp_options->setCheckable( TRUE );
264
	menubar->insertItem( "&ScrollView", vp_options );
265
	connect( vp_options, SIGNAL(activated(int)), this, SLOT(doVPMenuItem(int)) );
266
267
	vauto_id = vp_options->insertItem( "Vertical Auto" );
268
	vaoff_id = vp_options->insertItem( "Vertical AlwaysOff" );
269
	vaon_id = vp_options->insertItem( "Vertical AlwaysOn" );
270
	vp_options->insertSeparator();
271
	hauto_id = vp_options->insertItem( "Horizontal Auto" );
272
	haoff_id = vp_options->insertItem( "Horizontal AlwaysOff" );
273
	haon_id = vp_options->insertItem( "Horizontal AlwaysOn" );
274
	vp_options->insertSeparator();
275
	corn_id = vp_options->insertItem( "cornerWidget" );
276
*/
277
	if (technique == 1) {
278
	    vp = new Q3ScrollView(this);
279
	    BigShrinker *bs = new BigShrinker(0);
280
	    vp->addChild(bs);
281
	    bs->setAcceptDrops(TRUE);
282
	} else {
283
	    vp = new BigMatrix(this);
284
	    if ( technique == 3 )
285
		vp->enableClipper(TRUE);
286
	    srand(1);
287
/*
288
	    for (int i=0; i<30; i++) {
289
		QMultiLineEdit *l = new QMultiLineEdit(vp->viewport(),"First");
290
		l->setText("Drag out more of these.");
291
		l->resize(100,100);
292
		vp->addChild(l, rand()%800, rand()%10000);
293
	    }
294
*/
295
            vp->viewport()->setBackgroundMode(Qt::NoBackground);
296
	}
297
298
/*
299
	f_options = new QPopupMenu( menubar );
300
	Q_CHECK_PTR( f_options );
301
	f_options->setCheckable( TRUE );
302
	menubar->insertItem( "F&rame", f_options );
303
	connect( f_options, SIGNAL(activated(int)), this, SLOT(doFMenuItem(int)) );
304
305
	f_options->insertItem( "No Frame", style_id );
306
	f_options->insertItem( "Box", style_id|QFrame::Box );
307
	f_options->insertItem( "Panel", style_id|QFrame::Panel );
308
	f_options->insertItem( "WinPanel", style_id|QFrame::WinPanel );
309
	f_options->insertSeparator();
310
	f_options->insertItem( "Plain", style_id|QFrame::Plain );
311
	f_options->insertItem( "Raised", style_id|QFrame::Raised );
312
	f_laststyle = f_options->indexOf(
313
	    f_options->insertItem( "Sunken", style_id|QFrame::Sunken ));
314
	f_options->insertSeparator();
315
316
        lw_options = new QPopupMenu( menubar );
317
	Q_CHECK_PTR( lw_options );
318
	lw_options->setCheckable( TRUE );
319
	for (int lw = 1; lw <= max_lw; lw++) {
320
	    QString str;
321
	    str.sprintf("%d Pixels", lw);
322
	    lw_options->insertItem( str, lw_id | lw );
323
	}
324
	f_options->insertItem( "Line Width", lw_options );
325
	connect( lw_options, SIGNAL(activated(int)), this, SLOT(doFMenuItem(int)) );
326
327
        mlw_options = new QPopupMenu( menubar );
328
	Q_CHECK_PTR( mlw_options );
329
	mlw_options->setCheckable( TRUE );
330
	for (int mlw = 0; mlw <= max_mlw; mlw++) {
331
	    QString str;
332
	    str.sprintf("%d Pixels", mlw);
333
	    mlw_options->insertItem( str, mlw_id | mlw );
334
	}
335
	f_options->insertItem( "Midline Width", mlw_options );
336
	connect( mlw_options, SIGNAL(activated(int)), this, SLOT(doFMenuItem(int)) );
337
338
        mw_options = new QPopupMenu( menubar );
339
	Q_CHECK_PTR( mw_options );
340
	mw_options->setCheckable( TRUE );
341
	for (int mw = 0; mw <= max_mw; mw++) {
342
	    QString str;
343
	    str.sprintf("%d Pixels", mw);
344
	    mw_options->insertItem( str, mw_id | mw );
345
	}
346
	f_options->insertItem( "Margin Width", mw_options );
347
	connect( mw_options, SIGNAL(activated(int)), this, SLOT(doFMenuItem(int)) );
348
349
	setVPMenuItems();
350
	setFMenuItems();
351
*/
352
	QVBoxLayout* vbox = new QVBoxLayout(this);
353
/*
354
	vbox->setMenuBar(menubar);
355
	menubar->setSeparator(QMenuBar::InWindowsStyle);
356
*/
357
        vbox->addWidget(vp);
358
	vbox->activate();
359
360
	corner = new QSizeGrip(this);
361
	corner->hide();
362
    }
363
364
    void center( int x, int y )
365
    {
366
        vp->center( x, y );
367
    }
368
369
private slots:
370
/*
371
    void doVPMenuItem(int id)
372
    {
373
	if (id == vauto_id ) {
374
	    vp->setVScrollBarMode(Q3ScrollView::Auto);
375
	} else if (id == vaoff_id) {
376
	    vp->setVScrollBarMode(Q3ScrollView::AlwaysOff);
377
	} else if (id == vaon_id) {
378
	    vp->setVScrollBarMode(Q3ScrollView::AlwaysOn);
379
	} else if (id == hauto_id) {
380
	    vp->setHScrollBarMode(Q3ScrollView::Auto);
381
	} else if (id == haoff_id) {
382
	    vp->setHScrollBarMode(Q3ScrollView::AlwaysOff);
383
	} else if (id == haon_id) {
384
	    vp->setHScrollBarMode(Q3ScrollView::AlwaysOn);
385
	} else if (id == corn_id) {
386
	    bool corn = !vp->cornerWidget();
387
	    vp->setCornerWidget(corn ? corner : 0);
388
	} else {
389
	    return; // Not for us to process.
390
	}
391
	setVPMenuItems();
392
    }
393
*/
394
395
/*
396
    void setVPMenuItems()
397
    {
398
	Q3ScrollView::ScrollBarMode vm = vp->vScrollBarMode();
399
	vp_options->setItemChecked( vauto_id, vm == Q3ScrollView::Auto );
400
	vp_options->setItemChecked( vaoff_id, vm == Q3ScrollView::AlwaysOff );
401
	vp_options->setItemChecked( vaon_id, vm == Q3ScrollView::AlwaysOn );
402
403
	Q3ScrollView::ScrollBarMode hm = vp->hScrollBarMode();
404
	vp_options->setItemChecked( hauto_id, hm == Q3ScrollView::Auto );
405
	vp_options->setItemChecked( haoff_id, hm == Q3ScrollView::AlwaysOff );
406
	vp_options->setItemChecked( haon_id, hm == Q3ScrollView::AlwaysOn );
407
408
	vp_options->setItemChecked( corn_id, !!vp->cornerWidget() );
409
    }
410
*/
411
412
/*
413
    void doFMenuItem(int id)
414
    {
415
	if (id & style_id) {
416
	    int sty;
417
418
	    if (id == style_id) {
419
		sty = 0;
420
	    } else if (id & QFrame::MShape) {
421
		sty = vp->frameStyle()&QFrame::MShadow;
422
		sty = (sty ? sty : QFrame::Plain) | (id&QFrame::MShape);
423
	    } else {
424
		sty = vp->frameStyle()&QFrame::MShape;
425
		sty = (sty ? sty : QFrame::Box) | (id&QFrame::MShadow);
426
	    }
427
	    vp->setFrameStyle(sty);
428
	} else if (id & lw_id) {
429
	    vp->setLineWidth(id&~lw_id);
430
	} else if (id & mlw_id) {
431
	    vp->setMidLineWidth(id&~mlw_id);
432
	} else {
433
	    vp->setMargin(id&~mw_id);
434
	}
435
436
	vp->update();
437
	setFMenuItems();
438
    }
439
*/
440
441
/*
442
    void setFMenuItems()
443
    {
444
	int sty = vp->frameStyle();
445
446
	f_options->setItemChecked( style_id, !sty );
447
448
	for (int i=1; i <= f_laststyle; i++) {
449
	    int id = f_options->idAt(i);
450
	    if (id & QFrame::MShape)
451
		f_options->setItemChecked( id,
452
		    ((id&QFrame::MShape) == (sty&QFrame::MShape)) );
453
	    else
454
		f_options->setItemChecked( id,
455
		    ((id&QFrame::MShadow) == (sty&QFrame::MShadow)) );
456
	}
457
458
	for (int lw=1; lw<=max_lw; lw++)
459
	    lw_options->setItemChecked( lw_id|lw, vp->lineWidth() == lw );
460
461
	for (int mlw=0; mlw<=max_mlw; mlw++)
462
	    mlw_options->setItemChecked( mlw_id|mlw, vp->midLineWidth() == mlw );
463
464
	for (int mw=0; mw<=max_mw; mw++)
465
	    mw_options->setItemChecked( mw_id|mw, vp->margin() == mw );
466
    }
467
*/
468
469
private:
470
    Q3ScrollView* vp;
471
/*
472
    QPopupMenu* vp_options;
473
    QPopupMenu* f_options;
474
    QPopupMenu* lw_options;
475
    QPopupMenu* mlw_options;
476
    QPopupMenu* mw_options;
477
*/
478
    QSizeGrip* corner;
479
480
/*
481
    int vauto_id, vaoff_id, vaon_id,
482
	hauto_id, haoff_id, haon_id,
483
	corn_id;
484
485
    int f_laststyle;
486
*/
487
};
488
489
Q_DECLARE_METATYPE(ScrollViewExample *)
490
491
// *************************************************
492
// *************************************************
493
494
class QTestScrollView : public Q3ScrollView
495
{
496
public:
497
    QString scroll_text;
498
499
//private:
500
    void drawContents( QPainter *p, int cx, int cy, int cw, int ch )
501
    {
502
        if (scroll_text == "") {
503
            p->fillRect( cx, cy, cw, ch, Qt::white );
504
505
        } else {
506
            p->fillRect( cx, cy, cw, ch, Qt::white );
507
508
            QFont f;
509
            f.setFamily( "tahoma" );
510
            f.setPointSize( 24 );
511
            QFontMetrics fm(f);
512
            p->setFont( f );
513
514
            int height = fm.height();
515
            int y = 10;
516
            int x = 10;
517
            for( int i = 0; i < (int)scroll_text.length(); i++ ) {
518
                if ( !scroll_text[i].isMark() )
519
                    y += height + 5;
520
521
                p->setPen( Qt::red );
522
                QRect br = fm.boundingRect( scroll_text.at(i) );
523
                br.moveBy( x, y );
524
                p->drawRect( br );
525
                p->setPen( Qt::black );
526
                p->drawText( x, y, scroll_text, i, 1 );
527
            }
528
            resizeContents( 170, y + 100 );
529
        }
530
    }
531
};
532
533
// *************************************************
534
// *************************************************
535
536
tst_Q3ScrollView::tst_Q3ScrollView()
537
{
538
}
539
540
tst_Q3ScrollView::~tst_Q3ScrollView()
541
{
542
543
}
544
545
void tst_Q3ScrollView::initTestCase()
546
{
547
    testWidget = new QTestScrollView;
548
    testWidget->resize(200,200);
549
    qApp->setMainWidget(testWidget);
550
    testWidget->show();
551
}
552
553
void tst_Q3ScrollView::cleanupTestCase()
554
{
555
    delete testWidget;
556
    testWidget = 0;
557
}
558
559
void tst_Q3ScrollView::init()
560
{
561
    testWidget->setResizePolicy( Q3ScrollView::Default );
562
    testWidget->scroll_text = "";
563
}
564
565
void tst_Q3ScrollView::cleanup()
566
{
567
    if (QTest::currentTestFunction() == QLatin1String("center") ) {
568
        qApp->setMainWidget( testWidget );
569
        testWidget->show();
570
    }
571
}
572
573
void tst_Q3ScrollView::resizePolicy()
574
{
575
    DEPENDS_ON( "setResizePolicy");
576
}
577
578
void tst_Q3ScrollView::setResizePolicy()
579
{
580
    testWidget->setResizePolicy( Q3ScrollView::Default );
581
    QVERIFY( testWidget->resizePolicy() == Q3ScrollView::Default );
582
583
    testWidget->setResizePolicy( Q3ScrollView::Manual );
584
    QVERIFY( testWidget->resizePolicy() == Q3ScrollView::Manual );
585
586
    testWidget->setResizePolicy( Q3ScrollView::AutoOne );
587
    QVERIFY( testWidget->resizePolicy() == Q3ScrollView::AutoOne );
588
589
    testWidget->setResizePolicy( Q3ScrollView::AutoOneFit );
590
    QVERIFY( testWidget->resizePolicy() == Q3ScrollView::AutoOneFit );
591
}
592
593
QTEST_MAIN(tst_Q3ScrollView)
594
#include "tst_qscrollview.moc"