function varargout = ovladac(varargin)
% OVLADAC M-file for ovladac.fig
% pair-values:
% ovladac('hParent',hObject,... 
%         'pokyn',@prikaz,... 
%       'nazev','vykon',... 
%       'limity',[0 100 0 -100],... 
%       'nazevhodnoty','hodnota');
%      OVLADAC, by itself, creates a new OVLADAC or raises the existing
%      singleton*.
%
%      H = OVLADAC returns the handle to a new OVLADAC or the handle to
%      the existing singleton*.
%
%      OVLADAC('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in OVLADAC.M with the given input arguments.
%
%      OVLADAC('Property','Value',...) creates a new OVLADAC or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ovladac_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ovladac_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ovladac

% Last Modified by GUIDE v2.5 23-Oct-2009 20:41:46

%% Begin initialization code - DO NOT EDIT
gui_Singleton = 0;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ovladac_OpeningFcn, ...
                   'gui_OutputFcn',  @ovladac_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


%% --- Executes just before ovladac is made visible.
function ovladac_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ovladac (see VARARGIN)

% Choose default command line output for ovladac
handles.output = hObject;

%% inputparser moznosti nastaveni
p=inputParser;
p.addParamValue('pokyn',@(varargin)(eval('disp(''pokyn:''),disp(varargin)')));
p.addParamValue('hParent',[]);
p.addParamValue('nazev','');
p.addParamValue('limity',[0 100 0 -100]);
p.addParamValue('nazevhodnoty','hodnota');
p.addParamValue('promena','');
p.parse(varargin{:})
handles.pokyn=p.Results.pokyn;
handles.hParent=p.Results.hParent;
set(handles.nazev,'String',p.Results.nazev);
handles.nastaveni.limity=p.Results.limity;
handles.nazevhodnoty=p.Results.nazevhodnoty;
handles.nastaveni.promena=p.Results.promena;
%keyboard


%set(handles.axes1,'yLim',[0 430]);
fce_klik=get(handles.axes1,'ButtonDownFcn');
%% pozadi
handles.bNastav=0;
handles.nastaveni.bLogOsaHorni=0;
handles.nastaveni.bLogOsaDolni=0;
him=imshow(reshape([zeros(5,3);linspace(0,1,100)'*[0 0 1];ones(20,3);linspace(1,0,100)'*[1 0 0];zeros(5,3)],[230,1,3]));
handles.him=him;
axis normal

%% zobrazeni aktualni hodnoty
hHodnota=text(.9,115,'klikni');
set(hHodnota,'HitTest','off');
handles.hHodnota=hHodnota;
handles.aktHodnota=0;
%% cara pro aktualni hodnotu
handles.hCara=line([0.5 1.5],[120 120],'LineWidth',2,'Color','w','Visible','off','HitTest','off');
set(handles.him,'ButtonDownFcn',fce_klik);
%% nastaveni pocatecnich hodnot mezi
set(handles.kladna_dolni_mez,'String',handles.nastaveni.limity(1));
set(handles.kladna_horni_mez,'String',handles.nastaveni.limity(2));
set(handles.zaporna_dolni_mez,'String',handles.nastaveni.limity(3));
set(handles.zaporna_horni_mez,'String',handles.nastaveni.limity(4));

%% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ovladac wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ovladac_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%keyboard

handles.bNastav=~handles.bNastav;
if handles.bNastav
    set(handles.hCara,'LineStyle','--');
else
    set(handles.hCara,'LineStyle','-')
end
guidata(hObject,handles);
figure1_WindowButtonMotionFcn(hObject, eventdata, handles);
%keyboard





function kladna_horni_mez_Callback(hObject, eventdata, handles)
% hObject    handle to kladna_horni_mez (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of kladna_horni_mez as text
%        str2double(get(hObject,'String')) returns contents of kladna_horni_mez as a double
cislo=str2num(get(hObject,'String'));
if isempty(cislo)
set(hObject,'String',handles.nastaveni.limity(2));
else
    handles.nastaveni.limity(2)=cislo;guidata(hObject,handles);
end
% --- Executes during object creation, after setting all properties.
function kladna_horni_mez_CreateFcn(hObject, eventdata, handles)
% hObject    handle to kladna_horni_mez (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function kladna_dolni_mez_Callback(hObject, eventdata, handles)
% hObject    handle to kladna_dolni_mez (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of kladna_dolni_mez as text
%        str2double(get(hObject,'String')) returns contents of kladna_dolni_mez as a double
cislo=str2num(get(hObject,'String'));
if isempty(cislo)
set(hObject,'String',handles.nastaveni.limity(1));
else
    handles.nastaveni.limity(1)=cislo;guidata(hObject,handles);
end

% --- Executes during object creation, after setting all properties.
function kladna_dolni_mez_CreateFcn(hObject, eventdata, handles)
% hObject    handle to kladna_dolni_mez (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function zaporna_dolni_mez_Callback(hObject, eventdata, handles)
% hObject    handle to zaporna_dolni_mez (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of zaporna_dolni_mez as text
%        str2double(get(hObject,'String')) returns contents of zaporna_dolni_mez as a double
cislo=str2num(get(hObject,'String'));
if isempty(cislo)
set(hObject,'String',handles.nastaveni.limity(3));
else
    handles.nastaveni.limity(3)=cislo;guidata(hObject,handles);
end

% --- Executes during object creation, after setting all properties.
function zaporna_dolni_mez_CreateFcn(hObject, eventdata, handles)
% hObject    handle to zaporna_dolni_mez (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function zaporna_horni_mez_Callback(hObject, eventdata, handles)
% hObject    handle to zaporna_horni_mez (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of zaporna_horni_mez as text
%        str2double(get(hObject,'String')) returns contents of zaporna_horni_mez as a double
cislo=str2num(get(hObject,'String'));
if isempty(cislo)
set(hObject,'String',handles.nastaveni.limity(4));
else
    handles.nastaveni.limity(4)=cislo;guidata(hObject,handles);
end

% --- Executes during object creation, after setting all properties.
function zaporna_horni_mez_CreateFcn(hObject, eventdata, handles)
% hObject    handle to zaporna_horni_mez (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%keyboard
if handles.bNastav
pozice=get(handles.axes1,'CurrentPoint');
xlim=get(handles.axes1,'XLim');
ylim=get(handles.axes1,'YLim');
%disp([ylim,pozice(2)])
%keyboard
if (pozice(1)>xlim(1))&&(pozice(1)<xlim(2))
%keyboard
    pozice=round(pozice(1,1:2)+0.5);
set(handles.hCara,'Visible','on');
if pozice(2)<0
    elseif pozice(2)<5
        hodnota=handles.nastaveni.limity(2);
elseif pozice(2)<105
            if handles.nastaveni.bLogOsaHorni
            hodnota=(power(1000,(105-pozice(2))/100))/1000*(handles.nastaveni.limity(2)-handles.nastaveni.limity(1))+handles.nastaveni.limity(1);
            else
            hodnota=(105-pozice(2))/100*(handles.nastaveni.limity(2)-handles.nastaveni.limity(1))+handles.nastaveni.limity(1);
            end
     elseif pozice(2)<125
        hodnota=0;set(handles.hCara,'Visible','off');
        elseif pozice(2)<225
             if handles.nastaveni.bLogOsaDolni
             hodnota=(power(1000,(pozice(2)-125)/100)-1)/1000*(handles.nastaveni.limity(4)-handles.nastaveni.limity(3))+handles.nastaveni.limity(3);
             else    
             hodnota=(pozice(2)-125)/100*(handles.nastaveni.limity(4)-handles.nastaveni.limity(3))+handles.nastaveni.limity(3);
             end
                elseif pozice(2)<230
                hodnota=handles.nastaveni.limity(4);
end
    if exist('hodnota')&&handles.aktHodnota~=hodnota, set(handles.hHodnota,'String',num2str(hodnota));
    set(handles.hCara,'yData',[pozice(2) pozice(2)]);handles.aktHodnota=hodnota;guidata(hObject,handles);
       % keyboard
        if isempty(handles.nastaveni.promena)
            try handles.pokyn(handles.nazevhodnoty,handles.hParent,hodnota);end
        else 
            try handles.pokyn(handles.nazevhodnoty,handles.hParent,...
                    struct('hodnota',hodnota,'promena',handles.nastaveni.promena));end
        end
    end
end

end


% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
uloz.nazev=handles.nazevhodnoty;
uloz.data=handles.nastaveni;
handles.pokyn('uloz',uloz,handles.hParent);


% --- Executes on button press in logOsaHorni.
function logOsaHorni_Callback(hObject, eventdata, handles)
% hObject    handle to logOsaHorni (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of logOsaHorni
handles.nastaveni.bLogOsaHorni=get(hObject,'Value');guidata(hObject,handles);

% --- Executes on button press in logOsaDolni.
function logOsaDolni_Callback(hObject, eventdata, handles)
% hObject    handle to logOsaDolni (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of logOsaDolni

handles.nastaveni.bLogOsaDolni=get(hObject,'Value');guidata(hObject,handles);

