summaryrefslogtreecommitdiffstats
path: root/archiveview.cpp
blob: 35eaecaaea3fb2b7462eea3ff4ff441c3b9658ca (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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
  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 of the License, or (at your option) any later version.
*/

#include <QComboBox>
#include <QDialog>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QSettings>
#include <QSplitter>
#include <QVBoxLayout>
#include <QHeaderView>
#include <QSpinBox>
#include <QGroupBox>
#include <QTextEdit>
#include <QCompleter>
#include <QStandardItemModel>
#include <QFormLayout>

#include "archiveview.h"
#include "archivecontroller.h"
#include "mappingtablewidget.h"
#include "smglobals.h"
#include "helper.h"

ArchiveView::ArchiveView(QWidget *parent) : QWidget(parent) {
    QSettings s;
    mArchiveModel = static_cast<ArchiveModel*>(SmGlobals::instance()->model("ArchiveModel"));
    connect(mArchiveModel, SIGNAL(needRefresh()), this, SLOT(refreshArchive()));
    connect(mArchiveModel, SIGNAL(databaseError(QString)), this, SLOT(showDatabaseError(QString)));

    mProxy = new ArchiveProxy;
    mProxy->setSourceModel(mArchiveModel);
    mTree = new ArchiveTree;
    mTree->setModel(mProxy);
    mTree->setColumnHidden(ArchiveModel::GenericId, true);
    mTree->setColumnHidden(ArchiveModel::SeriesPartId, true);
    mTree->setColumnHidden(ArchiveModel::SeriesPart, true);
    mTree->setColumnHidden(ArchiveModel::Type, true);
    mTree->setColumnHidden(ArchiveModel::Favorite, true);
    mTree->setColumnHidden(ArchiveModel::Subtitle, true);
    mTree->setColumnHidden(ArchiveModel::Count, true);
    mTree->resizeColumnToContents(ArchiveModel::Name);
    mTree->setSortingEnabled(true);
    mTree->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(mTree, SIGNAL(expanded(QModelIndex)), this, SLOT(expandItem(QModelIndex)));
    connect(mTree, SIGNAL(collapsed(QModelIndex)), this, SLOT(collapseItem(QModelIndex)));

    QString sortOrderName = s.value("archivemodel/sortorder", "Series Name").toString();
    mSortOrder = new QComboBox;
    mSortOrder->addItems(mArchiveModel->availableOrders());
    mSortOrder->setCurrentText(sortOrderName);
    // this is _not_ redundant! when sortOrderName is the first
    // item in the list, nothing will happen otherwise!
    mArchiveModel->setOrder(sortOrderName);
    connect(mSortOrder, SIGNAL(currentIndexChanged(QString)), mArchiveModel, SLOT(setOrder(QString)));
    connect(mArchiveModel, SIGNAL(modelReset()), this, SLOT(doSelection()));

    QLabel *l1 = new QLabel(tr("Filter"));
    mFilter = new QLineEdit;
    QString savedFilter = s.value("archivemodel/filter", QString()).toString();
    connect(mFilter, SIGNAL(returnPressed()), this, SLOT(setFilter()));
    QPushButton *filter = new QPushButton(tr("Filter"));
    connect(filter, SIGNAL(clicked()), this, SLOT(setFilter()));
    QPushButton *clear = new QPushButton(tr("Clear"));
    mFilter->setText(savedFilter);
    connect(clear, SIGNAL(clicked()), this, SLOT(clearFilter()));
    mProxy->setFilter(savedFilter, currentSortOrder());
    QHBoxLayout *filterLayout = new QHBoxLayout;
    filterLayout->addWidget(l1);
    filterLayout->addWidget(mFilter);
    filterLayout->addWidget(filter);
    filterLayout->addWidget(clear);
    QVBoxLayout *treeLayout = new QVBoxLayout;
    treeLayout->addWidget(mSortOrder);
    treeLayout->addLayout(filterLayout);
    treeLayout->addWidget(mTree);
    QWidget *treeWidget = new QWidget;
    treeWidget->setLayout(treeLayout);

    mFiles = new ArchiveFiles;
    mFiles->setItemDelegateForColumn(ArchiveFilesModel::FileType, new FileTypeDelegate(this));
    mFiles->setItemDelegateForColumn(ArchiveFilesModel::DvdNo, new DvdNoDelegate(this));
    mFiles->setItemDelegateForColumn(ArchiveFilesModel::Size, new SizeDelegate(this));
    mFiles->setItemDelegateForColumn(ArchiveFilesModel::SizeDur, new DurationDelegate(this));
    mFiles->setItemDelegateForColumn(ArchiveFilesModel::FileNumber, new EmptyDelegate(this));
    mFiles->setSelectionMode(QAbstractItemView::ExtendedSelection);
    mFiles->setSelectionBehavior(QAbstractItemView::SelectRows);
    mFilesModel = new ArchiveFilesModel(QStringList() << QChar(0x26A7) << tr("Seriespartid") << tr("Filename") << tr("Md5Sum") << tr("Size") << tr("Dvd") << tr("Type") << tr("No") << tr("Q") << tr("Fileid") << tr("S/Dur") << tr("Full Path"), this);
    mFilesProxy = new ArchiveFilesProxy;
    mFilesProxy->setSourceModel(mFilesModel);
    mFiles->setModel(mFilesProxy);

    QHBoxLayout *bottomRightLayout = new QHBoxLayout;
    mActorView = new SmTreeView;
    mActorView->setHeaderHidden(true);
    mActorModel = new QStandardItemModel(this);
    mActorView->setModel(mActorModel);
    QGroupBox *actorBox = new QGroupBox(tr("Actors"));
    QHBoxLayout *actorLayout = new QHBoxLayout;
    actorLayout->addWidget(mActorView);
    actorBox->setLayout(actorLayout);

    mGenreView = new SmTreeView;
    mGenreView->setHeaderHidden(true);
    mGenreModel = new QStandardItemModel(this);
    mGenreView->setModel(mGenreModel);
    QGroupBox *genreBox = new QGroupBox(tr("Genres"));
    QHBoxLayout *genreLayout = new QHBoxLayout;
    genreLayout->addWidget(mGenreView);
    genreBox->setLayout(genreLayout);

    mMetadataView = new QTextEdit;
    mMetadataView->setReadOnly(true);
    QGroupBox *metadataBox = new QGroupBox("Metadata");
    QHBoxLayout *metadataLayout = new QHBoxLayout;
    metadataLayout->addWidget(mMetadataView);
    metadataBox->setLayout(metadataLayout);

    bottomRightLayout->addWidget(actorBox);
    bottomRightLayout->addWidget(genreBox);
    bottomRightLayout->addWidget(metadataBox);
    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout->addWidget(mFiles);
    rightLayout->addLayout(bottomRightLayout);
    QWidget *rightWidget = new QWidget;
    rightLayout->setStretchFactor(mFiles, 3);
    rightWidget->setLayout(rightLayout);

    mTreeSplitter = new QSplitter(Qt::Horizontal);
    mTreeSplitter->addWidget(treeWidget);
    mTreeSplitter->addWidget(rightWidget);
    mTreeSplitter->setStretchFactor(0, 1);
    mTreeSplitter->setStretchFactor(1, 4);
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(mTreeSplitter);

    setLayout(mainLayout);
    initController();
}

int ArchiveView::currentSortOrder() const {
    return mArchiveModel->availableOrdersHash().value(mSortOrder->currentText());
}

void ArchiveView::readSettings(){
    QSettings s;
    QVariantList list;
    list = s.value("archivemodel/expandedgenres").toList();
    mExpandedItems[ArchiveModel::Genre] = list;
    list = s.value("archivemodel/expandedactors").toList();
    mExpandedItems[ArchiveModel::Actor] = list;
    list = s.value("archivemodel/expandedseries").toList();
    mExpandedItems[ArchiveModel::SeriesName] = list;
    mCurrentArchivePath = s.value("archivemodel/selectedseries").toStringList();
    QByteArray splitterState = s.value("archivemodel/vsplitter").toByteArray();
    mTreeSplitter->restoreState(splitterState);
    doSelection();
    mFiles->readHeaderConfig();
    mFiles->setSortingEnabled(true);
}

void ArchiveView::writeSettings() {
    QSettings s;
    s.setValue("archivemodel/sortorder", mSortOrder->currentText());
    s.setValue("archivemodel/filter", mFilter->text());
    s.setValue("archivemodel/expandedgenres", mExpandedItems.value(ArchiveModel::Genre));
    s.setValue("archivemodel/expandedactors", mExpandedItems.value(ArchiveModel::Actor));
    s.setValue("archivemodel/expandedseries", mExpandedItems.value(ArchiveModel::SeriesName));
    s.setValue("archivemodel/selectedseries", mCurrentArchivePath);
    s.setValue("archivemodel/vsplitter", mTreeSplitter->saveState());
    mFiles->writeHeaderConfig();
}

void ArchiveView::refreshArchive(){
    writeSettings();
    mArchiveModel->refresh();
    doSelection();
}

void ArchiveView::doSelection(){
    QVariantList list = mExpandedItems.value(currentSortOrder());
    foreach(QVariant p, list){
        QStringList path = p.toStringList();
        QModelIndexList expand = mArchiveModel->pathToIndex(path);
        foreach(QModelIndex idx, expand){
            mTree->setExpanded(mProxy->mapFromSource(idx), true);
        }
    }
    if(!mCurrentArchivePath.isEmpty()){
        QModelIndexList l = mArchiveModel->pathToIndex(mCurrentArchivePath);
        if(!l.isEmpty()){
            QModelIndex sel = mProxy->mapFromSource(l.last());
            mTree->selectionModel()->select(sel, QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect);
        }
    }
}

void ArchiveView::setFilter(){
    QString filter = mFilter->text();
    mProxy->setFilter(filter, currentSortOrder());
    mTree->expandAll();
}

void ArchiveView::clearFilter(){
    mFilter->clear();
    mProxy->setFilter(QString(), currentSortOrder());
    mTree->collapseAll();
}

void ArchiveView::showDatabaseError(const QString &errorMsg){
    QMessageBox::critical(this, tr("Database Error"), errorMsg);
}

void ArchiveView::expandItem(const QModelIndex &idx){
    QModelIndex realIdx = mProxy->mapToSource(idx);
    if(realIdx.isValid()){
        QStringList path = mArchiveModel->indexToPath(realIdx);
        QVariantList list = mExpandedItems.value(currentSortOrder());
        if(!list.contains(path)){
            list << mArchiveModel->indexToPath(realIdx);
        }
        mExpandedItems[currentSortOrder()] = list;
    }
}

void ArchiveView::collapseItem(const QModelIndex &idx){
    QModelIndex realIdx = mProxy->mapToSource(idx);
    if(realIdx.isValid()){
        QVariantList list = mExpandedItems.value(currentSortOrder());
        list.removeAll(mArchiveModel->indexToPath(realIdx));
        mExpandedItems[currentSortOrder()] = list;
    }
}

void ArchiveView::initController(){
    mController = new ArchiveController(this);
    mController->setArchiveView(this);
    mController->setArchiveTree(mTree, mProxy);
    mController->setArchiveFiles(mFiles, mFilesProxy);
    mController->setModels(mArchiveModel, mFilesModel);
    mController->setMappingModels(mActorModel, mGenreModel);
    mController->setMetadata(mMetadataView);
    mController->init();
    SmGlobals::instance()->setArchiveController(mController);
}

ArchiveTree::ArchiveTree(QWidget *parent) : SmTreeView(parent) {}

void ArchiveTree::setModel(ArchiveProxy *model){
    mProxy = model;
    mModel = qobject_cast<ArchiveModel*>(mProxy->sourceModel());
    QTreeView::setModel(model);
}

void ArchiveTree::rename(){
    QModelIndex idx = currentIndex();
    int nodeType = idx.data(ArchiveModel::TypeRole).toInt();
    if(nodeType == ArchiveModel::SeriesPartNode){
        impossible();
        return;
    }
    QString currentName = idx.data(ArchiveModel::NameRole).toString();
    QString question = QString(tr("Rename %1 to:")).arg(currentName);
    QString newName = QInputDialog::getText(this, tr("Rename"), question, QLineEdit::Normal, currentName);
    if(!newName.isEmpty()){
        QModelIndex realIdx = mProxy->mapToSource(idx);
        mModel->setData(realIdx, newName, Qt::EditRole);
    }
}

void ArchiveTree::remove(){
    QModelIndex idx = currentIndex();
    QModelIndex realIdx = mProxy->mapToSource(idx);
    QString warningMsg = QString("Really remove %1?").arg(idx.data().toString());
    int retval = QMessageBox::warning(this, tr("Question"), warningMsg, QMessageBox::Yes | QMessageBox::No);
    if(retval == QMessageBox::Yes){
        if(!mModel->removeNode(realIdx)){
            impossible();
        }
    }
}

void ArchiveTree::editActors(){
    MappingEditor e(tr("Actors"), this);
    QStringList actors = mModel->allActors();
    e.fillCompleter(actors);
    QModelIndex idx = currentIndex();
    int pId = idx.data(ArchiveModel::SeriesPartIdRole).toInt();
    QStringList curActors = mModel->actors(QSet<int>() << pId);
    e.setCurrentItems(curActors);
    int res = e.exec();
    if(res == QDialog::Accepted){
        QStringList selActors = e.items();
        mModel->setActors(pId, selActors);
        ArchiveController *c = SmGlobals::instance()->archiveController();
        c->treeSelectionChanged(QItemSelection(), QItemSelection());
    }
}

void ArchiveTree::editGenres(){
    MappingEditor e(tr("Genres"), this);
    QStringList genres = mModel->allGenres();
    e.fillCompleter(genres);
    QModelIndex idx = currentIndex();
    int pId = idx.data(ArchiveModel::SeriesPartIdRole).toInt();
    QStringList curGenres = mModel->genres(QSet<int>() << pId);
    e.setCurrentItems(curGenres);
    int res = e.exec();
    if(res == QDialog::Accepted){
        QStringList selGenres = e.items();
        mModel->setGenres(pId, selGenres);
        ArchiveController *c = SmGlobals::instance()->archiveController();
        c->treeSelectionChanged(QItemSelection(), QItemSelection());
    }
}

void ArchiveTree::editMetadata(){
    MetadataEditor e(this);
    ArchiveController *c = SmGlobals::instance()->archiveController();
    QStringList sources = c->archiveTreeModel()->allSources();
    e.setSources(sources);
    QStringList relGroups = c->archiveTreeModel()->allReleaseGroups();
    e.setReleaseGroups(relGroups);
    QModelIndex idx = currentIndex();
    int pId = idx.data(ArchiveModel::SeriesPartIdRole).toInt();
    QList<QVariant> curMetadata = c->archiveTreeModel()->metadataList(pId);
    e.setMetadata(curMetadata);
    int res = e.exec();
    if(res == QDialog::Accepted){
        QList<QVariant> data = e.metadata();
        mModel->setMetadata(pId, data);
        c->treeSelectionChanged(QItemSelection(), QItemSelection());
    }
}


void ArchiveTree::impossible(const QString msg){
    QMessageBox::critical(this, tr("Error"), msg);
}

ArchiveFiles::ArchiveFiles(const QString &headerSettings, QWidget *parent) : SmTreeView(headerSettings, parent){
    setEditTriggers(QAbstractItemView::NoEditTriggers);
}

ArchiveProxy::ArchiveProxy(QObject *parent) : QSortFilterProxyModel(parent) {}

void ArchiveProxy::setFilter(const QString &filter, int sortOrder){
    mFilter = QRegExp(filter);
    mSortOrder = sortOrder;
    invalidateFilter();
}

bool ArchiveProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
    if(mFilter.isEmpty()){
        return true;
    }
    QModelIndex nameIdx = sourceModel()->index(sourceRow, ArchiveModel::Name, sourceParent);
    ArchiveModel *model = qobject_cast<ArchiveModel*>(sourceModel());
    return model->matchRecursive(nameIdx, mFilter);
}

