| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qtoolbox.h"
|
|---|
| 43 |
|
|---|
| 44 | #ifndef QT_NO_TOOLBOX
|
|---|
| 45 |
|
|---|
| 46 | #include <qapplication.h>
|
|---|
| 47 | #include <qeventloop.h>
|
|---|
| 48 | #include <qlayout.h>
|
|---|
| 49 | #include <qlist.h>
|
|---|
| 50 | #include <qpainter.h>
|
|---|
| 51 | #include <qscrollarea.h>
|
|---|
| 52 | #include <qstyle.h>
|
|---|
| 53 | #include <qstyleoption.h>
|
|---|
| 54 | #include <qtooltip.h>
|
|---|
| 55 | #include <qabstractbutton.h>
|
|---|
| 56 |
|
|---|
| 57 | #include "qframe_p.h"
|
|---|
| 58 |
|
|---|
| 59 | QT_BEGIN_NAMESPACE
|
|---|
| 60 |
|
|---|
| 61 | class QToolBoxButton : public QAbstractButton
|
|---|
| 62 | {
|
|---|
| 63 | Q_OBJECT
|
|---|
| 64 | public:
|
|---|
| 65 | QToolBoxButton(QWidget *parent)
|
|---|
| 66 | : QAbstractButton(parent), selected(false), indexInPage(-1)
|
|---|
| 67 | {
|
|---|
| 68 | setBackgroundRole(QPalette::Window);
|
|---|
| 69 | setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
|
|---|
| 70 | setFocusPolicy(Qt::NoFocus);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | inline void setSelected(bool b) { selected = b; update(); }
|
|---|
| 74 | inline void setIndex(int newIndex) { indexInPage = newIndex; }
|
|---|
| 75 |
|
|---|
| 76 | QSize sizeHint() const;
|
|---|
| 77 | QSize minimumSizeHint() const;
|
|---|
| 78 |
|
|---|
| 79 | protected:
|
|---|
| 80 | void initStyleOption(QStyleOptionToolBox *opt) const;
|
|---|
| 81 | void paintEvent(QPaintEvent *);
|
|---|
| 82 |
|
|---|
| 83 | private:
|
|---|
| 84 | bool selected;
|
|---|
| 85 | int indexInPage;
|
|---|
| 86 | };
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 | class QToolBoxPrivate : public QFramePrivate
|
|---|
| 90 | {
|
|---|
| 91 | Q_DECLARE_PUBLIC(QToolBox)
|
|---|
| 92 | public:
|
|---|
| 93 | struct Page
|
|---|
| 94 | {
|
|---|
| 95 | QToolBoxButton *button;
|
|---|
| 96 | QScrollArea *sv;
|
|---|
| 97 | QWidget *widget;
|
|---|
| 98 |
|
|---|
| 99 | inline void setText(const QString &text) { button->setText(text); }
|
|---|
| 100 | inline void setIcon(const QIcon &is) { button->setIcon(is); }
|
|---|
| 101 | #ifndef QT_NO_TOOLTIP
|
|---|
| 102 | inline void setToolTip(const QString &tip) { button->setToolTip(tip); }
|
|---|
| 103 | inline QString toolTip() const { return button->toolTip(); }
|
|---|
| 104 | #endif
|
|---|
| 105 | inline QString text() const { return button->text(); }
|
|---|
| 106 | inline QIcon icon() const { return button->icon(); }
|
|---|
| 107 |
|
|---|
| 108 | inline bool operator==(const Page& other) const
|
|---|
| 109 | {
|
|---|
| 110 | return widget == other.widget;
|
|---|
| 111 | }
|
|---|
| 112 | };
|
|---|
| 113 | typedef QList<Page> PageList;
|
|---|
| 114 |
|
|---|
| 115 | inline QToolBoxPrivate()
|
|---|
| 116 | : currentPage(0)
|
|---|
| 117 | {
|
|---|
| 118 | }
|
|---|
| 119 | void _q_buttonClicked();
|
|---|
| 120 | void _q_widgetDestroyed(QObject*);
|
|---|
| 121 |
|
|---|
| 122 | Page *page(QWidget *widget) const;
|
|---|
| 123 | const Page *page(int index) const;
|
|---|
| 124 | Page *page(int index);
|
|---|
| 125 |
|
|---|
| 126 | void updateTabs();
|
|---|
| 127 | void relayout();
|
|---|
| 128 |
|
|---|
| 129 | PageList pageList;
|
|---|
| 130 | QVBoxLayout *layout;
|
|---|
| 131 | Page *currentPage;
|
|---|
| 132 | };
|
|---|
| 133 |
|
|---|
| 134 | QToolBoxPrivate::Page *QToolBoxPrivate::page(QWidget *widget) const
|
|---|
| 135 | {
|
|---|
| 136 | if (!widget)
|
|---|
| 137 | return 0;
|
|---|
| 138 |
|
|---|
| 139 | for (PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd(); ++i)
|
|---|
| 140 | if ((*i).widget == widget)
|
|---|
| 141 | return (Page*) &(*i);
|
|---|
| 142 | return 0;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | QToolBoxPrivate::Page *QToolBoxPrivate::page(int index)
|
|---|
| 146 | {
|
|---|
| 147 | if (index >= 0 && index < pageList.size())
|
|---|
| 148 | return &pageList[index];
|
|---|
| 149 | return 0;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | const QToolBoxPrivate::Page *QToolBoxPrivate::page(int index) const
|
|---|
| 153 | {
|
|---|
| 154 | if (index >= 0 && index < pageList.size())
|
|---|
| 155 | return &pageList.at(index);
|
|---|
| 156 | return 0;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | void QToolBoxPrivate::updateTabs()
|
|---|
| 160 | {
|
|---|
| 161 | QToolBoxButton *lastButton = currentPage ? currentPage->button : 0;
|
|---|
| 162 | bool after = false;
|
|---|
| 163 | int index = 0;
|
|---|
| 164 | for (index = 0; index < pageList.count(); ++index) {
|
|---|
| 165 | const Page &page = pageList.at(index);
|
|---|
| 166 | QToolBoxButton *tB = page.button;
|
|---|
| 167 | // update indexes, since the updates are delayed, the indexes will be correct
|
|---|
| 168 | // when we actually paint.
|
|---|
| 169 | tB->setIndex(index);
|
|---|
| 170 | QWidget *tW = page.widget;
|
|---|
| 171 | if (after) {
|
|---|
| 172 | QPalette p = tB->palette();
|
|---|
| 173 | p.setColor(tB->backgroundRole(), tW->palette().color(tW->backgroundRole()));
|
|---|
| 174 | tB->setPalette(p);
|
|---|
| 175 | tB->update();
|
|---|
| 176 | } else if (tB->backgroundRole() != QPalette::Window) {
|
|---|
| 177 | tB->setBackgroundRole(QPalette::Window);
|
|---|
| 178 | tB->update();
|
|---|
| 179 | }
|
|---|
| 180 | after = tB == lastButton;
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | QSize QToolBoxButton::sizeHint() const
|
|---|
| 185 | {
|
|---|
| 186 | QSize iconSize(8, 8);
|
|---|
| 187 | if (!icon().isNull()) {
|
|---|
| 188 | int icone = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, parentWidget() /* QToolBox */);
|
|---|
| 189 | iconSize += QSize(icone + 2, icone);
|
|---|
| 190 | }
|
|---|
| 191 | QSize textSize = fontMetrics().size(Qt::TextShowMnemonic, text()) + QSize(0, 8);
|
|---|
| 192 |
|
|---|
| 193 | QSize total(iconSize.width() + textSize.width(), qMax(iconSize.height(), textSize.height()));
|
|---|
| 194 | return total.expandedTo(QApplication::globalStrut());
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | QSize QToolBoxButton::minimumSizeHint() const
|
|---|
| 198 | {
|
|---|
| 199 | if (icon().isNull())
|
|---|
| 200 | return QSize();
|
|---|
| 201 | int icone = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, parentWidget() /* QToolBox */);
|
|---|
| 202 | return QSize(icone + 8, icone + 8);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | void QToolBoxButton::initStyleOption(QStyleOptionToolBox *option) const
|
|---|
| 206 | {
|
|---|
| 207 | if (!option)
|
|---|
| 208 | return;
|
|---|
| 209 | option->initFrom(this);
|
|---|
| 210 | if (selected)
|
|---|
| 211 | option->state |= QStyle::State_Selected;
|
|---|
| 212 | if (isDown())
|
|---|
| 213 | option->state |= QStyle::State_Sunken;
|
|---|
| 214 | option->text = text();
|
|---|
| 215 | option->icon = icon();
|
|---|
| 216 |
|
|---|
| 217 | if (QStyleOptionToolBoxV2 *optionV2 = qstyleoption_cast<QStyleOptionToolBoxV2 *>(option)) {
|
|---|
| 218 | QToolBox *toolBox = static_cast<QToolBox *>(parentWidget()); // I know I'm in a tool box.
|
|---|
| 219 | int widgetCount = toolBox->count();
|
|---|
| 220 | int currIndex = toolBox->currentIndex();
|
|---|
| 221 | if (widgetCount == 1) {
|
|---|
| 222 | optionV2->position = QStyleOptionToolBoxV2::OnlyOneTab;
|
|---|
| 223 | } else if (indexInPage == 0) {
|
|---|
| 224 | optionV2->position = QStyleOptionToolBoxV2::Beginning;
|
|---|
| 225 | } else if (indexInPage == widgetCount - 1) {
|
|---|
| 226 | optionV2->position = QStyleOptionToolBoxV2::End;
|
|---|
| 227 | } else {
|
|---|
| 228 | optionV2->position = QStyleOptionToolBoxV2::Middle;
|
|---|
| 229 | }
|
|---|
| 230 | if (currIndex == indexInPage - 1) {
|
|---|
| 231 | optionV2->selectedPosition = QStyleOptionToolBoxV2::PreviousIsSelected;
|
|---|
| 232 | } else if (currIndex == indexInPage + 1) {
|
|---|
| 233 | optionV2->selectedPosition = QStyleOptionToolBoxV2::NextIsSelected;
|
|---|
| 234 | } else {
|
|---|
| 235 | optionV2->selectedPosition = QStyleOptionToolBoxV2::NotAdjacent;
|
|---|
| 236 | }
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | void QToolBoxButton::paintEvent(QPaintEvent *)
|
|---|
| 241 | {
|
|---|
| 242 | QPainter paint(this);
|
|---|
| 243 | QString text = QAbstractButton::text();
|
|---|
| 244 | QPainter *p = &paint;
|
|---|
| 245 | QStyleOptionToolBoxV2 opt;
|
|---|
| 246 | initStyleOption(&opt);
|
|---|
| 247 | style()->drawControl(QStyle::CE_ToolBoxTab, &opt, p, parentWidget());
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /*!
|
|---|
| 251 | \class QToolBox
|
|---|
| 252 |
|
|---|
| 253 | \brief The QToolBox class provides a column of tabbed widget items.
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 | \ingroup basicwidgets
|
|---|
| 257 |
|
|---|
| 258 | A toolbox is a widget that displays a column of tabs one above the
|
|---|
| 259 | other, with the current item displayed below the current tab.
|
|---|
| 260 | Every tab has an index position within the column of tabs. A tab's
|
|---|
| 261 | item is a QWidget.
|
|---|
| 262 |
|
|---|
| 263 | Each item has an itemText(), an optional itemIcon(), an optional
|
|---|
| 264 | itemToolTip(), and a widget(). The item's attributes can be
|
|---|
| 265 | changed with setItemText(), setItemIcon(), and
|
|---|
| 266 | setItemToolTip(). Each item can be enabled or disabled
|
|---|
| 267 | individually with setItemEnabled().
|
|---|
| 268 |
|
|---|
| 269 | Items are added using addItem(), or inserted at particular
|
|---|
| 270 | positions using insertItem(). The total number of items is given
|
|---|
| 271 | by count(). Items can be deleted with delete, or removed from the
|
|---|
| 272 | toolbox with removeItem(). Combining removeItem() and insertItem()
|
|---|
| 273 | allows you to move items to different positions.
|
|---|
| 274 |
|
|---|
| 275 | The index of the current item widget is returned by currentIndex(),
|
|---|
| 276 | and set with setCurrentIndex(). The index of a particular item can
|
|---|
| 277 | be found using indexOf(), and the item at a given index is returned
|
|---|
| 278 | by item().
|
|---|
| 279 |
|
|---|
| 280 | The currentChanged() signal is emitted when the current item is
|
|---|
| 281 | changed.
|
|---|
| 282 |
|
|---|
| 283 | \sa QTabWidget
|
|---|
| 284 | */
|
|---|
| 285 |
|
|---|
| 286 | /*!
|
|---|
| 287 | \fn void QToolBox::currentChanged(int index)
|
|---|
| 288 |
|
|---|
| 289 | This signal is emitted when the current item is changed. The new
|
|---|
| 290 | current item's index is passed in \a index, or -1 if there is no
|
|---|
| 291 | current item.
|
|---|
| 292 | */
|
|---|
| 293 |
|
|---|
| 294 | #ifdef QT3_SUPPORT
|
|---|
| 295 | /*!
|
|---|
| 296 | Constructs a toolbox called \a name with parent \a parent and flags \a f.
|
|---|
| 297 | */
|
|---|
| 298 | QToolBox::QToolBox(QWidget *parent, const char *name, Qt::WindowFlags f)
|
|---|
| 299 | : QFrame(*new QToolBoxPrivate, parent, f)
|
|---|
| 300 | {
|
|---|
| 301 | Q_D(QToolBox);
|
|---|
| 302 | setObjectName(QString::fromAscii(name));
|
|---|
| 303 | d->layout = new QVBoxLayout(this);
|
|---|
| 304 | d->layout->setMargin(0);
|
|---|
| 305 | setBackgroundRole(QPalette::Button);
|
|---|
| 306 | }
|
|---|
| 307 | #endif
|
|---|
| 308 |
|
|---|
| 309 | /*!
|
|---|
| 310 | Constructs a new toolbox with the given \a parent and the flags, \a f.
|
|---|
| 311 | */
|
|---|
| 312 | QToolBox::QToolBox(QWidget *parent, Qt::WindowFlags f)
|
|---|
| 313 | : QFrame(*new QToolBoxPrivate, parent, f)
|
|---|
| 314 | {
|
|---|
| 315 | Q_D(QToolBox);
|
|---|
| 316 | d->layout = new QVBoxLayout(this);
|
|---|
| 317 | d->layout->setMargin(0);
|
|---|
| 318 | setBackgroundRole(QPalette::Button);
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | /*!
|
|---|
| 322 | Destroys the toolbox.
|
|---|
| 323 | */
|
|---|
| 324 |
|
|---|
| 325 | QToolBox::~QToolBox()
|
|---|
| 326 | {
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | /*!
|
|---|
| 330 | \fn int QToolBox::addItem(QWidget *w, const QString &text)
|
|---|
| 331 | \overload
|
|---|
| 332 |
|
|---|
| 333 | Adds the widget \a w in a new tab at bottom of the toolbox. The
|
|---|
| 334 | new tab's text is set to \a text. Returns the new tab's index.
|
|---|
| 335 | */
|
|---|
| 336 |
|
|---|
| 337 | /*!
|
|---|
| 338 | \fn int QToolBox::addItem(QWidget *widget, const QIcon &iconSet,const QString &text)
|
|---|
| 339 | Adds the \a widget in a new tab at bottom of the toolbox. The
|
|---|
| 340 | new tab's text is set to \a text, and the \a iconSet is
|
|---|
| 341 | displayed to the left of the \a text. Returns the new tab's index.
|
|---|
| 342 | */
|
|---|
| 343 |
|
|---|
| 344 | /*!
|
|---|
| 345 | \fn int QToolBox::insertItem(int index, QWidget *widget, const QString &text)
|
|---|
| 346 | \overload
|
|---|
| 347 |
|
|---|
| 348 | Inserts the \a widget at position \a index, or at the bottom
|
|---|
| 349 | of the toolbox if \a index is out of range. The new item's text is
|
|---|
| 350 | set to \a text. Returns the new item's index.
|
|---|
| 351 | */
|
|---|
| 352 |
|
|---|
| 353 | /*!
|
|---|
| 354 | Inserts the \a widget at position \a index, or at the bottom
|
|---|
| 355 | of the toolbox if \a index is out of range. The new item's text
|
|---|
| 356 | is set to \a text, and the \a icon is displayed to the left of
|
|---|
| 357 | the \a text. Returns the new item's index.
|
|---|
| 358 | */
|
|---|
| 359 |
|
|---|
| 360 | int QToolBox::insertItem(int index, QWidget *widget, const QIcon &icon, const QString &text)
|
|---|
| 361 | {
|
|---|
| 362 | if (!widget)
|
|---|
| 363 | return -1;
|
|---|
| 364 |
|
|---|
| 365 | Q_D(QToolBox);
|
|---|
| 366 | connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(_q_widgetDestroyed(QObject*)));
|
|---|
| 367 |
|
|---|
| 368 | QToolBoxPrivate::Page c;
|
|---|
| 369 | c.widget = widget;
|
|---|
| 370 | c.button = new QToolBoxButton(this);
|
|---|
| 371 | c.button->setObjectName(QLatin1String("qt_toolbox_toolboxbutton"));
|
|---|
| 372 | connect(c.button, SIGNAL(clicked()), this, SLOT(_q_buttonClicked()));
|
|---|
| 373 |
|
|---|
| 374 | c.sv = new QScrollArea(this);
|
|---|
| 375 | c.sv->setWidget(widget);
|
|---|
| 376 | c.sv->setWidgetResizable(true);
|
|---|
| 377 | c.sv->hide();
|
|---|
| 378 | c.sv->setFrameStyle(QFrame::NoFrame);
|
|---|
| 379 |
|
|---|
| 380 | c.setText(text);
|
|---|
| 381 | c.setIcon(icon);
|
|---|
| 382 |
|
|---|
| 383 | if (index < 0 || index >= (int)d->pageList.count()) {
|
|---|
| 384 | index = d->pageList.count();
|
|---|
| 385 | d->pageList.append(c);
|
|---|
| 386 | d->layout->addWidget(c.button);
|
|---|
| 387 | d->layout->addWidget(c.sv);
|
|---|
| 388 | if (index == 0)
|
|---|
| 389 | setCurrentIndex(index);
|
|---|
| 390 | } else {
|
|---|
| 391 | d->pageList.insert(index, c);
|
|---|
| 392 | d->relayout();
|
|---|
| 393 | if (d->currentPage) {
|
|---|
| 394 | QWidget *current = d->currentPage->widget;
|
|---|
| 395 | int oldindex = indexOf(current);
|
|---|
| 396 | if (index <= oldindex) {
|
|---|
| 397 | d->currentPage = 0; // trigger change
|
|---|
| 398 | setCurrentIndex(oldindex);
|
|---|
| 399 | }
|
|---|
| 400 | }
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | c.button->show();
|
|---|
| 404 |
|
|---|
| 405 | d->updateTabs();
|
|---|
| 406 | itemInserted(index);
|
|---|
| 407 | return index;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | void QToolBoxPrivate::_q_buttonClicked()
|
|---|
| 411 | {
|
|---|
| 412 | Q_Q(QToolBox);
|
|---|
| 413 | QToolBoxButton *tb = qobject_cast<QToolBoxButton*>(q->sender());
|
|---|
| 414 | QWidget* item = 0;
|
|---|
| 415 | for (QToolBoxPrivate::PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd(); ++i)
|
|---|
| 416 | if ((*i).button == tb) {
|
|---|
| 417 | item = (*i).widget;
|
|---|
| 418 | break;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | q->setCurrentIndex(q->indexOf(item));
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | /*!
|
|---|
| 425 | \property QToolBox::count
|
|---|
| 426 | \brief The number of items contained in the toolbox.
|
|---|
| 427 |
|
|---|
| 428 | By default, this property has a value of 0.
|
|---|
| 429 | */
|
|---|
| 430 |
|
|---|
| 431 | int QToolBox::count() const
|
|---|
| 432 | {
|
|---|
| 433 | Q_D(const QToolBox);
|
|---|
| 434 | return d->pageList.count();
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | void QToolBox::setCurrentIndex(int index)
|
|---|
| 438 | {
|
|---|
| 439 | Q_D(QToolBox);
|
|---|
| 440 | QToolBoxPrivate::Page *c = d->page(index);
|
|---|
| 441 | if (!c || d->currentPage == c)
|
|---|
| 442 | return;
|
|---|
| 443 |
|
|---|
| 444 | c->button->setSelected(true);
|
|---|
| 445 | if (d->currentPage) {
|
|---|
| 446 | d->currentPage->sv->hide();
|
|---|
| 447 | d->currentPage->button->setSelected(false);
|
|---|
| 448 | }
|
|---|
| 449 | d->currentPage = c;
|
|---|
| 450 | d->currentPage->sv->show();
|
|---|
| 451 | d->updateTabs();
|
|---|
| 452 | emit currentChanged(index);
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | void QToolBoxPrivate::relayout()
|
|---|
| 456 | {
|
|---|
| 457 | Q_Q(QToolBox);
|
|---|
| 458 | delete layout;
|
|---|
| 459 | layout = new QVBoxLayout(q);
|
|---|
| 460 | layout->setMargin(0);
|
|---|
| 461 | for (QToolBoxPrivate::PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd(); ++i) {
|
|---|
| 462 | layout->addWidget((*i).button);
|
|---|
| 463 | layout->addWidget((*i).sv);
|
|---|
| 464 | }
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | void QToolBoxPrivate::_q_widgetDestroyed(QObject *object)
|
|---|
| 468 | {
|
|---|
| 469 | Q_Q(QToolBox);
|
|---|
| 470 | // no verification - vtbl corrupted already
|
|---|
| 471 | QWidget *p = (QWidget*)object;
|
|---|
| 472 |
|
|---|
| 473 | QToolBoxPrivate::Page *c = page(p);
|
|---|
| 474 | if (!p || !c)
|
|---|
| 475 | return;
|
|---|
| 476 |
|
|---|
| 477 | layout->removeWidget(c->sv);
|
|---|
| 478 | layout->removeWidget(c->button);
|
|---|
| 479 | c->sv->deleteLater(); // page might still be a child of sv
|
|---|
| 480 | delete c->button;
|
|---|
| 481 |
|
|---|
| 482 | bool removeCurrent = c == currentPage;
|
|---|
| 483 | pageList.removeAll(*c);
|
|---|
| 484 |
|
|---|
| 485 | if (!pageList.count()) {
|
|---|
| 486 | currentPage = 0;
|
|---|
| 487 | emit q->currentChanged(-1);
|
|---|
| 488 | } else if (removeCurrent) {
|
|---|
| 489 | currentPage = 0;
|
|---|
| 490 | q->setCurrentIndex(0);
|
|---|
| 491 | }
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | /*!
|
|---|
| 495 | Removes the item at position \a index from the toolbox. Note that
|
|---|
| 496 | the widget is \e not deleted.
|
|---|
| 497 | */
|
|---|
| 498 |
|
|---|
| 499 | void QToolBox::removeItem(int index)
|
|---|
| 500 | {
|
|---|
| 501 | Q_D(QToolBox);
|
|---|
| 502 | if (QWidget *w = widget(index)) {
|
|---|
| 503 | disconnect(w, SIGNAL(destroyed(QObject*)), this, SLOT(_q_widgetDestroyed(QObject*)));
|
|---|
| 504 | w->setParent(this);
|
|---|
| 505 | // destroy internal data
|
|---|
| 506 | d->_q_widgetDestroyed(w);
|
|---|
| 507 | itemRemoved(index);
|
|---|
| 508 | }
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 | /*!
|
|---|
| 513 | \property QToolBox::currentIndex
|
|---|
| 514 | \brief the index of the current item
|
|---|
| 515 |
|
|---|
| 516 | By default, for an empty toolbox, this property has a value of -1.
|
|---|
| 517 |
|
|---|
| 518 | \sa indexOf(), widget()
|
|---|
| 519 | */
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 | int QToolBox::currentIndex() const
|
|---|
| 523 | {
|
|---|
| 524 | Q_D(const QToolBox);
|
|---|
| 525 | return d->currentPage ? indexOf(d->currentPage->widget) : -1;
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | /*!
|
|---|
| 529 | Returns a pointer to the current widget, or 0 if there is no such item.
|
|---|
| 530 |
|
|---|
| 531 | \sa currentIndex(), setCurrentWidget()
|
|---|
| 532 | */
|
|---|
| 533 |
|
|---|
| 534 | QWidget * QToolBox::currentWidget() const
|
|---|
| 535 | {
|
|---|
| 536 | Q_D(const QToolBox);
|
|---|
| 537 | return d->currentPage ? d->currentPage->widget : 0;
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | /*!
|
|---|
| 541 | Makes\a widget the current widget. The \a widget must be an item in this tool box.
|
|---|
| 542 |
|
|---|
| 543 | \sa addItem(), setCurrentIndex(), currentWidget()
|
|---|
| 544 | */
|
|---|
| 545 | void QToolBox::setCurrentWidget(QWidget *widget)
|
|---|
| 546 | {
|
|---|
| 547 | int i = indexOf(widget);
|
|---|
| 548 | if (i >= 0)
|
|---|
| 549 | setCurrentIndex(i);
|
|---|
| 550 | else
|
|---|
| 551 | qWarning("QToolBox::setCurrentWidget: widget not contained in tool box");
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | /*!
|
|---|
| 555 | Returns the widget at position \a index, or 0 if there is no such
|
|---|
| 556 | item.
|
|---|
| 557 | */
|
|---|
| 558 |
|
|---|
| 559 | QWidget *QToolBox::widget(int index) const
|
|---|
| 560 | {
|
|---|
| 561 | Q_D(const QToolBox);
|
|---|
| 562 | if (index < 0 || index >= (int) d->pageList.size())
|
|---|
| 563 | return 0;
|
|---|
| 564 | return d->pageList.at(index).widget;
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | /*!
|
|---|
| 568 | Returns the index of \a widget, or -1 if the item does not
|
|---|
| 569 | exist.
|
|---|
| 570 | */
|
|---|
| 571 |
|
|---|
| 572 | int QToolBox::indexOf(QWidget *widget) const
|
|---|
| 573 | {
|
|---|
| 574 | Q_D(const QToolBox);
|
|---|
| 575 | QToolBoxPrivate::Page *c = (widget ? d->page(widget) : 0);
|
|---|
| 576 | return c ? d->pageList.indexOf(*c) : -1;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | /*!
|
|---|
| 580 | If \a enabled is true then the item at position \a index is enabled; otherwise
|
|---|
| 581 | the item at position \a index is disabled.
|
|---|
| 582 | */
|
|---|
| 583 |
|
|---|
| 584 | void QToolBox::setItemEnabled(int index, bool enabled)
|
|---|
| 585 | {
|
|---|
| 586 | Q_D(QToolBox);
|
|---|
| 587 | QToolBoxPrivate::Page *c = d->page(index);
|
|---|
| 588 | if (!c)
|
|---|
| 589 | return;
|
|---|
| 590 |
|
|---|
| 591 | c->button->setEnabled(enabled);
|
|---|
| 592 | if (!enabled && c == d->currentPage) {
|
|---|
| 593 | int curIndexUp = index;
|
|---|
| 594 | int curIndexDown = curIndexUp;
|
|---|
| 595 | const int count = d->pageList.count();
|
|---|
| 596 | while (curIndexUp > 0 || curIndexDown < count-1) {
|
|---|
| 597 | if (curIndexDown < count-1) {
|
|---|
| 598 | if (d->page(++curIndexDown)->button->isEnabled()) {
|
|---|
| 599 | index = curIndexDown;
|
|---|
| 600 | break;
|
|---|
| 601 | }
|
|---|
| 602 | }
|
|---|
| 603 | if (curIndexUp > 0) {
|
|---|
| 604 | if (d->page(--curIndexUp)->button->isEnabled()) {
|
|---|
| 605 | index = curIndexUp;
|
|---|
| 606 | break;
|
|---|
| 607 | }
|
|---|
| 608 | }
|
|---|
| 609 | }
|
|---|
| 610 | setCurrentIndex(index);
|
|---|
| 611 | }
|
|---|
| 612 | }
|
|---|
| 613 |
|
|---|
| 614 |
|
|---|
| 615 | /*!
|
|---|
| 616 | Sets the text of the item at position \a index to \a text.
|
|---|
| 617 |
|
|---|
| 618 | If the provided text contains an ampersand character ('&'), a
|
|---|
| 619 | mnemonic is automatically created for it. The character that
|
|---|
| 620 | follows the '&' will be used as the shortcut key. Any previous
|
|---|
| 621 | mnemonic will be overwritten, or cleared if no mnemonic is defined
|
|---|
| 622 | by the text. See the \l {QShortcut#mnemonic}{QShortcut}
|
|---|
| 623 | documentation for details (to display an actual ampersand, use
|
|---|
| 624 | '&&').
|
|---|
| 625 | */
|
|---|
| 626 |
|
|---|
| 627 | void QToolBox::setItemText(int index, const QString &text)
|
|---|
| 628 | {
|
|---|
| 629 | Q_D(QToolBox);
|
|---|
| 630 | QToolBoxPrivate::Page *c = d->page(index);
|
|---|
| 631 | if (c)
|
|---|
| 632 | c->setText(text);
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | /*!
|
|---|
| 636 | Sets the icon of the item at position \a index to \a icon.
|
|---|
| 637 | */
|
|---|
| 638 |
|
|---|
| 639 | void QToolBox::setItemIcon(int index, const QIcon &icon)
|
|---|
| 640 | {
|
|---|
| 641 | Q_D(QToolBox);
|
|---|
| 642 | QToolBoxPrivate::Page *c = d->page(index);
|
|---|
| 643 | if (c)
|
|---|
| 644 | c->setIcon(icon);
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | #ifndef QT_NO_TOOLTIP
|
|---|
| 648 | /*!
|
|---|
| 649 | Sets the tooltip of the item at position \a index to \a toolTip.
|
|---|
| 650 | */
|
|---|
| 651 |
|
|---|
| 652 | void QToolBox::setItemToolTip(int index, const QString &toolTip)
|
|---|
| 653 | {
|
|---|
| 654 | Q_D(QToolBox);
|
|---|
| 655 | QToolBoxPrivate::Page *c = d->page(index);
|
|---|
| 656 | if (c)
|
|---|
| 657 | c->setToolTip(toolTip);
|
|---|
| 658 | }
|
|---|
| 659 | #endif // QT_NO_TOOLTIP
|
|---|
| 660 |
|
|---|
| 661 | /*!
|
|---|
| 662 | Returns true if the item at position \a index is enabled; otherwise returns false.
|
|---|
| 663 | */
|
|---|
| 664 |
|
|---|
| 665 | bool QToolBox::isItemEnabled(int index) const
|
|---|
| 666 | {
|
|---|
| 667 | Q_D(const QToolBox);
|
|---|
| 668 | const QToolBoxPrivate::Page *c = d->page(index);
|
|---|
| 669 | return c && c->button->isEnabled();
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | /*!
|
|---|
| 673 | Returns the text of the item at position \a index, or an empty string if
|
|---|
| 674 | \a index is out of range.
|
|---|
| 675 | */
|
|---|
| 676 |
|
|---|
| 677 | QString QToolBox::itemText(int index) const
|
|---|
| 678 | {
|
|---|
| 679 | Q_D(const QToolBox);
|
|---|
| 680 | const QToolBoxPrivate::Page *c = d->page(index);
|
|---|
| 681 | return (c ? c->text() : QString());
|
|---|
| 682 | }
|
|---|
| 683 |
|
|---|
| 684 | /*!
|
|---|
| 685 | Returns the icon of the item at position \a index, or a null
|
|---|
| 686 | icon if \a index is out of range.
|
|---|
| 687 | */
|
|---|
| 688 |
|
|---|
| 689 | QIcon QToolBox::itemIcon(int index) const
|
|---|
| 690 | {
|
|---|
| 691 | Q_D(const QToolBox);
|
|---|
| 692 | const QToolBoxPrivate::Page *c = d->page(index);
|
|---|
| 693 | return (c ? c->icon() : QIcon());
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | #ifndef QT_NO_TOOLTIP
|
|---|
| 697 | /*!
|
|---|
| 698 | Returns the tooltip of the item at position \a index, or an
|
|---|
| 699 | empty string if \a index is out of range.
|
|---|
| 700 | */
|
|---|
| 701 |
|
|---|
| 702 | QString QToolBox::itemToolTip(int index) const
|
|---|
| 703 | {
|
|---|
| 704 | Q_D(const QToolBox);
|
|---|
| 705 | const QToolBoxPrivate::Page *c = d->page(index);
|
|---|
| 706 | return (c ? c->toolTip() : QString());
|
|---|
| 707 | }
|
|---|
| 708 | #endif // QT_NO_TOOLTIP
|
|---|
| 709 |
|
|---|
| 710 | /*! \reimp */
|
|---|
| 711 | void QToolBox::showEvent(QShowEvent *e)
|
|---|
| 712 | {
|
|---|
| 713 | QWidget::showEvent(e);
|
|---|
| 714 | }
|
|---|
| 715 |
|
|---|
| 716 | /*! \reimp */
|
|---|
| 717 | void QToolBox::changeEvent(QEvent *ev)
|
|---|
| 718 | {
|
|---|
| 719 | Q_D(QToolBox);
|
|---|
| 720 | if(ev->type() == QEvent::StyleChange)
|
|---|
| 721 | d->updateTabs();
|
|---|
| 722 | QFrame::changeEvent(ev);
|
|---|
| 723 | }
|
|---|
| 724 |
|
|---|
| 725 | /*!
|
|---|
| 726 | This virtual handler is called after a new item was added or
|
|---|
| 727 | inserted at position \a index.
|
|---|
| 728 |
|
|---|
| 729 | \sa itemRemoved()
|
|---|
| 730 | */
|
|---|
| 731 | void QToolBox::itemInserted(int index)
|
|---|
| 732 | {
|
|---|
| 733 | Q_UNUSED(index)
|
|---|
| 734 | }
|
|---|
| 735 |
|
|---|
| 736 | /*!
|
|---|
| 737 | This virtual handler is called after an item was removed from
|
|---|
| 738 | position \a index.
|
|---|
| 739 |
|
|---|
| 740 | \sa itemInserted()
|
|---|
| 741 | */
|
|---|
| 742 | void QToolBox::itemRemoved(int index)
|
|---|
| 743 | {
|
|---|
| 744 | Q_UNUSED(index)
|
|---|
| 745 | }
|
|---|
| 746 |
|
|---|
| 747 | /*!
|
|---|
| 748 | \fn void QToolBox::setItemLabel(int index, const QString &text)
|
|---|
| 749 |
|
|---|
| 750 | Use setItemText() instead.
|
|---|
| 751 | */
|
|---|
| 752 |
|
|---|
| 753 | /*!
|
|---|
| 754 | \fn QString QToolBox::itemLabel(int index) const
|
|---|
| 755 |
|
|---|
| 756 | Use itemText() instead.
|
|---|
| 757 | */
|
|---|
| 758 |
|
|---|
| 759 | /*!
|
|---|
| 760 | \fn QWidget *QToolBox::currentItem() const
|
|---|
| 761 |
|
|---|
| 762 | Use widget(currentIndex()) instead.
|
|---|
| 763 | */
|
|---|
| 764 |
|
|---|
| 765 | /*!
|
|---|
| 766 | \fn void QToolBox::setCurrentItem(QWidget *widget)
|
|---|
| 767 |
|
|---|
| 768 | Use setCurrentIndex(indexOf(widget)) instead.
|
|---|
| 769 | */
|
|---|
| 770 |
|
|---|
| 771 | /*!
|
|---|
| 772 | \fn void QToolBox::setItemIconSet(int index, const QIcon &icon)
|
|---|
| 773 |
|
|---|
| 774 | Use setItemIcon() instead.
|
|---|
| 775 | */
|
|---|
| 776 |
|
|---|
| 777 | /*!
|
|---|
| 778 | \fn QIcon QToolBox::itemIconSet(int index) const
|
|---|
| 779 |
|
|---|
| 780 | Use itemIcon() instead.
|
|---|
| 781 | */
|
|---|
| 782 |
|
|---|
| 783 | /*!
|
|---|
| 784 | \fn int QToolBox::removeItem(QWidget *widget)
|
|---|
| 785 |
|
|---|
| 786 | Use toolbox->removeItem(toolbox->indexOf(widget)) instead.
|
|---|
| 787 | */
|
|---|
| 788 |
|
|---|
| 789 | /*!
|
|---|
| 790 | \fn QWidget *QToolBox::item(int index) const
|
|---|
| 791 |
|
|---|
| 792 | Use widget() instead.
|
|---|
| 793 | */
|
|---|
| 794 |
|
|---|
| 795 | /*!
|
|---|
| 796 | \fn void QToolBox::setMargin(int margin)
|
|---|
| 797 | Sets the width of the margin around the contents of the widget to \a margin.
|
|---|
| 798 |
|
|---|
| 799 | Use QWidget::setContentsMargins() instead.
|
|---|
| 800 | \sa margin(), QWidget::setContentsMargins()
|
|---|
| 801 | */
|
|---|
| 802 |
|
|---|
| 803 | /*!
|
|---|
| 804 | \fn int QToolBox::margin() const
|
|---|
| 805 | Returns the width of the margin around the contents of the widget.
|
|---|
| 806 |
|
|---|
| 807 | Use QWidget::getContentsMargins() instead.
|
|---|
| 808 | \sa setMargin(), QWidget::getContentsMargins()
|
|---|
| 809 | */
|
|---|
| 810 |
|
|---|
| 811 | /*! \reimp */
|
|---|
| 812 | bool QToolBox::event(QEvent *e)
|
|---|
| 813 | {
|
|---|
| 814 | return QFrame::event(e);
|
|---|
| 815 | }
|
|---|
| 816 |
|
|---|
| 817 | QT_END_NAMESPACE
|
|---|
| 818 |
|
|---|
| 819 | #include "moc_qtoolbox.cpp"
|
|---|
| 820 | #include "qtoolbox.moc"
|
|---|
| 821 |
|
|---|
| 822 | #endif //QT_NO_TOOLBOX
|
|---|