File size: 10,661 Bytes
a5ffdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2024 sand1024
**
** This file 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 2 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, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/

#include <QMessageBox>

#include "lc_layerdialog_ex.h"
#include "lc_layertreeitem.h"
#include "lc_layertreemodel.h"
#include "lc_layertreemodel_options.h"
#include "rs_layer.h"
#include "rs_layerlist.h"

LC_LayerDialogEx::LC_LayerDialogEx(QWidget* parent, const QString& name, LC_LayerTreeModel* model, LC_LayerTreeItem *treeItem, RS_LayerList* ll)
      : QDialog(parent/*, f*l*/)
{
    setupUi(this);
    setModal(true);
    setObjectName(name);

    m_layerTreeModel = model;
    m_layerList = ll;
    m_editedTreeItem = treeItem;
}

void LC_LayerDialogEx::languageChange(){
    retranslateUi(this);
}

void LC_LayerDialogEx::setMode(int viewMode){
    int mode = viewMode;
    this->setProperty("mode", mode);
    leParentPath->setEnabled(false);
    switch (mode){
        case LC_LayerDialogEx::MODE_RENAME_VIRTUAL:{
            leName->setFocus();
            wPen->setVisible(false);
            cbConstructionLayer ->setVisible(false);
            gbLayerType->setVisible(false);
            allowChangingLayerType(false);
            setWindowTitle(tr("Rename Virtual Layer"));
            break;
        }
        case LC_LayerDialogEx::MODE_ADD_SECONDARY_LAYER:{
            setWindowTitle(tr("Add Secondary Layer"));
            leName->setFocus();
            allowChangingLayerType(false);
          break;
        }
        case LC_LayerDialogEx::MODE_ADD_CHILD_LAYER:{
            setWindowTitle(tr("Add Layer"));
            leParentPath->setEnabled(false);
            leName->setFocus();
            allowChangingLayerType(true);
            break;
        }
        case LC_LayerDialogEx::MODE_ADD_LAYER:{
            setWindowTitle(tr("Add Layer"));
            leName->setFocus();
            allowChangingLayerType(true);
            break;
        }
        case LC_LayerDialogEx::MODE_EDIT_LAYER:{
            setWindowTitle(tr("Edit Layer"));
            if (m_editedTreeItem->isZero()){
                allowChangingLayerType(false);
                leName->setDisabled(true);
            }
            else {
                leName->setFocus();
                allowChangingLayerType(true);
            }
            break;
        }
        default:
            break;
    }
}

void LC_LayerDialogEx::allowChangingLayerType(bool value){
    gbLayerType->setEnabled(value);
    if (value){
        connect(rbNormal, &QRadioButton::clicked, this, &LC_LayerDialogEx::layerTypeChanged);
        connect(rbDimensions, &QRadioButton::clicked, this, &LC_LayerDialogEx::layerTypeChanged);
        connect(rbAlternativePosition, &QRadioButton::clicked, this, &LC_LayerDialogEx::layerTypeChanged);
        connect(rbInformational, &QRadioButton::clicked, this, &LC_LayerDialogEx::layerTypeChanged);
    }
}

void LC_LayerDialogEx::setLayerType(int type) const {
    switch (type){
        case LC_LayerTreeItem::VIRTUAL: {
            gbLayerType->setVisible(false);
            break;
        }
        case LC_LayerTreeItem::NOT_DEFINED_LAYER_TYPE:
        case LC_LayerTreeItem::NORMAL: {
            gbLayerType->setVisible(true);
            rbNormal->setChecked(true);
            break;
        }
        case LC_LayerTreeItem::DIMENSIONAL: {
            gbLayerType->setVisible(true);
            rbDimensions->setChecked(true);
            break;
        }
        case LC_LayerTreeItem::ALTERNATE_POSITION: {
            gbLayerType->setVisible(true);
            rbAlternativePosition->setChecked(true);
            break;
        }
        case LC_LayerTreeItem::INFORMATIONAL: {
            gbLayerType->setVisible(true);
            rbInformational->setChecked(true);
            break;
        }
        default:
            break;
    }
}

int LC_LayerDialogEx::getEditedLayerType() const {
    int result = LC_LayerTreeItem::NOT_DEFINED_LAYER_TYPE;
    if (rbNormal->isChecked()){
        result = LC_LayerTreeItem::NORMAL;
    }
    else if (rbDimensions->isChecked()){
        result = LC_LayerTreeItem::DIMENSIONAL;
    }
    else if (rbAlternativePosition->isChecked()){
        result = LC_LayerTreeItem::ALTERNATE_POSITION;
    }
    else if (rbInformational->isChecked()){
        result = LC_LayerTreeItem::INFORMATIONAL;
    }
    return result;
}

void LC_LayerDialogEx::setLayerName(const QString& name) const {
    leName ->setText(name);
}

void LC_LayerDialogEx::setParentPath(const QString &name) const {
    leParentPath -> setText(name);
}

