X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ffileio.cpp;h=1b11902e37b672f1f6a72bd6dc443894acce5efd;hb=742d2aa9bb46bce4f690474fa22f5980e175e55e;hp=1002dca87fecf22b79cbb2883253e9660fe6c34b;hpb=e78daf62eb771ee29a59035d16cf63c1e6ebe144;p=architektonas diff --git a/src/fileio.cpp b/src/fileio.cpp index 1002dca..1b11902 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -19,6 +19,9 @@ #include #include #include +#include "applicationwindow.h" +#include "drawingview.h" +#include "global.h" #include "structs.h" /* @@ -58,7 +61,7 @@ Connect() function. Or, instead of a point, a parameter value? Doing that, with the Line and a parameter "t", if t == 0 we have endpoint 1. if t == 1, then we have endpoint 2. With a Circle, the parameter is a number -between 0 and 1 (scaled to 0 to 2π). With an Arc, the parameter goes from 0 to +between 0 and 1 (scaled to 0 to tau). With an Arc, the parameter goes from 0 to 1, 0 being enpoint 1 and 1 being endpoint 2. How does this work for moving objects that are connected? Again, with the Line @@ -112,11 +115,9 @@ OTHER CONSIDERATIONS: // OTFDimension, OTFPolygon, OTFText, OTFImage, OTFBlock, OTFEndOfFile }; enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; - // Instantiate class variables /*static*/ int FileIO::objectFileType = OTFObject; - /*static*/ bool FileIO::SaveAtnsFile(FILE * file, Container * c) { /* Approach: loop through the container, doing a depth-first traversal. Any @@ -125,98 +126,110 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; virtual Object function that reports the object by itself if it's a non- Container, otherwise it enumerates all objects within itself. */ - fprintf(file, "ARCHITEKTONAS DRAWING V1.1\n"); -#if 0 - object->Enumerate(file); -#else + fprintf(file, "ARCHITEKTONAS DRAWING V1.2\n"); + fprintf(file, "LAYERS %i\n", Global::numLayers); + + for(int i=0; i containerStack; - Container * currentTopContainer = drawing;//new Container(Vector(0, 0)); - Object * object; -// ObjectType objectType; - int objectType; + Container * currentTopContainer = drawing; + ResetLayerVectors(); while (!feof(file)) { - if (FileIO::GetObjectFromFile(file, currentTopContainer, &object, &objectType) == false) - return false; - - // object->type down below can be replaced with objType. - // Above could be: bool FileIO::GetObjectFromFile(FILE *, Object *, - // ObjectType *); where the return value tells if it's a valid object, - // Object * returns the reconstructed object and ObjectType * returns - // the object type. + // Reconstruct the object + Object * obj = GetObjectFromFile(file); - if (objectType == OTFEndOfFile) - { -//printf("Load: container size = %li\n", drawing->objects.size()); - return true; - } - else if (objectType == OTFContainer) + // objectFileType is set in GetObjectFromFile()... + if (objectFileType == OTFObject) { - containerStack.push_back(currentTopContainer); - currentTopContainer = new Container(Vector(0, 0), currentTopContainer); + if (obj == NULL) + return false; + + currentTopContainer->objects.push_back(obj); +//printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size()); + + // If the object is a container, push current TLC on the stack and + // set it as the new TLC + if (obj->type == OTContainer) + { + containerStack.push_back(currentTopContainer); + currentTopContainer = (Container *)obj; + } } - else if (objectType == OTFContainerEnd) + else if (objectFileType == OTFContainerEnd) { - Container * containerToAdd = currentTopContainer; + // Container is done, so pop the stack to get back the previous TLC currentTopContainer = containerStack.back(); containerStack.pop_back(); - currentTopContainer->Add(containerToAdd); } - else + else if (objectFileType == OTFEndOfFile) { - currentTopContainer->Add(object); -//printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size()); +//printf("Load: container size = %li\n", drawing->objects.size()); + return true; } } return false; -#else - return false; -#endif } - -/*static*/ bool FileIO::LoadVersion1_0(FILE * file, Container * drawing) +/*static*/ bool FileIO::LoadVersion1_1(FILE * file, Container * drawing) { // Approach: read each object in the file, one by one. If the object is a // Container, add objects to it until an "endContainer" marker is found. // This will require a stack to maintain the current Container. std::vector containerStack; Container * currentTopContainer = drawing; + ResetLayerVectors(); while (!feof(file)) { - // Reconstruct the object - Object * obj = GetObjectFromFile(file); + // Reconstruct the object (extended format!) + Object * obj = GetObjectFromFile(file, true); // objectFileType is set in GetObjectFromFile()... if (objectFileType == OTFObject) @@ -237,6 +250,12 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; } else if (objectFileType == OTFContainerEnd) { + // Add the extents of the current container + Rect r = ApplicationWindow::drawing->GetObjectExtents((Object *)currentTopContainer); + currentTopContainer->p[0] = r.TopLeft(); + currentTopContainer->p[1] = r.BottomRight(); +//printf("Container extents: <%lf, %lf>, <%lf, %lf>\n", r.l, r.t, r.r, r.b); + // Container is done, so pop the stack to get back the previous TLC currentTopContainer = containerStack.back(); containerStack.pop_back(); @@ -251,9 +270,24 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; return false; } - -/*static*/ bool FileIO::LoadVersion1_1(FILE * file, Container * drawing) +/*static*/ bool FileIO::LoadVersion1_2(FILE * file, Container * drawing) { + int hidden, locked; + char textBuffer[65536]; + + // Load layer information first + fscanf(file, "LAYERS %i\n", &Global::numLayers); + + for(int i=0; iGetObjectExtents((Object *)currentTopContainer); + currentTopContainer->p[0] = r.TopLeft(); + currentTopContainer->p[1] = r.BottomRight(); +//printf("Container extents: <%lf, %lf>, <%lf, %lf>\n", r.l, r.t, r.r, r.b); + // Container is done, so pop the stack to get back the previous TLC currentTopContainer = containerStack.back(); containerStack.pop_back(); @@ -298,10 +338,10 @@ enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile }; return false; } - -/*static*/ Object * FileIO::GetObjectFromFile(FILE * file, bool extended/*= false*/) +/*static*/ Object * FileIO::GetObjectFromFile(FILE * file, bool extended/*= false*/, bool ext2/*= false*/) { char buffer[256]; + char textBuffer[65536]; int foundLayer = 0; /*int num =*/ fscanf(file, "%s", buffer); Object * obj = NULL; @@ -336,6 +376,7 @@ if (errno) } // Need to add pen attributes as well... do that for v1.1 :-P + // And fill attributes... do that for v1.3 (1.2 added layers) if (strcmp(buffer, "LINE") == 0) { Point p1, p2; @@ -356,12 +397,23 @@ if (errno) fscanf(file, "(%lf,%lf) %lf, %lf, %lf", &p.x, &p.y, &r, &a1, &a2); obj = (Object *)new Arc(p, r, a1, a2); } + else if (strcmp(buffer, "TEXT") == 0) + { + Point p; + fscanf(file, "(%lf,%lf) \"%[^\"]\"", &p.x, &p.y, textBuffer); + obj = (Object *)new Text(p, textBuffer); + } else if (strcmp(buffer, "DIMENSION") == 0) { Point p1, p2; DimensionType type; + double offset = 0; fscanf(file, "(%lf,%lf) (%lf,%lf) %i", &p1.x, &p1.y, &p2.x, &p2.y, (int *)&type); - obj = (Object *)new Dimension(p1, p2, type); + + if (ext2) + fscanf(file, " %lf", &offset); + + obj = (Object *)new Dimension(p1, p2, type, offset); } else if (strcmp(buffer, "CONTAINER") == 0) { @@ -376,6 +428,8 @@ if (errno) { objectFileType = OTFEndOfFile; } + else + printf("Unknown object type '%s'...\n", buffer); if (obj != NULL) { @@ -383,14 +437,13 @@ if (errno) if (extended && (obj->type != OTContainer)) { - fscanf(file, " (%i, %f, %i)", &obj->color, &obj->thickness, &obj->style); + fscanf(file, " (%i, %f, %i)\n", &obj->color, &obj->thickness, &obj->style); } } return obj; } - /*static*/ bool FileIO::WriteObjectToFile(FILE * file, Object * obj) { // Sanity check @@ -414,7 +467,7 @@ if (errno) break; case OTDimension: // fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i\n", layer, position.x, position.y, endpoint.x, endpoint.y, dimensionType); - fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i", obj->layer, obj->p[0].x, obj->p[0].y, obj->p[1].x, obj->p[1].y, ((Dimension *)obj)->subtype); + fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i %lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->p[1].x, obj->p[1].y, ((Dimension *)obj)->subtype, ((Dimension *)obj)->offset); break; case OTSpline: break; @@ -447,4 +500,3 @@ if (errno) return true; } -