summaryrefslogtreecommitdiffstats
path: root/filesystemwidget.cpp
blob: 3798dcc5fc3f216df1e18533a69bd990bd740407 (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
/*
  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 "filesystemwidget.h"
#include "filesystemdirproxy.h"
#include "fileview.h"
#include "shemoviconprovider.h"

FilesystemWidget::FilesystemWidget(QWidget *parent) : QWidget(parent) {
	mModel = new QDirModel;
	mModel->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
	mModel->setSorting(QDir::DirsFirst | QDir::IgnoreCase);
	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);

	mFileView = new FileView;
	mFileView->setModel(mModel);
	connect(mDirView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(directoryChanged(const QModelIndex &)));

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

	mFileView->resizeColumnToContents(0);

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

	setLayout(mainLayout);
}

void FilesystemWidget::directoryChanged(const QModelIndex &index){
	QModelIndex real = mDirProxy->mapToSource(index);
	if(!index.isValid()){
		return;
	}
	mFileView->setRootIndex(real);
}