%function handles = View(m)
classdef View < handle
    %VIEW  a GUI representation of the signal model
    properties
        h
        model
        mouse
        key
        
        menuBar
        
        % gui handles
        fig
        ax
        line
        img
        ctrlPointsPlot
        figurePanel
        buttonPanel
        tab1
        tab2
        tab3
        tab4
        tab5
        btnTab5
        btnTab4
        btnTab3
        fromPointPanel
        toPointPanel
        popupStrings
        popupStringsImg
        %experimental
        hSlider
        hContainer
        hRangeSlider
        hRangeContainer
    end
    methods
        function obj = View(m)
            
            obj.initGUI(); %% build the GUI
            obj.model = m;
            obj.updatePopup()
            onChangeImg(obj); % populate with initial values
            % handles = initGUI();
            % %onChangedF(handles, m);    % populate with initial values
            % onChangeImg(handles,m)
            % observe on model changes and update view accordingly
            % (tie listener to model object lifecycle)
            %addlistener(m, 'f','PostSet', @(o,e) onChangedF(handles,m));  %@(o,e) onChangedF(handles,e.AffectedObject));
            
            addlistener(m, 'displayedImg','PostSet', @(o,e) obj.onChangeImg());
            addlistener(m, 'CtrlPointsChanged', @(o,e) obj.onChangeCtrlPoints());
            addlistener(m, 'ClosestPointChanged', @(o,e) obj.onChangeClosestPoints());
            addlistener(m,'CtrlPointsStructChanged',@(o,e) obj.updatePopup());
            %addlistener(m,'ImgStructChanged',@(o,e) obj.updatePopupImg());
            %     addlistener(m, 'EventFrequencyChanged', @(o,e) onChangedF(handles,m));  %@(o,e) onChangedF(handles,e.AffectedObject));
            obj.mouse = struct('buttonDown',0,'buttonPulse',1,'position',[NaN,NaN],'holdAndDrag',0);
            obj.key = struct('control',0,'shift',0);
        end
    end
    methods
        function initGUI(obj)
            %% initialize GUI controls structure
            obj.fig = figure('Menubar','none','Visible','off','Name','Pre-deform GUI','NumberTitle','off');
            
            
            obj.initMenuBar(); % import, export, projector, webcam
            obj.initGUIaxes();
            
            % button panel
            obj.buttonPanel= uipanel('Parent',obj.fig,'BackgroundColor','white',...
                'Position',[0 0.8 1 0.2]);
            
            hTabgp= uitabgroup(obj.buttonPanel,'Position',[.05 .05 0.9 0.9]); % R2014b
            obj.tab5= uitab('Parent',hTabgp, 'Title', 'Manage images','Tag','tab5'); % R2014b
            obj.tab1= uitab('Parent',hTabgp, 'Title', 'Calibration','Tag','tab1'); % R2014b
            %obj.tab2= uitab('Parent',hTabgp, 'Title', 'Tabnr. 2','Tag','tab2'); % R2014b
%             obj.tab3= uitab('Parent',hTabgp, 'Title', 'Animation Control','Tag','tab3'); % R2014b
            obj.tab4= uitab('Parent',hTabgp, 'Title', 'Control Points','Tag','tab4'); % R2014b
            
            
            obj.fromPointPanel = uipanel('Parent',obj.tab3,'BackgroundColor','white',...
                'BorderType','none','Position',[0 0.5 0.5 1]);
            obj.toPointPanel = uipanel('Parent',obj.tab3,'BackgroundColor','yellow',...
                'BorderType','none','Position',[0.5 0.5 0.5 1]);
            %% buttons Tab1 - calibration
            buttonsString1 = {'<html>Aquire Image for<br>camera perspective (QR)','<html>Aquire Images for<br>brigthness/contrast','<html>Aquire Images for<br>pre-deformation','<html>Send checkerboard<br>to projector',...
                '<html>Find QR corners<br>correct perspective','<html>Compile <br>darkfield, clearfield','<html>Find checkerboard<br>for pre-deformation','<html>Calculate <br>pre-deformation'};
%             buttonsString2 = {'SSH beagle','Disconnect Socket','b3','b4','b5',};
            layout = [2,Inf];
            obj.fillTabWithButtons(obj.tab1,buttonsString1,layout)
%             obj.fillTabWithButtons(obj.tab2,buttonsString2,layout)
            %% buttons Tab3
            %left top
