/
Block.js
220 lines (187 loc) · 6.46 KB
/
Block.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
/**
* 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.
*/
/**
* \defgroup ecma_block Block Tools
* \ingroup ecma_scripts
*
* \brief This module contains ECMAScript implementations of
* block related tools.
*/
include("scripts/EAction.js");
include("scripts/Widgets/BlockList/BlockList.js");
/**
* \class Block
* \ingroup ecma_block
* \brief Base class for block tools.
*/
function Block(guiAction) {
EAction.call(this, guiAction);
}
Block.prototype = new EAction();
Block.includeBasePath = includeBasePath;
Block.prototype.beginEvent = function() {
EAction.prototype.beginEvent.call(this);
if (!isNull(this.getGuiAction()) && this.getGuiAction().objectName==="BlockToolsPanelAction") {
EAction.showCadToolBarPanel("BlockToolsPanel");
this.terminate();
}
};
Block.getMenu = function() {
var menu = EAction.getMenu(Block.getTitle(), "BlockMenu");
menu.setProperty("scriptFile", Block.includeBasePath + "/Block.js");
return menu;
};
Block.getToolBar = function() {
var tb = EAction.getToolBar(Block.getTitle(), "BlockToolBar");
tb.visible = false;
return tb;
};
Block.getCadToolBarPanel = function() {
var mtb = EAction.getMainCadToolBarPanel();
var actionName = "BlockToolsPanelAction";
if (!isNull(mtb) && mtb.findChild(actionName)==undefined) {
var action = new RGuiAction(qsTr("Block Tools"), mtb);
action.setScriptFile(Block.includeBasePath + "/Block.js");
action.objectName = actionName;
action.setRequiresDocument(true);
//action.setStatusTip(qsTr("Show block tools"));
action.setDefaultShortcut(new QKeySequence("w,b"));
action.setNoState();
action.setDefaultCommands(["blockmenu"]);
action.setGroupSortOrder(60);
action.setSortOrder(100);
action.setWidgetNames(["!MainToolsPanel"]);
}
var tb = EAction.getCadToolBarPanel(
Block.getTitle(),
"BlockToolsPanel",
true
);
return tb;
};
Block.getToolMatrixPanel = function() {
RSettings.setValue("Block/VisibleInMatrixPanel", false, false);
return EAction.getToolMatrixPanel(Block.getTitle(), "BlockMatrixPanel", 8000);
};
Block.getTitle = function() {
return qsTr("&Block");
};
Block.prototype.getTitle = function() {
return Block.getTitle();
};
Block.init = function() {
Block.getMenu();
Block.getToolBar();
Block.getCadToolBarPanel();
Block.getToolMatrixPanel();
};
/**
* \return The ID of the block that is currently highlighted (selected) in
* the block list. This is usually not that block that is currently being edited.
*/
Block.getActiveBlockId = function() {
var blockList = EAction.getMainWindow().findChild("BlockList");
if (isNull(blockList)) {
return RObject.INVALID_ID;
}
var list = blockList.selectedItems();
if (list.length === 0) {
return RObject.INVALID_ID;
}
var item = list[0];
if (isNull(item)) {
return RObject.INVALID_ID;
}
var document = EAction.getDocument();
if (isNull(document)) {
return RObject.INVALID_ID;
}
var blockId = document.getBlockId(item.data(BlockList.colName, Qt.UserRole));
return blockId;
};
Block.showHide = function(show, action, blockId) {
var op = new RModifyObjectsOperation();
op.setText(action.getToolTitle());
var blocks = action.getDocument().queryAllBlocks();
for (var b = 0; b < blocks.length; ++b) {
var block = action.getDocument().queryBlock(blocks[b]);
if (blocks[b] !== blockId) {
block.setFrozen(!show);
} else {
block.setFrozen(false);
}
op.addObject(block);
}
var di = action.getDocumentInterface();
di.applyOperation(op);
di.clearPreview();
di.repaintViews();
};
/**
* Makes the given block the one that is being edited. This method
* stores / restores the zoom and zoom offset per block.
*/
Block.prototype.editBlock = function(blockName) {
var di = this.getDocumentInterface();
//var doc = this.getDocument();
//var views = this.getGraphicsViews();
Block.editBlock(di, blockName);
};
Block.editBlock = function(di, blockName) {
var doc = di.getDocument();
var views = EAction.getGraphicsViews(di);
var i, view;
if (doc.isEditingWorkingSet()) {
EAction.handleUserWarning(qsTr("Cannot edit block while editing a block in-place"));
return;
}
// store offset and zoom factor of all views in a map blockId -> [factor, offset vector]:
var blockId = doc.getCurrentBlockId();
for (i=0; i<views.length; i++) {
view = views[i];
var viewWidget = view.getWidget();
if (isNull(viewWidget) || !isFunction(viewWidget.property)) {
continue;
}
viewWidget.setProperty("BlockZoomFactor_" + blockId, view.getFactor());
viewWidget.setProperty("BlockZoomOffset_" + blockId, view.getOffset());
//print("stored zoom for block: ", blockId, ", view: ", i, ", factor: ", view.getFactor());
}
// change current block that is being edited:
di.setCurrentBlock(blockName);
// restore offset and zoom factor for all views:
blockId = doc.getCurrentBlockId();
for (i=0; i<views.length; i++) {
view = views[i];
var viewWidget = view.getWidget();
if (isNull(viewWidget) || !isFunction(viewWidget.property)) {
continue;
}
var blockZoomFactor = viewWidget.property("BlockZoomFactor_" + blockId);
var blockZoomOffset = viewWidget.property("BlockZoomOffset_" + blockId);
if (isNull(blockZoomFactor) || isNull(blockZoomOffset)) {
view.autoZoom(-1, true);
}
else {
view.setFactor(blockZoomFactor);
view.setOffset(blockZoomOffset);
//print("restored zoom for block: ", blockId, ", view: ", i, ", factor: ", view.getFactor());
}
}
};