-
Notifications
You must be signed in to change notification settings - Fork 18
/
asImageTextClass.m
197 lines (166 loc) · 7.09 KB
/
asImageTextClass.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
% Copyright (C) 2009-2013 Biomedizinische NMR Forschungs GmbH
% http://www.biomednmr.mpg.de
% Author: Tilman Johannes Sumpf <tsumpf@gwdg.de>
%
% Distributed under the Boost Software License, Version 1.0.
% (See accompanying file LICENSE_1_0.txt or copy at
% http://www.boost.org/LICENSE_1_0.txt)
classdef asImageTextClass < handle
properties (Access = private)
parentAxesHandle = 0; % parent axes handle
parentFigureHandle = 0;
textHandle = [];
textContextMenuHandle = [];
textColor = 'white';
textBgColor = 'black';
autoWrapText = true;
cmenuHandle = struct();
ImageTextCell = {};
ImageTextCellSize = [];
end
properties (Access = protected)
string = [];
end
properties (Access = public)
end
methods
function obj = asImageTextClass(parentAxesHandle, str)
if nargin < 2
str = '';
if nargin < 1
parentAxesHandle = gca;
end
end
obj.parentAxesHandle = parentAxesHandle;
obj.parentFigureHandle = get(get(obj.parentAxesHandle,'Parent'),'Parent');
obj.createTextContextMenu();
if iscell(str)
obj.parseCellArray(str)
else
obj.string = str;
end
end
function setString(obj, str, dimSelector)
if nargin < 3
if ~isempty(obj.textHandle) && ishandle(obj.textHandle)
delete(obj.textHandle);
end
if isempty(str)
str='';
end
obj.string = str;
obj.textHandle = text(.01,.01,str,'Units','normalized','parent',obj.parentAxesHandle,...
'Color',obj.textColor,'BackgroundColor',obj.textBgColor,...
'VerticalAlignment','bottom',...
'interpreter','none',...
'UIContextMenu',obj.textContextMenuHandle);
if obj.autoWrapText
obj.wrapText;
end
else
if ~isempty(obj.ImageTextCell) % check if the cell array is not empty
if ~isempty(obj.textHandle) && ishandle(obj.textHandle)
delete(obj.textHandle);
end
if length(dimSelector) == ndims(obj.ImageTextCell) % check if the dimensions fit, if not set str='';
for j=1:1:length(dimSelector)
numdimSel(j)=str2num(dimSelector{j});
end
if numdimSel <= obj.ImageTextCellSize
% convert dimSelector from cell to string
dimSelStr=dimSelector{1};
for j=2:1:length(dimSelector)
dimSelStr=[dimSelStr ',', dimSelector{j}];
end
str=eval(['obj.ImageTextCell{', dimSelStr, '}']);
else
str='';
end
else
str='';
end
end
% check it str is a string and write it if true
if ischar(str)
obj.string=str;
obj.textHandle = text(.01,.01,str,'Units','normalized','parent',obj.parentAxesHandle,...
'Color',obj.textColor,'BackgroundColor',obj.textBgColor,...
'VerticalAlignment','bottom',...
'interpreter','none',...
'UIContextMenu',obj.textContextMenuHandle);
if obj.autoWrapText
obj.wrapText;
end
else
error(' ImageTextCell contains non string entries.');
end
end
end
function parseCellArray(obj, cellArray)
% this function parses a cell array and writes the first entry in
% the cell array to the image
if iscell(cellArray)
obj.ImageTextCell = cellArray;
obj.ImageTextCellSize= size(cellArray);
end
end
function wrapText(obj)
str = get(obj.textHandle,'String');
if iscell(str)
cellStr = {strcat(str{:})};
else
cellStr{1} = str;
end
% ugly workaround to use textwrap
parentPanel = get(obj.parentAxesHandle,'Parent');
textPanel = uicontrol('Style','Text','units','normalized',...
'parent',parentPanel,'Position',[.01,.01,.8,.9]);
outstring = textwrap(textPanel,cellStr);
delete(textPanel);
set(obj.textHandle,'String',outstring)
end
function str = getString(obj)
str = obj.string;
end
function Size = getImageTextCellSize(obj)
Size = obj.ImageTextCellSize;
end
function size = getImageTextCellSizeAsCell(obj)
size=cell(1, length(obj.ImageTextCellSize));
for j=1:1:length(obj.ImageTextCellSize)
size{j} = num2str(obj.ImageTextCellSize(j));
end
end
function edit(obj)
if ~iscell(obj.string)
prevCellStr{1} = obj.string;
else
prevCellStr{1} = strvcat(obj.string{:});
end
newStr = inputdlg('Enter image text','Change image text',3,prevCellStr,'on');
if ~isempty(newStr)
obj.setString(newStr);
end
end
function updateAxesHandle(obj, parentAxesHandle)
obj.parentAxesHandle = parentAxesHandle;
% obj.setString(obj.string);
end
function delete(obj)
if ~isempty(obj.textHandle) && ishandle(obj.textHandle)
delete(obj.textHandle);
end
end
end
methods (Access = private)
function createTextContextMenu(obj)
if isempty(obj.textContextMenuHandle)
obj.textContextMenuHandle = uicontextmenu('Parent',obj.parentFigureHandle);
uimenu(obj.textContextMenuHandle,'Label','Edit text' ,...
'callback',@(src,evnt)obj.edit());
uimenu(obj.textContextMenuHandle,'Label','Delete text' ,...
'callback',@(src,evnt)obj.delete);
end
end
end
end