~ubuntu-branches/ubuntu/gutsy/kid3/gutsy

« back to all changes in this revision

Viewing changes to kid3/filelistitem.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2007-07-01 00:31:03 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070701003103-2qownnv49a7jdqm3
Tags: 0.9-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 */
9
9
 
10
10
#include "filelistitem.h"
 
11
#include "filelist.h"
11
12
#include "taggedfile.h"
 
13
#include "dirinfo.h"
 
14
#include "kid3.h"
12
15
 
13
16
#include <qapplication.h>
14
17
#include <qpainter.h>
15
18
#include <qpixmap.h>
 
19
#include <qfileinfo.h>
16
20
 
17
21
/** Empty pixmap, will be allocated in constructor */
18
 
QPixmap *FileListItem::nullPixmap = 0;
 
22
QPixmap* FileListItem::nullPixmap = 0;
19
23
/** Pixmap for modified file, will be allocated in constructor */
20
 
QPixmap *FileListItem::modifiedPixmap = 0;
 
24
QPixmap* FileListItem::modifiedPixmap = 0;
21
25
/** Pixmap for V1V2, will be allocated in constructor */
22
 
QPixmap *FileListItem::v1v2Pixmap = 0;
 
26
QPixmap* FileListItem::v1v2Pixmap = 0;
23
27
/** Pixmap for V1, will be allocated in constructor */
24
 
QPixmap *FileListItem::v1Pixmap = 0;
 
28
QPixmap* FileListItem::v1Pixmap = 0;
25
29
/** Pixmap for V2, will be allocated in constructor */
26
 
QPixmap *FileListItem::v2Pixmap = 0;
 
30
QPixmap* FileListItem::v2Pixmap = 0;
27
31
/** Pixmap for "no tag", will be allocated in constructor */
28
 
QPixmap *FileListItem::notagPixmap = 0;
 
32
QPixmap* FileListItem::notagPixmap = 0;
 
33
/** Pixmap for closed folder, will be allocated in constructor */
 
34
QPixmap* FileListItem::folderClosedPixmap = 0;
 
35
/** Pixmap for open folder, will be allocated in constructor */
 
36
QPixmap* FileListItem::folderOpenPixmap = 0;
29
37
 
30
38
/* The bitmaps are stored here instead of using KDE bitmaps to make
31
39
   it work for the Qt only versions. */
32
40
/** picture for modified pixmap */
33
 
