/***************************************************************************
 **
 **  Copyright (C) 2023 wangjianjun <wangjianjun1018@qq.com>
 **  Contact: https://blog.csdn.net/wocanimei007?type=blog
 **
 **
****************************************************************************/
#include "fancyribbonpage.h"
#include "fancyribbongroup.h"
#include <QHBoxLayout>



class FancyRibbonPagePrivate : public QObject
{
    Q_OBJECT
public:
    FancyRibbonPagePrivate();
    ~FancyRibbonPagePrivate();

    void init();

    bool validateIndex(int index) const
    { return index >= 0 && index < m_listGroups.count(); }

    void insertGroup(int index, FancyRibbonGroup *group);
    void removeGroup(int index);
    int groupIndex(FancyRibbonGroup *group);

public:
    FancyRibbonPage *q;
    QHBoxLayout *m_layout;

    QList<FancyRibbonGroup *> m_listGroups;

    QString m_title;
};

FancyRibbonPagePrivate::FancyRibbonPagePrivate()
    : m_title(QString("page"))
{}

FancyRibbonPagePrivate::~FancyRibbonPagePrivate()
{}

void FancyRibbonPagePrivate::init()
{
    m_layout = new QHBoxLayout();
    m_layout->setSpacing(1);
    m_layout->setMargin(0);

    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->setSpacing(0);
    mainLayout->setMargin(0);
    mainLayout->addLayout(m_layout);
    mainLayout->addStretch();

    q->setLayout(mainLayout);
}

void FancyRibbonPagePrivate::insertGroup(int index, FancyRibbonGroup *group)
{
    if (validateIndex(index)) {
        m_listGroups.insert(index, group);
        m_layout->insertWidget(index, group);
    } else {
        m_listGroups.append(group);
        m_layout->addWidget(group);
    }
}

void FancyRibbonPagePrivate::removeGroup(int index)
{
    if (validateIndex(index)) {
        FancyRibbonGroup *group = m_listGroups.at(index);
        m_listGroups.removeAt(index);
        m_layout->removeWidget(group);
    }
}

int FancyRibbonPagePrivate::groupIndex(FancyRibbonGroup *group)
{
    return m_listGroups.indexOf(group);
}

FancyRibbonPage::FancyRibbonPage(QWidget *parent)
    : QWidget(parent)
    , d(new FancyRibbonPagePrivate)
{
    d->q = this;
    d->init();
}

FancyRibbonPage::~FancyRibbonPage()
{
    delete d;
}

void FancyRibbonPage::addGroup(FancyRibbonGroup *group)
{
    insertGroup(-1, group);
}

FancyRibbonGroup *FancyRibbonPage::addGroup(const QString &title)
{
    return insertGroup(-1, title);
}

void FancyRibbonPage::insertGroup(int index, FancyRibbonGroup *group)
{
    d->insertGroup(index, group);
}

FancyRibbonGroup *FancyRibbonPage::insertGroup(int index, const QString &title)
{
    FancyRibbonGroup *group = new FancyRibbonGroup(title);
    insertGroup(index, group);
    return group;
}

void FancyRibbonPage::removeGroup(FancyRibbonGroup *group)
{
    removeGroup(d->groupIndex(group));
}

void FancyRibbonPage::removeGroup(int index)
{
    d->removeGroup(index);
}

void FancyRibbonPage::clearGroups()
{
    for (int i = d->m_listGroups.count() - 1; i >= 0; i--) {
        removeGroup(i);
    }
}

QString FancyRibbonPage::title() const
{
    return d->m_title;
}

void FancyRibbonPage::setBackgroundColor(const QColor &color)
{
    QPalette palette;
    palette.setColor(QPalette::Window, color);
    this->setPalette(palette);
    this->setAutoFillBackground(true);
}

void FancyRibbonPage::setTitle(const QString &title)
{
    d->m_title = title;
}



#include "fancyribbonpage.moc"
