/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2003-03-09
 * Description : Album properties dialog.
 *
 * Copyright (C) 2003-2004 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Copyright (C) 2005 by Tom Albers <tomalbers@kde.nl>
 * Copyright (C) 2006-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * 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, 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.
 *
 * ============================================================ */

// TQt includes.

#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqdatetime.h>
#include <tqdir.h>
#include <tqfileinfo.h>
#include <tqframe.h>
#include <tqgroupbox.h>
#include <tqhbox.h>
#include <tqheader.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlistview.h>
#include <tqpushbutton.h>
#include <tqregexp.h>
#include <tqvalidator.h>

// KDE includes.

#include <kdatepicker.h>
#include <ktextedit.h>
#include <klineedit.h>
#include <tdelocale.h>
#include <kurl.h>
#include <tdemessagebox.h>
#include <kcursor.h>
#include <tdeversion.h>
#include <kinputdialog.h>

// Local includes.

#include "album.h"
#include "albumdb.h"
#include "albummanager.h"
#include "albumsettings.h"
#include "albumpropsedit.h"
#include "albumpropsedit.moc"

namespace Digikam
{

class AlbumPropsEditPriv
{

public:

    AlbumPropsEditPriv()
    {
        titleEdit          = 0;
        collectionCombo    = 0;
        commentsEdit       = 0;
        datePicker         = 0;
        album              = 0;
    }

    TQStringList     albumCollections;

    TQComboBox      *collectionCombo;

    KLineEdit      *titleEdit;

    KTextEdit      *commentsEdit;

    KDatePicker    *datePicker;