ArchiveFilesProxy::ArchiveFilesProxy(QObject *parent) : QSortFilterProxyModel(parent) {}

/* Delegate for file type */

FileTypeDelegate::FileTypeDelegate(QObject *parent) : QStyledItemDelegate(parent){
    mFiletypeMap = SmGlobals::instance()->filetypeMap();
}

QString FileTypeDelegate::displayText(const QVariant &value, const QLocale &locale) const{
    Q_UNUSED(locale);
    int type = value.toInt();
    QString retval = mFiletypeMap.value(type);
    if(!retval.isEmpty()){
        return retval;
    }
    return tr("n/a");
}

QWidget *FileTypeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    Q_UNUSED(option);
    Q_UNUSED(index);
    QComboBox *retval = new QComboBox(parent);
    retval->addItems(mFiletypeMap.values());
    return retval;
}

/* Mapping Editor */

MappingEditor::MappingEditor(const QString &caption, QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f) {
    mModel = new QStandardItemModel(this);
    mModel->setHorizontalHeaderLabels(QStringList() << caption);
    mProxy = new QSortFilterProxyModel(this);
    mProxy->setSourceModel(mModel);
    QVBoxLayout *viewLayout = new QVBoxLayout;
    mView = new SmTreeView;
    viewLayout->addWidget(mView);
    mView->setModel(mProxy);
    mView->setSortingEnabled(true);

    QHBoxLayout *editLayout = new QHBoxLayout;
    QLabel *l2 = new QLabel(tr("Add:"));
    mEditor = new QLineEdit;
    mCompleter = new QCompleter(this);
    mCompleterModel = new QStringListModel(this);
    mCompleter->setModel(mCompleterModel);
    mEditor->setCompleter(mCompleter);
    editLayout->addWidget(l2);
    editLayout->addWidget(mEditor);

    QHBoxLayout *editButtonLayout = new QHBoxLayout;
    mRemove = new QPushButton(tr("Remove"));
    connect(mRemove, SIGNAL(clicked()), this, SLOT(removeItem()));
    editButtonLayout->addWidget(mRemove);
    mAdd = new QPushButton(tr("Add"));
    connect(mAdd, SIGNAL(clicked()), this, SLOT(addItem()));
    editButtonLayout->addWidget(mAdd);

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch();
    mCancel = new QPushButton(tr("Cancel"));
    connect(mCancel, SIGNAL(clicked()), this, SLOT(reject()));
    buttonLayout->addWidget(mCancel);
    mAccept = new QPushButton(tr("Accept"));
    connect(mAccept, SIGNAL(clicked()), this, SLOT(accept()));
    buttonLayout->addWidget(mAccept);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(viewLayout);
    mainLayout->addLayout(editLayout);
    mainLayout->addLayout(editButtonLayout);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);
    setMinimumWidth(400);
}

