File size: 11,813 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
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
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software 
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** 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 "rs_actionblocksinsert.h"

#include "qg_insertoptions.h"
#include "rs_block.h"
#include "rs_creation.h"
#include "rs_graphic.h"
#include "rs_insert.h"
#include "rs_preview.h"

/**
 * Constructor.
 */
// fixme - sand - ucs - SUPPORT UCS, ANGLES FOR INSERTION!
RS_ActionBlocksInsert::RS_ActionBlocksInsert(LC_ActionContext *actionContext)
    : RS_PreviewActionInterface("Blocks Insert", actionContext, RS2::ActionBlocksInsert),
      m_block(nullptr),
      m_lastStatus(SetUndefined){
    reset(); // init data Member
}

RS_ActionBlocksInsert::~RS_ActionBlocksInsert() = default;

void RS_ActionBlocksInsert::init(int status){
    RS_PreviewActionInterface::init(status);
    reset();

    if (m_graphic != nullptr) {
        m_block = m_graphic->getActiveBlock();
        if (m_block != nullptr) {
            QString blockName = m_block->getName();
            m_data->name = blockName;
            if (m_document->is(RS2::EntityBlock)) {
                QString parentBlockName = static_cast<RS_Block*>(m_document)->getName();
                if (parentBlockName == blockName) {
                    commandMessage(tr("Block cannot contain an insert of itself."));
                    finish(false);
                } else {
                    QStringList bnChain = m_block->findNestedInsert(parentBlockName);
                    if (!bnChain.empty()) {
                        // fixme - sand - think where to report the error...
                        commandMessage(blockName
                                       + tr(" has nested insert of current block in:\n")
                                       + bnChain.join("->")
                                       + tr("\nThis block cannot be inserted."));
                        finish(false);
                    }
                }
            }
        } else {
            finish(false);
        }
    }
}

void RS_ActionBlocksInsert::reset(){
    m_data.reset(new RS_InsertData("", RS_Vector(0.0, 0.0), RS_Vector(1.0, 1.0), 0.0,
                                   1, 1, RS_Vector(1.0, 1.0), nullptr, RS2::Update));
}

void RS_ActionBlocksInsert::trigger(){
    deletePreview();

    if (m_block != nullptr) {
        RS_Creation creation(m_container, m_viewport);
        m_data->updateMode = RS2::Update;
        auto insertData = m_data.get();
        auto insertDataCopy = new RS_InsertData(*insertData);
        insertDataCopy->angle = toWorldAngleFromUCSBasis(insertData->angle);
        creation.createInsert(insertDataCopy);
    }

    redrawDrawing();
}

void RS_ActionBlocksInsert::onMouseMoveEvent(int status, LC_MouseEvent *e){
    switch (status) {
        case SetTargetPoint: {
            m_data->insertionPoint = e->snapPoint;
            if (m_block != nullptr) {
                //preview->addAllFrom(*block);
                //preview->move(data->insertionPoint);
                RS_Creation creation(m_preview.get(), m_viewport, false);
                // Create insert as preview only
                m_data->updateMode = RS2::PreviewUpdate;
                auto insertData = m_data.get();
                auto insertDataCopy = new RS_InsertData(*insertData);
                insertDataCopy->angle = toWorldAngleFromUCSBasis(insertData->angle);
                insertDataCopy->updateMode = RS2::Update;
                creation.createInsert(insertDataCopy);
            }
            break;
        }
        default:
            break;
    }
}

bool RS_ActionBlocksInsert::doUpdateAngleByInteractiveInput(const QString& tag, double angle) {
    if (tag == "angle") {
        setAngle(angle);
        return true;
    }
    return false;
}

bool RS_ActionBlocksInsert::doUpdateDistanceByInteractiveInput(const QString& tag, double distance) {
    if (tag == "spacingX") {
        setColumnSpacing(distance);
        return true;
    }
    if (tag == "spacingY") {
        setRowSpacing(distance);
        return true;
    }
    return false;
}

void RS_ActionBlocksInsert::onMouseLeftButtonRelease([[maybe_unused]] int status, LC_MouseEvent *e){
    fireCoordinateEvent(e->snapPoint);
}

void RS_ActionBlocksInsert::onMouseRightButtonRelease(int status, [[maybe_unused]] LC_MouseEvent *e){
    initPrevious(status);
}

void RS_ActionBlocksInsert::onCoordinateEvent([[maybe_unused]] int status, [[maybe_unused]] bool isZero, const RS_Vector &pos){
    m_data->insertionPoint = pos;
    trigger();
}

