This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimPlugin.m
156 lines (132 loc) · 5.12 KB
/
imPlugin.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
classdef imPlugin < handle
% imPlugin class definition for imobject plugins
%__________________________________________________________________________
% SYNTAX:
% imPlugin(imObjectHandle);
%
% DESCRIPTION:
%__________________________________________________________________________
% DEFINE THE PUBLIC PROPERTIES OF THE CLASS
properties % Public properties
MenuParent = ''; % The name of the parent menu
MenuOptions = {}; % Property parings defining the uimenu item
MenuSubmenu = {}; % Cell array of uimenu items for submenus
MenuOrder = NaN; % The placement order within the partent
PushtoolCdata = []; % Image name or icons.ico or icons.mat
PushtoolToggle = false; % True = uitoggletool; false = uipushtool
PushtoolOrder = NaN; % The placement order of the button
PushtoolOptions = {}; % Property pairings for the button
% Defines the data structure for the user editable preferences
Pref = struct('Value',{},'Options',{},'Label',{},'Function',{});
plugin; % String containing the name of the plugin
plugintype; % String defininig the type of image
end
% SET THE PRIVATE PROPERTIES OF THE CLASS
properties (SetAccess = private, Transient = true)
parent; % Handle for the parent imObject
end
% DEFINE THE METHODS OF THE imPlugin CLASS
methods
% IMPLUGION: operates when the imPlugin object is created
function obj = imPlugin(imObject,PluginName)
obj.parent = imObject; % Gather the image handle
obj.plugin = PluginName; % The plugin name
obj = obj.getDefaultPref; % Load the defaults
end
% CREATEMENUITEM: contructs the menu item(s)
function createmenuitem(obj)
% Return if the parent menu is empty
if isempty(obj.MenuParent); return; end
% Locate or create the parent menu
h = obj.parent.imhandle;
m = findall(h,'Label',obj.MenuParent);
if isempty(m);
m = uimenu(h,'Label',obj.MenuParent);
end
% Create the menu items
mm = uimenu(m,obj.MenuOptions{:});
% Create the submenus
sub = obj.MenuSubmenu;
if ~isempty(sub) && iscell(sub);
for i = 1:length(sub);
uimenu(mm,sub{i}{:});
end
end
% Set the enable setting
if ~isempty(obj.plugintype) && sum(strcmpi(obj.parent.type,...
obj.plugintype)) == 0
set(mm,'enable','off');
end
end
% CREATEPUSHTOOL: constructs the toolbar buttons
function createpushtool(obj)
% Return if the pushtool settings are empty
if isempty(obj.PushtoolCdata); return; end
% Determine the type of button to produce
if obj.PushtoolToggle;
func = 'uitoggletool';
else
func = 'uipushtool';
end
% Locate or create the toolbar for button placement
h = obj.parent.imhandle; % Handle of the image
tbar = findobj(h,'Tag','TheToolBar');
if isempty(tbar);
tbar = uitoolbar(h,'Tag','TheToolBar');
end
% Create the button
mm = feval(func,tbar,'Cdata',obj.PushtoolCdata,...
obj.PushtoolOptions{:});
% Set the enable setting
if ~isempty(obj.plugintype) && sum(strcmpi(obj.parent.type,...
obj.plugintype)) == 0;
set(mm,'enable','off');
end
end
% PUSHTOOLCDATA: gathers the icon image data
function set.PushtoolCdata(obj,input)
% Get icon structure from the icon files
S = geticons;
% Gather the icon data based on the input type
if isnumeric(input) && all(size(input)==[16,16,3]); % Numeric
obj.PushtoolCdata = input;
elseif exist(input,'file'); % Image file
obj.PushtoolCdata = imread(input);
elseif isfield(S,input); % icon file structure name
obj.PushtoolCdata = S.(input);
else
warning('Input for icon image Cdata was not recognized');
obj.PushtoolCdata = rand(16,16,3);
end
end
% SETDEFAULTPREF: stores the plugin preferences via MATLAB pref
function obj = setDefaultPref(obj)
setpref('imPlugin',obj.plugin,obj.Pref);
end
% GETDEFAULTPREF: loads stored default preferences
function obj = getDefaultPref(obj)
if ispref('imPlugin',obj.plugin);
obj.Pref = getpref('imPlugin',obj.plugin);
end
end
% CLEARDEFAULTPREF: clears any stored preferences
function obj = clearDefaultPref(obj)
rmpref('imPlugin',obj.plugin);
end
end
end
%--------------------------------------------------------------------------
function S = geticons
% GETICIONS loads and compiles the *.ico files into a single structure
ico = dir(['icon',filesep,'*.ico']); % Gather the files
c = {}; fields = {}; % Intilize storage
% Cylce through the files
for i = 1:length(ico);
s = load(['icon',filesep,ico(i).name],'-mat');
f = fieldnames(s);
d = struct2cell(s);
c = [c;d];
fields = [fields;f];
end
S = cell2struct(c,fields);
end