Skip to content

Commit

Permalink
implemented the possibility to switch on/off undo in different datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
N-thony committed Oct 22, 2024
1 parent 743cec7 commit d4f93dd
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 8 deletions.
2 changes: 2 additions & 0 deletions instat/Interface/IDataViewGrid.vb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Public Interface IDataViewGrid

Event WorksheetChanged()

Event WorksheetInserted()

Event WorksheetRemoved(worksheet As clsWorksheetAdapter)

Event FindRow()
Expand Down
27 changes: 27 additions & 0 deletions instat/Model/DataFrame/clsDataFramePage.vb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,33 @@ Public Class clsDataFramePage

End Sub

Public Function IsUndo(strCurrentDataFrame As String)
Dim clsIsUndoFunction As New RFunction
Dim expTemp As SymbolicExpression
clsIsUndoFunction.SetRCommand(_clsRLink.strInstatDataObject & "$is_undo")
clsIsUndoFunction.AddParameter("data_name", Chr(34) & strCurrentDataFrame & Chr(34))

If clsIsUndoFunction IsNot Nothing Then
expTemp = frmMain.clsRLink.RunInternalScriptGetValue(clsIsUndoFunction.ToScript(), bSilent:=True)
If expTemp IsNot Nothing AndAlso expTemp.AsCharacter(0) = "TRUE" Then
Return True
End If
End If

Return False
End Function

Public Sub DisableEnableUndo(bDisable As Boolean, strCurrentDataFrame As String)
Dim clsEnableDisableUndoRFunction As New RFunction
clsEnableDisableUndoRFunction.SetRCommand(_clsRLink.strInstatDataObject & "$set_enable_disable_undo")
clsEnableDisableUndoRFunction.AddParameter("data_name", Chr(34) & strCurrentDataFrame & Chr(34))

Dim strDisable As String = If(bDisable, "TRUE", "FALSE")
clsEnableDisableUndoRFunction.AddParameter("disable_undo", strDisable)
_clsRLink.RunScript(clsEnableDisableUndoRFunction.ToScript)

End Sub

Public Function HasUndoHistory()
Dim expTemp As SymbolicExpression
Dim bHasHistory As Boolean = False
Expand Down
2 changes: 2 additions & 0 deletions instat/UserControls/DataGrid/Linux/ucrDataViewLinuxGrid.vb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Public Class ucrDataViewLinuxGrid

Public Event WorksheetChanged() Implements IDataViewGrid.WorksheetChanged

Public Event WorksheetInserted() Implements IDataViewGrid.WorksheetInserted

Public Event WorksheetRemoved(worksheet As clsWorksheetAdapter) Implements IDataViewGrid.WorksheetRemoved

Public Sub AddColumns(visiblePage As clsDataFramePage) Implements IDataViewGrid.AddColumns
Expand Down
7 changes: 7 additions & 0 deletions instat/UserControls/DataGrid/ReoGrid/ucrDataViewReoGrid.vb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Public Class ucrDataViewReoGrid

Public Event WorksheetChanged() Implements IDataViewGrid.WorksheetChanged

Public Event WorksheetInserted() Implements IDataViewGrid.WorksheetInserted

Public Event WorksheetRemoved(worksheet As clsWorksheetAdapter) Implements IDataViewGrid.WorksheetRemoved

Public Sub AddColumns(visiblePage As clsDataFramePage) Implements IDataViewGrid.AddColumns
Expand Down Expand Up @@ -222,6 +224,11 @@ Public Class ucrDataViewReoGrid
RaiseEvent WorksheetChanged()
End Sub

Private Sub grdData_WorksheetInserted(sender As Object, e As EventArgs) Handles grdData.WorksheetInserted
RaiseEvent WorksheetInserted()
End Sub


Private Sub grdData_WorksheetRemoved(sender As Object, e As WorksheetRemovedEventArgs) Handles grdData.WorksheetRemoved
RaiseEvent WorksheetRemoved(New clsWorksheetAdapter(e.Worksheet))
End Sub
Expand Down
1 change: 1 addition & 0 deletions instat/dlgOptions.vb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ Public Class dlgOptions
frmMain.clsInstatOptions.SetMaximumOutputsHeight(If(ucrChkMaxOutputsHeight.Checked, ucrNudMaxOutputsHeight.Value, -1))

