?? qlistbox.cpp
字號:
/************************************************************************ $Id: qt/src/widgets/qlistbox.cpp 2.3.12 edited 2005-10-27 $**** Implementation of QListBox widget class**** Created : 941121**** 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 "qlistbox.h"#ifndef QT_NO_LISTBOX#include "qarray.h"#include "qfontmetrics.h"#include "qpainter.h"#include "qstrlist.h"#include "qscrollbar.h"#include "qpixmap.h"#include "qapplication.h"#include "qptrdict.h"#include <stdlib.h>#ifdef QT_KEYPAD_MODEextern bool qt_singleFocusWidget(const QWidget *in);extern bool qt_modalEditingEnabled;#endifclass QListBoxPrivate{public: QListBoxPrivate( QListBox *lb ): head( 0 ), last( 0 ), cache( 0 ), cacheIndex( -1 ), current( 0 ), highlighted( 0 ), layoutDirty( TRUE ), mustPaintAll( TRUE ), dragging( FALSE ), variableHeight( TRUE /* !!! ### FALSE */ ), variableWidth( FALSE ), columnPosOne( 0 ), rowModeWins( FALSE ), rowMode( QListBox::FixedNumber ), columnMode( QListBox::FixedNumber ), numRows( 1 ), numColumns( 1 ), currentRow( 0 ), currentColumn( 0 ), mousePressRow( -1 ), mousePressColumn( -1 ), mouseMoveRow( -1 ), mouseMoveColumn( -1 ), scrollTimer( 0 ), updateTimer( 0 ), visibleTimer( 0 ), selectionMode( QListBox::Single ), count( 0 ), ignoreMoves( FALSE ), listBox( lb ), currInputString( QString::null ) {} void findItemByName( const QString &text ); ~QListBoxPrivate(); QListBoxItem * head, *last, *cache; int cacheIndex; QListBoxItem * current, *highlighted, *tmpCurrent; bool layoutDirty; bool mustPaintAll; bool dragging; bool variableHeight; bool variableWidth; QArray<int> columnPos; QArray<int> rowPos; int columnPosOne; bool rowModeWins; QListBox::LayoutMode rowMode; QListBox::LayoutMode columnMode; int numRows; int numColumns; int currentRow; int currentColumn; int mousePressRow; int mousePressColumn; int mouseMoveRow; int mouseMoveColumn; QTimer * scrollTimer; QTimer * updateTimer; QTimer * visibleTimer; QTimer * resizeTimer; QPoint scrollPos; QListBox::SelectionMode selectionMode; int count; bool ignoreMoves; bool clearing; QListBox *listBox; QString currInputString; QTimer *inputTimer; QListBoxItem *pressedItem, *selectAnchor; bool select; bool pressedSelected; QRect *rubber; QPtrDict<bool> selectable; struct SortableItem { QListBoxItem *item; }; QSize sizeHint; QSize minimumSizeHint;};QListBoxPrivate::~QListBoxPrivate(){ ASSERT( !head );}// NOT REVISED/*! \class QListBoxItem qlistbox.h \brief This is the base class of all list box items. This class is the abstract base class of all list box items. If you need to insert customized items into a QListBox, you must inherit this class and reimplement paint(), height() and width(). \sa QListBox*//*! Constructs an empty list box item in the listbox \a listbox*/QListBoxItem::QListBoxItem( QListBox* listbox ){ lbox = listbox; s = FALSE; dirty = TRUE; custom_highlight = FALSE; p = n = 0; // just something that'll look noticeable in the debugger x = y = 42; if (listbox) listbox->insertItem( this );}/*! Constructs an empty list box item in the listbox \a listbox and inserts it after the item \a after.*/QListBoxItem::QListBoxItem( QListBox* listbox, QListBoxItem *after ){ lbox = listbox; s = FALSE; dirty = TRUE; custom_highlight = FALSE; p = n = 0; // just something that'll look noticeable in the debugger x = y = 42; if (listbox) listbox->insertItem( this, after );}/*! Destroys the list box item.*/QListBoxItem::~QListBoxItem(){ if ( lbox ) lbox->takeItem( this );}/*! Defines whether the list box items is responsible to draw itself in a highlighted state when being selected. If \a b is FALSE (the default), then the listbox will draw some default highlight indicator before calling paint(). \sa selected(), paint() */void QListBoxItem::setCustomHighlighting( bool b ){ custom_highlight = b;}/*! \fn void QListBoxItem::paint( QPainter *p ) Implement this function to draw your item. \sa height(), width()*//*! \fn int QListBoxItem::width( const QListBox* lb ) const Implement this function to return the width of your item. The \a lb parameter is the same as listBox() and is provided for convenience and compatibility. \sa paint(), height()*/int QListBoxItem::width(const QListBox*) const{ return QApplication::globalStrut().width();}/*! \fn int QListBoxItem::height( const QListBox* lb ) const Implement this function to return the height of your item. The \a lb parameter is the same as listBox() and is provided for convenience and compatibility. \sa paint(), width()*/int QListBoxItem::height(const QListBox*) const{ return QApplication::globalStrut().height();}/*! Returns the text of the item, which is used for sorting too. \sa setText()*/QString QListBoxItem::text() const{ return txt;}/*! Returns the pixmap connected with the item, if any. The default implementation of this function returns a null pointer.*/const QPixmap *QListBoxItem::pixmap() const{ return 0;}/*! Specifies if this item may be selected by the user or not. \sa isSelectable()*/void QListBoxItem::setSelectable( bool b ){ if ( !listBox() ) return; bool *sel = listBox()->d->selectable.find( this ); if ( !sel ) listBox()->d->selectable.insert( this, new bool( b ) ); else listBox()->d->selectable.replace( this, new bool( b ) );}/*! Returns if this item is selectable or not. \sa setSelectable()*/bool QListBoxItem::isSelectable() const{ if ( !listBox() ) return TRUE; bool *sel = listBox()->d->selectable.find( ( (QListBoxItem*)this ) ); if ( !sel ) return TRUE; else return *sel;}/*! \fn void QListBoxItem::setText( const QString &text ) Sets the text of the widget, which is used for sorting too. The text is not shown unless explicitly drawn in paint(). \sa text()*//*! \class QListBoxText qlistbox.h \brief The QListBoxText class provides list box items with text. The text is drawn in the widget's current font. If you need several different fonts, you have to make your own subclass of QListBoxItem. \sa QListBox, QListBoxItem*//*! Constructs a list box item in listbox \a listtbox showing the text \a text.*/QListBoxText::QListBoxText( QListBox *listbox, const QString &text ) :QListBoxItem( listbox ){ setText( text );}/*! Constructs a list box item showing the text \a text.*/QListBoxText::QListBoxText( const QString &text ) :QListBoxItem(){ setText( text );}/*! Constructs a list box item in listbox \a listtbox showing the text \a text. The item gets inserted after the item \a after.*/QListBoxText::QListBoxText( QListBox* listbox, const QString &text, QListBoxItem *after ) : QListBoxItem( listbox, after ){ setText( text );}/*! Destroys the item.*/QListBoxText::~QListBoxText(){}/*! Draws the text using \a painter.*/void QListBoxText::paint( QPainter *painter ){ QFontMetrics fm = painter->fontMetrics(); painter->drawText( 3, fm.ascent() + fm.leading()/2, text() );}/*! Returns the height of a line of text. \sa paint(), width()*/int QListBoxText::height( const QListBox* lb ) const{ int h = lb ? lb->fontMetrics().lineSpacing() + 2 : 0; return QMAX( h, QApplication::globalStrut().height() );}/*! Returns the width of this line. \sa paint(), height()*/int QListBoxText::width( const QListBox* lb ) const{ int w = lb ? lb->fontMetrics().width( text() ) + 6 : 0; return QMAX( w, QApplication::globalStrut().width() );}/*! \class QListBoxPixmap qlistbox.h \brief The QListBoxPixmap class provides list box items with a pixmap and an optional text. \sa QListBox, QListBoxItem*//*! Constructs a new list box item in listbox \a listbox showing the pixmap \a pixmap.*/QListBoxPixmap::QListBoxPixmap( QListBox* listbox, const QPixmap &pixmap ) : QListBoxItem( listbox ){ pm = pixmap;}/*! Constructs a new list box item showing the pixmap \a pixmap.*/QListBoxPixmap::QListBoxPixmap( const QPixmap &pixmap ) : QListBoxItem(){ pm = pixmap;}/*! Constructs a new list box item in listbox \a listbox showing the pixmap \a pixmap. The item gets inserted after the item \a after.*/QListBoxPixmap::QListBoxPixmap( QListBox* listbox, const QPixmap &pixmap, QListBoxItem *after ) : QListBoxItem( listbox, after ){ pm = pixmap;}/*! Destroys the item.*/QListBoxPixmap::~QListBoxPixmap(){}/*! Constructs a new list box item in listbox \a listbox showing the pixmap \a pixmap and the text \a text*/QListBoxPixmap::QListBoxPixmap( QListBox* listbox, const QPixmap &pix, const QString& text) : QListBoxItem( listbox ){ pm = pix; setText( text );}/*! Constructs a new list box item in listbox \a listbox showing the pixmap \a pixmap. The item gets inserted after the item \a after.*/QListBoxPixmap::QListBoxPixmap( const QPixmap & pix, const QString& text) : QListBoxItem(){ pm = pix; setText( text );}/*! Constructs a new list box item in listbox \a listbox showing the pixmap \a pixmap and the string \a text . The item gets inserted after the item \a after.*/QListBoxPixmap::QListBoxPixmap( QListBox* listbox, const QPixmap & pix, const QString& text, QListBoxItem *after ) : QListBoxItem( listbox, after ){ pm = pix; setText( text );}/*! \fn const QPixmap *QListBoxPixmap::pixmap() const Returns the pixmap connected with the item.*//*! Draws the pixmap using \a painter.*/void QListBoxPixmap::paint( QPainter *painter ){ painter->drawPixmap( 3, 0, pm ); if ( !text().isEmpty() ) { QFontMetrics fm = painter->fontMetrics(); int yPos; // vertical text position if ( pm.height() < fm.height() ) yPos = fm.ascent() + fm.leading()/2; else yPos = pm.height()/2 - fm.height()/2 + fm.ascent(); painter->drawText( pm.width() + 5, yPos, text() ); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -