QtDesignStudio Gradient Picker Dialog

Dialog allows you to save your gradients and to use stored presets,
including system gradients.

Change-Id: I6888446afd63c00f3cb8d9277d9e37517243491f
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Aleksei German
2019-03-18 14:59:24 +01:00
parent 1f46098349
commit 092d969f10
18 changed files with 1325 additions and 13 deletions

View File

@@ -39,6 +39,8 @@ Item {
signal clicked() signal clicked()
signal doubleClicked()
property alias tooltip: toolTipArea.tooltip property alias tooltip: toolTipArea.tooltip
width: 24 + leftPadding width: 24 + leftPadding
@@ -100,7 +102,8 @@ Item {
buttonRowButton.parent.__checkButton(index()) buttonRowButton.parent.__checkButton(index())
} }
buttonRowButton.clicked() buttonRowButton.clicked()
} }
onDoubleClicked: buttonRowButton.doubleClicked()
} }
} }

View File

@@ -234,6 +234,46 @@ Column {
gradientLine.addGradient() gradientLine.addGradient()
} }
GradientPresetList {
id: presetList
visible: false
function applyPreset() {
if (presetList.gradientData.presetType == 0) {
gradientLine.setPresetByID(presetList.gradientData.presetID);
}
else if (presetList.gradientData.presetType == 1) {
gradientLine.setPresetByStops(
presetList.gradientData.stops,
presetList.gradientData.colors,
presetList.gradientData.stopsCount);
}
else { console.log("INVALID GRADIENT TYPE: " + presetList.gradientData.presetType); }
}
onApply: {
if (presetList.gradientData.stopsCount > 0) {
applyPreset();
}
}
onSaved: {
gradientLine.savePreset();
presetList.updatePresets();
}
onAccepted: { //return key
if (presetList.gradientData.stopsCount > 0) {
applyPreset();
}
}
}
onDoubleClicked: {
presetList.open()
}
tooltip: qsTr("Linear Gradient") tooltip: qsTr("Linear Gradient")
GradientPopupIndicator { GradientPopupIndicator {

View File

@@ -59,6 +59,22 @@ Item {
gradientModel.deleteGradient() gradientModel.deleteGradient()
} }
function setPresetByID(presetID) {
gradientModel.setPresetByID(presetID)
colorLine.invalidate()
colorLine.select(0)
}
function setPresetByStops(stopsPositions, stopsColors, stopsCount) {
gradientModel.setPresetByStops(stopsPositions, stopsColors, stopsCount)
colorLine.invalidate()
colorLine.select(0)
}
function savePreset() {
gradientModel.savePreset()
}
Connections { Connections {
target: modelNodeBackend target: modelNodeBackend
onSelectionChanged: { onSelectionChanged: {

View File

@@ -0,0 +1,129 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
import QtQuick 2.11
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5
import QtQuick.Dialogs 1.3
import HelperWidgets 2.0
import QtQuickDesignerTheme 1.0
Dialog {
id: dialogWindow
width: 1200
height: 650
title: "Gradient Picker"
signal saved;
property alias gradientData: gradientPickerData;
QtObject {
id: gradientPickerData
property var stops;
property var colors;
property int stopsCount;
property int presetID;
property int presetType; //default(0) or custom(1)
}
function addGradient(stopsPositions, stopsColors, stopsCount) {
customPresetListModel.addGradient(stopsPositions, stopsColors, stopsCount);
}
function updatePresets() {
customPresetListModel.readPresets();
}
GradientPresetDefaultListModel { id: defaultPresetListModel; }
GradientPresetCustomListModel { id: customPresetListModel; }
standardButtons: Dialog.NoButton
Rectangle {
anchors.fill: parent
anchors.margins: -12
anchors.bottomMargin: -70
color: "#363636"
ColumnLayout {
anchors.fill: parent
anchors.margins: 13
anchors.bottomMargin: 71
TabView {
id: presetTabView
Layout.alignment: Qt.AlignTop | Qt.AlignHCenter
Layout.fillHeight: true
Layout.fillWidth: true
Tab {
title: qsTr("System Presets")
anchors.fill:parent
GradientPresetTabContent {
id: defaultTabContent
viewModel: defaultPresetListModel
editableName: false
}
} //tab default
Tab {
title: qsTr("User Presets")
anchors.fill: parent
GradientPresetTabContent {
id: customTabContent
viewModel: customPresetListModel
editableName: true
onPresetNameChanged: customPresetListModel.changePresetName(id, name);
property int deleteId;
onDeleteButtonClicked: {
deleteId = id;
deleteDialog.open()
}
MessageDialog {
id: deleteDialog
visible: false
modality: Qt.WindowModal
standardButtons: Dialog.No | Dialog.Yes
title: qsTr("Delete preset?")
text: qsTr("Are you sure you want to delete this preset?")
onAccepted: customPresetListModel.deletePreset(customTabContent.deleteId);
}
}
} //tab custom
} //tabview
RowLayout {
Layout.alignment: Qt.AlignBottom | Qt.AlignRight
Layout.topMargin: 5
Button { id: buttonClose; text: qsTr("Close"); onClicked: { dialogWindow.reject(); } }
Button { id: buttonSave; text: qsTr("Save"); onClicked: { dialogWindow.saved(); } }
Button { id: buttonApply; text: qsTr("Apply"); onClicked: { dialogWindow.apply(); } }
} //RowLayout
} //ColumnLayout
} //rectangle
} //dialog

View File

@@ -0,0 +1,217 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
import QtQuick 2.11
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5
import QtQuick.Controls.Styles 1.4
import QtQuick.Dialogs 1.3
import HelperWidgets 2.0
import QtQuickDesignerTheme 1.0
Rectangle {
id: tabBackground
width: parent.width
height: parent.height
color: "#242424"
anchors.fill: parent
property alias viewModel : gradientTable.model;
property bool editableName : false;
signal presetNameChanged(int id, string name)
signal deleteButtonClicked(int id)
property int delegateWidth: 153;
property int delegateHeight: 173;
property int gridCellWidth: 160;
ScrollView {
Layout.fillWidth: true
Layout.fillHeight: true
anchors.fill: parent
GridView {
id: gradientTable
Layout.fillWidth: true
Layout.fillHeight: true
anchors.fill: parent
anchors.leftMargin: 10
clip: true
delegate: gradientDelegate
property int gridColumns: width / tabBackground.gridCellWidth;
cellWidth: width / gridColumns
cellHeight: 180
Component {
id: gradientDelegate
Rectangle {
id: backgroundCard
color: "#404040"
clip: false
property real flexibleWidth: (gradientTable.width - gradientTable.cellWidth * gradientTable.gridColumns) / gradientTable.gridColumns
width: gradientTable.cellWidth + flexibleWidth - 8; height: tabBackground.delegateHeight
radius: 16
MouseArea {
id: rectMouseArea
anchors.fill: parent
hoverEnabled: true
onClicked: {
gradientTable.currentIndex = index;
gradientData.stops = stopsPosList;
gradientData.colors = stopsColorList;
gradientData.stopsCount = stopListSize;
gradientData.presetID = presetID;
gradientData.presetType = presetTabView.currentIndex
// console.log( "#" + preset + " " + presetName + " Stops: " + stopsPosList + " Colors: " + stopsColorList);
}
onEntered: {
if (backgroundCard.state != "CLICKED") {
backgroundCard.state = "HOVER";
}
}
onExited: {
if (backgroundCard.state != "CLICKED") {
backgroundCard.state = "USUAL";
}
}
} //mouseArea
states: [
State {
name: "HOVER"
PropertyChanges {
target: backgroundCard
color: "#606060"
z: 5
clip: true
border.width: 1
border.color: "#029de0"
}
},
State {
name: "USUAL"
PropertyChanges
{
target: backgroundCard
color: "#404040"
scale: 1.0
border.width: 0
}
}
] //states
ColumnLayout {
anchors.fill: parent
Rectangle {
width: 150; height: 150
id: gradientRect
radius: 16
Layout.alignment: Qt.AlignHCenter | Qt.AlignTop
Layout.topMargin: 2
gradient: Gradient {
id: showGr
}
Component {
id: stopComponent
GradientStop {}
}
Component.onCompleted: {
var stopsAmount = stopListSize;
var newStops = [];
for (var i = 0; i < stopsAmount; i++ ) {
newStops.push( stopComponent.createObject(showGr, { "position": stopsPosList[i], "color": stopsColorList[i] }) );
}
showGr.stops = newStops;
}
Rectangle {
id: removeItemRect
anchors.right: parent.right
anchors.rightMargin: 2
anchors.top: parent.top
anchors.topMargin: 2
height: 16
width: 16
visible: editableName && rectMouseArea.containsMouse
color: "#804682b4"
MouseArea {
anchors.fill: parent;
onClicked: tabBackground.deleteButtonClicked(index);
}
Image {
id: remoreItemImg
source: "image://icons/close"
fillMode: Image.PreserveAspectFit
anchors.fill: parent;
Layout.alignment: Qt.AlignCenter
}
}
} //rectangle gradient
TextInput {
id: presetNameBox
readOnly: !editableName
text: (presetName)
Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom
Layout.preferredWidth: backgroundCard.width
Layout.topMargin: -5
padding: 5.0
topPadding: -2.0
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.Wrap
color: "#ffffff"
activeFocusOnPress: true
onEditingFinished: tabBackground.presetNameChanged(index, text);
Keys.onPressed: {
if (event.key === Qt.Key_Enter || event.key === Qt.Key_Return) {
event.accepted = true;
editingFinished();
focus = false;
}
} //Keys.onPressed
} //textInput
} //columnLayout
} //rectangle background
} //component delegate
} //gridview
} //scrollView
} //rectangle

View File

@@ -21,6 +21,8 @@ FontComboBox 2.0 FontComboBox.qml
FontSection 2.0 FontSection.qml FontSection 2.0 FontSection.qml
FontStyleButtons 2.0 FontStyleButtons.qml FontStyleButtons 2.0 FontStyleButtons.qml
GradientLine 2.0 GradientLine.qml GradientLine 2.0 GradientLine.qml
GradientPresetList 2.0 GradientPresetList.qml
GradientPresetTabContent 2.0 GradientPresetTabContent.qml
GroupBox 2.0 GroupBox.qml GroupBox 2.0 GroupBox.qml
HueSlider 2.0 HueSlider.qml HueSlider 2.0 HueSlider.qml
IconLabel 2.0 IconLabel.qml IconLabel 2.0 IconLabel.qml

View File

@@ -27,6 +27,8 @@
#include "qmlanchorbindingproxy.h" #include "qmlanchorbindingproxy.h"
#include "propertyeditorview.h" #include "propertyeditorview.h"
#include "gradientpresetitem.h"
#include "gradientpresetcustomlistmodel.h"
#include <exception.h> #include <exception.h>
#include <nodeproperty.h> #include <nodeproperty.h>
@@ -262,16 +264,7 @@ void GradientModel::deleteGradient()
if (!m_itemNode.modelNode().metaInfo().hasProperty(gradientPropertyName().toUtf8())) if (!m_itemNode.modelNode().metaInfo().hasProperty(gradientPropertyName().toUtf8()))
return; return;
QmlDesigner::ModelNode modelNode = m_itemNode.modelNode(); deleteGradientNode(true);
if (m_itemNode.isInBaseState()) {
if (modelNode.hasProperty(gradientPropertyName().toUtf8())) {
QmlDesigner::RewriterTransaction transaction = view()->beginRewriterTransaction(QByteArrayLiteral("GradientModel::deleteGradient"));
QmlDesigner::ModelNode gradientNode = modelNode.nodeProperty(gradientPropertyName().toUtf8()).modelNode();
if (QmlDesigner::QmlObjectNode(gradientNode).isValid())
QmlDesigner::QmlObjectNode(gradientNode).destroy();
}
}
emit hasGradientChanged(); emit hasGradientChanged();
emit gradientTypeChanged(); emit gradientTypeChanged();
@@ -477,6 +470,23 @@ QmlDesigner::ModelNode GradientModel::createGradientStopNode()
return view()->createModelNode(fullTypeName, majorVersion, minorVersion); return view()->createModelNode(fullTypeName, majorVersion, minorVersion);
} }
void GradientModel::deleteGradientNode(bool saveTransaction)
{
QmlDesigner::ModelNode modelNode = m_itemNode.modelNode();
if (m_itemNode.isInBaseState()) {
if (modelNode.hasProperty(gradientPropertyName().toUtf8())) {
if (saveTransaction)
QmlDesigner::RewriterTransaction transaction = view()->beginRewriterTransaction(
QByteArrayLiteral("GradientModel::deleteGradient"));
QmlDesigner::ModelNode gradientNode
= modelNode.nodeProperty(gradientPropertyName().toUtf8()).modelNode();
if (QmlDesigner::QmlObjectNode(gradientNode).isValid())
QmlDesigner::QmlObjectNode(gradientNode).destroy();
}
}
}
void GradientModel::setGradientProperty(const QString &propertyName, qreal value) void GradientModel::setGradientProperty(const QString &propertyName, qreal value)
{ {
QTC_ASSERT(m_itemNode.isValid(), return); QTC_ASSERT(m_itemNode.isValid(), return);
@@ -494,3 +504,84 @@ void GradientModel::setGradientProperty(const QString &propertyName, qreal value
e.showException(); e.showException();
} }
} }
void GradientModel::setPresetByID(int presetID)
{
const QGradient gradient(static_cast<QGradient::Preset>(presetID));
const QList<QGradientStop> gradientStops = gradient.stops().toList();
QList<qreal> stopsPositions;
QList<QString> stopsColors;
for (const QGradientStop &stop : gradientStops) {
stopsPositions.append(stop.first);
stopsColors.append(stop.second.name());
}
setPresetByStops(stopsPositions, stopsColors, gradientStops.size());
}
void GradientModel::setPresetByStops(const QList<qreal> &stopsPositions,
const QList<QString> &stopsColors,
int stopsCount)
{
if (m_locked)
return;
if (!m_itemNode.isValid() || gradientPropertyName().isEmpty())
return;
QmlDesigner::RewriterTransaction transaction = view()->beginRewriterTransaction(
QByteArrayLiteral("GradientModel::setCustomPreset"));
//delete an old gradient without rewriter transaction
deleteGradientNode(false);
//create a new gradient:
if (!m_itemNode.modelNode().hasNodeProperty(gradientPropertyName().toUtf8())) {
try {
QmlDesigner::ModelNode gradientNode = createGradientNode();
m_itemNode.modelNode()
.nodeProperty(gradientPropertyName().toUtf8())
.reparentHere(gradientNode);
//create stops and give them positions and colors based on value
for (int i = 0; i < stopsCount; i++) {
QmlDesigner::ModelNode gradientStopNode = createGradientStopNode();
gradientStopNode.variantProperty("position").setValue(stopsPositions.at(i));
gradientStopNode.variantProperty("color").setValue(stopsColors.at(i));
gradientNode.nodeListProperty("stops").reparentHere(gradientStopNode);
}
} catch (const QmlDesigner::Exception &e) {
e.showException();
}
}
setupModel();
emit hasGradientChanged();
emit gradientTypeChanged();
}
void GradientModel::savePreset()
{
//preparing qgradient:
QGradient currentGradient;
QGradientStops currentStops;
QGradientStop stop; //double, color
for (int i = 0; i < rowCount(); i++) {
stop.first = getPosition(i);
stop.second = getColor(i);
currentStops.append(stop);
}
currentGradient.setStops(currentStops);
const GradientPresetItem item(currentGradient, "Custom Gradient");
//reading the custom gradient file
//filling the file with old data + new data
const QString filename(GradientPresetCustomListModel::getFilename());
QList<GradientPresetItem> items = GradientPresetCustomListModel::storedPresets(filename);
items.append(item);
GradientPresetCustomListModel::storePresets(filename, items);
}

View File

@@ -70,6 +70,13 @@ public:
Q_INVOKABLE void setGradientProperty(const QString &propertyName, qreal value); Q_INVOKABLE void setGradientProperty(const QString &propertyName, qreal value);
Q_INVOKABLE void setPresetByID(int presetID);
Q_INVOKABLE void setPresetByStops(const QList<qreal> &stopsPositions,
const QList<QString> &stopsColors,
int stopsCount);
Q_INVOKABLE void savePreset();
signals: signals:
void anchorBackendChanged(); void anchorBackendChanged();
void hasGradientChanged(); void hasGradientChanged();
@@ -87,6 +94,7 @@ private:
bool locked() const; bool locked() const;
QmlDesigner::ModelNode createGradientNode(); QmlDesigner::ModelNode createGradientNode();
QmlDesigner::ModelNode createGradientStopNode(); QmlDesigner::ModelNode createGradientStopNode();
void deleteGradientNode(bool saveTransaction);
private: private:
QmlDesigner::QmlItemNode m_itemNode; QmlDesigner::QmlItemNode m_itemNode;

View File

@@ -0,0 +1,161 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "gradientpresetcustomlistmodel.h"
#include "gradientpresetitem.h"
#include <coreplugin/icore.h>
#include <utils/qtcassert.h>
#include <utils/algorithm.h>
#include <QHash>
#include <QByteArray>
#include <QDebug>
#include <QSettings>
#include <QFile>
namespace Internal {
static const char settingsKey[] = "GradientPresetCustomList";
static const char settingsFileName[] = "/GradientPresets.ini";
QString settingsFullFilePath(const QSettings::Scope &scope)
{
if (scope == QSettings::SystemScope)
return Core::ICore::installerResourcePath() + settingsFileName;
return Core::ICore::userResourcePath() + settingsFileName;
}
} // namespace Internal
GradientPresetCustomListModel::GradientPresetCustomListModel(QObject *parent)
: GradientPresetListModel(parent)
, m_filename(getFilename())
{
qRegisterMetaTypeStreamOperators<GradientPresetItem>("GradientPresetItem");
readPresets();
}
GradientPresetCustomListModel::~GradientPresetCustomListModel() {}
void GradientPresetCustomListModel::registerDeclarativeType()
{
qmlRegisterType<GradientPresetCustomListModel>("HelperWidgets",
2,
0,
"GradientPresetCustomListModel");
}
QString GradientPresetCustomListModel::getFilename()
{
return Internal::settingsFullFilePath(QSettings::UserScope);
}
void GradientPresetCustomListModel::storePresets(const QString &filename,
const QList<GradientPresetItem> &items)
{
const QList<QVariant> presets
= Utils::transform<QList<QVariant>>(items, [](const GradientPresetItem &item) {
return QVariant::fromValue(item);
});
QSettings settings(filename, QSettings::IniFormat);
settings.clear();
settings.setValue(Internal::settingsKey, QVariant::fromValue(presets));
}
QList<GradientPresetItem> GradientPresetCustomListModel::storedPresets(const QString &filename)
{
const QSettings settings(filename, QSettings::IniFormat);
const QVariant presetSettings = settings.value(Internal::settingsKey);
if (!presetSettings.isValid())
return {};
const QList<QVariant> presets = presetSettings.toList();
QList<GradientPresetItem> out;
for (const QVariant &preset : presets) {
if (preset.isValid()) {
out.append(preset.value<GradientPresetItem>());
}
}
return out;
}
void GradientPresetCustomListModel::addGradient(const QList<qreal> &stopsPositions,
const QList<QString> &stopsColors,
int stopsCount)
{
QGradient tempGradient;
QGradientStops gradientStops;
QGradientStop gradientStop;
for (int i = 0; i < stopsCount; i++) {
gradientStop.first = stopsPositions.at(i);
gradientStop.second = stopsColors.at(i);
gradientStops.push_back(gradientStop);
}
tempGradient.setStops(gradientStops);
addItem(GradientPresetItem(tempGradient));
}
void GradientPresetCustomListModel::changePresetName(int id, const QString &newName)
{
QTC_ASSERT(id >= 0, return);
QTC_ASSERT(id < m_items.size(), return);
m_items[id].setPresetName(newName);
writePresets();
}
void GradientPresetCustomListModel::deletePreset(int id)
{
QTC_ASSERT(id >= 0, return);
QTC_ASSERT(id < m_items.size(), return);
beginResetModel();
m_items.removeAt(id);
writePresets();
endResetModel();
}
void GradientPresetCustomListModel::writePresets()
{
storePresets(m_filename, m_items);
}
void GradientPresetCustomListModel::readPresets()
{
const QList<GradientPresetItem> presets = storedPresets(m_filename);
beginResetModel();
m_items.clear();
for (const GradientPresetItem &preset : presets) {
addItem(preset);
}
endResetModel();
}

View File

@@ -0,0 +1,62 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "gradientpresetlistmodel.h"
#include <QObject>
#include <QAbstractListModel>
#include <QtQml/qqml.h>
class GradientPresetCustomListModel : public GradientPresetListModel
{
Q_OBJECT
public:
explicit GradientPresetCustomListModel(QObject *parent = nullptr);
~GradientPresetCustomListModel() override;
static void registerDeclarativeType();
static QString getFilename();
static void storePresets(const QString &filename, const QList<GradientPresetItem> &items);
static QList<GradientPresetItem> storedPresets(const QString &filename);
Q_INVOKABLE void addGradient(const QList<qreal> &stopsPositions,
const QList<QString> &stopsColors,
int stopsCount);
Q_INVOKABLE void changePresetName(int id, const QString &newName);
Q_INVOKABLE void deletePreset(int id);
Q_INVOKABLE void writePresets();
Q_INVOKABLE void readPresets();
private:
QString m_filename;
};
QML_DECLARE_TYPE(GradientPresetCustomListModel)

View File

@@ -0,0 +1,60 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "gradientpresetdefaultlistmodel.h"
#include <QHash>
#include <QByteArray>
#include <QDebug>
#include <QFile>
#include "gradientpresetitem.h"
GradientPresetDefaultListModel::GradientPresetDefaultListModel(QObject *parent)
: GradientPresetListModel(parent)
{
addAllPresets();
}
GradientPresetDefaultListModel::~GradientPresetDefaultListModel() {}
void GradientPresetDefaultListModel::registerDeclarativeType()
{
qmlRegisterType<GradientPresetDefaultListModel>("HelperWidgets",
2,
0,
"GradientPresetDefaultListModel");
}
void GradientPresetDefaultListModel::addAllPresets()
{
const QMetaObject &metaObj = QGradient::staticMetaObject;
const QMetaEnum metaEnum = metaObj.enumerator(metaObj.indexOfEnumerator("Preset"));
for (int i = 0; i < metaEnum.keyCount(); i++) {
addItem(GradientPresetItem(QGradient::Preset(metaEnum.value(i))));
}
}

View File

@@ -0,0 +1,49 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <QObject>
#include <QAbstractListModel>
#include <QtQml/qqml.h>
#include <memory>
#include "gradientpresetlistmodel.h"
class GradientPresetDefaultListModel : public GradientPresetListModel
{
Q_OBJECT
public:
explicit GradientPresetDefaultListModel(QObject *parent = nullptr);
~GradientPresetDefaultListModel() override;
static void registerDeclarativeType();
private:
void addAllPresets();
};
QML_DECLARE_TYPE(GradientPresetDefaultListModel)

View File

@@ -0,0 +1,201 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "gradientpresetitem.h"
#include <utils/qtcassert.h>
#include <utils/algorithm.h>
#include <QVariant>
#include <QMetaObject>
#include <QMetaEnum>
#include <algorithm>
#include <QDebug>
GradientPresetItem::GradientPresetItem()
: m_gradientVal(QGradient::Preset(0))
, m_gradientID(QGradient::Preset(0))
, m_presetName(QString())
{}
GradientPresetItem::GradientPresetItem(const QGradient &value, const QString &name)
: m_gradientVal(value)
, m_gradientID(QGradient::Preset(0))
, m_presetName(name)
{}
GradientPresetItem::GradientPresetItem(const QGradient::Preset value)
: m_gradientVal(QGradient::Preset(value))
, m_gradientID(value)
, m_presetName(getNameByPreset(value))
{}
GradientPresetItem::~GradientPresetItem() = default;
QVariant GradientPresetItem::getProperty(GradientPresetItem::Property id) const
{
QVariant out;
switch (id) {
case objectNameRole:
out.setValue(QString());
break;
case presetRole:
out.setValue(preset());
break;
case stopsPosListRole:
out.setValue(stopsPosList());
break;
case stopsColorListRole:
out.setValue(stopsColorList());
break;
case stopListSizeRole:
out.setValue(stopListSize());
break;
case presetNameRole:
out.setValue(presetName());
break;
case presetIDRole:
out.setValue(presetID());
break;
default:
qWarning() << "GradientPresetItem Property switch default case";
break; //replace with assert before switch?
}
return out;
}
QGradient GradientPresetItem::gradientVal() const
{
return m_gradientVal;
}
QGradient::Preset GradientPresetItem::preset() const
{
return m_gradientID;
}
void GradientPresetItem::setGradient(const QGradient &value)
{
m_gradientVal = value;
m_gradientID = QGradient::Preset(0);
m_presetName = QString();
}
void GradientPresetItem::setGradient(const QGradient::Preset value)
{
m_gradientID = value;
m_gradientVal = QGradient(value);
m_presetName = getNameByPreset(value);
}
QList<qreal> GradientPresetItem::stopsPosList() const
{
const QList<QPair<qreal, QColor>> subres = m_gradientVal.stops().toList();
const QList<qreal> result = Utils::transform<QList<qreal>>(subres,
[](const QPair<qreal, QColor> &item) {
return item.first;
});
return result;
}
QList<QString> GradientPresetItem::stopsColorList() const
{
const QList<QPair<qreal, QColor>> subres = m_gradientVal.stops().toList();
const QList<QString> result
= Utils::transform<QList<QString>>(subres, [](const QPair<qreal, QColor> &item) {
return item.second.name();
});
return result;
}
int GradientPresetItem::stopListSize() const
{
return m_gradientVal.stops().size();
}
void GradientPresetItem::setPresetName(const QString &value)
{
m_presetName = value;
}
QString GradientPresetItem::presetName() const
{
return m_presetName;
}
int GradientPresetItem::presetID() const
{
return static_cast<int>(m_gradientID);
}
QString GradientPresetItem::getNameByPreset(QGradient::Preset value)
{
const QMetaObject &metaObj = QGradient::staticMetaObject;
const QMetaEnum metaEnum = metaObj.enumerator(metaObj.indexOfEnumerator("Preset"));
QString enumName = QString::fromUtf8(metaEnum.valueToKey(static_cast<int>(value)));
const QStringList sl = enumName.split(QRegExp("(?=[A-Z])"), QString::SkipEmptyParts);
enumName.clear();
std::for_each(sl.begin(), sl.end(), [&enumName](const QString &s) { enumName += (s + " "); });
enumName.chop(1); //let's remove the last empty space
return (enumName.isEmpty() ? "Custom" : enumName);
}
QDebug &operator<<(QDebug &stream, const GradientPresetItem &gradient)
{
stream << "\"stops:" << gradient.m_gradientVal.stops() << "\"";
stream << "\"preset:" << gradient.m_gradientID << "\"";
stream << "\"name:" << gradient.m_presetName << "\"";
return stream;
}
QDataStream &operator<<(QDataStream &stream, const GradientPresetItem &gradient)
{
stream << gradient.m_gradientVal.stops();
stream << static_cast<int>(gradient.m_gradientID);
stream << gradient.m_presetName;
return stream;
}
QDataStream &operator>>(QDataStream &stream, GradientPresetItem &gradient)
{
QGradientStops stops;
stream >> stops;
gradient.m_gradientVal.setStops(stops);
int gradientID;
stream >> gradientID;
gradient.m_gradientID = static_cast<QGradient::Preset>(gradientID);
stream >> gradient.m_presetName;
return stream;
}

View File

@@ -0,0 +1,85 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include <QObject>
#include <QGradient>
class GradientPresetItem
{
Q_GADGET
Q_PROPERTY(QGradient::Preset preset READ preset FINAL)
Q_PROPERTY(QList<qreal> stopsPosList READ stopsPosList FINAL)
Q_PROPERTY(QList<QString> stopsColorList READ stopsColorList FINAL)
Q_PROPERTY(int stopListSize READ stopListSize FINAL)
Q_PROPERTY(QString presetName READ presetName FINAL)
Q_PROPERTY(int presetID READ presetID FINAL)
public:
explicit GradientPresetItem();
explicit GradientPresetItem(const QGradient &value, const QString &name = QString());
explicit GradientPresetItem(const QGradient::Preset number);
~GradientPresetItem();
enum Property {
objectNameRole = 0,
presetRole = 1,
stopsPosListRole = 2,
stopsColorListRole = 3,
stopListSizeRole = 4,
presetNameRole = 5,
presetIDRole = 6
};
QVariant getProperty(Property id) const;
QGradient gradientVal() const;
QGradient::Preset preset() const;
void setGradient(const QGradient &value);
void setGradient(const QGradient::Preset value);
QList<qreal> stopsPosList() const;
QList<QString> stopsColorList() const;
int stopListSize() const;
void setPresetName(const QString &value);
QString presetName() const;
int presetID() const;
static QString getNameByPreset(QGradient::Preset value);
friend QDebug &operator<<(QDebug &stream, const GradientPresetItem &gradient);
friend QDataStream &operator<<(QDataStream &stream, const GradientPresetItem &gradient);
friend QDataStream &operator>>(QDataStream &stream, GradientPresetItem &gradient);
private:
QGradient m_gradientVal;
QGradient::Preset m_gradientID;
QString m_presetName;
};
Q_DECLARE_METATYPE(GradientPresetItem)

View File

@@ -0,0 +1,115 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "gradientpresetlistmodel.h"
#include "gradientpresetitem.h"
#include <QHash>
#include <QByteArray>
#include <QDebug>
#include <QSettings>
GradientPresetListModel::GradientPresetListModel(QObject *parent)
: QAbstractListModel(parent)
{
m_roleNames
= {{static_cast<int>(GradientPresetItem::Property::objectNameRole), "objectName"},
{static_cast<int>(GradientPresetItem::Property::presetRole), "preset"},
{static_cast<int>(GradientPresetItem::Property::stopsPosListRole), "stopsPosList"},
{static_cast<int>(GradientPresetItem::Property::stopsColorListRole), "stopsColorList"},
{static_cast<int>(GradientPresetItem::Property::stopListSizeRole), "stopListSize"},
{static_cast<int>(GradientPresetItem::Property::presetNameRole), "presetName"},
{static_cast<int>(GradientPresetItem::Property::presetIDRole), "presetID"}};
}
GradientPresetListModel::~GradientPresetListModel()
{
clearItems();
}
int GradientPresetListModel::rowCount(const QModelIndex & /*parent*/) const
{
return m_items.count();
}
QVariant GradientPresetListModel::data(const QModelIndex &index, int role) const
{
if (index.isValid() && (index.row() >= 0) && (index.row() < m_items.count())) {
if (m_roleNames.contains(role)) {
QVariant value = m_items.at(index.row())
.getProperty(static_cast<GradientPresetItem::Property>(role));
if (auto model = qobject_cast<GradientPresetListModel *>(value.value<QObject *>()))
return QVariant::fromValue(model);
return value;
}
qWarning() << Q_FUNC_INFO << "invalid role requested";
return QVariant();
}
qWarning() << Q_FUNC_INFO << "invalid index requested";
return QVariant();
}
QHash<int, QByteArray> GradientPresetListModel::roleNames() const
{
return m_roleNames;
}
void GradientPresetListModel::clearItems()
{
beginResetModel();
m_items.clear();
endResetModel();
}
void GradientPresetListModel::addItem(const GradientPresetItem &element)
{
beginResetModel();
m_items.append(element);
endResetModel();
}
const QList<GradientPresetItem> &GradientPresetListModel::items() const
{
return m_items;
}
void GradientPresetListModel::sortItems()
{
auto itemSort = [](const GradientPresetItem &first, const GradientPresetItem &second) {
return (static_cast<int>(first.preset()) < static_cast<int>(second.preset()));
};
std::sort(m_items.begin(), m_items.end(), itemSort);
}
void GradientPresetListModel::registerDeclarativeType()
{
qmlRegisterType<GradientPresetListModel>("HelperWidgets", 2, 0, "GradientPresetListModel");
}

View File

@@ -0,0 +1,61 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <QObject>
#include <QAbstractListModel>
#include <QtQml/qqml.h>
#include <memory>
class GradientPresetItem;
class GradientPresetListModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit GradientPresetListModel(QObject *parent = nullptr);
~GradientPresetListModel() override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;
void clearItems();
void addItem(const GradientPresetItem &element);
const QList<GradientPresetItem> &items() const;
void sortItems();
static void registerDeclarativeType();
protected:
QList<GradientPresetItem> m_items;
QHash<int, QByteArray> m_roleNames;
};
//QML_DECLARE_TYPE(GradientPresetListModel)

View File

@@ -11,7 +11,11 @@ SOURCES += propertyeditorview.cpp \
propertyeditorwidget.cpp \ propertyeditorwidget.cpp \
fileresourcesmodel.cpp \ fileresourcesmodel.cpp \
gradientmodel.cpp \ gradientmodel.cpp \
qmlmodelnodeproxy.cpp qmlmodelnodeproxy.cpp \
gradientpresetitem.cpp \
gradientpresetlistmodel.cpp \
gradientpresetdefaultlistmodel.cpp \
gradientpresetcustomlistmodel.cpp
HEADERS += propertyeditorview.h \ HEADERS += propertyeditorview.h \
qmlanchorbindingproxy.h \ qmlanchorbindingproxy.h \
@@ -24,6 +28,10 @@ HEADERS += propertyeditorview.h \
propertyeditorwidget.h \ propertyeditorwidget.h \
fileresourcesmodel.h \ fileresourcesmodel.h \
gradientmodel.h \ gradientmodel.h \
qmlmodelnodeproxy.h qmlmodelnodeproxy.h \
gradientpresetitem.h \
gradientpresetlistmodel.h \
gradientpresetdefaultlistmodel.h \
gradientpresetcustomlistmodel.h
QT += qml quick QT += qml quick

View File

@@ -28,6 +28,8 @@
#include "propertyeditorvalue.h" #include "propertyeditorvalue.h"
#include "fileresourcesmodel.h" #include "fileresourcesmodel.h"
#include "gradientmodel.h" #include "gradientmodel.h"
#include "gradientpresetdefaultlistmodel.h"
#include "gradientpresetcustomlistmodel.h"
#include "qmlanchorbindingproxy.h" #include "qmlanchorbindingproxy.h"
#include "theme.h" #include "theme.h"
@@ -48,6 +50,8 @@ void Quick2PropertyEditorView::registerQmlTypes()
PropertyEditorValue::registerDeclarativeTypes(); PropertyEditorValue::registerDeclarativeTypes();
FileResourcesModel::registerDeclarativeType(); FileResourcesModel::registerDeclarativeType();
GradientModel::registerDeclarativeType(); GradientModel::registerDeclarativeType();
GradientPresetDefaultListModel::registerDeclarativeType();
GradientPresetCustomListModel::registerDeclarativeType();
Internal::QmlAnchorBindingProxy::registerDeclarativeType(); Internal::QmlAnchorBindingProxy::registerDeclarativeType();
} }
} }