Skip to content

Commit d2b536c

Browse files
committed
Finishing support for editable export and import.
1 parent e508f30 commit d2b536c

File tree

6 files changed

+106
-4
lines changed

6 files changed

+106
-4
lines changed

src/CanvasImage.vala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ public class CanvasImage {
588588
string[] options = { "compression" };
589589
string[] values = { "9" };
590590
var name = GLib.Path.build_filename( image_dir, "background.png" );
591+
stdout.printf( "Attempting to save canvas image as %s\n", name );
591592
try {
592593
_buf.savev( name, "png", options, values );
593594
node->set_prop( "filename", name );
@@ -610,7 +611,8 @@ public class CanvasImage {
610611

611612
var fname = node->get_prop( "filename" );
612613
if( fname != null ) {
613-
_canvas.open_image( fname );
614+
stdout.printf( "Opening image: %s\n", fname );
615+
_canvas.editor.open_image( fname );
614616
}
615617

616618
var a = node->get_prop( "angle" );

src/CanvasItem.vala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ public class CanvasItem {
461461
}
462462
}
463463

464+
bbox_changed();
465+
464466
}
465467

466468
/* Returns the canvas item type from the given XML node */

src/MainWindow.vala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,17 @@ public class MainWindow : Gtk.ApplicationWindow {
498498
image is successfully read and displayed.
499499
*/
500500
public void open_file( string filename ) {
501-
_editor.open_image( filename );
501+
stdout.printf( "HERE A\n" );
502+
if( filename.has_suffix( ".annotator" ) ) {
503+
stdout.printf( "HERE B\n" );
504+
var export = (_editor.canvas.image.exports.get_by_name( "annotator" ) as ExportEditable);
505+
if( export != null ) {
506+
stdout.printf( "HERE C\n" );
507+
export.import( filename );
508+
}
509+
} else {
510+
_editor.open_image( filename );
511+
}
502512
_zoom_btn.set_sensitive( true );
503513
_export_btn.set_sensitive( true );
504514
}

src/canvas_items/CanvasItemArrow.vala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ public class CanvasItemArrow : CanvasItem {
276276
if( vc != null ) {
277277
_valley_c = double.parse( vc );
278278
}
279-
bbox_changed();
280279
}
281280

282281
/* Draw the rectangle */

src/canvas_items/CanvasItemStar.vala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ public class CanvasItemStar : CanvasItem {
182182
if( i != null ) {
183183
_inner_radius = double.parse( i );
184184
}
185-
bbox_changed();
186185
}
187186

188187
/* Draw the rectangle */

src/exports/ExportEditable.vala

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,96 @@ public class ExportEditable : Export {
168168

169169
}
170170

171+
//-------------------------------------------------------------
172+
// Loads the XML file and updates the editor.
173+
private void load_xml( string fname ) {
174+
175+
Xml.Doc* doc = Xml.Parser.read_file( fname, null, Xml.ParserOption.HUGE );
176+
if( doc == null ) return;
177+
178+
for( Xml.Node* it=doc->get_root_element()->children; it!=null; it=it->next ) {
179+
if( (it->type == Xml.ElementType.ELEMENT_NODE) && (it->name == "canvas") ) {
180+
canvas.load( it );
181+
}
182+
}
183+
184+
delete doc;
185+
186+
}
187+
188+
//-------------------------------------------------------------
189+
// Imports the given filename.
190+
public void import( string filename ) {
191+
192+
string temp_dir;
193+
194+
// Make the temporary directory
195+
try {
196+
temp_dir = DirUtils.make_tmp( "annotator-XXXXXX" );
197+
} catch( Error e ) {
198+
critical( e.message );
199+
return;
200+
}
201+
202+
Archive.Read archive = new Archive.Read();
203+
archive.support_filter_gzip();
204+
archive.support_format_all();
205+
206+
Archive.ExtractFlags flags;
207+
flags = Archive.ExtractFlags.TIME;
208+
flags |= Archive.ExtractFlags.PERM;
209+
flags |= Archive.ExtractFlags.ACL;
210+
flags |= Archive.ExtractFlags.FFLAGS;
211+
212+
Archive.WriteDisk extractor = new Archive.WriteDisk();
213+
extractor.set_options( flags );
214+
extractor.set_standard_lookup();
215+
216+
/* Open the portable Minder file for reading */
217+
if( archive.open_filename( filename, 16384 ) != Archive.Result.OK ) {
218+
load_xml( filename );
219+
return;
220+
}
221+
222+
unowned Archive.Entry entry;
223+
224+
while( archive.next_header( out entry ) == Archive.Result.OK ) {
225+
226+
/*
227+
We will need to modify the entry pathname so the file is written to the
228+
proper location.
229+
*/
230+
if( entry.pathname() == "annotations.xml" ) {
231+
entry.set_pathname( GLib.Path.build_filename( temp_dir, entry.pathname() ) );
232+
} else {
233+
entry.set_pathname( GLib.Path.build_filename( temp_dir, "images", entry.pathname() ) );
234+
}
235+
236+
/* Read from the archive and write the files to disk */
237+
if( extractor.write_header( entry ) != Archive.Result.OK ) {
238+
continue;
239+
}
240+
uint8[] buffer;
241+
Archive.int64_t offset;
242+
243+
while( archive.read_data_block( out buffer, out offset ) == Archive.Result.OK ) {
244+
if( extractor.write_data_block( buffer, offset ) != Archive.Result.OK ) {
245+
break;
246+
}
247+
}
248+
249+
}
250+
251+
/* Close the archive */
252+
if( archive.close () != Archive.Result.OK) {
253+
error( "Error: %s (%d)", archive.error_string(), archive.errno() );
254+
}
255+
256+
/* Finally, load the XML file */
257+
load_xml( GLib.Path.build_filename( temp_dir, "annotations.xml" ) );
258+
259+
}
260+
171261
//-------------------------------------------------------------
172262
// Add the setting to enable/disable including the images included
173263
// in the annotation.

0 commit comments

Comments
 (0)