/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2006-02-08
 * Description : simple image properties side bar used by 
 *               camera gui.
 *
 * Copyright (C) 2006-2008 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 <tqsplitter.h>

// KDE includes.

#include <klocale.h>
#include <kconfig.h>
#include <kapplication.h>
#include <kcursor.h>
#include <kglobal.h>
#include <kiconloader.h>

// Local includes.

#include "ddebug.h"
#include "dmetadata.h"
#include "gpiteminfo.h"
#include "cameraiconview.h"
#include "cameraiconitem.h"
#include "cameraitempropertiestab.h"
#include "imagepropertiesmetadatatab.h"
#include "statusnavigatebar.h"
#include "navigatebarwidget.h"
#include "imagepropertiessidebarcamgui.h"
#include "imagepropertiessidebarcamgui.moc"

namespace Digikam
{

class ImagePropertiesSideBarCamGuiPriv
{
public:

    ImagePropertiesSideBarCamGuiPriv()
    {
        dirtyMetadataTab   = false;
        dirtyCameraItemTab = false;
        metadataTab        = 0;
        cameraItemTab      = 0;
        itemInfo           = 0;
        cameraView         = 0;
        cameraItem         = 0;
        exifData           = TQByteArray();
        currentURL         = KURL();
    }

    bool                        dirtyMetadataTab;
    bool                        dirtyCameraItemTab;

    TQByteArray                  exifData;

    KURL                        currentURL;

    GPItemInfo                 *itemInfo;

    ImagePropertiesMetaDataTab *metadataTab;

    CameraIconView             *cameraView;

    CameraIconViewItem         *cameraItem;

    CameraItemPropertiesTab    *cameraItemTab;
};

ImagePropertiesSideBarCamGui::ImagePropertiesSideBarCamGui(TQWidget *parent, const char *name,
                                                           TQSplitter *splitter, Side side,
                                                           bool mimimizedDefault)
                            : Sidebar(parent, name, side, mimimizedDefault)
{
    d = new ImagePropertiesSideBarCamGuiPriv;
    d->cameraItemTab = new CameraItemPropertiesTab(parent, true);
    d->metadataTab   = new ImagePropertiesMetaDataTab(parent, true);

    setSplitter(splitter);

    appendTab(d->cameraItemTab, SmallIcon("info"), i18n("Properties"));
    appendTab(d->metadataTab, SmallIcon("exifinfo"), i18n("Metadata"));

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

    connectNavigateSignals(d->cameraItemTab);
    connectNavigateSignals(d->metadataTab);

    connect(this, TQT_SIGNAL(signalChangedTab(TQWidget*)),
            this, TQT_SLOT(slotChangedTab(TQWidget*)));
}

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

void ImagePropertiesSideBarCamGui::connectNavigateSignals(NavigateBarTab *tab)
{
    connect(tab, TQT_SIGNAL(signalFirstItem()),
            this, TQT_SIGNAL(signalFirstItem()));

    connect(tab, TQT_SIGNAL(signalPrevItem()),
            this, TQT_SIGNAL(signalPrevItem()));

    connect(tab, TQT_SIGNAL(signalNextItem()),
            this, TQT_SIGNAL(signalNextItem()));

    connect(tab, TQT_SIGNAL(signalLastItem()),
            this, TQT_SIGNAL(signalLastItem()));
}

void ImagePropertiesSideBarCamGui::itemChanged(GPItemInfo* itemInfo, const KURL& url,
                                               const TQByteArray& exifData,
                                               CameraIconView* view, CameraIconViewItem* item)
{
    if (!itemInfo)
        return;
    
    d->exifData           = exifData;
    d->itemInfo           = itemInfo;
    d->currentURL         = url;
    d->dirtyMetadataTab   = false;
    d->dirtyCameraItemTab = false;
    d->cameraView         = view;
    d->cameraItem         = item;

    if (d->exifData.isEmpty())
    {
        DMetadata metaData(d->currentURL.path());
        d->exifData = metaData.getExif();
    }

    slotChangedTab( getActiveTab() );
}

void ImagePropertiesSideBarCamGui::slotNoCurrentItem(void)
{
    d->itemInfo           = 0;
    d->cameraItem         = 0;
    d->exifData           = TQByteArray();
    d->currentURL         = KURL();
    d->dirtyMetadataTab   = false;
    d->dirtyCameraItemTab = false;

    d->cameraItemTab->setCurrentItem();
    d->metadataTab->setCurrentURL();
}

void ImagePropertiesSideBarCamGui::slotChangedTab(TQWidget* tab)
{
    if (!d->itemInfo)
        return;

    setCursor(KCursor::waitCursor());

    if (tab == d->cameraItemTab && !d->dirtyCameraItemTab)
    {
        d->cameraItemTab->setCurrentItem(d->itemInfo,
                                         d->cameraItem->getDownloadName(), d->exifData,
                                         d->currentURL);

        d->dirtyCameraItemTab = true;
    }
    else if (tab == d->metadataTab && !d->dirtyMetadataTab)
    {
        d->metadataTab->setCurrentData(d->exifData, TQByteArray(), 
                                       d->itemInfo->name);

        d->dirtyMetadataTab = true;
    }

    // setting of NavigateBar, common for all tabs
    NavigateBarTab *navtab = dynamic_cast<NavigateBarTab *>(tab);
    if (navtab)
    {
        int currentItemType = StatusNavigateBar::ItemCurrent;
        if (d->cameraView->firstItem() == d->cameraItem)
            currentItemType = StatusNavigateBar::ItemFirst;
        else if (d->cameraView->lastItem() == d->cameraItem)
            currentItemType = StatusNavigateBar::ItemLast;

        navtab->setNavigateBarState(currentItemType);
        navtab->setNavigateBarFileName();
    }
    unsetCursor();
}

}  // NameSpace Digikam
