summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/kameraklient/thumbitem.cpp
blob: 5809fc5d28189923f183914e69d57fa11cc00163 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* ============================================================
 * Copyright 2004 by Tudor Calin <tudor@1xtech.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
#include <tqpixmap.h>
#include <tqfont.h>
#include <tqfontmetrics.h>
#include <tqpalette.h>
#include <tqpainter.h>
#include <tqevent.h>
#include <tqtextedit.h>
// Local
#include "thumbview.h"
#include "thumbitem.h"

namespace KIPIKameraKlientPlugin
{

class ThumbItemLineEdit : public TQTextEdit {

public:
    ThumbItemLineEdit(const TQString& text, TQWidget* tqparent, ThumbItem* item);

private:
    void keyPressEvent(TQKeyEvent *e);
    void focusOutEvent(TQFocusEvent *e);
    ThumbItem *thumbItem;
    TQString    startText;
};

ThumbItemLineEdit::ThumbItemLineEdit(const TQString& text, TQWidget* tqparent, ThumbItem* item)
    : TQTextEdit(tqparent), thumbItem(item), startText(text) {
    setFrameStyle(TQFrame::Plain | TQFrame::Box);
    setLineWidth(1);

    setHScrollBarMode( AlwaysOff );
    setVScrollBarMode( AlwaysOff );

    setWordWrap( WidgetWidth );
    setWrapColumnOrWidth(item->pixmapRect().width());
    resize(200, 200);
    setText(text);
    tqsetAlignment(TQt::AlignCenter);
    resize(wrapColumnOrWidth() + 2, heightForWidth(wrapColumnOrWidth()) + 2);
}

void ThumbItemLineEdit::keyPressEvent( TQKeyEvent *e ) {
    if ( e->key()  == Key_Escape) {
		thumbItem->setText(startText);
		thumbItem->cancelRenameItem();
    } else if ( e->key() == Key_Enter || e->key() == Key_Return ) {
		thumbItem->renameItem();
    } else {
		TQTextEdit::keyPressEvent( e );
		sync();
    }
}

void ThumbItemLineEdit::focusOutEvent( TQFocusEvent *e ) {
    if (e->reason() != TQFocusEvent::Popup ) {
		thumbItem->cancelRenameItem();
	}
}

class ThumbItemPrivate {

	public:
		TQString text;
		TQPixmap* pixmap;

		TQRect rect;
		TQRect textRect;
		TQRect pixmapRect;

		bool selected;
		TQString    key;
};

ThumbItem::ThumbItem(ThumbView* tqparent, const TQString& text, const TQPixmap& pixmap) {
    view = tqparent;
    next = 0;
    prev = 0;
    renameBox = 0;

    d = new ThumbItemPrivate;
    d->text = text;
    d->pixmap = new TQPixmap(pixmap);
    d->selected = false;
    d->key = d->text;
    // make sure to calcrect before inserting; this will calculate the size of the rect; inserting would move it to the required location
    d->rect       = TQRect(0, 0, 0, 0);
    d->textRect   = TQRect(0, 0, 0, 0);
    d->pixmapRect = TQRect(0, 0, 0, 0);
    calcRect();

    view->insertItem(this);
}

ThumbItem::~ThumbItem() {
    view->takeItem(this);
    delete d->pixmap;
    delete d;
}

void ThumbItem::calcRect() {
    TQRect rect(d->rect);
    TQRect textRect(d->textRect);
    TQRect pixmapRect(d->pixmapRect);
    // set initial pixrect
    int pw = d->pixmap->width();
    int ph = d->pixmap->height();

    pixmapRect.setWidth(pw);
    pixmapRect.setHeight(ph);
    // word wrap
    TQFontMetrics fm(view->font());
    TQRect r = TQRect(fm.boundingRect(0, 0, pixmapRect.width(),
                                    0xFFFFFFFF, TQt::AlignHCenter |
                                    TQt::WordBreak | TQt::BreakAnywhere,
                                    d->text));
    r.setWidth(r.width() + 4);

    textRect.setWidth(r.width());
    textRect.setHeight(r.height());
    // Now start updating the rects
    int w = TQMAX(textRect.width(), pixmapRect.width());
    int h = textRect.height() + pixmapRect.height() + 1;

    rect.setWidth(w);
    rect.setHeight(h);
    // Center the pix and text rect
    pixmapRect = TQRect((rect.width() - pixmapRect.width())/2,
                       0,
                       pixmapRect.width(), pixmapRect.height());
    textRect   = TQRect((rect.width() - textRect.width())/2,
                       rect.height() - textRect.height(),
                       textRect.width(), textRect.height());
    // Finally save the settings
    setRect(rect);
    setPixmapRect(pixmapRect);
    setTextRect(textRect);
}

void ThumbItem::paintItem(TQPainter *, const TQColorGroup& cg) {
    TQRect pRect=pixmapRect(true);
    TQRect tRect=textRect(true);

    TQPixmap pix(rect().width(), rect().height());
    pix.fill(cg.base());
    TQPainter painter(&pix);
    painter.drawPixmap(pRect.x(), pRect.y(), *pixmap() );
    if (isSelected()) {
        TQPen pen;
        pen.setColor(cg.highlight());
        painter.setPen(pen);
        painter.drawRect(0, 0, pix.width(), pix.height());
        painter.fillRect(0, tRect.y(), pix.width(),
                     tRect.height(), cg.highlight() );
        painter.setPen( TQPen( cg.highlightedText() ) );
    } else {
        painter.setPen( cg.text() );
	}
    painter.drawText(tRect, TQt::WordBreak|TQt::BreakAnywhere|TQt::AlignHCenter|TQt::AlignTop,text());

    painter.end();

    TQRect 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());
}

