source: aedes_headerbrowser.m @ 146

Last change on this file since 146 was 146, checked in by tjniskan, 9 years ago
  • Renamed aedes_viewprocpar.m to aedes_headerbrowser.m, since it's now

a more generic file header viewer. The favourite parameters are also
file format dependent. Aedes_headerbrowser should work in Matlab R2007b and
newer. Keyboard shortcuts don't work in Matlab R2007b if the uitable
has focus. Might work with older Matlab versions too, but I haven't
tried.

  • Added a shortcut to aedes_headerbrowser.m in Aedes menubar

View->File Header. Keyboard shortcut CTRL-H can also be used to launch
the aedes_headerbrowser.m.

  • Fixed a small bug in aedes_data_read.m. The file header information

from DICOM files was placed in a wrong field in Aedes DATA-structure.

D aedes_viewprocpar.m
M aedes_data_read.m
M aedes.m
A + aedes_headerbrowser.m
M aedes_revision.m

File size: 31.6 KB
RevLine 
[146]1function aedes_headerbrowser(inStruct)
2% AEDES_HEADERBROWSER - Browse and search file header information or any
3%                       Matlab structure
[2]4%   
5%
[146]6% Synopsis:
7%       aedes_headerbrowser(DATA) % Open the file format specific header
[2]8%
[146]9%       aedes_headerbrowser(STRUCT) % Browse a generic Matlab structure
10%
[2]11% Description:
[146]12%       
[2]13%
14% Examples:
15%
16% See also:
[146]17%       AEDES, AEDES_READPROCPAR, AEDES_READ_NIFTI
[2]18
[39]19% This function is a part of Aedes - A graphical tool for analyzing
[36]20% medical images
[2]21%
[36]22% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
23%
[45]24% Department of Physics, Department of Neurobiology
[39]25% University of Kuopio, FINLAND
[36]26%
27% This program may be used under the terms of the GNU General Public
28% License version 2.0 as published by the Free Software Foundation
29% and appearing in the file LICENSE.TXT included in the packaging of
30% this program.
31%
32% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
33% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
[2]34
[144]35% Public variables
36H = [];    % Handles structure
37Dat = [];  % Internal data structure
[2]38
[144]39% Do some error checking
40if nargin ~= 1
41  error('Invalid number of input arguments!')
42end
[2]43
[146]44% Check that the input argument is structure
45if ~isstruct(inStruct)
46  error('Input argument must be structure!')
47end
48
49% Get the saved Favourite list
50try
51  Dat.FavList = getpref('Aedes','HeaderBrowserFavList');
52catch
53  Dat.FavList.nifti = {};
54  Dat.FavList.vnmr = {};
55  Dat.FavList.dcm = {};
56  Dat.FavList.genheader = {};
57  Dat.FavList.generic = {};
58end
59
[144]60% Check if input is a Aedes data structure
[146]61if isfield(inStruct,'HDR') && isfield(inStruct,'FTDATA')
62  if isfield(inStruct,'DataFormat')
63    if any(strcmpi(inStruct.DataFormat,{'NIfTI(1)','NIfTI(2)','Analyze75'}))
64      % NIfTI and Analyze75 files
65      Dat.HeaderFileName = [inStruct.HDR.fpath,inStruct.HDR.fname];
66      Dat.FileFormatName = 'NIfTI/Analyze';
67      try
68        Dat.FavouriteList = Dat.FavList.nifti;
69      catch
70        Dat.FavList.nifti = {}
71        Dat.FavouriteList = Dat.FavList.nifti;
72      end
73      inStruct=inStruct.HDR.FileHeader;
74     
75    elseif strcmpi(inStruct.DataFormat,'VNMR')
76      % Varian VNMR files
77      Dat.HeaderFileName = [inStruct.HDR.fpath,'procpar'];
78      Dat.FileFormatName = 'VNMR Procpar';
79      try
80        Dat.FavouriteList = Dat.FavList.vnmr;
81      catch
82        Dat.FavList.vnmr = {}
83        Dat.FavouriteList = Dat.FavList.vnmr;
84      end
85      inStruct=inStruct.PROCPAR;
86     
87    elseif strcmpi(inStruct.DataFormat,'DCM')
88      % DICOM files
89      Dat.HeaderFileName = [inStruct.HDR.fpath,inStruct.HDR.fname];
90      Dat.FileFormatName = 'DICOM';
91      try
92        Dat.FavouriteList = Dat.FavList.dcm;
93      catch
94        Dat.FavList.dcm = {}
95        Dat.FavouriteList = Dat.FavList.dcm;
96      end
97      inStruct=inStruct.HDR.FileHeader;
98     
99    else
100      % Generic header
101      Dat.HeaderFileName = [inStruct.HDR.fpath,inStruct.HDR.fname];
102      Dat.FileFormatName = 'generic header';
103      try
104        Dat.FavouriteList = Dat.FavList.genheader;
105      catch
106        Dat.FavList.genheader = {}
107        Dat.FavouriteList = Dat.FavList.genheader;
108      end
109      inStruct=inStruct.HDR.FileHeader;
110     
111    end
112  else
113    % Generic header
114    inStruct=inStruct.HDR.FileHeader;
115    Dat.FileFormatName = 'generic header';
116    try
117      Dat.FavouriteList = Dat.FavList.genheader;
118    catch
119      Dat.FavList.genheader = {}
120      Dat.FavouriteList = Dat.FavList.genheader;
121    end
122    Dat.HeaderFileName = [inStruct.HDR.fpath,inStruct.HDR.fname];
123  end
[144]124else
[146]125  % Generic Matlab structure
126  Dat.HeaderFileName = '';
127  Dat.FileFormatName = 'generic structure';
128  try
129    Dat.FavouriteList = Dat.FavList.generic;
130  catch
131    Dat.FavList.generic = {}
132    Dat.FavouriteList = Dat.FavList.generic;
133  end
[144]134end
[2]135
[146]136% Initialize the cell array for uitable
137Dat.FullTable = {};
138Dat.StructMaxDepth = 10; % Limit recursion to something reasonable...
139Dat.FieldSeparator = '\';
140Dat.TableFavSeparator = '<html><b>=============</b></html>';
141Dat.SelectedCells = [];
[2]142
[144]143% Draw main GUI
[146]144[H,Dat]=l_DrawGui(Dat);
145l_ParseHeaderStructure(inStruct,'',0);
146Dat.DataTable = Dat.FullTable;
147l_SearchParams([],[]);
[2]148
[144]149% Update Data Table
150l_UpdateDataTable([],[]);
151
152% Set initial focus to search editbox
153uicontrol(H.SearchEdit);
154
[146]155% Get java handle to the table for Matlab R2007a and older
156if Dat.MatlabVersion<=7.05
157  drawnow, pause(0.01) % Force refresh
158 
159  % Set Table to non-editable
160  set(H.DataTable,'Editable',false)
161 
162  % Get java handle
163  H.DataTableJavaHandle = handle(H.DataTable.getTable,'callbackproperties');
164 
165  % Set KeyPressedCallback
166  set(H.DataTableJavaHandle.KeyPressedCallback,@l_KeyPressFcn)
167end
168assignin('base','H',H) % debug
[144]169
[146]170%% ------------------------------------------
171  function [H,Dat]=l_DrawGui(Dat)
172
[144]173    % Load default font and colors
174    Dat.GD=aedes_gui_defaults;
[145]175   
176    % Get Matlab version
177    Dat.MatlabVersion = aedes_getmatlabversion;
[144]178
179    % Position figure to the center of the screen
180    scrsz = get(0,'screensize');
181    fig_w = 600;
182    fig_h = 700;
183    fig_pos = [scrsz(3)/2-fig_w/2 scrsz(4)/2-fig_h/2 fig_w fig_h];
184   
185   
186    % Main Figure ----------------------------
[146]187    H.Fig = figure('Units','Pixel', ...
[144]188      'position',fig_pos,...
[146]189      'Name',['File Header Browser - ',Dat.FileFormatName], ...
[144]190      'Numbertitle','off', ...
[146]191      'Tag','header_browser_fig', ...
[144]192      'Color',Dat.GD.col.mainfig, ...
193      'Toolbar','none', ...
194      'Menubar','none', ...
195      'DoubleBuffer','on', ...
196      'DockControls','off',...
197      'renderer','painters',...
198      'resize','off',...
199      'Handlevisibility','off',...
[146]200      'KeyPressFcn',@l_KeyPressFcn,...
[144]201      'CloseReq',@l_CloseFig);
202   
203    % File name
204    fname_txh = 0;
[146]205    if ~isempty(Dat.HeaderFileName)
[144]206      fname_txh = 70;
[146]207      H.FileNameUipanel = uipanel('parent',H.Fig,...
[144]208        'units','pixel',...
209        'position',[10 fig_h-fname_txh fig_w-20 fname_txh-10],...
210        'title','File Name',...
211        'backgroundcolor',Dat.GD.col.mainfig);
212     
213      tmp_pos=get(H.FileNameUipanel,'position');
214      H.FileNameText = uicontrol('parent',H.FileNameUipanel,...
215        'style','text',...
[146]216        'string',Dat.HeaderFileName,...
[144]217        'units','pixel',...
218        'position',[10 5 tmp_pos(3)-20 fname_txh-35],...
219        'horizontalalign','left',...
220        'backgroundcolor',Dat.GD.col.mainfig);
221    end
222   
223    % Search uipanel ----------------------
224    up_height = 80;
[146]225    H.SearchUipanel = uipanel('parent',H.Fig,...
[144]226      'units','pixel',...
227      'position',[10 fig_h-up_height-10-fname_txh fig_w-20 up_height],...
228      'title','Search Options',...
229      'backgroundcolor',Dat.GD.col.mainfig);
230
231    % Search text
232    pos=get(H.SearchUipanel,'position');
[146]233    %H.SearchText = uicontrol('parent',H.SearchUipanel,...
234    %  'style','text',...
235    %  'string','Parameter:',...
236    %  'units','pixel',...
237    %  'position',[10 pos(4)-45 100 19],...
238    %  'horizontalalign','left',...
239    %  'backgroundcolor',Dat.GD.col.mainfig);
240    try
241      val=getpref('Aedes','HeaderBrowserSearchWhere');
242    catch
243      val=1;
244    end
245    H.SearchWherePopup = uicontrol('parent',H.SearchUipanel,...
246      'style','popup',...
247      'string',{'Parameter',...
248      'Value'},...
[144]249      'units','pixel',...
[146]250      'position',[10 pos(4)-45 100 22],...
251      'value',val,...
252      'tag','',...
253      'backgroundcolor',Dat.GD.col.popup,...
254      'callback',@l_SearchParams,...
255      'KeyPressFcn',@l_KeyPressFcn);
256   
[144]257    % Search type popupmenu
258    try
[146]259      val = getpref('Aedes','HeaderBrowserSearchHow');
[144]260    catch
261      val = 1;
262    end
[146]263    tmp_pos = get(H.SearchWherePopup,'position');
264    %tmp_pos = get(H.SearchText,'position');
[144]265    H.SearchHowPopup = uicontrol('parent',H.SearchUipanel,...
266      'style','popup',...
267      'string',{'contains...',...
268      'is exactly...','begins with...',...
[146]269      'ends with...',...
270      'matches regexp...'},...
[144]271      'units','pixel',...
272      'position',[tmp_pos(1)+tmp_pos(3)+5 tmp_pos(2) ...
273      150 22],...
274      'value',val,...
275      'tag','',...
276      'backgroundcolor',Dat.GD.col.popup,...
277      'callback',@l_SearchParams,...
[146]278      'KeyPressFcn',@l_KeyPressFcn);
[144]279
280    % Search editbox
281    tmp_pos = get(H.SearchHowPopup,'position');
282    H.SearchEdit = uicontrol('parent',H.SearchUipanel,...
283      'style','edit',...
284      'units','pixel',...
285      'position',[tmp_pos(1)+tmp_pos(3)+5 tmp_pos(2) pos(3)-tmp_pos(1)-tmp_pos(3)-15 tmp_pos(4)],...
286      'Callback',@l_SearchParams,...
287      'backgroundcolor',Dat.GD.col.edit,...
288      'KeyPressFcn',@l_SearchParams);
289   
290    % Case sensitive
291    try
[146]292      val = getpref('Aedes','HeaderBrowserCaseSensitivity');
[144]293    catch
294      val = 1;
295    end
296    tmp_pos = get(H.SearchEdit,'position');
297    H.SearchCaseSens = uicontrol('parent',H.SearchUipanel,...
298      'style','checkbox',...
299      'units','pixel',...
300      'string','Case sensitive',...
301      'position',[tmp_pos(1) tmp_pos(2)-25 tmp_pos(3) tmp_pos(4)],...
302      'Callback',@l_SearchParams,...
303      'value',val,...
304      'backgroundcolor',Dat.GD.col.mainfig,...
[146]305      'KeyPressFcn',@l_KeyPressFcn);
[144]306   
307    % --------------------------------------
308    % Favourite params list
309    favup_height = 150;
310    tmp_pos = get(H.SearchUipanel,'position');
[146]311    H.FavUipanel = uipanel('parent',H.Fig,...
[144]312      'units','pixel',...
313      'position',[10 fig_h-up_height-favup_height-fname_txh-15 fig_w-20 favup_height],...
[146]314      'title',['Favourite parameters for ',Dat.FileFormatName],...
[144]315      'backgroundcolor',Dat.GD.col.mainfig);
316   
317    tmp_pos = get(H.FavUipanel,'position');
318    InfoTx = {['The parameter names entered here will be searched ',...
319      'automatically. The matching parameters are placed on top of the parameter list ',...
320      'in boldface. This automatic search is case sensitive and only returns exact matches.']};
321    H.FavInfoText = uicontrol('parent',H.FavUipanel,...
322      'style','text',...
323      'units','pixel',...
324      'string','',...
325      'position',[10 10 250 120],...
326      'horizontalalign','left',...
327      'fontsize',10,...
328      'backgroundcolor',[0.85 0.85 0.85]);%Dat.GD.col.mainfig);
329    InfoTx=textwrap(H.FavInfoText,InfoTx);
330    set(H.FavInfoText,'string',InfoTx)
331   
332    tmp_pos = get(H.FavInfoText,'position');
333    H.FavAdd = uicontrol('parent',H.FavUipanel,...
334      'style','pushbutton',...
335      'units','pixels',...
336      'position',[tmp_pos(1)+tmp_pos(3)+5 ...
337      tmp_pos(2)+tmp_pos(4)-25 70 25],...
338      'String','Add',...
[146]339      'callback',{@l_AddFavourite,[]},...
340      'KeyPressFcn',@l_KeyPressFcn);
[144]341   
342    H.FavRem = uicontrol('parent',H.FavUipanel,...
343      'style','pushbutton',...
344      'units','pixels',...
345      'position',[tmp_pos(1)+tmp_pos(3)+5 ...
346      tmp_pos(2)+tmp_pos(4)-2*25-5 70 25],...
347      'String','Remove',...
348      'callback',@l_RemFavourite,...
[146]349      'KeyPressFcn',@l_KeyPressFcn);
[144]350   
351    tmp_pos = get(H.FavInfoText,'position');
352    H.FavListBox = uicontrol('parent',H.FavUipanel,...
353      'style','listbox',...
354      'units','pixels',...
355      'min',1,...
356      'max',3,...
357      'position',[tmp_pos(1)+tmp_pos(3)+80 ...
358      tmp_pos(2) fig_w-30-tmp_pos(3)-90 tmp_pos(4)],...
359      'String',Dat.FavouriteList,...
360      'value',[],...
361      'backgroundcolor','w',...
[146]362      'KeyPressFcn',@l_KeyPressFcn);
[144]363   
364    % Close & Export to workspace btns -------
365    tmp_pos = get(H.FavUipanel,'position');
[146]366    btn_w = (fig_w-20-2*5)/3;
367    %btn_w = 200;
[144]368    btn_h = 25;
[146]369    H.ExportBtn = uicontrol('parent',H.Fig,...
[144]370      'style','pushbutton',...
371      'units','pixels',...
372      'position',[tmp_pos(1) ...
373      tmp_pos(2)-btn_h-5 btn_w btn_h],...
374      'string','Export to workspace',...
375      'callback',@l_ExportToWorkspace,...
[146]376      'KeyPressFcn',@l_KeyPressFcn);
[144]377   
[146]378    %btn_w = 200;
[144]379    btn_h = 25;
[146]380    H.AddParamsToFavBtn = uicontrol('parent',H.Fig,...
[144]381      'style','pushbutton',...
382      'units','pixels',...
[146]383      'position',[tmp_pos(1)+btn_w+5 tmp_pos(2)-btn_h-5 btn_w btn_h],...
384      'string','Add to favourites',...
385      'enable','off',...
386      'callback',@l_AddToFavBtnCallback,...
387      'KeyPressFcn',@l_KeyPressFcn);
388    if Dat.MatlabVersion<=7.05
389      % With Matlab R2007b and older this has to be handled differently
390      set(H.AddParamsToFavBtn,'enable','on');
391    end
392   
393    %btn_w = 200;
394    btn_h = 25;
395    H.CloseBtn = uicontrol('parent',H.Fig,...
396      'style','pushbutton',...
397      'units','pixels',...
[144]398      'position',[tmp_pos(1)+tmp_pos(3)-btn_w ...
399      tmp_pos(2)-btn_h-5 btn_w btn_h],...
400      'string','Close',...
401      'callback',@l_CloseFig,...
[146]402      'KeyPressFcn',@l_KeyPressFcn);
[144]403   
404    % Data Table -----------------------------
405    pos=get(H.FavUipanel,'position');
[145]406    if Dat.MatlabVersion>=7.06
[146]407      H.DataTable = uitable('Parent',H.Fig,...
408        'Data',{'loading...','loading...'},...
[145]409        'units','pixel',...
410        'position',[10 10 fig_w-20 pos(2)-20-btn_h-5],...
[146]411        'KeyPressFcn',@l_KeyPressFcn,...
412        'CellSelectionCallback',@l_DataTableCellSelectionFcn,...
413        'ColumnFormat',{'char','char'},...
414        'ColumnWidth',{280,280},...
415        'RowName',[],...
416        'ColumnName',{'Parameter','Value(s)'});
[145]417    else
[146]418      H.DataTable = uitable('Parent',H.Fig,...
[145]419        'position',[10 10 fig_w-20 pos(2)-20-btn_h-5],...
420        'Data',{'loading...','loading...'},...
[146]421        'ColumnWidth',265,...
[145]422        'ColumnNames',{'Parameter','Value(s)'});
423    end
[146]424   
425    % If other header browser windows are currently open, the figure name
426    % has to be unique for finding the correct javaFrame.
427    figs=findall(0,'tag','header_browser_fig');
428    if length(figs)>1
429      set(H.Fig,'Name',['File Header Browser - ',Dat.FileFormatName,' (',num2str(H.Fig),')']);
430      drawnow,pause(0.02);
431    end
432    H.SearchEditJavaHandle=l_GetEditboxJavaHandle(H.Fig);
433    if isempty(H.SearchEditJavaHandle)
434      % Fall back to basic matlab code -> no search as you type... :-(
435      Dat.SearchEditUseJava = false;
436      set(H.SearchEdit,'KeyPressFcn','')
437    else
438      Dat.SearchEditUseJava = true;
439    end
440    set(H.Fig,'Name',['File Header Browser - ',Dat.FileFormatName]); % Set Figure name back to original...
441   
[144]442  end
443
[146]444  function l_ParseHeaderStructure(inStruct,parent,level)
445    % This subfunction goes throught the input structure recursively and
446    % constructs the n-by-2 cell array for the uitable.
447   
448    % Get fieldnames
449    fldnames = fieldnames(inStruct);
450    fldnames = sort(fldnames);
451   
452    % Loop over the field names
[144]453    for ii=1:length(fldnames)
[146]454      cVal = inStruct.(fldnames{ii});
455     
456      % Structures
457      if isstruct(cVal)
458        if level>=Dat.StructMaxDepth
459          % Don't go beyond the StructMaxDepth level
460          Dat.FullTable{end,2} = regexprep(regexprep(mat2str(size(cVal)),'\s','x'),'\]$',' struct]');
461        else
462          % Recursively go through the next level
463          if isempty(parent) % First level
464            l_ParseHeaderStructure(cVal,fldnames{ii},level+1);
465          else
466            l_ParseHeaderStructure(cVal,[parent,Dat.FieldSeparator,fldnames{ii}],level+1);
467          end
468        end
469      else
470        if isempty(parent)
471          Dat.FullTable{end+1,1} = fldnames{ii};
472        else
473          Dat.FullTable{end+1,1} = [parent,Dat.FieldSeparator,fldnames{ii}];
474        end
475       
476        % Numeric values
477        if isnumeric(cVal)
478          if ndims(cVal)<=2 || any(size(cVal)==1)
479            Dat.FullTable{end,2} = mat2str(cVal);
480          else
481            Dat.FullTable{end,2} = regexprep(regexprep(mat2str(size(cVal)),'\s','x'),'\]$',' matrix]');
482          end
483         
484          % Logical arrays
485        elseif islogical(cVal)
486          if ndims(cVal)<=2 || any(size(cVal)==1)
487            Dat.FullTable{end,2} = num2str(cVal);
488          else
489            Dat.FullTable{end,2} = regexprep(regexprep(mat2str(size(cVal)),'\s','x'),'\]$',' logical]');
490          end
491         
492          % Character strings
493        elseif ischar(cVal)
494          Dat.FullTable{end,2} = ['''',cVal,''''];
495         
496          % Cell arrays
497        elseif iscell(cVal)
498          str = '{';
499          for kk=1:length(cVal)
500            if kk==1
501              sep='';
502            else
503              sep=', ';
504            end
505            if ischar(cVal{kk})
506              str = [str,sep,'''',cVal{kk},''''];
507            elseif isnumeric(cVal{kk}) || islogical(cVal{kk})
508              if ndims(cVal{kk})>1
509                tmp_str = regexprep(regexprep(mat2str(size(cVal{kk})),'\s','x'),'\]$',' matrix]');
510                str = [str,sep,tmp_str];
511              else
512                str = [str,sep,mat2str(cVal{kk})];
[144]513              end
[146]514            elseif iscell(cVal{kk})
515              % Don't handle cell arrays inside cell arrays
516              tmp_str = regexprep(regexprep(mat2str(size(cVal{kk})),'\s','x'),'\]$',' cell array]');
517              str = [str,sep,tmp_str];
518            elseif isstruct(cVal{kk})
519              % Don't handle structures inside cell arrays
520              tmp_str = regexprep(regexprep(mat2str(size(cVal{kk})),'\s','x'),'\]$',' struct]');
521              str = [str,sep,tmp_str];
[144]522            else
[146]523              tmp_str = regexprep(regexprep(mat2str(size(cVal{kk})),'\s','x'),'\]$',[' ',class(cVal{kk}),']']);
524              str = [str,sep,tmp_str];
[144]525            end
526          end
[146]527          str(end+1) = '}';
528          Dat.FullTable{end,2} = str;
529         
530          % Unsupported class/object
[144]531        else
[146]532          tmp_str = regexprep(regexprep(mat2str(size(cVal)),'\s','x'),'\]$',[' ',class(cVal),']']);
533          Dat.FullTable{end,2} = tmp_str;
[144]534        end
[2]535      end
[144]536    end
[146]537   
[144]538    % Put favourites on top
[146]539    %Dat.DataTable = l_PutFavouritesOnTop(Dat.FullTable);
540   
[144]541  end
542
543%% ---------------------------------------------
544  function l_SearchParams(fh,evd)
545
546    if ~isempty(evd) && isfield(evd,'Key') && ...
547        strcmpi(evd.Key,'escape')
[146]548      l_KeyPressFcn(fh,evd);
[144]549      return
550    end
551   
552    % Get the search string
553    if Dat.SearchEditUseJava
[146]554      drawnow
555      SearchStr = get(H.SearchEditJavaHandle,'Text');
[2]556    else
[144]557      SearchStr = get(H.SearchEdit,'string');
558    end
559   
560    % Get case sensitivity
561    CaseSens = get(H.SearchCaseSens,'value');
562    if CaseSens==0
563      RegExpOpt = 'ignorecase';
564    else
565      RegExpOpt = 'matchcase';
566    end
567   
[146]568    % Search from parameters or values...?
569    col_ind=get(H.SearchWherePopup,'value');
570   
[144]571    if isempty(SearchStr)
572      % If the search string is empty, show full table
573      Dat.DataTable = l_PutFavouritesOnTop(Dat.FullTable);
574    else
[146]575      OrigSearchStr = SearchStr;
576     
577      % Escape the regexp special characters
578      SearchStr=regexptranslate('escape',SearchStr);
579     
580      if col_ind==1
581        SearchStr=strrep(SearchStr,' ','|');
582      end
[144]583      switch get(H.SearchHowPopup,'value')
584        case 1
585          % Search contains
586          RegExpStr = SearchStr;
587        case 2
588          % Search is exactly
[146]589          SearchStr=regexprep(SearchStr,'\|','\$\|\^');
[144]590          RegExpStr = ['^',SearchStr,'$'];
591        case 3
592          % Search begins with
[146]593          SearchStr=regexprep(SearchStr,'\|','\|\^');
[144]594          RegExpStr = ['^',SearchStr];
595        case 4
596          % Search ends with
[146]597          SearchStr=regexprep(SearchStr,'\|','\$\|');
[144]598          RegExpStr = [SearchStr,'$'];
[146]599        case 5
600          % Search matches regexp
601          RegExpStr = OrigSearchStr;
602          SearchStr = OrigSearchStr;
[2]603      end
[144]604     
605      % Regexp the full table parameters
[146]606      ind=find(~cellfun('isempty',regexp(Dat.FullTable(:,col_ind),RegExpStr,RegExpOpt)));
[144]607     
608      % Update DataTable
609      if isempty(ind)
[145]610        if Dat.MatlabVersion>=7.06
611          Dat.DataTable = {'<html><b>No matches!</b></html>',''};
612          %set(H.SearchEdit,'string',['<html><font color="red">',SearchStr,'</font></html>']);
613        else
614          Dat.DataTable = {'<html><b>No matches!</b></html>','<html><b>No matches!</b></html>';...
615            '=============','============='};
616        end
[144]617      else
618        Dat.DataTable = Dat.FullTable(ind,:);
[146]619        Dat.DataTable(:,col_ind)=regexprep(Dat.DataTable(:,col_ind),['(',SearchStr,')'],['<b>$1</b>'],RegExpOpt);
620        Dat.DataTable(:,col_ind)=regexprep(Dat.DataTable(:,col_ind),'(^.*$)','<html>$1</html>',RegExpOpt);
[144]621      end
[2]622    end
[144]623
624    % Update Data Table
625    l_UpdateDataTable([],[])
626   
[146]627   
628   
[144]629  end
630
631%% ---------------------------------------------
632  function l_UpdateDataTable(fh,evd)
633   
[145]634    if Dat.MatlabVersion>=7.06
[146]635      set(H.DataTable,'Data',Dat.DataTable)
[145]636    else
[146]637      if size(Dat.DataTable,1)==1
638        Dat.DataTable(end+1,:)={Dat.TableFavSeparator,Dat.TableFavSeparator};
639      end
640      set(H.DataTable,'Data',Dat.DataTable)
[145]641    end
642   
[144]643  end
644
645%% ---------------------------------------------
[146]646  function l_AddFavourite(h,evd,param)
[144]647   
[146]648    if isempty(param)
649      % Prompt for a favourite search string
650      resp = aedes_inputdlg('Search text to be added:');
651      if isempty(resp) || isempty(resp{1})
652        return
653      end
654      param = resp;
[144]655    end
656   
[146]657    params_added = 0;
658    for ii=1:length(param)
659     
660      % Check if parameter is separator
661      if strcmp(param{ii},Dat.TableFavSeparator)
662        continue
663      end
664     
665      % Check if the parameter is already in favourites
666      if any(strcmp(param{ii},Dat.FavouriteList))
667        % Parameter already in the favourite list
668        %if ~isempty(h) && h==H.FavAdd
669          % Put the favourite on top of the list, if already exists
670          ind = find(strcmp(param{ii},Dat.FavouriteList));
671          inds = [ind setdiff(1:length(Dat.FavouriteList),ind)];
672          Dat.FavouriteList = Dat.FavouriteList(inds);
673          params_added = params_added + 1;
674        %else
675          % Add search string to list
676          %Dat.FavouriteList{end+1} = param{ii};
677          %params_added = params_added + 1;
678        %  continue
679        %end
680      else
681        % Add search string to list
682        Dat.FavouriteList{end+1} = param{ii};
683        params_added = params_added + 1;
684      end
685    end
[144]686   
[146]687    if params_added>0
688      set(H.FavListBox,'string',Dat.FavouriteList,...
689        'value',[]);
[144]690   
[146]691      % Run search
692      l_SearchParams([],[]);
693    end
694   
[144]695  end
696
697%% ---------------------------------------------
[146]698  function l_AddToFavBtnCallback(h,evd)
699   
700    % Matlab R2008a onwards...
701    if Dat.MatlabVersion>=7.06
702      if isempty(Dat.SelectedCells)
703        % This should not happen, but return immediately if it does...
704        return
705      end
706     
707      ind = find(Dat.SelectedCells(:,2)==1);
708      if isempty(ind)
709        return
710      end
711     
712      % Get index to favourites separator
713      ind=Dat.SelectedCells(ind,1);
714      sep_ind=find(strcmp(Dat.TableFavSeparator,Dat.DataTable(:,1)));
715     
716      % Remove separator from the list
717      if ~isempty(sep_ind)
718        ind=setdiff(ind,sep_ind);
719        if isempty(ind)
720          return
721        end
722      end
723      %if ~isempty(sep_ind)
724      %  ind(ind<=sep_ind)=[];
725      %  if isempty(ind)
726      %    return
727      %  end
728      %end
729    else
730      % Matlab R2007a and older
731      selCols = H.DataTableJavaHandle.SelectedColumns;
732      selRows = H.DataTableJavaHandle.SelectedRows;
733     
734      if any(selCols==0)
735        ind = selRows+1;
736      else
737        return
738      end
739     
740      % Get index to favourites separator
741      sep_ind=find(strcmp(Dat.TableFavSeparator,Dat.DataTable(:,1)));
742     
743      % Remove separator from the list
744      if ~isempty(sep_ind)
745        ind=setdiff(ind,sep_ind);
746        if isempty(ind)
747          return
748        end
749      end
750    end
751   
752    % Get the parameters to be added
753    params=Dat.DataTable(ind,1);
754   
755    % Remove html tags from the strings
756    params=regexprep(params,'<html>|</html>|<b>|</b>','');
757   
758    % Add to favourites
759    l_AddFavourite([],[],params);
760   
761  end
762
763%% ---------------------------------------------
[144]764  function l_RemFavourite(fh,evd)
765   
766    % Get selected indices
767    ind = get(H.FavListBox,'value');
768    if isempty(ind)
769      return
770    end
771   
772    % Ask for confirmation
773    resp = questdlg('Remove selected favourites?',...
774      'Remove favourites','Yes','No','No');
775    if strcmpi(resp,'No')
776      return
777    end
778    Dat.FavouriteList(ind)=[];
779    set(H.FavListBox,'string',Dat.FavouriteList,...
780      'value',[]);
781   
782    % Run search to update changes
783    l_SearchParams([],[]);
784   
785  end
786
787
788%% ---------------------------------------------
789  function TableOut = l_PutFavouritesOnTop(TableIn)
790   
[146]791    FavouriteList = Dat.FavouriteList;%get(H.FavListBox,'string');
[144]792   
793    % Put favourite list matches on top of the search list and bold face
794    ind = [];
795    for ii=1:length(FavouriteList)
[146]796      SearchStr = regexptranslate('escape',FavouriteList{ii});
797      tmp_ind=find(~cellfun('isempty',regexp(TableIn(:,1),['^',SearchStr,'$'])));
798      if ~isempty(tmp_ind)
799        ind = [ind tmp_ind];
800      end
[144]801      if isempty(tmp_ind)
802        % If favourite is not found, color it red in the listbox
803        ListboxStr=get(H.FavListBox,'string');
[146]804        ListboxStr = regexprep(ListboxStr,['(^',SearchStr,'$)'],...
[144]805          '<html><font color="red"><b>$1 (not found)</b></font></html>');
806        set(H.FavListBox,'string',ListboxStr)
807      end
808    end
809    if ~isempty(ind)
810      ind2 = setdiff(1:size(TableIn,1),ind);
[146]811      tmpFavList = TableIn(ind,:);
812      tmpFavList(:,1)=regexprep(tmpFavList(:,1),'(.*)',...
[144]813        '<html><b>$1</b></html>');
814      TableOut = cat(1,...
[146]815        tmpFavList,...
816        {Dat.TableFavSeparator,Dat.TableFavSeparator},...
[144]817        TableIn(ind2,:));
[2]818    else
[144]819      TableOut = TableIn;
[2]820    end
821  end
822
[144]823%% --------------------------------------
824  function l_ExportToWorkspace(fh,evd)
825   
[146]826    varname = 'header_struct';
827    assignin('base',varname,inStruct);
828    msgstr = sprintf('Structure exported to variable "%s".',varname);
[144]829   
830    % Print notification to workspace
[146]831    fprintf(1,['AEDES_HEADERBROWSER: ',msgstr,'\n']);
[144]832   
833    % Nag with a dialog...
[146]834    helpdlg(msgstr,'Structure exported!')
[144]835   
836  end
[2]837
[144]838%% --------------------------------------------
[146]839  function l_KeyPressFcn(fh,evd)
[144]840   
841    % Call l_CloseFig if ESC is pressed
842    if ~isempty(evd) && isfield(evd,'Key') && ...
843        strcmpi(evd.Key,'escape')
844      l_CloseFig([],[])
[146]845     
846    elseif ~isempty(evd) && ~isempty(evd.Modifier)
847      if ( strcmpi(evd.Modifier{1},'control') && strcmpi(evd.Key,'l') ) || ...
848          ( strcmpi(evd.Modifier{1},'alt') && strcmpi(evd.Key,'d') )
849        % Change focus to the search editbox
850        % using CTRL-L or ALT-D
851        uicontrol(H.SearchEdit);
852      end
[144]853    end
854   
855  end
[2]856
[146]857%% ---------------------------------------------
858  function l_DataTableCellSelectionFcn(h,evd)
859
860    if isfield(evd,'Indices')
861      Dat.SelectedCells = evd.Indices;
862      if isempty(Dat.SelectedCells)
863        set(H.AddParamsToFavBtn,'enable','off');
864      else
865        set(H.AddParamsToFavBtn,'enable','on');
866      end
867    else
868      Dat.SelectedCells = [];
869      set(H.AddParamsToFavBtn,'enable','off');
870    end
871   
872  end
[144]873%% --------------------------------------------
874  function l_CloseFig(fh,evd)
875   
876    % Try to save preferences to disk before exiting
877    try
878      CaseSens = get(H.SearchCaseSens,'value');
[146]879      setpref('Aedes','HeaderBrowserCaseSensitivity',CaseSens);
[144]880     
[146]881      valhow = get(H.SearchHowPopup,'value');
882      setpref('Aedes','HeaderBrowserSearchHow',valhow);
[144]883     
[146]884      valwhere = get(H.SearchWherePopup,'value');
885      setpref('Aedes','HeaderBrowserSearchWhere',valwhere);
886     
887      % Update the corresponding FavList
888      if strcmp(Dat.FileFormatName,'NIfTI/Analyze')
889        Dat.FavList.nifti = Dat.FavouriteList;
890      elseif strcmp(Dat.FileFormatName,'VNMR Procpar')
891        Dat.FavList.vnmr = Dat.FavouriteList;
892      elseif strcmp(Dat.FileFormatName,'DICOM')
893        Dat.FavList.dcm = Dat.FavouriteList;
894      elseif strcmp(Dat.FileFormatName,'generic header')
895        Dat.FavList.genheader = Dat.FavouriteList;
896      else strcmp(Dat.FileFormatName,'generic structure')
897        Dat.FavList.generic = Dat.FavouriteList;
898      end
899      setpref('Aedes','HeaderBrowserFavList',Dat.FavList);
[144]900    catch
[146]901      disp('Warning: Could not save AEDES_HEADERBROWSER preferences!');
[144]902    end
[146]903    delete(H.Fig)
[144]904   
905  end
906
907%% -----------------------------------------------
908  function jhEditBox=l_GetEditboxJavaHandle(hFig)
909    % Parts of this code are originally written by Yair Altman, whose
910    % wizardry in Java programming in Matlab has been of great help in many
911    % occasions. Please see http://undocumentedmatlab.com and the
912    % comp.soft-sys.matlab news group for further information. The
913    % following license notice is for findjobj.m (by Yair Altman, available
914    % in the Matlab File Exchange,
915    % http://www.mathworks.com/matlabcentral/fileexchange/14317) from which
916    % I have used code snippets in this subfunction.
917    %
918    %
919    % Copyright (c) 2009, Yair Altman All rights reserved.
920    %
921    % Redistribution and use in source and binary forms, with or without
922    % modification, are permitted provided that the following conditions
923    % are met:
924    %
925    %   * Redistributions of source code must retain the above copyright
926    %     notice, this list of conditions and the following disclaimer.
927    %   * Redistributions in binary form must reproduce the above copyright
928    %     notice, this list of conditions and the following disclaimer in
929    %     the documentation and/or other materials provided with the
930    %     distribution
931    %
932    % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
933    % "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
934    % LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
935    % A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
936    % OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
937    % SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
938    % LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
939    % DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
940    % THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
941    % (INCLUDING NEGLIGENCE OR OTHERWISE) RISING IN ANY WAY OUT OF THE USE
942    % OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
943   
944    % Ensure that objects have been drawn
945    drawnow;
946    pause(0.01);
947   
948    % Get figure's root pane
949     try
950       figName = get(hFig,'Name');
951       mde = com.mathworks.mde.desk.MLDesktop.getInstance;
952       jFigPanel = mde.getClient(figName);
953       jRootPane = jFigPanel;
954       jRootPane = jFigPanel.getRootPane;
955     catch
956       try
957         warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');  % R2008b compatibility
958         jFrame = get(hFig,'JavaFrame');
959         jFigPanel = get(jFrame,'FigurePanelContainer');
960         jRootPane = jFigPanel;
961         jRootPane = jFigPanel.getComponent(0).getRootPane;
962       catch
963         % Never mind
964       end
965     end
966     try
967      % If invalid RootPane - try another method...
968      warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');  % R2008b compatibility
969      jFrame = get(hFig,'JavaFrame');
970      jAxisComponent = get(jFrame,'AxisComponent');
971      jRootPane = jAxisComponent.getParent.getParent.getRootPane;
972     catch
973      % Never mind
974     end
975     %jRootPane=handle(jRootPane,'callbackproperties');
976     
977     % Find java handle to Search editbox recursively
978     Dat.javaHandleFound = false;
979     jhEditBox=l_FindJavaHandle(jRootPane);
980  end
981
982% Recursion function for finding java handle for search editbox
983  function jhout=l_FindJavaHandle(jhin)
984    jhout = handle([]);
985    jhin=handle(jhin,'callbackproperties');
986   
987    if ~isempty(regexp(char(jhin.toString),'EditTextPeer\$hgTextField'))
988      jhout = jhin;
989      Dat.javaHandleFound=true;
990      return;
991    else
992      try
993        compCount = jhin.getComponentCount;
994        for ii=0:compCount-1
995          childComp = jhin.getComponent(ii);
996          if ~Dat.javaHandleFound
997            jhout=l_FindJavaHandle(childComp);
998          else
999            break;
1000          end
1001        end
1002      catch
1003        % Nothing to do here...
1004      end
1005    end
1006   
1007  end
1008     
1009
1010end
1011
1012 
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008