?? qtoolbar.cpp
字號:
/****************************************************************************** $Id: qt/src/widgets/qtoolbar.cpp 2.3.12 edited 2005-10-27 $**** Implementation of QToolBar class**** Created : 980315**** 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 "qtoolbar.h"#ifndef QT_NO_TOOLBAR#include "qmainwindow.h"#include "qpushbutton.h"#include "qtooltip.h"#include "qlayout.h"#include "qframe.h"#include "qobjectlist.h"#include "qobjectdict.h"#include "qpainter.h"#include "qdrawutil.h"#include "qapplication.h"#include "qtoolbutton.h"#include "qpopupmenu.h"#include "qtimer.h"class QArrowWidget : public QWidget{ Q_OBJECTpublic: QArrowWidget( Qt::Orientation o, QWidget *parent ) : QWidget( parent, "qt_arrow_widget" ), orient( o ) {}#ifndef QT_NO_POPUPMENUprotected: void paintEvent( QPaintEvent * ) { QPainter p( this ); QPointArray a; if ( orient == Horizontal ) { int h = height(); a.setPoints( 5, 0, 0, 3, h / 4, 0, h / 2, 3,3 * h / 4, 0, h ); } else { int w = width(); a.setPoints( 5, 0, 0, w / 4, 3 , w / 2, 0 , 3 * w / 4, 3 , w, 0 ); } p.setPen( colorGroup().light() ); p.drawPolyline( a ); if ( orient == Qt::Horizontal ) a.translate( 1, 0 ); else a.translate( 0, 1 ); p.setPen( colorGroup().midlight() ); p.drawPolyline( a ); }#endifprivate: Qt::Orientation orient;};class QToolBarPrivate{public: QToolBarPrivate() : moving( FALSE ),#ifndef QT_NO_POPUPMENU arrow( 0 ), menu( 0 ), back( 0 ),#endif button( 0 ) { stretchable[ 0 ] = FALSE; stretchable[ 1 ] = FALSE; hiddenItems.setAutoDelete( FALSE ); } bool moving; bool stretchable[ 2 ];#ifndef QT_NO_POPUPMENU QToolButton *arrow; QPopupMenu *menu; QArrowWidget *back;#endif QIntDict<QButton> hiddenItems; QButton *button;};class QToolBarSeparator : public QFrame{ Q_OBJECTpublic: QToolBarSeparator( Orientation, QToolBar *parent, const char* name=0 ); QSize sizeHint() const; QSizePolicy sizePolicy() const; Orientation orientation() const { return orient; }public slots: void setOrientation( Orientation );protected: void styleChange( QStyle& );private: Orientation orient;};QToolBarSeparator::QToolBarSeparator(Orientation o , QToolBar *parent, const char* name ) :QFrame( parent, name ){ connect( parent, SIGNAL(orientationChanged(Orientation)), this, SLOT(setOrientation(Orientation)) ); setOrientation( o ); setBackgroundMode( parent->backgroundMode() ); setBackgroundOrigin( ParentOrigin ); setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) );}void QToolBarSeparator::setOrientation( Orientation o ){ orient = o; if ( style() == WindowsStyle ) { if ( orientation() == Vertical ) setFrameStyle( HLine + Sunken ); else setFrameStyle( VLine + Sunken ); } else { setFrameStyle( NoFrame ); }}void QToolBarSeparator::styleChange( QStyle& ){ setOrientation( orient );}QSize QToolBarSeparator::sizeHint() const{ return orientation() == Vertical ? QSize( 0, 6 ) : QSize( 6, 0 );}QSizePolicy QToolBarSeparator::sizePolicy() const{ //### removeme 3.0 return QWidget::sizePolicy();}#include "qtoolbar.moc"// NOT REVISED/*! \class QToolBar qtoolbar.h \brief The QToolBar class provides a tool bar. \ingroup application A toolbar is a panel that contains a set of controls, usually represented by small icons. Its purpose is to provide quick access to frequently used commands or options. Within a main window, the user can drag toolbars freely around and hide them with a click on the toolbar handle. To use QToolBar, you simply create a QToolBar as child of a QMainWindow, create a number of QToolButton widgets (or other widgets) in left to right (or top to bottom) order, call addSeparator() when you want a separator, and that's all. The application/application.cpp example does precisely this. You may use any kind of widget within a toolbar, with QToolButton and QComboBox being the two most common ones. Each QToolBar lives in a \link QMainWindow dock \endlink in a QMainWindow, and can optionally start a new line in its dock. Tool bars that start a new line are always positioned at the left end or top of the tool bar dock; others are placed next to the previous tool bar and word-wrapped as necessary. The main window can be resized to a smaller size than a toolbar would need to show all items. If this happens QToolbar shows a little arrow button at the right or bottom end. When clicking on that button, a popup menu is opened which shows all items of the toolbar which are outside the visible area of the mainwindow. Usually, a toolbar gets just the space it needs. However, with setHorizontalStretchable()/setVerticalStretchable() or setStretchableWidget() you can advise the main window to expand the toolbar to fill all available width in the specified orientation. The tool bar arranges its buttons either horizontally or vertically (see setOrientation() for details). Generally, QMainWindow will set the orientation correctly for you. The toolbar emits a signal orientationChanged() each time the orientation changes, in case some child widgets need adjustments. To remove all items from a toolbar, you can use the clear() method. \sa QToolButton QMainWindow <a href="http://www.iarchitect.com/visual.htm">Parts of Isys on Visual Design</a> <a href="guibooks.html#fowler">GUI Design Handbook: Tool Bar.</a>*//*! Constructs an empty tool bar which is a child of \a parent and managed by \a parent, initially in \a dock, labelled \a and starting a new line in the dock if \a newLine is TRUE. \a name is the object name, as usual.*/QToolBar::QToolBar( const QString &label, QMainWindow * parent, QMainWindow::ToolBarDock dock, bool newLine, const char * name ) : QWidget( parent, name ){ mw = parent; o = (dock == QMainWindow::Left || dock == QMainWindow::Right ) ? Vertical : Horizontal; init(); if ( parent ) parent->addToolBar( this, label, dock, newLine );}/*! Constructs an empty horizontal tool bar with a parent of \a parent and managed by \a mainWindow. The \a label and \a newLine are passed straight to QMainWindow::addToolBar(). \a name is the object name and \a f is the widget flags. This is the constructor to use if you want to create torn-off toolbars, or toolbars in the status bar.*/QToolBar::QToolBar( const QString &label, QMainWindow * mainWindow, QWidget * parent, bool newLine, const char * name, WFlags f ) : QWidget( parent, name, f ){ mw = mainWindow; o = Horizontal; init(); if ( mainWindow ) mainWindow->addToolBar( this, label, QMainWindow::Unmanaged, newLine );}/*! Constructs an empty tool bar in the top dock of its parent, without any label and without requiring a newline. This is mostly useless. */QToolBar::QToolBar( QMainWindow * parent, const char * name ) : QWidget( parent, name ){ mw = parent; o = Horizontal; init(); if ( parent ) parent->addToolBar( this, QString::null, QMainWindow::Top );}/*! Common initialization code. Requires that \c mw and \c o are set. Does not call QMainWindow::addToolBar*/void QToolBar::init(){ d = new QToolBarPrivate; bl = 0; sw = 0; bl = new QBoxLayout( this, orientation() == Vertical ? QBoxLayout::Down : QBoxLayout::LeftToRight,#ifdef _WS_QWS_ 1,#else style() == WindowsStyle ? 2 : 1,#endif 0 ); boxLayout()->setAutoAdd( TRUE ); if ( !mw || mw->toolBarsMovable() ) boxLayout()->addSpacing( 9 ); if ( mw ) { connect( mw, SIGNAL( startMovingToolBar(QToolBar*) ), this, SLOT( startMoving(QToolBar*) ) ); connect( mw, SIGNAL( endMovingToolBar(QToolBar*) ), this, SLOT( endMoving(QToolBar*) ) ); }#if defined(CHECK_NULL) else qWarning( "QToolBar::QToolBar main window cannot be 0." );#endif setBackgroundMode( PaletteButton); setFocusPolicy( NoFocus );}QBoxLayout *QToolBar::boxLayout(){ if ( !layout() ) { bl = new QBoxLayout( this, orientation() == Vertical ? QBoxLayout::Down : QBoxLayout::LeftToRight, style() == WindowsStyle ? 2 : 1, 0 ); if ( !mw || mw->toolBarsMovable() ) boxLayout()->addSpacing( 9 ); return bl; } if ( bl == layout() || layout()->inherits( "QBoxLayout" ) ) { bl = (QBoxLayout*)layout(); return (QBoxLayout*)layout(); } return 0;}/*! Destructs the object and frees any allocated resources. */QToolBar::~QToolBar(){ d->menu = 0; delete d; d = 0;}/*! Adds a separator to the end of the toolbar. */void QToolBar::addSeparator(){ (void) new QToolBarSeparator( orientation(), this, "tool bar separator" );}/*!\reimp*/void QToolBar::styleChange( QStyle& /*old*/ ){ QObjectList *childs = queryList( "QWidget" ); if ( childs ) { QObject *ob = 0; for ( ob = childs->first(); ob; ob = childs->next() ) { if ( ob->inherits( "QToolButton" ) ) { QToolButton* w = (QToolButton*)ob; w->setStyle( &style() ); } else if ( ob->inherits( "QToolBarSeparator" ) ) { QToolBarSeparator* w = (QToolBarSeparator*)ob; w->setStyle( &style() ); } } } delete childs;} /*! Sets this toolbar to organize its content vertically if \a newOrientation is \c Vertical and horizontally if \a newOrientation is \c Horizontal. Emits the orientationChanged() signal. \sa orientation()*/void QToolBar::setOrientation( Orientation newOrientation ){ if ( o != newOrientation ) { o = newOrientation;#ifndef QT_NO_POPUPMENU if ( d->arrow ) { delete d->arrow; d->arrow = 0; } if ( d->back ) { delete d->back; d->back = 0; }#endif boxLayout()->setDirection( o==Horizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom ); emit orientationChanged( newOrientation ); }}/*! \fn Orientation QToolBar::orientation() const Returns the current orientation of the toolbar.*//*! \reimp. */void QToolBar::show(){ QWidget::show(); if ( mw ) mw->triggerLayout( FALSE );}/*! \reimp*/void QToolBar::hide(){ QWidget::hide(); if ( mw ) mw->triggerLayout( FALSE );}void QToolBar::setUpGM(){ //Does nothing, present for binary compatibility}/*! Paint the handle. The Motif style is rather close to Netscape and even closer to KDE.*/void QToolBar::paintEvent( QPaintEvent * ){ paintToolBar();}/*! Returns a pointer to the QMainWindow which controls this tool bar.*/QMainWindow * QToolBar::mainWindow(){ return mw;}/*! Sets \a w to be expanded if this toolbar is requested to stretch (because QMainWindow right-justifies the dock it's in or isVerticalStretchable() or isHorizontalStretchable() of this toolbar is TRUE). If you call setStretchableWidget() and the toolbar is not stretchable yet, setStretchable( ) is called. \sa QMainWindow::setRightJustification(), setVerticalStretchable(), setHorizontalStretchable()*/void QToolBar::setStretchableWidget( QWidget * w ){ sw = w; boxLayout()->setStretchFactor( w, 1 ); if ( !isHorizontalStretchable() && !isVerticalStretchable() ) { if ( orientation() == Horizontal ) setHorizontalStretchable( TRUE ); else setVerticalStretchable( TRUE ); }}/*! \reimp */bool QToolBar::event( QEvent * e ){ bool r = QWidget::event( e ); //after the event filters have dealt with it: if ( e->type() == QEvent::ChildInserted ) { QObject * child = ((QChildEvent*)e)->child(); if ( isVisible() && child && child->isWidgetType() && child->parent() == this && !child->inherits( "QPopupMenu" ) ) ( (QWidget*)child )->show(); if ( child && child->isWidgetType() && ((QWidget*)child) == sw ) boxLayout()->setStretchFactor( (QWidget*)child, 1 );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -