function updateTsgStructWithUndoLevels(hMainFig)
%
% This function is called after change option/parameter from preferenceForm
% or tsg_initialisation
%
% Input
% -----
% hMainFig ............ Handle to the main user interface
%
% $Id$


% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');

% Number of QC history levels for UNDO function
% ---------------------------------------------
new_qc_history_size = str2double(tsg.preference.qc_history_size);

if isfield(tsg,'qc_history_size')
    
    if new_qc_history_size ~= tsg.qc_history_size
        
        % Modify size of QC history array if already created
        % --------------------------------------------------
        if ~isempty(tsg.qc_history_state)
            
            if new_qc_history_size > tsg.qc_history_size
                
                tsg.qc_history_state = cat(2,tsg.qc_history_state,int8(zeros(size(tsg.SSPS_QC,1),(new_qc_history_size-tsg.qc_history_size))));
                
            else
                
                if (tsg.qc_history_size-tsg.qc_history_pointer) >= (tsg.qc_history_size-new_qc_history_size)

                    tsg.qc_history_state = tsg.qc_history_state(:,1:new_qc_history_size);
                    tsg.qc_redo_possible = max(0,tsg.qc_redo_possible-(tsg.qc_history_size-new_qc_history_size));
                    
                else
                    
                    tsg.qc_history_state = tsg.qc_history_state(:,(1+tsg.qc_history_pointer-new_qc_history_size):tsg.qc_history_pointer);
                    tsg.qc_history_pointer = size(tsg.qc_history_state,2);
                    tsg.qc_redo_possible = 0;
                
                end
                
            end
            
        end
        
    end
    
end

tsg.qc_history_size = str2double(tsg.preference.qc_history_size);



% Save tsg structure
% ------------------
setappdata( hMainFig, 'tsg_data', tsg);

end
