File: collapsing_widget.cpp

package info (click to toggle)
cataclysm-dda 0.H-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 710,808 kB
  • sloc: cpp: 524,019; python: 11,580; sh: 1,228; makefile: 1,169; xml: 507; javascript: 150; sql: 56; exp: 41; perl: 37
file content (57 lines) | stat: -rw-r--r-- 2,219 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
#include "collapsing_widget.h"
#include <QtWidgets/qlabel.h>
#include <QtWidgets/qtoolbutton.h>

creator::collapsing_widget::collapsing_widget(QWidget *parent, const QString &text, QLayout& contentLayout) 
    : QWidget(parent)
{
    //Create a vertcal layout
    QVBoxLayout *layout = new QVBoxLayout(this);

    //Add a button and a label to a horizontal layout and add it to the vertical layout
    QHBoxLayout *hlayout = new QHBoxLayout();
    layout->addLayout(hlayout);
    QToolButton *button = new QToolButton();
    button->setCheckable(true);
    button->setArrowType(Qt::ArrowType::DownArrow);
    //make sure the button starts checked
    button->setChecked(true);
    hlayout->addWidget(button);
    QLabel *label = new QLabel();
    label->setText(text);
    hlayout->addWidget(label);

    //Add the content layout to a new scrollarea and add it to the vertical layout
    QScrollArea *scrollArea = new QScrollArea();
    scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    scrollArea->setWidgetResizable(true);
    scrollArea->setLayout(&contentLayout);
    
    contentLayout.setContentsMargins(0, 0, 0, 0);
    contentLayout.setSpacing(0);
    scrollArea->setMaximumHeight(scrollArea->layout()->sizeHint().height());
    scrollArea->setMinimumHeight(scrollArea->layout()->sizeHint().height());
    layout->addWidget(scrollArea);

    //When the button is pressed, toggle the content
    connect(button, &QToolButton::toggled, [=](){
        button->setArrowType(checked ? Qt::ArrowType::DownArrow : Qt::ArrowType::RightArrow);
        scrollArea != nullptr && checked ? showContent(scrollArea) : hideContent(scrollArea);
    });
}

void creator::collapsing_widget::hideContent(QScrollArea* scrollArea) {
    scrollArea->setMaximumHeight(0);
    scrollArea->setMinimumHeight(0);
    this->adjustSize();
    this->parentWidget()->adjustSize();
    checked = true;
}

void creator::collapsing_widget::showContent(QScrollArea* scrollArea) {
    scrollArea->setMaximumHeight(scrollArea->layout()->sizeHint().height());
    scrollArea->setMinimumHeight(scrollArea->layout()->sizeHint().height());
    this->adjustSize();
    this->parentWidget()->adjustSize();
    checked = false;
}