?? qmainwindow.cpp
字號:
/****************************************************************************** $Id: qt/src/widgets/qmainwindow.cpp 2.3.12 edited 2005-10-27 $**** Implementation of QMainWindow class**** Created : 980312**** 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 "qmainwindow.h"#ifndef QT_NO_MAINWINDOW#include "qtimer.h"#include "qlayout.h"#include "qobjectlist.h"#include "qobjectdict.h"#include "qapplication.h"#include "qlist.h"#include "qmap.h"#include "qcursor.h"#include "qpainter.h"#include "qmenubar.h"#include "qtoolbar.h"#include "qstatusbar.h"#include "qscrollview.h"#include "qtooltip.h"#include "qdatetime.h"#include "qtooltip.h"#include "qwhatsthis.h"#include "qbitmap.h"//#define QMAINWINDOW_DEBUG//****************************************************************************// -------------------------- static convenience functions -------------------//****************************************************************************bool operator<( const QRect &r1, const QRect &r2 ){ return ( r1.x() < r2.x() || r1.y() < r2.y() );}static Qt::Orientation swap_orientation( Qt::Orientation o ){ if ( o == Qt::Horizontal ) return Qt::Vertical; else return Qt::Horizontal;}#ifndef QT_NO_TOOLBARstatic int tb_pos( QToolBar *t, Qt::Orientation orient, bool swap = FALSE ){ Qt::Orientation o = orient; if ( swap ) o = swap_orientation( o ); if ( o == Qt::Horizontal ) return t->x(); else return t->y();}static int tb_extend( QToolBar *t, Qt::Orientation orient, bool swap = FALSE ){ Qt::Orientation o = orient; if ( swap ) o = swap_orientation( o ); if ( o == Qt::Horizontal ) return t->width(); else return t->height();}#endifstatic int rect_pos( const QRect &r, Qt::Orientation orient, bool swap = FALSE ){ Qt::Orientation o = orient; if ( swap ) o = swap_orientation( o ); if ( o == Qt::Horizontal ) return r.x(); else return r.y();}static int rect_extend( const QRect &r, Qt::Orientation orient, bool swap = FALSE ){ Qt::Orientation o = orient; if ( swap ) o = swap_orientation( o ); if ( o == Qt::Horizontal ) return r.width(); else return r.height();}static int size_extend( const QSize &s, Qt::Orientation orient, bool swap = FALSE ){ Qt::Orientation o = orient; if ( swap ) o = swap_orientation( o ); if ( o == Qt::Horizontal ) return s.width(); else return s.height();}#ifndef QT_NO_TOOLBARstatic QSize size_hint( QToolBar *tb ){ if ( !tb || !tb->isVisibleTo( tb->parentWidget() ) ) return QSize( 0, 0 ); QSize s = tb->sizeHint(); if ( tb->minimumWidth() > s.width() ) s.setWidth( tb->minimumWidth() ); if ( tb->minimumHeight() > s.height() ) s.setHeight( tb->minimumHeight() ); return s;}#endif//************************************************************************************************// ------------------------ QMainWindowPrivate -----------------------//************************************************************************************************class QHideDock;class QToolLayout;class QMainWindowPrivate {public:#ifndef QT_NO_TOOLBAR struct ToolBar { ToolBar() : t(0), nl(FALSE) {} ToolBar( QToolBar * tb, bool n=FALSE ) : t( tb ), hiddenBefore( 0 ), hiddenAfter( 0 ), nl( n ), oldDock( QMainWindow::Top ), oldIndex( 0 ), extraOffset( -1 ) {} bool isStretchable( Qt::Orientation o ) const { return o == Qt::Horizontal ? t->isHorizontalStretchable() : t->isVerticalStretchable(); } QToolBar * t; ToolBar *hiddenBefore; ToolBar *hiddenAfter; bool nl; QValueList<int> disabledDocks; QMainWindow::ToolBarDock oldDock; int oldIndex; int extraOffset; }; typedef QList<ToolBar> ToolBarDock;#endif enum InsertPos { Before, After, Above, Below, SameIndex }; QMainWindowPrivate() : #ifndef QT_NO_TOOLBAR tornOff(0), unmanaged(0), hidden( 0 ),#endif mb(0), sb(0), ttg(0), mc(0), timer(0), tll(0), ubp( FALSE ), utl( FALSE ), justify( FALSE ) { rectPainter = 0;#ifndef QT_NO_TOOLBAR top = new ToolBarDock; left = new ToolBarDock; right = new ToolBarDock; bottom = new ToolBarDock; hidden = new ToolBarDock; unmanaged = new ToolBarDock; tornOff = new ToolBarDock; dockable[ (int)QMainWindow::Left ] = TRUE; dockable[ (int)QMainWindow::Right ] = TRUE; dockable[ (int)QMainWindow::Top ] = TRUE; dockable[ (int)QMainWindow::Bottom ] = TRUE; dockable[ (int)QMainWindow::Unmanaged ] = TRUE; dockable[ (int)QMainWindow::Minimized ] = TRUE; dockable[ (int)QMainWindow::TornOff ] = TRUE; lLeft = lRight = lTop = lBottom = 0; movable = TRUE; inMovement = FALSE; dockMenu = TRUE;#endif lastTopHeight = -1;#ifndef QT_NO_CURSOR oldCursor = ArrowCursor;#endif }#ifndef QT_NO_TOOLBAR ToolBar *findToolbar( QToolBar *t, QMainWindowPrivate::ToolBarDock *&dock ); ToolBar *takeToolBarFromDock( QToolBar * t, bool remember = FALSE );#endif ~QMainWindowPrivate() {#ifndef QT_NO_TOOLBAR if ( top ) { top->setAutoDelete( TRUE ); delete top; } if ( left ) { left->setAutoDelete( TRUE ); delete left; } if ( right ) { right->setAutoDelete( TRUE ); delete right; } if ( bottom ) { bottom->setAutoDelete( TRUE ); delete bottom; } if ( tornOff ) { tornOff->setAutoDelete( TRUE ); delete tornOff; } if ( unmanaged ) { unmanaged->setAutoDelete( TRUE ); delete unmanaged; } if ( hidden ) { hidden->setAutoDelete( TRUE ); delete hidden; }#endif }#ifndef QT_NO_TOOLBAR ToolBarDock * top, * left, * right, * bottom, * tornOff, * unmanaged, *hidden; QToolLayout *lLeft, *lRight, *lTop, *lBottom;#endif#ifndef QT_NO_MENUBAR QMenuBar * mb;#else QWidget * mb;#endif QStatusBar * sb; QToolTipGroup * ttg; QWidget * mc; QTimer * timer; QBoxLayout * tll; bool ubp; bool utl; bool justify; QPoint pos; QPoint offset; QPainter *rectPainter; QRect oldPosRect; QRect origPosRect; bool oldPosRectValid, movedEnough; QPoint cursorOffset; int lastTopHeight;#ifndef QT_NO_CURSOR QCursor oldCursor;#endif#ifndef QT_NO_TOOLBAR QMainWindow::ToolBarDock oldDock, origDock; QHideDock *hideDock; QMap< int, bool > dockable; bool movable; bool opaque; bool inMovement; bool dockMenu;#endif};#ifndef QT_NO_TOOLBARQMainWindowPrivate::ToolBar * QMainWindowPrivate::findToolbar( QToolBar * t, QMainWindowPrivate::ToolBarDock *&dock ){ QMainWindowPrivate::ToolBarDock* docks[] = { left, right, top, bottom, unmanaged, tornOff, hidden }; QMainWindowPrivate::ToolBarDock *l = 0; for ( unsigned int i = 0; i < 7; ++i ) { l = docks[ i ]; if ( !l ) continue; QMainWindowPrivate::ToolBar * ct = l->first(); do { if ( ct && ct->t == t ) { dock = l; return ct; } } while( ( ct = l->next() ) != 0 ); } dock = 0; return 0;}QMainWindowPrivate::ToolBar * QMainWindowPrivate::takeToolBarFromDock( QToolBar * t, bool remember ){ QMainWindowPrivate::ToolBarDock *l; QMainWindowPrivate::ToolBar *tb = findToolbar( t, l ); if ( tb && l ) { int p = l->findRef( tb ); if ( remember ) { if ( p < (int)l->count() - 1 && !l->at( p + 1 )->nl ) { l->at( p + 1 )->hiddenBefore = tb;#ifdef QMAINWINDOW_DEBUG qDebug( "remember toolbar before me" );#endif } else if ( p > 0 && !tb->nl ) { l->at( p - 1 )->hiddenAfter = tb;#ifdef QMAINWINDOW_DEBUG qDebug( "remember toolbar after me" );#endif } if ( p < (int)l->count() - 1 && tb->nl ) l->at( p + 1 )->nl = TRUE; tb->oldIndex = p; } return l->take( p ); } return 0;}#endif//************************************************************************************************// --------------------------------- QToolLayout ---------------------------------//************************************************************************************************/* This layout class does not work very well for vertical toolbars yet */class QToolLayout : public QLayout{ Q_OBJECT#ifndef QT_NO_TOOLBARpublic: QToolLayout( QLayout* parent, QMainWindowPrivate::ToolBarDock *d, QBoxLayout::Direction dd, bool justify, int space=-1, const char *name=0 ) : QLayout( parent, space, name ), dock(d), dir(dd), fill(justify) { init(); } ~QToolLayout(); void addItem( QLayoutItem *item); bool hasHeightForWidth() const; int heightForWidth( int ) const; int widthForHeight( int ) const; QSize sizeHint() const; QSize minimumSize() const; QLayoutIterator iterator(); QSizePolicy::ExpandData expanding() const { return QSizePolicy::NoDirection; } void setDirection( QBoxLayout::Direction d ) { dir = d; } QBoxLayout::Direction direction() const { return dir; } void newLine(); void setRightJustified( bool on ) { fill = on; } bool rightJustified() const { return fill; } void invalidate();protected: void setGeometry( const QRect& );private: void init(); int layoutItems( const QRect&, bool testonly = FALSE ); QMainWindowPrivate::ToolBarDock *dock; QBoxLayout::Direction dir; bool fill; int cached_width, cached_height; int cached_hfw, cached_wfh; friend class QMainWindowLayout;#endif};#ifndef QT_NO_TOOLBARQSize QToolLayout::sizeHint() const{ if ( !dock || !dock->first() ) return QSize(0,0); int w = 0; int h = 0; QListIterator<QMainWindowPrivate::ToolBar> it(*dock); QMainWindowPrivate::ToolBar *tb; it.toFirst(); int y = -1; int x = -1; int ph = 0; int pw = 0; while ( (tb=it.current()) != 0 ) { int plush = 0, plusw = 0; ++it; if ( hasHeightForWidth() ) { if ( y != tb->t->y() ) plush = ph; y = tb->t->y(); ph = tb->t->height(); } else { if ( x != tb->t->x() ) plusw = pw; x = tb->t->x(); pw = tb->t->width(); } h = QMAX( h, tb->t->height() + plush ); w = QMAX( w, tb->t->width() + plusw ); } if ( hasHeightForWidth() ) return QSize( 0, h ); return QSize( w, 0 );}bool QToolLayout::hasHeightForWidth() const{ //direction is the dock's direction, which is perpendicular to the layout return dir == QBoxLayout::Up || dir == QBoxLayout::Down;}void QToolLayout::init(){ cached_width = 0; cached_height = 0; cached_hfw = -1; cached_wfh = -1;}QToolLayout::~QToolLayout(){}QSize QToolLayout::minimumSize() const{ if ( !dock ) return QSize(0,0); QSize s; QListIterator<QMainWindowPrivate::ToolBar> it(*dock); QMainWindowPrivate::ToolBar *tb; while ( (tb=it.current()) != 0 ) { ++it; s = s.expandedTo( tb->t->minimumSizeHint() ) .expandedTo(tb->t->minimumSize()); } if ( s.width() < 0 ) s.setWidth( 0 ); if ( s.height() < 0 ) s.setHeight( 0 ); return s;}void QToolLayout::invalidate(){ cached_width = 0; cached_height = 0;}int QToolLayout::layoutItems( const QRect &r, bool testonly )
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -