#include "ModalDimOverlay.h"

#include <QPainter>

ModalDimOverlay::ModalDimOverlay(const QColor& dimColor, QWidget* parent)
    : QWidget(parent)
    , m_dimColor(dimColor)
{
    setAttribute(Qt::WA_TransparentForMouseEvents, true);
    hide();
}

void ModalDimOverlay::pushModal()
{
    if (m_modalDepth++ == 0)
    {
        raise();
        show();
        // Force an immediate synchronous paint so the scrim is visible before the
        // caller enters a blocking dialog exec() (no undimmed frame flashes through).
        repaint();
    }
}

void ModalDimOverlay::popModal()
{
    if (m_modalDepth > 0 && --m_modalDepth == 0)
    {
        hide();
    }
}

void ModalDimOverlay::setDimColor(const QColor& dimColor)
{
    m_dimColor = dimColor;
    if (isVisible())
    {
        update();
    }
}

void ModalDimOverlay::paintEvent(QPaintEvent* /*event*/)
{
    QPainter painter(this);
    painter.fillRect(rect(), m_dimColor);
}