void MappingEditor::fillCompleter(const QStringList &completions){
    mCompleterModel->setStringList(completions);
}

void MappingEditor::setCurrentItems(const QStringList &items){
    foreach(QString i, items){
        QStandardItem *item = new QStandardItem(i);
        item->setIcon(QIcon(":/huge_bra.png"));
        mModel->appendRow(item);
    }
}

QStringList MappingEditor::items() const{
    QStringList retval;
    for(int i = 0; i < mModel->rowCount(); ++i){
        QStandardItem *item = mModel->item(i);
        retval << item->text();
    }
    return retval;
}

void MappingEditor::addItem(){
    QString itemName = mEditor->text();
    if(itemName.isEmpty()){
        return;
    }
    QList<QStandardItem*> found = mModel->findItems(itemName, Qt::MatchFixedString);
    if(found.size()){
        return;
    }
    QStandardItem *newItem = new QStandardItem(itemName);
    newItem->setIcon(QIcon(":/huge_bra.png"));
    mModel->appendRow(newItem);
    mEditor->clear();
}

void MappingEditor::removeItem(){
    QModelIndexList sel = mView->selectionModel()->selectedRows();
    if(sel.isEmpty()){
        return;
    }
    QModelIndex real = mProxy->mapToSource(sel.first());
    mModel->removeRow(real.row());
}

