HAL
expanding_list_widget.cpp
Go to the documentation of this file.
2 
5 
6 #include <QFrame>
7 #include <QStyle>
8 #include <QVBoxLayout>
9 
10 namespace hal
11 {
12 
14  : QScrollArea(parent), mContent(new QFrame()), mContentLayout(new QVBoxLayout()),
15  mSpacer(new QFrame()), mSelectedButton(nullptr), mOffset(0)
16  {
18  setWidget(mContent);
19  setWidgetResizable(true);
20  mContent->setObjectName("content");
21  mContent->setFrameStyle(QFrame::NoFrame);
22  mContent->setLayout(mContentLayout);
23  mContentLayout->setAlignment(Qt::AlignTop);
24  mContentLayout->setContentsMargins(0, 0, 0, 0);
25  mContentLayout->setSpacing(0);
26  mSpacer->setObjectName("spacer");
28  mContentLayout->addWidget(mSpacer);
29  }
30 
32  {
33  ExpandingListItem* item = new ExpandingListItem(button);
34  QString tag = button->text();
35  if (!groupName.isEmpty()) tag.prepend(groupName + ":");
36  auto itInsert = mItemMap.insert(tag,item);
37  int inx = 0;
38  for (auto it=mItemMap.begin(); it!=itInsert; ++it) ++inx;
39  mContentLayout->insertWidget(inx,item);
40 
41  if (!groupName.isEmpty())
42  mButtonGroup[groupName].append(item);
44  }
45 
47  {
48  if (!mSelectedButton) return;
49  mSelectedButton->setSelected(false);
50  mSelectedButton = nullptr;
51  }
52 
54  {
55  if (button == mSelectedButton)
56  return;
57 
58  if (!button)
59  {
60  if (mSelectedButton) clearSelected();
61  return;
62  }
63 
64  mSelectedButton = button;
65  for (auto it = mItemMap.begin(); it!=mItemMap.end(); ++it)
66  {
67  ExpandingListItem* item = it.value();
68  ExpandingListButton* but = item->button();
69  but->setSelected(but == mSelectedButton);
70  }
71 
72  Q_EMIT buttonSelected(button);
73  }
74 
75 
77  {
78  if (mItemMap.isEmpty()) return;
79 
80  if (mSelectedButton)
81  {
82  selectButton(mSelectedButton);
83  return;
84  }
85 
86  ExpandingListItem* first = mItemMap.value("Appearance:Style");
87  if (!first) first = mItemMap.begin().value();
88  selectButton(first->button());
89  }
90 
92  {
93  QStyle* s = style();
94 
95  s->unpolish(this);
96  s->polish(this);
97 
98  for (auto it = mItemMap.begin(); it!=mItemMap.end(); ++it)
99  it.value()->repolish();
100  }
101 
103  {
104  QObject* obj = sender();
105  ExpandingListButton* button = static_cast<ExpandingListButton*>(obj);
106 
107  auto grpIt = mButtonGroup.find(button->text());
108  if (grpIt != mButtonGroup.end())
109  {
110  grpIt->toggleCollapsed(mSelectedButton); // except selected
111  return;
112  }
113 
114  selectButton(button);
115  }
116 
117  bool ExpandingListWidget::hasGroup(const QString& groupName) const
118  {
119  return mButtonGroup.contains(groupName);
120  }
121 
123  {
125  for (ExpandingListItem* item : *this)
126  {
127  if (mCollapsed)
128  {
129  if (item->button() != exceptSelected)
130  item->collapse();
131  }
132  else
133  item->expand();
134  }
135  }
136 }
One button of the ExpandingListWidget.
void toggleCollapsed(ExpandingListButton *exceptSelected)
Wrapper for ExpandingListButtons.
ExpandingListButton * button() const
void selectButton(ExpandingListButton *button)
void appendItem(ExpandingListButton *button, const QString &groupName=QString())
void buttonSelected(ExpandingListButton *button)
bool hasGroup(const QString &groupName) const
ExpandingListWidget(QWidget *parent=nullptr)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void insertWidget(int index, QWidget *widget, int stretch, Qt::Alignment alignment)
void setSpacing(int spacing)
void setFrameStyle(int style)
bool setAlignment(QWidget *w, Qt::Alignment alignment)
void setContentsMargins(int left, int top, int right, int bottom)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setObjectName(const QString &name)
QObject * sender() const const
void setWidget(QWidget *widget)
void setWidgetResizable(bool resizable)
bool isEmpty() const const
QString & prepend(QChar ch)
virtual void polish(QWidget *widget)
virtual void unpolish(QWidget *widget)
AlignTop
void setLayout(QLayout *layout)
QStyle * style() const const