📄 qtoolbox.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui 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 "qtoolbox.h"#ifndef QT_NO_TOOLBOX#include <qapplication.h>#include <qeventloop.h>#include <qlayout.h>#include <qlist.h>#include <qpainter.h>#include <qscrollarea.h>#include <qstyle.h>#include <qstyleoption.h>#include <qtooltip.h>#include <qabstractbutton.h>#include "qframe_p.h"class QToolBoxButton : public QAbstractButton{ Q_OBJECTpublic: QToolBoxButton(QWidget *parent) : QAbstractButton(parent), selected(false), indexInPage(-1) { setBackgroundRole(QPalette::Window); setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); setFocusPolicy(Qt::NoFocus); } inline void setSelected(bool b) { selected = b; update(); } inline void setIndex(int newIndex) { indexInPage = newIndex; } QSize sizeHint() const; QSize minimumSizeHint() const;protected: void initStyleOption(QStyleOptionToolBox *opt) const; void paintEvent(QPaintEvent *);private: bool selected; int indexInPage;};#include "qtoolbox.moc"class QToolBoxPrivate : public QFramePrivate{ Q_DECLARE_PUBLIC(QToolBox)public: struct Page { QToolBoxButton *button; QScrollArea *sv; QWidget *widget; inline void setText(const QString &text) { button->setText(text); } inline void setIcon(const QIcon &is) { button->setIcon(is); }#ifndef QT_NO_TOOLTIP inline void setToolTip(const QString &tip) { button->setToolTip(tip); } inline QString toolTip() const { return button->toolTip(); }#endif inline QString text() const { return button->text(); } inline QIcon icon() const { return button->icon(); } inline bool operator==(const Page& other) const { return widget == other.widget; } }; typedef QList<Page> PageList; inline QToolBoxPrivate() : currentPage(0) { } void _q_buttonClicked(); void _q_widgetDestroyed(QObject*); Page *page(QWidget *widget) const; const Page *page(int index) const; Page *page(int index); void updateTabs(); void relayout(); PageList pageList; QVBoxLayout *layout; Page *currentPage;};QToolBoxPrivate::Page *QToolBoxPrivate::page(QWidget *widget) const{ if (!widget) return 0; for (PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd(); ++i) if ((*i).widget == widget) return (Page*) &(*i); return 0;}QToolBoxPrivate::Page *QToolBoxPrivate::page(int index){ if (index >= 0 && index < pageList.size()) return &pageList[index]; return 0;}const QToolBoxPrivate::Page *QToolBoxPrivate::page(int index) const{ if (index >= 0 && index < pageList.size()) return &pageList.at(index); return 0;}void QToolBoxPrivate::updateTabs(){ QToolBoxButton *lastButton = currentPage ? currentPage->button : 0; bool after = false; int index = 0; for (index = 0; index < pageList.count(); ++index) { const Page &page = pageList.at(index); QToolBoxButton *tB = page.button; // update indexes, since the updates are delayed, the indexes will be correct // when we actually paint. tB->setIndex(index); QWidget *tW = page.widget; if (after) { QPalette p = tB->palette(); p.setColor(tB->backgroundRole(), tW->palette().color(tW->backgroundRole())); tB->setPalette(p); tB->update(); } else if (tB->backgroundRole() != QPalette::Window) { tB->setBackgroundRole(QPalette::Window); tB->update(); } after = tB == lastButton; }}QSize QToolBoxButton::sizeHint() const{ QSize iconSize(8, 8); if (!icon().isNull()) { int icone = style()->pixelMetric(QStyle::PM_SmallIconSize); iconSize += QSize(icone + 2, icone); } QSize textSize = fontMetrics().size(Qt::TextShowMnemonic, text()) + QSize(0, 8); QSize total(iconSize.width() + textSize.width(), qMax(iconSize.height(), textSize.height())); return total.expandedTo(QApplication::globalStrut());}QSize QToolBoxButton::minimumSizeHint() const{ if (icon().isNull()) return QSize(); int icone = style()->pixelMetric(QStyle::PM_SmallIconSize); return QSize(icone + 8, icone + 8);}void QToolBoxButton::initStyleOption(QStyleOptionToolBox *option) const{ if (!option) return; option->initFrom(this); if (selected) option->state |= QStyle::State_Selected; if (isDown()) option->state |= QStyle::State_Sunken; option->text = text(); option->icon = icon(); if (QStyleOptionToolBoxV2 *optionV2 = qstyleoption_cast<QStyleOptionToolBoxV2 *>(option)) { QToolBox *toolBox = static_cast<QToolBox *>(parentWidget()); // I know I'm in a tool box. int widgetCount = toolBox->count(); int currIndex = toolBox->currentIndex(); if (widgetCount == 1) { optionV2->position = QStyleOptionToolBoxV2::OnlyOneTab; } else if (indexInPage == 0) { optionV2->position = QStyleOptionToolBoxV2::Beginning; } else if (indexInPage == widgetCount - 1) { optionV2->position = QStyleOptionToolBoxV2::End; } else { optionV2->position = QStyleOptionToolBoxV2::Middle; } if (currIndex == indexInPage - 1) { optionV2->selectedPosition = QStyleOptionToolBoxV2::PreviousIsSelected; } else if (currIndex == indexInPage + 1) { optionV2->selectedPosition = QStyleOptionToolBoxV2::NextIsSelected; } else { optionV2->selectedPosition = QStyleOptionToolBoxV2::NotAdjacent; } }}void QToolBoxButton::paintEvent(QPaintEvent *){ QPainter paint(this); QString text = QAbstractButton::text(); QPainter *p = &paint; QStyleOptionToolBoxV2 opt; initStyleOption(&opt); style()->drawControl(QStyle::CE_ToolBoxTab, &opt, p, parentWidget());}/*! \class QToolBox \brief The QToolBox class provides a column of tabbed widget items. \mainclass \ingroup advanced A toolbox is a widget that displays a column of tabs one above the other, with the current item displayed below the current tab. Every tab has an index position within the column of tabs. A tab's item is a QWidget. Each item has an itemText(), an optional itemIcon(), an optional itemToolTip(), and a widget(). The item's attributes can be changed with setItemText(), setItemIcon(), and setItemToolTip(). Each item can be enabled or disabled individually with setItemEnabled(). Items are added using addItem(), or inserted at particular positions using insertItem(). The total number of items is given by count(). Items can be deleted with delete, or removed from the toolbox with removeItem(). Combining removeItem() and insertItem() allows you to move items to different positions. The index of the current item widget is returned by currentIndex(), and set with setCurrentIndex(). The index of a particular item can be found using indexOf(), and the item at a given index is returned by item(). The currentChanged() signal is emitted when the current item is changed. \sa QTabWidget*//*! \fn void QToolBox::currentChanged(int index) This signal is emitted when the current item is changed. The new current item's index is passed in \a index, or -1 if there is no current item.*/#ifdef QT3_SUPPORT/*! Constructs a toolbox called \a name with parent \a parent and flags \a f.*/QToolBox::QToolBox(QWidget *parent, const char *name, Qt::WindowFlags f) : QFrame(*new QToolBoxPrivate, parent, f){ Q_D(QToolBox); setObjectName(QString::fromAscii(name)); d->layout = new QVBoxLayout(this); d->layout->setMargin(0); setBackgroundRole(QPalette::Button);}#endif/*! Constructs a new toolbox with the given \a parent and the flags, \a f.*/QToolBox::QToolBox(QWidget *parent, Qt::WindowFlags f) : QFrame(*new QToolBoxPrivate, parent, f){ Q_D(QToolBox); d->layout = new QVBoxLayout(this); d->layout->setMargin(0); setBackgroundRole(QPalette::Button);}/*! Destroys the toolbox.*/QToolBox::~QToolBox(){}/*! \fn int QToolBox::addItem(QWidget *w, const QString &text) \overload Adds the widget \a w in a new tab at bottom of the toolbox. The new tab's text is set to \a text. Returns the new tab's index.*//*! \fn int QToolBox::addItem(QWidget *widget, const QIcon &iconSet,const QString &text) Adds the \a widget in a new tab at bottom of the toolbox. The new tab's text is set to \a text, and the \a iconSet is displayed to the left of the \a text. Returns the new tab's index.*//*! \fn int QToolBox::insertItem(int index, QWidget *widget, const QString &text) \overload Inserts the \a widget at position \a index, or at the bottom of the toolbox if \a index is out of range. The new item's text is set to \a text. Returns the new item's index.*//*! Inserts the \a widget at position \a index, or at the bottom of the toolbox if \a index is out of range. The new item's text is set to \a text, and the \a icon is displayed to the left of the \a text. Returns the new item's index.*/int QToolBox::insertItem(int index, QWidget *widget, const QIcon &icon, const QString &text){ if (!widget) return -1; Q_D(QToolBox); connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(_q_widgetDestroyed(QObject*))); QToolBoxPrivate::Page c; c.widget = widget; c.button = new QToolBoxButton(this); connect(c.button, SIGNAL(clicked()), this, SLOT(_q_buttonClicked())); c.sv = new QScrollArea(this); c.sv->setWidget(widget); c.sv->setWidgetResizable(true); c.sv->hide(); c.sv->setFrameStyle(QFrame::NoFrame); c.setText(text); c.setIcon(icon); if (index < 0 || index >= (int)d->pageList.count()) { index = d->pageList.count(); d->pageList.append(c); d->layout->addWidget(c.button); d->layout->addWidget(c.sv); if (index == 0) setCurrentIndex(index); } else { d->pageList.insert(index, c); d->relayout(); if (d->currentPage) { QWidget *current = d->currentPage->widget; int oldindex = indexOf(current); if (index <= oldindex) { d->currentPage = 0; // trigger change setCurrentIndex(oldindex); } } } c.button->show(); d->updateTabs(); itemInserted(index); return index;}void QToolBoxPrivate::_q_buttonClicked(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -