function varargout = GUI_main(varargin)
% GUI_MAIN MATLAB code for GUI_main.fig
%      GUI_MAIN, by itself, creates a new GUI_MAIN or raises the existing
%      singleton*.
%
%      H = GUI_MAIN returns the handle to a new GUI_MAIN or the handle to
%      the existing singleton*.
%
%      GUI_MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_MAIN.M with the given input arguments.
%
%      GUI_MAIN('Property','Value',...) creates a new GUI_MAIN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before GUI_main_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GUI_main_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 GUI_main

% Last Modified by GUIDE v2.5 17-May-2015 12:10:58

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_main_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_main_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 GUI_main is made visible.
function GUI_main_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 GUI_main (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


global ModelName;

set(gcf, 'Name', 'Simulátor QOS');

%naství optimalizaci na "žádnou"
set(handles.rbgAuto, 'SelectedObject', []);

%nastaveni defaultnich vah pro fronty
set(handles.rbgQ1weight, 'SelectedObject', handles.rbQ1weight5);
set(handles.rbgQ2weight, 'SelectedObject', handles.rbQ2weight3);
set(handles.rbgQ3weight, 'SelectedObject', handles.rbQ3weight1);

ModelName = 'diffserv_model';
open_system(ModelName);
set_param(ModelName,'BlockReduction','off'); %Zabraneni seskupeni bloku





% --- Outputs from this function are returned to the command line.
function varargout = GUI_main_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 button press in pb_start.
function pb_start_Callback(hObject, eventdata, handles)
% hObject    handle to pb_start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ModelName;

set(hObject, 'String', 'Inicializace....');

try
    sim_len = str2num(get(handles.etSimLen, 'String'));
    set_param(ModelName,'StopTime', get(handles.etSimLen, 'String'));
catch
    errordlg('Zadaná délka simulace je neplatná (pozn.: u desetinných čísel je nutné použít desetinnou tečku)')
end


%------------nastaveni parametru do simulace pomoci nastavovani promennych v base workspace,
%ktere jsou nadefinovane v model exploreru a bloky je maji jako vstupni
%paramety (Simulink.Parameter)-----------

%nastaveni konzervace práce pokud je zvoleno (přeskočí obsluhu fronty pokud je prázdná)

if get(handles.cbWorkConservation, 'Value')
    assignin('base', 'workConservation', 1);
else
    assignin('base', 'workConservation', 0);
end


%nastaveni vahovani pokud je zvoleno
if get(handles.cbWeighting, 'Value')
    assignin('base', 'weighting', 1);
else
    assignin('base', 'weighting', 0);
end

%nastaveni prioritizace pokud je zvoleno
if get(handles.cbPrioritization, 'Value')
    assignin('base', 'prioritization', 1);
else
    assignin('base', 'prioritization', 0);
end

%nastaveni preemptivniho prioritniho planovani na mnozstevni urovni pokud je zvoleno
if get(handles.cbPreemption, 'Value')
    assignin('base', 'preemption', 1);
else
    assignin('base', 'preemption', 0);
end

if get(handles.cbPolicing, 'Value')
    assignin('base', 'Q1_policer', 1);
else
    assignin('base', 'Q1_policer', 0);
end

%nastaveni delky front
assignin('base', 'Q1_length', str2num(get(handles.etQ1len, 'String')));
assignin('base', 'Q2_length', str2num(get(handles.etQ2len, 'String')));
assignin('base', 'Q3_length', str2num(get(handles.etQ3len, 'String')));

%nastaveni vahovani
assignin('base', 'Q1_weight', str2num(get(get(handles.rbgQ1weight, 'SelectedObject'), 'String')));
assignin('base', 'Q2_weight', str2num(get(get(handles.rbgQ2weight, 'SelectedObject'), 'String')));
assignin('base', 'Q3_weight', str2num(get(get(handles.rbgQ3weight, 'SelectedObject'), 'String')));




ConfigSet = getActiveConfigSet('diffserv_model'); %ziska aktualni konfiguraci z modelu

set(hObject, 'String', 'Simulace spuštěna....');

simOut = sim(ModelName, ConfigSet); %spusti simulaci a vycka dokud neskonci

set(hObject, 'String', 'Zpracování výsledků....');


%-----------vykreslení průběžného zaplnění vybraných front--------------

set(gcf,'CurrentAxes',handles.axQ1); %nastavi pro aktualni okno aktualni souradny system axQ1
plot(simOut.get('R2_O2_Q1_stacking')); %vykresli delku frontz Q1 ve vybranem QOS modulu
title('');
xlabel('Čas simulace [s]');
ylabel('Počet paketů');
xlim([0, sim_len]);
ylim([0, str2num(get(handles.etQ1len, 'String'))]);

set(gcf,'CurrentAxes',handles.axQ2);
plot(simOut.get('R1_O2_Q2_stacking')); %vykresli delku fronty Q2 ve vybranem QOS modulu
title('');
xlabel('Čas simulace [s]');
ylabel('Počet paketů');
xlim([0, sim_len]);
ylim([0, str2num(get(handles.etQ2len, 'String'))]);

set(gcf,'CurrentAxes',handles.axQ3);
plot(simOut.get('R2_O1_Q3_stacking')); %vykresli delku fronty Q3 ve vybranem QOS modulu
title('');
xlabel('Čas simulace [s]');
ylabel('Počet paketů');
xlim([0, sim_len]);
ylim([0, str2num(get(handles.etQ3len, 'String'))]);



%++++++++++++Následující výpočty berou v úvahu jen definované provozy (U1-> U3: FTP, U2 -> U3: VoIP, U1 -> U3: IM, U4(HTTP server) -> U2:HTTP)

%------------vypocet a vykresleni dosazenych parametru VoIP ---------------
VoIP_tho = ((max(simOut.get('U3_VoIP_packets_delivered')) / sim_len) * 4000) / 1000; %vypocet propustnosti v kbps (500B/paket, 8b/1B)

%vypocet ztratovosti v procentech (nepocita pakety, ktere zustaly po skonceni simulace ve fronte, 
%protoze tyto nejsou ani doruceny ani zahozeny).

%Zahozené pakety je nutno sčítat, protože některé provozy procházejí oběma směrovači.
VoIP_dropped_total = max(simOut.get('R2_O2_VoIP_packets_dropped'));
VoIP_loss = (VoIP_dropped_total / max(simOut.get('U2_VoIP_packets_sent'))) * 100;
    
delay_ts = simOut.get('U3_VoIP_packets_delay');
VoIP_avg_delay = mean(delay_ts) * 1000; %prumerne zpozdeni v ms

VoIP_max_delay = max(delay_ts) * 1000; %maximalni zpozdeni v ms

%vypocet jitteru
delay_array = delay_ts.Data;
VoIP_jitter = 0;
for i = 2:(size(delay_array)-1)
    if (delay_array(i) - delay_array(i+1)) > VoIP_jitter
        VoIP_jitter = delay_array(i) - delay_array(i+1);
    end
end
VoIP_jitter = VoIP_jitter * 1000; %prevod z s na ms


VoIP_total = max(simOut.get('U3_VoIP_packets_delivered'));

table_data = {VoIP_tho, 'kbps'; VoIP_loss, '%'; VoIP_avg_delay, 'ms'; VoIP_max_delay, 'ms'; VoIP_jitter, 'ms'; VoIP_total, 'paketů'};
set(handles.tbVoIP, 'Data', table_data); %vypise vysledky simulace do tabulky

set(gcf,'CurrentAxes',handles.axDelayVoIP); %nastavi aktualni souradny system na graf zpozdeni VoIP
plot(delay_ts); %vykresli zpozdeni VoIP
title('Průběh zpoždění');
xlabel('Čas simulace [s]');
ylabel('Paketové zpoždění [s]');
xlim([0, sim_len]);




%------------vypocet a vykresleni dosazenych parametru FTP---------------
FTP_tho = ((max(simOut.get('U3_FTP_packets_delivered')) / sim_len) * 4000) / 1000; %vypocet propustnsti v kbps (500B/paket, 8b/1B)

%vypocet ztratovosti v procentech (nepocita pakety, ktere zustaly po skonceni simulace ve fronte, 
%protoze tyto nejsou ani doruceny ani zahozeny)

%Zahozené pakety je nutno sčítat, protože některé provozy procházejí oběma směrovači.
FTP_dropped_total = max(simOut.get('R1_O2_FTP_packets_dropped')) + max(simOut.get('R2_O2_FTP_packets_dropped'));
FTP_loss = (FTP_dropped_total / max(simOut.get('U1_FTP_packets_sent'))) * 100;


delay_ts = simOut.get('U3_FTP_packets_delay');
FTP_avg_delay = mean(delay_ts) * 1000; %prumerne zpozdeni v ms

FTP_max_delay = max(delay_ts) * 1000; %maximalni zpozdeni v ms

%vypocet jitteru
delay_array = delay_ts.Data;
FTP_jitter = 0;
for i = 2:(size(delay_array)-1)
    if (delay_array(i) - delay_array(i+1)) > FTP_jitter
        FTP_jitter = delay_array(i) - delay_array(i+1);
    end
end
FTP_jitter = FTP_jitter * 1000; %prevod z s na ms

FTP_total = max(simOut.get('U3_FTP_packets_delivered'));

table_data = {FTP_tho, 'kbps'; FTP_loss, '%'; FTP_avg_delay, 'ms'; FTP_max_delay, 'ms'; FTP_jitter, 'ms'; FTP_total, 'paketů'};
set(handles.tbFTP, 'Data', table_data); %vypise vysledky simulace do tabulky

set(gcf,'CurrentAxes',handles.axDelayFTP); %nastavi aktualni souradny system na graf zpozdeni FTP
plot(delay_ts); %vykresli zpozdeni FTP
title('Průběh zpoždění');
xlabel('Čas simulace [s]');
ylabel('Paketové zpoždění [s]');
xlim([0, sim_len]);



%------------vypocet a vykresleni dosazenych parametru IM---------------
IM_tho = ((max(simOut.get('U3_IM_packets_delivered')) / sim_len) * 4000) / 1000; %vypocet propustnsti v kbps (500B/paket, 8b/1B)

%vypocet ztratovosti v procentech (nepocita pakety, ktere zustaly po skonceni simulace ve fronte, 
%protoze tyto nejsou ani doruceny ani zahozeny)

%Zahozené pakety je nutno sčítat, protože některé provozy procházejí oběma směrovači.
IM_dropped_total = max(simOut.get('R1_O2_IM_packets_dropped'))  + max(simOut.get('R2_O2_IM_packets_dropped'));
IM_loss = (IM_dropped_total / max(simOut.get('U1_IM_packets_sent'))) * 100; 

delay_ts = simOut.get('U3_IM_packets_delay');
IM_avg_delay = mean(delay_ts) * 1000; %prumerne zpozdeni v ms

IM_max_delay = max(delay_ts) * 1000; %maximalni zpozdeni v ms


%vypocet jitteru
delay_array = delay_ts.Data;
IM_jitter = 0;
for i = 2:(size(delay_array)-1)
    if (delay_array(i) - delay_array(i+1)) > IM_jitter
        IM_jitter = delay_array(i) - delay_array(i+1);
    end
end
IM_jitter = IM_jitter * 1000; %prevod z s na ms


IM_total = max(simOut.get('U3_IM_packets_delivered'));

table_data = {IM_tho, 'kbps'; IM_loss, '%'; IM_avg_delay, 'ms'; IM_max_delay, 'ms'; IM_jitter, 'ms'; IM_total, 'paketů'};
set(handles.tbIM, 'Data', table_data); %vypise vysledky simulace do tabulky

set(gcf,'CurrentAxes',handles.axDelayIM); %nastavi aktualni souradny system na graf zpozdeni IM
plot(delay_ts); %vykresli zpozdeni IM
title('Průběh zpoždění');
xlabel('Čas simulace [s]');
ylabel('Paketové zpoždění [s]');
xlim([0, sim_len]);



%------------vypocet a vykresleni dosazenych parametru HTTP---------------
HTTP_tho = ((max(simOut.get('U2_HTTP_packets_delivered')) / sim_len) * 4000) / 1000; %vypocet propustnsti v kbps (500B/paket, 8b/1B)


%vypocet ztratovosti v procentech (nepocita pakety, ktere zustaly po skonceni simulace ve fronte, 
%protoze tyto nejsou ani doruceny ani zahozeny)

%Zahozené pakety je nutno sčítat, protože některé provozy procházejí oběma směrovači.
HTTP_dropped_total = max(simOut.get('R1_O2_HTTP_packets_dropped')) + max(simOut.get('R2_O1_HTTP_packets_dropped'));
HTTP_loss = (HTTP_dropped_total / max(simOut.get('U4_HTTP_packets_sent'))) * 100;

delay_ts = simOut.get('U2_HTTP_packets_delay');
HTTP_avg_delay = mean(delay_ts) * 1000; %prumerne zpozdeni v ms

HTTP_max_delay = max(delay_ts) * 1000; %maximalni zpozdeni v ms


%vypocet jitteru
delay_array = delay_ts.Data;
HTTP_jitter = 0;
for i = 2:(size(delay_array)-1)
    if (delay_array(i) - delay_array(i+1)) > HTTP_jitter
        HTTP_jitter = delay_array(i) - delay_array(i+1);
    end
end
HTTP_jitter = HTTP_jitter * 1000; %prevod z s na ms


HTTP_total = max(simOut.get('U2_HTTP_packets_delivered'));

table_data = {HTTP_tho, 'kbps'; HTTP_loss, '%'; HTTP_avg_delay, 'ms'; HTTP_max_delay, 'ms'; HTTP_jitter, 'ms'; HTTP_total, 'paketů'};
set(handles.tbHTTP, 'Data', table_data); %vypise vysledky simulace do tabulky

set(gcf,'CurrentAxes',handles.axDelayHTTP); %nastavi aktualni souradny system na graf zpozdeni HTTP
plot(delay_ts); %vykresli zpozdeni HTTP
title('Průběh zpoždění');
xlabel('Čas simulace [s]');
ylabel('Paketové zpoždění [s]');
xlim([0, sim_len]);

%--------------------------Výpočet celkových parametrů u sledovaných provozů váženým průměrem (maximální zpoždění je max a celkově přeneseno je součet----------------
global_tho = (VoIP_tho*VoIP_total + FTP_tho*FTP_total + IM_tho*IM_total + HTTP_tho*HTTP_total) / (VoIP_total + FTP_total + IM_total + HTTP_total);

global_loss = (VoIP_loss*VoIP_total + FTP_loss*FTP_total + IM_loss*IM_total + HTTP_loss*HTTP_total) / (VoIP_total + FTP_total + IM_total + HTTP_total);

global_avg_delay = (VoIP_avg_delay*VoIP_total + FTP_avg_delay*FTP_total + IM_avg_delay*IM_total + HTTP_avg_delay*HTTP_total) / (VoIP_total + FTP_total + IM_total + HTTP_total);

global_max_delay = max([VoIP_max_delay; FTP_max_delay; IM_max_delay; HTTP_max_delay]);

global_jitter = (VoIP_jitter*VoIP_total + FTP_jitter*FTP_total + IM_jitter*IM_total + HTTP_jitter*HTTP_total) / (VoIP_total + FTP_total + IM_total + HTTP_total);

global_total = VoIP_total + FTP_total + IM_total + HTTP_total;

table_data = {global_tho, 'kbps'; global_loss, '%'; global_avg_delay, 'ms'; global_max_delay, 'ms'; global_jitter, 'ms'; global_total, 'paketů'};
set(handles.tbGlobal, 'Data', table_data); %vypise vysledky simulace do tabulky

set(hObject, 'String', 'Start simulace');


% --- Executes on button press in cbPrioritization.
function cbPrioritization_Callback(hObject, eventdata, handles)
% hObject    handle to cbPrioritization (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 cbPrioritization

if get(hObject, 'Value') && get(handles.cbWeighting, 'Value') %povoli preempci pokud je zvolena prioritizace a soucasne zapnuto vahovani
    set(handles.cbPreemption, 'Enable', 'on');
else
    set(handles.cbPreemption, 'Enable', 'off');
    set(handles.cbPreemption, 'Value', 0);    
end


% --- Executes on button press in cbWeighting.
function cbWeighting_Callback(hObject, eventdata, handles)
% hObject    handle to cbWeighting (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 cbWeighting

if get(hObject, 'Value')
    if get(handles.cbPrioritization, 'Value'); %povoli preempci pokud zapneme vahovani a je soucasne zvolena prioritizace
        set(handles.cbPreemption, 'Enable', 'on');
    end 
    set(get(handles.rbgQ1weight, 'Children'), 'Enable', 'on'); %pri zapnuti vahovani povoli vybery vah front 
    set(get(handles.rbgQ2weight, 'Children'), 'Enable', 'on');
    set(get(handles.rbgQ3weight, 'Children'), 'Enable', 'on');
else
    set(handles.cbPreemption, 'Enable', 'off');
    set(handles.cbPreemption, 'Value', 0);
    set(get(handles.rbgQ1weight, 'Children'), 'Enable', 'off');
    set(get(handles.rbgQ2weight, 'Children'), 'Enable', 'off');
    set(get(handles.rbgQ3weight, 'Children'), 'Enable', 'off');
end


% --- Executes on slider movement.
function slQ1length_Callback(hObject, eventdata, handles)
% hObject    handle to slQ1length (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%zobrazi hodnotu ze slideru ve formatu bez desetin
set(handles.etQ1len, 'String', num2str(get(hObject, 'Value'), '%1.0f'));


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slQ2length_Callback(hObject, eventdata, handles)
% hObject    handle to slQ2length (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
set(handles.etQ2len, 'String', num2str(get(hObject, 'Value'), '%1.0f'));

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



% --- Executes on slider movement.
function slQ3length_Callback(hObject, eventdata, handles)
% hObject    handle to slQ3length (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
set(handles.etQ3len, 'String', num2str(get(hObject, 'Value'), '%1.0f'));


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- Executes during object creation, after setting all properties.
function etQ2len_CreateFcn(hObject, eventdata, handles)
% hObject    handle to etQ2len (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 during object creation, after setting all properties.
function etQ3len_CreateFcn(hObject, eventdata, handles)
% hObject    handle to etQ3len (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 during object creation, after setting all properties.
function etQ1len_CreateFcn(hObject, eventdata, handles)
% hObject    handle to etQ1len (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 when selected object is changed in rbgQ1weight.
function rbgQ1weight_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in rbgQ1weight 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)



% --- Executes on button press in tbManual.
function tbManual_Callback(hObject, eventdata, handles)
% hObject    handle to tbManual (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 tbManual

%Pokud je vybrán manuální mód, povolí ovládací prvky
if get(hObject, 'Value')
    set(handles.cbPrioritization, 'Enable', 'on');
    set(handles.cbWeighting, 'Enable', 'on');
    set(handles.cbWorkConservation, 'Enable', 'on');
    set(handles.cbPolicing, 'Enable', 'on');
    set(get(handles.rbgAuto, 'Children'), 'Enable', 'off');
    
if get(handles.cbWeighting, 'Value')
    if get(handles.cbPrioritization, 'Value') %povoli preempci pokud zapneme vahovani a je soucasne zvolena prioritizace
        set(handles.cbPreemption, 'Enable', 'on');
    end 
    set(get(handles.rbgQ1weight, 'Children'), 'Enable', 'on') %pri zapnuti vahovani povoli vybery vah front 
    set(get(handles.rbgQ2weight, 'Children'), 'Enable', 'on');
    set(get(handles.rbgQ3weight, 'Children'), 'Enable', 'on');
else
    set(handles.cbPreemption, 'Enable', 'off');
    set(handles.cbPreemption, 'Value', 0);
    set(get(handles.rbgQ1weight, 'Children'), 'Enable', 'off');
    set(get(handles.rbgQ2weight, 'Children'), 'Enable', 'off');
    set(get(handles.rbgQ3weight, 'Children'), 'Enable', 'off');
end
    
%následující řádky nastavují defaultní hodnoty
%{    
    set(handles.cbWorkConservation, 'Value', 0);
    set(handles.cbWeighting, 'Value', 0);
    set(handles.cbPrioritization, 'Value', 0);
    set(handles.cbPreemption, 'Value', 0);
    set(handles.cbPolicing, 'Value', 0);
    set(handles.slQ1length, 'Value', 25);
    set(handles.etQ1len, 'String', '25');
    set(handles.slQ2length, 'Value', 25);
    set(handles.etQ2len, 'String', '25');
    set(handles.slQ3length, 'Value', 25);
    set(handles.etQ3len, 'String', '25');
    set(handles.rbgQ1weight, 'SelectedObject', handles.rbQ1weight5);
    set(handles.rbgQ2weight, 'SelectedObject', handles.rbQ2weight3);
    set(handles.rbgQ3weight, 'SelectedObject', handles.rbQ3weight1);
    %}
else
    set(handles.cbPrioritization, 'Enable', 'off');
    set(handles.cbWeighting, 'Enable', 'off');
    set(handles.cbWorkConservation, 'Enable', 'off');
    set(handles.cbPolicing, 'Enable', 'off');
    set(handles.cbPreemption, 'Enable', 'off');
    set(get(handles.rbgAuto, 'Children'), 'Enable', 'on');
    set(handles.rbgAuto, 'SelectedObject', '');
end;


% --- Executes when selected object is changed in rbgAuto.
function rbgAuto_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in rbgAuto 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)

switch get(get(handles.rbgAuto, 'SelectedObject'), 'Tag')
    
    case 'rbThoroughput'
        set(handles.cbWorkConservation, 'Value', 1);
        set(handles.cbWeighting, 'Value', 1);
        set(handles.cbPrioritization, 'Value', 0);
        set(handles.cbPreemption, 'Value', 0);
        set(handles.cbPolicing, 'Value', 0);
        set(handles.slQ1length, 'Value', 25);
        set(handles.etQ1len, 'String', '25');
        set(handles.slQ2length, 'Value', 25);
        set(handles.etQ2len, 'String', '25');
        set(handles.slQ3length, 'Value', 25);
        set(handles.etQ3len, 'String', '25');
        set(handles.rbgQ1weight, 'SelectedObject', handles.rbQ1weight5);
        set(handles.rbgQ2weight, 'SelectedObject', handles.rbQ2weight10);
        set(handles.rbgQ3weight, 'SelectedObject', handles.rbQ3weight1);
        
    case 'rbDelay'
        set(handles.cbWorkConservation, 'Value', 1);
        set(handles.cbWeighting, 'Value', 1);
        set(handles.cbPrioritization, 'Value', 1);
        set(handles.cbPreemption, 'Value', 1);
        set(handles.cbPolicing, 'Value', 1);
        set(handles.slQ1length, 'Value', 1);
        set(handles.etQ1len, 'String', '1');
        set(handles.slQ2length, 'Value', 3);
        set(handles.etQ2len, 'String', '3');
        set(handles.slQ3length, 'Value', 3);
        set(handles.etQ3len, 'String', '3');
        set(handles.rbgQ1weight, 'SelectedObject', handles.rbQ1weight1);
        set(handles.rbgQ2weight, 'SelectedObject', handles.rbQ2weight3);
        set(handles.rbgQ3weight, 'SelectedObject', handles.rbQ3weight3);
        
    case 'rbJitter'
        set(handles.cbWorkConservation, 'Value', 1);
        set(handles.cbWeighting, 'Value', 0);
        set(handles.cbPrioritization, 'Value', 1);
        set(handles.cbPreemption, 'Value', 0);
        set(handles.cbPolicing, 'Value', 1);
        set(handles.slQ1length, 'Value', 1);
        set(handles.etQ1len, 'String', '1');
        set(handles.slQ2length, 'Value', 10);
        set(handles.etQ2len, 'String', '10');
        set(handles.slQ3length, 'Value', 10);
        set(handles.etQ3len, 'String', '10');
        set(handles.rbgQ1weight, 'SelectedObject', handles.rbQ1weight1);
        set(handles.rbgQ2weight, 'SelectedObject', handles.rbQ2weight1);
        set(handles.rbgQ3weight, 'SelectedObject', handles.rbQ3weight1);
        
    case 'rbLoss'
        set(handles.cbWorkConservation, 'Value', 1);
        set(handles.cbWeighting, 'Value', 1);
        set(handles.cbPrioritization, 'Value', 0);
        set(handles.cbPreemption, 'Value', 0);
        set(handles.cbPolicing, 'Value', 0);
        set(handles.slQ1length, 'Value', 100);
        set(handles.etQ1len, 'String', '100');
        set(handles.slQ2length, 'Value', 100);
        set(handles.etQ2len, 'String', '100');
        set(handles.slQ3length, 'Value', 100);
        set(handles.etQ3len, 'String', '100');
        set(handles.rbgQ1weight, 'SelectedObject', handles.rbQ1weight5);
        set(handles.rbgQ2weight, 'SelectedObject', handles.rbQ2weight5);
        set(handles.rbgQ3weight, 'SelectedObject', handles.rbQ3weight3);
                
end;

            



function etSimLen_Callback(hObject, eventdata, handles)
% hObject    handle to etSimLen (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 etSimLen as text
%        str2double(get(hObject,'String')) returns contents of etSimLen as a double

if ~isempty(get(hObject,'String'))
    val = str2num(get(hObject, 'String'));
    if (val < 1) || (val > 10)
        errordlg('Zadejte hodnotu 1 - 10!');
        set(hObject, 'String', '');
    end;
end;
