Repository navigation
Expand file tree
/
Copy pathsvgeditor.cpp
More file actions
312 lines (251 loc) · 10.7 KB
/
Copy pathsvgeditor.cpp
File metadata and controls
312 lines (251 loc) · 10.7 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "svgeditor.h"
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QFileInfoList>
#include <QFileDialog>
#include <QListWidgetItem>
#include <QImage>
#include <QGraphicsScene>
#include <QPixmap>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTimer>
#include <QResizeEvent>
#include <algorithm>
#include "nanosvg.h"
#include "SvgDebugLog.h"
SVGEditor::SVGEditor(QWidget* parent)
: QMainWindow(parent), fileList(nullptr)
{
ui.setupUi(this);
SvgDebugSetOutput(ui.textEditLog);
// Создаём список файлов
fileList = new QListWidget(this);
fileList->setObjectName(QString::fromUtf8("fileList"));
fileList->setFocusPolicy(Qt::StrongFocus);
// Первоначально откроем текущую папку приложения
currentFolder = QDir::currentPath();
populateFileList(currentFolder);
if (fileList->count() > 0) {
fileList->setCurrentRow(0);
fileList->setFocus();
}
createRelations();
}
SVGEditor::~SVGEditor()
{
SvgDebugSetOutput(nullptr);
}
void SVGEditor::resizeEvent(QResizeEvent* event)
{
QMainWindow::resizeEvent(event);
emit windowResized();
}
bool SVGEditor::eventFilter(QObject* obj, QEvent* event)
{
// Если graphicsView изменил размер (например, при ресайзе окна), перерисовываем SVG
if (obj == ui.graphicsView && event->type() == QEvent::Resize) {
QTimer::singleShot(0, this, &SVGEditor::onDraw);
}
return QMainWindow::eventFilter(obj, event);
}
void SVGEditor::populateFileList(const QString& path)
{
fileList->clear();
QDir dir(path);
QStringList filters;
filters << "*.svg" << "*.xml"; // << "*.txt"; // по вкусу — добавьте нужные расширения
QFileInfoList list = dir.entryInfoList(filters, QDir::Files, QDir::Name);
for (const QFileInfo& fi : list) {
QListWidgetItem* it = new QListWidgetItem(fi.fileName(), fileList);
it->setData(Qt::UserRole, fi.filePath()); // хранить полный путь
fileList->addItem(it);
}
}
void SVGEditor::onGenerate()
{
ui.textEdit->setText("<?xml version=\"1.0\" encoding=\"UTF - 8\"?>\n"
"<svg width = \"1000\" height = \"800\" viewBox = \"0 0 1000 800\" xmlns = \"http://www.w3.org/2000/svg\" "
"xmlns:clover = \"https://github.com/CloverHackyColor/CloverBootloader\" >\n</svg>");
}
void SVGEditor::onDraw()
{
QByteArray svgData = ui.textEdit->toPlainText().toUtf8();
if (svgData.isEmpty()) return;
NSVGparser* parser = nsvg__parse(svgData.data(), 72, 1.f);
if (!parser || !parser->image) {
if (parser) nsvg__deleteParser(parser);
return;
}
NSVGimage* SVGimage = parser->image;
//DBG("SVG from Edit: width=%d height=%d\n", (int)(SVGimage->width), (int)(SVGimage->height));
{
QSize viewSize = ui.graphicsView->viewport()->size();
INTN Width = viewSize.width() - 5;
INTN Height = viewSize.height() - 5;
if (Width <= 0 || Height <= 0) {
nsvg__deleteParser(parser);
return;
}
NSVGrasterizer* rast = nsvg__createRasterizer();
float Scale, ScaleX, ScaleY;
// Rasterize
QImage image(Width, Height, QImage::Format_RGBA8888);
image.fill(Qt::transparent);
if (SVGimage->width <= 0) SVGimage->width = (float)Width;
if (SVGimage->height <= 0) SVGimage->height = (float)Height;
ScaleX = (Width - 10) / SVGimage->width;
ScaleY = (Height - 10) / SVGimage->height;
Scale = (ScaleX > ScaleY) ? ScaleY : ScaleX;
float tx = 2;
float ty = 2;
//DBG("timing rasterize start tx=%f ty=%f\n", tx, ty); //the aim is measure duration
nsvgRasterize(rast, SVGimage, tx, ty, Scale, Scale, image.bits(), (int)Width, (int)Height, (int)Width * 4);
//DBG("timing rasterize end\n");
nsvg__deleteRasterizer(rast);
if (!ui.graphicsView->scene()) {
ui.graphicsView->setScene(new QGraphicsScene(ui.graphicsView));
}
ui.graphicsView->scene()->clear();
// Явно задаем размер сцены, чтобы исключить авто-центрирование и смещения
ui.graphicsView->scene()->setSceneRect(0, 0, Width, Height);
ui.graphicsView->scene()->addPixmap(QPixmap::fromImage(image));
// Сбрасываем любую трансформацию, чтобы изображение не масштабировалось
ui.graphicsView->resetTransform();
}
nsvg__deleteParser(parser);
}
void SVGEditor::onFileSelected(int row)
{
if (row < 0 || !fileList) return;
QString fullPath = fileList->item(row)->data(Qt::UserRole).toString();
if (fullPath.isEmpty()) return;
QFile file(fullPath);
if (!file.open(QIODevice::ReadOnly)) return;
currentFilePath = fullPath;
// Читаем все байты и явно конвертируем из UTF-8
QByteArray bytes = file.readAll();
QString text = QString::fromUtf8(bytes);
ui.textEdit->setPlainText(text);
ui.textEditLog->clear();
if (ui.tabWidget->currentIndex() == ui.tabWidget->indexOf(ui.ViewTab)) {
onDraw();
}
}
void SVGEditor::onChooseFolder()
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Select Folder"), currentFolder,
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (dir.isEmpty()) return;
currentFolder = dir;
populateFileList(currentFolder);
if (fileList->count() > 0) {
fileList->setCurrentRow(0);
fileList->setFocus();
}
}
void SVGEditor::onSave()
{
QString initialPath = currentFilePath;
if (initialPath.isEmpty()) {
initialPath = QDir::homePath() + "/untitled.svg";
}
// Пример сохранения в UTF-8 (можно заменить на сохранение в исходный путь)
QString filePath = QFileDialog::getSaveFileName(
this,
tr("Save SVG"),
initialPath,
tr("SVG files (*.svg);;All files (*)"));
if (filePath.isEmpty()) return;
if (!filePath.endsWith(".svg", Qt::CaseInsensitive)) {
filePath += ".svg";
}
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) return;
QByteArray data = ui.textEdit->toPlainText().toUtf8(); // явная конвертация в UTF-8
if (file.write(data) != data.size() || !file.flush()) {
return;
}
currentFilePath = filePath;
}
void SVGEditor::createRelations()
{
// при смене выбранной строки — загружаем файл
connect(fileList, &QListWidget::currentRowChanged, this, &SVGEditor::onFileSelected);
// кнопка "Folder" открывает диалог выбора папки
connect(ui.pushButtonLoad, &QPushButton::clicked, this, &SVGEditor::onChooseFolder);
connect(ui.pushButtonSave, &QPushButton::clicked, this, &SVGEditor::onSave);
// Create new SVG — тоже в нижнем ряду
connect(ui.pushButtonNew, &QPushButton::clicked, this, &SVGEditor::onGenerate);
connect(ui.tabWidget, &QTabWidget::currentChanged, this, [this](int index) {
if (index == ui.tabWidget->indexOf(ui.ViewTab)) {
QTimer::singleShot(0, this, &SVGEditor::onDraw);
}
});
// КРИТИЧНО: Удаляем внутренний layout tabWidget, чтобы он не мешал setGeometry
if (ui.tabWidget->layout()) {
QLayoutItem* item;
while ((item = ui.tabWidget->layout()->takeAt(0)) != nullptr) {}
delete ui.tabWidget->layout();
}
// 2. Логика ручного пересчета геометрии при изменении размера окна
connect(this, &SVGEditor::windowResized, this, [this]() {
int newWidth = width();
int newHeight = height();
const int margin = 10;
const int logH = 70;
// --- Кнопка "Folder" (pushButtonLoad) ---
// Оставляем в левом верхнем углу, размер НЕ меняем (сохраняем "логотип")
int btnX = margin;
int btnY = ui.pushButtonLoad->y();
//int btnW = ui.pushButtonLoad->width();
int btnH = ui.pushButtonLoad->height();
// --- TabWidget ---
// Располагаем СТРОГО справа от кнопки, чтобы ничего не перекрывалось
// 1. TabWidget
ui.tabWidget->setGeometry(210, 10, newWidth - 220, newHeight - logH - 30);
QRect tabGeomNew = ui.tabWidget->geometry();
// --- graphicsView внутри tabWidget ---
// Учитываем высоту заголовка вкладок (примерно 25px) и небольшие отступы
// 5. GraphicsView внутри tabWidget
// ВАЖНО: Координаты задаются ОТНОСИТЕЛЬНО tabWidget, а не главного окна!
int tabBarHeight = ui.tabWidget->tabBar()->height();
if (tabBarHeight <= 0) tabBarHeight = 25; // Фоллбэк, если tabBar еще не инициализирован
int gvMargin = 5;
ui.graphicsView->setGeometry(gvMargin, gvMargin,
tabGeomNew.width() - 4 * gvMargin,
tabGeomNew.height() - tabBarHeight - 4 * gvMargin);
// --- Список файлов ---
// Под кнопкой "Folder", той же ширины, растягивается по высоте
int listX = btnX;
int listY = btnY + btnH + margin;
int listW = tabGeomNew.x() - margin * 2;
if (listW < 120) listW = 120; // Минимальная ширина
// Высота списка: всё свободное место до кнопок Save/New
//int btnAreaH = ui.pushButtonSave->height() + margin;
int listH = tabGeomNew.height() + margin - listY;
if (listH < 50) listH = 50;
fileList->setGeometry(listX, listY, listW, listH);
// --- Кнопки New и Save ---
// Прижимаются к низу (под списком файлов), сдвигаются вместе с ним
int btnNewY = listY + listH + margin;
int btnSaveY = btnNewY;
ui.pushButtonNew->setGeometry(listX, btnNewY, 75, ui.pushButtonNew->height());
ui.pushButtonSave->setGeometry(listX + 100, btnSaveY, 75, ui.pushButtonSave->height());
// --- Лог-панель ---
// Внизу, под tabWidget
int logX = tabGeomNew.x();
int logY = tabGeomNew.y() + tabGeomNew.height() + margin;
int logW = tabGeomNew.width();
ui.textEditLog->setGeometry(logX, logY, logW, logH);
// --- Перерисовка SVG ---
// Используем таймер, чтобы дать время на применение новой геометрии viewport
if (ui.tabWidget->currentIndex() == ui.tabWidget->indexOf(ui.ViewTab)) {
QTimer::singleShot(0, this, &SVGEditor::onDraw);
}
});
ui.graphicsView->setAlignment(Qt::AlignLeft | Qt::AlignTop);
// Первичный вызов ресайза для установки начальной геометрии при запуске
emit windowResized();
}