/* Metadata Editor */

MetadataEditor::MetadataEditor(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
    QLabel *captionL = new QLabel(tr("<b>Edit Metadata</b>"));

    QFormLayout *dataLayout = new QFormLayout;
    mReleaseYear = new QSpinBox;
    mReleaseYear->setMinimum(1900);
    mReleaseYear->setMaximum(3000);
    dataLayout->addRow(tr("&Release Year"), mReleaseYear);
    mSource = new QComboBox;
    mSource->setEditable(true);
    dataLayout->addRow(tr("&Source"), mSource);
    mRelGroup = new QComboBox;
    mRelGroup->setEditable(true);
    dataLayout->addRow(tr("Release &Group"), mRelGroup);
    mSubject = new QLineEdit;
    dataLayout->addRow(tr("Sub&ject"), mSubject);
    mEncoderOpts = new QLineEdit;
    dataLayout->addRow(tr("&Encoder Opts."), mEncoderOpts);
    mPasses = new QSpinBox;
    mPasses->setMinimum(0);
    mPasses->setMaximum(10);
    dataLayout->addRow(tr("&Passes"), mPasses);

    QVBoxLayout *commentLayout = new QVBoxLayout;
    QLabel *commentL = new QLabel(tr("<b>Comment</b>"));
    commentLayout->addWidget(commentL);
    mComment = new QTextEdit;
    commentLayout->addWidget(mComment);

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch();
    mCancel = new QPushButton(tr("Cancel"));
    connect(mCancel, SIGNAL(clicked()), this, SLOT(reject()));
    buttonLayout->addWidget(mCancel);
    mAccept = new QPushButton(tr("Accept"));
    connect(mAccept, SIGNAL(clicked()), this, SLOT(accept()));
    buttonLayout->addWidget(mAccept);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(captionL);
    mainLayout->addLayout(dataLayout);
    mainLayout->addLayout(commentLayout);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);
    setMinimumWidth(400);
}

