filebrowser.cpp 3.97 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
6
7
8
/**
 * data file browser
 * @author Tobias Weber <tweber@ill.fr>
 * @date 9-Apr-2018
 * @license see 'LICENSE' file
 */

#include "filebrowser.h"
Tobias WEBER's avatar
Tobias WEBER committed
9
10

#include <QtCore/QDirIterator>
Tobias WEBER's avatar
Tobias WEBER committed
11
12
13
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QFileDialog>
Tobias WEBER's avatar
Tobias WEBER committed
14
15
16
17
18
19
#include <algorithm>

#include "tlibs/file/loadinstr.h"


using t_real = double;
Tobias WEBER's avatar
Tobias WEBER committed
20
21


Tobias WEBER's avatar
Tobias WEBER committed
22
// ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
23

Tobias WEBER's avatar
Tobias WEBER committed
24
25
FileBrowserWidget::FileBrowserWidget(QWidget *pParent, QSettings *pSettings)
	: QWidget(pParent), m_pSettings(pSettings)
Tobias WEBER's avatar
Tobias WEBER committed
26
{
Tobias WEBER's avatar
Tobias WEBER committed
27
28
	m_pListFiles->setAlternatingRowColors(true);

Tobias WEBER's avatar
Tobias WEBER committed
29
30
31
	// ------------------------------------------------------------------------
	// layout
	auto *pBtnFolders = new QPushButton("Folder...", this);
Tobias WEBER's avatar
Tobias WEBER committed
32

Tobias WEBER's avatar
Tobias WEBER committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	auto *pGrid = new QGridLayout(this);
	pGrid->addWidget(pBtnFolders, 0, 0, 1, 1);
	pGrid->addWidget(m_pEditFolder, 0, 1, 1, 1);
	pGrid->addWidget(m_pListFiles, 1, 0, 2, 2);
	// ------------------------------------------------------------------------


	// ------------------------------------------------------------------------
	// connections
	connect(pBtnFolders, &QPushButton::clicked, this, &FileBrowserWidget::SelectFolder);
	connect(m_pEditFolder, &QLineEdit::textChanged, this, &FileBrowserWidget::SetFolder);
	// ------------------------------------------------------------------------


	// ------------------------------------------------------------------------
	// settings
	if(m_pSettings)
	{
		m_pEditFolder->setText(m_pSettings->value("filebrowser/dir", "").toString());
	}
	// ------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
54
55
}

Tobias WEBER's avatar
Tobias WEBER committed
56

Tobias WEBER's avatar
Tobias WEBER committed
57
58
59
60
61
FileBrowserWidget::~FileBrowserWidget()
{
}


Tobias WEBER's avatar
Tobias WEBER committed
62
63
64
65
66
67
68
69
70
/**
 * open a folder selection dialog
 */
void FileBrowserWidget::SelectFolder()
{
	QString dir;
	if(m_pSettings)
		dir = m_pSettings->value("filebrowser/dir", "").toString();
	dir = QFileDialog::getExistingDirectory(this, "Select Folder", dir);
Tobias WEBER's avatar
Tobias WEBER committed
71
72
	if(dir != "")
		m_pEditFolder->setText(dir);
Tobias WEBER's avatar
Tobias WEBER committed
73
74
75
76
77
78
79
80
}


/**
 * set a given folder as working dir and populate file list
 */
void FileBrowserWidget::SetFolder(const QString& dir)
{
Tobias WEBER's avatar
Tobias WEBER committed
81
82
	m_pListFiles->clear();

Tobias WEBER's avatar
Tobias WEBER committed
83
84
85
	if(dir == "" || !QDir(dir).exists())
		return;

Tobias WEBER's avatar
Tobias WEBER committed
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
	// create file list
	std::vector<QString> vecFiles;
	for(QDirIterator iter(dir); ; iter.next())
	{
		if(iter.fileInfo().isFile())
			vecFiles.push_back(iter.fileName());

		if(!iter.hasNext())
			break;
	}


	// sort file list
	std::sort(vecFiles.begin(), vecFiles.end(),
	[](const auto& str1, const auto& str2) -> bool
	{
		return std::lexicographical_compare(str1.begin(), str1.end(), str2.begin(), str2.end());
	});


	// add files to list widget
	for(const auto& strFile : vecFiles)
	{
		QString fileFull = dir + "/" + strFile;

		// load file to get a description
		std::unique_ptr<tl::FileInstrBase<t_real>> pInstr(tl::FileInstrBase<t_real>::LoadInstr(fileFull.toStdString().c_str()));
		if(pInstr && pInstr->GetColNames().size())	// only valid files with a non-zero column count
		{
			QString strDescr;
			auto vecScanVars = pInstr->GetScannedVars();
			if(vecScanVars.size())
			{
				strDescr += ": ";
				for(std::size_t iScVar=0; iScVar<vecScanVars.size(); ++iScVar)
				{
					strDescr += vecScanVars[iScVar].c_str();
					if(iScVar < vecScanVars.size()-1)
						strDescr += ", ";
				}
				strDescr += " scan";
			}

			auto *pItem = new QListWidgetItem(m_pListFiles);
			pItem->setText(strFile + strDescr);
			pItem->setData(Qt::UserRole, fileFull);
		}
	}
Tobias WEBER's avatar
Tobias WEBER committed
134
135
136
137
138
139
140
141

	if(m_pSettings)
		m_pSettings->setValue("filebrowser/dir", dir);
}
// ----------------------------------------------------------------------------



Tobias WEBER's avatar
Tobias WEBER committed
142

Tobias WEBER's avatar
Tobias WEBER committed
143
144
145
146
147

// ----------------------------------------------------------------------------

FileBrowser::FileBrowser(QWidget* pParent, QSettings *pSettings)
	: QDockWidget(pParent), m_pBrowser(std::make_unique<FileBrowserWidget>(this, pSettings))
Tobias WEBER's avatar
Tobias WEBER committed
148
149
150
{
	this->setObjectName("fileBrowser");
	this->setWindowTitle("File Browser");
Tobias WEBER's avatar
Tobias WEBER committed
151
	this->setWidget(m_pBrowser.get());
Tobias WEBER's avatar
Tobias WEBER committed
152
153
154
155
156
}

FileBrowser::~FileBrowser()
{
}
Tobias WEBER's avatar
Tobias WEBER committed
157
158

// ----------------------------------------------------------------------------