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