2021-04-06 17:06:09 +00:00
/ * *
2022-01-20 17:19:36 +00:00
* LogarithmPlotter - 2 D plotter software to make BODE plots , sequences and repartition functions .
2022-01-12 17:47:17 +00:00
* Copyright ( C ) 2022 Ad5001
2021-04-06 17:06:09 +00:00
*
* 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
2021-05-07 22:34:11 +00:00
. import "mathlib.js" as MathLib
2021-04-06 17:06:09 +00:00
2021-08-14 16:57:29 +00:00
var history = null ;
2021-04-06 17:06:09 +00:00
class Action {
// Type of the action done.
2021-05-07 22:34:11 +00:00
type ( ) { return 'Unknown' }
2021-04-06 17:06:09 +00:00
// 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 ( ) { }
2021-05-07 22:34:11 +00:00
export ( ) {
return [ this . targetName , this . targetType ]
}
2021-04-06 17:06:09 +00:00
getReadableString ( ) {
return 'Unknown action'
}
}
class CreateNewObject extends Action {
// Action used for the creation of an object
2021-05-07 22:34:11 +00:00
type ( ) { return 'CreateNewObject' }
2021-04-06 17:06:09 +00:00
constructor ( targetName = "" , targetType = "Point" , properties = [ ] ) {
super ( targetName , targetType )
this . targetProperties = properties
}
2021-05-07 22:34:11 +00:00
2021-04-06 17:06:09 +00:00
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 )
}
2021-05-07 22:34:11 +00:00
export ( ) {
return [ this . targetName , this . targetType , this . targetProperties ]
}
2021-04-06 17:06:09 +00:00
getReadableString ( ) {
2022-01-22 22:18:25 +00:00
return qsTr ( "New %1 %2 created." ) . arg ( Objects . types [ this . targetType ] . displayType ( ) ) . arg ( this . targetName )
2021-04-06 17:06:09 +00:00
}
}
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.
2021-05-07 22:34:11 +00:00
type ( ) { return 'DeleteObject' }
2021-04-06 17:06:09 +00:00
undo ( ) {
super . redo ( )
}
redo ( ) {
super . undo ( )
}
getReadableString ( ) {
2022-01-22 22:18:25 +00:00
return qsTr ( "%1 %2 deleted." ) . arg ( Objects . types [ this . targetType ] . displayType ( ) ) . arg ( this . targetName )
2021-04-06 17:06:09 +00:00
}
}
class EditedProperty extends Action {
// Action used everytime an object's property has been changed
2021-05-07 22:34:11 +00:00
type ( ) { return 'EditedProperty' }
2021-04-06 17:06:09 +00:00
2021-05-07 22:34:11 +00:00
constructor ( targetName = "" , targetType = "Point" , targetProperty = "visible" , previousValue = false , newValue = true , valueIsExpressionNeedingImport = false ) {
2021-04-06 17:06:09 +00:00
super ( targetName , targetType )
this . targetProperty = targetProperty
this . targetPropertyReadable = Utils . camelCase2readable ( this . targetProperty )
this . previousValue = previousValue
this . newValue = newValue
2021-05-07 22:34:11 +00:00
if ( valueIsExpressionNeedingImport ) {
this . previousValue = new MathLib . Expression ( this . previousValue ) ;
this . newValue = new MathLib . Expression ( this . newValue ) ;
}
2021-04-06 17:06:09 +00:00
}
undo ( ) {
Objects . getObjectByName ( this . targetName , this . targetType ) [ this . targetProperty ] = this . previousValue
}
redo ( ) {
Objects . getObjectByName ( this . targetName , this . targetType ) [ this . targetProperty ] = this . newValue
}
2021-05-07 22:34:11 +00:00
export ( ) {
if ( this . previousValue instanceof MathLib . Expression ) {
return [ this . targetName , this . targetType , this . targetProperty , this . previousValue . toEditableString ( ) , this . newValue . toEditableString ( ) , true ]
} else {
return [ this . targetName , this . targetType , this . targetProperty , this . previousValue , this . newValue , false ]
}
}
2021-04-06 17:06:09 +00:00
getReadableString ( ) {
var prev = this . previousValue == null ? "" + this . previousValue : this . previousValue . toString ( )
var next = this . newValue == null ? "" + this . newValue : this . newValue . toString ( )
2022-01-22 22:18:25 +00:00
return qsTr ( '%1 of %2 %3 changed from "%4" to "%5".' ) . arg ( this . targetPropertyReadable ) . arg ( Objects . types [ this . targetType ] . displayType ( ) ) . arg ( this . targetName ) . arg ( prev ) . arg ( next )
2021-04-06 17:06:09 +00:00
}
}
class EditedVisibility extends EditedProperty {
// Action used everytime an object's property has been changed
2021-05-07 22:34:11 +00:00
type ( ) { return 'EditedVisibility' }
2021-04-06 17:06:09 +00:00
constructor ( targetName = "" , targetType = "Point" , newValue = true ) {
super ( targetName , targetType , "visible" , ! newValue , newValue )
}
getReadableString ( ) {
if ( this . newValue ) {
2022-01-20 17:19:36 +00:00
return qsTr ( '%1 %2 shown.' ) . arg ( this . targetType ) . arg ( this . targetName )
2021-04-06 17:06:09 +00:00
} else {
2022-01-20 17:19:36 +00:00
return qsTr ( '%1 %2 hidden.' ) . arg ( this . targetType ) . arg ( this . targetName )
2021-04-06 17:06:09 +00:00
}
}
}
2021-05-07 22:34:11 +00:00
class NameChanged extends EditedProperty {
// Action used everytime an object's property has been changed
type ( ) { return 'EditedVisibility' }
constructor ( targetName = "" , targetType = "Point" , newName = "" ) {
super ( targetName , targetType , "name" , targetName , newName )
}
undo ( ) {
Objects . getObjectByName ( this . newValue , this . targetType ) [ 'name' ] = this . previousValue
}
redo ( ) {
Objects . getObjectByName ( this . previousValue , this . targetType ) [ this . targetProperty ] = this . newValue
}
}
var Actions = {
"Action" : Action ,
"CreateNewObject" : CreateNewObject ,
"DeleteObject" : DeleteObject ,
"EditedProperty" : EditedProperty ,
"EditedVisibility" : EditedVisibility ,
}