%             uicontrol( 'Parent',obj.tab3,'Style','text', ...
%                 'String', "Starting deformation",'Units', 'normalized','Position' , [0 0.75 0.25 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.fromPointPanel.BackgroundColor);
%             obj.popupStrings = uicontrol( 'Parent',obj.tab3,'Style','popupmenu', ...
%                 'String', {'Empty'},'Units', 'normalized','Position' , [0 0.5 0.25 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.fromPointPanel.BackgroundColor);
%             uicontrol( 'Parent',obj.tab3,'Style','edit', ...
%                 'String', '0','Units', 'normalized','Position' , [0.25 0.5 0.125 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.fromPointPanel.BackgroundColor);
%             uicontrol( 'Parent',obj.tab3,'Style','text', ...
%                 'String', "%",'Units', 'normalized','Position' , [0.25+0.125 0.5 0.0625 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.fromPointPanel.BackgroundColor);
%             
%             % FPS
%             uicontrol( 'Parent',obj.tab3,'Style','text', ...
%                 'String', "FPS",'Units', 'normalized','Position' , [0.5 0.25 0.0625 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             uicontrol( 'Parent',obj.tab3,'Style','edit', ...
%                 'String', "20",'Units', 'normalized','Position' , [0.5+0.0625 0.25 0.0625 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             uicontrol( 'Parent',obj.tab3,'Style','text', ...
%                 'String', "18",'Units', 'normalized','Position' , [0.5+2*0.0625 0.25 0.0625 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             uicontrol( 'Parent',obj.tab3,'Style','text', ...
%                 'String', "Duration",'Units', 'normalized','Position' , [0.75 0.25 0.1250 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             uicontrol( 'Parent',obj.tab3,'Style','edit', ...
%                 'String', "20",'Units', 'normalized','Position' , [0.75+0.1250 0.25 0.0625 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             uicontrol( 'Parent',obj.tab3,'Style','text', ...
%                 'String', "Start delay",'Units', 'normalized','Position' , [0.5 0.0 0.1250 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             uicontrol( 'Parent',obj.tab3,'Style','edit', ...
%                 'String', "www3s",'Units', 'normalized','Position' , [0.5+0.1250 0 0.0625 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             
%             %right top
%             uicontrol( 'Parent',obj.tab3,'Style','text', ...
%                 'String', "Ending deformation",'Units', 'normalized','Position' , [0.5 0.75 0.25 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             obj.popupStrings(2) = uicontrol( 'Parent',obj.tab3,'Style','popupmenu', ...
%                 'String', {'Empty'},'Units', 'normalized','Position' , [0.5 0.5 0.25 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             uicontrol( 'Parent',obj.tab3,'Style','edit', ...
%                 'String', '0','Units', 'normalized','Position' , [0.75 0.5 0.125 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             uicontrol( 'Parent',obj.tab3,'Style','text', ...
%                 'String', "%",'Units', 'normalized','Position' , [0.75+0.125 0.5 0.0625 0.25 ],...
%                 'Tag','StartText','BackgroundColor',obj.toPointPanel.BackgroundColor);
%             
%             %slider1
%             jSlider = javax.swing.JSlider(0,100,12); % min,max,set
%             set(jSlider, 'MajorTickSpacing',10, 'PaintLabels',false,...
%                 'MinorTickSpacing',5,'PaintTicks',false,'Background',java.awt.Color.white);
%             [obj.hSlider,obj.hContainer] = javacomponent(jSlider,[5,0,300,30],obj.tab3); % [x y width height],parent
%             %range slider
%             jRangeSlider = com.jidesoft.swing.RangeSlider(0,100,20,70);  % min,max,low,high
%             set(jRangeSlider, 'MajorTickSpacing',25, 'MinorTickSpacing',5, 'PaintTicks',false, 'PaintLabels',false, ...
%                 'Background',java.awt.Color.white);
%             [obj.hRangeSlider,obj.hRangeContainer] = javacomponent(jRangeSlider, [5,15,300,15], obj.tab3);
%             obj.btnTab3{1} = uicontrol( 'Parent',obj.tab3,'Style','pushbutton', ...
%                 'String', 'Add to que','Units', 'normalized','Position' , [0.75 0 0.25 0.25 ],...
%                 'Tag','StartText');
            
            %% panel 4
            uicontrol( 'Parent',obj.tab4,'Style','text', ...
                'String', "Control points name:",'Units', 'normalized','Position' , [0 0.75 0.25 0.25 ],...
                'Tag','StartText','BackgroundColor','white');
            obj.popupStrings = uicontrol( 'Parent',obj.tab4,'Style','popupmenu', ...
                'String', {'Empty'},'Units', 'normalized','Position' , [0.25 0.75 0.25 0.25 ],...
                'Tag','StartText','BackgroundColor','white');
%             uicontrol( 'Parent',obj.tab4,'Style','text', ...
%                 'String', "Select point group",'Units', 'normalized','Position' , [0 0.5 0.25 0.25 ],...
%                 'Tag','StartText','BackgroundColor','white');
%             uicontrol( 'Parent',obj.tab4,'Style','popupmenu', ...
%                 'String', {'Not implemented'},'Units', 'normalized','Position' , [0.25 0.5 0.25 0.25 ],...
%                 'Tag','StartText','BackgroundColor','white');
            
            %save
            obj.btnTab4{1} = uicontrol( 'Parent',obj.tab4,'Style','pushbutton', ...
                'String', 'Save','Units', 'normalized','Position' , [0 0 0.25 0.5 ],...
                'Tag','StartText');
            obj.btnTab4{2} = uicontrol( 'Parent',obj.tab4,'Style','pushbutton', ...
                'String', 'Save As','Units', 'normalized','Position' , [0.25 0 0.25 0.5 ],...
                'Tag','StartText');
            obj.btnTab4{3} = uicontrol( 'Parent',obj.tab4,'Style','pushbutton', ...
                'String', 'Load','Units', 'normalized','Position' , [0.5 0 0.25 0.5 ],...
                'Tag','StartText');
            obj.btnTab4{4} = uicontrol( 'Parent',obj.tab4,'Style','pushbutton', ...
                'String', 'Rename','Units', 'normalized','Position' , [0.75 0 0.25 0.5 ],...
                'Tag','StartText');
            
            obj.btnTab4{5} = uicontrol( 'Parent',obj.tab4,'Style','pushbutton', ...
                'String', 'Deform','Units', 'normalized','Position' , [0.5 0.75 0.25 0.25 ],...
                'Tag','StartText');
            
            %%
            obj.Tab5Init
            % customize
            
            %xlabel(obj.ax, 't [s]')
            %ylabel(obj.ax, 'Amplitude')
            
            % return a structure of GUI handles
            % handles = struct('fig',hFig, 'ax',obj.ax, 'line', obj.line, 'img',obj.img, ...
            %     'slider',hSlid,'figurePanel',hPanelTop,'buttonPanel',hPanel,'tab1',hTab1,'tab2',hTab2);
        end
        function Tab5Init(obj)
            %% panel 5
            uicontrol( 'Parent',obj.tab5,'Style','text', ...
                'String', "Selected Image",'Units', 'normalized','Position' , [0 0.75 0.25 0.25 ],...
                'Tag','StartText','BackgroundColor','white');
            obj.popupStringsImg = uicontrol( 'Parent',obj.tab5,'Style','popupmenu', ...
                'String', {'Empty'},'Units', 'normalized','Position' , [0.25 0.75 0.25 0.25 ],...
                'Tag','StartText','BackgroundColor','white');
%             obj.btnTab5{1} = uicontrol( 'Parent',obj.tab5,'Style','checkbox', ...
%                 'String', "Filter: Used in",'Units', 'normalized','Position' , [0.05 0.5 0.25 0.25 ],...
%                 'Tag','StartText','BackgroundColor','white');
%             obj.btnTab5{2}.Key = [];
%             obj.popupStringsImg(2) = uicontrol( 'Parent',obj.tab5,'Style','popupmenu', ...
%                 'String', {'Not implemented'},'Units', 'normalized','Position' , [0.25 0.5 0.25 0.25 ],...
%                 'Tag','StartText','BackgroundColor','white');
            %save
            obj.btnTab5{3} = uicontrol( 'Parent',obj.tab5,'Style','pushbutton', ...
                'String', 'Send 2 BBB','Units', 'normalized','Position' , [0 0 0.25 0.5 ],...
                'Tag','StartText');
            obj.btnTab5{4} = uicontrol( 'Parent',obj.tab5,'Style','pushbutton', ...
                'String', 'Save As','Units', 'normalized','Position' , [0.25 0 0.25 0.5 ],...
                'Tag','StartText');
            obj.btnTab5{5} = uicontrol( 'Parent',obj.tab5,'Style','pushbutton', ...
                'String', 'Load','Units', 'normalized','Position' , [0.5 0 0.25 0.5 ],...
                'Tag','StartText');
            obj.btnTab5{6} = uicontrol( 'Parent',obj.tab5,'Style','pushbutton', ...
                'String', 'Rename','Units', 'normalized','Position' , [0.75 0 0.25 0.5 ],...
                'Tag','StartText');
            obj.btnTab5{7} = uicontrol( 'Parent',obj.tab5,'Style','pushbutton', ...
                'String', 'fliplr()','Units', 'normalized','Position' , [0.5 0.5 0.25 0.5 ],...
                'Tag','StartText');
            obj.btnTab5{8} = uicontrol( 'Parent',obj.tab5,'Style','pushbutton', ...
                'String', 'flipud()','Units', 'normalized','Position' , [0.75 0.5 0.25 0.5 ],...
                'Tag','StartText');
            
        end
        function ReSizingWindowFcn(obj,imgSize)
            % define in px
            buttonPanelHeightPx = 100;
            bottomPanelHeightPx = 20;
            TabGrpBordrPx = 0.025*buttonPanelHeightPx;
            % resize uipanel (from px to normalized)
            uiPanelsBorder = buttonPanelHeightPx/obj.fig.Position(4);
            obj.buttonPanel.Position(4) = uiPanelsBorder;
            obj.buttonPanel.Position(2) = 1-uiPanelsBorder;
            obj.figurePanel.Position(2) = bottomPanelHeightPx/obj.fig.Position(4);
            obj.figurePanel.Position(4) = 1-uiPanelsBorder - bottomPanelHeightPx/obj.fig.Position(4);
            % resize uitabgroup
            h = buttonPanelHeightPx; w = obj.fig.Position(3); b = TabGrpBordrPx;
            obj.buttonPanel.Children.Position = [b/w, b/h, (w-2*b)/w, (h-2*b)/h] ;
            obj.buttonPanel.Children.Position = [0,0,1,1];
            % resize figure to fit to screen (screen ratio and centering maitained
            % by matlab - disabled Stretch-to-Fill Behavior)
            obj.ax.Position(4) = 1;
            set(obj.ax,'XLim',[0,imgSize(2)],'YLim',[0,imgSize(1)])
            set(obj.hContainer,'position',[5,0,w/2-5,15]); %note container size change
            set(obj.hRangeContainer,'position',[5,15,w/2-5,15]); %note container size change
        end
        function initMenuBar(obj)
            % import
            obj.menuBar.Import = uimenu(obj.fig,'Text','&Import');
            obj.menuBar.ImportCtrlPoints = uimenu(obj.menuBar.Import,'Text','Import control points');
%             obj.menuBar.ImportCtrlPointsOption{1} = uimenu(obj.menuBar.Import,'Text','  Option 1','Checked','off','Separator','on');
%             obj.menuBar.ImportCtrlPointsOption{2} = uimenu(obj.menuBar.Import,'Text','  Option 2','Checked','on');
            obj.menuBar.ImportImg = uimenu(obj.menuBar.Import,'Text','Import Image');
%             obj.menuBar.ImportImgFolder = uimenu(obj.menuBar.Import,'Text','Load image folder');
            
            % export
            obj.menuBar.Export = uimenu(obj.fig,'Text','Export');
            obj.menuBar.ExportCtrlPoints = uimenu(obj.menuBar.Export,'Text','Export control points');
            obj.menuBar.ExportImg = uimenu(obj.menuBar.Export,'Text','Export Image');
            
            % projector
            obj.menuBar.Projector = uimenu(obj.fig,'Text','Projector');
            obj.menuBar.ProjectorStatus = uimenu(obj.menuBar.Projector,'Text','Status: Disconected - simulation mode','enable','off');
            obj.menuBar.ProjectorConnect = uimenu(obj.menuBar.Projector,'Text','Connect','enable','on');
            obj.menuBar.ProjectorON = uimenu(obj.menuBar.Projector,'Text','Turn Light On','enable','off');
            obj.menuBar.ProjectorOFF = uimenu(obj.menuBar.Projector,'Text','Turn Light Off','enable','off');
            
            obj.menuBar.ProjectorSend = uimenu(obj.menuBar.Projector,'Text','Send img','Separator','on','enable','off');
            obj.menuBar.ProjectorSynch = uimenu(obj.menuBar.Projector,'Text','Sync img with GUI','Checked','off','enable','off');
            
            obj.menuBar.ProjectorPreDeform = uimenu(obj.menuBar.Projector,'Text','Apply Pre-deformation','Checked','off','Separator','on','enable','off');
            obj.menuBar.ProjectorBrightnessCorr = uimenu(obj.menuBar.Projector,'Text','Apply brightess correction','Checked','off','enable','off');
            obj.menuBar.ProjectorBrightnessCorr2 = uimenu(obj.menuBar.Projector,'Text','Apply brightess correction2','Checked','off','enable','off');

            
            % camera
            obj.menuBar.Cam = uimenu(obj.fig,'Text','Camera');
            obj.menuBar.CamSelect = uimenu(obj.menuBar.Cam,'Text','Select camera');
            obj.menuBar.Cam_Open = uimenu(obj.menuBar.Cam,'Text','Open preview','enable','on');
            obj.menuBar.Cam_Close = uimenu(obj.menuBar.Cam,'Text','Close preview','enable','off');
            obj.menuBar.Cam_Snap = uimenu(obj.menuBar.Cam,'Text','Take snapshot','enable','on');
            
            
        end
        function initGUIaxes(obj)
            % figure panel
            obj.figurePanel= uipanel('Parent',obj.fig,'BackgroundColor','white',...
                'Position',[0 0.05 1 0.75]);
            obj.ax = axes('Parent',obj.figurePanel,'DataAspectRatio',[1 1 1],'YDir', 'reverse');
            obj.img = image('Parent',obj.ax,'cData',[],'HitTest','off'); % pass the ButtonDownCallback to parent
            for i =1:6
                obj.ctrlPointsPlot(i) = line('XData',NaN, 'YData',NaN, 'Parent',obj.ax,'HitTest','off');
            end
            set(obj.ax,'YTick',[],'XTick',[],'XColor', 'none','YColor','none')
            obj.ax.Position = obj.ax.OuterPosition;
        end
        
        function fillTabWithButtons(~,tab,buttonsString,layout)
            % debug layout:
            % buttonsString = cellstr(strcat(tab.Tag,'_',num2str(1:length(buttonsString),'%d').'));
            
            layout = [layout(1),ceil(length(buttonsString)/layout(1))];
            height = 1/layout(1);
            width = 1/layout(2);
            pos1 = linspace(0,1,layout(1)+1);
            pos2 = linspace(0,1,layout(2)+1);
            [pos1,pos2]=meshgrid(pos1(1:end-1),pos2(1:end-1));
            pos = [pos2(:),flip(pos1(:))];
            % childrens are numbered
            for i = length(buttonsString):-1:1
                uicontrol( 'style','pushbutton', 'Parent',tab,...
                    'string', buttonsString{i},...
                    'units', 'normalized' ,...
                    'position' , [pos(i,:) width height],...
                    'tag',num2str(i),...
                    'callback' , 'set(Text1,''string'',''Done'')');
                
            end
        end
        
        function onChangedF(handles,model)
            %     % respond to model changes by updating view
            %     if ~ishghandle(handles.fig), return, end
            set(handles.line, 'XData',model.t, 'YData',model.x)
            set(handles.slider, 'Value',model.f);
            disp('plot updated')
            model.f
            drawnow
        end
        function onChangeImg(obj)
            set(obj.img,'cData',obj.model.displayedImg)
        end
        function onChangeCtrlPoints(obj)
            if ~isempty(obj.model.CtrlPointsInitDone)
                %fprintf('Ploting points\n')
                id1 = obj.model.selectedCtrlPoints;
                id2 = obj.model.dispCtrlPoints(end).selectedPointGroup;
                if ~isempty(obj.model.dispCtrlPoints(end).p{id2})
                    %p
                    set(obj.ctrlPointsPlot,'YData',[],'XData',[],'LineStyle','none','Marker','.',...
                        'MarkerSize',15,'MarkerEdgeColor','g','MarkerFaceColor','none');
                    set(obj.ctrlPointsPlot, 'XData',obj.model.dispCtrlPoints(end).p{id2}(:,1),...
                        'YData',obj.model.dispCtrlPoints(end).p{id2}(:,2));
                    %q
                    set(obj.ctrlPointsPlot(2),'YData',[],'XData',[],'LineStyle','none','Marker','.',...
                        'MarkerSize',15,'MarkerEdgeColor','r','MarkerFaceColor','none');
                    set(obj.ctrlPointsPlot(2), 'XData',obj.model.dispCtrlPoints(end).q{id2}(:,1),...
                        'YData',obj.model.dispCtrlPoints(end).q{id2}(:,2));
                end
            end
        end
        function onChangeClosestPoints(obj)
            if ~isempty(obj.model.CtrlPointsInitDone)
                %fprintf('Ploting points\n')
                id1 = obj.model.selectedCtrlPoints;
                id2 = obj.model.dispCtrlPoints(end).selectedPointGroup;
                set(obj.ctrlPointsPlot(3),'YData',[],'XData',[],'LineStyle','none','Marker','.',...
                    'MarkerSize',30,'MarkerEdgeColor','g','MarkerFaceColor','none');
                set(obj.ctrlPointsPlot(4),'YData',[],'XData',[],'LineStyle','none','Marker','.',...
                    'MarkerSize',30,'MarkerEdgeColor','r','MarkerFaceColor','none');
                if obj.model.closestCP.draw && (length(obj.model.dispCtrlPoints(end).p{id2}(:,1)) >=obj.model.closestCP.drawID)
                    % closest p
                    
                    set(obj.ctrlPointsPlot(3), 'XData',obj.model.dispCtrlPoints(end).p{id2}(obj.model.closestCP.drawID,1),...
                        'YData',obj.model.dispCtrlPoints(end).p{id2}(obj.model.closestCP.drawID,2));
                    % closest q
                    
                    set(obj.ctrlPointsPlot(4), 'XData',obj.model.dispCtrlPoints(end).q{id2}(obj.model.closestCP.drawID,1),...
                        'YData',obj.model.dispCtrlPoints(end).q{id2}(obj.model.closestCP.drawID,2));
                end
            end
        end
        function updatePopup(obj)
            if isempty(obj.model.ctrlPoints)
                for i = 1:length(obj.popupStrings)
                    obj.popupStrings(i).String = {'empty'};
                end
            else
                for i = 1:length(obj.popupStrings)
                    
                    obj.popupStrings(i).String = {obj.model.ctrlPoints(:).tag};
                end
                obj.popupStrings(1).Value = obj.model.selectedCtrlPoints;
            end
        end
        
        function updatePopupImg(obj)
            if isempty(obj.model.libraryImgs)
                for i = 1:length(obj.popupStringsImg)
                    obj.popupStringsImg(i).String = {'empty'};
                end
            end
            IDs = obj.btnTab5{2}.customP;
            
            if isempty(IDs)
                IDStr = {obj.model.libraryImgs(:).Name};
                selectedID = obj.model.assigned2Imgs.ToDisplay;
            else
                IDStr = {obj.model.libraryImgs(IDs).Name};
                % add dispalyed as first if not within filter criteria (add*)
                if ~any(ismember(obj.model.assigned2Imgs.ToDisplay,IDs))
                    IDs = [obj.model.assigned2Imgs.ToDisplay,IDs];
                    
                    st1 = sprintf('%s*',obj.model.libraryImgs(obj.model.assigned2Imgs.ToDisplay).Name);
                    IDStr = {st1,IDStr{:}};
                    %obj.btnTab5{2}.customP = IDs;  
                end
                selectedID = find(obj.model.assigned2Imgs.ToDisplay==IDs);
                
                
            end
            obj.popupStringsImg(1).String = IDStr;
            obj.popupStringsImg(1).Value = selectedID;
            
        end
        function importImgTypesPrint(obj)
            % struct with image formats
            formatInfo = imformats;
            % long string of all formats
            allFormats = char(join(strcat('*.',[formatInfo(:).ext]),';'));
            % join eg. {'*.jpg', '*.jpeg'} into {'*.jpg; *.jpeg'}
            in = cellfun(@(x)  char(join(strcat('*.',x),'; ')),{formatInfo.ext},'UniformOutput',false);
            % supported format + description
            in = {in{:};formatInfo.description}';
            for i = 1:length(in)
                in{i,2} = sprintf('%s (%s)',in{i,2},in{i,1});
            end
            
            obj.menuBar.ImgImportIn = {allFormats,in{:,1};'All imread() supported file formats',in{:,2}}';
            obj.menuBar.ImgExportOut = {in{:,1};in{:,2}}';
        end
    end
end
