function varargout = rggui(varargin)
%RGGUI  GUI for random graph generation
%
%    Start GUI by typing  rggui
%
%  Last change:  10/2/2005 - Florian Knorn

%  The GUI also requires rggui.fig !

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @rggui_OpeningFcn, ...
                   'gui_OutputFcn',  @rggui_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 rggui is made visible.
function rggui_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

% Choose default command line output for rggui
handles.output = hObject;
global A;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = rggui_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;

% ========================  RADIO  =======================================================

% --- Executes during object creation, after setting all properties.
function radiobutton1_CreateFcn(hObject, eventdata, handles)
set(handles.edit3,'visible','off');
set(handles.text3,'visible','on');
set(handles.text4,'visible','off');
set(handles.text5,'visible','off');
set(handles.text6,'visible','off');
set(handles.text7,'visible','off');
set(handles.text8,'visible','on');
set(handles.text9,'visible','off');
set(handles.text10,'visible','off');

% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
set(handles.edit3,'visible','off');
set(handles.text3,'visible','on');
set(handles.text4,'visible','off');
set(handles.text5,'visible','off');
set(handles.text6,'visible','off');
set(handles.text7,'visible','off');
set(handles.text8,'visible','on');
set(handles.text9,'visible','off');
set(handles.text10,'visible','off');
set(handles.edit2,'String','0.5');
edit2_Callback(handles.edit2, [], handles);
edit3_Callback(handles.edit3, [], handles);

% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
set(handles.edit3,'visible','on');
set(handles.text3,'visible','off');
set(handles.text4,'visible','on');
set(handles.text5,'visible','on');
set(handles.text6,'visible','off');
set(handles.text7,'visible','off');
set(handles.text8,'visible','off');
set(handles.text9,'visible','on');
set(handles.text10,'visible','off');
set(handles.edit3,'string','0.2');
edit2_Callback(handles.edit2, [], handles);
edit3_Callback(handles.edit3, [], handles);

% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
set(handles.edit3,'visible','on');
set(handles.text3,'visible','off');
set(handles.text4,'visible','off');
set(handles.text5,'visible','off');
set(handles.text6,'visible','on');
set(handles.text7,'visible','on');
set(handles.text8,'visible','off');
set(handles.text9,'visible','off');
set(handles.text10,'visible','on');
set(handles.edit2,'String','1');
set(handles.edit3,'String','1');
edit2_Callback(handles.edit2, [], handles);
edit3_Callback(handles.edit3, [], handles);

% =========================  EDITS  ======================================================

% --- EDIT 1
function edit1_Callback(hObject, eventdata, handles)
val = str2double(get(hObject,'String'));
if val < 1
	set(hObject,'String','1');
	val = str2double(get(hObject,'String'));
end
if round(val) ~= val
	set(hObject,'String',num2str(round(val)));
end

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- EDIT 2
function edit2_Callback(hObject, eventdata, handles)
val = str2double(get(hObject,'String'));
if get(handles.radiobutton1,'Value') == get(handles.radiobutton1,'Max') % er-situation
	if val < 0
		set(hObject,'String','0');
	elseif val > 1
		set(hObject,'String','1');
	end
elseif get(handles.radiobutton2,'Value') == get(handles.radiobutton1,'Max') % sw-situation
	if val < 2
		set(hObject,'String','2');
		val = str2double(get(hObject,'String'));
	end
	if fix(val) ~= val % not an integer...
		set(hObject,'String',int2str(fix(val)));
		val = str2double(get(hObject,'String'));
	end
	if mod(val,2) ~= 0
		set(hObject,'String',int2str(val+1));
	end
end
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- EDIT 3
function edit3_Callback(hObject, eventdata, handles)
val = str2double(get(hObject,'String'));
if get(handles.radiobutton2,'Value') == get(handles.radiobutton2,'Max') % sw-situation
	if val < 0
		set(hObject,'String','0');
	elseif val > 1
		set(hObject,'String','1');
	end
end
if get(handles.radiobutton3,'Value') == get(handles.radiobutton3,'Max') % sf-situation
	if val < 1
		set(hObject,'String','1');
		val = str2double(get(hObject,'String'));
	end
	if round(val) ~= val % not an integer...
		set(hObject,'String',int2str(round(val)));
		val = str2double(get(hObject,'String'));
	end
	if val > str2double(get(handles.edit2,'String'))
		set(hObject,'String',get(handles.edit2,'String'));
	end
end


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% ===========================  BUTTONS  ==================================================


% --- CLOSE
function pushbutton1_Callback(hObject, eventdata, handles)
close;


% --- GRAPH
function pushbutton3_Callback(hObject, eventdata, handles)
global A;
if ~isempty(A)
	a2p(A,'temp.net',1);
end


% --- GENERATE
function pushbutton2_Callback(hObject, eventdata, handles)
global A;
val1 = str2double(get(handles.edit1,'String'));
val2 = str2double(get(handles.edit2,'String'));
val3 = str2double(get(handles.edit3,'String'));
if get(handles.radiobutton1,'Value') == get(handles.radiobutton1,'Max') % er-situation
	if val1 < 1 || val2 < 0 || val2 > 1
		error('Theres something wrong with the values you entered');
	else
		disp('Generating Erdös-Renyi Network now .........');
		A = gen_er(val1,val2);
		assignin('base','A',A);
		disp('The adjaceny matrix of the graph is now stored in the variable A');
	end
	
elseif get(handles.radiobutton2,'Value') == get(handles.radiobutton2,'Max') % sw-situation
	if val1 < 1 || mod(val2,2) ~= 0 || val3 < 0 || val3 > 1
		error('Theres something wrong with the values you entered');
	else
		disp('Generating Small-World Network now.........');
		A = gen_sw(val1,val2,val3);
		assignin('base','A',A);
		disp('The adjaceny matrix of the graph is now stored in the variable A');
	end
	
elseif get(handles.radiobutton3,'Value') == get(handles.radiobutton3,'Max') % sf-situation
	if val1 < 1 || val2 < 1 || val3 < 1 || val3 > val2
		error('Theres something wrong with the values you entered');
	else
		disp('Generating Scale-Free Network now.........');
		A = gen_sf(val1,val2,val3);
		assignin('base','A',A);
		disp('The adjaceny matrix of the graph is now stored in the variable A');
	end
end