File: CollapsibleSection.cxx

package info (click to toggle)
gr-framework 0.73.22%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,476 kB
  • sloc: ansic: 87,950; cpp: 58,388; objc: 3,057; javascript: 2,647; python: 1,000; yacc: 855; pascal: 554; sh: 281; fortran: 228; makefile: 174
file content (71 lines) | stat: -rw-r--r-- 3,214 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <QPropertyAnimation>
#include "CollapsibleSection.hxx"

/*
 * Code inspired from Stackoverflow user https://stackoverflow.com/users/887074/erotemic
 * Full code under:
 * https://stackoverflow.com/questions/32476006/how-to-make-an-expandable-collapsable-section-widget-in-qt/37119983#37119983
 */

CollapsibleSection::CollapsibleSection(const QString &title, const int animation_duration, QWidget *parent)
    : QWidget(parent), animation_duration(animation_duration)
{
  int row = 0;

  toggle_button.setStyleSheet("QToolButton { border: none; }");
  toggle_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  toggle_button.setArrowType(Qt::ArrowType::RightArrow);
  toggle_button.setText(title);
  toggle_button.setCheckable(true);
  toggle_button.setChecked(false);

  header_line.setFrameShape(QFrame::HLine);
  header_line.setFrameShadow(QFrame::Sunken);
  header_line.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);

  content_area.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
  // so all labels are displayed completely
  // start out collapsed
  content_area.setMaximumHeight(0);
  content_area.setMinimumHeight(0);
  // let the entire widget grow and shrink with its content
  toggle_animation.addAnimation(new QPropertyAnimation(this, "minimumHeight"));
  toggle_animation.addAnimation(new QPropertyAnimation(this, "maximumHeight"));
  toggle_animation.addAnimation(new QPropertyAnimation(&content_area, "maximumHeight"));
  // don't waste space
  main_layout.setVerticalSpacing(0);
  main_layout.setContentsMargins(0, 0, 0, 0);

  main_layout.addWidget(&toggle_button, row, 0, 1, 1, Qt::AlignLeft);
  main_layout.addWidget(&header_line, row++, 2, 1, 1);
  main_layout.addWidget(&content_area, row, 0, 1, 3);
  setLayout(&main_layout);
  QObject::connect(&toggle_button, &QToolButton::clicked, [this](const bool checked) {
    toggle_button.setArrowType(checked ? Qt::ArrowType::DownArrow : Qt::ArrowType::RightArrow);
    toggle_animation.setDirection(checked ? QAbstractAnimation::Forward : QAbstractAnimation::Backward);
    toggle_animation.start();
  });
}

CollapsibleSection::~CollapsibleSection() = default;

void CollapsibleSection::setContentLayout(QLayout &content_layout, bool clicked)
{
  delete content_area.layout();
  content_area.setLayout(&content_layout);
  const auto collapsed_height = sizeHint().height() - content_area.maximumHeight();
  auto content_height = content_layout.sizeHint().height();
  for (int i = 0; i < toggle_animation.animationCount() - 1; ++i)
    {
      auto collapsible_section_animation = static_cast<QPropertyAnimation *>(toggle_animation.animationAt(i));
      collapsible_section_animation->setDuration(animation_duration);
      collapsible_section_animation->setStartValue(collapsed_height);
      collapsible_section_animation->setEndValue(collapsed_height + content_height);
    }
  auto content_animation =
      static_cast<QPropertyAnimation *>(toggle_animation.animationAt(toggle_animation.animationCount() - 1));
  content_animation->setDuration(animation_duration);
  content_animation->setStartValue(0);
  content_animation->setEndValue(content_height);
  if (clicked) toggle_button.clicked(true);
}