bool RS_ActionBlocksInsert::doProcessCommand(int status, const QString &c){
    bool accept = false;
    switch (status) {
        case SetTargetPoint: {
            if (checkCommand("angle", c)) {
                deletePreview();
                m_lastStatus = (Status) status;
                setStatus(SetAngle);
                accept = true;
            } else if (checkCommand("factor", c)) {
                deletePreview();
                m_lastStatus = (Status) status;
                setStatus(SetFactor);
                accept = true;
            } else if (checkCommand("columns", c)) {
                deletePreview();
                m_lastStatus = (Status) status;
                setStatus(SetColumns);
                accept = true;
            } else if (checkCommand("rows", c)) {
                deletePreview();
                m_lastStatus = (Status) status;
                setStatus(SetRows);
                accept = true;
            } else if (checkCommand("columnspacing", c)) {
                deletePreview();
                m_lastStatus = (Status) status;
                accept = true;
                setStatus(SetColumnSpacing);
            } else if (checkCommand("rowspacing", c)) {
                deletePreview();
                m_lastStatus = (Status) status;
                setStatus(SetRowSpacing);
                accept = true;
            }
            break;
        }
        case SetAngle: {
            bool ok;
            double a = RS_Math::eval(c, &ok);
            if (ok) {
                accept = true;
                m_data->angle = RS_Math::deg2rad(a);
            } else {
                commandMessage(tr("Not a valid expression"));
            }
            updateOptions();
            setStatus(m_lastStatus);
            break;
        }
        case SetFactor: {
            bool ok;
            double f = RS_Math::eval(c, &ok);
            if (ok) {
                setFactor(f);
                accept = true;
            } else {
                commandMessage(tr("Not a valid expression"));
            }
            updateOptions();
            setStatus(m_lastStatus);
            break;
        }
        case SetColumns: {
            bool ok;
            int cols = (int) RS_Math::eval(c, &ok);
            if (ok) {
                m_data->cols = cols;
                accept = true;
            } else {
                commandMessage(tr("Not a valid expression"));
            }
            updateOptions();
            setStatus(m_lastStatus);
            break;
        }
        case SetRows: {
            bool ok;
            int rows = (int) RS_Math::eval(c, &ok);
            if (ok) {
                m_data->rows = rows;
                accept = true;
            } else {
                commandMessage(tr("Not a valid expression"));
            }
            updateOptions();
            setStatus(m_lastStatus);
            break;
        }
        case SetColumnSpacing: {
            bool ok;
            double cs = (int) RS_Math::eval(c, &ok);
            if (ok) {
                m_data->spacing.x = cs;
                accept = true;
            } else {
                commandMessage(tr("Not a valid expression"));
            }
            updateOptions();
            setStatus(m_lastStatus);
            break;
        }
        case SetRowSpacing: {
            bool ok;
            int rs = (int) RS_Math::eval(c, &ok);
            if (ok) {
                m_data->spacing.y = rs;
                accept = true;
            } else {
                commandMessage(tr("Not a valid expression"));
            }
            updateOptions();
            setStatus(m_lastStatus);
            break;
        }
        default:
            break;
    }
    return accept;
}

double RS_ActionBlocksInsert::getAngle() const{
    return m_data->angle;
}

void RS_ActionBlocksInsert::setAngle(double a){
    m_data->angle = a;
}

double RS_ActionBlocksInsert::getFactor() const{
    return m_data->scaleFactor.x;
}

void RS_ActionBlocksInsert::setFactor(double f){
    m_data->scaleFactor = RS_Vector(f, f);
}

int RS_ActionBlocksInsert::getColumns() const{
    return m_data->cols;
}

void RS_ActionBlocksInsert::setColumns(int c){
    m_data->cols = c;
}

int RS_ActionBlocksInsert::getRows() const{
    return m_data->rows;
}

void RS_ActionBlocksInsert::setRows(int r){
    m_data->rows = r;
}

double RS_ActionBlocksInsert::getColumnSpacing() const{
    return m_data->spacing.x;
}

void RS_ActionBlocksInsert::setColumnSpacing(double cs){
    m_data->spacing.x = cs;
}

double RS_ActionBlocksInsert::getRowSpacing() const{
    return m_data->spacing.y;
}

void RS_ActionBlocksInsert::setRowSpacing(double rs){
    m_data->spacing.y = rs;
}

QStringList RS_ActionBlocksInsert::getAvailableCommands(){
    QStringList cmd;

    switch (getStatus()) {
        case SetTargetPoint:
            cmd += command("angle");
            cmd += command("factor");
            cmd += command("columns");
            cmd += command("rows");
            cmd += command("columnspacing");
            cmd += command("rowspacing");
            break;
        default:
            break;
    }

    return cmd;
}

void RS_ActionBlocksInsert::updateMouseButtonHints(){
    switch (getStatus()) {
        case SetTargetPoint:
            updateMouseWidgetTRCancel(tr("Specify reference point"));
            break;
        case SetAngle:
            updateMouseWidget(tr("Enter angle:"));
            break;
        case SetFactor:
            updateMouseWidget(tr("Enter factor:"));
            break;
        case SetColumns:
            updateMouseWidget(tr("Enter columns:"));
            break;
        case SetRows:
            updateMouseWidget(tr("Enter rows:"));
            break;
        case SetColumnSpacing:
            updateMouseWidget(tr("Enter column spacing:"));
            break;
        case SetRowSpacing:
            updateMouseWidget(tr("Enter row spacing:"));
            break;
        default:
            updateMouseWidget();
            break;
    }
}

RS2::CursorType RS_ActionBlocksInsert::doGetMouseCursor([[maybe_unused]] int status){
    return RS2::CadCursor;
}

LC_ActionOptionsWidget *RS_ActionBlocksInsert::createOptionsWidget(){
    return new QG_InsertOptions();
}