/*
 * Copyright 2010  Omar Plummer <rkoder@gmail.com>
 *           2010  Jonas Emanuel Müller <zanoi@zanoi.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of 
 * the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "informationlistwidget.h"

#include <QGridLayout>
#include <QString>

#include <QLabel>
#include <QScrollArea>
#include <QObject>

struct InformationListWidget::InformationListWidgetPrivate
{
    InformationListWidgetPrivate()
    {
    }
    ~InformationListWidgetPrivate()
    {
    }
    
    QGridLayout* m_gridLayout;
};

InformationListWidget::InformationListWidget(QWidget *parent, Qt::WindowFlags f) :
    QWidget(parent, f), d(new InformationListWidgetPrivate)
{
  d->m_gridLayout = new QGridLayout(this);
}

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

void InformationListWidget::addRow ( QString label, QString value )
{
    QLabel* labelWidget = new QLabel(label, this);
    QLabel* valueWidget = new QLabel(value, this);

    // use a brighter color for the label and a small font size
    QPalette palette = labelWidget->palette();
    const QPalette::ColorRole role = foregroundRole();
    QColor textColor = palette.color(role);
    textColor.setAlpha(128);
    palette.setColor(role, textColor);
    labelWidget->setPalette(palette);
    labelWidget->setForegroundRole(role);
    labelWidget->setFont(font());
    labelWidget->setWordWrap(true);
    labelWidget->setAlignment(Qt::AlignTop | Qt::AlignRight);

    valueWidget->setForegroundRole(role);
    valueWidget->setFont(font());
    valueWidget->setWordWrap(true);
    valueWidget->setAlignment(Qt::AlignTop | Qt::AlignLeft);

    // add the row to grid layout
    const int rowIndex = d->m_gridLayout->rowCount();
    d->m_gridLayout->addWidget(labelWidget, rowIndex, 0, Qt::AlignRight);
    const int spacerWidth = QFontMetrics(font()).size(Qt::TextSingleLine, " ").width();
    d->m_gridLayout->addItem(new QSpacerItem(spacerWidth, 1), rowIndex, 1);
    
    if(value.count() > 800) {
       QScrollArea *scrollArea = new QScrollArea(this);
        scrollArea->setWidgetResizable(true);
        scrollArea->setFrameShape(QFrame::NoFrame);
        scrollArea->setWidget(valueWidget);
        scrollArea->setMinimumHeight(1);
        d->m_gridLayout->addWidget(scrollArea, rowIndex, 2, Qt::AlignLeft);
        d->m_gridLayout->setRowMinimumHeight(0, 1);
    } else {
        d->m_gridLayout->addWidget(valueWidget, rowIndex, 2, Qt::AlignLeft);
    }
}

void InformationListWidget::clear()
{
    QObject* child;
    QList<QObject*> children = this->children();
    foreach(child, children) {
        delete child;
    }
    d->m_gridLayout = new QGridLayout(this);
}
