summaryrefslogtreecommitdiffstats
path: root/digikam/themedesigner/themediconitem.cpp
blob: 5d867d76eae982c94e6cb77225fc1dbcb76147a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* ============================================================
 *
 * 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 <kglobal.h>
#include <klocale.h>
#include <kapplication.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 = KGlobal::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();
        KIconLoader *iconLoader = KApplication::kApplication()->iconLoader();    
        TQPixmap thumbnail = iconLoader->loadIcon("colors", KIcon::NoGroup,
                                                 100, KIcon::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::tqcurrentDateTime();

        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