/* 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 #include #include "filestreewidget.h" #include "smmodelsingleton.h" #include "filestreemodel.h" #include "seriestreemodel.h" FilesTreeWidget::FilesTreeWidget(QWidget *parent) : QWidget(parent), mSelectedSize(0){ //the view mView = new FilesTreeView; mView->setSelectionMode(QAbstractItemView::ExtendedSelection); mModel = static_cast(SmModelSingleton::instance()->model("FilesModel")); mProxy = new FilesTreeSortModel(this); mProxy->setSourceModel(mModel); mView->setModel(mProxy); mView->setSortingEnabled(true); QItemSelectionModel *selModel = mView->selectionModel(); connect(selModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(fileSelectionChanged(QModelIndex,QModelIndex))); connect(selModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(fileSelectionChanged(QItemSelection,QItemSelection))); //layout QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addWidget(mView); setLayout(mainLayout); //series model mSeriesModel = static_cast(SmModelSingleton::instance()->model("SeriesModel")); } void FilesTreeWidget::fileSelectionChanged(const QModelIndex ¤t, const QModelIndex &previous){ Q_UNUSED(previous); int seriesPartId = current.data(FilesTreeModel::SeriesPartIdRole).toInt(); int seriesId = mSeriesModel->seriesIdByPartId(seriesPartId); int filePart = current.data(FilesTreeModel::PartNoRole).toInt(); QModelIndex seriesIdx = mSeriesModel->findValue(seriesId, QModelIndex(), SeriesTreeModel::SeriesId); if(seriesIdx.isValid()){ QModelIndex seriesPartIdx = mSeriesModel->findValue(seriesPartId, seriesIdx, SeriesTreeModel::SeriesPartId); QString seriesNumber = QString::number(seriesPartIdx.data(SeriesTreeModel::SeriesPartRole).toInt()); QString msg; if(filePart != 0){ msg = QString(tr("%1 %2 (%3)")).arg(seriesIdx.data(SeriesTreeModel::NameRole).toString()).arg(seriesNumber).arg(filePart); }else{ msg = QString(tr("%1 %2")).arg(seriesIdx.data(SeriesTreeModel::NameRole).toString()).arg(seriesNumber); } emit statusMessage(msg); } } void FilesTreeWidget::fileSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected){ QModelIndexList sel = selected.indexes(); QModelIndexList desel = deselected.indexes(); foreach(QModelIndex i, sel){ if(i.column() == 0){ mSelectedSize += i.data(FilesTreeModel::SizeRole).toLongLong(); } } foreach(QModelIndex i, desel){ if(i.column() == 0){ mSelectedSize -= i.data(FilesTreeModel::SizeRole).toLongLong(); } } emit sizeChanged(mSelectedSize); } FilesTreeView::FilesTreeView(QWidget *parent) : QTreeView(parent){} FilesTreeSortModel::FilesTreeSortModel(QObject *parent) : QSortFilterProxyModel(parent) {} bool FilesTreeSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const{ if(left.parent() == QModelIndex()){ return false; } if(left.column() == FilesTreeModel::SizeDisplay){ return left.data(FilesTreeModel::SizeRole).toLongLong() < right.data(FilesTreeModel::SizeRole).toLongLong(); } if(left.column() == FilesTreeModel::DvdNoRole){ return left.data(FilesTreeModel::DvdNoRole).toInt() < right.data(FilesTreeModel::DvdNoRole).toInt(); } return QSortFilterProxyModel::lessThan(left, right); }