void MetadataEditor::setSources(const QStringList &sources){
    mSource->addItems(sources);
}

void MetadataEditor::setReleaseGroups(const QStringList &relGroups){
    mRelGroup->addItems(relGroups);
}

void MetadataEditor::setMetadata(const QList<QVariant> &data){
    mReleaseYear->setValue(data.at(ArchiveModel::ReleaseYear).toInt());
    mSource->setCurrentText(data.at(ArchiveModel::Source).toString());
    mSubject->setText(data.at(ArchiveModel::Subject).toString());
    mRelGroup->setCurrentText(data.at(ArchiveModel::ReleaseGroup).toString());
    mEncoderOpts->setText(data.at(ArchiveModel::EncoderOpts).toString());
    mPasses->setValue(data.at(ArchiveModel::Passes).toInt());
    mComment->setText(data.at(ArchiveModel::Comment).toString());
    mAdded = data.at(ArchiveModel::Added).toDate();
}

QList<QVariant> MetadataEditor::metadata() const{
    QList<QVariant> retval;
    for(int i = 0; i < ArchiveModel::MetadataNumFields; ++i){
        retval << QVariant();
    }
    retval[ArchiveModel::ReleaseYear] = mReleaseYear->value();
    retval[ArchiveModel::Source] = mSource->currentText();
    retval[ArchiveModel::Subject] = mSubject->text();
    retval[ArchiveModel::ReleaseGroup] = mRelGroup->currentText();
    retval[ArchiveModel::EncoderOpts] = mEncoderOpts->text();
    retval[ArchiveModel::Passes] = mPasses->value();
    retval[ArchiveModel::Comment] = mComment->toPlainText();
    retval[ArchiveModel::Added] = mAdded;
    return retval;
}