    PAlbum         *album;
};

AlbumPropsEdit::AlbumPropsEdit(PAlbum* album, bool create)
              : KDialogBase( Plain, create ? i18n("New Album") : i18n("Edit Album"),
                             Help|Ok|Cancel, Ok,
                             0, 0, true, true )
{
    d = new AlbumPropsEditPriv;
    d->album = album;
    setHelp("albumpropsedit.anchor", "digikam");

    TQGridLayout *topLayout = new TQGridLayout( plainPage(), 2, 6,
                                              0, spacingHint() );

    TQLabel *topLabel = new TQLabel( plainPage() );
    if (create)
    {
        topLabel->setText( i18n( "<qt><b>Create new Album in \"<i>%1</i>\"</b></qt>")
                           .arg(album->title()));
    }
    else
    {
        topLabel->setText( i18n( "<qt><b>\"<i>%1</i>\" Album Properties</b></qt>")
                           .arg(album->title()));
    }
    topLabel->setAlignment(TQt::AlignAuto | TQt::AlignVCenter | TQt::SingleLine);
    topLayout->addMultiCellWidget( topLabel, 0, 0, 0, 1  );

    // --------------------------------------------------------

    TQFrame *topLine = new TQFrame( plainPage() );
    topLine->setFrameShape( TQFrame::HLine );
    topLine->setFrameShadow( TQFrame::Sunken );
    topLayout->addMultiCellWidget( topLine, 1, 1, 0, 1  );

    // --------------------------------------------------------

    TQLabel *titleLabel = new TQLabel( plainPage( ) );
    titleLabel->setText( i18n( "&Title:" ) );
    topLayout->addWidget( titleLabel, 2, 0 );

    d->titleEdit = new KLineEdit( plainPage( ) );
    topLayout->addWidget( d->titleEdit, 2, 1 );
    titleLabel->setBuddy( d->titleEdit );

    TQRegExp titleRx("[^/]+");
    TQValidator *titleValidator = new TQRegExpValidator(titleRx, this);
    d->titleEdit->setValidator(titleValidator);

    TQLabel *collectionLabel = new TQLabel( plainPage( ) );
    collectionLabel->setText( i18n( "Co&llection:" ) );
    topLayout->addWidget( collectionLabel, 3, 0 );

    d->collectionCombo = new TQComboBox( plainPage( ) );
    d->collectionCombo->setEditable(true);
    topLayout->addWidget( d->collectionCombo, 3, 1 );
    collectionLabel->setBuddy( d->collectionCombo );

    TQLabel *commentsLabel = new TQLabel( plainPage( ) );
    commentsLabel->setText( i18n( "Ca&ption:" ) );
    topLayout->addWidget( commentsLabel, 4, 0, TQt::AlignAuto|TQt::AlignTop );

    d->commentsEdit = new KTextEdit( plainPage( ) );
    topLayout->addWidget( d->commentsEdit, 4, 1 );
    commentsLabel->setBuddy( d->commentsEdit );
    d->commentsEdit->setCheckSpellingEnabled(true);
    d->commentsEdit->setWordWrap(TQTextEdit::WidgetWidth);
    d->commentsEdit->setWrapPolicy(TQTextEdit::AtWhiteSpace);

    TQLabel *dateLabel = new TQLabel( plainPage( ) );
    dateLabel->setText( i18n( "Album &date:" ) );
    topLayout->addWidget( dateLabel, 5, 0, TQt::AlignAuto|TQt::AlignTop );

    d->datePicker = new KDatePicker( plainPage( ) );
    topLayout->addWidget( d->datePicker, 5, 1 );
    dateLabel->setBuddy( d->datePicker );

    TQHBox *buttonRow = new TQHBox( plainPage( ) );
    TQPushButton *dateLowButton = new TQPushButton(
            i18n("Selects the date of the oldest image",
                 "&Oldest" ), buttonRow );
    TQPushButton *dateAvgButton = new TQPushButton(
            i18n("Calculates the average date",
                 "&Average" ), buttonRow );
    TQPushButton *dateHighButton = new TQPushButton(
            i18n("Selects the date of the newest image",
                 "Newest" ), buttonRow );

    topLayout->addWidget( buttonRow, 6, 1);

    setTabOrder(d->titleEdit, d->collectionCombo);
    setTabOrder(d->collectionCombo, d->commentsEdit);
    setTabOrder(d->commentsEdit, d->datePicker);
    d->commentsEdit->setTabChangesFocus(true);
    d->titleEdit->selectAll();
    d->titleEdit->setFocus();

    // Initialize ---------------------------------------------

    AlbumSettings *settings = AlbumSettings::instance();
    if (settings)
    {
        d->collectionCombo->insertItem( TQString() );
        TQStringList collections = settings->getAlbumCollectionNames();
        d->collectionCombo->insertStringList( collections );
        int collectionIndex = collections.findIndex( album->collection() );

        if ( collectionIndex != -1 )
        {
            // + 1 because of the empty item
            d->collectionCombo->setCurrentItem(collectionIndex + 1);
        }
    }

    if (create)
    {
        d->titleEdit->setText( i18n("New Album") );
        d->datePicker->setDate( TQDate::currentDate() );
    }
    else
    {
        d->titleEdit->setText( album->title() );
        d->commentsEdit->setText( album->caption() );
        d->datePicker->setDate( album->date() );
    }

    // -- slots connections -------------------------------------------

    connect(d->titleEdit, TQ_SIGNAL(textChanged(const TQString&)),
            this, TQ_SLOT(slotTitleChanged(const TQString&)));

    connect(dateLowButton, TQ_SIGNAL( clicked() ),
            this, TQ_SLOT( slotDateLowButtonClicked()));

    connect(dateAvgButton, TQ_SIGNAL( clicked() ),
            this, TQ_SLOT( slotDateAverageButtonClicked()));

    connect(dateHighButton, TQ_SIGNAL( clicked() ),
            this, TQ_SLOT( slotDateHighButtonClicked()));

    adjustSize();
}

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

TQString AlbumPropsEdit::title() const
{
    return d->titleEdit->text();
}

TQString AlbumPropsEdit::comments() const
{
    return d->commentsEdit->text();
}

TQDate AlbumPropsEdit::date() const
{
    return d->datePicker->date();
}

TQString AlbumPropsEdit::collection() const
{
    TQString name = d->collectionCombo->currentText();

    if (name.isEmpty())
    {
        name = i18n( "Uncategorized Album" );
    }

    return name;
}

TQStringList AlbumPropsEdit::albumCollections() const
{
    TQStringList collections;
    AlbumSettings *settings = AlbumSettings::instance();
    if (settings)
    {
        collections = settings->getAlbumCollectionNames();
    }

    TQString currentCollection = d->collectionCombo->currentText();
    if ( collections.findIndex( currentCollection ) == -1 )
    {
        collections.append(currentCollection);
    }

    collections.sort();
    return collections;
}

bool AlbumPropsEdit::editProps(PAlbum *album, TQString& title,
                               TQString& comments, TQDate& date, TQString& collection,
                               TQStringList& albumCollections)
{
    AlbumPropsEdit dlg(album);

    bool ok = dlg.exec() == TQDialog::Accepted;

    title            = dlg.title();
    comments         = dlg.comments();
    date             = dlg.date();
    collection       = dlg.collection();
    albumCollections = dlg.albumCollections();

    return ok;
}

bool AlbumPropsEdit::createNew(PAlbum  *parent,
                               TQString& title,
                               TQString& comments,
                               TQDate&   date,
                               TQString& collection,
                               TQStringList& albumCollections)
{
    AlbumPropsEdit dlg(parent, true);

    bool ok = dlg.exec() == TQDialog::Accepted;

    title            = dlg.title();
    comments         = dlg.comments();
    date             = dlg.date();
    collection       = dlg.collection();
    albumCollections = dlg.albumCollections();

    return ok;
}

void AlbumPropsEdit::slotTitleChanged(const TQString& newtitle)
{
    enableButtonOK(!newtitle.isEmpty());
}

void AlbumPropsEdit::slotDateLowButtonClicked()
{
    setCursor( KCursor::waitCursor() );

    AlbumDB* db = AlbumManager::instance()->albumDB();
    TQDate avDate = db->getAlbumLowestDate( d->album->id() );
    setCursor( KCursor::arrowCursor() );

    if ( avDate.isValid() )
        d->datePicker->setDate( avDate );
}

void AlbumPropsEdit::slotDateHighButtonClicked()
{
    setCursor( KCursor::waitCursor() );

    AlbumDB* db = AlbumManager::instance()->albumDB();
    TQDate avDate = db->getAlbumHighestDate( d->album->id() );
    setCursor( KCursor::arrowCursor() );

    if ( avDate.isValid() )
        d->datePicker->setDate( avDate );
}

void AlbumPropsEdit::slotDateAverageButtonClicked()
{
    setCursor( KCursor::waitCursor() );

    AlbumDB* db = AlbumManager::instance()->albumDB();
    TQDate avDate = db->getAlbumAverageDate( d->album->id() );
    setCursor( KCursor::arrowCursor() );

    if ( avDate.isValid() )
        d->datePicker->setDate( avDate );
    else
        KMessageBox::error( plainPage( ),
                            i18n( "Could not calculate an average."),
                            i18n( "Could Not Calculate Average" ) );
}

}  // namespace Digikam


