qtoolbox.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qtoolbox.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5-
6-
7-
8class QToolBoxButton : public QAbstractButton-
9{-
10 public: template <typename ThisObject> inline void qt_check_for_QOBJECT_macro(const ThisObject &_q_argument) const { int i = qYouForgotTheQ_OBJECT_Macro(this, &_q_argument); i = i + 1; }-
11#pragma GCC diagnostic push-
12 static const QMetaObject staticMetaObject; virtual const QMetaObject *metaObject() const; virtual void *qt_metacast(const char *); virtual int qt_metacall(QMetaObject::Call, int, void **); static inline QString tr(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } __attribute__ ((__deprecated__)) static inline QString trUtf8(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } private: __attribute__((visibility("hidden"))) static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **);-
13#pragma GCC diagnostic pop-
14 struct QPrivateSignal {};-
15public:-
16 QToolBoxButton(QWidget *parent)-
17 : QAbstractButton(parent), selected(false), indexInPage(-1)-
18 {-
19 setBackgroundRole(QPalette::Window);-
20 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);-
21 setFocusPolicy(Qt::NoFocus);-
22 }-
23-
24 inline void setSelected(bool b) { selected = b; update(); }-
25 inline void setIndex(int newIndex) { indexInPage = newIndex; }-
26-
27 QSize sizeHint() const override;-
28 QSize minimumSizeHint() const override;-
29-
30protected:-
31 void initStyleOption(QStyleOptionToolBox *opt) const;-
32 void paintEvent(QPaintEvent *) override;-
33-
34private:-
35 bool selected;-
36 int indexInPage;-
37};-
38-
39-
40class QToolBoxPrivate : public QFramePrivate-
41{-
42 inline QToolBox* q_func() { return static_cast<QToolBox *>(q_ptr); } inline const QToolBox* q_func() const { return static_cast<const QToolBox *>(q_ptr); } friend class QToolBox;-
43public:-
44 struct Page-
45 {-
46 QToolBoxButton *button;-
47 QScrollArea *sv;-
48 QWidget *widget;-
49-
50 inline void setText(const QString &text) { button->setText(text); }-
51 inline void setIcon(const QIcon &is) { button->setIcon(is); }-
52-
53 inline void setToolTip(const QString &tip) { button->setToolTip(tip); }-
54 inline QString toolTip() const { return button->toolTip(); }-
55-
56 inline QString text() const { return button->text(); }-
57 inline QIcon icon() const { return button->icon(); }-
58-
59 inline bool operator==(const Page& other) const-
60 {-
61 return widget == other.widget;-
62 }-
63 };-
64 typedef QList<Page> PageList;-
65-
66 inline QToolBoxPrivate()-
67 : currentPage(0)-
68 {-
69 }-
70 void _q_buttonClicked();-
71 void _q_widgetDestroyed(QObject*);-
72-
73 const Page *page(const QObjectQWidget *widget) const;-
74 const Page *page(int index) const;-
75 Page *page(int index);-
76-
77 void updateTabs();-
78 void relayout();-
79-
80 PageList pageList;-
81 QVBoxLayout *layout;-
82 Page *currentPage;-
83};-
84-
85const QToolBoxPrivate::Page *QToolBoxPrivate::page(const QObjectQWidget *widget) const-
86{-
87 if (!widget
!widgetDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
88 return
never executed: return 0;
0;
never executed: return 0;
0
89-
90 for (PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd()
i != pageList.constEnd()Description
TRUEnever evaluated
FALSEnever evaluated
; ++i)
0
91 if ((*
(*i).widget == widgetDescription
TRUEnever evaluated
FALSEnever evaluated
i).widget == widget
(*i).widget == widgetDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
92 return
never executed: return (const Page*) &(*i);
(const Page*) &(*i);
never executed: return (const Page*) &(*i);
0
93 return
never executed: return 0;
0;
never executed: return 0;
0
94}-
95-
96QToolBoxPrivate::Page *QToolBoxPrivate::page(int index)-
97{-
98 if (index >= 0 && index < pageList.size())-
99 return &pageList[index];-
100 return 0;-
101}-
102-
103const QToolBoxPrivate::Page *QToolBoxPrivate::page(int index) const-
104{-
105 if (index >= 0 && index < pageList.size())-
106 return &pageList.at(index);-
107 return 0;-
108}-
109-
110void QToolBoxPrivate::updateTabs()-
111{-
112 QToolBoxButton *lastButton = currentPage ? currentPage->button : 0;-
113 bool after = false;-
114 int index = 0;-
115 for (index = 0; index < pageList.count(); ++index) {-
116 const Page &page = pageList.at(index);-
117 QToolBoxButton *tB = page.button;-
118-
119-
120 tB->setIndex(index);-
121 QWidget *tW = page.widget;-
122 if (after) {-
123 QPalette p = tB->palette();-
124 p.setColor(tB->backgroundRole(), tW->palette().color(tW->backgroundRole()));-
125 tB->setPalette(p);-
126 tB->update();-
127 } else if (tB->backgroundRole() != QPalette::Window) {-
128 tB->setBackgroundRole(QPalette::Window);-
129 tB->update();-
130 }-
131 after = tB == lastButton;-
132 }-
133}-
134-
135QSize QToolBoxButton::sizeHint() const-
136{-
137 QSize iconSize(8, 8);-
138 if (!icon().isNull()) {-
139 int icone = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, parentWidget() );-
140 iconSize += QSize(icone + 2, icone);-
141 }-
142 QSize textSize = fontMetrics().size(Qt::TextShowMnemonic, text()) + QSize(0, 8);-
143-
144 QSize total(iconSize.width() + textSize.width(), qMax(iconSize.height(), textSize.height()));-
145 return total.expandedTo(QApplication::globalStrut());-
146}-
147-
148QSize QToolBoxButton::minimumSizeHint() const-
149{-
150 if (icon().isNull())-
151 return QSize();-
152 int icone = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, parentWidget() );-
153 return QSize(icone + 8, icone + 8);-
154}-
155-
156void QToolBoxButton::initStyleOption(QStyleOptionToolBox *option) const-
157{-
158 if (!option)-
159 return;-
160 option->initFrom(this);-
161 if (selected)-
162 option->state |= QStyle::State_Selected;-
163 if (isDown())-
164 option->state |= QStyle::State_Sunken;-
165 option->text = text();-
166 option->icon = icon();-
167-
168 QToolBox *toolBox = static_cast<QToolBox *>(parentWidget());-
169 const int widgetCount = toolBox->count();-
170 const int currIndex = toolBox->currentIndex();-
171 if (widgetCount == 1) {-
172 option->position = QStyleOptionToolBox::OnlyOneTab;-
173 } else if (indexInPage == 0) {-
174 option->position = QStyleOptionToolBox::Beginning;-
175 } else if (indexInPage == widgetCount - 1) {-
176 option->position = QStyleOptionToolBox::End;-
177 } else {-
178 option->position = QStyleOptionToolBox::Middle;-
179 }-
180 if (currIndex == indexInPage - 1) {-
181 option->selectedPosition = QStyleOptionToolBox::PreviousIsSelected;-
182 } else if (currIndex == indexInPage + 1) {-
183 option->selectedPosition = QStyleOptionToolBox::NextIsSelected;-
184 } else {-
185 option->selectedPosition = QStyleOptionToolBox::NotAdjacent;-
186 }-
187}-
188-
189void QToolBoxButton::paintEvent(QPaintEvent *)-
190{-
191 QPainter paint(this);-
192 QPainter *p = &paint;-
193 QStyleOptionToolBox opt;-
194 initStyleOption(&opt);-
195 style()->drawControl(QStyle::CE_ToolBoxTab, &opt, p, parentWidget());-
196}-
197QToolBox::QToolBox(QWidget *parent, Qt::WindowFlags f)-
198 : QFrame(*new QToolBoxPrivate, parent, f)-
199{-
200 QToolBoxPrivate * const d = d_func();-
201 d->layout = new QVBoxLayout(this);-
202 d->layout->setMargin(0);-
203 setBackgroundRole(QPalette::Button);-
204}-
205-
206-
207-
208-
209-
210QToolBox::~QToolBox()-
211{-
212}-
213int QToolBox::insertItem(int index, QWidget *widget, const QIcon &icon, const QString &text)-
214{-
215 if (!widget)-
216 return -1;-
217-
218 QToolBoxPrivate * const d = d_func();-
219 connect(widget, qFlagLocation("2""destroyed(QObject*)" "\0" __FILE__ ":" "342""348"), this, qFlagLocation("1""_q_widgetDestroyed(QObject*)" "\0" __FILE__ ":" "342""348"));-
220-
221 QToolBoxPrivate::Page c;-
222 c.widget = widget;-
223 c.button = new QToolBoxButton(this);-
224 c.button->setObjectName(QLatin1String("qt_toolbox_toolboxbutton"));-
225 connect(c.button, qFlagLocation("2""clicked()" "\0" __FILE__ ":" "348""354"), this, qFlagLocation("1""_q_buttonClicked()" "\0" __FILE__ ":" "348""354"));-
226-
227 c.sv = new QScrollArea(this);-
228 c.sv->setWidget(widget);-
229 c.sv->setWidgetResizable(true);-
230 c.sv->hide();-
231 c.sv->setFrameStyle(QFrame::NoFrame);-
232-
233 c.setText(text);-
234 c.setIcon(icon);-
235-
236 if (index < 0 || index >= (int)d->pageList.count()) {-
237 index = d->pageList.count();-
238 d->pageList.append(c);-
239 d->layout->addWidget(c.button);-
240 d->layout->addWidget(c.sv);-
241 if (index == 0)-
242 setCurrentIndex(index);-
243 } else {-
244 d->pageList.insert(index, c);-
245 d->relayout();-
246 if (d->currentPage) {-
247 QWidget *current = d->currentPage->widget;-
248 int oldindex = indexOf(current);-
249 if (index <= oldindex) {-
250 d->currentPage = 0;-
251 setCurrentIndex(oldindex);-
252 }-
253 }-
254 }-
255-
256 c.button->show();-
257-
258 d->updateTabs();-
259 itemInserted(index);-
260 return index;-
261}-
262-
263void QToolBoxPrivate::_q_buttonClicked()-
264{-
265 QToolBox * const q = q_func();-
266 QToolBoxButton *tb = qobject_cast<QToolBoxButton*>(q->sender());-
267 QWidget* item = 0;-
268 for (QToolBoxPrivate::PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd(); ++i)-
269 if ((*i).button == tb) {-
270 item = (*i).widget;-
271 break;-
272 }-
273-
274 q->setCurrentIndex(q->indexOf(item));-
275}-
276int QToolBox::count() const-
277{-
278 const QToolBoxPrivate * const d = d_func();-
279 return d->pageList.count();-
280}-
281-
282void QToolBox::setCurrentIndex(int index)-
283{-
284 QToolBoxPrivate * const d = d_func();-
285 QToolBoxPrivate::Page *c = d->page(index);-
286 if (!c || d->currentPage == c)-
287 return;-
288-
289 c->button->setSelected(true);-
290 if (d->currentPage) {-
291 d->currentPage->sv->hide();-
292 d->currentPage->button->setSelected(false);-
293 }-
294 d->currentPage = c;-
295 d->currentPage->sv->show();-
296 d->updateTabs();-
297 currentChanged(index);-
298}-
299-
300void QToolBoxPrivate::relayout()-
301{-
302 QToolBox * const q = q_func();-
303 delete layout;-
304 layout = new QVBoxLayout(q);-
305 layout->setMargin(0);-
306 for (QToolBoxPrivate::PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd(); ++i) {-
307 layout->addWidget((*i).button);-
308 layout->addWidget((*i).sv);-
309 }-
310}-
311-
312void QToolBoxPrivate::_q_widgetDestroyed(QObject *object)-
313{-
314 QToolBox * const q = q_func();-
315-
316 QWidget *p = (QWidget*)object;-
317-
318 const QToolBoxPrivate::Page *constc = page(objectp);-
319 if (!p
!pDescription
TRUEnever evaluated
FALSEnever evaluated
|| !
!cDescription
TRUEnever evaluated
FALSEnever evaluated
c
!cDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
320 return;
never executed: return;
0
321-
322 layout->removeWidget(c->sv);-
323 layout->removeWidget(c->button);-
324 c->sv->deleteLater();-
325 delete c->button;-
326-
327 bool removeCurrent = c == currentPage;-
328 pageList.removeAll(*c);-
329-
330 if (!pageList.count()
!pageList.count()Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
331 currentPage = 0;-
332 q->currentChanged(-1);-
333 }
never executed: end of block
else if (removeCurrent
removeCurrentDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
334 currentPage = 0;-
335 q->setCurrentIndex(0);-
336 }
never executed: end of block
0
337}
never executed: end of block
0
338-
339-
340-
341-
342-
343-
344void QToolBox::removeItem(int index)-
345{-
346 QToolBoxPrivate * const d = d_func();-
347 if (QWidget *w = widget(index)) {-
348 disconnect(w, qFlagLocation("2""destroyed(QObject*)" "\0" __FILE__ ":" "477""485"), this, qFlagLocation("1""_q_widgetDestroyed(QObject*)" "\0" __FILE__ ":" "477""485"));-
349 w->setParent(this);-
350-
351 d->_q_widgetDestroyed(w);-
352 itemRemoved(index);-
353 }-
354}-
355int QToolBox::currentIndex() const-
356{-
357 const QToolBoxPrivate * const d = d_func();-
358 return d->currentPage ? indexOf(d->currentPage->widget) : -1;-
359}-
360-
361-
362-
363-
364-
365-
366-
367QWidget * QToolBox::currentWidget() const-
368{-
369 const QToolBoxPrivate * const d = d_func();-
370 return d->currentPage ? d->currentPage->widget : 0;-
371}-
372-
373-
374-
375-
376-
377-
378void QToolBox::setCurrentWidget(QWidget *widget)-
379{-
380 int i = indexOf(widget);-
381 if (__builtin_expect(!!(
__builtin_expe...i < 0), false)Description
TRUEnever evaluated
FALSEnever evaluated
i >=< 0)
__builtin_expe...i < 0), false)Description
TRUEnever evaluated
FALSEnever evaluated
0
setCurrentIndex(i);
__builtin_expe...i < 0), false)Description
TRUEnever evaluated
FALSEnever evaluated
else
__builtin_expe...i < 0), false)Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_expe...i < 0), false)Description
TRUEnever evaluated
FALSEnever evaluated
), false)
__builtin_expe...i < 0), false)Description
TRUEnever evaluated
FALSEnever evaluated
)
382 QMessageLogger(__FILE__, 525531, __PRETTY_FUNCTION__).warning("QToolBox::setCurrentWidget: widget not contained in tool box");
never executed: QMessageLogger(__FILE__, 531, __PRETTY_FUNCTION__).warning("QToolBox::setCurrentWidget: widget not contained in tool box");
0
383 else-
384 setCurrentIndex(i
never executed: setCurrentIndex(i);
never executed: setCurrentIndex(i);
);
never executed: setCurrentIndex(i);
0
385}-
386-
387-
388-
389-
390-
391-
392QWidget *QToolBox::widget(int index) const-
393{-
394 const QToolBoxPrivate * const d = d_func();-
395 if (index < 0 || index >= (int) d->pageList.size())-
396 return 0;-
397 return d->pageList.at(index).widget;-
398}-
399-
400-
401-
402-
403-
404-
405int QToolBox::indexOf(QWidget *widget) const-
406{-
407 const QToolBoxPrivate * const d = d_func();-
408 const QToolBoxPrivate::Page *c = (widget ? d->page(widget) : 0);-
409 return c ? d->pageList.indexOf(*c) : -1;-
410}-
411-
412-
413-
414-
415-
416-
417void QToolBox::setItemEnabled(int index, bool enabled)-
418{-
419 QToolBoxPrivate * const d = d_func();-
420 QToolBoxPrivate::Page *c = d->page(index);-
421 if (!c)-
422 return;-
423-
424 c->button->setEnabled(enabled);-
425 if (!enabled && c == d->currentPage) {-
426 int curIndexUp = index;-
427 int curIndexDown = curIndexUp;-
428 const int count = d->pageList.count();-
429 while (curIndexUp > 0 || curIndexDown < count-1) {-
430 if (curIndexDown < count-1) {-
431 if (d->page(++curIndexDown)->button->isEnabled()) {-
432 index = curIndexDown;-
433 break;-
434 }-
435 }-
436 if (curIndexUp > 0) {-
437 if (d->page(--curIndexUp)->button->isEnabled()) {-
438 index = curIndexUp;-
439 break;-
440 }-
441 }-
442 }-
443 setCurrentIndex(index);-
444 }-
445}-
446void QToolBox::setItemText(int index, const QString &text)-
447{-
448 QToolBoxPrivate * const d = d_func();-
449 QToolBoxPrivate::Page *c = d->page(index);-
450 if (c)-
451 c->setText(text);-
452}-
453-
454-
455-
456-
457-
458void QToolBox::setItemIcon(int index, const QIcon &icon)-
459{-
460 QToolBoxPrivate * const d = d_func();-
461 QToolBoxPrivate::Page *c = d->page(index);-
462 if (c)-
463 c->setIcon(icon);-
464}-
465-
466-
467-
468-
469-
470-
471void QToolBox::setItemToolTip(int index, const QString &toolTip)-
472{-
473 QToolBoxPrivate * const d = d_func();-
474 QToolBoxPrivate::Page *c = d->page(index);-
475 if (c)-
476 c->setToolTip(toolTip);-
477}-
478-
479-
480-
481-
482-
483-
484bool QToolBox::isItemEnabled(int index) const-
485{-
486 const QToolBoxPrivate * const d = d_func();-
487 const QToolBoxPrivate::Page *c = d->page(index);-
488 return c && c->button->isEnabled();-
489}-
490-
491-
492-
493-
494-
495-
496QString QToolBox::itemText(int index) const-
497{-
498 const QToolBoxPrivate * const d = d_func();-
499 const QToolBoxPrivate::Page *c = d->page(index);-
500 return (c ? c->text() : QString());-
501}-
502-
503-
504-
505-
506-
507-
508QIcon QToolBox::itemIcon(int index) const-
509{-
510 const QToolBoxPrivate * const d = d_func();-
511 const QToolBoxPrivate::Page *c = d->page(index);-
512 return (c ? c->icon() : QIcon());-
513}-
514-
515-
516-
517-
518-
519-
520-
521QString QToolBox::itemToolTip(int index) const-
522{-
523 const QToolBoxPrivate * const d = d_func();-
524 const QToolBoxPrivate::Page *c = d->page(index);-
525 return (c ? c->toolTip() : QString());-
526}-
527-
528-
529-
530void QToolBox::showEvent(QShowEvent *e)-
531{-
532 QWidget::showEvent(e);-
533}-
534-
535-
536void QToolBox::changeEvent(QEvent *ev)-
537{-
538 QToolBoxPrivate * const d = d_func();-
539 if(ev->type() == QEvent::StyleChange)-
540 d->updateTabs();-
541 QFrame::changeEvent(ev);-
542}-
543-
544-
545-
546-
547-
548-
549-
550void QToolBox::itemInserted(int index)-
551{-
552 (void)index;-
553}-
554-
555-
556-
557-
558-
559-
560-
561void QToolBox::itemRemoved(int index)-
562{-
563 (void)index;-
564}-
565-
566-
567bool QToolBox::event(QEvent *e)-
568{-
569 return QFrame::event(e);-
570}-
571-
572-
573-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9