1 //------------------------------------------------------------------------------
2 // emFilePanel.cpp
3 //
4 // Copyright (C) 2004-2008,2016-2018 Oliver Hamann.
5 //
6 // Homepage: http://eaglemode.sourceforge.net/
7 //
8 // This program is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License version 3 as published by the
10 // Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License version 3
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //------------------------------------------------------------------------------
20 
21 #include <emCore/emFilePanel.h>
22 
23 
emFilePanel(ParentArg parent,const emString & name,emFileModel * fileModel,bool updateFileModel)24 emFilePanel::emFilePanel(
25 	ParentArg parent, const emString & name, emFileModel * fileModel,
26 	bool updateFileModel
27 )
28 	: emPanel(parent,name)
29 {
30 	CustomError=NULL;
31 	SetFileModel(fileModel,updateFileModel);
32 }
33 
34 
~emFilePanel()35 emFilePanel::~emFilePanel()
36 {
37 	if (CustomError) delete CustomError;
38 }
39 
40 
SetFileModel(emFileModel * fileModel,bool updateFileModel)41 void emFilePanel::SetFileModel(emFileModel * fileModel, bool updateFileModel)
42 {
43 	emFileModel * fm;
44 
45 	fm=FileModelClient.GetModel();
46 	if (fm != fileModel) {
47 		if (fm) RemoveWakeUpSignal(fm->GetFileStateSignal());
48 		FileModelClient.SetModel(fileModel);
49 		if (fileModel) AddWakeUpSignal(fileModel->GetFileStateSignal());
50 		Signal(VirFileStateSignal);
51 		InvalidatePainting();
52 	}
53 	if (fileModel && updateFileModel) fileModel->Update();
54 }
55 
56 
SetCustomError(const emString & message)57 void emFilePanel::SetCustomError(const emString & message)
58 {
59 	if (CustomError) delete CustomError;
60 	CustomError=new emString(message);
61 	Signal(VirFileStateSignal);
62 	InvalidatePainting();
63 }
64 
65 
ClearCustomError()66 void emFilePanel::ClearCustomError()
67 {
68 	if (CustomError) {
69 		delete CustomError;
70 		CustomError=NULL;
71 		Signal(VirFileStateSignal);
72 		InvalidatePainting();
73 	}
74 }
75 
76 
GetCustomError() const77 emString emFilePanel::GetCustomError() const
78 {
79 	if (CustomError) return *CustomError;
80 	else return emString();
81 }
82 
83 
GetVirFileState() const84 emFilePanel::VirtualFileState emFilePanel::GetVirFileState() const
85 {
86 	const emFileModel * fm;
87 
88 	if (CustomError) return VFS_CUSTOM_ERROR;
89 	fm=FileModelClient.GetModel();
90 	if (!fm) return VFS_NO_FILE_MODEL;
91 	if (fm->GetMemoryNeed()>FileModelClient.GetMemoryLimit()) return VFS_TOO_COSTLY;
92 	return (VirtualFileState)fm->GetFileState();
93 }
94 
95 
IsVFSGood() const96 bool emFilePanel::IsVFSGood() const
97 {
98 	VirtualFileState s;
99 
100 	s=GetVirFileState();
101 	return s==VFS_LOADED || s==VFS_UNSAVED;
102 }
103 
104 
GetIconFileName() const105 emString emFilePanel::GetIconFileName() const
106 {
107 	return "file.tga";
108 }
109 
110 
IsContentReady(bool * pReadying) const111 bool emFilePanel::IsContentReady(bool * pReadying) const
112 {
113 	switch (GetVirFileState()) {
114 	case VFS_WAITING:
115 	case VFS_LOADING:
116 		if (pReadying) *pReadying=true;
117 		return false;
118 	case VFS_LOADED:
119 	case VFS_UNSAVED:
120 	case VFS_SAVING:
121 		return emPanel::IsContentReady(pReadying);
122 	default:
123 		if (pReadying) *pReadying=false;
124 		return false;
125 	}
126 }
127 
128 
Cycle()129 bool emFilePanel::Cycle()
130 {
131 	emFileModel * fm;
132 
133 	fm=FileModelClient.GetModel();
134 	if (fm && IsSignaled(fm->GetFileStateSignal())) {
135 		InvalidatePainting();
136 		Signal(VirFileStateSignal);
137 	}
138 	return false;
139 }
140 
141 
Notice(NoticeFlags flags)142 void emFilePanel::Notice(NoticeFlags flags)
143 {
144 	emUInt64 m;
145 	VirtualFileState vfs;
146 
147 	if (flags&NF_UPDATE_PRIORITY_CHANGED) {
148 		FileModelClient.SetPriority(GetUpdatePriority());
149 	}
150 	if (flags&NF_MEMORY_LIMIT_CHANGED) {
151 		m=GetMemoryLimit();
152 		if (m!=FileModelClient.GetMemoryLimit()) {
153 			vfs=GetVirFileState();
154 			FileModelClient.SetMemoryLimit(m);
155 			if (vfs!=GetVirFileState()) {
156 				Signal(VirFileStateSignal);
157 				InvalidatePainting();
158 			}
159 		}
160 	}
161 }
162 
163 
IsOpaque() const164 bool emFilePanel::IsOpaque() const
165 {
166 	switch (GetVirFileState()) {
167 	case VFS_LOAD_ERROR:
168 	case VFS_SAVE_ERROR:
169 	case VFS_CUSTOM_ERROR:
170 		return true;
171 	default:
172 		return false;
173 	}
174 }
175 
176 
Paint(const emPainter & painter,emColor canvasColor) const177 void emFilePanel::Paint(const emPainter & painter, emColor canvasColor) const
178 {
179 	char tmp[256];
180 	emColor c;
181 
182 	switch (GetVirFileState()) {
183 	case VFS_WAITING:
184 		painter.PaintTextBoxed(
185 			0,
186 			0,
187 			1,
188 			GetHeight(),
189 			"Wait...",
190 			GetHeight()/6,
191 			emColor(92,92,0,192),
192 			canvasColor,
193 			EM_ALIGN_CENTER,
194 			EM_ALIGN_LEFT,
195 			1.0
196 		);
197 		break;
198 	case VFS_LOADING:
199 		sprintf(tmp,"Loading: %.1f%%",GetFileModel()->GetFileProgress());
200 		painter.PaintTextBoxed(
201 			0,
202 			0,
203 			1,
204 			GetHeight(),
205 			tmp,
206 			GetHeight()/6,
207 			emColor(0,112,0,192),
208 			canvasColor,
209 			EM_ALIGN_CENTER,
210 			EM_ALIGN_LEFT,
211 			1.0
212 		);
213 		break;
214 	case VFS_LOADED:
215 		painter.PaintTextBoxed(
216 			0,
217 			0,
218 			1,
219 			GetHeight(),
220 			"Loaded",
221 			GetHeight()/6,
222 			emColor(0,116,112,192),
223 			canvasColor,
224 			EM_ALIGN_CENTER,
225 			EM_ALIGN_LEFT,
226 			1.0
227 		);
228 		break;
229 	case VFS_UNSAVED:
230 		painter.PaintTextBoxed(
231 			0,
232 			0,
233 			1,
234 			GetHeight(),
235 			"Unsaved",
236 			GetHeight()/6,
237 			emColor(144,0,144,192),
238 			canvasColor,
239 			EM_ALIGN_CENTER,
240 			EM_ALIGN_LEFT,
241 			1.0
242 		);
243 		break;
244 	case VFS_SAVING:
245 		sprintf(tmp,"Saving: %.1f%%",GetFileModel()->GetFileProgress());
246 		painter.PaintTextBoxed(
247 			0,
248 			0,
249 			1,
250 			GetHeight(),
251 			tmp,
252 			GetHeight()/6,
253 			emColor(0,112,0,192),
254 			canvasColor,
255 			EM_ALIGN_CENTER,
256 			EM_ALIGN_LEFT,
257 			1.0
258 		);
259 		break;
260 	case VFS_TOO_COSTLY:
261 		painter.PaintTextBoxed(
262 			0,
263 			0,
264 			1,
265 			GetHeight(),
266 			"Costly",
267 			GetHeight()/6,
268 			emColor(112,64,64,192),
269 			canvasColor,
270 			EM_ALIGN_CENTER,
271 			EM_ALIGN_LEFT,
272 			1.0
273 		);
274 		break;
275 	case VFS_LOAD_ERROR:
276 		c.Set(128,0,0);
277 		painter.Clear(c,canvasColor);
278 		painter.PaintTextBoxed(
279 			0.05,
280 			GetHeight()*0.15,
281 			0.9,
282 			GetHeight()*0.1,
283 			"Loading Failed",
284 			GetHeight()*0.1,
285 			emColor(204,136,0),
286 			c,
287 			EM_ALIGN_CENTER,
288 			EM_ALIGN_LEFT,
289 			1.0
290 		);
291 		painter.PaintTextBoxed(
292 			0.05,
293 			GetHeight()*0.3,
294 			0.9,
295 			GetHeight()*0.4,
296 			GetFileModel()->GetErrorText(),
297 			GetHeight()*0.4,
298 			emColor(255,255,0),
299 			c,
300 			EM_ALIGN_CENTER,
301 			EM_ALIGN_LEFT,
302 			1.0
303 		);
304 		break;
305 	case VFS_SAVE_ERROR:
306 		c.Set(128,0,0);
307 		painter.Clear(c,canvasColor);
308 		painter.PaintTextBoxed(
309 			0.05,
310 			GetHeight()*0.15,
311 			0.9,
312 			GetHeight()*0.3,
313 			"Saving Failed",
314 			GetHeight()*0.3,
315 			emColor(255,0,0),
316 			c,
317 			EM_ALIGN_CENTER,
318 			EM_ALIGN_LEFT,
319 			1.0
320 		);
321 		painter.PaintTextBoxed(
322 			0.05,
323 			GetHeight()*0.5,
324 			0.9,
325 			GetHeight()*0.3,
326 			GetFileModel()->GetErrorText(),
327 			GetHeight()*0.3,
328 			emColor(255,255,0),
329 			c,
330 			EM_ALIGN_CENTER,
331 			EM_ALIGN_LEFT,
332 			1.0
333 		);
334 		break;
335 	case VFS_CUSTOM_ERROR:
336 		c.Set(128,0,0);
337 		painter.Clear(c,canvasColor);
338 		painter.PaintTextBoxed(
339 			0.05,
340 			GetHeight()*0.15,
341 			0.9,
342 			GetHeight()*0.2,
343 			"Error",
344 			GetHeight()*0.2,
345 			emColor(221,0,0),
346 			c,
347 			EM_ALIGN_CENTER,
348 			EM_ALIGN_LEFT,
349 			1.0
350 		);
351 		painter.PaintTextBoxed(
352 			0.05,
353 			GetHeight()*0.45,
354 			0.9,
355 			GetHeight()*0.3,
356 			*CustomError,
357 			GetHeight()*0.4,
358 			emColor(255,255,0),
359 			c,
360 			EM_ALIGN_CENTER,
361 			EM_ALIGN_LEFT,
362 			1.0
363 		);
364 		break;
365 	case VFS_NO_FILE_MODEL:
366 		painter.PaintTextBoxed(
367 			0,
368 			0,
369 			1,
370 			GetHeight(),
371 			"No file model",
372 			GetHeight()/6,
373 			emColor(128,0,0,192),
374 			canvasColor,
375 			EM_ALIGN_CENTER,
376 			EM_ALIGN_LEFT,
377 			1.0
378 		);
379 		break;
380 	}
381 }
382 
383 
IsHopeForSeeking() const384 bool emFilePanel::IsHopeForSeeking() const
385 {
386 	VirtualFileState s;
387 
388 	s=GetVirFileState();
389 	return s==VFS_WAITING || s==VFS_LOADING || s==VFS_SAVING;
390 }
391