void LC_LayerDialogEx::disableNames() const {
    leName ->setEnabled(false);
    leParentPath ->setVisible(false);
    lParentPathName->setVisible(false);
}

void LC_LayerDialogEx::setLayer(const RS_Layer* layer) const {
    if (layer != nullptr){
        wPen->setVisible(true);
        wPen->setPen(layer->getPen(), false, false, tr("Default Pen"));
        cbConstructionLayer->setChecked(layer->isConstruction());
        cbConstructionLayer->setVisible(true);
    }
    else{
        wPen ->setVisible(false);
        cbConstructionLayer ->setVisible(false);
    }
}

RS_Pen LC_LayerDialogEx::getPen() const {
    return wPen->getPen();
}
void LC_LayerDialogEx::init(){
    leName->setFocus();
    wPen->setVisible(false);
    cbConstructionLayer->setVisible(false);
    setWindowTitle(tr("Rename Layer"));
}

void LC_LayerDialogEx::layerTypeChanged() const {
    int layerType = -1;
    if (rbNormal->isChecked()){
        layerType = LC_LayerTreeItem::NORMAL;
    }
    else if (rbDimensions ->isChecked()){
        layerType = LC_LayerTreeItem::DIMENSIONAL;
    }
    else if (rbAlternativePosition -> isChecked()){
        layerType = LC_LayerTreeItem::ALTERNATE_POSITION;
    }
    else if (rbInformational->isChecked()){
        layerType = LC_LayerTreeItem::INFORMATIONAL;
    }

    if (layerType > 0){
        RS_Pen defaultPen = m_layerTreeModel ->getOptions()->getDefaultPen(layerType);
        RS_Pen penCopy  = RS_Pen(defaultPen);
        wPen->setPen(penCopy, false, false, tr("Default Pen"));
    }
}

void LC_LayerDialogEx::validate() {
    QString layerName = leName->text();
    if (layerName.trimmed().isEmpty()){
        QMessageBox::information(parentWidget(),
                                 QMessageBox::tr("Layer Properties"),
                                 QMessageBox::tr("Layer empty name is not allowed."),
                                 QMessageBox::Ok);
    }
    else{
        int newLayerType = getEditedLayerType();
        QStringList newLayerNamesList;

        int dialogMode = property("mode").toInt();
        switch (dialogMode) {
            case MODE_ADD_LAYER: {
                QString newLayerName = m_layerTreeModel->createFullLayerName(
                    m_editedTreeItem, layerName, newLayerType, true);
                newLayerNamesList << newLayerName;
                break;
            }
            case MODE_ADD_CHILD_LAYER:
            case MODE_ADD_SECONDARY_LAYER: {
                QString newLayerName = m_layerTreeModel->createFullLayerName(
                    m_editedTreeItem, layerName, newLayerType, true);
                newLayerNamesList << newLayerName;
                break;
            }
            case MODE_RENAME_VIRTUAL: {
                QString oldLayerName = m_editedTreeItem->getName();
                if (layerName != oldLayerName) {
                    newLayerNamesList = m_layerTreeModel->getLayersListForRenamedVirtualLayer(m_editedTreeItem, layerName);
                }
                break;
            }
            case MODE_EDIT_LAYER: {
                QString originalName = m_editedTreeItem->getName();
                int originalLayerType = m_editedTreeItem->getLayerType();
                bool typeChanged = newLayerType != originalLayerType;
                bool nameChanged = originalName != layerName;
                if (nameChanged || typeChanged) {
                    // inner name should be changed - so we have to check for possible duplicates.
                    newLayerNamesList = m_layerTreeModel->getLayersListForRenamedPrimary(
                        m_editedTreeItem, layerName, newLayerType);
                }
                break;
            }
            default:
                break;
        }

        if (newLayerNamesList.isEmpty()){
          accept();
        }
        else{
            bool duplicateNameFound = checkForDuplicatedNames(newLayerNamesList);
            if (!duplicateNameFound){                
                accept();
            }
        }
    }
}

bool LC_LayerDialogEx::checkForDuplicatedNames(const QStringList &newLayerNamesList) const {
    int count = newLayerNamesList.size();
    bool duplicateNameFound = false;
    for (int i = 0; i < count; i++) {
        QString candidateLayerName = newLayerNamesList.at(i);
        // here we check against layer list and not against model, since some filtering may be applied
        RS_Layer* existingLayer = m_layerList->find(candidateLayerName);
        if (existingLayer != nullptr){
            QMessageBox::information(parentWidget(),
                                     QMessageBox::tr("Layer Properties"),
                                     QMessageBox::tr("Attempt to create layer with duplicating name. Duplicated layer name is \n[%1].\n"
                                                     "Please specify a different name.")
                                     .arg(candidateLayerName),
                                     QMessageBox::Ok);
            leName->setFocus();
            leName->selectAll();
            duplicateNameFound = true;
            break;
        }
    }
    return duplicateNameFound;
}

QString LC_LayerDialogEx::getLayerName() const {
    return leName->text();
}