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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
/***************************************************************************
copyright : (C) 2002-2006 by Robby Stephenson
email : robby@periapsis.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of version 2 of the GNU General Public License as *
* published by the Free Software Foundation; *
* *
***************************************************************************/
#include "entryiconview.h"
#include "collection.h"
#include "collectionfactory.h"
#include "imagefactory.h"
#include "controller.h"
#include "entry.h"
#include "field.h"
#include "tellico_utils.h"
#include "tellico_debug.h"
#include "listviewcomparison.h"
#include <kpopupmenu.h>
#include <kstringhandler.h>
#include <kiconloader.h>
#include <kwordwrap.h>
#include <kimageeffect.h>
#include <klocale.h>
#include <tqpainter.h>
namespace {
static const int MIN_ENTRY_ICON_SIZE = 32;
static const int MAX_ENTRY_ICON_SIZE = 128;
static const int ENTRY_ICON_SIZE_PAD = 6;
static const int ENTRY_ICON_SHADOW_RIGHT = 1;
static const int ENTRY_ICON_SHADOW_BOTTOM = 1;
}
using Tellico::EntryIconView;
using Tellico::EntryIconViewItem;
EntryIconView::EntryIconView(TQWidget* parent_, const char* name_/*=0*/)
: KIconView(parent_, name_), m_coll(0), m_maxAllowedIconWidth(MAX_ENTRY_ICON_SIZE),
m_maxIconWidth(MIN_ENTRY_ICON_SIZE), m_maxIconHeight(MIN_ENTRY_ICON_SIZE),
m_comparison(0) {
setAutoArrange(true);
setSorting(true);
setItemsMovable(false);
setSelectionMode(TQIconView::Extended);
setResizeMode(TQIconView::Adjust);
setMode(KIconView::Select);
setSpacing(4);
// setWordWrapIconText(false);
m_defaultPixmaps.setAutoDelete(true);
connect(this, TQT_SIGNAL(selectionChanged()), TQT_SLOT(slotSelectionChanged()));
connect(this, TQT_SIGNAL(doubleClicked(TQIconViewItem*)), TQT_SLOT(slotDoubleClicked(TQIconViewItem*)));
connect(this, TQT_SIGNAL(contextMenuRequested(TQIconViewItem*, const TQPoint&)),
TQT_SLOT(slotShowContextMenu(TQIconViewItem*, const TQPoint&)));
}
EntryIconView::~EntryIconView() {
delete m_comparison;
m_comparison = 0;
}
EntryIconViewItem* EntryIconView::firstItem() const {
return static_cast<EntryIconViewItem*>(KIconView::firstItem());
}
void EntryIconView::findImageField() {
m_imageField.truncate(0);
if(!m_coll) {
return;
}
const Data::FieldVec& fields = m_coll->imageFields();
if(!fields.isEmpty()) {
m_imageField = fields[0]->name();
}
// myDebug() << "EntryIconView::findImageField() - image field = " << m_imageField << endl;
}
const TQString& EntryIconView::imageField() {
return m_imageField;
}
const TQString& EntryIconView::sortField() {
return m_comparison ? m_comparison->fieldName() : TQString();
}
const TQPixmap& EntryIconView::defaultPixmap() {
TQPixmap* pix = m_defaultPixmaps[m_coll->type()];
if(pix) {
return *pix;
}
KIconLoader* loader = TDEGlobal::instance()->iconLoader();
TQPixmap tmp = loader->loadIcon(TQString::fromLatin1("nocover_") + CollectionFactory::typeName(m_coll->type()),
KIcon::User, 0, KIcon::DefaultState, 0, true /*canReturnNull */);
if(tmp.isNull()) {
myLog() << "EntryIconView::defaultPixmap() - null nocover image, loading tellico.png" << endl;
tmp = UserIcon(TQString::fromLatin1("tellico"));
}
if(TQMAX(tmp.width(), tmp.height()) > static_cast<int>(MIN_ENTRY_ICON_SIZE)) {
tmp.convertFromImage(tmp.convertToImage().smoothScale(m_maxIconWidth, m_maxIconHeight, TQ_ScaleMin));
}
pix = new TQPixmap(tmp);
m_defaultPixmaps.insert(m_coll->type(), pix);
return *pix;
}
void EntryIconView::setMaxAllowedIconWidth(int width_) {
m_maxAllowedIconWidth = TQMAX(MIN_ENTRY_ICON_SIZE, TQMIN(MAX_ENTRY_ICON_SIZE, width_));
setMaxItemWidth(m_maxAllowedIconWidth + 2*ENTRY_ICON_SIZE_PAD);
m_defaultPixmaps.clear();
refresh();
}
void EntryIconView::fillView() {
setSorting(false);
setGridX(m_maxAllowedIconWidth + 2*ENTRY_ICON_SIZE_PAD); // set default spacing initially
GUI::CursorSaver cs(TQt::waitCursor);
bool allDefaultImages = true;
m_maxIconWidth = TQMAX(MIN_ENTRY_ICON_SIZE, m_maxIconWidth);
m_maxIconHeight = TQMAX(MIN_ENTRY_ICON_SIZE, m_maxIconHeight);
EntryIconViewItem* item;
for(Data::EntryVecIt it = m_entries.begin(); it != m_entries.end(); ++it) {
item = new EntryIconViewItem(this, it);
m_maxIconWidth = TQMAX(m_maxIconWidth, item->width());
m_maxIconHeight = TQMAX(m_maxIconHeight, item->pixmapRect().height());
if(item->usesImage()) {
allDefaultImages = false;
}
}
if(allDefaultImages) {
m_maxIconWidth = m_maxAllowedIconWidth;
m_maxIconHeight = m_maxAllowedIconWidth;
}
// if both width and height are min, then that means there are no images
m_defaultPixmaps.clear();
// now reset size of all default pixmaps
for(item = firstItem(); item; item = item->nextItem()) {
if(!item->usesImage()) {
item->updatePixmap();
}
}
setGridX(m_maxIconWidth + 2*ENTRY_ICON_SIZE_PAD); // the pad is so the text can be wider than the icon
setGridY(m_maxIconHeight + fontMetrics().height());
sort();
setSorting(true);
}
void EntryIconView::refresh() {
if(!m_coll) {
return;
}
showEntries(m_entries);
}
void EntryIconView::clear() {
KIconView::clear();
m_coll = 0;
m_entries.clear();
m_imageField.truncate(0);
}
void EntryIconView::showEntries(const Data::EntryVec& entries_) {
setUpdatesEnabled(false);
KIconView::clear(); // don't call EntryIconView::clear() since that clears the entries_ ref
if(entries_.isEmpty()) {
return;
}
m_coll = entries_[0]->collection();
m_entries = entries_;
findImageField();
fillView();
setUpdatesEnabled(true);
}
void EntryIconView::addEntries(Data::EntryVec entries_) {
if(entries_.isEmpty()) {
return;
}
if(!m_coll) {
m_coll = entries_[0]->collection();
}
// since the view probably doesn't show all the current entries
// only add the new ones if the count is the total
if(m_entries.count() + entries_.count() < m_coll->entryCount()) {
return;
}
int w = MIN_ENTRY_ICON_SIZE;
int h = MIN_ENTRY_ICON_SIZE;
for(Data::EntryVecIt entry = entries_.begin(); entry != entries_.end(); ++entry) {
m_entries.append(entry);
EntryIconViewItem* item = new EntryIconViewItem(this, entry);
w = TQMAX(w, item->width());
h = TQMAX(h, item->pixmapRect().height());
}
if(w > m_maxIconWidth || h > m_maxIconHeight) {
refresh();
}
}
void EntryIconView::modifyEntries(Data::EntryVec entries_) {
for(Data::EntryVecIt entry = entries_.begin(); entry != entries_.end(); ++entry) {
EntryIconViewItem* item = 0;
for(EntryIconViewItem* it = firstItem(); it; it = it->nextItem()) {
if(it->entry() == entry) {
item = it;
break;
}
}
if(!item) {
continue;
}
item->update();
}
}
void EntryIconView::removeEntries(Data::EntryVec entries_) {
for(Data::EntryVecIt entry = entries_.begin(); entry != entries_.end(); ++entry) {
m_entries.remove(entry);
}
bool found = false;
EntryIconViewItem* item = firstItem();
while(item) {
if(entries_.contains(item->entry())) {
EntryIconViewItem* prev = item;
item = item->nextItem();
delete prev;
found = true;
} else {
item = item->nextItem();
}
}
if(found) {
arrangeItemsInGrid();
}
}
void EntryIconView::slotSelectionChanged() {
Data::EntryVec entries;
const TQPtrList<EntryIconViewItem>& items = selectedItems();
for(TQPtrListIterator<EntryIconViewItem> it(items); it.current(); ++it) {
entries.append(it.current()->entry());
}
Controller::self()->slotUpdateSelection(this, entries);
}
void EntryIconView::slotDoubleClicked(TQIconViewItem* item_) {
EntryIconViewItem* i = static_cast<EntryIconViewItem*>(item_);
if(i) {
Controller::self()->editEntry(i->entry());
}
}
void EntryIconView::updateSelected(EntryIconViewItem* item_, bool s_) const {
if(s_) {
m_selectedItems.append(item_);
} else {
m_selectedItems.removeRef(item_);
}
}
void EntryIconView::slotShowContextMenu(TQIconViewItem* item_, const TQPoint& point_) {
TDEPopupMenu menu(this);
// only insert entry items if one is selected
if(item_) {
Controller::self()->plugEntryActions(&menu);
menu.insertSeparator();
}
TDEPopupMenu sortMenu(&menu);
const TQStringList titles = m_coll->fieldTitles();
for(TQStringList::ConstIterator it = titles.begin(); it != titles.end(); ++it) {
sortMenu.insertItem(*it);
}
connect(&sortMenu, TQT_SIGNAL(activated(int)), TQT_SLOT(slotSortMenuActivated(int)));
menu.insertItem(i18n("&Sort By"), &sortMenu);
menu.exec(point_);
}
void EntryIconView::slotSortMenuActivated(int id_) {
const TDEPopupMenu* menu = ::tqqt_cast<TDEPopupMenu*>(sender());
if(menu) {
TQString title = menu->text(id_);
Data::FieldPtr f = m_coll->fieldByTitle(title);
if(f) {
delete m_comparison;
m_comparison = ListViewComparison::create(f);
sort();
}
}
}
int EntryIconView::compare(const EntryIconViewItem* item1, EntryIconViewItem* item2) {
if(m_comparison) {
return m_comparison->compare(item1, item2);
}
return 0;
}
/* *********************************************************** */
EntryIconViewItem::EntryIconViewItem(EntryIconView* parent_, Data::EntryPtr entry_)
: KIconViewItem(parent_, entry_->title()), m_entry(entry_), m_usesImage(false) {
setDragEnabled(false);
const TQString& imageField = parent_->imageField();
if(!imageField.isEmpty()) {
TQPixmap p = ImageFactory::pixmap(m_entry->field(imageField),
parent_->maxAllowedIconWidth(),
parent_->maxAllowedIconWidth());
if(!p.isNull()) {
setPixmap(p);
m_usesImage = true;
}
}
}
EntryIconViewItem::~EntryIconViewItem() {
// be sure to remove from selected list when it's deleted
EntryIconView* view = iconView();
if(view) {
view->updateSelected(this, false);
}
}
void EntryIconViewItem::setSelected(bool s_) {
setSelected(s_, false);
}
void EntryIconViewItem::setSelected(bool s_, bool cb_) {
EntryIconView* view = iconView();
if(!view) {
return;
}
if(s_ != isSelected()) {
view->updateSelected(this, s_);
KIconViewItem::setSelected(s_, cb_);
}
}
void EntryIconViewItem::updatePixmap() {
EntryIconView* view = iconView();
const TQString& imageField = view->imageField();
m_usesImage = false;
if(imageField.isEmpty()) {
setPixmap(view->defaultPixmap());
} else {
TQPixmap p = ImageFactory::pixmap(m_entry->field(imageField),
view->maxAllowedIconWidth(),
view->maxAllowedIconWidth());
if(p.isNull()) {
setPixmap(view->defaultPixmap());
} else {
setPixmap(p);
m_usesImage = true;
calcRect();
}
}
}
void EntryIconViewItem::update() {
setText(m_entry->title());
updatePixmap();
iconView()->arrangeItemsInGrid();
}
void EntryIconViewItem::calcRect(const TQString& text_) {
KIconViewItem::calcRect(text_);
TQRect r = pixmapRect();
r.rRight() += ENTRY_ICON_SHADOW_RIGHT;
r.rBottom() += ENTRY_ICON_SHADOW_BOTTOM;
setPixmapRect(r);
}
void EntryIconViewItem::paintItem(TQPainter* p_, const TQColorGroup &cg_) {
p_->save();
paintPixmap(p_, cg_);
paintText(p_, cg_);
p_->restore();
}
void EntryIconViewItem::paintFocus(TQPainter*, const TQColorGroup&) {
// don't draw anything
}
void EntryIconViewItem::paintPixmap(TQPainter* p_, const TQColorGroup& cg_) {
// only draw the shadow if there's an image
// oh, and don't draw it if it's a file catalog, it doesn't look right
if(m_usesImage && !isSelected() && m_entry->collection()->type() != Data::Collection::File) {
// pixmapRect() already includes shadow size, so shift the rect by that amount
TQRect r = pixmapRect(false);
r.setLeft(r.left() + ENTRY_ICON_SHADOW_RIGHT);
r.setTop(r.top() + ENTRY_ICON_SHADOW_BOTTOM);
TQColor c = Tellico::blendColors(iconView()->backgroundColor(), TQt::black, 10);
p_->fillRect(r, c);
}
KIconViewItem::paintPixmap(p_, cg_);
}
void EntryIconViewItem::paintText(TQPainter* p_, const TQColorGroup &cg_) {
TQRect tr = textRect(false);
int textX = tr.x() + 2;
int textY = tr.y();
if(isSelected()) {
p_->setBrush(TQBrush(cg_.highlight()));
p_->setPen(TQPen(cg_.highlight()));
p_->drawRoundRect(tr, 1000/tr.width(), 1000/tr.height());
p_->setPen(TQPen(cg_.highlightedText()));
} else {
if(iconView()->itemTextBackground() != Qt::NoBrush) {
p_->fillRect(tr, iconView()->itemTextBackground());
}
p_->setPen(cg_.text());
}
int align = iconView()->itemTextPos() == TQIconView::Bottom ? AlignHCenter : AlignAuto;
wordWrap()->drawText(p_, textX, textY, align | KWordWrap::Truncate);
}
TQString EntryIconViewItem::key() const {
const TQString& sortField = iconView()->sortField();
if(sortField.isEmpty()) {
return KIconViewItem::key();
}
return m_entry->field(sortField);
}
int EntryIconViewItem::compare(TQIconViewItem* item_) const {
int res = iconView()->compare(this, static_cast<EntryIconViewItem*>(item_));
return res == 0 ? KIconViewItem::compare(item_) : res;
}
#include "entryiconview.moc"
|