1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "gradientpresetcustomlistmodel.h"
27 #include "gradientpresetitem.h"
28
29 #include <coreplugin/icore.h>
30 #include <utils/qtcassert.h>
31 #include <utils/algorithm.h>
32
33 #include <QHash>
34 #include <QByteArray>
35 #include <QDebug>
36 #include <QSettings>
37 #include <QFile>
38
39 namespace Internal {
40
41 static const char settingsKey[] = "GradientPresetCustomList";
42 static const char settingsFileName[] = "GradientPresets.ini";
43
settingsFullFilePath(const QSettings::Scope & scope)44 QString settingsFullFilePath(const QSettings::Scope &scope)
45 {
46 if (scope == QSettings::SystemScope)
47 return Core::ICore::installerResourcePath(settingsFileName).toString();
48
49 return Core::ICore::userResourcePath(settingsFileName).toString();
50 }
51
52 } // namespace Internal
53
GradientPresetCustomListModel(QObject * parent)54 GradientPresetCustomListModel::GradientPresetCustomListModel(QObject *parent)
55 : GradientPresetListModel(parent)
56 , m_filename(getFilename())
57 {
58 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
59 qRegisterMetaTypeStreamOperators<GradientPresetItem>("GradientPresetItem");
60 #endif
61 readPresets();
62 }
63
~GradientPresetCustomListModel()64 GradientPresetCustomListModel::~GradientPresetCustomListModel() {}
65
registerDeclarativeType()66 void GradientPresetCustomListModel::registerDeclarativeType()
67 {
68 qmlRegisterType<GradientPresetCustomListModel>("HelperWidgets",
69 2,
70 0,
71 "GradientPresetCustomListModel");
72 }
73
getFilename()74 QString GradientPresetCustomListModel::getFilename()
75 {
76 return Internal::settingsFullFilePath(QSettings::UserScope);
77 }
78
storePresets(const QString & filename,const QList<GradientPresetItem> & items)79 void GradientPresetCustomListModel::storePresets(const QString &filename,
80 const QList<GradientPresetItem> &items)
81 {
82 const QList<QVariant> presets
83 = Utils::transform<QList<QVariant>>(items, [](const GradientPresetItem &item) {
84 return QVariant::fromValue(item);
85 });
86
87 QSettings settings(filename, QSettings::IniFormat);
88 settings.clear();
89 settings.setValue(Internal::settingsKey, QVariant::fromValue(presets));
90 }
91
storedPresets(const QString & filename)92 QList<GradientPresetItem> GradientPresetCustomListModel::storedPresets(const QString &filename)
93 {
94 const QSettings settings(filename, QSettings::IniFormat);
95 const QVariant presetSettings = settings.value(Internal::settingsKey);
96
97 if (!presetSettings.isValid())
98 return {};
99
100 const QList<QVariant> presets = presetSettings.toList();
101
102 QList<GradientPresetItem> out;
103 for (const QVariant &preset : presets) {
104 if (preset.isValid()) {
105 out.append(preset.value<GradientPresetItem>());
106 }
107 }
108
109 return out;
110 }
111
addGradient(const QList<qreal> & stopsPositions,const QList<QString> & stopsColors,int stopsCount)112 void GradientPresetCustomListModel::addGradient(const QList<qreal> &stopsPositions,
113 const QList<QString> &stopsColors,
114 int stopsCount)
115 {
116 QGradient tempGradient;
117 QGradientStops gradientStops;
118 QGradientStop gradientStop;
119 for (int i = 0; i < stopsCount; i++) {
120 gradientStop.first = stopsPositions.at(i);
121 gradientStop.second = stopsColors.at(i);
122 gradientStops.push_back(gradientStop);
123 }
124
125 tempGradient.setStops(gradientStops);
126
127 addItem(GradientPresetItem(tempGradient));
128 }
129
changePresetName(int id,const QString & newName)130 void GradientPresetCustomListModel::changePresetName(int id, const QString &newName)
131 {
132 QTC_ASSERT(id >= 0, return);
133 QTC_ASSERT(id < m_items.size(), return);
134 m_items[id].setPresetName(newName);
135 writePresets();
136 }
137
deletePreset(int id)138 void GradientPresetCustomListModel::deletePreset(int id)
139 {
140 QTC_ASSERT(id >= 0, return);
141 QTC_ASSERT(id < m_items.size(), return);
142 beginResetModel();
143 m_items.removeAt(id);
144 writePresets();
145 endResetModel();
146 }
147
writePresets()148 void GradientPresetCustomListModel::writePresets()
149 {
150 storePresets(m_filename, m_items);
151 }
152
readPresets()153 void GradientPresetCustomListModel::readPresets()
154 {
155 const QList<GradientPresetItem> presets = storedPresets(m_filename);
156 beginResetModel();
157 m_items.clear();
158
159 for (const GradientPresetItem &preset : presets) {
160 addItem(preset);
161 }
162 endResetModel();
163 }
164