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
|
/*
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 <QHBoxLayout>
#include <QSettings>
#include <QMessageBox>
#include <QFile>
#include <QDir>
#include <QContextMenuEvent>
#include <QMenu>
#include <QDebug>
#include "filestreewidget.h"
#include "smmodelsingleton.h"
#include "filestreemodel.h"
#include "seriestreemodel.h"
#include "helper.h"
FilesTreeWidget::FilesTreeWidget(QWidget *parent) : QWidget(parent), mSelectedSize(0){
//the view
mView = new FilesTreeView;
mView->setSelectionMode(QAbstractItemView::ExtendedSelection);
mModel = static_cast<FilesTreeModel*>(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<SeriesTreeModel*>(SmModelSingleton::instance()->model("SeriesModel"));
}
void FilesTreeWidget::moveToBurn(){
QModelIndexList selected = mView->selectionModel()->selectedRows();
if(selected.isEmpty()){
return;
}
QSettings s;
QString burnDir = s.value("paths/burn").toString();
if(burnDir.isEmpty()){
QMessageBox::critical(this, tr("Error"), tr("No target directory configured."));
return;
}
foreach(QModelIndex i, selected){
int type = i.data(FilesTreeModel::FileTypeRole).toInt();
if(type == FilesTreeModel::BackCover || type == FilesTreeModel::FrontCover || type == FilesTreeModel::GeneralCover){
continue;
}
int seriesPartId = i.data(FilesTreeModel::SeriesPartIdRole).toInt();
int seriesId = mSeriesModel->seriesIdByPartId(seriesPartId);
if(seriesId != -1){
QModelIndex seriesIdx = mSeriesModel->findValue(seriesId, QModelIndex(), SeriesTreeModel::SeriesId);
if(seriesIdx.isValid()){
QString seriesName = seriesIdx.data(SeriesTreeModel::NameRole).toString();
QString dirName = seriesName.replace(' ', ".");
QDir target(burnDir);
target.mkdir(dirName);
const QHash<QString, QString> files = mModel->filesBySeriesPartId(seriesPartId);
foreach(QString name, files.keys()){
QString sourceFile = Helper::createArchivePath(name, files.value(name));
QString targetFile = QString("%1/%2/%3").arg(burnDir).arg(dirName).arg(name);
if(name == i.data(FilesTreeModel::FileNameRole).toString()){
QFile::rename(sourceFile, targetFile);
}else{
QFile::copy(sourceFile, targetFile);
}
}
}
}
}
}
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){}
void FilesTreeView::contextMenuEvent(QContextMenuEvent *event){
QMenu ctxMenu;
foreach(QAction *a, actions()){
ctxMenu.addAction(a);
}
ctxMenu.exec(event->globalPos());
}
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);
}
|