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 "errordetailsmodel.h"
24 :
25 : #include <QApplication>
26 : #include <QClipboard>
27 : #include <QTextDocument>
28 :
29 : #include "log.h"
30 :
31 : using namespace muse::interactive;
32 :
33 : static const QString ERROR_TEXT_KEY("errorText");
34 : static const QString ERROR_PLAIN_TEXT_KEY("errorPlainText");
35 :
36 0 : static QString toPlainText(const QString& html)
37 : {
38 0 : QTextDocument doc;
39 0 : doc.setHtml(html);
40 :
41 0 : return doc.toPlainText();
42 0 : }
43 :
44 0 : ErrorDetailsModel::ErrorDetailsModel(QObject* parent)
45 0 : : QAbstractListModel(parent)
46 : {
47 0 : }
48 :
49 0 : QVariant ErrorDetailsModel::data(const QModelIndex& index, int role) const
50 : {
51 0 : if (!index.isValid() || index.row() >= rowCount()) {
52 0 : return QVariant();
53 : }
54 :
55 0 : const QVariantMap& item = m_items.at(index.row());
56 0 : switch (role) {
57 0 : case ErrorText: return item[ERROR_TEXT_KEY];
58 0 : case ErrorPlainText: return item[ERROR_PLAIN_TEXT_KEY];
59 : }
60 :
61 0 : return QVariant();
62 : }
63 :
64 0 : int ErrorDetailsModel::rowCount(const QModelIndex&) const
65 : {
66 0 : return m_items.size();
67 : }
68 :
69 0 : QHash<int, QByteArray> ErrorDetailsModel::roleNames() const
70 : {
71 : static const QHash<int, QByteArray> roles = {
72 0 : { ErrorText, ERROR_TEXT_KEY.toUtf8() },
73 0 : { ErrorPlainText, ERROR_PLAIN_TEXT_KEY.toUtf8() },
74 0 : };
75 :
76 0 : return roles;
77 0 : }
78 :
79 0 : void ErrorDetailsModel::load(const QString& detailedText)
80 : {
81 0 : TRACEFUNC;
82 :
83 0 : beginResetModel();
84 :
85 0 : QStringList errors = detailedText.split('\n');
86 :
87 0 : for (const QString& error : errors) {
88 0 : QVariantMap item;
89 0 : item[ERROR_TEXT_KEY] = error;
90 0 : item[ERROR_PLAIN_TEXT_KEY] = toPlainText(error);
91 :
92 0 : m_items << item;
93 0 : }
94 :
95 0 : endResetModel();
96 0 : }
97 :
98 0 : bool ErrorDetailsModel::copyDetailsToClipboard()
99 : {
100 0 : QClipboard* clipboard = QGuiApplication::clipboard();
101 0 : if (!clipboard || m_items.empty()) {
102 0 : return false;
103 : }
104 :
105 0 : QString text;
106 0 : int lastIdx = m_items.size() - 1;
107 :
108 0 : for (int i = 0; i < m_items.size(); ++i) {
109 0 : text += m_items[i][ERROR_PLAIN_TEXT_KEY].toString();
110 :
111 0 : if (i != lastIdx) {
112 0 : text += "\n";
113 : }
114 : }
115 :
116 0 : clipboard->setText(text);
117 :
118 0 : return true;
119 0 : }
|