-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesview.cpp
122 lines (98 loc) · 3.7 KB
/
filesview.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
#include <QVBoxLayout>
#include <QFileDialog>
#include "ioc.h"
#include "reader.h"
#include "filesview.h"
FilesView::FilesView(QWidget* parent) :
QWidget(parent)
{
filesystem_model = new QFileSystemModel(this);
list_view = new QListView(this);
// TODO set filesystem filters
filesystem_model->setRootPath(QDir::currentPath());
list_view->setModel(filesystem_model);
list_view->setRootIndex(filesystem_model->index(QDir::currentPath()));
auto vbox_layout = new QVBoxLayout(this);
auto hbox_layout = new QHBoxLayout(this);
back_button = new QPushButton(this);
back_button->setText("Back");
change_directory_button = new QPushButton(this);
change_directory_button->setText("Open");
current_directory_line_edit = new QLineEdit(this);
current_directory_line_edit->setText(QDir::currentPath());
hbox_layout->addWidget(back_button, 0);
hbox_layout->addWidget(current_directory_line_edit, 1);
hbox_layout->addWidget(change_directory_button, 0);
vbox_layout->addLayout(hbox_layout);
vbox_layout->addWidget(list_view);
setMinimumWidth(400);
QObject::connect(list_view, &QListView::clicked, this, &FilesView::onFileClicked);
QObject::connect(list_view, &QListView::activated, this, &FilesView::onFileSelected);
QObject::connect(back_button, &QPushButton::clicked, this, &FilesView::onBackClicked);
QObject::connect(change_directory_button, &QPushButton::clicked, this, &FilesView::onChangedDirectoryButton);
QObject::connect(current_directory_line_edit, &QLineEdit::returnPressed, this, &FilesView::onChangedDirectoryLineEdit);
}
void FilesView::onFileClicked(const QModelIndex& index)
{
auto extention = filesystem_model->fileInfo(index).fileName().split(".").last();
if (extention == "csv")
{
IoCContainer::registerService<IReader>(std::make_shared<CSVReader>());
return;
}
IoCContainer::registerService<IReader>(nullptr);
}
void FilesView::onBackClicked()
{
auto directory_path = current_directory_line_edit->text();
int start = directory_path.size() - 1;
if (directory_path[start] == QChar('/') || directory_path[start - 1] == QChar('\\'))
start--;
while (start >= 0 && directory_path[start] != QChar('/') && directory_path[start] != QChar('\\'))
{
start--;
}
if (start < 0)
return;
directory_path.truncate(start+1);
filesystem_model->setRootPath(directory_path);
list_view->setRootIndex(filesystem_model->index(directory_path));
current_directory_line_edit->setText(directory_path);
}
void FilesView::onChangedDirectoryButton()
{
auto directory_path = QFileDialog::getExistingDirectory();
if (!directory_path.isEmpty())
{
filesystem_model->setRootPath(directory_path);
list_view->setRootIndex(filesystem_model->index(directory_path));
current_directory_line_edit->setText(directory_path);
}
}
void FilesView::onChangedDirectoryLineEdit()
{
auto directory_path = current_directory_line_edit->text();
QFileInfo dir{ directory_path };
if (dir.isDir())
{
filesystem_model->setRootPath(directory_path);
list_view->setRootIndex(filesystem_model->index(directory_path));
current_directory_line_edit->setText(directory_path);
}
}
void FilesView::onFileSelected(const QModelIndex& index)
{
auto info = filesystem_model->fileInfo(index);
if (info.isDir())
{
filesystem_model->setRootPath(info.absoluteFilePath());
list_view->setRootIndex(index);
current_directory_line_edit->setText(info.absoluteFilePath());
return;
}
if (model_index != index)
{
emit FilesView::fileSelected(info);
model_index = index;
}
}