#include "kpagedialog.h"
#include "ui_kpagedialog.h"
#include <QPainter>

const int iconSize=48;

void k9IconItem::paint(QPainter *painter,
                             const QStyleOptionViewItem &option,
                             const QModelIndex &index) const
 {

        QString t=index.data(Qt::DisplayRole).toString();
        QRect r=option.rect;
        QFont font=option.font;
        font.setBold(true);
        painter->setFont(font);
        painter->setPen(option.palette.color(QPalette::Text));
        if (option.state.testFlag(QStyle::State_Selected) ) {
            painter->fillRect(r, option.palette.highlight());
            painter->setPen(option.palette.color(QPalette::HighlightedText));
        }

        QIcon img=index.data(Qt::DecorationRole).value<QIcon>();
        img.paint(painter,r.width()/2-iconSize/2 ,r.top(),iconSize,iconSize);
//        r.setBottom(r.bottom()+option.fontMetrics.height());
        painter->drawText(r,Qt::AlignBottom|Qt::AlignHCenter, t);
 }

KPageWidgetItem::KPageWidgetItem(QObject *parent,QWidget *widget,const QString &label):QObject(parent) {
    KPageDialog *pageDialog=(KPageDialog*)parent;
    m_index=pageDialog->m_ui->listWidget->count();
    QListWidgetItem * item=new QListWidgetItem(label);
    QStyleOptionViewItem opt;
    opt.initFrom(pageDialog->m_ui->listWidget);
    int h=opt.fontMetrics.height()+10+iconSize;
    item->setSizeHint(QSize(h,h));
    pageDialog->m_ui->listWidget->addItem(item);
    pageDialog->m_ui->stackedWidget->addWidget(widget);

}

void KPageWidgetItem::setHeader(const QString &text) {
    m_header=text;
    KPageDialog *pageDialog=(KPageDialog*)parent();
    if (pageDialog->m_ui->listWidget->currentRow() == m_index)
        pageDialog->m_ui->gbLabel->setTitle( pageDialog->m_pages[m_index]->getHeader());
}

void KPageWidgetItem::setIcon(const QIcon  &icon){
    KPageDialog *pageDialog=(KPageDialog*) parent();

    QIcon icon48(icon.pixmap(QSize(48, 48)));
    pageDialog->m_ui->listWidget->item(m_index)->setIcon(icon48);
}

KPageDialog::KPageDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::KPageDialog)
{
    m_ui->setupUi(this);

    m_ui->listWidget->clear();
    k9IconItem *delegate=new k9IconItem(this);
    m_ui->listWidget->setItemDelegate(delegate);

}

KPageDialog::~KPageDialog()
{
    delete m_ui;
}

void KPageDialog::accept() {
    emit okClicked();
    QDialog::accept() ;
}
void KPageDialog::clicked(QAbstractButton* b) {
    if (m_ui->buttonBox->buttonRole(b)==QDialogButtonBox::ApplyRole)
        emit  applyClicked();
}

void KPageDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

KPageWidgetItem *KPageDialog::addPage(QWidget *widget, const QString &label) {
    KPageWidgetItem *page=new KPageWidgetItem(this,widget,label);
    m_pages << page;
    if (m_pages.count()==1)
        m_ui->listWidget->setCurrentRow(0);
    return page;
}

void KPageDialog::itemChanged(int row) {
    if (row < m_pages.count() && row >=0) {
        m_ui->stackedWidget->setCurrentIndex(row);
        m_ui->gbLabel->setTitle( m_pages[row]->getHeader());
    }
}
