summaryrefslogtreecommitdiffstats
path: root/filesystemwidget.cpp
blob: c424eaa9f587c0f9dbb8bea8d270ce687e1a4e07 (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
165
/*
  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 <QDirModel>
#include <QTreeView>
#include <QSettings>
#include <QDir>
#include <QSplitter>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QLabel>
#include <QCompleter>
#include <QProcess>
#include <QApplication>
#include <QDebug>

#include "filesystemwidget.h"
#include "filesystemdirproxy.h"
#include "fileview.h"
#include "shemoviconprovider.h"
#include "filesystemfileproxy.h"
#include "helper.h"

FilesystemWidget::FilesystemWidget(QWidget *parent) : QWidget(parent) {
	mModel = new QDirModel;
	mModel->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
	mModel->setSorting(QDir::DirsFirst | QDir::IgnoreCase);
	mModel->setReadOnly(false);
	SheMovIconProvider *p = new SheMovIconProvider;
	mModel->setIconProvider(p);

	mDirProxy = new FilesystemDirProxy;
	mDirProxy->setSourceModel(mModel);
	mDirView = new QTreeView;
	mDirView->setModel(mDirProxy);
	mDirView->setColumnHidden(1, true);
	mDirView->setColumnHidden(2, true);
	mDirView->setColumnHidden(3, true);
	mDirView->setRootIsDecorated(false);
	mDirView->setSelectionMode(QAbstractItemView::SingleSelection);

	mFileView = new FileView;
	mFileProxy = new FilesystemFileProxy;
	mFileProxy->setSourceModel(mModel);
	mFileView->setModel(mFileProxy);
	mFileView->setSortingEnabled(true);
	mFileView->sortByColumn(0, Qt::AscendingOrder);
	mFileView->setItemsExpandable(false);
	mFileView->setSelectionMode(QAbstractItemView::MultiSelection);

	QWidget *fileWidget = new QWidget;
	QHBoxLayout *directoryEdit = new QHBoxLayout;
	QLabel *dirLabel = new QLabel(tr("&Directory"));
	mDirEdit = new QLineEdit;
	QCompleter *completer = new QCompleter(this);
	completer->setModel(mModel);
	completer->setCompletionMode(QCompleter::PopupCompletion);
	mDirEdit->setCompleter(completer);
	dirLabel->setBuddy(mDirEdit);
	directoryEdit->addWidget(dirLabel);
	directoryEdit->addWidget(mDirEdit);
	QVBoxLayout *fwLayout = new QVBoxLayout;
	fwLayout->addLayout(directoryEdit);
	fwLayout->addWidget(mFileView);
	fileWidget->setLayout(fwLayout);

	connect(mDirView->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(directoryChanged(const QModelIndex &, const QModelIndex &)));
	connect(mFileView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(fileViewActivated(const QModelIndex &)));
	connect(mFileView, SIGNAL(enterPressed(const QModelIndex &)), this, SLOT(fileViewActivated(const QModelIndex &)));
	connect(mFileView, SIGNAL(upDir()), this, SLOT(parentDir()));
	connect(mDirEdit, SIGNAL(returnPressed()), this, SLOT(directoryEdited()));

	QSettings s;
	QString startDir = s.value("paths/start", QDir::homePath()).toString();
	windowTitle(startDir);
	QModelIndex startIndex = mModel->index(startDir);
	if(startIndex.isValid()){
		QModelIndex proxyIndex = mDirProxy->mapFromSource(startIndex);
		mDirView->setCurrentIndex(proxyIndex);
		mDirView->setExpanded(proxyIndex, true);
		QModelIndex parent = proxyIndex.parent();
		do {
			mDirView->setExpanded(parent, true);
			parent = parent.parent();
		}while(parent.isValid());
	}

	mFileView->resizeColumnToContents(0);
	setWindowTitle(startDir);
	
	QVBoxLayout *mainLayout = new QVBoxLayout;
	QSplitter *splitter = new QSplitter;
	splitter->addWidget(mDirView);
	splitter->addWidget(fileWidget);
	splitter->setStretchFactor(0, 1);
	splitter->setStretchFactor(1, 2);
	mainLayout->addWidget(splitter);

	setLayout(mainLayout);
}

void FilesystemWidget::directoryChanged(const QModelIndex &selected, const QModelIndex &deselected){
	QModelIndex real = mDirProxy->mapToSource(selected);
	if(!real.isValid()){
		return;
	}
	mDirEdit->setText(mModel->filePath(real));
	setWindowTitle(mModel->filePath(real));
	QModelIndex oldSelected = mDirProxy->mapToSource(deselected);
	mFileView->selectionModel()->setCurrentIndex(mFileProxy->mapFromSource(oldSelected), QItemSelectionModel::NoUpdate);
	mFileView->setRootIndex(mFileProxy->mapFromSource(real));
}

void FilesystemWidget::directoryEdited(){
	QString path = mDirEdit->text();
	if(path.isEmpty()){
		return;
	}
	QModelIndex index = mModel->index(path);
	if(index.isValid()){
		mDirView->setCurrentIndex(mDirProxy->mapFromSource(index));
	}
	mFileView->setFocus(Qt::ActiveWindowFocusReason);
}

void FilesystemWidget::fileViewActivated(const QModelIndex &idx){
	QModelIndex real = mFileProxy->mapToSource(idx);
	if(mModel->isDir(real)){
		mDirView->setCurrentIndex(mDirProxy->mapFromSource(real));
		return;
	}
	QString path = mModel->filePath(real);
	QString mt = Helper::mimeType(path);
	QSettings s;
	QStringList programArgs;
	QString program;
	if(mt.toLower().startsWith("video")){
		program = s.value("paths/video").toString();
		programArgs = s.value("paths/videoargs").toStringList();
	}
	if(mt.toLower().startsWith("image")){
		program = s.value("paths/picture").toString();
	}
	programArgs << path;
	QProcess::execute(program, programArgs);
}

void FilesystemWidget::parentDir(){
	QModelIndex idx = mDirView->currentIndex();
	if(idx.parent().isValid()){
		mDirView->setCurrentIndex(idx.parent());
	}
	mFileView->selectionModel()->clearSelection();
}

void FilesystemWidget::setWindowTitle(const QString &dir){
	mWindowTitle = QString("%1 - %2").arg(qApp->applicationName()).arg(dir);
	emit windowTitle(mWindowTitle);
}