/
InsertBlock.js
341 lines (283 loc) · 10.6 KB
/
InsertBlock.js
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
/**
* Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
*
* This file is part of the QCAD project.
*
* QCAD 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.
*
* QCAD 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 QCAD.
*/
include("../BlockInsert.js");
/**
* \class InsertBlock
* \ingroup ecma_block
* \brief This action handles the user interaction to insert a block
* from the block list into the drawing.
*/
function InsertBlock(guiAction) {
BlockInsert.call(this, guiAction);
this.setUiOptions("InsertBlock.ui");
this.blockReferenceData = new RBlockReferenceData();
this.blockReferenceData.setPosition(new RVector(0.0,0.0));
this.blockReferenceData.setScaleFactors(new RVector(1.0,1.0));
this.blockReferenceData.setRotation(0.0);
}
InsertBlock.State = {
SettingPosition : 0
};
InsertBlock.prototype = new BlockInsert();
InsertBlock.includeBasePath = includeBasePath;
InsertBlock.prototype.beginEvent = function() {
BlockInsert.prototype.beginEvent.call(this);
var blockId = Block.getActiveBlockId();
if (blockId===RObject.INVALID_ID) {
this.terminate();
return;
}
this.blockReferenceData.setReferencedBlockId(blockId);
var i, label, edit, sep, a;
var optionsToolBar = EAction.getOptionsToolBar();
// init block attribute inputs to options tool bar:
var doc = this.getDocument();
if (!isNull(doc)) {
var ids = doc.queryBlockEntities(blockId);
var first = true;
var items = [];
for (i=0; i<ids.length; i++) {
var id = ids[i];
var e = doc.queryEntityDirect(id);
if (!isAttributeDefinitionEntity(e)) {
continue;
}
if (first) {
this.showAttributeControls(true);
first = false;
}
var tag = e.getTag();
var prompt = e.getPrompt();
var defaultValue = e.getEscapedText();
items.push([prompt, [tag, defaultValue]]);
}
items.sort(function(a, b) {
return Array.alphaNumericalSorter(a[0], b[0]);
});
var tagCombo = optionsToolBar.findChild("AttributeTag");
for (i=0; i<items.length; i++) {
var item = items[i];
tagCombo.addItem(item[0], item[1]);
}
}
this.setState(InsertBlock.State.SettingPosition);
};
InsertBlock.prototype.initUiOptions = function(resume, optionsToolBar) {
BlockInsert.prototype.initUiOptions.call(this, resume, optionsToolBar);
//var optionsToolBar = EAction.getOptionsToolBar();
var combo = optionsToolBar.findChild("ScaleX");
combo.setCompleter(null);
combo = optionsToolBar.findChild("ScaleY");
combo.setCompleter(null);
combo = optionsToolBar.findChild("Rotation");
combo.setCompleter(null);
};
InsertBlock.prototype.setState = function(state) {
BlockInsert.prototype.setState.call(this, state);
this.setCrosshairCursor();
this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
var appWin = RMainWindowQt.getMainWindow();
var trPos = qsTr("Position");
this.setCommandPrompt(trPos);
this.setLeftMouseTip(trPos);
this.setRightMouseTip(EAction.trCancel);
EAction.showSnapTools();
};
InsertBlock.prototype.pickCoordinate = function(event, preview) {
var di = this.getDocumentInterface();
var pos = event.getModelPosition();
this.blockReferenceData.setPosition(pos);
if (preview) {
di.previewOperation(this.getOperation(true));
}
else {
var op = this.getOperation(false);
if (!isNull(op)) {
di.applyOperation(op);
di.setRelativeZero(pos);
}
}
};
InsertBlock.prototype.getOperation = function(preview) {
var doc = this.getDocument();
if (isNull(doc)) {
return undefined;
}
var blockId = this.blockReferenceData.getReferencedBlockId();
var ids = doc.queryBlockEntities(blockId);
var bd = this.blockReferenceData;
if (preview) {
bd = bd.copy();
// reduce columns / rows for preview:
if (bd.getColumnCount()>1 || bd.getRowCount()>1) {
// max insertions:
var max = RSettings.getPreviewEntities() / ids.length;
// reduce rows:
if (max<2) {
bd.setColumnCount(1);
bd.setRowCount(1);
}
else if (max<bd.getColumnCount()) {
bd.setColumnCount(max);
bd.setRowCount(1);
}
else {
bd.setRowCount(Math.min(bd.getRowCount(), max/bd.getColumnCount()));
}
}
}
var op = new RAddObjectsOperation();
op.setText(this.getToolTitle());
var blockRef = new RBlockReferenceEntity(
doc,
bd
);
op.addObject(blockRef);
// create attribute for each attribute definition in block:
var blockRefId = doc.getStorage().getMaxObjectId();
for (var i=0; i<ids.length; i++) {
var id = ids[i];
var attDef = doc.queryEntity(id);
if (!isAttributeDefinitionEntity(attDef)) {
continue;
}
var att = new RAttributeEntity(
doc,
new RAttributeData(attDef.getData(), blockRefId, attDef.getTag())
);
att.setBlockId(doc.getCurrentBlockId());
att.setInvisible(attDef.isInvisible());
blockRef.applyTransformationTo(att);
// assign values to attributes:
var tag = att.getTag();
if (!isNull(this.attributes[tag])) {
att.setText(this.attributes[tag]);
}
op.addObject(att, false);
}
return op;
};
InsertBlock.prototype.slotKeepProportionsChanged = function(value) {
var optionsToolBar = EAction.getOptionsToolBar();
var scaleXCombo = optionsToolBar.findChild("ScaleX");
var scaleYCombo = optionsToolBar.findChild("ScaleY");
var scaleYLabel = optionsToolBar.findChild("ScaleYLabel");
var keepProportionsSwitch = optionsToolBar.findChild("KeepProportions");
if (value===true) {
scaleYCombo.setEditText(scaleXCombo.currentText);
scaleYCombo.enabled = false;
scaleYLabel.enabled = false;
keepProportionsSwitch.icon = new QIcon(InsertBlock.includeBasePath + "/KeepProportionsOn.svg");
}
else {
scaleYCombo.enabled = true;
scaleYLabel.enabled = true;
keepProportionsSwitch.icon = new QIcon(InsertBlock.includeBasePath + "/KeepProportionsOff.svg");
}
}
InsertBlock.prototype.slotScaleXChanged = function(value) {
if (!isNumber(value)) {
this.blockReferenceData.setScaleFactors(new RVector(1, 1));
return;
}
var optionsToolBar = EAction.getOptionsToolBar();
var keepProportionsSwitch = optionsToolBar.findChild("KeepProportions");
var keepProportions = keepProportionsSwitch.checked;
var scaleX = value;
var scaleY = this.blockReferenceData.getScaleFactors().y;
// if keep proportions is checked, adjust y scale proportionally to x scale:
if (keepProportions) {
var previousScaleX = this.blockReferenceData.getScaleFactors().x;
var factor = scaleX / previousScaleX;
scaleY = scaleY * factor;
var scaleYCombo = optionsToolBar.findChild("ScaleY");
scaleYCombo.setEditText("%1".arg(scaleY));
}
this.blockReferenceData.setScaleFactors(new RVector(scaleX, scaleY));
};
InsertBlock.prototype.slotScaleYChanged = function(value) {
if (isNumber(value)) {
this.blockReferenceData.setScaleFactors(new RVector(this.blockReferenceData.getScaleFactors().x, value));
} else {
this.blockReferenceData.setScaleFactors(new RVector(this.blockReferenceData.getScaleFactors().x, 1.0));
}
};
InsertBlock.prototype.slotRotationChanged = function(value) {
if (isNumber(value)) {
this.blockReferenceData.setRotation(value);
} else {
this.blockReferenceData.setRotation(0);
}
this.updatePreview(true);
};
InsertBlock.prototype.slotFlipHorizontal = function() {
this.blockReferenceData.flipHorizontal();
this.updateOptionsToolBar();
};
InsertBlock.prototype.slotFlipVertical = function() {
this.blockReferenceData.flipVertical();
this.updateOptionsToolBar();
};
InsertBlock.prototype.slotSingle = function() {
this.blockReferenceData.setColumnCount(1);
this.blockReferenceData.setRowCount(1);
this.blockReferenceData.setColumnSpacing(0);
this.blockReferenceData.setRowSpacing(0);
this.updatePreview();
};
InsertBlock.prototype.slotArray = function() {
// show array options in dialog:
var dialog = WidgetFactory.createDialog(InsertBlock.includeBasePath, "InsertBlockDialog.ui");
WidgetFactory.restoreState(dialog, this.settingsGroup, this);
dialog.exec();
WidgetFactory.saveState(dialog, this.settingsGroup);
destr(dialog);
EAction.activateMainWindow();
};
InsertBlock.prototype.slotColumnCountChanged = function(value) {
this.blockReferenceData.setColumnCount(parseInt(value));
this.updatePreview(true);
};
InsertBlock.prototype.slotRowCountChanged = function(value) {
this.blockReferenceData.setRowCount(parseInt(value));
this.updatePreview(true);
};
InsertBlock.prototype.slotColumnSpacingChanged = function(value) {
this.blockReferenceData.setColumnSpacing(value);
this.updatePreview(true);
};
InsertBlock.prototype.slotRowSpacingChanged = function(value) {
this.blockReferenceData.setRowSpacing(value);
this.updatePreview(true);
};
InsertBlock.prototype.updateOptionsToolBar = function() {
var optionsToolBar = EAction.getOptionsToolBar();
var rotationCombo = optionsToolBar.findChild("Rotation");
var scaleXCombo = optionsToolBar.findChild("ScaleX");
var scaleYCombo = optionsToolBar.findChild("ScaleY");
rotationCombo.setEditText("%1".arg(RMath.rad2deg(this.blockReferenceData.getRotation())));
scaleXCombo.setEditText("%1".arg(this.blockReferenceData.getScaleFactors().x));
scaleYCombo.setEditText("%1".arg(this.blockReferenceData.getScaleFactors().y));
};
InsertBlock.prototype.slotReset = function() {
BlockInsert.prototype.slotReset.call(this);
var optionsToolBar = EAction.getOptionsToolBar();
var scaleYCombo = optionsToolBar.findChild("ScaleY");
scaleYCombo.setEditText("1");
};