frmMain.clsInstatOptions.ExecuteRGlobalOptions()
frmMain.DisableEnableUndo(ucrChkTurnOffUndo.Checked)
End Sub

Private Sub SetView()
Expand Down
4 changes: 4 additions & 0 deletions instat/frmMain.vb
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,10 @@ Public Class frmMain
End If
End Sub

Public Sub DisableEnableUndo(bDisable As Boolean)
ucrDataViewer.DisableEnableUndo(bDisable)
End Sub

Private Sub mnuFileNewDataFrame_Click(sender As Object, e As EventArgs) Handles mnuFileNewDataFrame.Click
dlgNewDataFrame.ShowDialog()
End Sub
Expand Down
24 changes: 16 additions & 8 deletions instat/static/InstatObject/R/data_object_R6.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DataSheet <- R6::R6Class("DataSheet",
imported_from = "",
messages = TRUE, convert=TRUE, create = TRUE,
start_point=1, filters = list(), column_selections = list(), objects = list(),
calculations = list(), scalars = list(), keys = list(), comments = list(), keep_attributes = TRUE, undo_history = list(), redo_undo_history = list())
calculations = list(), scalars = list(), keys = list(), comments = list(), keep_attributes = TRUE, undo_history = list(), redo_undo_history = list(), disable_undo = FALSE)
{
# Set up the data object
self$set_data(data, messages)
Expand All @@ -31,7 +31,6 @@ DataSheet <- R6::R6Class("DataSheet",
self$set_scalars(scalars)
self$set_keys(keys)
self$set_comments(comments)
self$set_undo_history(undo_history)

# If no name for the data.frame has been given in the list we create a default one.
# Decide how to choose default name index
Expand Down Expand Up @@ -65,7 +64,8 @@ DataSheet <- R6::R6Class("DataSheet",
comments = list(),
calculations = list(),
scalars = list(),
changes = list(),
changes = list(),
disable_undo = FALSE,
.current_filter = list(),
.current_column_selection = list(),
.data_changed = FALSE,
Expand Down Expand Up @@ -163,11 +163,19 @@ DataSheet$set("public", "set_data", function(new_data, messages=TRUE, check_name
}
}
)
DataSheet$set("public", "set_enable_disable_undo", function(disable_undo) {
private$disable_undo <- disable_undo
if(disable_undo) {private$undo_history <- list()}
})

DataSheet$set("public", "save_state_to_undo_history", function() {
self$set_undo_history(list(private$data))
})

DataSheet$set("public", "is_undo", function() {
return(private$disable_undo)
})

DataSheet$set("public", "set_meta", function(new_meta) {
meta_data_copy <- new_meta
self$clear_metadata()
Expand All @@ -192,14 +200,14 @@ DataSheet$set("public", "clear_metadata", function() {
)

DataSheet$set("public", "has_undo_history", function() {
return(length(private$undo_history) > 1)
return(length(private$undo_history) > 0)
}
)

DataSheet$set("public", "undo_last_action", function() {

# Perform the undo action
if (length(private$undo_history) > 1) {
if (length(private$undo_history) > 0) {
previous_state <- private$undo_history[[length(private$undo_history)]]
self$set_data(as.data.frame(previous_state)) # Restore the previous state

Expand Down Expand Up @@ -293,7 +301,7 @@ DataSheet$set("public", "set_scalars", function(new_scalars) {
# Set undo_history with memory management
DataSheet$set("public", "set_undo_history", function(new_undo_history) {
if (!is.list(new_undo_history)) stop("undo_history must be of type: list")

if(!private$disable_undo){
# Define memory and undo_history limits
MAX_undo_history_SIZE <- 10 # Limit to last 10 undo_history states
MAX_MEMORY_LIMIT_MB <- 1024 # Limit the memory usage for undo undo_history
Expand All @@ -314,9 +322,9 @@ DataSheet$set("public", "set_undo_history", function(new_undo_history) {
}

private$undo_history <- append(private$undo_history, list(new_undo_history))

# Explicitly call garbage collection to release memory
gc()
#gc()
}
})

DataSheet$set("public", "set_keys", function(new_keys) {
Expand Down
9 changes: 9 additions & 0 deletions instat/static/InstatObject/R/instat_object_R6.R
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,15 @@ DataBook$set("public","has_key", function(data_name) {
}
)

DataBook$set("public","set_enable_disable_undo", function(data_name, disable_undo) {
self$get_data_objects(data_name)$set_enable_disable_undo(disable_undo)
}
)

DataBook$set("public", "is_undo", function(data_name) {
self$get_data_objects(data_name)$is_undo()
})

DataBook$set("public","has_undo_history", function(data_name) {
self$get_data_objects(data_name)$has_undo_history()
}
Expand Down
19 changes: 19 additions & 0 deletions instat/ucrDataView.vb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Public Class ucrDataView

Private Sub AttachEventsToGrid()
AddHandler _grid.WorksheetChanged, AddressOf CurrentWorksheetChanged
AddHandler _grid.WorksheetInserted, AddressOf WorksheetInserted
AddHandler _grid.WorksheetRemoved, AddressOf WorksheetRemoved
AddHandler _grid.ReplaceValueInData, AddressOf ReplaceValueInData
AddHandler _grid.PasteValuesToDataframe, AddressOf PasteValuesToDataFrame
Expand All @@ -101,6 +102,7 @@ Public Class ucrDataView
_grid.UpdateWorksheetStyle(fillWorkSheet)
dataFrame.clsVisibleDataFramePage.HasChanged = False
frmMain.mnuUndo.Enabled = dataFrame.clsVisibleDataFramePage.HasUndoHistory

RefreshDisplayInformation()
End Sub

Expand Down Expand Up @@ -248,9 +250,14 @@ Public Class ucrDataView
dlgName.ShowDialog()
End Sub

Public Sub WorksheetInserted()
DisableEnableUndo(frmMain.clsInstatOptions.bSwitchOffUndo)
End Sub

Public Sub CurrentWorksheetChanged()
frmMain.ucrColumnMeta.SetCurrentDataFrame(GetCurrentDataFrameNameFocus())
RefreshDisplayInformation()
IsUndo()
End Sub

Public Function GetFirstRowHeader() As String
Expand All @@ -269,6 +276,12 @@ Public Class ucrDataView
_grid.AdjustColumnWidthAfterWrapping(strColumn, bApplyWrap)
End Sub

Public Sub IsUndo()
If GetWorkSheetCount() <> 0 AndAlso _clsDataBook IsNot Nothing AndAlso GetCurrentDataFrameFocus() IsNot Nothing Then
frmMain.clsInstatOptions.SetOffUndo(GetCurrentDataFrameFocus.clsVisibleDataFramePage.IsUndo(GetCurrentDataFrameNameFocus))
End If
End Sub

Private Sub RefreshDisplayInformation()
If GetWorkSheetCount() <> 0 AndAlso _clsDataBook IsNot Nothing AndAlso GetCurrentDataFrameFocus() IsNot Nothing Then
frmMain.tstatus.Text = _grid.CurrentWorksheet.Name
Expand All @@ -282,6 +295,12 @@ Public Class ucrDataView
End If
End Sub

Public Sub DisableEnableUndo(bDisable As Boolean)
If GetWorkSheetCount() <> 0 AndAlso _clsDataBook IsNot Nothing AndAlso GetCurrentDataFrameFocus() IsNot Nothing Then
GetCurrentDataFrameFocus.clsVisibleDataFramePage.DisableEnableUndo(bDisable, GetCurrentDataFrameNameFocus)
End If
End Sub

Private Sub ResizeLabels()
Const iMinSize As Single = 4.5
TblPanPageDisplay.Font = New Font(TblPanPageDisplay.Font.FontFamily, 12, TblPanPageDisplay.Font.Style)
Expand Down

0 comments on commit d4f93dd

Please sign in to comment.