aboutsummaryrefslogtreecommitdiff
path: root/libs/uibase/src/expanderwidget.cpp
blob: 451ab070a518094ee25d6a86f0223273436fc672 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <uibase/expanderwidget.h>
#include <QIODevice>

namespace MOBase
{

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 != opened_) {
    emit aboutToToggle(b);
  }

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

  if (b != opened_) {
    // 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;

    emit toggled(b);
  }
}

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

QByteArray ExpanderWidget::saveState() const
{
  QByteArray result;
  QDataStream stream(&result, QIODevice::WriteOnly);

  stream << opened();

  return result;
}

void ExpanderWidget::restoreState(const QByteArray& a)
{
  QDataStream stream(a);

  bool opened = false;
  stream >> opened;

  if (stream.status() == QDataStream::Ok) {
    toggle(opened);
  }
}

QToolButton* ExpanderWidget::button() const
{
  return m_button;
}

}  // namespace MOBase