-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowIO.cpp
More file actions
104 lines (83 loc) · 2.34 KB
/
FlowIO.cpp
File metadata and controls
104 lines (83 loc) · 2.34 KB
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
/**
* @file FlowIO.cpp
* @brief Implementation of file IO operations
* @author Ehcochwy
* @date 2025-05-10
*/
#include "FlowIO.h"
#include "DiagramCanvas.h"
#include "DiagramShape.h"
#include <QFile>
#include <QDataStream>
#include <QColor>
#include <QSize>
bool FlowIO::save(const QString& filename, DiagramCanvas* canvas)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
return false;
}
QDataStream stream(&file);
stream.setVersion(QDataStream::Qt_5_12);
// Write file header
stream << QString("FLOWCHART");
stream << 1; // version
// Write canvas properties
QColor backgroundColor = canvas->backgroundColor(); // assumes getter exists
QSize canvasSize = canvas->canvasSize(); // assumes getter exists
stream << backgroundColor;
stream << canvasSize;
// Write number of shapes
auto& shapes = canvas->allShapes();
stream << shapes.size();
// Write each shape
for (const auto& shape : shapes) {
shape->save(stream);
}
file.close();
return true;
}
bool FlowIO::load(const QString& filename, DiagramCanvas* canvas)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
QDataStream stream(&file);
stream.setVersion(QDataStream::Qt_5_12);
// Read file header
QString header;
stream >> header;
if (header != "FLOWCHART") {
file.close();
return false;
}
// Read version
int version;
stream >> version;
// Read canvas properties
QColor backgroundColor;
QSize canvasSize;
stream >> backgroundColor;
stream >> canvasSize;
// Set canvas background and size (assumes public setter methods)
canvas->setBackgroundColor(backgroundColor);
canvas->setCanvasSize(canvasSize);
// Clear current canvas
canvas->clear();
// Read number of shapes
int shapeCount;
stream >> shapeCount;
// Read each shape
for (int i = 0; i < shapeCount; ++i) {
int type;
stream >> type;
auto shape = DiagramShape::createShape((DiagramShape::Type)type);
if (shape) {
shape->load(stream);
canvas->addShape(shape);
}
}
file.close();
return true;
}