Skip to content

Commit 10735b0

Browse files
author
Aleksey Kontsevich
committed
Fixed and improved Widget Box signals/slot handling. Moved Doxygen comments to .cpp file.
1 parent 55a9a5e commit 10735b0

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

Sources/widgetbox.cpp

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
#include <QDebug>
12
#include <QBoxLayout>
23
#include <QHeaderView>
34
#include "widgetbox.h"
45

56

7+
/**
8+
* @class PageButton
9+
* @brief The PageButton class: page (category) button for widget box
10+
*/
611
PageButton::PageButton(const QString &text, QTreeWidget* parent,
712
QTreeWidgetItem *item)
813
: QPushButton(text, parent)
@@ -26,15 +31,25 @@ void PageButton::setTitle(QString const &title)
2631

2732
void PageButton::buttonPressed()
2833
{
29-
mItem->treeWidget()->setCurrentItem(mItem);
3034
mItem->setExpanded(!mItem->isExpanded());
35+
int index = mItem->treeWidget()->indexOfTopLevelItem(mItem);
36+
((WidgetBox *)mItem->treeWidget()->parent())->setCurrentIndex(index);
37+
3138
if(mItem->isExpanded()) {
3239
setIcon(QIcon(":/plugins/widgetbox/expanded.png"));
3340
} else {
3441
setIcon(QIcon(":/plugins/widgetbox/collapsed.png"));
3542
}
3643
}
3744

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+
3853
WidgetBox::WidgetBox(QWidget *parent) : QWidget(parent)
3954
{
4055
// Set default tree widget settings
@@ -56,11 +71,16 @@ WidgetBox::WidgetBox(QWidget *parent) : QWidget(parent)
5671
QBoxLayout* layout = new QVBoxLayout(this);
5772
layout->setContentsMargins(0, 0, 0, 0);
5873
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)));
5979
}
6080

6181
QSize WidgetBox::sizeHint() const
6282
{
63-
return QSize(130, 210);
83+
return QSize(130, 210);
6484
}
6585

6686
void WidgetBox::createCategoryButton(QTreeWidgetItem* page, QString pageName)
@@ -118,8 +138,8 @@ void WidgetBox::insertPage(int index, QWidget *widget)
118138
QString pageName = widget->windowTitle();
119139
if (pageName.isEmpty())
120140
{
121-
pageName = tr("Page %1").arg(count() + 1);
122-
widget->setWindowTitle(pageName);
141+
pageName = tr("Page %1").arg(count() + 1);
142+
widget->setWindowTitle(pageName);
123143
}
124144

125145
QTreeWidgetItem* page = insertCategory(index, pageName);
@@ -213,3 +233,24 @@ void WidgetBox::setPageExpanded(bool expanded)
213233
categoryButton(currentIndex())->setExpanded(expanded);
214234
}
215235
}
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+
}

Sources/widgetbox.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
#include <QPushButton>
66
#include <QTreeWidget>
77

8-
/**
9-
* @brief The PageButton class: page (category) button for widget box
10-
*/
8+
119
class PageButton : public QPushButton
1210
{
1311
Q_OBJECT
@@ -26,11 +24,7 @@ private slots:
2624
QTreeWidgetItem* mItem;
2725
};
2826

29-
/**
30-
* @brief The WidgetBox class: Widget similar to the Widget Box in the Qt Designer.
31-
It contains a list of widgets (pages) separated by categories. Each category
32-
button can be clicked in order to expand and collapse the list below the button.
33-
*/
27+
3428
class WidgetBox : public QWidget
3529
{
3630
Q_OBJECT
@@ -69,13 +63,17 @@ public slots:
6963
void createContainerWidget(QTreeWidgetItem* page, QWidget *widget);
7064
void createCategoryButton(QTreeWidgetItem* page, QString pageName);
7165

66+
protected slots:
67+
void onItemClicked(QTreeWidgetItem *item, int);
68+
7269
signals:
7370
void currentIndexChanged(int index);
7471
void pageTitleChanged(const QString &title);
7572

7673
private:
7774
bool checkIndex(int index) const;
7875
void setupWidget(QWidget *widget);
76+
int getPageIndex(QTreeWidgetItem *item);
7977

8078
QTreeWidget *mTreeWidget;
8179
};

Sources/widgetboxplugin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ QString WidgetBoxPlugin::includeFile() const
112112
{
113113
return QLatin1String("widgetbox.h");
114114
}
115+
115116
void WidgetBoxPlugin::currentIndexChanged(int index)
116117
{
117118
Q_UNUSED(index);

0 commit comments

Comments
 (0)