/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-07-15
 * Description : themed icon item 
 * 
 * Copyright (C) 2005 by Renchi Raju <renchi at pooh.tam.uiuc.edu>
 *
 * 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 <tqpainter.h>
#include <tqpixmap.h>
#include <tqpalette.h>
#include <tqpen.h>
#include <tqfontmetrics.h>
#include <tqfont.h>
#include <tqdatetime.h>

// KDE includes.

#include <tdeglobal.h>
#include <tdelocale.h>
#include <tdeapplication.h>
#include <kiconloader.h>

// Local includes.

#include "themeengine.h"
#include "themediconview.h"
#include "themediconitem.h"

namespace Digikam
{

static void dateToString(const TQDateTime& datetime, TQString& str)
{
    str = TDEGlobal::locale()->formatDateTime(datetime, true, false);
}

static TQString squeezedText(TQPainter* p, int width, const TQString& text)
{
    TQString fullText(text);
    fullText.replace("\n"," ");
    TQFontMetrics fm(p->fontMetrics());
    int textWidth = fm.width(fullText);
    if (textWidth > width) 
    {
        // start with the dots only
        TQString squeezedText = "...";
        int squeezedWidth = fm.width(squeezedText);

        // estimate how many letters we can add to the dots on both sides
        int letters = fullText.length() * (width - squeezedWidth) / textWidth;
        if (width < squeezedWidth) letters=1;
        squeezedText = fullText.left(letters) + "...";
        squeezedWidth = fm.width(squeezedText);

        if (squeezedWidth < width) 
        {
            // we estimated too short
            // add letters while text < label
            do 
            {
                letters++;
                squeezedText = fullText.left(letters) + "..."; 
                squeezedWidth = fm.width(squeezedText);
            }
            while (squeezedWidth < width);

            letters--;
            squeezedText = fullText.left(letters) + "..."; 
        } 
        else if (squeezedWidth > width) 
        {
            // we estimated too long
            // remove letters while text > label
            do 
            {
                letters--;
                squeezedText = fullText.left(letters) + "...";
                squeezedWidth = fm.width(squeezedText);
            } 
            while (letters && squeezedWidth > width);
        }

        if (letters >= 5) 
        {
            return squeezedText;
        }
    }
    
    return fullText;   
}

ThemedIconItem::ThemedIconItem(IconGroupItem* parent)
              : IconItem(parent)
{    
}

ThemedIconItem::~ThemedIconItem()
{    
}

void ThemedIconItem::paintItem()
{
    ThemedIconView* view = (ThemedIconView*)iconView();

    TQPixmap pix;
    TQRect   r;
    
    if (isSelected())
        pix = *(view->itemBaseSelPixmap());
    else
        pix = *(view->itemBaseRegPixmap());

    ThemeEngine* te = ThemeEngine::instance();
    
    TQPainter p(&pix);
    p.setPen(isSelected() ? te->textSelColor() : te->textRegColor());

    {
        r = view->itemPixmapRect();
        TDEIconLoader *iconLoader = tdeApp->iconLoader();    
        TQPixmap thumbnail = iconLoader->loadIcon("colors", TDEIcon::NoGroup,
                                                 100, TDEIcon::DefaultState, 0, true);
        
        p.drawPixmap(r.x() + (r.width()-thumbnail.width())/2,
                     r.y() + (r.height()-thumbnail.height())/2,
                     thumbnail);
    }

    r = view->itemNameRect();
    p.setFont(view->itemFontReg());
    p.drawText(r, TQt::AlignCenter, squeezedText(&p, r.width(), "IMG_00.JPG"));

    p.setFont(view->itemFontCom());
    r = view->itemCommentsRect();
    p.drawText(r, TQt::AlignCenter, squeezedText(&p, r.width(), i18n("Photo caption")));

    p.setFont(view->itemFontXtra());
    {
        TQDateTime date = TQDateTime::currentDateTime();

        r = view->itemDateRect();    
        p.setFont(view->itemFontXtra());
        TQString str;
        dateToString(date, str);
        p.drawText(r, TQt::AlignCenter, squeezedText(&p, r.width(), str));
    }

    p.setFont(view->itemFontCom());
    p.setPen(isSelected() ? te->textSpecialSelColor() : te->textSpecialRegColor());

    {
        TQString tags = i18n("Events, Places, Vacation");
        
        r = view->itemTagRect();    
        p.drawText(r, TQt::AlignCenter, 
                   squeezedText(&p, r.width(), tags));
    }
    

    if (this == view->currentItem())
    {
        p.setPen(TQPen(isSelected() ? te->textSelColor() : te->textRegColor(),
                      0, TQt::DotLine));
        p.drawRect(1, 1, pix.width()-2, pix.height()-2);
    }
    
    p.end();
    
    r = rect();
    r = TQRect(view->contentsToViewport(TQPoint(r.x(), r.y())),
              TQSize(r.width(), r.height()));

    bitBlt(view->viewport(), r.x(), r.y(), &pix,
           0, 0, r.width(), r.height());
}

}  // NameSpace Digikam