static const char * const modified_xpm[] = {
 
41
static const char* const modified_xpm[] = {
34
42
        "16 16 33 1",
35
43
        ". c None",
36
44
        "B c None",
84
92
};
85
93
 
86
94
/** picture for empty pixmap */
87
 
static const char * const null_xpm[] = {
 
95
static const char* const null_xpm[] = {
88
96
        "16 16 2 1",
89
97
        "# c None",
90
98
        ". c None",
106
114
        "#.#.#.#.#.#.#.#."
107
115
};
108
116
 
109
 
/* XPM */
110
 
static const char * const v1v2_xpm[] = {
111
 
        "16 16 3 1",
112
 
        "       c None",
113
 
        ".      c #000000",
114
 
        "+      c #FFFFFF",
115
 
        "                ",
116
 
        "                ",
117
 
        "   .   .   .    ",
118
 
        "   .   .  ..    ",
119
 
        "    . .  . .    ",
120
 
        "    . .    .    ",
121
 
        "     .     .    ",
122
 
        "                ",
123
 
        "                ",
124
 
        "   .   .  ..    ",
125
 
        "   .   . .  .   ",
126
 
        "    . .    .    ",
127
 
        "    . .   .     ",
128
 
        "     .   ....   ",
129
 
        "                ",
130
 
        "                "};
131
 
 
132
 
/* XPM */
133
 
static const char * const v1_xpm[] = {
134
 
        "16 16 3 1",
135
 
        "       c None",
136
 
        ".      c #000000",
137
 
        "+      c #FFFFFF",
138
 
        "                ",
139
 
        "                ",
140
 
        "   .   .   .    ",
141
 
        "   .   .  ..    ",
142
 
        "    . .  . .    ",
143
 
        "    . .    .    ",
144
 
        "     .     .    ",
145
 
        "                ",
146
 
        "                ",
147
 
        "                ",
148
 
        "                ",
149
 
        "                ",
150
 
        "                ",
151
 
        "                ",
152
 
        "                ",
153
 
        "                "};
154
 
 
155
 
/* XPM */
156
 
static const char * const v2_xpm[] = {
157
 
        "16 16 3 1",
158
 
        "       c None",
159
 
        ".      c #000000",
160
 
        "+      c #FFFFFF",
161
 
        "                ",
162
 
        "                ",
163
 
        "                ",
164
 
        "                ",
165
 
        "                ",
166
 
        "                ",
167
 
        "                ",
168
 
        "                ",
169
 
        "                ",
170
 
        "   .   .  ..    ",
171
 
        "   .   . .  .   ",
172
 
        "    . .    .    ",
173
 
        "    . .   .     ",
174
 
        "     .   ....   ",
175
 
        "                ",
176
 
        "                "};
177
 
 
178
 
/* XPM */
179
 
static const char * const notag_xpm[] = {
 
117
/** picture with V1 and V2 */
 
118
static const char* const v1v2_xpm[] = {
 
119
        "16 16 3 1",
 
120
        "       c None",
 
121
        ".      c #000000",
 
122
        "+      c #FFFFFF",
 
123
        "                ",
 
124
        "                ",
 
125
        "   .   .   .    ",
 
126
        "   .   .  ..    ",
 
127
        "    . .  . .    ",
 
128
        "    . .    .    ",
 
129
        "     .     .    ",
 
130
        "                ",
 
131
        "                ",
 
132
        "   .   .  ..    ",
 
133
        "   .   . .  .   ",
 
134
        "    . .    .    ",
 
135
        "    . .   .     ",
 
136
        "     .   ....   ",
 
137
        "                ",
 
138
        "                "};
 
139
 
 
140
/** picture with V1 */
 
141
static const char* const v1_xpm[] = {
 
142
        "16 16 3 1",
 
143
        "       c None",
 
144
        ".      c #000000",
 
145
        "+      c #FFFFFF",
 
146
        "                ",
 
147
        "                ",
 
148
        "   .   .   .    ",
 
149
        "   .   .  ..    ",
 
150
        "    . .  . .    ",
 
151
        "    . .    .    ",
 
152
        "     .     .    ",
 
153
        "                ",
 
154
        "                ",
 
155
        "                ",
 
156
        "                ",
 
157
        "                ",
 
158
        "                ",
 
159
        "                ",
 
160
        "                ",
 
161
        "                "};
 
162
 
 
163
/** picture with V2 */
 
164
static const char* const v2_xpm[] = {
 
165
        "16 16 3 1",
 
166
        "       c None",
 
167
        ".      c #000000",
 
168
        "+      c #FFFFFF",
 
169
        "                ",
 
170
        "                ",
 
171
        "                ",
 
172
        "                ",
 
173
        "                ",
 
174
        "                ",
 
175
        "                ",
 
176
        "                ",
 
177
        "                ",
 
178
        "   .   .  ..    ",
 
179
        "   .   . .  .   ",
 
180
        "    . .    .    ",
 
181
        "    . .   .     ",
 
182
        "     .   ....   ",
 
183
        "                ",
 
184
        "                "};
 
185
 
 
186
/** picture with NO TAG */
 
187
static const char* const notag_xpm[] = {
180
188
        "16 16 3 1",
181
189
        "       c None",
182
190
        ".      c #000000",
198
206
        "                ",
199
207
        "                "};
200
208
 
201
 
/** width of both pixmaps, got using QPixmap::width() */
202
 
static const int pixmapWidth = 16;
203
 
/** height of both pixmaps, got using QPixmap::height() */
204
 
static const int pixmapHeight = 16;
 
209
/** picture with closed folder */
 
210
static const char* folder_closed_xpm[]={
 
211
        "16 16 10 1",
 
212
        "       c #0E279A",
 
213
        ".      c #2852AF",
 
214
        "+      c #2B66D3",
 
215
        "@      c #2A83FE",
 
216
        "#      c #627CB1",
 
217
        "$      c #5FA1FC",
 
218
        "%      c #B0B2CA",
 
219
        "&      c #89BEF9",
 
220
        "*      c #CDE9FD",
 
221
        "=      c #FCFFFC",
 
222
        "==++++++========",
 
223
        "=+======+=======",
 
224
        "+==*=*==#++++++=",
 
225
        "+**=*****=*=*=*+",
 
226
        ".********+++++++",
 
227
        ".*******+&====&+",
 
228
        ".*&+++++&=&&&&&+",
 
229
        ".&+$=*===$$$$$$+",
 
230
        ".&+@$@$@$$$$$@$+",
 
231
        ".&.@@@@@@@@@@@@ ",
 
232
        ".& @@@@@@@@@@@@ ",
 
233
        ".& ===========% ",
 
234
        ".& ===========% ",
 
235
        ".$ %%%%%%%%%%%% ",
 
236
        "=.             =",
 
237
        "================"};
 
238
 
 
239
/** picture with open folder */
 
240
static const char* folder_open_xpm[]={
 
241
        "16 16 10 1",
 
242
        "       c #0E279A",
 
243
        ".      c #2852AF",
 
244
        "+      c #2B66D3",
 
245
        "@      c #2A83FE",
 
246
        "#      c #627CB1",
 
247
        "$      c #5FA1FC",
 
248
        "%      c #B0B2CA",
 
249
        "&      c #89BEF9",
 
250
        "*      c #CDE9FD",
 
251
        "=      c #FCFFFC",
 
252
        "==++++++========",
 
253
        "=+======+=======",
 
254
        "+==*=*==#++++++=",
 
255
        "+**=*****=*=*=*+",
 
256
        ".********+++++++",
 
257
        ".*******+&====*+",
 
258
        ".*&+++++#=&&&&&+",
 
259
        ".&+$=*==*$$$$$$+",
 
260
        ".&+$$@$$@$$$$$@ ",
 
261
        ".$+@@@@@@@@@@@@ ",
 
262
        "..+@@@@@@@@@@@+ ",
 
263
        ".+%===========.=",
 
264
        ". %==========% =",
 
265
        ". %%%%%%%%%%%% =",
 
266
        "=              =",
 
267
        "================"};
205
268
 
206
269
/**
207
270
 * Constructor.
208
271
 *
209
 
 * @param file tagged file (will be owned by this item)
210
 
 */
211
 
FileListItem::FileListItem(TaggedFile* file) : m_file(file)
 
272
 * @param parent parent file list
 
273
 * @param after  this item is inserted after item @a after
 
274
 * @param file   tagged file (will be owned by this item)
 
275
 */
 
276
FileListItem::FileListItem(FileList* parent, FileListItem* after,
 
277
                                                                                                         TaggedFile* file) :
 
278
        Q3ListViewItem(parent, after), m_file(file), m_dirInfo(0)
 
279
{
 
280
        init();
 
281
}
 
282
 
 
283
/**
 
284
 * Constructor for non top-level items.
 
285
 *
 
286
 * @param parent parent file list item
 
287
 * @param after  this item is inserted after item @a after
 
288
 * @param file   tagged file (will be owned by this item)
 
289
 */
 
290
FileListItem::FileListItem(FileListItem* parent, FileListItem* after,
 
291
                                                                                                         TaggedFile* file) :
 
292
        Q3ListViewItem(parent, after), m_file(file), m_dirInfo(0)
 
293
{
 
294
        init();
 
295
}
 
296
 
 
297
/**
 
298
 * Initialize file list item.
 
299
 * Common initialization for all constructors.
 
300
 */
 
301
void FileListItem::init()
212
302
{
213
303
        setInSelection(false);
214
304
        if (m_file) {
215
 
                setText(m_file->getFilename());
 
305
                setText(0, m_file->getFilename());
216
306
        }
217
307
 
218
308
        // this two objects should be destructed when the program terminates.
236
326
        if (!notagPixmap) {
237
327
                notagPixmap = new QPixmap((const char **)notag_xpm);
238
328
        }
 
329
        if (!folderClosedPixmap) {
 
330
                folderClosedPixmap = new QPixmap((const char **)folder_closed_xpm);
 
331
        }
 
332
        if (!folderOpenPixmap) {
 
333
                folderOpenPixmap = new QPixmap((const char **)folder_open_xpm);
 
334
        }
 
335
 
 
336
        updateIcons();
239
337
}
240
338
 
241
339
/**
244
342
FileListItem::~FileListItem()
245
343
{
246
344
        delete m_file;
 
345
        delete m_dirInfo;
 
346
}
 
347
 
 
348
/**
 
349
 * Paints the contents of one column of an item.
 
350
 *
 
351
 * @param p      painter
 
352
 * @param cg     color group
 
353
 * @param column number of column
 
354
 * @param width  width
 
355
 * @param align  alignment
 
356
 */
 
357
void FileListItem::paintCell(QPainter* p, const QColorGroup& cg,
 
358
                                                                                                                 int column, int width, int align)
 
359
{
 
360
        if (Kid3App::s_miscCfg.m_markTruncations &&
 
361
                        column == 0 && m_file && m_file->getTruncationFlags() != 0) {
 
362
                QColorGroup g(cg);
 
363
#if QT_VERSION >= 0x040000
 
364
                g.setColor(QPalette::Window, Qt::red);
 
365
#else
 
366
                g.setColor(QColorGroup::Base, Qt::red);
 
367
#endif
 
368
                Q3ListViewItem::paintCell(p, g, column, width, align);
 
369
        } else {
 
370
                Q3ListViewItem::paintCell(p, cg, column, width, align);
 
371
        }
 
372
}
 
373
 
 
374
/**
 
375
 * Opens or closes an item.
 
376
 *
 
377
 * @param o true to open
 
378
 */
 
379
void FileListItem::setOpen(bool o)
 
380
{
 
381
        if (m_dirInfo) {
 
382
                setPixmap(0, o ? *folderOpenPixmap : *folderClosedPixmap);
 
383
 
 
384
                if (o && !childCount()) {
 
385
                        listView()->setUpdatesEnabled(false);
 
386
                        FileList::readSubDirectory(m_dirInfo, this, 0);
 
387
                        listView()->setUpdatesEnabled(true);
 
388
                }
 
389
                updateIcons();
 
390
        }
 
391
        QListViewItem::setOpen(o);
 
392
}
 
393
 
 
394
/**
 
395
 * Called before showing the item.
 
396
 */
 
397
void FileListItem::setup()
 
398
{
 
399
        if (!m_file) {
 
400
                setExpandable(true);
 
401
        }
 
402
        QListViewItem::setup();
247
403
}
248
404
 
249
405
/**
259
415
        }
260
416
        m_file = file;
261
417
        if (m_file) {
262
 
                setText(m_file->getFilename());
263
 
        }
264
 
}
265
 
 
266
 
/**
267
 
 * Get height of item.
268
 
 *
269
 
 * @param lb listbox containing the item
270
 
 *
271
 
 * @return height.
272
 
 */
273
 
int FileListItem::height(const Q3ListBox* lb) const
274
 
{
275
 
        int h = text().isEmpty() ? pixmapHeight :
276
 
                QMAX(pixmapHeight, lb->fontMetrics().lineSpacing() + 1);
277
 
        return QMAX(h, QApplication::globalStrut().height());
278
 
}
279
 
 
280
 
/**
281
 
 * Get width of item.
282
 
 *
283
 
 * @param lb listbox containing the item
284
 
 *
285
 
 * @return width.
286
 
 */
287
 
int FileListItem::width(const Q3ListBox* lb) const
288
 
{
289
 
        if (text().isEmpty()) {
290
 
                return QMAX(pixmapWidth * 2 + 6, QApplication::globalStrut().width());
291
 
        }
292
 
        return QMAX(pixmapWidth * 2 + 6 + lb->fontMetrics().width(text()), QApplication::globalStrut().width());
293
 
}
294
 
 
295
 
/**
296
 
 * Paint item.
297
 
 *
298
 
 * @param painter painter used
299
 
 */
300
 
void FileListItem::paint(QPainter *painter)
301
 
{
302
 
        static const QPixmap *tagpm[] = {
303
 
                notagPixmap, v1Pixmap, v2Pixmap, v1v2Pixmap, nullPixmap
304
 
        };
305
 
        int tagpmIdx;
306
 
        if (!m_file->isTagInformationRead()) {
307
 
                tagpmIdx = 4;
308
 
        } else {
309
 
                tagpmIdx = 0;
310
 
                if (m_file->hasTagV1()) {
311
 
                        tagpmIdx |= 1;
312
 
                }
313
 
                if (m_file->hasTagV2()) {
314
 
                        tagpmIdx |= 2;
315
 
                }
316
 
        }
317
 
        painter->drawPixmap(3, 0, m_file->isChanged() ? *modifiedPixmap : *nullPixmap);
318
 
        painter->drawPixmap(pixmapWidth + 3, 0, *tagpm[tagpmIdx]);
319
 
        if (!text().isEmpty()) {
320
 
                QFontMetrics fm = painter->fontMetrics();
321
 
                painter->drawText(pixmapWidth * 2 + 5,
322
 
                                                  pixmapHeight < fm.height() ?
323
 
                                                  fm.ascent() + fm.leading() / 2 :
324
 
                                                  pixmapHeight / 2 - fm.height() / 2 + fm.ascent(),
325
 
                                                  text());
 
418
                setText(0, m_file->getFilename());
 
419
        }
 
420
        updateIcons();
 
421
}
 
422
 
 
423
/**
 
424
 * Set directory information.
 
425
 * An item can represent a file (file is set) or
 
426
 * a directory (directory information is set).
 
427
 * The item takes ownership of this directory information
 
428
 * and the old information is deleted.
 
429
 *
 
430
 * @param dirInfo directory information
 
431
 */
 
432
void FileListItem::setDirInfo(DirInfo* dirInfo)
 
433
{
 
434
        if (m_dirInfo) {
 
435
                delete m_dirInfo;
 
436
        }
 
437
        m_dirInfo = dirInfo;
 
438
        if (m_dirInfo) {
 
439
                QFileInfo fi(m_dirInfo->getDirname());
 
440
                setText(0, fi.fileName());
 
441
                setOpen(false);
 
442
        }
 
443
}
 
444
 
 
445
/**
 
446
 * Update the icons according to the modificaton state and the tags present.
 
447
 */
 
448
void FileListItem::updateIcons()
 
449
{
 
450
        if (m_file) {
 
451
                if (m_file->isChanged()) {
 
452
                        setPixmap(0, *modifiedPixmap);
 
453
                } else {
 
454
                        static const QPixmap* tagpm[] = {
 
455
                                notagPixmap, v1Pixmap, v2Pixmap, v1v2Pixmap, nullPixmap
 
456
                        };
 
457
                        int tagpmIdx;
 
458
                        if (!m_file->isTagInformationRead()) {
 
459
                                tagpmIdx = 4;
 
460
                        } else {
 
461
                                tagpmIdx = 0;
 
462
                                if (m_file->hasTagV1()) {
 
463
                                        tagpmIdx |= 1;
 
464
                                }
 
465
                                if (m_file->hasTagV2()) {
 
466
                                        tagpmIdx |= 2;
 
467
                                }
 
468
                        }
 
469
                        setPixmap(0, *tagpm[tagpmIdx]);
 
470
                }
 
471
        }
 
472
}
 
473
 
 
474
/**
 
475
 * Update the text according to the file name.
 
476
 */
 
477
void FileListItem::updateText()
 
478
{
 
479
        if (m_file) {
 
480
                setText(0, m_file->getFilename());
 
481
        }
 
482
}
 
483
 
 
484
/**
 
485
 * Set directory name.
 
486
 * Sets a new directory name if the item represents a directory.
 
487
 *
 
488
 * @param dirName new directory name
 
489
 */
 
490
void FileListItem::setDirName(const QString& dirName)
 
491
{
 
492
        if (m_dirInfo) {
 
493
                m_dirInfo->setDirname(dirName);
 
494
                QFileInfo fi(dirName);
 
495
                setText(0, fi.fileName());
 
496
                setOpen(false);
326
497
        }
327
498
}