summaryrefslogtreecommitdiffstats
path: root/archivebrowser.cpp
blob: e6a1efb7ded666cdf7d131fa8238434a524eb78a (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
/*
  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 <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QCheckBox>
#include <QSortFilterProxyModel>

#include "archivebrowser.h"
#include "archivebrowsermodel.h"
#include "smtreeview.h"
#include "smglobals.h"
#include "delegates.h"

ArchiveBrowser::ArchiveBrowser(QWidget *parent) : QWidget(parent), mSelectedSize(0), mSelectedItems(0){
    //prep
    mModel = static_cast<ArchiveBrowserModel*>(SmGlobals::instance()->model("BrowserModel"));
    mProxy = new ArchiveBrowserModelProxy;
    mProxy->setSourceModel(mModel);
    mTree = new SmTreeView;
    mTree->setModel(mProxy);
    mTree->setColumnHidden(ArchiveBrowserModel::GenericId, true);
    mTree->setColumnHidden(ArchiveBrowserModel::NodeType, true);

    mTree->setItemDelegateForColumn(ArchiveBrowserModel::TotalSize, new SizeDelegate(this));
    mTree->setItemDelegateForColumn(ArchiveBrowserModel::FileType, new FileTypeDelegate(this));
    mTree->setSelectionMode(QAbstractItemView::ExtendedSelection);

    //filters
    QHBoxLayout *filterLayout = new QHBoxLayout;
    mQualityFilter = new QComboBox;
    QLabel *filterL = new QLabel(tr("Filters:"));
    QLabel *qualityL = new QLabel(tr("Quality"));
    filterLayout->addWidget(filterL);
    filterLayout->addWidget(qualityL);
    setupQualityFilter();
    connect(mQualityFilter, SIGNAL(currentIndexChanged(QString)), mProxy, SLOT(setQualityFilter(QString)));
    filterLayout->addWidget(mQualityFilter);
    mSizeFilter = new QCheckBox(tr("Size Filter"));
    connect(mSizeFilter, SIGNAL(stateChanged(int)), mProxy, SLOT(setSizeFilter(int)));
    filterLayout->addWidget(mSizeFilter);
    filterLayout->addStretch();

    //connect
    connect(mTree->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(browserSelectionChanged(QItemSelection,QItemSelection)));

    //make widget
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(filterLayout);
    mainLayout->addWidget(mTree);
    setLayout(mainLayout);
    mTree->setSortingEnabled(true);
}

void ArchiveBrowser::browserSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
    QModelIndexList selectedIdx = selectedRows(selected);
    QModelIndexList deselectedIdx = selectedRows(deselected);
    foreach(QModelIndex sel, selectedIdx){
        mSelectedSize += sel.data(ArchiveBrowserModel::TotalSizeRole).toDouble();
        ++mSelectedItems;
        mModel->setData(sel, true, ArchiveBrowserModel::SelectedRole);
    }
    foreach(QModelIndex desel, deselectedIdx){
        mSelectedSize -= desel.data(ArchiveBrowserModel::TotalSizeRole).toDouble();
        --mSelectedItems;
        mModel->setData(desel, false, ArchiveBrowserModel::SelectedRole);
    }
    emit sizeChanged(mSelectedSize);
    emit itemCountChanged(mSelectedItems);
    qint64 remaining = Q_INT64_C(1024 * 1024 * 1024 * 4) - mSelectedSize;
    mProxy->setBytesRemaining(remaining);
}

void ArchiveBrowser::setupQualityFilter(){
    mQualityFilter->clear();
    QList<int> qualities = mModel->availableQualities();
    qSort(qualities);
    QStringList qualityList = QStringList() << tr("(none)");
    foreach(int q, qualities){
        qualityList << QString::number(q);
    }
    mQualityFilter->addItems(qualityList);
}

QModelIndexList ArchiveBrowser::selectedRows(const QItemSelection &sel){
    QModelIndexList retval;
    QModelIndexList selIdx = sel.indexes();
    foreach(QModelIndex idx, selIdx){
        QModelIndex real = mProxy->mapToSource(idx);
        if(real.column() == 0){
            retval << real;
        }
    }
    return retval;
}