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 : #include "filedialog.h"
23 :
24 : #include "log.h"
25 :
26 : using namespace muse;
27 : using namespace muse::extensions::apiv1;
28 :
29 0 : FileDialog::FileDialog(QObject* parent)
30 0 : : QObject(parent), Contextable(muse::iocCtxForQmlObject(this)) {}
31 :
32 0 : QString FileDialog::doOpen(const QString& title, const QString& folder)
33 : {
34 0 : io::path_t path;
35 0 : if (m_type == Load) {
36 0 : path = interactive()->selectOpeningFileSync(title.toStdString(), folder, {});
37 0 : LOGD() << "selectOpeningFile: " << path;
38 : } else {
39 0 : path = interactive()->selectSavingFileSync(title.toStdString(), folder, {});
40 0 : LOGD() << "selectSavingFile: " << path;
41 : }
42 :
43 0 : setVisible(false);
44 :
45 0 : return path.toQString();
46 0 : }
47 :
48 0 : QString FileDialog::title() const
49 : {
50 0 : return m_title;
51 : }
52 :
53 0 : void FileDialog::setTitle(const QString& newTitle)
54 : {
55 0 : if (m_title == newTitle) {
56 0 : return;
57 : }
58 0 : m_title = newTitle;
59 0 : emit titleChanged();
60 : }
61 :
62 0 : bool FileDialog::visible() const
63 : {
64 0 : return m_visible;
65 : }
66 :
67 0 : void FileDialog::setVisible(bool newVisible)
68 : {
69 0 : if (m_visible == newVisible) {
70 0 : return;
71 : }
72 0 : m_visible = newVisible;
73 0 : emit visibleChanged();
74 :
75 0 : if (m_visible) {
76 0 : m_filePath = doOpen(m_title, m_folder);
77 0 : if (!m_filePath.isEmpty()) {
78 0 : emit accepted();
79 : } else {
80 0 : emit rejected();
81 : }
82 : }
83 : }
84 :
85 0 : void FileDialog::open()
86 : {
87 0 : setVisible(true);
88 0 : }
89 :
90 0 : void FileDialog::close()
91 : {
92 0 : setVisible(false);
93 0 : }
94 :
95 0 : QString FileDialog::folder() const
96 : {
97 0 : return m_folder;
98 : }
99 :
100 0 : void FileDialog::setFolder(const QString& newFolder)
101 : {
102 0 : if (m_folder == newFolder) {
103 0 : return;
104 : }
105 0 : m_folder = newFolder;
106 0 : emit folderChanged();
107 : }
108 :
109 0 : QString FileDialog::filePath() const
110 : {
111 0 : return m_filePath;
112 : }
113 :
114 0 : void FileDialog::setFilePath(const QString& newFilePath)
115 : {
116 0 : if (m_filePath == newFilePath) {
117 0 : return;
118 : }
119 0 : m_filePath = newFilePath;
120 0 : emit filePathChanged();
121 : }
122 :
123 0 : QUrl FileDialog::fileUrl() const
124 : {
125 0 : return QUrl::fromLocalFile(m_filePath);
126 : }
127 :
128 0 : void FileDialog::setFileUrl(const QUrl& newFileUrl)
129 : {
130 0 : setFilePath(newFileUrl.toLocalFile());
131 0 : }
132 :
133 0 : QList<QUrl> FileDialog::fileUrls() const
134 : {
135 0 : QList<QUrl> urls;
136 0 : urls.append(QUrl::fromLocalFile(m_filePath));
137 0 : return urls;
138 0 : }
139 :
140 0 : void FileDialog::setFileUrls(const QList<QUrl>& newFileUrls)
141 : {
142 0 : if (newFileUrls.isEmpty()) {
143 0 : setFilePath(QString());
144 : } else {
145 0 : setFilePath(newFileUrls.first().toLocalFile());
146 : }
147 0 : }
148 :
149 0 : FileDialog::Type FileDialog::type() const
150 : {
151 0 : return m_type;
152 : }
153 :
154 0 : void FileDialog::setType(Type newType)
155 : {
156 0 : if (m_type == newType) {
157 0 : return;
158 : }
159 0 : m_type = newType;
160 0 : emit typeChanged();
161 : }
|