summaryrefslogtreecommitdiff
path: root/src/expanderwidget.cpp
blob: 2f47da5ba53d70557a61b57c393ff2c013866bc4 (plain)
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
#include "expanderwidget.h"

ExpanderWidget::ExpanderWidget()
  : m_button(nullptr), m_content(nullptr), opened_(false)
{
}

ExpanderWidget::ExpanderWidget(QToolButton* button, QWidget* content)
  : ExpanderWidget()
{
  set(button, content);
}

void ExpanderWidget::set(QToolButton* button, QWidget* content, bool o)
{
  m_button = button;
  m_content = content;

  m_button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  QObject::connect(m_button, &QToolButton::clicked, [&]{ toggle(); });

  toggle(o);
}

void ExpanderWidget::toggle()
{
  if (opened()) {
    toggle(false);
  }
  else {
    toggle(true);
  }
}

void ExpanderWidget::toggle(bool b)
{
  if (b) {
    m_button->setArrowType(Qt::DownArrow);
    m_content->show();
  } else {
    m_button->setArrowType(Qt::RightArrow);
    m_content->hide();
  }

  // the state has to be remembered instead of using m_content's visibility
  // because saving the state in saveConflictExpandersState() happens after the
  // dialog is closed, which marks all the widgets hidden
  opened_ = b;
}

bool ExpanderWidget::opened() const
{
  return opened_;
}