⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q3combobox.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "q3combobox.h"#ifndef QT_NO_COMBOBOX#include "qpainter.h"#include "qdrawutil.h"#include "qpixmap.h"#include "qtimer.h"#include "qapplication.h"#include "qlineedit.h"#include "qbitmap.h"#include "qstringlist.h"#include "qstyle.h"#include "qevent.h"#include "qmenu.h"#include "qmenudata.h"#include "qstyleoption.h"#include "qdesktopwidget.h"#include "q3popupmenu.h"#include "q3listbox.h"#include "q3strlist.h"#include "q3frame.h"#include <limits.h>#include <qdebug.h>#ifndef QT_NO_EFFECTS# include <private/qeffects_p.h>#endif#if defined(QT_ACCESSIBILITY_SUPPORT)#include "qaccessible.h"#endif/*!    \class Q3ComboBox q3combobox.h    \brief The Q3ComboBox widget is a combined button and popup list.    \since 4.1    \compat    A combobox is a selection widget which displays the current item    and can pop up a list of items. A combobox may be editable in    which case the user can enter arbitrary strings.    Comboboxes provide a means of showing the user's current choice    out of a list of options in a way that takes up the minimum amount    of screen space.    Q3ComboBox supports three different display styles: Aqua/Motif 1.x,    Motif 2.0 and Windows. In Motif 1.x, a combobox was called    XmOptionMenu. In Motif 2.0, OSF introduced an improved combobox    and named that XmComboBox. Q3ComboBox provides both.    Q3ComboBox provides two different constructors. The simplest    constructor creates an "old-style" combobox in Motif (or Aqua)    style:    \code        Q3ComboBox *c = new Q3ComboBox(this, tr("read-only combobox"));    \endcode    The other constructor creates a new-style combobox in Motif style,    and can create both read-only and editable comboboxes:    \code        Q3ComboBox *c1 = new Q3ComboBox(false, this, tr("read-only combobox"));        Q3ComboBox *c2 = new Q3ComboBox(true, this, tr("editable combobox"));    \endcode    New-style comboboxes use a list box in both Motif and Windows    styles, and both the content size and the on-screen size of the    list box can be limited with sizeLimit() and setMaxCount()    respectively. Old-style comboboxes use a popup in Aqua and Motif    style, and that popup will happily grow larger than the desktop if    you put enough data into it.    The two constructors create identical-looking comboboxes in    Windows style.    Comboboxes can contain pixmaps as well as strings; the    insertItem() and changeItem() functions are suitably overloaded.    For editable comboboxes, the function clearEdit() is provided,    to clear the displayed string without changing the combobox's    contents.    A combobox emits two signals, activated() and highlighted(), when    a new item has been activated (selected) or highlighted (made    current). Both signals exist in two versions, one with a \c    QString argument and one with an \c int argument. If the user    highlights or activates a pixmap, only the \c int signals are    emitted. Whenever the text of an editable combobox is changed the    textChanged() signal is emitted.    When the user enters a new string in an editable combobox, the    widget may or may not insert it, and it can insert it in several    locations. The default policy is is \c AtBottom but you can change    this using setInsertionPolicy().    It is possible to constrain the input to an editable combobox    using QValidator; see setValidator(). By default, any input is    accepted.    If the combobox is not editable then it has a default    focusPolicy() of \c TabFocus, i.e. it will not grab focus if    clicked. This differs from both Windows and Motif. If the combobox    is editable then it has a default focusPolicy() of \c StrongFocus,    i.e. it will grab focus if clicked.    A combobox can be populated using the insert functions,    insertStringList() and insertItem() for example. Items can be    changed with changeItem(). An item can be removed with    removeItem() and all items can be removed with clear(). The text    of the current item is returned by currentText(), and the text of    a numbered item is returned with text(). The current item can be    set with setCurrentItem() or setCurrentText(). The number of items    in the combobox is returned by count(); the maximum number of    items can be set with setMaxCount(). You can allow editing using    setEditable(). For editable comboboxes you can set auto-completion    using setAutoCompletion() and whether or not the user can add    duplicates is set with setDuplicatesEnabled().    Depending on the style, Q3ComboBox will use a list box or a    popup menu to display the list of items. See setListBox() for    more information.    \sa QComboBox, QLineEdit, QSpinBox        {GUI Design Handbook}{GUI Design Handbook: Combo Box, Drop-Down List Box}*//*!    \enum Q3ComboBox::Policy    This enum specifies what the Q3ComboBox should do when a new string    is entered by the user.    \value NoInsertion the string will not be inserted into the    combobox.    \value AtTop insert the string as the first item in the combobox.    \value AtCurrent replace the previously selected item with the    string the user has entered.    \value AtBottom insert the string as the last item in the    combobox.    \value AfterCurrent insert the string after the previously    selected item.    \value BeforeCurrent insert the string before the previously    selected item.    activated() is always emitted when the string is entered.    If inserting the new string would cause the combobox to breach its    content size limit, the item at the other end of the list is    deleted. The definition of "other end" is    implementation-dependent.    \omitvalue NoInsert    \omitvalue InsertAtTop    \omitvalue InsertAtCurrent    \omitvalue InsertAtBottom    \omitvalue InsertAfterCurrent    \omitvalue InsertBeforeCurrent*//*!    \fn void Q3ComboBox::activated( int index )    This signal is emitted when a new item has been activated    (selected). The \a index is the position of the item in the    combobox.    This signal is not emitted if the item is changed    programmatically, e.g. using setCurrentItem().*//*!    \overload    \fn void Q3ComboBox::activated( const QString &string )    This signal is emitted when a new item has been activated    (selected). \a string is the selected string.    You can also use the activated(int) signal, but be aware that its    argument is meaningful only for selected strings, not for user    entered strings.*//*!    \fn void Q3ComboBox::highlighted( int index )    This signal is emitted when a new item has been set to be the    current item. The \a index is the position of the item in the    combobox.    This signal is not emitted if the item is changed    programmatically, e.g. using setCurrentItem().*//*!    \overload    \fn void Q3ComboBox::highlighted( const QString &string )    This signal is emitted when a new item has been set to be the    current item. \a string is the item's text.    You can also use the highlighted(int) signal.*//*!    \fn void Q3ComboBox::textChanged( const QString &string )    This signal is used for editable comboboxes. It is emitted    whenever the contents of the text entry field changes. \a string    contains the new text.*//*!    \property Q3ComboBox::autoCompletion    \brief whether auto-completion is enabled    This property can only be set for editable comboboxes, for    non-editable comboboxes it has no effect. It is false by default.*//*!    \property Q3ComboBox::autoResize    \brief whether auto-resize is enabled    \obsolete    If this property is set to true then the combobox will resize    itself whenever its contents change. The default is false.*//*!    \property Q3ComboBox::count    \brief the number of items in the combobox*//*!    \property Q3ComboBox::currentItem    \brief the index of the current item in the combobox    Note that the activated() and highlighted() signals are only    emitted when the user changes the current item, not when it is    changed programmatically.*//*!    \property Q3ComboBox::currentText    \brief the text of the combobox's current item*//*!    \property Q3ComboBox::duplicatesEnabled    \brief whether duplicates are allowed    If the combobox is editable and the user enters some text in the    combobox's lineedit and presses Enter (and the insertionPolicy()    is not \c NoInsertion), then what happens is this:    \list    \i If the text is not already in the list, the text is inserted.    \i If the text is in the list and this property is true (the    default), the text is inserted.    \i If the text is in the list and this property is false, the text    is \e not inserted; instead the item which has matching text becomes    the current item.    \endlist    This property only affects user-interaction. You can use    insertItem() to insert duplicates if you wish regardless of this    setting.*//*!    \property Q3ComboBox::editable    \brief whether the combobox is editable    This property's default is false. Note that the combobox will be    cleared if this property is set to true for a 1.x Motif style    combobox. To avoid this, use setEditable() before inserting any    items. Also note that the 1.x version of Motif didn't have any    editable comboboxes, so the combobox will change its appearance    to a 2.0 style Motif combobox is it is set to be editable.*//*!    \property Q3ComboBox::insertionPolicy    \brief the position of the items inserted by the user    The default insertion policy is \c AtBottom. See \l Policy.*//*!    \property Q3ComboBox::maxCount    \brief the maximum number of items allowed in the combobox*//*!    \property Q3ComboBox::sizeLimit    \brief the maximum on-screen size of the combobox.    This property is ignored for both Motif 1.x style and non-editable    comboboxes in Mac style. The default limit is ten    lines. If the number of items in the combobox is or grows larger    than lines, a scroll bar is added.*/class Q3ComboBoxPopup : public Q3PopupMenu{public:    Q3ComboBoxPopup( QWidget *parent=0, const char *name=0 )	: Q3PopupMenu( parent, name )    {    }    int itemHeight( int index )    {	return Q3PopupMenu::itemHeight( index );    }};static inline QString escapedComboString(const QString &str){    QString stringToReturn = str;    return stringToReturn.replace(QLatin1Char('&'), QLatin1String("&&"));}class Q3ComboBoxPopupItem : public QMenuItem{    Q3ListBoxItem *li;    QSize sc; // Size cache optimizationpublic:    Q3ComboBoxPopupItem(Q3ListBoxItem *i) : QMenuItem(), li(i), sc(0, 0) {  }    virtual bool fullSpan() const { return true; }    virtual void paint( QPainter*, const QColorGroup&, bool, bool, int, int, int, int);    virtual QSize sizeHint() { if (sc.isNull()) sc = QSize(li->width(li->listBox()), QMAX(25, li->height(li->listBox()))); return sc; }};void Q3ComboBoxPopupItem::paint( QPainter* p, const QColorGroup&, bool,				bool, int x, int y, int, int){    p->save();    p->translate(x, y + ((sizeHint().height() / 2) - (li->height(li->listBox()) / 2)));    li->paint(p);    p->restore();}class Q3ComboBoxData{public:    Q3ComboBoxData( Q3ComboBox *cb ): current( 0 ), arrowDown(false), ed( 0 ), usingLBox( false ), pop( 0 ), lBox( 0 ), combo( cb )    {	duplicatesEnabled = true;	cb->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) );    }    inline bool usingListBox()  { return usingLBox; }    inline Q3ListBox * listBox() { return lBox; }    inline Q3ComboBoxPopup * popup() { return pop; }    void updateLinedGeometry();    void setListBox( Q3ListBox *l ) { lBox = l ; usingLBox = true;				l->setMouseTracking( true );}    void setPopupMenu( Q3ComboBoxPopup * pm, bool isPopup=true )	{ pop = pm; if(isPopup) usingLBox = false; }    QStyleOptionComboBox getStyleOption() const        {            QStyleOptionComboBox opt;            opt.init(combo);            if (!combo->editable() && combo->hasFocus())                opt.state |= QStyle::State_Selected;            opt.subControls = QStyle::SC_All;            if (arrowDown) {                opt.activeSubControls = QStyle::SC_ComboBoxArrow;                opt.state |= QStyle::State_Sunken;            }            opt.editable = combo->editable();            opt.frame = 1; // ### get from style?            if (current > -1 && current < combo->count()) {                opt.currentText = combo->text(current);                if (combo->pixmap(current))                    opt.currentIcon = QIcon(*combo->pixmap(current));            }            opt.iconSize = QSize(22, 22); // ### need a sane value here//             if (container && container->isVisible())//                 opt.state |= QStyle::State_On;            return opt;        }    int		current;    int		maxCount;    int		sizeLimit;    Q3ComboBox::Policy p;    bool	autoresize;    bool	poppedUp;    bool	mouseWasInsidePopup;    bool	arrowPressed;    bool	arrowDown;    bool	discardNextMousePress;    bool	shortClick;    bool	useCompletion;    bool	completeNow;    int		completeAt;    bool duplicatesEnabled;    int fullHeight, currHeight;    QLineEdit * ed;  // /bin/ed rules!    QTimer *completionTimer;    QSize sizeHint;    QHash<int, QPixmap> popupPixmaps;private:    bool	usingLBox;    Q3ComboBoxPopup *pop;    Q3ListBox   *lBox;    Q3ComboBox *combo;};void Q3ComboBoxData::updateLinedGeometry(){    if ( !ed || !combo )	return;    QStyleOptionComboBox opt = getStyleOption();    QRect r = combo->style()->subControlRect(        QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, combo);    const QPixmap *pix = current < combo->count() ? combo->pixmap( current ) : 0;    if ( pix && pix->width() < r.width() )	r.setLeft( r.left() + pix->width() + 4 );    if ( r != ed->geometry() )	ed->setGeometry( r );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -