#include "SectionBox.h"

#include <QFont>
#include <QLabel>
#include <QPalette>
#include <QVBoxLayout>

namespace
{

// Point-size drop of the caption relative to the card's text, so it reads as a heading
// without competing with the values beneath it.
const int kCaptionSizeDropPt = 1;

}  // namespace


SectionBox::SectionBox(const QString& caption, QWidget* parent)
    : QWidget(parent)
{
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(2);

    m_captionLabel = new QLabel(caption.toUpper(), this);
    QFont captionFont = m_captionLabel->font();
    captionFont.setPointSize(qMax(1, captionFont.pointSize() - kCaptionSizeDropPt));
    captionFont.setBold(true);
    m_captionLabel->setFont(captionFont);

    // Dimmed to the palette's disabled text, so the caption sits behind its content.
    QPalette captionPalette = m_captionLabel->palette();
    captionPalette.setColor(QPalette::WindowText,
                            palette().color(QPalette::Disabled, QPalette::WindowText));
    m_captionLabel->setPalette(captionPalette);
    m_captionLabel->setVisible(!caption.isEmpty());

    QWidget* content = new QWidget(this);
    m_contentLayout = new QVBoxLayout(content);
    m_contentLayout->setContentsMargins(0, 0, 0, 0);
    m_contentLayout->setSpacing(2);

    layout->addWidget(m_captionLabel);
    layout->addWidget(content);
}

QVBoxLayout* SectionBox::getContentLayout()
{
    return m_contentLayout;
}
