295 if not filename: |
291 if not filename: |
296 filename = EricFileDialog.getOpenFileName( |
292 filename = EricFileDialog.getOpenFileName( |
297 self, |
293 self, |
298 self.tr("Load Diagram"), |
294 self.tr("Load Diagram"), |
299 "", |
295 "", |
300 self.tr( |
296 self.tr("Eric Graphics File (*.egj);;All Files (*)"), |
301 "Eric Graphics File (*.egj);;" |
|
302 "Eric Text Graphics File (*.e5g);;" |
|
303 "All Files (*)" |
|
304 ), |
|
305 ) |
297 ) |
306 if not filename: |
298 if not filename: |
307 # Canceled by user |
299 # Canceled by user |
308 return False |
300 return False |
309 |
301 |
310 return ( |
302 return self.__readJsonGraphicsFile(filename) |
311 self.__readLineBasedGraphicsFile(filename) |
|
312 if filename.endswith(".e5g") |
|
313 else |
|
314 # JSON format is the default |
|
315 self.__readJsonGraphicsFile(filename) |
|
316 ) |
|
317 |
|
318 ####################################################################### |
|
319 ## Methods to read and write eric graphics files of the old line |
|
320 ## based file format. |
|
321 ####################################################################### |
|
322 |
|
323 def __readLineBasedGraphicsFile(self, filename): |
|
324 """ |
|
325 Private method to read an eric graphics file using the old line |
|
326 based file format. |
|
327 |
|
328 @param filename name of the file to be read |
|
329 @type str |
|
330 @return flag indicating success |
|
331 @rtype bool |
|
332 """ |
|
333 try: |
|
334 with open(filename, "r", encoding="utf-8") as f: |
|
335 data = f.read() |
|
336 except OSError as err: |
|
337 EricMessageBox.critical( |
|
338 self, |
|
339 self.tr("Load Diagram"), |
|
340 self.tr( |
|
341 """<p>The file <b>{0}</b> could not be read.</p>""" |
|
342 """<p>Reason: {1}</p>""" |
|
343 ).format(filename, str(err)), |
|
344 ) |
|
345 return False |
|
346 |
|
347 lines = data.splitlines() |
|
348 if len(lines) < 3: |
|
349 self.__showInvalidDataMessage(filename) |
|
350 return False |
|
351 |
|
352 try: |
|
353 # step 1: check version |
|
354 linenum = 0 |
|
355 key, value = lines[linenum].split(": ", 1) |
|
356 if key.strip() != "version" or value.strip() not in UMLDialog.FileVersions: |
|
357 self.__showInvalidDataMessage(filename, linenum) |
|
358 return False |
|
359 else: |
|
360 version = value |
|
361 |
|
362 # step 2: extract diagram type |
|
363 linenum += 1 |
|
364 key, value = lines[linenum].split(": ", 1) |
|
365 if key.strip() != "diagram_type": |
|
366 self.__showInvalidDataMessage(filename, linenum) |
|
367 return False |
|
368 try: |
|
369 diagramType = value.strip().split(None, 1)[0] |
|
370 self.__diagramType = UMLDialogType(int(diagramType)) |
|
371 except ValueError: |
|
372 self.__showInvalidDataMessage(filename, linenum) |
|
373 return False |
|
374 self.scene.clear() |
|
375 self.builder = self.__diagramBuilder(self.__diagramType, "") |
|
376 |
|
377 # step 3: extract scene size |
|
378 linenum += 1 |
|
379 key, value = lines[linenum].split(": ", 1) |
|
380 if key.strip() != "scene_size": |
|
381 self.__showInvalidDataMessage(filename, linenum) |
|
382 return False |
|
383 try: |
|
384 width, height = [float(v.strip()) for v in value.split(";")] |
|
385 except ValueError: |
|
386 self.__showInvalidDataMessage(filename, linenum) |
|
387 return False |
|
388 self.umlView.setSceneSize(width, height) |
|
389 |
|
390 # step 4: extract builder data if available |
|
391 linenum += 1 |
|
392 key, value = lines[linenum].split(": ", 1) |
|
393 if key.strip() == "builder_data": |
|
394 ok = self.builder.parsePersistenceData(version, value) |
|
395 if not ok: |
|
396 self.__showInvalidDataMessage(filename, linenum) |
|
397 return False |
|
398 linenum += 1 |
|
399 |
|
400 # step 5: extract the graphics items |
|
401 ok, vlinenum = self.umlView.parsePersistenceData(version, lines[linenum:]) |
|
402 if not ok: |
|
403 self.__showInvalidDataMessage(filename, linenum + vlinenum) |
|
404 return False |
|
405 |
|
406 except IndexError: |
|
407 self.__showInvalidDataMessage(filename) |
|
408 return False |
|
409 |
|
410 # everything worked fine, so remember the file name and set the |
|
411 # window title |
|
412 self.setWindowTitle(self.__getDiagramTitel(self.__diagramType)) |
|
413 self.__fileName = filename |
|
414 |
|
415 return True |
|
416 |
|
417 def __showInvalidDataMessage(self, filename, linenum=-1): |
|
418 """ |
|
419 Private slot to show a message dialog indicating an invalid data file. |
|
420 |
|
421 @param filename name of the file containing the invalid data |
|
422 @type str |
|
423 @param linenum number of the invalid line |
|
424 @type int |
|
425 """ |
|
426 msg = ( |
|
427 self.tr( |
|
428 """<p>The file <b>{0}</b> does not contain valid data.</p>""" |
|
429 ).format(filename) |
|
430 if linenum < 0 |
|
431 else self.tr( |
|
432 """<p>The file <b>{0}</b> does not contain""" |
|
433 """ valid data.</p><p>Invalid line: {1}</p>""" |
|
434 ).format(filename, linenum + 1) |
|
435 ) |
|
436 EricMessageBox.critical(self, self.tr("Load Diagram"), msg) |
|
437 |
303 |
438 ####################################################################### |
304 ####################################################################### |
439 ## Methods to read and write eric graphics files of the JSON based |
305 ## Methods to read and write eric graphics files of the JSON based |
440 ## file format. |
306 ## file format. |
441 ####################################################################### |
307 ####################################################################### |
|
308 |
|
309 def __showInvalidDataMessage(self, filename): |
|
310 """ |
|
311 Private slot to show a message dialog indicating an invalid data file. |
|
312 |
|
313 @param filename name of the file containing the invalid data |
|
314 @type str |
|
315 """ |
|
316 EricMessageBox.critical( |
|
317 self, |
|
318 self.tr("Load Diagram"), |
|
319 self.tr( |
|
320 """<p>The file <b>{0}</b> does not contain valid data.</p>""" |
|
321 ).format(filename), |
|
322 ) |
442 |
323 |
443 def __writeJsonGraphicsFile(self, filename): |
324 def __writeJsonGraphicsFile(self, filename): |
444 """ |
325 """ |
445 Private method to write an eric graphics file using the JSON based |
326 Private method to write an eric graphics file using the JSON based |
446 file format. |
327 file format. |