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
|
/*
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 <QContextMenuEvent>
#include <QMenu>
#include <QAction>
#include <QKeyEvent>
#include <QModelIndex>
#include <QRegExp>
#include <QDirModel>
#include <QSortFilterProxyModel>
#include <QDebug>
#include "fileview.h"
#include "messagedialog.h"
FileView::FileView(QWidget *parent) : QTreeView(parent) {
setRootIsDecorated(false);
mMarkDialog = new MessageDialog(tr("Enter pattern to mark"), this);
connect(mMarkDialog, SIGNAL(accepted()), this, SLOT(doMark()));
mCreateFolderDialog = new MessageDialog(tr("Enter folder name"), this);
connect(mCreateFolderDialog, SIGNAL(accepted()), this, SLOT(doCreateFolder()));
}
void FileView::markFiles(){
mMarkDialog->show();
}
void FileView::unmarkFiles(){
selectionModel()->clearSelection();
}
void FileView::createFolder(){
mCreateFolderDialog->show();
}
void FileView::doMark(){
int rowCount = model()->rowCount(rootIndex());
QString sRegex = mMarkDialog->text();
if(rowCount && !sRegex.isEmpty()){
QRegExp re(sRegex);
QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(model());
QDirModel *model = static_cast<QDirModel*>(proxy->sourceModel());
bool match(false);
for(int i = 0; i < rowCount; ++i){
QModelIndex cur = rootIndex().child(i, 0);
QModelIndex sCur = proxy->mapToSource(cur);
if(model->isDir(sCur)){
continue;
}
if(re.indexIn(cur.data().toString()) != -1){
selectionModel()->select(cur, QItemSelectionModel::Select | QItemSelectionModel::Rows);
match = true;
}
}
if(match){
statusbarMessage(QString());
}else{
emit statusbarMessage(tr("No match found!"));
}
}else{
emit statusbarMessage(tr("Nothing to mark!"));
}
}
void FileView::doCreateFolder(){
QString folderName = mCreateFolderDialog->text();
if(folderName.isEmpty()){
emit statusbarMessage(tr("No foldername given!"));
return;
}
QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(model());
QDirModel *model = static_cast<QDirModel*>(proxy->sourceModel());
QModelIndex sRoot = proxy->mapToSource(rootIndex());
QModelIndex newIdx = model->mkdir(sRoot, folderName);
if(newIdx == QModelIndex()){
QString msg = QString(tr("Failed to create %1/%2")).arg(model->filePath(sRoot)).arg(folderName);
emit statusbarMessage(msg);
}else{
QString msg = QString(tr("Created folder %1")).arg(model->filePath(newIdx));
emit statusbarMessage(msg);
}
}
void FileView::contextMenuEvent(QContextMenuEvent *e){
QMenu contextMenu(this);
int ctr(0);
foreach(QAction *a, actions()){
contextMenu.addAction(a);
if((ctr == 1)){
contextMenu.addSeparator();
}
++ctr;
}
contextMenu.exec(e->globalPos());
}
void FileView::keyPressEvent(QKeyEvent *e){
switch(e->key()){
case Qt::Key_Backspace:
emit upDir();
e->accept();
break;
case Qt::Key_Enter:
case Qt::Key_Return:
emit enterPressed(currentIndex());
e->accept();
break;
default:
QTreeView::keyPressEvent(e);
}
}
void FileView::resizeEvent(QResizeEvent *e){
if(e->size().width() != e->oldSize().width()){
int width = e->size().width();
int c1width = width / 2; // * 2;
setColumnWidth(0, c1width);
}
}
|