void ThumbItem::tqrepaint() {
    TQRect r(view->contentsRectToViewport(d->rect));
    view->viewport()->tqrepaint(r);
}

TQRect ThumbItem::rect() {
    return d->rect;
}

TQRect ThumbItem::textRect(bool relative) {
    if (relative) {
        return d->textRect;
    } else {
        TQRect r(x() + d->textRect.x(), y() + d->textRect.y(), d->textRect.width(), d->textRect.height());
        return r;
    }
}

TQRect ThumbItem::pixmapRect(bool relative) {
    if (relative) {
        return d->pixmapRect;
    } else {
        TQRect r(x() + d->pixmapRect.x(), y() + d->pixmapRect.y(), d->pixmapRect.width(), d->pixmapRect.height());
        return r;
    }

}

void ThumbItem::setRect(const TQRect& rect) {
	if (rect.isValid()) {
        d->rect = rect;
    }
}

void ThumbItem::setTextRect(const TQRect& rect)
{
   if (rect.isValid()) {
        d->textRect = rect;
    }
}

void ThumbItem::setPixmapRect(const TQRect& rect) {
	if (rect.isValid()) {
        d->pixmapRect = rect;
    }
}

TQPixmap * ThumbItem::pixmap() const {
    return d->pixmap;
}

TQString ThumbItem::text() const {
    return d->text;
}

int ThumbItem::x() const {
    return d->rect.x();
}

int ThumbItem::y() const {
    return d->rect.y();
}

int ThumbItem::width() const {
    return d->rect.width();
}

int ThumbItem::height() const {
    return d->rect.height();
}

bool ThumbItem::move(int x, int y) {
    if (x == this->x() && y == this->y()) {
		return false;
	}
    d->rect.setRect(x, y, d->rect.width(), d->rect.height());
    return true;
}

void ThumbItem::setSelected(bool val, bool cb) {
    if (cb) {
        view->blockSignals(true);
        view->clearSelection();
        view->blockSignals(false);
    }
    d->selected = val;
    view->selectItem(this, val);
    TQRect r(d->rect);
    r = TQRect(view->contentsToViewport(TQPoint(r.x(), r.y())), TQSize(r.width(), r.height()));
    view->viewport()->update(r);
}

bool ThumbItem::isSelected() {
    return d->selected;
}

void ThumbItem::setPixmap(const TQPixmap& pixmap) {
    if (d->pixmap) {
        delete d->pixmap;
        d->pixmap = 0;
    }
    d->pixmap = new TQPixmap(pixmap);

    TQRect oR(d->rect);
    calcRect();
    oR = oR.unite(d->rect);
    oR = TQRect(view->contentsToViewport(TQPoint(oR.x(), oR.y())), TQSize(oR.width(), oR.height()));

    view->updateItemContainer(this);

    if(oR.intersects(TQRect(view->contentsX(), view->contentsY(), view->visibleWidth(), view->visibleHeight()))) {
        view->viewport()->tqrepaint(oR);
	}
}

void ThumbItem::setText(const TQString& text) {
    d->text = text;
    d->key  = text;

    TQRect oR(d->rect);
    calcRect();
    oR = oR.unite(d->rect);
    oR = TQRect(view->contentsToViewport(TQPoint(oR.x(), oR.y())),
               TQSize(oR.width(), oR.height()));

    view->updateItemContainer(this);
    
    if(oR.intersects(TQRect(view->contentsX(), view->contentsY(), view->visibleWidth(), view->visibleHeight()))) {
        view->viewport()->tqrepaint(oR);
	}
}

ThumbItem * ThumbItem::nextItem() {
    return next;
}

ThumbItem * ThumbItem::prevItem() {
    return prev;
}

ThumbView* ThumbItem::iconView() {
    return view;    
}

void ThumbItem::rename() {
    if (renameBox) {
        delete renameBox;
        renameBox = 0;
    }
    renameBox = new ThumbItemLineEdit(d->text, view->viewport(), this);
    TQRect tr(textRect(false));
    view->addChild(renameBox, tr.x() + (tr.width()/2 - renameBox->width()/2 ), tr.y() - 3);
    renameBox->selectAll();
    view->viewport()->setFocusProxy(renameBox);
    renameBox->setFocus();
    renameBox->show();

    view->renamingItem = this;
}

void ThumbItem::renameItem() {
    if (!renameBox) {
		return;
	}
    setText(renameBox->text());

    bool resetFocus = view->viewport()->focusProxy() == renameBox;
    delete renameBox;
    renameBox = 0;
    if (resetFocus) {
		view->viewport()->setFocusProxy(view);
		view->setFocus();
    }
    view->renamingItem = 0;
    view->emitRenamed(this);
}

void ThumbItem::cancelRenameItem() {
    tqrepaint();

    bool resetFocus = view->viewport()->focusProxy() == renameBox;
    delete renameBox;
    renameBox = 0;
    if (resetFocus) {
		view->viewport()->setFocusProxy(view);
		view->setFocus();
    }
    view->renamingItem = 0;
}

int ThumbItem::compare(ThumbItem *item) {
    return key().localeAwareCompare(item->key());
}

TQString ThumbItem::key() const {
    return d->key;    
}

}  // NameSpace KIPIKameraKlientPlugin