WSL/SLF GitLab Repository

PathView.cc 3.06 KB
Newer Older
Mathias Bavay's avatar
Mathias Bavay committed
1
//SPDX-License-Identifier: GPL-3.0-or-later
2
3
4
5
6
/*****************************************************************************/
/*  Copyright 2019 WSL Institute for Snow and Avalanche Research  SLF-DAVOS  */
/*****************************************************************************/
/* This file is part of INIshell.
   INIshell is free software: you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
9
10
11
12
13
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   INIshell is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15

16
17
   You should have received a copy of the GNU General Public License
   along with INIshell.  If not, see <http://www.gnu.org/licenses/>.
18
19
*/

20
21
#include <src/gui/PathView.h>
#include <src/main/constants.h>
22

23
#include <QDir>
24
25
#include <QVBoxLayout>

26
27
28
29
30
31
32
33
34
35
36
37
/**
 * @class DragListModel
 * @brief File system model with dragging abilities.
 * @details This class inherits from QFileSystemModel and enables the flags necessary for
 * the built-in dragging abilities.
 */

/**
 * @brief Set flags to enable dragging in the QFileSystemModel.
 * @param[in] index File model index of the item that is being draged.
 * @return Default flags plus dragging flag.
 */
38
39
Qt::ItemFlags DragListModel::flags(const QModelIndex &index) const
{
40
	Qt::ItemFlags def_flags(QFileSystemModel::flags(index));
41
	if (index.isValid())
42
		return Qt::ItemIsDragEnabled | def_flags;
Michael Reisecker's avatar
Michael Reisecker committed
43
	return def_flags;
44
45
}

46
47
48
49
50
/**
 * @brief MIME data types enabled for dragging.
 * @details At the moment text files are enabled, for example .smet or .pro.
 * @return A list of the enabled MIME types.
 */
51
52
QStringList DragListModel::mimeTypes() const
{
Mathias Bavay's avatar
Mathias Bavay committed
53
	QStringList mime_types = { "application/vnd.text.list" }; //text
54
55
56
	return mime_types;
}

57
58
59
60
61
62
/**
 * @class PathView
 * @brief Default constructor for a PathView panel.
 * @details This constructor creates a file system model and connects a display list to it.
 * @param[in] parent The PathView's parent panel/window.
 */
63
64
PathView::PathView(QWidget *parent) : QWidget(parent)
{
65
66
67
	/* file system model and connected list view */
	file_model_ = new DragListModel; //subclass QFileSystemModel + dragging
	file_model_->setRootPath(""); //model is the whole file system
68
69
	file_list_ = new QListView;
	file_list_->setDragEnabled(true);
70
	file_list_->setModel(file_model_);
71
	file_list_->setRootIndex(file_model_->index(QDir::currentPath())); //default list view path
72
	file_list_->setToolTip(QDir::currentPath());
73

74
75
	//TODO: add navigation possibility for the PathView (toolbar or context menu)

76
	/* main layout */
77
78
79
80
	auto *main_layout = new QVBoxLayout;
	main_layout->addWidget(file_list_);
	this->setLayout(main_layout);
}
81

82
83
/**
 * @brief Set the path to display in the list.
Michael Reisecker's avatar
Michael Reisecker committed
84
 * @param[in] path The path to display.
85
 */
86
87
88
void PathView::setPath(const QString &path)
{
	file_list_->setRootIndex(file_model_->index(path));
89
	file_list_->setToolTip(path);
90
}