1
+ #include < QDebug>
1
2
#include < QBoxLayout>
2
3
#include < QHeaderView>
3
4
#include " widgetbox.h"
4
5
5
6
7
+ /* *
8
+ * @class PageButton
9
+ * @brief The PageButton class: page (category) button for widget box
10
+ */
6
11
PageButton::PageButton (const QString &text, QTreeWidget* parent,
7
12
QTreeWidgetItem *item)
8
13
: QPushButton(text, parent)
@@ -26,15 +31,25 @@ void PageButton::setTitle(QString const &title)
26
31
27
32
void PageButton::buttonPressed ()
28
33
{
29
- mItem ->treeWidget ()->setCurrentItem (mItem );
30
34
mItem ->setExpanded (!mItem ->isExpanded ());
35
+ int index = mItem ->treeWidget ()->indexOfTopLevelItem (mItem );
36
+ ((WidgetBox *)mItem ->treeWidget ()->parent ())->setCurrentIndex (index);
37
+
31
38
if (mItem ->isExpanded ()) {
32
39
setIcon (QIcon (" :/plugins/widgetbox/expanded.png" ));
33
40
} else {
34
41
setIcon (QIcon (" :/plugins/widgetbox/collapsed.png" ));
35
42
}
36
43
}
37
44
45
+ /* !
46
+ * \class WidgetBox
47
+ * \brief The WidgetBox class: Widget similar to the Widget Box in the Qt Designer.
48
+ * It contains a list of widgets (pages) separated by categories. Each category
49
+ * button can be clicked in order to expand and collapse the list below the button.
50
+ *
51
+ */
52
+
38
53
WidgetBox::WidgetBox (QWidget *parent) : QWidget(parent)
39
54
{
40
55
// Set default tree widget settings
@@ -56,11 +71,16 @@ WidgetBox::WidgetBox(QWidget *parent) : QWidget(parent)
56
71
QBoxLayout* layout = new QVBoxLayout (this );
57
72
layout->setContentsMargins (0 , 0 , 0 , 0 );
58
73
layout->addWidget (mTreeWidget );
74
+
75
+ connect (mTreeWidget , SIGNAL (itemClicked (QTreeWidgetItem*, int )),
76
+ SLOT (onItemClicked (QTreeWidgetItem*,int )));
77
+ connect (mTreeWidget , SIGNAL (itemActivated (QTreeWidgetItem*, int )),
78
+ SLOT (onItemClicked (QTreeWidgetItem*,int )));
59
79
}
60
80
61
81
QSize WidgetBox::sizeHint () const
62
82
{
63
- return QSize (130 , 210 );
83
+ return QSize (130 , 210 );
64
84
}
65
85
66
86
void WidgetBox::createCategoryButton (QTreeWidgetItem* page, QString pageName)
@@ -118,8 +138,8 @@ void WidgetBox::insertPage(int index, QWidget *widget)
118
138
QString pageName = widget->windowTitle ();
119
139
if (pageName.isEmpty ())
120
140
{
121
- pageName = tr (" Page %1" ).arg (count () + 1 );
122
- widget->setWindowTitle (pageName);
141
+ pageName = tr (" Page %1" ).arg (count () + 1 );
142
+ widget->setWindowTitle (pageName);
123
143
}
124
144
125
145
QTreeWidgetItem* page = insertCategory (index, pageName);
@@ -213,3 +233,24 @@ void WidgetBox::setPageExpanded(bool expanded)
213
233
categoryButton (currentIndex ())->setExpanded (expanded);
214
234
}
215
235
}
236
+
237
+ int WidgetBox::getPageIndex (QTreeWidgetItem *item)
238
+ {
239
+ if (!item) return -1 ;
240
+
241
+ QTreeWidgetItem *parent = item->parent ();
242
+ if (parent) // Parent is top level item
243
+ {
244
+ return mTreeWidget ->indexOfTopLevelItem (parent);
245
+ }
246
+ else // Current item is top level
247
+ {
248
+ return item->treeWidget ()->indexOfTopLevelItem (item);
249
+ }
250
+ }
251
+
252
+ void WidgetBox::onItemClicked (QTreeWidgetItem *item, int )
253
+ {
254
+ int index = getPageIndex (item);
255
+ setCurrentIndex (index);
256
+ }
0 commit comments