History of actions (CTRL+Z, CTRL+SHIFT+Z)! Fixing a lot of bugs in the process
This commit is contained in:
parent
d0a4494038
commit
a2ff0da4cf
10 changed files with 311 additions and 36 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -16,6 +16,8 @@ docs/html
|
||||||
.directory
|
.directory
|
||||||
*.kdev4
|
*.kdev4
|
||||||
*.json
|
*.json
|
||||||
|
*.lpf
|
||||||
|
*.lgg
|
||||||
.kdev4
|
.kdev4
|
||||||
AccountFree.pro
|
AccountFree.pro
|
||||||
AccountFree.pro.user
|
AccountFree.pro.user
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
import QtQuick 2.12
|
import QtQuick 2.12
|
||||||
import QtQuick.Controls 2.12
|
import QtQuick.Controls 2.12
|
||||||
import "js/objects.js" as Objects
|
import "js/objects.js" as Objects
|
||||||
|
import "js/historylib.js" as HistoryLib
|
||||||
|
|
||||||
MenuBar {
|
MenuBar {
|
||||||
Menu {
|
Menu {
|
||||||
|
@ -53,6 +54,23 @@ MenuBar {
|
||||||
}
|
}
|
||||||
Menu {
|
Menu {
|
||||||
title: qsTr("&Edit")
|
title: qsTr("&Edit")
|
||||||
|
Action {
|
||||||
|
text: qsTr("&Undo")
|
||||||
|
shortcut: StandardKey.Undo
|
||||||
|
onTriggered: history.undo()
|
||||||
|
icon.name: 'edit-undo'
|
||||||
|
icon.color: enabled ? sysPalette.windowText : sysPaletteIn.windowText
|
||||||
|
enabled: history.undoCount > 0
|
||||||
|
}
|
||||||
|
Action {
|
||||||
|
text: qsTr("&Redo")
|
||||||
|
shortcut: StandardKey.Redo
|
||||||
|
onTriggered: history.redo()
|
||||||
|
icon.name: 'edit-redo'
|
||||||
|
icon.color: enabled ? sysPalette.windowText : sysPaletteIn.windowText
|
||||||
|
enabled: history.redoCount > 0
|
||||||
|
}
|
||||||
|
MenuSeparator { }
|
||||||
Action {
|
Action {
|
||||||
text: qsTr("&Copy diagram")
|
text: qsTr("&Copy diagram")
|
||||||
shortcut: StandardKey.Copy
|
shortcut: StandardKey.Copy
|
||||||
|
@ -74,7 +92,8 @@ MenuBar {
|
||||||
icon.name: modelData
|
icon.name: modelData
|
||||||
icon.color: sysPalette.windowText
|
icon.color: sysPalette.windowText
|
||||||
onTriggered: {
|
onTriggered: {
|
||||||
Objects.createNewRegisteredObject(modelData)
|
var newObj = Objects.createNewRegisteredObject(modelData)
|
||||||
|
history.addToHistory(new HistoryLib.CreateNewObject(newObj.name, modelData, newObj.export()))
|
||||||
objectLists.update()
|
objectLists.update()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
72
qml/History.qml
Normal file
72
qml/History.qml
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
/**
|
||||||
|
* Logarithm Graph Creator - Create graphs with logarithm scales.
|
||||||
|
* Copyright (C) 2020 Ad5001
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import QtQuick 2.12
|
||||||
|
import "js/objects.js" as Objects
|
||||||
|
import "js/historylib.js" as HistoryLib
|
||||||
|
|
||||||
|
|
||||||
|
QtObject {
|
||||||
|
// Using a QtObject is necessary in order to have proper property propagation in QML
|
||||||
|
id: historyObj
|
||||||
|
property int undoCount: 0
|
||||||
|
property int redoCount: 0
|
||||||
|
property var undoStack: []
|
||||||
|
property var redoStack: []
|
||||||
|
|
||||||
|
function empty() {
|
||||||
|
undoStack = []
|
||||||
|
redoStack = []
|
||||||
|
}
|
||||||
|
|
||||||
|
function addToHistory(action) {
|
||||||
|
if(action instanceof HistoryLib.Action) {
|
||||||
|
console.log("Added new entry to history: " + action.getReadableString())
|
||||||
|
undoStack.push(action)
|
||||||
|
undoCount++;
|
||||||
|
redoStack = []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function undo() {
|
||||||
|
if(undoStack.length > 0) {
|
||||||
|
var action = undoStack.pop()
|
||||||
|
action.undo()
|
||||||
|
objectLists.update()
|
||||||
|
redoStack.push(action)
|
||||||
|
undoCount--;
|
||||||
|
redoCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function redo() {
|
||||||
|
if(redoStack.length > 0) {
|
||||||
|
var action = redoStack.pop()
|
||||||
|
action.redo()
|
||||||
|
objectLists.update()
|
||||||
|
undoStack.push(action)
|
||||||
|
undoCount++;
|
||||||
|
redoCount--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Component.onCompleted: {
|
||||||
|
Objects.history = historyObj
|
||||||
|
Objects.HistoryLib = HistoryLib
|
||||||
|
}
|
||||||
|
}
|
1
qml/HistoryBrowser.qml
Normal file
1
qml/HistoryBrowser.qml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
|
|
@ -33,6 +33,7 @@ ApplicationWindow {
|
||||||
|
|
||||||
SystemPalette { id: sysPalette; colorGroup: SystemPalette.Active }
|
SystemPalette { id: sysPalette; colorGroup: SystemPalette.Active }
|
||||||
SystemPalette { id: sysPaletteIn; colorGroup: SystemPalette.Disabled }
|
SystemPalette { id: sysPaletteIn; colorGroup: SystemPalette.Disabled }
|
||||||
|
History { id: history }
|
||||||
|
|
||||||
menuBar: AppMenuBar {}
|
menuBar: AppMenuBar {}
|
||||||
|
|
||||||
|
@ -200,6 +201,4 @@ ApplicationWindow {
|
||||||
drawCanvas.save(file)
|
drawCanvas.save(file)
|
||||||
Helper.copyImageToClipboard()
|
Helper.copyImageToClipboard()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import QtQuick.Controls 2.12
|
||||||
import "js/objects.js" as Objects
|
import "js/objects.js" as Objects
|
||||||
import "js/mathlib.js" as MathLib
|
import "js/mathlib.js" as MathLib
|
||||||
import "js/utils.js" as Utils
|
import "js/utils.js" as Utils
|
||||||
|
import "js/historylib.js" as HistoryLib
|
||||||
|
|
||||||
|
|
||||||
ListView {
|
ListView {
|
||||||
|
@ -88,6 +89,9 @@ ListView {
|
||||||
anchors.left: parent.left
|
anchors.left: parent.left
|
||||||
anchors.leftMargin: 5
|
anchors.leftMargin: 5
|
||||||
onClicked: {
|
onClicked: {
|
||||||
|
history.addToHistory(new HistoryLib.EditedVisibility(
|
||||||
|
objEditor.obj.name, objEditor.objType, this.checked
|
||||||
|
))
|
||||||
Objects.currentObjects[objType][index].visible = this.checked
|
Objects.currentObjects[objType][index].visible = this.checked
|
||||||
objectListList.changed()
|
objectListList.changed()
|
||||||
controlRow.obj = Objects.currentObjects[objType][index]
|
controlRow.obj = Objects.currentObjects[objType][index]
|
||||||
|
@ -129,6 +133,9 @@ ListView {
|
||||||
icon.name: 'delete'
|
icon.name: 'delete'
|
||||||
|
|
||||||
onClicked: {
|
onClicked: {
|
||||||
|
history.addToHistory(new HistoryLib.DeleteObject(
|
||||||
|
objEditor.obj.name, objEditor.objType, objEditor.obj.export()
|
||||||
|
))
|
||||||
Objects.currentObjects[objType][index].delete()
|
Objects.currentObjects[objType][index].delete()
|
||||||
Objects.currentObjects[objType].splice(index, 1)
|
Objects.currentObjects[objType].splice(index, 1)
|
||||||
objectListList.update()
|
objectListList.update()
|
||||||
|
@ -158,7 +165,11 @@ ListView {
|
||||||
color: obj.color
|
color: obj.color
|
||||||
title: `Pick new color for ${objType} ${obj.name}`
|
title: `Pick new color for ${objType} ${obj.name}`
|
||||||
onAccepted: {
|
onAccepted: {
|
||||||
Objects.currentObjects[objType][index].color = color.toString()
|
history.addToHistory(new HistoryLib.EditedProperty(
|
||||||
|
objEditor.obj.name, objEditor.objType, "color",
|
||||||
|
objEditor.obj.color, color.toString()
|
||||||
|
))
|
||||||
|
objEditor.obj.color = color.toString()
|
||||||
controlRow.obj = Objects.currentObjects[objType][index]
|
controlRow.obj = Objects.currentObjects[objType][index]
|
||||||
objectListList.update()
|
objectListList.update()
|
||||||
}
|
}
|
||||||
|
@ -209,9 +220,7 @@ ListView {
|
||||||
newName = Objects.getNewName(newName)
|
newName = Objects.getNewName(newName)
|
||||||
}
|
}
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex].name = newName
|
Objects.currentObjects[objEditor.objType][objEditor.objIndex].name = newName
|
||||||
// TODO Resolve dependencies
|
|
||||||
objEditor.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex]
|
objEditor.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex]
|
||||||
//objEditor.editingRow.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex]
|
|
||||||
objectListList.update()
|
objectListList.update()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,8 +236,6 @@ ListView {
|
||||||
currentIndex: model.indexOf(objEditor.obj.labelContent)
|
currentIndex: model.indexOf(objEditor.obj.labelContent)
|
||||||
onActivated: function(newIndex) {
|
onActivated: function(newIndex) {
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex].labelContent = model[newIndex]
|
Objects.currentObjects[objEditor.objType][objEditor.objIndex].labelContent = model[newIndex]
|
||||||
//objEditor.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex]
|
|
||||||
//objEditor.editingRow.obj = objEditor.obj
|
|
||||||
objectListList.update()
|
objectListList.update()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -267,12 +274,18 @@ ListView {
|
||||||
'number': () => objEditor.obj[modelData[0]]
|
'number': () => objEditor.obj[modelData[0]]
|
||||||
}[modelData[1]]() : ""
|
}[modelData[1]]() : ""
|
||||||
onChanged: function(newValue) {
|
onChanged: function(newValue) {
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex][modelData[0]] = {
|
var newValue = {
|
||||||
'Expression': () => new MathLib.Expression(newValue),
|
'Expression': () => new MathLib.Expression(newValue),
|
||||||
'Domain': () => MathLib.parseDomain(newValue),
|
'Domain': () => MathLib.parseDomain(newValue),
|
||||||
'string': () => newValue,
|
'string': () => newValue,
|
||||||
'number': () => parseFloat(newValue)
|
'number': () => parseFloat(newValue)
|
||||||
}[modelData[1]]()
|
}[modelData[1]]()
|
||||||
|
history.addToHistory(new HistoryLib.EditedProperty(
|
||||||
|
objEditor.obj.name, objEditor.objType, modelData[0],
|
||||||
|
objEditor.obj[modelData[0]], newValue
|
||||||
|
))
|
||||||
|
//Objects.currentObjects[objEditor.objType][objEditor.objIndex][modelData[0]] = newValue
|
||||||
|
objEditor.obj[modelData[0]] = newValue
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex].update()
|
Objects.currentObjects[objEditor.objType][objEditor.objIndex].update()
|
||||||
objectListList.update()
|
objectListList.update()
|
||||||
}
|
}
|
||||||
|
@ -288,6 +301,10 @@ ListView {
|
||||||
|
|
||||||
checked: visible ? objEditor.obj[modelData[0]] : false
|
checked: visible ? objEditor.obj[modelData[0]] : false
|
||||||
onClicked: {
|
onClicked: {
|
||||||
|
history.addToHistory(new HistoryLib.EditedProperty(
|
||||||
|
objEditor.obj.name, objEditor.objType, modelData[0],
|
||||||
|
objEditor.obj[modelData[0]], this.checked
|
||||||
|
))
|
||||||
objEditor.obj[modelData[0]] = this.checked
|
objEditor.obj[modelData[0]] = this.checked
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex].update()
|
Objects.currentObjects[objEditor.objType][objEditor.objIndex].update()
|
||||||
objectListList.update()
|
objectListList.update()
|
||||||
|
@ -302,6 +319,7 @@ ListView {
|
||||||
icon: `icons/settings/custom/${parent.label}.svg`
|
icon: `icons/settings/custom/${parent.label}.svg`
|
||||||
// True to select an object of type, false for enums.
|
// True to select an object of type, false for enums.
|
||||||
property bool selectObjMode: paramTypeIn(modelData[1], ['ObjectType'])
|
property bool selectObjMode: paramTypeIn(modelData[1], ['ObjectType'])
|
||||||
|
|
||||||
model: visible ?
|
model: visible ?
|
||||||
(selectObjMode ? Objects.getObjectsName(modelData[1].objType).concat(['+ Create new ' + modelData[1].objType]) : modelData[1].values)
|
(selectObjMode ? Objects.getObjectsName(modelData[1].objType).concat(['+ Create new ' + modelData[1].objType]) : modelData[1].values)
|
||||||
: []
|
: []
|
||||||
|
@ -314,14 +332,26 @@ ListView {
|
||||||
var selectedObj = Objects.getObjectByName(model[newIndex], modelData[1].objType)
|
var selectedObj = Objects.getObjectByName(model[newIndex], modelData[1].objType)
|
||||||
if(selectedObj == null) {
|
if(selectedObj == null) {
|
||||||
selectedObj = Objects.createNewRegisteredObject(modelData[1].objType)
|
selectedObj = Objects.createNewRegisteredObject(modelData[1].objType)
|
||||||
|
history.addToHistory(new HistoryLib.CreateNewObject(selectedObj.name, modelData[1].objType, selectedObj.export()))
|
||||||
model = Objects.getObjectsName(modelData[1].objType).concat(['+ Create new ' + modelData[1].objType])
|
model = Objects.getObjectsName(modelData[1].objType).concat(['+ Create new ' + modelData[1].objType])
|
||||||
currentIndex = model.indexOf(selectedObj.name)
|
currentIndex = model.indexOf(selectedObj.name)
|
||||||
}
|
}
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex][modelData[0]].requiredBy = objEditor.obj[modelData[0]].filter((obj) => objEditor.obj.name != obj.name)
|
//Objects.currentObjects[objEditor.objType][objEditor.objIndex].requiredBy = objEditor.obj[modelData[0]].filter((obj) => objEditor.obj.name != obj.name)
|
||||||
|
objEditor.obj.requiredBy = objEditor.obj.requiredBy.filter((obj) => objEditor.obj.name != obj.name)
|
||||||
selectedObj.requiredBy.push(Objects.currentObjects[objEditor.objType][objEditor.objIndex])
|
selectedObj.requiredBy.push(Objects.currentObjects[objEditor.objType][objEditor.objIndex])
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex][modelData[0]] = selectedObj
|
history.addToHistory(new HistoryLib.EditedProperty(
|
||||||
|
objEditor.obj.name, objEditor.objType, modelData[0],
|
||||||
|
objEditor.obj[modelData[0]], selectedObj
|
||||||
|
))
|
||||||
|
//Objects.currentObjects[objEditor.objType][objEditor.objIndex][modelData[0]] = selectedObj
|
||||||
|
objEditor.obj[modelData[0]] = selectedObj
|
||||||
} else {
|
} else {
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex][modelData[0]] = model[newIndex]
|
history.addToHistory(new HistoryLib.EditedProperty(
|
||||||
|
objEditor.obj.name, objEditor.objType, modelData[0],
|
||||||
|
objEditor.obj[modelData[0]], model[newIndex]
|
||||||
|
))
|
||||||
|
//Objects.currentObjects[objEditor.objType][objEditor.objIndex][modelData[0]] = model[newIndex]
|
||||||
|
objEditor.obj[modelData[0]] = model[newIndex]
|
||||||
}
|
}
|
||||||
// Refreshing
|
// Refreshing
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex].update()
|
Objects.currentObjects[objEditor.objType][objEditor.objIndex].update()
|
||||||
|
@ -347,8 +377,15 @@ ListView {
|
||||||
forbidAdding: visible ? modelData[1].forbidAdding : false
|
forbidAdding: visible ? modelData[1].forbidAdding : false
|
||||||
|
|
||||||
onChanged: {
|
onChanged: {
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex][modelData[0]] = exportModel()
|
var exported = exportModel()
|
||||||
Objects.currentObjects[objEditor.objType][objEditor.objIndex].update()
|
history.addToHistory(new HistoryLib.EditedProperty(
|
||||||
|
objEditor.obj.name, objEditor.objType, modelData[0],
|
||||||
|
objEditor.obj[modelData[0]], exported
|
||||||
|
))
|
||||||
|
//Objects.currentObjects[objEditor.objType][objEditor.objIndex][modelData[0]] = exported
|
||||||
|
objEditor.obj[modelData[0]] = exported
|
||||||
|
//Objects.currentObjects[objEditor.objType][objEditor.objIndex].update()
|
||||||
|
objEditor.obj.update()
|
||||||
objectListList.update()
|
objectListList.update()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -368,6 +405,7 @@ ListView {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create items
|
||||||
footer: Column {
|
footer: Column {
|
||||||
id: createRow
|
id: createRow
|
||||||
width: parent.width
|
width: parent.width
|
||||||
|
@ -400,7 +438,8 @@ ListView {
|
||||||
icon.color: sysPalette.windowText
|
icon.color: sysPalette.windowText
|
||||||
|
|
||||||
onClicked: {
|
onClicked: {
|
||||||
Objects.createNewRegisteredObject(modelData)
|
var newObj = Objects.createNewRegisteredObject(modelData)
|
||||||
|
history.addToHistory(new HistoryLib.CreateNewObject(newObj.name, modelData, newObj.export()))
|
||||||
objectListList.update()
|
objectListList.update()
|
||||||
objEditor.obj = Objects.currentObjects[modelData][Objects.currentObjects[modelData].length - 1]
|
objEditor.obj = Objects.currentObjects[modelData][Objects.currentObjects[modelData].length - 1]
|
||||||
objEditor.objType = modelData
|
objEditor.objType = modelData
|
||||||
|
|
|
@ -71,7 +71,10 @@ Item {
|
||||||
var value = text
|
var value = text
|
||||||
if(control.isInt) value = Math.max(control.min,parseInt(value).toString()=="NaN"?control.min:parseInt(value))
|
if(control.isInt) value = Math.max(control.min,parseInt(value).toString()=="NaN"?control.min:parseInt(value))
|
||||||
if(control.isDouble) value = Math.max(control.min,parseFloat(value).toString()=="NaN"?control.min:parseFloat(value))
|
if(control.isDouble) value = Math.max(control.min,parseFloat(value).toString()=="NaN"?control.min:parseFloat(value))
|
||||||
if(value != "") control.changed(value)
|
if(value != "" && value.toString() != defValue) {
|
||||||
|
control.changed(value)
|
||||||
|
defValue = value.toString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
128
qml/js/historylib.js
Normal file
128
qml/js/historylib.js
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
/**
|
||||||
|
* Logarithm Graph Creator - Create graphs with logarithm scales.
|
||||||
|
* Copyright (C) 2020 Ad5001
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This library helps containing actions to be undone or redone (in other words, editing history)
|
||||||
|
// Each type of event is repertoried as an action that can be listed for everything that's undoable.
|
||||||
|
.pragma library
|
||||||
|
|
||||||
|
.import "objects.js" as Objects
|
||||||
|
.import "utils.js" as Utils
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
// Type of the action done.
|
||||||
|
static type(){return 'Unknown'}
|
||||||
|
|
||||||
|
// TargetName is the name of the object that's targeted by the event.
|
||||||
|
constructor(targetName = "", targetType = "Point") {
|
||||||
|
this.targetName = targetName
|
||||||
|
this.targetType = targetType
|
||||||
|
}
|
||||||
|
|
||||||
|
undo() {}
|
||||||
|
|
||||||
|
redo() {}
|
||||||
|
|
||||||
|
getReadableString() {
|
||||||
|
return 'Unknown action'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CreateNewObject extends Action {
|
||||||
|
// Action used for the creation of an object
|
||||||
|
static type(){return 'CreateNewObject'}
|
||||||
|
|
||||||
|
constructor(targetName = "", targetType = "Point", properties = []) {
|
||||||
|
super(targetName, targetType)
|
||||||
|
this.targetProperties = properties
|
||||||
|
}
|
||||||
|
|
||||||
|
undo() {
|
||||||
|
var targetIndex = Objects.getObjectsName(this.targetType).indexOf(this.targetName)
|
||||||
|
Objects.currentObjects[this.targetType][targetIndex].delete()
|
||||||
|
Objects.currentObjects[this.targetType].splice(targetIndex, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
redo() {
|
||||||
|
Objects.createNewRegisteredObject(this.targetType, this.targetProperties)
|
||||||
|
}
|
||||||
|
|
||||||
|
getReadableString() {
|
||||||
|
return `New ${this.targetType} ${this.targetName} created.`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DeleteObject extends CreateNewObject {
|
||||||
|
// Action used at the deletion of an object. Basicly the same thing as creating a new object, except Redo & Undo are reversed.
|
||||||
|
static type(){return 'DeleteObject'}
|
||||||
|
|
||||||
|
undo() {
|
||||||
|
super.redo()
|
||||||
|
}
|
||||||
|
|
||||||
|
redo() {
|
||||||
|
super.undo()
|
||||||
|
}
|
||||||
|
|
||||||
|
getReadableString() {
|
||||||
|
return `${this.targetType} ${this.targetName} deleted.`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class EditedProperty extends Action {
|
||||||
|
// Action used everytime an object's property has been changed
|
||||||
|
static type(){return 'EditedProperty'}
|
||||||
|
|
||||||
|
constructor(targetName = "", targetType = "Point", targetProperty = "visible", previousValue = false, newValue = true) {
|
||||||
|
super(targetName, targetType)
|
||||||
|
this.targetProperty = targetProperty
|
||||||
|
this.targetPropertyReadable = Utils.camelCase2readable(this.targetProperty)
|
||||||
|
this.previousValue = previousValue
|
||||||
|
this.newValue = newValue
|
||||||
|
}
|
||||||
|
|
||||||
|
undo() {
|
||||||
|
Objects.getObjectByName(this.targetName, this.targetType)[this.targetProperty] = this.previousValue
|
||||||
|
}
|
||||||
|
|
||||||
|
redo() {
|
||||||
|
Objects.getObjectByName(this.targetName, this.targetType)[this.targetProperty] = this.newValue
|
||||||
|
}
|
||||||
|
|
||||||
|
getReadableString() {
|
||||||
|
var prev = this.previousValue == null ? ""+this.previousValue : this.previousValue.toString()
|
||||||
|
var next = this.newValue == null ? ""+this.newValue : this.newValue.toString()
|
||||||
|
return `${this.targetPropertyReadable} of ${this.targetType} ${this.targetName} changed from "${prev}" to "${next}".`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class EditedVisibility extends EditedProperty {
|
||||||
|
// Action used everytime an object's property has been changed
|
||||||
|
static type(){return 'EditedVisibility'}
|
||||||
|
|
||||||
|
constructor(targetName = "", targetType = "Point", newValue = true) {
|
||||||
|
super(targetName, targetType, "visible", !newValue, newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
getReadableString() {
|
||||||
|
if(this.newValue) {
|
||||||
|
return `${this.targetType} ${this.targetName} shown.`
|
||||||
|
} else {
|
||||||
|
return `${this.targetType} ${this.targetName} hidden.`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -580,7 +580,7 @@ class MinusDomain extends Domain {
|
||||||
static import(frm) {
|
static import(frm) {
|
||||||
var domains = frm.trim().split("∖")
|
var domains = frm.trim().split("∖")
|
||||||
if(domains.length == 1) domains = frm.trim().split("\\") // Fallback
|
if(domains.length == 1) domains = frm.trim().split("\\") // Fallback
|
||||||
var dom1 = parseDomain(domains.pop())
|
var dom1 = parseDomain(domains.shift())
|
||||||
var dom2 = parseDomain(domains.join('∪'))
|
var dom2 = parseDomain(domains.join('∪'))
|
||||||
return new MinusDomain(dom1, dom2)
|
return new MinusDomain(dom1, dom2)
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,8 @@
|
||||||
.import "mathlib.js" as MathLib
|
.import "mathlib.js" as MathLib
|
||||||
.import "parameters.js" as P
|
.import "parameters.js" as P
|
||||||
|
|
||||||
|
var history = null
|
||||||
|
var HistoryLib = null
|
||||||
|
|
||||||
function getNewName(allowedLetters) {
|
function getNewName(allowedLetters) {
|
||||||
var newid = 0
|
var newid = 0
|
||||||
|
@ -375,6 +377,7 @@ class GainBode extends ExecutableObject {
|
||||||
om_0.name = getNewName('ω')
|
om_0.name = getNewName('ω')
|
||||||
om_0.labelContent = 'name'
|
om_0.labelContent = 'name'
|
||||||
om_0.color = this.color
|
om_0.color = this.color
|
||||||
|
history.addToHistory(new HistoryLib.CreateNewObject(om_0.name, 'Point', om_0.export()))
|
||||||
labelPosition = 'below'
|
labelPosition = 'below'
|
||||||
}
|
}
|
||||||
om_0.requiredBy.push(this)
|
om_0.requiredBy.push(this)
|
||||||
|
@ -654,6 +657,7 @@ class PhaseBode extends ExecutableObject {
|
||||||
om_0.color = this.color
|
om_0.color = this.color
|
||||||
om_0.labelContent = 'name'
|
om_0.labelContent = 'name'
|
||||||
om_0.labelPosition = this.phase.execute() >= 0 ? 'bottom' : 'top'
|
om_0.labelPosition = this.phase.execute() >= 0 ? 'bottom' : 'top'
|
||||||
|
history.addToHistory(new HistoryLib.CreateNewObject(om_0.name, 'Point', om_0.export()))
|
||||||
labelPosition = 'below'
|
labelPosition = 'below'
|
||||||
}
|
}
|
||||||
om_0.requiredBy.push(this)
|
om_0.requiredBy.push(this)
|
||||||
|
@ -886,14 +890,9 @@ class CursorX extends DrawableObject {
|
||||||
static type(){return 'X Cursor'}
|
static type(){return 'X Cursor'}
|
||||||
static typeMultiple(){return 'X Cursors'}
|
static typeMultiple(){return 'X Cursors'}
|
||||||
static properties() {
|
static properties() {
|
||||||
var elementTypes = Object.keys(currentObjects).filter(objType => types[objType].prototype instanceof ExecutableObject)
|
|
||||||
var elementNames = ['']
|
|
||||||
elementTypes.forEach(function(elemType){
|
|
||||||
elementNames = elementNames.concat(currentObjects[elemType].map(obj => obj.name))
|
|
||||||
})
|
|
||||||
return {
|
return {
|
||||||
'x': 'Expression',
|
'x': 'Expression',
|
||||||
'targetElement': new P.Enum(...elementNames),
|
'targetElement': new P.ObjectType('ExecutableObject'),
|
||||||
'labelPosition': new P.Enum('left', 'right'),
|
'labelPosition': new P.Enum('left', 'right'),
|
||||||
'approximate': 'Boolean',
|
'approximate': 'Boolean',
|
||||||
'rounding': 'number',
|
'rounding': 'number',
|
||||||
|
@ -917,6 +916,9 @@ class CursorX extends DrawableObject {
|
||||||
if(typeof x == 'number' || typeof x == 'string') x = new MathLib.Expression(x.toString())
|
if(typeof x == 'number' || typeof x == 'string') x = new MathLib.Expression(x.toString())
|
||||||
this.x = x
|
this.x = x
|
||||||
this.targetElement = targetElement
|
this.targetElement = targetElement
|
||||||
|
if(typeof targetElement == "string") {
|
||||||
|
this.targetElement = getObjectByName(targetElement, elementTypes)
|
||||||
|
}
|
||||||
this.labelPosition = labelPosition
|
this.labelPosition = labelPosition
|
||||||
this.displayStyle = displayStyle
|
this.displayStyle = displayStyle
|
||||||
this.targetValuePosition = targetValuePosition
|
this.targetValuePosition = targetValuePosition
|
||||||
|
@ -924,17 +926,17 @@ class CursorX extends DrawableObject {
|
||||||
|
|
||||||
export() {
|
export() {
|
||||||
return [this.name, this.visible, this.color.toString(), this.labelContent,
|
return [this.name, this.visible, this.color.toString(), this.labelContent,
|
||||||
this.x.toEditableString(), this.targetElement, this.labelPosition,
|
this.x.toEditableString(), this.targetElement == null ? null : this.targetElement.name, this.labelPosition,
|
||||||
this.approximate, this.rounding, this.displayStyle, this.targetValuePosition]
|
this.approximate, this.rounding, this.displayStyle, this.targetValuePosition]
|
||||||
}
|
}
|
||||||
|
|
||||||
getReadableString() {
|
getReadableString() {
|
||||||
if(this.getTargetElement() == null) return `${this.name} = ${this.x.toString()}`
|
if(this.targetElement == null) return `${this.name} = ${this.x.toString()}`
|
||||||
return `${this.name} = ${this.x.toString()}\n${this.getTargetValueLabel()}`
|
return `${this.name} = ${this.x.toString()}\n${this.getTargetValueLabel()}`
|
||||||
}
|
}
|
||||||
|
|
||||||
getTargetValueLabel() {
|
getTargetValueLabel() {
|
||||||
var t = this.getTargetElement()
|
var t = this.targetElement
|
||||||
var approx = ''
|
var approx = ''
|
||||||
if(this.approximate) {
|
if(this.approximate) {
|
||||||
approx = t.execute(this.x.execute())
|
approx = t.execute(this.x.execute())
|
||||||
|
@ -944,12 +946,6 @@ class CursorX extends DrawableObject {
|
||||||
(this.approximate ? ' ≈ ' + approx : '')
|
(this.approximate ? ' ≈ ' + approx : '')
|
||||||
}
|
}
|
||||||
|
|
||||||
getTargetElement() {
|
|
||||||
// TODO: Use the dependency system instead.
|
|
||||||
var elementTypes = Object.keys(currentObjects).filter(objType => types[objType].prototype instanceof ExecutableObject)
|
|
||||||
return getObjectByName(this.targetElement, elementTypes)
|
|
||||||
}
|
|
||||||
|
|
||||||
getLabel() {
|
getLabel() {
|
||||||
switch(this.labelContent) {
|
switch(this.labelContent) {
|
||||||
case 'name':
|
case 'name':
|
||||||
|
@ -1005,10 +1001,10 @@ class CursorX extends DrawableObject {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this.targetValuePosition == 'Next to target' && this.getTargetElement() != null) {
|
if(this.targetValuePosition == 'Next to target' && this.targetElement != null) {
|
||||||
var text = this.getTargetValueLabel()
|
var text = this.getTargetValueLabel()
|
||||||
var textSize = canvas.measureText(ctx, text)
|
var textSize = canvas.measureText(ctx, text)
|
||||||
var ypox = canvas.y2px(this.getTargetElement().execute(this.x.execute()))
|
var ypox = canvas.y2px(this.targetElement.execute(this.x.execute()))
|
||||||
switch(this.labelPosition) {
|
switch(this.labelPosition) {
|
||||||
case 'left':
|
case 'left':
|
||||||
canvas.drawVisibleText(ctx, text, xpos-textSize.width-5, ypox+textSize.height)
|
canvas.drawVisibleText(ctx, text, xpos-textSize.width-5, ypox+textSize.height)
|
||||||
|
@ -1303,8 +1299,11 @@ var currentObjects = {}
|
||||||
function getObjectByName(objName, objType = null) {
|
function getObjectByName(objName, objType = null) {
|
||||||
var objectTypes = Object.keys(currentObjects)
|
var objectTypes = Object.keys(currentObjects)
|
||||||
if(typeof objType == 'string') {
|
if(typeof objType == 'string') {
|
||||||
if(currentObjects[objType] == undefined) return null
|
if(objType == "ExecutableObject") {
|
||||||
objectTypes = [objType]
|
objectTypes = getExecutableTypes()
|
||||||
|
} else if(currentObjects[objType] != undefined) {
|
||||||
|
objectTypes = [objType]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(Array.isArray(objType)) objectTypes = objType
|
if(Array.isArray(objType)) objectTypes = objType
|
||||||
var retObj = null
|
var retObj = null
|
||||||
|
@ -1318,10 +1317,23 @@ function getObjectByName(objName, objType = null) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getObjectsName(objType) {
|
function getObjectsName(objType) {
|
||||||
|
if(objType == "ExecutableObject") {
|
||||||
|
var types = getExecutableTypes()
|
||||||
|
var elementNames = ['']
|
||||||
|
types.forEach(function(elemType){
|
||||||
|
elementNames = elementNames.concat(currentObjects[elemType].map(obj => obj.name))
|
||||||
|
})
|
||||||
|
console.log(elementNames)
|
||||||
|
return elementNames
|
||||||
|
}
|
||||||
if(currentObjects[objType] == undefined) return []
|
if(currentObjects[objType] == undefined) return []
|
||||||
return currentObjects[objType].map(obj => obj.name)
|
return currentObjects[objType].map(obj => obj.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getExecutableTypes() {
|
||||||
|
return Object.keys(currentObjects).filter(objType => types[objType].prototype instanceof ExecutableObject)
|
||||||
|
}
|
||||||
|
|
||||||
function createNewRegisteredObject(objType, args=[]) {
|
function createNewRegisteredObject(objType, args=[]) {
|
||||||
if(Object.keys(types).indexOf(objType) == -1) return null // Object type does not exist.
|
if(Object.keys(types).indexOf(objType) == -1) return null // Object type does not exist.
|
||||||
var newobj = new types[objType](...args)
|
var newobj = new types[objType](...args)
|
||||||
|
|
Loading…
Reference in a new issue