Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "filepickermodel.h"
24 :
25 : using namespace muse::uicomponents;
26 :
27 0 : FilePickerModel::FilePickerModel(QObject* parent)
28 0 : : QObject(parent), muse::Contextable(muse::iocCtxForQmlObject(this))
29 : {
30 0 : }
31 :
32 0 : QString FilePickerModel::title() const
33 : {
34 0 : return m_title;
35 : }
36 :
37 0 : QString FilePickerModel::dir() const
38 : {
39 0 : return m_dir;
40 : }
41 :
42 0 : QStringList FilePickerModel::filter() const
43 : {
44 0 : return m_filter;
45 : }
46 :
47 0 : QString FilePickerModel::selectFile()
48 : {
49 0 : std::vector<std::string> filter;
50 0 : for (const QString& nameFilter : m_filter) {
51 0 : filter.push_back(nameFilter.toStdString());
52 : }
53 :
54 0 : io::path_t file = interactive()->selectOpeningFileSync(m_title.toStdString(), m_dir, filter);
55 :
56 0 : if (!file.empty()) {
57 0 : m_dir = io::dirpath(file).toQString();
58 : }
59 :
60 0 : return file.toQString();
61 0 : }
62 :
63 0 : QString FilePickerModel::selectDirectory()
64 : {
65 0 : io::path_t directory = interactive()->selectDirectory(m_title.toStdString(), m_dir);
66 :
67 0 : if (!directory.empty()) {
68 0 : m_dir = directory.toQString();
69 : }
70 :
71 0 : return directory.toQString();
72 0 : }
73 :
74 0 : QString FilePickerModel::selectMultipleDirectories(const QString& selectedDirectoriesStr)
75 : {
76 0 : io::paths_t selectedDirectories = io::pathsFromString(selectedDirectoriesStr.toStdString());
77 0 : io::paths_t directories = interactive()->selectMultipleDirectories(m_title.toStdString(), m_dir, selectedDirectories);
78 :
79 0 : QStringList result;
80 0 : for (const io::path_t& dir: directories) {
81 0 : result << dir.toQString();
82 : }
83 :
84 0 : return result.join(";");
85 0 : }
86 :
87 0 : QString FilePickerModel::selectAny()
88 : {
89 0 : std::vector<std::string> filter;
90 0 : for (const QString& nameFilter : m_filter) {
91 0 : filter.push_back(nameFilter.toStdString());
92 : }
93 :
94 0 : io::path_t file = interactive()->selectSavingFileSync(m_title.toStdString(), m_dir, filter);
95 :
96 0 : if (!file.empty()) {
97 0 : m_dir = io::dirpath(file).toQString();
98 : }
99 :
100 0 : return file.toQString();
101 0 : }
102 :
103 0 : void FilePickerModel::setTitle(const QString& title)
104 : {
105 0 : if (title == m_title) {
106 0 : return;
107 : }
108 :
109 0 : m_title = title;
110 0 : emit titleChanged(title);
111 : }
112 :
113 0 : void FilePickerModel::setDir(const QString& dir)
114 : {
115 0 : if (dir == m_dir) {
116 0 : return;
117 : }
118 :
119 0 : m_dir = dir;
120 0 : emit dirChanged(dir);
121 : }
122 :
123 0 : void FilePickerModel::setFilter(const QStringList& filter)
124 : {
125 0 : if (filter == m_filter) {
126 0 : return;
127 : }
128 :
129 0 : m_filter = filter;
130 0 : emit filterChanged(filter);
131 : }
|