?? qscrollview.cpp
字號:
/****************************************************************************** $Id: qt/src/widgets/qscrollview.cpp 2.3.12 edited 2005-10-27 $**** Implementation of QScrollView class**** Created : 950524**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of the widgets module of the Qt GUI Toolkit.**** This file may be distributed under the terms of the Q Public License** as defined by Trolltech AS of Norway and appearing in the file** LICENSE.QPL included in the packaging of this file.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition** licenses may use this file in accordance with the Qt Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Qt Commercial License Agreements.** See http://www.trolltech.com/qpl/ for QPL licensing information.** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "qwidget.h"#ifndef QT_NO_SCROLLVIEW#include "qscrollbar.h"#include "qobjectlist.h"#include "qobjectdict.h"#include "qpainter.h"#include "qpixmap.h"#include "qfocusdata.h"#include "qscrollview.h"#include "qptrdict.h"#include "qapplication.h"#include "qtimer.h"#ifdef QT_KEYPAD_MODEextern bool qt_modalEditingEnabled;#endifconst int coord_limit = 4000;static const int autoscroll_margin = 16;static const int initialScrollTime = 30;static const int initialScrollAccel = 5;struct QSVChildRec { QSVChildRec(QWidget* c, int xx, int yy) : child(c), x(xx), y(yy) { } void moveBy(QScrollView* sv, int dx, int dy, QWidget* clipped_viewport) { moveTo( sv, x+dx, y+dy, clipped_viewport ); } void moveTo(QScrollView* sv, int xx, int yy, QWidget* clipped_viewport) { if ( x != xx || y != yy ) { x = xx; y = yy; hideOrShow(sv,clipped_viewport); } } void hideOrShow(QScrollView* sv, QWidget* clipped_viewport) { if ( clipped_viewport ) { if ( x+child->width() < sv->contentsX()+clipped_viewport->x() || x > sv->contentsX()+clipped_viewport->width() || y+child->height() < sv->contentsY()+clipped_viewport->y() || y > sv->contentsY()+clipped_viewport->height() ) { child->move(clipped_viewport->width(), clipped_viewport->height()); } else { child->move(x-sv->contentsX()-clipped_viewport->x(), y-sv->contentsY()-clipped_viewport->y()); } } else { child->move(x-sv->contentsX(), y-sv->contentsY()); } } QWidget* child; int x, y;};class QClipperWidget : public QWidget {public: QClipperWidget( QWidget * parent=0, const char * name=0, WFlags f=0 ) : QWidget ( parent,name,f) { blockFocus = FALSE; } bool focusNextPrevChild( bool next ) { if ( !blockFocus ) return QWidget::focusNextPrevChild( next ); else return TRUE; } bool blockFocus;};struct QScrollViewData { QScrollViewData(QWidget* parent, int vpwflags) : hbar( QScrollBar::Horizontal, parent, "qt_hbar" ), vbar( QScrollBar::Vertical, parent, "qt_vbar" ), viewport( parent, "qt_viewport", vpwflags ), clipped_viewport( 0 ), flags( vpwflags ), vx( 0 ), vy( 0 ), vwidth( 1 ), vheight( 1 )#ifndef QT_NO_DRAGANDDROP , autoscroll_timer( parent ), drag_autoscroll( TRUE )#endif { l_marg = r_marg = t_marg = b_marg = 0; viewport.setBackgroundMode( QWidget::PaletteDark ); vMode = QScrollView::Auto; hMode = QScrollView::Auto; corner = 0; vbar.setSteps( 20, 1/*set later*/ ); hbar.setSteps( 20, 1/*set later*/ ); policy = QScrollView::Default; signal_choke = FALSE; static_bg = FALSE; } ~QScrollViewData() { deleteAll(); } QSVChildRec* rec(QWidget* w) { return childDict.find(w); } QSVChildRec* ancestorRec(QWidget* w) { if ( clipped_viewport ) { while (w->parentWidget() != clipped_viewport) { w = w->parentWidget(); if (!w) return 0; } } else { while (w->parentWidget() != &viewport) { w = w->parentWidget(); if (!w) return 0; } } return rec(w); } QSVChildRec* addChildRec(QWidget* w, int x, int y ) { QSVChildRec *r = new QSVChildRec(w,x,y); children.append(r); childDict.insert(w, r); return r; } void deleteChildRec(QSVChildRec* r) { childDict.remove(r->child); children.removeRef(r); delete r; } void hideOrShowAll(QScrollView* sv, bool isScroll = FALSE ) { if ( clipped_viewport ) { if ( clipped_viewport->x() <= 0 && clipped_viewport->y() <= 0 && clipped_viewport->width()+clipped_viewport->x() >= viewport.width() && clipped_viewport->height()+clipped_viewport->y() >= viewport.height() ) { // clipped_viewport still covers viewport if( static_bg ) clipped_viewport->repaint( clipped_viewport->visibleRect(), TRUE ); else if ( ( !isScroll && !clipped_viewport->testWFlags( Qt::WNorthWestGravity) ) || static_bg ) QApplication::postEvent( clipped_viewport, new QPaintEvent( clipped_viewport->visibleRect(), !clipped_viewport->testWFlags(Qt::WResizeNoErase) ) ); } else { // Re-center int nx = ( viewport.width() - clipped_viewport->width() ) / 2; int ny = ( viewport.height() - clipped_viewport->height() ) / 2; // hide the clipped_viewport while we mess around // with it. To avoid having the focus jumping // around, we block it. clipped_viewport->blockFocus = TRUE; clipped_viewport->hide(); clipped_viewport->move(nx,ny); clipped_viewport->blockFocus = FALSE; // no need to update, we'll receive a paintevent after show. } for (QSVChildRec *r = children.first(); r; r=children.next()) { r->hideOrShow(sv, clipped_viewport); } clipped_viewport->show(); } } void moveAllBy(int dx, int dy) { if ( clipped_viewport && !static_bg ) { clipped_viewport->move( clipped_viewport->x()+dx, clipped_viewport->y()+dy ); } else { for (QSVChildRec *r = children.first(); r; r=children.next()) { r->child->move(r->child->x()+dx,r->child->y()+dy); } if ( static_bg ) viewport.repaint( viewport.visibleRect(), TRUE ); } } void deleteAll() { for (QSVChildRec *r = children.first(); r; r=children.next()) { delete r; } } bool anyVisibleChildren() { for (QSVChildRec *r = children.first(); r; r=children.next()) { if (r->child->isVisible()) return TRUE; } return FALSE; } void autoMove(QScrollView* sv) { if ( policy == QScrollView::AutoOne ) { QSVChildRec* r = children.first(); if (r) sv->setContentsPos(-r->child->x(),-r->child->y()); } } void autoResize(QScrollView* sv) { if ( policy == QScrollView::AutoOne ) { QSVChildRec* r = children.first(); if (r) sv->resizeContents(r->child->width(),r->child->height()); } } void autoResizeHint(QScrollView* sv) { if ( policy == QScrollView::AutoOne ) { QSVChildRec* r = children.first(); if (r) { QSize s = r->child->sizeHint(); if ( s.isValid() ) r->child->resize(s); } } else if ( policy == QScrollView::AutoOneFit ) { QSVChildRec* r = children.first(); if (r) { QSize sh = r->child->sizeHint(); sh = sh.boundedTo( r->child->maximumSize() ); sv->resizeContents( sh.width(), sh.height() ); } } } void viewportResized( int w, int h ) { if ( policy == QScrollView::AutoOneFit ) { QSVChildRec* r = children.first(); if (r) { QSize sh = r->child->sizeHint(); sh = sh.boundedTo( r->child->maximumSize() ); r->child->resize( QMAX(w,sh.width()), QMAX(h,sh.height()) ); } } } QScrollBar hbar; QScrollBar vbar; QWidget viewport; QClipperWidget* clipped_viewport; int flags; QList<QSVChildRec> children; QPtrDict<QSVChildRec> childDict; QWidget* corner; int vx, vy, vwidth, vheight; // for drawContents-style usage int l_marg, r_marg, t_marg, b_marg; QScrollView::ResizePolicy policy; QScrollView::ScrollBarMode vMode; QScrollView::ScrollBarMode hMode;#ifndef QT_NO_DRAGANDDROP QPoint cpDragStart; QTimer autoscroll_timer; int autoscroll_time; int autoscroll_accel; bool drag_autoscroll;#endif bool static_bg; // This variable allows ensureVisible to move the contents then // update both the sliders. Otherwise, updating the sliders would // cause two image scrolls, creating ugly flashing. // bool signal_choke;};// NOT REVISED/*!\class QScrollView qscrollview.h\brief The QScrollView widget provides a scrolling area with on-demand scrollbars.\ingroup abstractwidgetsThe QScrollView is a large canvas - potentially larger than thecoordinate system normally supported by the underlying window system.This is important, as is is quite easy to go beyond such limitations(eg. many web pages are more than 32000 pixels high). Additionally,the QScrollView can have QWidgets positioned on it that scroll aroundwith the drawn content. These subwidgets can also have positionsoutside the normal coordinate range (but they are still limited insize).To provide content for the widget, inherit from QScrollView andreimplement drawContents(), and use resizeContents() to set the sizeof the viewed area. Use addChild() / moveChild() to position widgetson the view.To use QScrollView effectively, it is important to understand itswidget structure in the three styles of usage: a single large child widget,a large panning area with some widgets, a large panning area with many widgets.<dl><dt><b>One Big Widget</b><dd><img src=qscrollview-vp2.png>The first, simplest usage of QScrollView depicted above isappropriate for scrolling areaswhich are \e never more than about 4000 pixels in either dimension (thisis about the maximum reliable size on X11 servers). In this usage, youjust make one large child in the QScrollView. The child shouldbe a child of the viewport() of the scrollview, and be added with addChild():\code QScrollView* sv = new QScrollView(...); QVBox* big_box = new QVBox(sv->viewport()); sv->addChild(big_box);\endcodeYou may go on to add arbitrary child widgets to the single child inthe scrollview, as you would with any widget:\code QLabel* child1 = new QLabel("CHILD", big_box); QLabel* child2 = new QLabel("CHILD", big_box); QLabel* child3 = new QLabel("CHILD", big_box); ...\endcodeHere, the QScrollView has 4 children - the viewport(),the verticalScrollBar(), the horizontalScrollBar(), anda small cornerWidget(). The viewport() has 1 child, the big QVBox.The QVBox has the three labels as child widgets. When the view is scrolled,the QVBox is moved, and its children move with it as child widgets normallydo.<dt><b>Very Big View, some Widgets</b><dd><img src=qscrollview-vp.png>The second usage of QScrollView depicted above is appropriate whenfew, if any, widgets are on a very large scrolling area that ispotentially larger than 4000 pixels in either dimension. In thisusage, you call resizeContents() to set the size of the area, andreimplement drawContents() to paint the contents. You may also addsome widgets, by making them children of the viewport() and addingthem with addChild() (this is the same as the process for the singlelarge widget in the previous example): \code QScrollView* sv = new QScrollView(...); QLabel* child1 = new QLabel("CHILD", sv->viewport()); sv->addChild(child1); QLabel* child2 = new QLabel("CHILD", sv->viewport()); sv->addChild(child2); QLabel* child3 = new QLabel("CHILD", sv->viewport()); sv->addChild(child3);\endcodeHere, the QScrollView has the same 4 children - the viewport(),the verticalScrollBar(), the horizontalScrollBar(), anda small cornerWidget(). The viewport()has the three labels as child widgets. When the view is scrolled,the scrollview moves the child widgets individually.<dt><b>Very Big View, many Widgets</b><dd><img src=qscrollview-cl.png>The final usage of QScrollView depicted above isappropriate when many widgets are on a very large scrolling areathat is potentially larger than 4000 pixels in either dimension. In thisusage, you call resizeContents() to set the size of the area, and reimplementdrawContents() to paint the contents. You then call enableClipper(TRUE)and add widgets, againby making them children of the viewport() and adding them withaddChild():\code QScrollView* sv = new QScrollView(...); sv->enableClipper(TRUE); QLabel* child1 = new QLabel("CHILD", sv->viewport()); sv->addChild(child1); QLabel* child2 = new QLabel("CHILD", sv->viewport()); sv->addChild(child2); QLabel* child3 = new QLabel("CHILD", sv->viewport()); sv->addChild(child3);\endcodeHere, the QScrollView has 4 children - the clipper() (\e not theviewport() this time), the verticalScrollBar(), thehorizontalScrollBar(), and a small cornerWidget(). The clipper() has1 child - the viewport(). The viewport() has the three labels aschild widgets. When the view is scrolled, the viewport() is moved,and its children move with it as child widgets normally do.</dl>Normally you will use the first or third method if you want any childwidgets in the view.Note that the widget you see in the scrolled area is the viewport()widget, not the QScrollView itself. So, to turn mouse tracking on forexample, use viewport()->setMouseTracking(TRUE).To enable drag-and-drop, you would setAcceptDrops(TRUE) on theQScrollView (since drag-and-drop events propagate to the parent), butto work out what logical position in the view, you would need to mapthe drop co-ordinate from being relative to the QScrollView to beingrelative to the contents - use the function viewportToContents() for this.To handle mouse events on the scrolling area, subclass scrollview asyou would subclass other widgets, but rather than overridingmousePressEvent(), reimplement viewportMousePressEvent() instead (ifyou reimplement mousePressEvent() you'll only get called when part of theQScrollView is clicked - and the only such part is the "corner" (ifyou don't set a cornerWidget()) and the frame, everything else beingcovered up by the viewport, clipper, or scrollbars.When you construct a QScrollView, some of the widget flags apply to theviewport(), instead of being sent to the QWidget constructor for theQScrollView. This applies to \c WResizeNoErase, \c WNorthWestGravity,\c WRepaintNoErase and \c WPaintClever. See Qt::WidgetFlags fordocumentation about these flags. Here are some examples: <ul><li> An image manipulation widget would use \cWResizeNoErase|WNorthWestGravity, because the widget draws all pixelsitself and when the size increases, it only needs a paint event forthe new part, since the old part remains unchanged.<li>A word processing widget might use \c WResizeNoErase and repaintitself line by line to get a less flickery resizing. If the widget isin a mode where no text justification can take place, it might use \cWNorthWestGravity too, so that it would only get a repaint for thenewly visible parts.<li>A scrolling game widget where the background scrolls as thecharacters move might use \c WRepaintNoErase (in addition to \cWNorthWestGravity and \c WResizeNoErase) so that the window systembackground does not flash in and out during scrolling.</ul>\warning WResizeNoErase is currently set by default, i.e. you alwayshave to clear the background manually in scrollview subclasses. Thiswill change in a future version of Qt, and we recommend specifying theflag explicitly.<img src=qscrollview-m.png> <img src=qscrollview-w.png>*//*! \enum QScrollView::ResizePolicy This enum type is used to control QScrollView's reaction to resize events. There are four possible settings:<ul> <li> \c Default - QScrollView selects one of the other settings automatically when it has to. In this version of Qt, QScrollView changes to \c Manual if you resize the contents with resizeContents(), and to \c AutoOne if a child is added.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -