/***************************************************************************
 *   Copyright (C) 2006 by Peter Penz                                      *
 *   peter.penz@gmx.at                                                     *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "iconsviewsettingspage.h"

#include <tqlabel.h>
#include <tqslider.h>
#include <tqbuttongroup.h>
#include <tqradiobutton.h>
#include <tqspinbox.h>
#include <kiconloader.h>
#include <tdefontcombo.h>
#include <kdialog.h>
#include <tdelocale.h>
#include <assert.h>

#include "dolphiniconsviewsettings.h"
#include "dolphinsettings.h"
#include "pixmapviewer.h"

#define GRID_SPACING_BASE 8
#define GRID_SPACING_INC 12

IconsViewSettingsPage::IconsViewSettingsPage(DolphinIconsView::LayoutMode mode,
                                             TQWidget* parent) :
    TQVBox(parent),
    m_mode(mode),
    m_iconSizeSlider(0),
    m_previewSizeSlider(0),
    m_textWidthBox(0),
    m_gridSpacingBox(0),
    m_fontFamilyBox(0),
    m_fontSizeBox(0),
    m_textlinesCountBox(0),
    m_arrangementBox(0)
{
    const int spacing = KDialog::spacingHint();
    const int margin = KDialog::marginHint();
    const TQSizePolicy sizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Fixed);

    setSpacing(spacing);
    setMargin(margin);

    DolphinIconsViewSettings* settings = DolphinSettings::instance().iconsView(m_mode);
    assert(settings != 0);

    TQHBox* sizesLayout = new TQHBox(this);
    sizesLayout->setSpacing(spacing);
    sizesLayout->setSizePolicy(sizePolicy);

    // create 'Icon Size' group including slider and preview
    TQGroupBox* iconSizeGroup = new TQGroupBox(2, Qt::Vertical, i18n("Icon Size"), sizesLayout);
    iconSizeGroup->setSizePolicy(sizePolicy);
    iconSizeGroup->setMargin(margin);

    const TQColor iconBackgroundColor(TDEGlobalSettings::baseColor());

    TQHBox* iconSizeVBox = new TQHBox(iconSizeGroup);
    iconSizeVBox->setSpacing(spacing);
    new TQLabel(i18n("Small"), iconSizeVBox);
    m_iconSizeSlider = new TQSlider(0, 5, 1, 0,  Qt::Horizontal, iconSizeVBox);
    m_iconSizeSlider->setValue(sliderValue(settings->iconSize()));
    m_iconSizeSlider->setTickmarks(TQSlider::Below);
    connect(m_iconSizeSlider, TQT_SIGNAL(valueChanged(int)),
            this, TQT_SLOT(slotIconSizeChanged(int)));
    new TQLabel(i18n("Large"), iconSizeVBox);

    m_iconSizeViewer = new PixmapViewer(iconSizeGroup);
    m_iconSizeViewer->setMinimumWidth(TDEIcon::SizeEnormous);
    m_iconSizeViewer->setFixedHeight(TDEIcon::SizeEnormous);
    m_iconSizeViewer->setEraseColor(iconBackgroundColor);
    slotIconSizeChanged(m_iconSizeSlider->value());

    if (m_mode == DolphinIconsView::Previews) {
        // create 'Preview Size' group including slider and preview
        TQGroupBox* previewSizeGroup = new TQGroupBox(2, Qt::Vertical, i18n("Preview Size"), sizesLayout);
        previewSizeGroup->setSizePolicy(sizePolicy);
        previewSizeGroup->setMargin(margin);

        TQHBox* previewSizeVBox = new TQHBox(previewSizeGroup);
        previewSizeVBox->setSpacing(spacing);
        new TQLabel(i18n("Small"), previewSizeVBox);
        m_previewSizeSlider = new TQSlider(0, 5, 1, 0,  Qt::Horizontal, previewSizeVBox);
        m_previewSizeSlider->setValue(sliderValue(settings->previewSize()));
        m_previewSizeSlider->setTickmarks(TQSlider::Below);
        connect(m_previewSizeSlider, TQT_SIGNAL(valueChanged(int)),
                this, TQT_SLOT(slotPreviewSizeChanged(int)));
        new TQLabel(i18n("Large"), previewSizeVBox);

        m_previewSizeViewer = new PixmapViewer(previewSizeGroup);
        m_previewSizeViewer->setMinimumWidth(TDEIcon::SizeEnormous);
        m_previewSizeViewer->setFixedHeight(TDEIcon::SizeEnormous);
        m_previewSizeViewer->setEraseColor(iconBackgroundColor);

        slotPreviewSizeChanged(m_previewSizeSlider->value());
    }

    TQGroupBox* textGroup = new TQGroupBox(2, Qt::Horizontal, i18n("Text"), this);
    textGroup->setSizePolicy(sizePolicy);
    textGroup->setMargin(margin);

    new TQLabel(i18n("Font family:"), textGroup);
    m_fontFamilyBox = new TDEFontCombo(textGroup);
    m_fontFamilyBox->setCurrentFont(settings->fontFamily());

    new TQLabel(i18n("Font size:"), textGroup);
    m_fontSizeBox = new TQSpinBox(6, 99, 1, textGroup);
    m_fontSizeBox->setValue(settings->fontSize());

    new TQLabel(i18n("Number of lines:"), textGroup);
    m_textlinesCountBox = new TQSpinBox(1, 5, 1, textGroup);
    m_textlinesCountBox->setValue(settings->textlinesCount());

    new TQLabel(i18n("Text width:"), textGroup);
    m_textWidthBox = new TQComboBox(textGroup);
    m_textWidthBox->insertItem(i18n("Small"));
    m_textWidthBox->insertItem(i18n("Medium"));
    m_textWidthBox->insertItem(i18n("Large"));

    TQGroupBox* gridGroup = new TQGroupBox(2, Qt::Horizontal, i18n("Grid"), this);
    gridGroup->setSizePolicy(sizePolicy);
    gridGroup->setMargin(margin);

    const bool leftToRightArrangement = (settings->arrangement() == TQIconView::LeftToRight);
    new TQLabel(i18n("Arrangement:"), gridGroup);
    m_arrangementBox = new TQComboBox(gridGroup);
    m_arrangementBox->insertItem(i18n("Left to right"));
    m_arrangementBox->insertItem(i18n("Top to bottom"));
    m_arrangementBox->setCurrentItem(leftToRightArrangement ? 0 : 1);

    new TQLabel(i18n("Grid spacing:"), gridGroup);
    m_gridSpacingBox = new TQComboBox(gridGroup);
    m_gridSpacingBox->insertItem(i18n("Small"));
    m_gridSpacingBox->insertItem(i18n("Medium"));
    m_gridSpacingBox->insertItem(i18n("Large"));
    m_gridSpacingBox->setCurrentItem((settings->gridSpacing() - GRID_SPACING_BASE) / GRID_SPACING_INC);

    // Add a dummy widget with no restriction regarding
    // a vertical resizing. This assures that the dialog layout
    // is not stretched vertically.
    new TQWidget(this);

    adjustTextWidthSelection();
}

IconsViewSettingsPage::~IconsViewSettingsPage()
{
}

void IconsViewSettingsPage::applySettings()
{
    DolphinIconsViewSettings* settings = DolphinSettings::instance().iconsView(m_mode);
    assert(settings != 0);

    const int defaultSize = iconSize(m_iconSizeSlider->value());
    settings->setIconSize(defaultSize);

    int previewSize = (m_mode == DolphinIconsView::Previews) ?
                      iconSize(m_previewSizeSlider->value()) :
                      defaultSize;
    if (previewSize < defaultSize) {
        // assure that the preview size is never smaller than the icon size
        previewSize = defaultSize;
    }
    settings->setPreviewSize(previewSize);

    const int fontSize = m_fontSizeBox->value();

    TQIconView::Arrangement arrangement = (m_arrangementBox->currentItem() == 0) ?
                                         TQIconView::LeftToRight :
                                         TQIconView::TopToBottom;
    settings->setArrangement(arrangement);
    settings->calculateGridSize(m_textWidthBox->currentItem());

    settings->setFontFamily(m_fontFamilyBox->currentFont());
    settings->setFontSize(fontSize);
    settings->setTextlinesCount(m_textlinesCountBox->value());

    settings->setGridSpacing(GRID_SPACING_BASE +
                             m_gridSpacingBox->currentItem() * GRID_SPACING_INC);
}

void IconsViewSettingsPage::slotIconSizeChanged(int value)
{
    TDEIconLoader iconLoader;
    m_iconSizeViewer->setPixmap(iconLoader.loadIcon("folder", TDEIcon::Desktop, iconSize(value)));

    if (m_previewSizeSlider != 0) {
        int previewSizeValue = m_previewSizeSlider->value();
        if (previewSizeValue < value) {
            // assure that the preview size is never smaller than the icon size
            previewSizeValue = value;
        }
        slotPreviewSizeChanged(previewSizeValue);
    }
}

void IconsViewSettingsPage::slotPreviewSizeChanged(int value)
{
    TDEIconLoader iconLoader;
    const int iconSizeValue = m_iconSizeSlider->value();
    if (value < iconSizeValue) {
        // assure that the preview size is never smaller than the icon size
        value = iconSizeValue;
    }
    m_previewSizeViewer->setPixmap(iconLoader.loadIcon("preview", TDEIcon::Desktop, iconSize(value)));
}

int IconsViewSettingsPage::iconSize(int sliderValue) const
{
    int size = TDEIcon::SizeMedium;
    switch (sliderValue) {
        case 0: size = TDEIcon::SizeSmall; break;
        case 1: size = TDEIcon::SizeSmallMedium; break;
        case 2: size = TDEIcon::SizeMedium; break;
        case 3: size = TDEIcon::SizeLarge; break;
        case 4: size = TDEIcon::SizeHuge; break;
        case 5: size = TDEIcon::SizeEnormous; break;
    }
    return size;
}

int IconsViewSettingsPage::sliderValue(int iconSize) const
{
    int value = 0;
    switch (iconSize) {
        case TDEIcon::SizeSmall: value = 0; break;
        case TDEIcon::SizeSmallMedium: value = 1; break;
        case TDEIcon::SizeMedium: value = 2; break;
        case TDEIcon::SizeLarge: value = 3; break;
        case TDEIcon::SizeHuge: value = 4; break;
        case TDEIcon::SizeEnormous: value = 5; break;
        default: break;
    }
    return value;
}

void IconsViewSettingsPage::adjustTextWidthSelection()
{
    DolphinIconsViewSettings* settings = DolphinSettings::instance().iconsView(m_mode);
    assert(settings != 0);
    m_textWidthBox->setCurrentItem(settings->textWidthHint());
}

#include "iconsviewsettingspage.moc"
