/
layermanager.cpp
355 lines (295 loc) · 12.6 KB
/
layermanager.cpp
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
/**********************************************************************
** smepowercad
** Copyright (C) 2015 Smart Micro Engineering GmbH
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include "layermanager.h"
#include "ui_layermanager.h"
Q_LOGGING_CATEGORY(layermanager, "powercard.layermanager")
LayerManager::LayerManager(ItemDB *itemDb, QWidget *parent) :
QDockWidget(parent),
ui(new Ui::LayerManager),
m_itemDb(itemDb),
m_clipboardIndex()
{
ui->setupUi(this);
this->setStyleSheet(StylesheetProvider::getStylesheet("QTreeView,LayerManager"));
QDesktopWidget desktopWidget;
QRect rightScreenRect = desktopWidget.screenGeometry(desktopWidget.numScreens() -1);
if (desktopWidget.screenCount() > 1) {
this->resize(this->width(), rightScreenRect.height());
this->move(rightScreenRect.topLeft());
}
ui->treeView_layer->setItemDelegate(new TreeViewItemDelegate(this));
ui->treeView_layer->setModel(m_itemDb);
ui->treeView_layer->setColumnWidth(1, 24);
ui->treeView_layer->setColumnWidth(2, 24);
ui->treeView_layer->setColumnWidth(3, 24);
ui->treeView_layer->header()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->treeView_layer->header()->setSectionResizeMode(1, QHeaderView::Fixed);
ui->treeView_layer->header()->setSectionResizeMode(2, QHeaderView::Fixed);
ui->treeView_layer->header()->setSectionResizeMode(3, QHeaderView::Fixed);
ui->treeView_layer->header()->setSectionResizeMode(4, QHeaderView::ResizeToContents);
ui->treeView_layer->header()->setSectionResizeMode(5, QHeaderView::ResizeToContents);
ui->treeView_layer->header()->setSectionResizeMode(6, QHeaderView::ResizeToContents);
ui->treeView_layer->header()->setSectionResizeMode(7, QHeaderView::ResizeToContents);
m_menuNoItem = new QMenu(this);
m_menuNoItem->addAction(tr("New layer"), this, SLOT(slot_appendNewLayer()));
m_menuOnItem = new QMenu(this);
m_menuOnItem->addAction(tr("Edit name"), this, SLOT(slot_editLayerName()));
m_menuOnItem->addAction(tr("Edit line width"), this, SLOT(slot_editLayerLineWidth()));
m_menuOnItem->addAction(tr("Edit line type"), this, SLOT(slot_editLayerLineType()));
m_menuOnItem->addSeparator();
m_menuOnItem->addAction(tr("Append new Layer"), this, SLOT(slot_appendNewLayer()));
m_menuOnItem->addAction(tr("Append new Layer as child"), this, SLOT(slot_appendNewLayerAsChild()));
m_menuOnItem->addAction(tr("Delete layer"), this, SLOT(slot_deleteLayer()));
m_menuOnItem->addSeparator();
m_actionCut = m_menuOnItem->addAction(tr("Cut"), this, SLOT(slot_cutLayer()));
m_actionPasteHere = m_menuOnItem->addAction(tr("Paste here"), this, SLOT(slot_pasteLayerHere()));
m_actionPasteAsChild = m_menuOnItem->addAction(tr("Paste as child"), this, SLOT(slot_pasteLayerAsChild()));
m_actionPasteHere->setDisabled(true);
m_actionPasteAsChild->setDisabled(true);
}
LayerManager::~LayerManager()
{
delete ui;
}
void LayerManager::updateLayer(Layer *layer)
{
// QModelIndex index;
// QModelIndexList indexList = m_itemDb->match(m_itemDb->index(0, 0, QModelIndex()), Qt::DisplayRole, layer->name, 1, Qt::MatchRecursive);
// if (indexList.count() >= 1)
// index = indexList.first();
// else
// index = m_itemDb->index(0, 0, QModelIndex());
// emit m_itemDb->dataChanged(index, index);
}
void LayerManager::updateSoloActive()
{
m_itemDb->layerSoloActive = false;
updateSoloActive_processLayers(m_itemDb->getRootLayer()->getChildLayers());
if (m_itemDb->layerSoloActive) {
setWindowTitle(tr("Layermanager [solo active]"));
} else {
setWindowTitle(tr("Layermanager"));
}
}
void LayerManager::updateSoloActive_processLayers(LayerList layers)
{
foreach (Layer *layer, layers) {
if (layer->solo)
m_itemDb->layerSoloActive = true;
updateSoloActive_processLayers(layer->getChildLayers());
}
}
void LayerManager::slot_editLayerName(Layer *layer)
{
if (!layer) layer = m_layerAtContextMenuRequest;
QString layerName;
bool ok;
do {
layerName = QInputDialog::getText(this, tr("Rename layer %1").arg(layer->name),
tr("Layer name"), QLineEdit::Normal, layer->name, &ok);
if (!ok) break;
if (layerName.isEmpty())
QMessageBox::warning(this, tr("New layer"), tr("Layer name cannot be empty"));
} while (layerName.isEmpty());
if (ok) layer->name = layerName;
}
void LayerManager::slot_editLayerLineWidth(Layer *layer)
{
if (!layer) layer = m_layerAtContextMenuRequest;
int lineWidth = QInputDialog::getInt(this, tr("Edit line width of layer %1").arg(layer->name),
tr("New width (pixel)"), layer->lineWidth, 1, 10);
layer->lineWidth = lineWidth;
}
void LayerManager::slot_editLayerLineType(Layer *layer)
{
if (!layer) layer = m_layerAtContextMenuRequest;
QStringList items = QStringList();
for (int i = 0; i < layer->metaEnum_lineType.keyCount(); i++) {
items.append(layer->metaEnum_lineType.key(i));
}
Layer::LineType lineType = (Layer::LineType)items.indexOf(
QInputDialog::getItem(this, tr("Edit line type of layer %1").arg(layer->name),
tr("New Linetype"), items, layer->lineType, false));
layer->lineType = lineType;
}
void LayerManager::slot_appendNewLayer()
{
QString layerName;
bool alreadyExists = false;
bool ok;
QString path;
do {
if (alreadyExists)
QMessageBox::warning(this, tr("New layer"), tr("Layer name already in use! Try a different name!"));
if (layerName.isEmpty() || alreadyExists) {
do {
layerName = QInputDialog::getText(this, tr("New layer"), tr("Layer name"), QLineEdit::Normal, NULL, &ok);
if (!ok) break;
if (layerName.isEmpty())
QMessageBox::warning(this, tr("New layer"), tr("Layer name cannot be empty"));
} while(layerName.isEmpty());
}
if (m_layerAtContextMenuRequest == NULL)
path = layerName;
else
path = m_layerAtContextMenuRequest->parentLayer()->path(layerName);
if (!ok) break;
} while (m_itemDb->findLayerByPath(path) != NULL && (alreadyExists = true));
if (ok) {
int at = 0;
if (m_layerAtContextMenuRequest)
at = m_layerAtContextMenuRequest->parentLayer()->childCount();
m_itemDb->insertLayer(layerName, m_itemDb->parent(m_indexAtContextMenuRequest), at);
}
}
void LayerManager::slot_appendNewLayerAsChild()
{
QString layerName;
bool alreadyExists = false;
bool ok;
QString path;
do {
if (alreadyExists)
QMessageBox::warning(this, tr("New layer"), tr("Layer name already in use! Try a different name!"));
if (layerName.isEmpty() || alreadyExists) {
do {
layerName = QInputDialog::getText(this, tr("New layer"), tr("Layer name"), QLineEdit::Normal, NULL, &ok);
if (!ok) break;
if (layerName.isEmpty())
QMessageBox::warning(this, tr("New layer"), tr("Layer name cannot be empty"));
} while(layerName.isEmpty());
}
path = m_layerAtContextMenuRequest->path(layerName);
if (!ok) break;
} while (m_itemDb->findLayerByPath(path) != NULL && (alreadyExists = true));
if (ok) {
m_itemDb->insertLayer(layerName, m_indexAtContextMenuRequest,
m_layerAtContextMenuRequest->childCount());
ui->treeView_layer->expand(m_indexAtContextMenuRequest);
}
}
void LayerManager::slot_deleteLayer()
{
int ret = QMessageBox::question(this, tr("Delete layer"),
tr("Do you want to delete layer %1").arg(m_layerAtContextMenuRequest->name),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (ret != QMessageBox::Yes) return;
bool success = m_itemDb->deleteLayerAt(m_itemDb->parent(m_indexAtContextMenuRequest),
m_indexAtContextMenuRequest);
if (!success)
QMessageBox::warning(this, tr("Delete layer"), tr("Unable to delete layer. Maybe it's not empty."));
}
void LayerManager::slot_cutLayer()
{
m_clipboardIndex = m_indexAtContextMenuRequest;
}
void LayerManager::slot_pasteLayerHere()
{
QModelIndex index_destination = m_itemDb->parent(m_indexAtContextMenuRequest);
quint32 position = m_indexAtContextMenuRequest.row() + 1;
m_itemDb->moveLayer(m_clipboardIndex, index_destination, position);
m_actionPasteHere->setDisabled(true);
m_actionPasteAsChild->setDisabled(true);
m_clipboardIndex = QModelIndex();
}
void LayerManager::slot_pasteLayerAsChild()
{
QModelIndex index_destination = m_indexAtContextMenuRequest;
m_itemDb->moveLayer(m_clipboardIndex, index_destination, 0);
m_actionPasteHere->setDisabled(true);
m_actionPasteAsChild->setDisabled(true);
m_clipboardIndex = QModelIndex();
}
void LayerManager::on_treeView_layer_customContextMenuRequested(const QPoint &pos)
{
QModelIndex index = ui->treeView_layer->indexAt(pos);
m_indexAtContextMenuRequest = index;
m_layerAtContextMenuRequest = static_cast<Layer*>(index.internalPointer());
if (index.isValid()) {
if (m_itemDb->isChildOfLayer(static_cast<Layer*>(m_clipboardIndex.internalPointer()), m_layerAtContextMenuRequest))
{
m_actionPasteHere->setDisabled(true);
m_actionPasteAsChild->setDisabled(true);
}
else
{
if (m_clipboardIndex.isValid())
{
m_actionPasteHere->setEnabled(true);
m_actionPasteAsChild->setEnabled(true);
}
}
m_menuOnItem->popup(QCursor::pos());
} else {
m_menuNoItem->popup(QCursor::pos());
}
}
void LayerManager::on_treeView_layer_activated(const QModelIndex &index)
{
m_itemDb->setCurrentLayer(static_cast<Layer*>(index.internalPointer()));
}
void LayerManager::on_treeView_layer_clicked(const QModelIndex &index)
{
Layer *layer = static_cast<Layer*>(index.internalPointer());
m_itemDb->setCurrentLayer(layer);
int col = index.column();
if (col == 1) { // Ein
layer->isOn = !layer->isOn;
} else if (col == 2) { // R/W
layer->isWriteable = !layer->isWriteable;
} else if (col == 3) { // Solo
layer->solo = !layer->solo;
updateSoloActive();
} else if (col == 4) { // Brush
QColorDialog colorDialog(layer->brush.color(), this);
colorDialog.setWindowTitle(tr("Fillcolor of layer %1").arg(layer->name));
colorDialog.setModal(true);
colorDialog.setOption(QColorDialog::ShowAlphaChannel, true);
if (colorDialog.exec() == QDialog::Rejected) {
return;
}
layer->brush.setColor(colorDialog.currentColor());
} else if (col == 5) { // Pen
QColorDialog colorDialog(layer->pen.color(), this);
colorDialog.setWindowTitle(tr("Linecolor of layer %1").arg(layer->name));
colorDialog.setModal(true);
colorDialog.setOption(QColorDialog::ShowAlphaChannel, true);
if (colorDialog.exec() == QDialog::Rejected) {
return;
}
layer->pen.setColor(colorDialog.currentColor());
}
emit ui->treeView_layer->dataChanged(index, index);
// TODO: updateLayer(layer);
emit signal_repaintNeeded();
}
void LayerManager::on_treeView_layer_doubleClicked(const QModelIndex &index)
{
Layer *layer = static_cast<Layer*>(index.internalPointer());
int col = index.column();
if (col == 0) { // name
slot_editLayerName(layer);
} else if (col == 6) { // line width
slot_editLayerLineWidth(layer);
} else if (col == 7) { // line type
slot_editLayerLineType(layer);
}
emit ui->treeView_layer->dataChanged(index, index);
}
void LayerManager::on_treeView_layer_expanded(const QModelIndex &index)
{
Q_UNUSED(index)
ui->treeView_layer->resizeColumnToContents(0);
}