summaryrefslogtreecommitdiffstats
path: root/fileview.cpp
blob: 88cc15c67a7fb4698270d015e0c290ea90ce23e3 (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
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
/*
  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 <QAction>
#include <QApplication>
#include <QDebug>

#include "fileview.h"
#include "messagedialog.h"

FileView::FileView(QWidget *parent) : QTreeView(parent), mDeleteA(0) {
	setRootIsDecorated(false);
	QString title = QString("%1 - %2");
	QString markTitle = title.arg(qApp->applicationName(), tr("Mark files"));
	mMarkDialog = new MessageDialog(tr("Enter pattern to mark"), markTitle, this);
	connect(mMarkDialog, SIGNAL(accepted()), this, SLOT(doMark()));
	QString folderTitle = title.arg(qApp->applicationName(), tr("Create folder"));
	mCreateFolderDialog = new MessageDialog(tr("Enter folder name"), folderTitle, this);
	connect(mCreateFolderDialog, SIGNAL(accepted()), this, SLOT(doCreateFolder()));
}

void FileView::findActions(){
	foreach(QAction *a, actions()){
		if(a->data().toString() == "DA"){
			mDeleteA = a;
		}
	}
	Q_ASSERT(mDeleteA != 0);
}

void FileView::markFiles(){
	mMarkDialog->show();
}

void FileView::unmarkFiles(){
	selectionModel()->clearSelection();
}

void FileView::createFolder(){
	mCreateFolderDialog->show();
}

void FileView::refresh(){
	QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(model());
	QDirModel *model = static_cast<QDirModel*>(proxy->sourceModel());
	QModelIndex root = rootIndex();
	QModelIndex real = proxy->mapToSource(root);
	model->refresh(root);
}

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);
	QMenu renameMenu(tr("Rename..."));
	QMenu extractMenu(tr("Extract To..."));
	int ctr(0);
	foreach(QAction *a, actions()){
		if(a->data() == "RenameMenu"){
			renameMenu.addAction(a);
		}else if(a->data() == "ExtractMenu"){
			extractMenu.addAction(a);
		}else{
			contextMenu.addAction(a);
			if((ctr == 0) || (ctr == 1) || (ctr == 3)){
				contextMenu.addSeparator();
			}
			if(ctr == 8){
				contextMenu.addMenu(&renameMenu);
				contextMenu.addSeparator();
				contextMenu.addMenu(&extractMenu);
			}
			++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;
		case Qt::Key_Delete:
			mDeleteA->trigger();
			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);
	}
}