classdef SelectDefData < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        SelectDefDataUIFigure        matlab.ui.Figure
        cbChamberLog                 matlab.ui.control.CheckBox
        cbStateDiagram               matlab.ui.control.CheckBox
        cbHistVoxelTimes             matlab.ui.control.CheckBox
        cbDefvsTicks                 matlab.ui.control.CheckBox
        cbVoxeltimevstime            matlab.ui.control.CheckBox
        cbMaximumdeflectionpervoxel  matlab.ui.control.CheckBox
        cbDefvstime                  matlab.ui.control.CheckBox
        nedEndtimesec                matlab.ui.control.NumericEditField
        EndtimesecEditFieldLabel     matlab.ui.control.Label
        nedStarttimesec              matlab.ui.control.NumericEditField
        StarttimesecEditFieldLabel   matlab.ui.control.Label
        OKButton                     matlab.ui.control.Button
    end

    properties (Access = public)
        Mainapp % calling app object (VCG)
        filenp  % active print file to analyze
    end

    methods (Access = private)

    end


    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app, CallingApp, IS)
            app.Mainapp=CallingApp;
            app.SelectDefDataUIFigure.Name = ('Select data to display');
            SDD_GUI_ApplyColors(app);
            
            app.SelectDefDataUIFigure.Position(1) = app.Mainapp.VCGfigure.Position(1)+...
                app.Mainapp.VCGfigure.Position(3);
            app.SelectDefDataUIFigure.Position(2) = app.Mainapp.VCGfigure.Position(2)+...
                app.Mainapp.VCGfigure.Position(4)-app.SelectDefDataUIFigure.Position(4);
            
            app.filenp = IS.filenp;
            load(app.filenp, 'AD');
            if AD.DevFlag && app.Mainapp.DevFlag % show all graphs only in dev mode
                app.cbDefvsTicks.Visible    = 'on';
                app.cbStateDiagram.Visible  = 'on';
                app.cbChamberLog.Visible    = 'on';
            else
                app.cbDefvsTicks.Visible    = 'off';
                app.cbStateDiagram.Visible  = 'off';
                app.cbChamberLog.Visible    = 'off';
            end
            maxPrintTime = AD.TotPrintTime;
            if maxPrintTime > 1000
                app.nedEndtimesec.Value = maxPrintTime;
                app.nedStarttimesec.Value = maxPrintTime-500;
            else
                app.nedEndtimesec.Value = maxPrintTime;
                app.nedStarttimesec.Value = 0;
            end
        end

        % Button pushed function: OKButton
        function OKButtonPushed(app, event)
            SDD_RunDataAnalysis(app);
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create SelectDefDataUIFigure and hide until all components are created
            app.SelectDefDataUIFigure = uifigure('Visible', 'off');
            app.SelectDefDataUIFigure.AutoResizeChildren = 'off';
            app.SelectDefDataUIFigure.Position = [100 100 405 367];
            app.SelectDefDataUIFigure.Name = 'MATLAB App';
            app.SelectDefDataUIFigure.Resize = 'off';
            app.SelectDefDataUIFigure.WindowStyle = 'modal';

            % Create OKButton
            app.OKButton = uibutton(app.SelectDefDataUIFigure, 'push');
            app.OKButton.ButtonPushedFcn = createCallbackFcn(app, @OKButtonPushed, true);
            app.OKButton.Position = [273 19 109 22];
            app.OKButton.Text = 'OK';

            % Create StarttimesecEditFieldLabel
            app.StarttimesecEditFieldLabel = uilabel(app.SelectDefDataUIFigure);
            app.StarttimesecEditFieldLabel.HorizontalAlignment = 'right';
            app.StarttimesecEditFieldLabel.Position = [53 307 87 22];
            app.StarttimesecEditFieldLabel.Text = 'Start time (sec)';

            % Create nedStarttimesec
            app.nedStarttimesec = uieditfield(app.SelectDefDataUIFigure, 'numeric');
            app.nedStarttimesec.ValueDisplayFormat = '%11.8g';
            app.nedStarttimesec.Tooltip = {'Set a start time for the graphs to plot.'};
            app.nedStarttimesec.Position = [149 307 100 22];

            % Create EndtimesecEditFieldLabel
            app.EndtimesecEditFieldLabel = uilabel(app.SelectDefDataUIFigure);
            app.EndtimesecEditFieldLabel.HorizontalAlignment = 'right';
            app.EndtimesecEditFieldLabel.Position = [53 270 83 22];
            app.EndtimesecEditFieldLabel.Text = 'End time (sec)';

            % Create nedEndtimesec
            app.nedEndtimesec = uieditfield(app.SelectDefDataUIFigure, 'numeric');
            app.nedEndtimesec.ValueDisplayFormat = '%11.8g';
            app.nedEndtimesec.Tooltip = {'Set an end time for the graphs to plot.'};
            app.nedEndtimesec.Position = [149 270 100 22];

            % Create cbDefvstime
            app.cbDefvstime = uicheckbox(app.SelectDefDataUIFigure);
            app.cbDefvstime.Text = 'Deflection vs time';
            app.cbDefvstime.Position = [53 239 117 16];
            app.cbDefvstime.Value = true;

            % Create cbMaximumdeflectionpervoxel
            app.cbMaximumdeflectionpervoxel = uicheckbox(app.SelectDefDataUIFigure);
            app.cbMaximumdeflectionpervoxel.Text = 'Maximum deflection per voxel';
            app.cbMaximumdeflectionpervoxel.Position = [53 208 181 16];

            % Create cbVoxeltimevstime
            app.cbVoxeltimevstime = uicheckbox(app.SelectDefDataUIFigure);
            app.cbVoxeltimevstime.Text = 'Voxel time vs time';
            app.cbVoxeltimevstime.Position = [53 177 119 16];

            % Create cbDefvsTicks
            app.cbDefvsTicks = uicheckbox(app.SelectDefDataUIFigure);
            app.cbDefvsTicks.Visible = 'off';
            app.cbDefvsTicks.Text = 'Deflection vs ticks';
            app.cbDefvsTicks.Position = [53 115 118 16];

            % Create cbHistVoxelTimes
            app.cbHistVoxelTimes = uicheckbox(app.SelectDefDataUIFigure);
            app.cbHistVoxelTimes.Text = 'Histogram of voxel times';
            app.cbHistVoxelTimes.Position = [53 146 154 16];

            % Create cbStateDiagram
            app.cbStateDiagram = uicheckbox(app.SelectDefDataUIFigure);
            app.cbStateDiagram.Visible = 'off';
            app.cbStateDiagram.Text = 'State diagram with deflection vs ticks';
            app.cbStateDiagram.Position = [53 85 220 16];

            % Create cbChamberLog
            app.cbChamberLog = uicheckbox(app.SelectDefDataUIFigure);
            app.cbChamberLog.Visible = 'off';
            app.cbChamberLog.Text = 'Chamber log';
            app.cbChamberLog.Position = [53 49 220 22];

            % Show the figure after all components are created
            app.SelectDefDataUIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = SelectDefData(varargin)

            runningApp = getRunningApp(app);

            % Check for running singleton app
            if isempty(runningApp)

                % Create UIFigure and components
                createComponents(app)

                % Register the app with App Designer
                registerApp(app, app.SelectDefDataUIFigure)

                % Execute the startup function
                runStartupFcn(app, @(app)startupFcn(app, varargin{:}))
            else

                % Focus the running singleton app
                figure(runningApp.SelectDefDataUIFigure)

                app = runningApp;
            end

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.SelectDefDataUIFigure)
        end
    end
end