-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilelistwidget.cpp
178 lines (146 loc) · 5.33 KB
/
filelistwidget.cpp
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
#include "filelistwidget.h"
FileListWidget::FileListWidget(QWidget *parent)
: QWidget(parent) {
/* Initialize the widgets and variables */
_searchBox = new QLineEdit();
_searchBox->setPlaceholderText(tr("Search"));
_searchBox->setAttribute(Qt::WA_MacShowFocusRect, false);
_searchBox->setObjectName("searchLineEdit");
_view = new QListViewDrop;
_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
_view->setAcceptDrops(true);
_view->setDropIndicatorShown(true);
_view->setAttribute(Qt::WA_MacShowFocusRect, false);
connect(_view, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(requestOpenFile(QModelIndex)));
//_proxyModel = new QSortFilterProxyModelFixed();
//_proxyModel->setFilterRole(Qt::DisplayRole);
//_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
//connect(_searchBox, SIGNAL(textChanged(QString)),
// _proxyModel, SLOT(setFilterRegExp(QString)));
_proxyModel = new QIdentityProxyModel;
_view->setModel(_proxyModel);
/* Defining the layout */
_layout = new QVBoxLayout;
_layout->setSpacing(0);
_layout->setMargin(0);
_layout->addWidget(_searchBox);
_layout->addWidget(_view);
this->setLayout(_layout);
}
void FileListWidget::setModel(FileListModel *model) {
_model = model;
connect(_view, SIGNAL(requestAddFiles(QList<QUrl>)),
_model, SLOT(addFiles(QList<QUrl>)));
_proxyModel->setSourceModel(_model);
}
void FileListWidget::connectActions(QMap<QString, QAction *> *actions) {
// General actions
connect(actions->value("add"), SIGNAL(triggered(bool)),
this, SLOT(addElement()));
connect(actions->value("copy"), SIGNAL(triggered(bool)),
this, SLOT(copyElement()));
connect(actions->value("paste"), SIGNAL(triggered(bool)),
this, SLOT(pasteElement()));
connect(actions->value("remove"), SIGNAL(triggered(bool)),
this, SLOT(deleteElement()));
connect(actions->value("find"), SIGNAL(triggered(bool)),
this, SLOT(activateFind()));
// Specific actions
connect(actions->value("previous"), SIGNAL(triggered(bool)),
this, SLOT(previous()));
connect(actions->value("open"), SIGNAL(triggered(bool)),
this, SLOT(requestOpenFile()));
connect(actions->value("openInExplorer"), SIGNAL(triggered(bool)),
this, SLOT(requestOpenFileInFinder()));
}
void FileListWidget::addElement() {
/* QFileDialog does not allow to select files and directories
* A fix would be to reimplement QFileDialog ...
* Prefer drag'n'drop over addActions
*/
QFileDialog *dialog = new QFileDialog();
dialog->setDirectory(QDir::homePath());
QStringList fileNames;
if (dialog->exec())
fileNames = dialog->selectedFiles();
delete dialog;
_model->addFiles(fileNames);
}
void FileListWidget::deleteElement() {
// Removing element from last to first as we operate on a list
QModelIndexList indexes = _view->selectionModel()->selectedRows();
std::sort(indexes.begin(), indexes.end());
QModelIndexList::reverse_iterator it;
for(it = indexes.rbegin(); it != indexes.rend(); ++it) {
_view->model()->removeRow(it->row(), it->parent());
}
}
void FileListWidget::copyElement() {
qDebug() << "Copy on tags to be implemented";
}
void FileListWidget::pasteElement() {
qDebug() << "Paste on tags to be implemented";
}
void FileListWidget::activateFind() {
// If ctrl+f if pressed setting focus in the search field
_searchBox->setFocus();
}
void FileListWidget::previous() {
// The user want to go back to the tag view
delete _model;
emit viewTagList();
}
void openFile(QUrl url) {
QDesktopServices::openUrl(url);
}
void openFile(QString path) {
if (!path.startsWith("file://")) {
path = "file://" + path;
}
openFile(QUrl(path));
}
void FileListWidget::requestOpenFile(QModelIndex index) {
QString path = index.data(FileListModel::MyRoles::PathRole).toString();
openFile(path);
}
bool confirmMultipleOpenings(int len) {
// If trying to open more that thresold file asking for confirmation
static int thresold = 4;
if (len > thresold) {
QMessageBox msgBox;
msgBox.setText(QString("You are about to open %1 file%2.").arg(len).arg(len > 1 ? "s" : ""));
msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Cancel);
return msgBox.exec() == QMessageBox::Ok ? true : false;
}
return true;
}
void FileListWidget::requestOpenFile() {
QModelIndexList indexes = _view->selectionModel()->selectedRows();
if (!confirmMultipleOpenings(indexes.length())) {
return;
}
for (QModelIndex index : indexes) {
requestOpenFile(index);
}
}
void FileListWidget::requestOpenFileInFinder() {
QModelIndexList indexes = _view->selectionModel()->selectedRows();
if (!confirmMultipleOpenings(indexes.length())) {
return;
}
for (QModelIndex index : indexes) {
QString path = index.data(FileListModel::MyRoles::PathRole).toString();
if (!QDir(path).exists()) {
path = path.section('/', 0, -2);
}
openFile(path);
}
}
FileListWidget::~FileListWidget() {
delete _layout;
delete _view;
delete _searchBox;
delete _proxyModel;
}