-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
424 lines (356 loc) · 16.8 KB
/
Copy pathmainwindow.cpp
File metadata and controls
424 lines (356 loc) · 16.8 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "mainwindow.h"
#include <QColorDialog>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QFileDialog>
#include <QFormLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QJsonDocument>
#include <QMessageBox>
#include <QSignalBlocker>
#include <QToolBar>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent),
m_registry(new ShapeRegistry(this)),
m_canvas(new Canvas(m_registry, this)) {
setWindowTitle(QStringLiteral("ЛР3: сериализация и плагины"));
resize(1320, 820);
buildToolbar();
QWidget* central = new QWidget(this);
QHBoxLayout* layout = new QHBoxLayout(central);
layout->setContentsMargins(8, 8, 8, 8);
layout->setSpacing(8);
layout->addWidget(m_canvas, 1);
layout->addWidget(buildSidebar());
setCentralWidget(central);
connectSignals();
refreshToolSelector();
refreshUi();
}
void MainWindow::chooseStrokeColor() {
const QColor color = QColorDialog::getColor(m_canvas->currentStyle().strokeColor, this, QStringLiteral("Цвет линии"));
if (color.isValid()) {
m_canvas->setStrokeColor(color);
}
}
void MainWindow::chooseFillColor() {
const QColor color = QColorDialog::getColor(m_canvas->currentStyle().fillColor, this, QStringLiteral("Цвет заливки"));
if (color.isValid()) {
m_canvas->setFillColor(color);
}
}
void MainWindow::loadDocument() {
const QString filePath = QFileDialog::getOpenFileName(this,
QStringLiteral("Открыть документ"),
QString(),
QStringLiteral("JSON (*.json)"));
if (filePath.isEmpty()) {
return;
}
QString errorMessage;
QStringList skippedTypes;
if (!m_canvas->loadFromFile(filePath, &skippedTypes, &errorMessage)) {
QMessageBox::warning(this, QStringLiteral("Ошибка загрузки"), errorMessage);
return;
}
if (!skippedTypes.isEmpty()) {
skippedTypes.removeDuplicates();
QMessageBox::information(this,
QStringLiteral("Документ загружен частично"),
QStringLiteral("Некоторые фигуры пропущены, потому что их типы не зарегистрированы:\n%1")
.arg(skippedTypes.join(QStringLiteral("\n"))));
}
}
void MainWindow::saveDocument() {
const QString filePath = QFileDialog::getSaveFileName(this,
QStringLiteral("Сохранить документ"),
QStringLiteral("drawing.json"),
QStringLiteral("JSON (*.json)"));
if (filePath.isEmpty()) {
return;
}
QString errorMessage;
if (!m_canvas->saveToFile(filePath, &errorMessage)) {
QMessageBox::warning(this, QStringLiteral("Ошибка сохранения"), errorMessage);
}
}
void MainWindow::loadPlugin() {
const QString filePath = QFileDialog::getOpenFileName(this,
QStringLiteral("Загрузить плагин"),
QString(),
QStringLiteral("Плагины (*.dylib *.so *.dll)"));
if (filePath.isEmpty()) {
return;
}
QString errorMessage;
if (!m_registry->loadPlugin(filePath, &errorMessage)) {
QMessageBox::warning(this, QStringLiteral("Ошибка загрузки плагина"), errorMessage);
return;
}
QMessageBox::information(this, QStringLiteral("Плагин загружен"), QStringLiteral("Новая фигура добавлена в список инструментов."));
}
void MainWindow::refreshUi() {
m_isRefreshingUi = true;
refreshLayersList();
refreshSelectionControls();
if (m_undoAction) {
m_undoAction->setEnabled(m_canvas->canUndo());
}
if (m_redoAction) {
m_redoAction->setEnabled(m_canvas->canRedo());
}
m_isRefreshingUi = false;
}
void MainWindow::onLayerSelectionChanged() {
if (m_isRefreshingUi || !m_layersList || !m_layersList->currentItem()) {
return;
}
m_canvas->setActiveLayerById(m_layersList->currentItem()->data(Qt::UserRole).toString());
}
void MainWindow::onToolSelectionChanged(int index) {
if (m_isRefreshingUi || index < 0) {
return;
}
const QString toolId = m_toolCombo->itemData(index).toString();
if (toolId.isEmpty()) {
m_canvas->setSelectMode();
} else {
m_canvas->setTool(toolId);
}
}
void MainWindow::refreshToolSelector() {
if (!m_toolCombo) {
return;
}
const QString currentTool = m_canvas->currentToolId();
QSignalBlocker blocker(m_toolCombo);
m_toolCombo->clear();
m_toolCombo->addItem(QStringLiteral("Выделение"), QString());
const QList<const IShapeDefinition*> definitions = m_registry->definitions();
for (const IShapeDefinition* definition : definitions) {
m_toolCombo->addItem(definition->displayName(), definition->typeId());
}
int index = m_toolCombo->findData(currentTool);
if (index < 0) {
index = 0;
}
m_toolCombo->setCurrentIndex(index);
}
QWidget* MainWindow::buildSidebar() {
QWidget* sidebar = new QWidget(this);
sidebar->setFixedWidth(320);
QVBoxLayout* layout = new QVBoxLayout(sidebar);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(8);
layout->addWidget(buildToolGroup());
layout->addWidget(buildStyleGroup());
layout->addWidget(buildTransformGroup());
layout->addWidget(buildMovementGroup());
layout->addWidget(buildLayersGroup());
layout->addWidget(buildActionsGroup());
layout->addStretch();
return sidebar;
}
QWidget* MainWindow::buildToolGroup() {
QGroupBox* group = new QGroupBox(QStringLiteral("Инструмент"), this);
QVBoxLayout* layout = new QVBoxLayout(group);
m_toolCombo = new QComboBox(group);
layout->addWidget(m_toolCombo);
return group;
}
QWidget* MainWindow::buildStyleGroup() {
QGroupBox* group = new QGroupBox(QStringLiteral("Стиль"), this);
QFormLayout* layout = new QFormLayout(group);
m_strokeColorButton = new QPushButton(QStringLiteral("Линия"), group);
m_fillColorButton = new QPushButton(QStringLiteral("Заливка"), group);
m_penWidthSpin = new QSpinBox(group);
m_penWidthSpin->setRange(1, 20);
layout->addRow(QStringLiteral("Цвет линии"), m_strokeColorButton);
layout->addRow(QStringLiteral("Цвет заливки"), m_fillColorButton);
layout->addRow(QStringLiteral("Толщина"), m_penWidthSpin);
return group;
}
QWidget* MainWindow::buildTransformGroup() {
QGroupBox* group = new QGroupBox(QStringLiteral("Трансформация"), this);
QVBoxLayout* layout = new QVBoxLayout(group);
QFormLayout* form = new QFormLayout();
m_scaleXSpin = new QDoubleSpinBox(group);
m_scaleXSpin->setRange(10.0, 2000.0);
m_scaleXSpin->setSuffix(QStringLiteral(" %"));
m_scaleXSpin->setDecimals(0);
m_scaleYSpin = new QDoubleSpinBox(group);
m_scaleYSpin->setRange(10.0, 2000.0);
m_scaleYSpin->setSuffix(QStringLiteral(" %"));
m_scaleYSpin->setDecimals(0);
m_rotationSpin = new QSpinBox(group);
m_rotationSpin->setRange(-3600, 3600);
m_rotationSpin->setSuffix(QStringLiteral("°"));
form->addRow(QStringLiteral("Ширина"), m_scaleXSpin);
form->addRow(QStringLiteral("Высота"), m_scaleYSpin);
form->addRow(QStringLiteral("Поворот"), m_rotationSpin);
layout->addLayout(form);
QHBoxLayout* rotateButtons = new QHBoxLayout();
m_rotateLeftButton = new QPushButton(QStringLiteral("↺ 15°"), group);
m_rotateRightButton = new QPushButton(QStringLiteral("15° ↻"), group);
rotateButtons->addWidget(m_rotateLeftButton);
rotateButtons->addWidget(m_rotateRightButton);
layout->addLayout(rotateButtons);
return group;
}
QWidget* MainWindow::buildMovementGroup() {
QGroupBox* group = new QGroupBox(QStringLiteral("Смещение"), this);
QGridLayout* layout = new QGridLayout(group);
QPushButton* up = new QPushButton(QStringLiteral("↑"), group);
QPushButton* down = new QPushButton(QStringLiteral("↓"), group);
QPushButton* left = new QPushButton(QStringLiteral("←"), group);
QPushButton* right = new QPushButton(QStringLiteral("→"), group);
layout->addWidget(up, 0, 1);
layout->addWidget(left, 1, 0);
layout->addWidget(right, 1, 2);
layout->addWidget(down, 2, 1);
connect(up, &QPushButton::clicked, this, [this]() { m_canvas->moveSelectionBy(0, -1); });
connect(down, &QPushButton::clicked, this, [this]() { m_canvas->moveSelectionBy(0, 1); });
connect(left, &QPushButton::clicked, this, [this]() { m_canvas->moveSelectionBy(-1, 0); });
connect(right, &QPushButton::clicked, this, [this]() { m_canvas->moveSelectionBy(1, 0); });
return group;
}
QWidget* MainWindow::buildLayersGroup() {
QGroupBox* group = new QGroupBox(QStringLiteral("Слои"), this);
QVBoxLayout* layout = new QVBoxLayout(group);
m_layersList = new QListWidget(group);
layout->addWidget(m_layersList);
QGridLayout* buttons = new QGridLayout();
QPushButton* add = new QPushButton(QStringLiteral("Добавить"), group);
QPushButton* remove = new QPushButton(QStringLiteral("Удалить"), group);
QPushButton* up = new QPushButton(QStringLiteral("Вверх"), group);
QPushButton* down = new QPushButton(QStringLiteral("Вниз"), group);
QPushButton* moveSelection = new QPushButton(QStringLiteral("Перенести сюда"), group);
buttons->addWidget(add, 0, 0);
buttons->addWidget(remove, 0, 1);
buttons->addWidget(up, 1, 0);
buttons->addWidget(down, 1, 1);
buttons->addWidget(moveSelection, 2, 0, 1, 2);
layout->addLayout(buttons);
connect(add, &QPushButton::clicked, m_canvas, &Canvas::addLayer);
connect(remove, &QPushButton::clicked, m_canvas, &Canvas::removeActiveLayer);
connect(up, &QPushButton::clicked, m_canvas, &Canvas::moveActiveLayerUp);
connect(down, &QPushButton::clicked, m_canvas, &Canvas::moveActiveLayerDown);
connect(moveSelection, &QPushButton::clicked, m_canvas, &Canvas::moveSelectionToActiveLayer);
return group;
}
QWidget* MainWindow::buildActionsGroup() {
QGroupBox* group = new QGroupBox(QStringLiteral("Действия"), this);
QVBoxLayout* layout = new QVBoxLayout(group);
QPushButton* copy = new QPushButton(QStringLiteral("Копировать"), group);
QPushButton* remove = new QPushButton(QStringLiteral("Удалить фигуру"), group);
QPushButton* clear = new QPushButton(QStringLiteral("Очистить холст"), group);
layout->addWidget(copy);
layout->addWidget(remove);
layout->addWidget(clear);
connect(copy, &QPushButton::clicked, m_canvas, &Canvas::copySelection);
connect(remove, &QPushButton::clicked, m_canvas, &Canvas::deleteSelection);
connect(clear, &QPushButton::clicked, m_canvas, &Canvas::clearAll);
return group;
}
void MainWindow::buildToolbar() {
QToolBar* toolbar = addToolBar(QStringLiteral("Файл"));
toolbar->setMovable(false);
QAction* openAction = toolbar->addAction(QStringLiteral("Открыть"));
connect(openAction, &QAction::triggered, this, &MainWindow::loadDocument);
QAction* saveAction = toolbar->addAction(QStringLiteral("Сохранить"));
connect(saveAction, &QAction::triggered, this, &MainWindow::saveDocument);
QAction* pluginAction = toolbar->addAction(QStringLiteral("Загрузить плагин"));
connect(pluginAction, &QAction::triggered, this, &MainWindow::loadPlugin);
toolbar->addSeparator();
m_undoAction = toolbar->addAction(QStringLiteral("Отменить"));
m_undoAction->setShortcut(QKeySequence::Undo);
connect(m_undoAction, &QAction::triggered, m_canvas, &Canvas::undo);
m_redoAction = toolbar->addAction(QStringLiteral("Повторить"));
m_redoAction->setShortcut(QKeySequence(QStringLiteral("Ctrl+Y")));
connect(m_redoAction, &QAction::triggered, m_canvas, &Canvas::redo);
}
void MainWindow::connectSignals() {
connect(m_strokeColorButton, &QPushButton::clicked, this, &MainWindow::chooseStrokeColor);
connect(m_fillColorButton, &QPushButton::clicked, this, &MainWindow::chooseFillColor);
connect(m_penWidthSpin, qOverload<int>(&QSpinBox::valueChanged), m_canvas, &Canvas::setPenWidth);
connect(m_toolCombo, qOverload<int>(&QComboBox::currentIndexChanged), this, &MainWindow::onToolSelectionChanged);
connect(m_scaleXSpin, qOverload<double>(&QDoubleSpinBox::valueChanged), this, [this](double value) {
if (!m_isRefreshingUi) {
m_canvas->setSelectionScale(value / 100.0, m_scaleYSpin->value() / 100.0);
}
});
connect(m_scaleYSpin, qOverload<double>(&QDoubleSpinBox::valueChanged), this, [this](double value) {
if (!m_isRefreshingUi) {
m_canvas->setSelectionScale(m_scaleXSpin->value() / 100.0, value / 100.0);
}
});
connect(m_rotationSpin, qOverload<int>(&QSpinBox::valueChanged), this, [this](int value) {
if (!m_isRefreshingUi) {
m_canvas->setSelectionRotation(value);
}
});
connect(m_rotateLeftButton, &QPushButton::clicked, this, [this]() { m_canvas->rotateSelectionBy(-15.0); });
connect(m_rotateRightButton, &QPushButton::clicked, this, [this]() { m_canvas->rotateSelectionBy(15.0); });
connect(m_canvas, &Canvas::documentStateChanged, this, &MainWindow::refreshUi);
connect(m_canvas, &Canvas::selectionStateChanged, this, &MainWindow::refreshUi);
connect(m_canvas, &Canvas::activeToolChanged, this, [this](const QString&) { refreshToolSelector(); });
connect(m_canvas, &Canvas::historyStateChanged, this, [this](bool canUndo, bool canRedo) {
m_undoAction->setEnabled(canUndo);
m_redoAction->setEnabled(canRedo);
});
connect(m_layersList, &QListWidget::itemSelectionChanged, this, &MainWindow::onLayerSelectionChanged);
connect(m_registry, &ShapeRegistry::definitionsChanged, this, &MainWindow::refreshToolSelector);
}
void MainWindow::updateColorButton(QPushButton* button, const QColor& color) {
const QString textColor = color.lightness() < 128 ? QStringLiteral("#ffffff") : QStringLiteral("#111111");
button->setStyleSheet(QStringLiteral("background:%1;color:%2;border:1px solid #6b7280;padding:6px;")
.arg(color.name(QColor::HexArgb), textColor));
}
void MainWindow::refreshLayersList() {
const QString currentActive = m_canvas->activeLayerId();
const QVector<LayerData>& layerData = m_canvas->layers();
QSignalBlocker blocker(m_layersList);
m_layersList->clear();
for (int index = layerData.size() - 1; index >= 0; --index) {
QListWidgetItem* item = new QListWidgetItem(layerData[index].name, m_layersList);
item->setData(Qt::UserRole, layerData[index].id);
if (layerData[index].id == currentActive) {
m_layersList->setCurrentItem(item);
}
}
}
void MainWindow::refreshSelectionControls() {
const Shape* selectedShape = m_canvas->primarySelectedShape();
const int selectedCount = m_canvas->selectedCount();
const ShapeStyle referenceStyle = selectedShape ? selectedShape->style : m_canvas->currentStyle();
{
QSignalBlocker blocker(m_penWidthSpin);
m_penWidthSpin->setValue(referenceStyle.penWidth);
}
updateColorButton(m_strokeColorButton, referenceStyle.strokeColor);
updateColorButton(m_fillColorButton, referenceStyle.fillColor);
const bool singleSelection = selectedCount == 1 && selectedShape;
const bool anySelection = selectedCount > 0;
m_scaleXSpin->setEnabled(singleSelection);
m_scaleYSpin->setEnabled(singleSelection);
m_rotationSpin->setEnabled(anySelection);
m_rotateLeftButton->setEnabled(anySelection);
m_rotateRightButton->setEnabled(anySelection);
{
QSignalBlocker blockerX(m_scaleXSpin);
QSignalBlocker blockerY(m_scaleYSpin);
QSignalBlocker blockerR(m_rotationSpin);
if (singleSelection) {
m_scaleXSpin->setValue(selectedShape->scaleX * 100.0);
m_scaleYSpin->setValue(selectedShape->scaleY * 100.0);
m_rotationSpin->setValue(static_cast<int>(selectedShape->rotation));
} else {
m_scaleXSpin->setValue(100.0);
m_scaleYSpin->setValue(100.0);
m_rotationSpin->setValue(selectedShape ? static_cast<int>(selectedShape->rotation) : 0);
}
}
}