File: file-selection.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (78 lines) | stat: -rw-r--r-- 1,802 bytes parent folder | download
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
#include "file-selection.hpp"
#include "obs-module-helper.hpp"

#include <QLayout>
#include <QFileDialog>
#include <QStandardPaths>

namespace advss {

FileSelection::FileSelection(FileSelection::Type type, QWidget *parent)
	: QWidget(parent),
	  _type(type),
	  _filePath(new VariableLineEdit(this)),
	  _browseButton(
		  new QPushButton(obs_module_text("AdvSceneSwitcher.browse")))
{
	QWidget::connect(_filePath, SIGNAL(editingFinished()), this,
			 SLOT(PathChange()));
	QWidget::connect(_browseButton, SIGNAL(clicked()), this,
			 SLOT(BrowseButtonClicked()));
	QHBoxLayout *layout = new QHBoxLayout;
	layout->addWidget(_filePath);
	layout->addWidget(_browseButton);
	layout->setContentsMargins(0, 0, 0, 0);
	setLayout(layout);
}

void FileSelection::SetPath(const StringVariable &path)
{
	_filePath->setText(path);
}

void FileSelection::SetPath(const QString &path)
{
	_filePath->setText(path);
}

QString FileSelection::GetPath() const
{
	return _filePath->text();
}

QString FileSelection::ValidPathOrDesktop(const QString &path)
{
	QFileInfo fileInfo(path);
	if (fileInfo.isFile()) {
		return path;
	}
	return QStandardPaths::writableLocation(
		QStandardPaths::DesktopLocation);
}

void FileSelection::BrowseButtonClicked()
{
	QString defaultPath = ValidPathOrDesktop(_filePath->text());
	QString path;
	if (_type == FileSelection::Type::WRITE) {
		path = QFileDialog::getSaveFileName(this, "", defaultPath);
	} else if (_type == FileSelection::Type::READ) {
		path = QFileDialog::getOpenFileName(this, "", defaultPath);
	} else {
		path = QFileDialog::getExistingDirectory(this, "", defaultPath);
	}

	if (path.isEmpty()) {
		return;
	}

	_filePath->setText(path);
	emit PathChanged(path);
}

void FileSelection::PathChange()
{
	emit PathChanged(_filePath->text());
}

} // namespace advss