/* Delegate for File no. */

QString FileNoDelegate::displayText(const QVariant &value, const QLocale &locale) const{
    Q_UNUSED(locale);
    int no = value.toInt();
    if(no){
        return QString::number(no);
    }
    return QString();
}

QWidget *FileNoDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    Q_UNUSED(option);
    QSpinBox *retval = new QSpinBox(parent);
    int no = index.data().toInt();
    retval->setValue(no);
    return retval;
}

/* Delegate for Dvd no. */

QString DvdNoDelegate::displayText(const QVariant &value, const QLocale &locale) const{
    Q_UNUSED(locale);
    int no = value.toInt();
    if(no < 0){
        return tr("(local)");
    }
    QString retval = QString(tr("#%1")).arg(QString::number(no));
    return retval;
}

/* Delegate for size */

QString SizeDelegate::displayText(const QVariant &value, const QLocale &locale) const{
    // locale.toString() doesn't work, maybe b/c it's const
    Q_UNUSED(locale);
    QLocale l;
    return l.toString(value.toLongLong());
}

/* Delegate for duration */

QString DurationDelegate::displayText(const QVariant &value, const QLocale &locale) const{
    Q_UNUSED(locale);
    if(value.toString().contains("x")){
        return value.toString();
    }
    qint64 secs = value.toInt();
    if(secs == 0){
        return tr("n/a");
    }
    Helper::Duration dur(secs);
    return dur.toString();
}

/* Empty delegate */

QString EmptyDelegate::displayText(const QVariant &value, const QLocale &locale) const{
    int i = value.toInt();
    if(i < 1){
        return QString();
    }
    return QStyledItemDelegate::displayText(value, locale);
}