| 1 |
# -*- coding: ascii -*- |
| 2 |
# $Id: CNCEditor.py,v 1.9 2014/10/15 15:04:38 bnv Exp $ |
| 3 |
# |
| 4 |
# Author: [email protected] |
| 5 |
# Date: 24-Aug-2014 |
| 6 |
|
| 7 |
from __future__ import print_function |
| 8 |
from __future__ import absolute_import |
| 9 |
try: |
| 10 |
from cStringIO import StringIO |
| 11 |
except ImportError: |
| 12 |
from io import StringIO |
| 13 |
try: |
| 14 |
import cPickle as pickle |
| 15 |
except ImportError: |
| 16 |
import pickle |
| 17 |
try: |
| 18 |
from Tkinter import * |
| 19 |
import tkFont |
| 20 |
except ImportError: |
| 21 |
from tkinter import * |
| 22 |
import tkinter.font as tkFont |
| 23 |
|
| 24 |
from CNC import Block, CNC |
| 25 |
import tkExtra |
| 26 |
import re |
| 27 |
import json |
| 28 |
#import tkDialogs |
| 29 |
|
| 30 |
BLOCK_COLOR = "LightYellow" |
| 31 |
COMMENT_COLOR = "Blue" |
| 32 |
DISABLE_COLOR = "Gray" |
| 33 |
from CNCCanvas import TAB_COLOR |
| 34 |
|
| 35 |
MAXINT = 1000000000 # python3 doesn't have maxint |
| 36 |
|
| 37 |
|
| 38 |
#============================================================================== |
| 39 |
# CNC Listbox |
| 40 |
#============================================================================== |
| 41 |
class CNCListbox(Listbox): |
| 42 |
def __init__(self, master, app, *kw, **kwargs): |
| 43 |
Listbox.__init__(self, master, *kw, **kwargs) |
| 44 |
self.bind("<Button-1>", self.button1) |
| 45 |
self.bind("<ButtonRelease-1>", self.release1) |
| 46 |
self.bind("<Double-1>", self.double) |
| 47 |
self.bind("<Return>", self.edit) |
| 48 |
self.bind("<KP_Enter>", self.edit) |
| 49 |
self.bind("<Insert>", self.insertItem) |
| 50 |
self.bind("<Control-Key-Return>",self.insertItem) |
| 51 |
self.bind("<Control-Key-space>",self.commandFocus) |
| 52 |
self.bind("<Left>", self.toggleKey) |
| 53 |
self.bind("<Right>", self.toggleKey) |
| 54 |
self.bind("<Control-Key-d>", self.clone) |
| 55 |
self.bind("<Control-Key-Up>", self.orderUp) |
| 56 |
self.bind("<Control-Key-Prior>",self.orderUp) |
| 57 |
self.bind("<Control-Key-Down>", self.orderDown) |
| 58 |
self.bind("<Control-Key-Next>", self.orderDown) |
| 59 |
self.bind("<Control-Key-p>", lambda e : "break") |
| 60 |
self.bind("<Control-Key-n>", lambda e : "break") |
| 61 |
self.bind("<Control-Key-D>", self.dump) |
| 62 |
self.bind("<Delete>", self.deleteBlock) |
| 63 |
self.bind("<BackSpace>", self.deleteBlock) |
| 64 |
try: |
| 65 |
self.bind("<KP_Delete>",self.deleteBlock) |
| 66 |
except: |
| 67 |
pass |
| 68 |
|
| 69 |
self.bind("<Control-Key-b>", self.insertBlock) |
| 70 |
self.bind("<Control-Key-r>", self.fill) |
| 71 |
|
| 72 |
self._blockPos = [] # listbox position of each block |
| 73 |
self._items = [] # each listbox lien which item (bid,lid) shows |
| 74 |
self.app = app |
| 75 |
self.gcode = app.gcode |
| 76 |
self.font = tkFont.nametofont(self.cget("font")) |
| 77 |
self._ystart = 0 |
| 78 |
self._double = False # double clicked handled |
| 79 |
self._hadfocus = False |
| 80 |
self.filter = None |
| 81 |
|
| 82 |
# ---------------------------------------------------------------------- |
| 83 |
def commandFocus(self, event=None): |
| 84 |
self.app.commandFocus(event) |
| 85 |
return "break" |
| 86 |
|
| 87 |
# ---------------------------------------------------------------------- |
| 88 |
# Change the value of a list item |
| 89 |
# and return the value of the old one |
| 90 |
# ---------------------------------------------------------------------- |
| 91 |
def set(self, index, value): |
| 92 |
"""Set/Change the value of a list item""" |
| 93 |
try: |
| 94 |
sel = self.selection_includes(index) |
| 95 |
act = self.index(ACTIVE) |
| 96 |
self.delete(index) |
| 97 |
except TclError: |
| 98 |
return |
| 99 |
self.insert(index, value) |
| 100 |
if sel: self.selection_set(index) |
| 101 |
self.activate(act) |
| 102 |
|
| 103 |
# ---------------------------------------------------------------------- |
| 104 |
# Fill listbox with enable items |
| 105 |
# ---------------------------------------------------------------------- |
| 106 |
def fill(self, event=None): |
| 107 |
ypos = self.yview()[0] |
| 108 |
act = self.index(ACTIVE) |
| 109 |
|
| 110 |
#sel = self.curselection() |
| 111 |
items = self.getSelection() |
| 112 |
self.delete(0,END) |
| 113 |
|
| 114 |
del self._blockPos[:] |
| 115 |
del self._items[:] |
| 116 |
y = 0 |
| 117 |
for bi,block in enumerate(self.gcode.blocks): |
| 118 |
if self.filter is not None: |
| 119 |
if not (self.filter in block.name() or \ |
| 120 |
self.filter=="enable" and block.enable or |
| 121 |
self.filter=="disable" and not block.enable): |
| 122 |
self._blockPos.append(None) |
| 123 |
continue |
| 124 |
|
| 125 |
self._blockPos.append(y) |
| 126 |
self.insert(END, block.header()) |
| 127 |
self._items.append((bi, None)) |
| 128 |
self.itemconfig(END, background=BLOCK_COLOR) |
| 129 |
y += 1 |
| 130 |
if not block.enable: |
| 131 |
self.itemconfig(END, foreground=DISABLE_COLOR) |
| 132 |
if not block.expand: continue |
| 133 |
|
| 134 |
for lj,line in enumerate(block): |
| 135 |
self.insert(END, line) |
| 136 |
y += 1 |
| 137 |
if line and line[0] in ("(","%"): |
| 138 |
self.itemconfig(END, foreground=COMMENT_COLOR) |
| 139 |
self._items.append((bi, lj)) |
| 140 |
|
| 141 |
self.select(items) |
| 142 |
#for i in sel: self.selection_set(i) |
| 143 |
self.yview_moveto(ypos) |
| 144 |
self.activate(act) |
| 145 |
self.see(act) |
| 146 |
|
| 147 |
# ---------------------------------------------------------------------- |
| 148 |
# Copy selected items to clipboard |
| 149 |
# ---------------------------------------------------------------------- |
| 150 |
def copy(self, event=None): |
| 151 |
jsonobj=[] |
| 152 |
for block,line in self.getCleanSelection(): |
| 153 |
if line is None: |
| 154 |
jsonobj.append(self.gcode.blocks[block].dump()) |
| 155 |
else: |
| 156 |
jsonobj.append(self.gcode.blocks[block][line]) |
| 157 |
self.clipboard_clear() |
| 158 |
jsonstring=json.dumps(jsonobj) |
| 159 |
self.clipboard_append(jsonstring) |
| 160 |
return "break" |
| 161 |
# ---------------------------------------------------------------------- |
| 162 |
def cut(self, event=None): |
| 163 |
self.copy() |
| 164 |
self.deleteBlock() |
| 165 |
return "break" |
| 166 |
|
| 167 |
# ---------------------------------------------------------------------- |
| 168 |
def paste(self, event=None): |
| 169 |
try: clipboard = self.selection_get(selection='CLIPBOARD') |
| 170 |
except: return |
| 171 |
|
| 172 |
ypos = self.yview()[0] |
| 173 |
# paste them after the last selected item |
| 174 |
# bid,lid push them to self so it can be accessed from addLines() |
| 175 |
# python3 might fix this with the inner scope |
| 176 |
try: |
| 177 |
self._bid, self._lid = self._items[self.curselection()[-1]] |
| 178 |
except: |
| 179 |
try: |
| 180 |
self._bid, self._lid = self._items[-1] |
| 181 |
except: |
| 182 |
self._bid = 0 |
| 183 |
self._lid = None |
| 184 |
|
| 185 |
selitems = [] |
| 186 |
undoinfo = [] |
| 187 |
|
| 188 |
def addLines(lines): |
| 189 |
for line in lines.splitlines(): |
| 190 |
# Create a new block |
| 191 |
if self._lid is None: |
| 192 |
self._bid += 1 |
| 193 |
if self._bid > len(self.gcode.blocks): |
| 194 |
self._bid = len(self.gcode.blocks) |
| 195 |
self._lid = MAXINT |
| 196 |
block = Block() |
| 197 |
undoinfo.append(self.gcode.addBlockUndo(self._bid,block)) |
| 198 |
selitems.append((self._bid, None)) |
| 199 |
else: |
| 200 |
block = self.gcode.blocks[self._bid] |
| 201 |
|
| 202 |
if self._lid == MAXINT: |
| 203 |
self._lid = len(block) |
| 204 |
selitems.append((self._bid, len(block))) |
| 205 |
else: |
| 206 |
self._lid += 1 |
| 207 |
selitems.append((self._bid, self._lid)) |
| 208 |
undoinfo.append(self.gcode.insLineUndo(self._bid, self._lid, line)) |
| 209 |
try : |
| 210 |
objs = json.loads(clipboard) |
| 211 |
except Exception as e: |
| 212 |
objs = [clipboard] |
| 213 |
for obj in objs: |
| 214 |
if isinstance(obj,list): |
| 215 |
obj =tuple(obj) |
| 216 |
if isinstance(obj,tuple): |
| 217 |
block = Block.load(obj) |
| 218 |
self._bid += 1 |
| 219 |
undoinfo.append(self.gcode.addBlockUndo(self._bid, block)) |
| 220 |
selitems.append((self._bid,None)) |
| 221 |
self._lid = None |
| 222 |
else: |
| 223 |
addLines(obj) |
| 224 |
if not undoinfo: return |
| 225 |
|
| 226 |
self.gcode.addUndo(undoinfo) |
| 227 |
|
| 228 |
self.selection_clear(0,END) |
| 229 |
self.fill() |
| 230 |
self.yview_moveto(ypos) |
| 231 |
self.select(selitems, clear=True) |
| 232 |
|
| 233 |
#self.selection_set(ACTIVE) |
| 234 |
#self.see(ACTIVE) |
| 235 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 236 |
|
| 237 |
# ---------------------------------------------------------------------- |
| 238 |
# Clone selected blocks |
| 239 |
# ---------------------------------------------------------------------- |
| 240 |
def clone(self, event=None): |
| 241 |
sel = list(map(int,self.curselection())) |
| 242 |
if not sel: return |
| 243 |
|
| 244 |
ypos = self.yview()[0] |
| 245 |
undoinfo = [] |
| 246 |
self.selection_clear(0,END) |
| 247 |
pos = self._items[sel[-1]][0]+1 |
| 248 |
blocks = [] |
| 249 |
for i in reversed(sel): |
| 250 |
bid, lid = self._items[i] |
| 251 |
if lid is None: |
| 252 |
undoinfo.append(self.gcode.cloneBlockUndo(bid, pos)) |
| 253 |
for i in range(len(blocks)): blocks[i] += 1 |
| 254 |
blocks.append(pos) |
| 255 |
else: |
| 256 |
undoinfo.append(self.gcode.cloneLineUndo(bid, lid)) |
| 257 |
self.gcode.addUndo(undoinfo) |
| 258 |
|
| 259 |
self.fill() |
| 260 |
self.yview_moveto(ypos) |
| 261 |
if blocks: |
| 262 |
self.selectBlocks(blocks) |
| 263 |
self.activate(self._blockPos[blocks[-1]]) |
| 264 |
else: |
| 265 |
self.selection_set(ACTIVE) |
| 266 |
self.see(ACTIVE) |
| 267 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 268 |
return "break" |
| 269 |
|
| 270 |
# ---------------------------------------------------------------------- |
| 271 |
# Delete selected blocks of code |
| 272 |
# ---------------------------------------------------------------------- |
| 273 |
def deleteBlock(self, event=None): |
| 274 |
sel = list(map(int,self.curselection())) |
| 275 |
if not sel: return |
| 276 |
|
| 277 |
ypos = self.yview()[0] |
| 278 |
undoinfo = [] |
| 279 |
for i in reversed(sel): |
| 280 |
bid, lid = self._items[i] |
| 281 |
if isinstance(lid,int): |
| 282 |
undoinfo.append(self.gcode.delLineUndo(bid, lid)) |
| 283 |
else: |
| 284 |
undoinfo.append(self.gcode.delBlockUndo(bid)) |
| 285 |
self.gcode.addUndo(undoinfo) |
| 286 |
|
| 287 |
self.selection_clear(0,END) |
| 288 |
self.fill() |
| 289 |
self.yview_moveto(ypos) |
| 290 |
self.selection_set(ACTIVE) |
| 291 |
self.see(ACTIVE) |
| 292 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 293 |
|
| 294 |
# ---------------------------------------------------------------------- |
| 295 |
# Edit active item |
| 296 |
# ---------------------------------------------------------------------- |
| 297 |
def edit(self, event=None): |
| 298 |
active = self.index(ACTIVE) |
| 299 |
txt = self.get(active) |
| 300 |
if event: |
| 301 |
x = event.x |
| 302 |
else: |
| 303 |
x = 0 |
| 304 |
|
| 305 |
ypos = self.yview()[0] |
| 306 |
bid, lid = self._items[active] |
| 307 |
if lid is None: |
| 308 |
txt0 = txt |
| 309 |
txt = self.gcode[bid].name() |
| 310 |
self.set(active, txt) |
| 311 |
edit = tkExtra.InPlaceEdit(self, select=False, bg=self.cget("bg")) |
| 312 |
else: |
| 313 |
edit = tkExtra.InPlaceEdit(self,x=x, select=False, bg=self.cget("bg")) |
| 314 |
|
| 315 |
if edit.value is None or edit.value==txt: |
| 316 |
if lid is None: |
| 317 |
self.set(active,txt0) |
| 318 |
self.itemconfig(active, background=BLOCK_COLOR) |
| 319 |
if not self.gcode[bid].enable: |
| 320 |
self.itemconfig(active, foreground=DISABLE_COLOR) |
| 321 |
return |
| 322 |
|
| 323 |
if isinstance(lid,int): |
| 324 |
self.gcode.addUndo(self.gcode.setLineUndo(bid, lid, edit.value)) |
| 325 |
self.set(active, edit.value) |
| 326 |
if edit.value and edit.value[0] in ("(","%"): |
| 327 |
self.itemconfig(active, foreground=COMMENT_COLOR) |
| 328 |
|
| 329 |
else: |
| 330 |
self.gcode.addUndo(self.gcode.setBlockNameUndo(bid, edit.value)) |
| 331 |
self.set(active, self.gcode[bid].header()) |
| 332 |
self.itemconfig(active, background=BLOCK_COLOR) |
| 333 |
if not self.gcode[bid].enable: |
| 334 |
self.itemconfig(active, foreground=DISABLE_COLOR) |
| 335 |
|
| 336 |
self.yview_moveto(ypos) |
| 337 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 338 |
|
| 339 |
# ---------------------------------------------------------------------- |
| 340 |
# return active block id |
| 341 |
# ---------------------------------------------------------------------- |
| 342 |
def activeBlock(self): |
| 343 |
active = self.index(ACTIVE) |
| 344 |
if self._items: |
| 345 |
bid, lid = self._items[active] |
| 346 |
else: |
| 347 |
bid = 0 |
| 348 |
return bid |
| 349 |
|
| 350 |
# ---------------------------------------------------------------------- |
| 351 |
# Insert a line or a block |
| 352 |
# ---------------------------------------------------------------------- |
| 353 |
def insertItem(self, event=None): |
| 354 |
active = self.index(ACTIVE) |
| 355 |
if active is None: return |
| 356 |
if len(self._items)==0 or self._items[active][1] is None: |
| 357 |
self.insertBlock() |
| 358 |
else: |
| 359 |
self.insertLine() |
| 360 |
|
| 361 |
# ---------------------------------------------------------------------- |
| 362 |
# Insert New Block |
| 363 |
# ---------------------------------------------------------------------- |
| 364 |
def insertBlock(self, event=None): |
| 365 |
active = self.index(ACTIVE) |
| 366 |
if self._items: |
| 367 |
bid, lid = self._items[active] |
| 368 |
bid += 1 |
| 369 |
else: |
| 370 |
bid = 0 |
| 371 |
|
| 372 |
block = Block() |
| 373 |
block.expand = True |
| 374 |
block.append("g0 x0 y0") |
| 375 |
block.append("g1 z0") |
| 376 |
block.append(CNC.zsafe()) |
| 377 |
self.gcode.addUndo(self.gcode.addBlockUndo(bid,block)) |
| 378 |
self.selection_clear(0,END) |
| 379 |
self.fill() |
| 380 |
# find location of new block |
| 381 |
while active < self.size(): |
| 382 |
if self._items[active][0] == bid: |
| 383 |
break |
| 384 |
active += 1 |
| 385 |
self.selection_set(active) |
| 386 |
self.see(active) |
| 387 |
self.activate(active) |
| 388 |
self.edit() |
| 389 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 390 |
|
| 391 |
# ---------------------------------------------------------------------- |
| 392 |
# Insert a new line below cursor |
| 393 |
# ---------------------------------------------------------------------- |
| 394 |
def insertLine(self, event=None): |
| 395 |
active = self.index(ACTIVE) |
| 396 |
if active is None: return |
| 397 |
if len(self._items)==0: |
| 398 |
self.insertBlock() |
| 399 |
return |
| 400 |
|
| 401 |
bid, lid = self._items[active] |
| 402 |
|
| 403 |
active += 1 |
| 404 |
|
| 405 |
self.insert(active,"") |
| 406 |
self.selection_clear(0,END) |
| 407 |
self.activate(active) |
| 408 |
self.selection_set(active) |
| 409 |
self.see(active) |
| 410 |
|
| 411 |
edit = tkExtra.InPlaceEdit(self, bg=self.cget("bg")) |
| 412 |
ypos = self.yview()[0] |
| 413 |
self.delete(active) |
| 414 |
|
| 415 |
if edit.value is None: |
| 416 |
# Cancel and leave |
| 417 |
active -= 1 |
| 418 |
self.activate(active) |
| 419 |
self.selection_set(active) |
| 420 |
self.see(active) |
| 421 |
return |
| 422 |
|
| 423 |
self.insert(active, edit.value) |
| 424 |
self.selection_set(active) |
| 425 |
self.activate(active) |
| 426 |
if edit.value and edit.value[0] in ("(","%"): |
| 427 |
self.itemconfig(active, foreground=COMMENT_COLOR) |
| 428 |
self.yview_moveto(ypos) |
| 429 |
|
| 430 |
# Add line into code |
| 431 |
|
| 432 |
# Correct pointers |
| 433 |
if lid is None: |
| 434 |
lid = 0 |
| 435 |
else: |
| 436 |
lid += 1 |
| 437 |
self.gcode.addUndo(self.gcode.insLineUndo(bid, lid, edit.value)) |
| 438 |
|
| 439 |
self._items.insert(active, (bid, lid)) |
| 440 |
for i in range(active+1,len(self._items)): |
| 441 |
b,l = self._items[i] |
| 442 |
if b != bid: break |
| 443 |
if isinstance(l,int): |
| 444 |
self._items[i] = (b,l+1) |
| 445 |
for i in range(bid+1, len(self._blockPos)): |
| 446 |
if self._blockPos[i] is not None: |
| 447 |
self._blockPos[i] += 1 # shift all blocks below by one |
| 448 |
|
| 449 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 450 |
|
| 451 |
# ---------------------------------------------------------------------- |
| 452 |
def toggleKey(self,event=None): |
| 453 |
if not self._items: return |
| 454 |
active = self.index(ACTIVE) |
| 455 |
bid,lid = self._items[active] |
| 456 |
if lid is None: |
| 457 |
self.toggleExpand() |
| 458 |
else: |
| 459 |
# Go to header |
| 460 |
self.selection_clear(0,END) |
| 461 |
self.activate(self._blockPos[bid]) |
| 462 |
self.selection_set(ACTIVE) |
| 463 |
self.see(ACTIVE) |
| 464 |
self.winfo_toplevel().event_generate("<<ListboxSelect>>") |
| 465 |
|
| 466 |
# ---------------------------------------------------------------------- |
| 467 |
# Button1 clicked |
| 468 |
# ---------------------------------------------------------------------- |
| 469 |
def button1(self, event): |
| 470 |
if self._double: return |
| 471 |
|
| 472 |
# Remember if we had the focus before clicking |
| 473 |
# to be used later in editing |
| 474 |
self._hadfocus = self.focus_get() == self |
| 475 |
|
| 476 |
# from a single click |
| 477 |
self._ystart = self.nearest(event.y) |
| 478 |
selected = self.selection_includes(self._ystart) |
| 479 |
loc = self._headerLocation(event) |
| 480 |
if loc is None: |
| 481 |
pass |
| 482 |
elif self._headerLocation(event)<2 and selected: |
| 483 |
return "break" # do not alter selection! |
| 484 |
|
| 485 |
# ---------------------------------------------------------------------- |
| 486 |
# Release button-1. Warning on separation of double or single click or |
| 487 |
# click and drag |
| 488 |
# ---------------------------------------------------------------------- |
| 489 |
def release1(self, event): |
| 490 |
if not self._items: return |
| 491 |
if self._double: |
| 492 |
self._double = False |
| 493 |
return |
| 494 |
|
| 495 |
self._double = False |
| 496 |
active = self.index(ACTIVE) |
| 497 |
|
| 498 |
# from a single click |
| 499 |
y = self.nearest(event.y) |
| 500 |
self.activate(y) |
| 501 |
if y != self._ystart: return |
| 502 |
|
| 503 |
loc = self._headerLocation(event) |
| 504 |
if loc is None: |
| 505 |
# Normal line |
| 506 |
if active==y: |
| 507 |
# In place edit if we had already the focus |
| 508 |
if self._hadfocus: |
| 509 |
self.edit(event) |
| 510 |
elif loc == 0: |
| 511 |
self.toggleExpand() |
| 512 |
elif loc == 1: |
| 513 |
self.toggleEnable() |
| 514 |
return "break" |
| 515 |
|
| 516 |
# ---------------------------------------------------------------------- |
| 517 |
def double(self, event): |
| 518 |
if self._headerLocation(event) == 2: |
| 519 |
self.edit() |
| 520 |
self._double = True |
| 521 |
else: |
| 522 |
self._double = False |
| 523 |
|
| 524 |
# ---------------------------------------------------------------------- |
| 525 |
# Return location where we clicked on header |
| 526 |
# 0 = expand arrow |
| 527 |
# 1 = enable ballot box |
| 528 |
# 2 = name |
| 529 |
# ---------------------------------------------------------------------- |
| 530 |
def _headerLocation(self, event): |
| 531 |
if not self._items: return None |
| 532 |
# from a single click |
| 533 |
y = self.nearest(event.y) |
| 534 |
|
| 535 |
block,line = self._items[y] |
| 536 |
if line is not None: return None |
| 537 |
|
| 538 |
txt = self.get(y) |
| 539 |
if event.x <= self.font.measure(txt[:2]): |
| 540 |
return 0 |
| 541 |
elif event.x <= self.font.measure(txt[:5]): |
| 542 |
return 1 |
| 543 |
else: |
| 544 |
return 2 |
| 545 |
|
| 546 |
# ---------------------------------------------------------------------- |
| 547 |
# Toggle expand selection |
| 548 |
# ---------------------------------------------------------------------- |
| 549 |
def toggleExpand(self, event=None): |
| 550 |
if not self._items: return None |
| 551 |
items = list(map(int,self.curselection())) |
| 552 |
expand = None |
| 553 |
active = self.index(ACTIVE) |
| 554 |
bactive,lactive = self._items[active] |
| 555 |
blocks = [] |
| 556 |
undoinfo = [] |
| 557 |
for i in reversed(items): |
| 558 |
bid,lid = self._items[i] |
| 559 |
if lid is not None: |
| 560 |
if bid in blocks: continue |
| 561 |
blocks.append(bid) |
| 562 |
if expand is None: expand = not self.gcode[bid].expand |
| 563 |
undoinfo.append(self.gcode.setBlockExpandUndo(bid, expand)) |
| 564 |
|
| 565 |
if undoinfo: |
| 566 |
self.gcode.addUndo(undoinfo) |
| 567 |
self.selection_clear(0,END) |
| 568 |
self.fill() |
| 569 |
active = self._blockPos[bactive] |
| 570 |
for bid in blocks: |
| 571 |
self.selectBlock(bid) |
| 572 |
self.activate(active) |
| 573 |
self.see(active) |
| 574 |
|
| 575 |
self.winfo_toplevel().event_generate("<<Status>>",data="Toggled Expand of selected objects") |
| 576 |
|
| 577 |
# ---------------------------------------------------------------------- |
| 578 |
def _toggleEnable(self, enable=None): |
| 579 |
if not self._items: return None |
| 580 |
items = list(map(int,self.curselection())) |
| 581 |
active = self.index(ACTIVE) |
| 582 |
ypos = self.yview()[0] |
| 583 |
undoinfo = [] |
| 584 |
blocks = [] |
| 585 |
for i in items: |
| 586 |
bid,lid = self._items[i] |
| 587 |
if lid is not None: |
| 588 |
if bid in blocks: continue |
| 589 |
pos = self._blockPos[bid] |
| 590 |
else: |
| 591 |
pos = i |
| 592 |
|
| 593 |
blocks.append(bid) |
| 594 |
block = self.gcode[bid] |
| 595 |
if block.name() in ("Header", "Footer"): continue |
| 596 |
if enable is None: enable = not block.enable |
| 597 |
undoinfo.append(self.gcode.setBlockEnableUndo(bid, enable)) |
| 598 |
|
| 599 |
sel = self.selection_includes(pos) |
| 600 |
self.delete(pos) |
| 601 |
self.insert(pos, block.header()) |
| 602 |
self.itemconfig(pos, background=BLOCK_COLOR) |
| 603 |
if not block.enable: |
| 604 |
self.itemconfig(pos, foreground=DISABLE_COLOR) |
| 605 |
if sel: self.selection_set(pos) |
| 606 |
|
| 607 |
if undoinfo: |
| 608 |
self.gcode.calculateEnableMargins() |
| 609 |
self.gcode.addUndo(undoinfo) |
| 610 |
self.activate(active) |
| 611 |
self.yview_moveto(ypos) |
| 612 |
self.winfo_toplevel().event_generate("<<ListboxSelect>>") |
| 613 |
|
| 614 |
# ---------------------------------------------------------------------- |
| 615 |
def enable(self, event=None): |
| 616 |
self._toggleEnable(True) |
| 617 |
self.winfo_toplevel().event_generate("<<Status>>",data="Enabled selected objects") |
| 618 |
|
| 619 |
# ---------------------------------------------------------------------- |
| 620 |
def disable(self, event=None): |
| 621 |
self._toggleEnable(False) |
| 622 |
self.winfo_toplevel().event_generate("<<Status>>",data="Disabled selected objects") |
| 623 |
|
| 624 |
# ---------------------------------------------------------------------- |
| 625 |
# toggle state enable/disable |
| 626 |
# ---------------------------------------------------------------------- |
| 627 |
def toggleEnable(self, event=None): |
| 628 |
self._toggleEnable() |
| 629 |
self.winfo_toplevel().event_generate("<<Status>>",data="Toggled Visibility of selected objects") |
| 630 |
|
| 631 |
# ---------------------------------------------------------------------- |
| 632 |
# comment uncomment row |
| 633 |
# ---------------------------------------------------------------------- |
| 634 |
def commentRow(self, event=None): |
| 635 |
if not self._items: return |
| 636 |
all_items = self._items |
| 637 |
sel_items = list(map(int,self.curselection())) |
| 638 |
mreg = re.compile("^\((.*)\)$") |
| 639 |
change = False |
| 640 |
for i in sel_items: |
| 641 |
my_item = all_items[i] |
| 642 |
if my_item[1] is not None: |
| 643 |
change = True |
| 644 |
#check for () |
| 645 |
line = self.gcode[my_item[0]][my_item[1]] |
| 646 |
m = mreg.search(line) |
| 647 |
if m is None: |
| 648 |
self.gcode[my_item[0]][my_item[1]] = "("+line+")" |
| 649 |
else: |
| 650 |
self.gcode[my_item[0]][my_item[1]] = m.group(1) |
| 651 |
if change: self.fill() |
| 652 |
|
| 653 |
# ---------------------------------------------------------------------- |
| 654 |
# splitBlocks |
| 655 |
# ---------------------------------------------------------------------- |
| 656 |
def joinBlocks(self, event=None): |
| 657 |
if not self._items: return |
| 658 |
all_items = self._items |
| 659 |
sel_items = list(map(int,self.curselection())) |
| 660 |
change = True |
| 661 |
bl = Block(self.gcode[sel_items[0]].name()) |
| 662 |
for bid in sel_items: |
| 663 |
for line in self.gcode[bid]: |
| 664 |
bl.append(line) |
| 665 |
bl.append("( ---------- cut-here ---------- )") |
| 666 |
del bl[-1] |
| 667 |
self.gcode.addUndo(self.gcode.addBlockUndo(bid+1,bl)) |
| 668 |
if change: self.fill() |
| 669 |
self.deleteBlock() |
| 670 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 671 |
|
| 672 |
# ---------------------------------------------------------------------- |
| 673 |
# splitBlocks |
| 674 |
# ---------------------------------------------------------------------- |
| 675 |
def splitBlocks(self, event=None): |
| 676 |
if not self._items: return |
| 677 |
all_items = self._items |
| 678 |
sel_items = list(map(int,self.curselection())) |
| 679 |
change = True |
| 680 |
newblocks = [] |
| 681 |
for bid in sel_items: |
| 682 |
bl = Block(self.gcode[bid].name()) |
| 683 |
for line in self.gcode[bid]: |
| 684 |
if line == "( ---------- cut-here ---------- )": |
| 685 |
#newblocks.append(bl) |
| 686 |
#self.insertBlock(bl) |
| 687 |
self.gcode.addUndo(self.gcode.addBlockUndo(bid+1,bl)) |
| 688 |
bl = Block(self.gcode[bid].name()) |
| 689 |
else: |
| 690 |
bl.append(line) |
| 691 |
self.gcode.addUndo(self.gcode.addBlockUndo(bid+1,bl)) |
| 692 |
#newblocks.append(bl) |
| 693 |
#self.gcode.extend(newblocks) |
| 694 |
if change: self.fill() |
| 695 |
self.deleteBlock() |
| 696 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 697 |
|
| 698 |
# ---------------------------------------------------------------------- |
| 699 |
# change color of a block |
| 700 |
# ---------------------------------------------------------------------- |
| 701 |
def changeColor(self, event=None): |
| 702 |
items = list(map(int,self.curselection())) |
| 703 |
if not items: |
| 704 |
self.winfo_toplevel().event_generate("<<Status>>",data="Nothing is selected") |
| 705 |
return |
| 706 |
|
| 707 |
# Find initial color |
| 708 |
bid,lid = self._items[items[0]] |
| 709 |
|
| 710 |
try: |
| 711 |
rgb, color = tkExtra.askcolor( |
| 712 |
title=_("Color"), |
| 713 |
initialcolor=self.gcode[bid].color, |
| 714 |
parent=self) |
| 715 |
except TclError: |
| 716 |
color = None |
| 717 |
if color is None: return |
| 718 |
|
| 719 |
blocks = [] |
| 720 |
undoinfo = [] |
| 721 |
for i in reversed(items): |
| 722 |
bid,lid = self._items[i] |
| 723 |
if lid is not None: |
| 724 |
if bid in blocks: continue |
| 725 |
blocks.append(bid) |
| 726 |
oldColor = self.gcode[bid].color |
| 727 |
undoinfo.append(self.gcode.setBlockColorUndo(bid, oldColor)) |
| 728 |
|
| 729 |
if undoinfo: |
| 730 |
self.gcode.addUndo(undoinfo) |
| 731 |
for bid in blocks: |
| 732 |
self.gcode[bid].color = color |
| 733 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 734 |
self.winfo_toplevel().event_generate("<<Status>>",data="Changed color of block") |
| 735 |
|
| 736 |
# ---------------------------------------------------------------------- |
| 737 |
# Select items in the form of (block, item) |
| 738 |
# ---------------------------------------------------------------------- |
| 739 |
def select(self, items, double=False, clear=False, toggle=True): |
| 740 |
if clear: |
| 741 |
self.selection_clear(0,END) |
| 742 |
toggle = False |
| 743 |
first = None |
| 744 |
|
| 745 |
for bi in items: |
| 746 |
bid,lid = bi |
| 747 |
try: |
| 748 |
block = self.gcode[bid] |
| 749 |
except: |
| 750 |
continue |
| 751 |
|
| 752 |
if double: |
| 753 |
if block.expand: |
| 754 |
# select whole block |
| 755 |
y = self._blockPos[bid] |
| 756 |
else: |
| 757 |
# select all blocks with the same name |
| 758 |
name = block.nameNop() |
| 759 |
for i,bl in enumerate(self.gcode.blocks): |
| 760 |
if name == bl.nameNop(): |
| 761 |
self.selection_set(self._blockPos[i]) |
| 762 |
continue |
| 763 |
|
| 764 |
elif not block.expand or lid is None: |
| 765 |
# select whole block |
| 766 |
y = self._blockPos[bid] |
| 767 |
|
| 768 |
elif isinstance(lid,int): |
| 769 |
# find line of block |
| 770 |
y = self._blockPos[bid]+1 + lid |
| 771 |
|
| 772 |
else: |
| 773 |
raise |
| 774 |
#continue |
| 775 |
|
| 776 |
if y is None: continue |
| 777 |
|
| 778 |
if toggle: |
| 779 |
select = not self.selection_includes(y) |
| 780 |
else: |
| 781 |
select = True |
| 782 |
|
| 783 |
if select: |
| 784 |
self.selection_set(y) |
| 785 |
if first is None: first = y |
| 786 |
elif toggle: |
| 787 |
self.selection_clear(y) |
| 788 |
|
| 789 |
if first is not None: |
| 790 |
self.activate(first) |
| 791 |
self.see(first) |
| 792 |
|
| 793 |
# ---------------------------------------------------------------------- |
| 794 |
# Select whole block lines if expanded |
| 795 |
# ---------------------------------------------------------------------- |
| 796 |
def selectBlock(self, bid): |
| 797 |
start = self._blockPos[bid] |
| 798 |
while True: |
| 799 |
bid += 1 |
| 800 |
if bid >= len(self._blockPos): |
| 801 |
end = END |
| 802 |
break |
| 803 |
elif self._blockPos[bid] is not None: |
| 804 |
end = self._blockPos[bid]-1 |
| 805 |
break |
| 806 |
self.selection_set(start,end) |
| 807 |
|
| 808 |
# ---------------------------------------------------------------------- |
| 809 |
def selectBlocks(self, blocks): |
| 810 |
self.selection_clear(0,END) |
| 811 |
for bid in blocks: |
| 812 |
self.selectBlock(bid) |
| 813 |
|
| 814 |
# ---------------------------------------------------------------------- |
| 815 |
def selectAll(self): |
| 816 |
self.selection_set(0,END) |
| 817 |
|
| 818 |
# ---------------------------------------------------------------------- |
| 819 |
def selectClear(self): |
| 820 |
self.selection_clear(0,END) |
| 821 |
|
| 822 |
# ---------------------------------------------------------------------- |
| 823 |
def selectInvert(self): |
| 824 |
for i in range(self.size()): |
| 825 |
if self.selection_includes(i): |
| 826 |
self.selection_clear(i) |
| 827 |
else: |
| 828 |
self.selection_set(i) |
| 829 |
|
| 830 |
# ---------------------------------------------------------------------- |
| 831 |
# Select all blocks with the same name of the selected layer |
| 832 |
# ---------------------------------------------------------------------- |
| 833 |
def selectLayer(self): |
| 834 |
for bid in self.getSelectedBlocks(): |
| 835 |
name = self.gcode[bid].nameNop() |
| 836 |
for i,bl in enumerate(self.gcode.blocks): |
| 837 |
if name == bl.nameNop(): |
| 838 |
self.selection_set(self._blockPos[i]) |
| 839 |
|
| 840 |
# ---------------------------------------------------------------------- |
| 841 |
# Return list of [(blocks,lines),...] currently being selected |
| 842 |
# ---------------------------------------------------------------------- |
| 843 |
def getSelection(self): |
| 844 |
return [self._items[int(i)] for i in self.curselection()] |
| 845 |
|
| 846 |
# ---------------------------------------------------------------------- |
| 847 |
# Return all blocks that at least an item is selected |
| 848 |
# ---------------------------------------------------------------------- |
| 849 |
def getSelectedBlocks(self): |
| 850 |
blocks = {} |
| 851 |
for i in self.curselection(): |
| 852 |
block,line = self._items[int(i)] |
| 853 |
blocks[block] = True |
| 854 |
return list(sorted(blocks.keys())) |
| 855 |
|
| 856 |
# ---------------------------------------------------------------------- |
| 857 |
# Return list of [(blocks,lines),...] currently being selected |
| 858 |
# Filtering all items that the block is also selected |
| 859 |
# ---------------------------------------------------------------------- |
| 860 |
def getCleanSelection(self): |
| 861 |
items = [self._items[int(i)] for i in self.curselection()] |
| 862 |
if not items: return items |
| 863 |
blocks = {} |
| 864 |
i = 0 |
| 865 |
while i<len(items): |
| 866 |
bid,lid = items[i] |
| 867 |
if lid is None: |
| 868 |
blocks[bid] = True |
| 869 |
i += 1 |
| 870 |
elif blocks.get(bid,False): |
| 871 |
del items[i] |
| 872 |
else: |
| 873 |
i += 1 |
| 874 |
return items |
| 875 |
|
| 876 |
# ---------------------------------------------------------------------- |
| 877 |
def getActive(self): |
| 878 |
active = self.index(ACTIVE) |
| 879 |
if active is None: return None |
| 880 |
if not self.selection_includes(active): |
| 881 |
try: |
| 882 |
active = self.curselection()[0] |
| 883 |
except: |
| 884 |
return (0,None) |
| 885 |
return self._items[int(active)] |
| 886 |
|
| 887 |
# ---------------------------------------------------------------------- |
| 888 |
# Move selected items upwards |
| 889 |
# ---------------------------------------------------------------------- |
| 890 |
def orderUp(self, event=None): |
| 891 |
items = self.getCleanSelection() |
| 892 |
if not items: return |
| 893 |
sel = self.gcode.orderUp(items) |
| 894 |
self.fill() |
| 895 |
self.select(sel,clear=True,toggle=False) |
| 896 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 897 |
return "break" |
| 898 |
|
| 899 |
# ---------------------------------------------------------------------- |
| 900 |
# Move selected items downwards |
| 901 |
# ---------------------------------------------------------------------- |
| 902 |
def orderDown(self, event=None): |
| 903 |
items = self.getCleanSelection() |
| 904 |
if not items: return |
| 905 |
sel = self.gcode.orderDown(items) |
| 906 |
self.fill() |
| 907 |
self.select(sel,clear=True,toggle=False) |
| 908 |
self.winfo_toplevel().event_generate("<<Modified>>") |
| 909 |
return "break" |
| 910 |
|
| 911 |
# ---------------------------------------------------------------------- |
| 912 |
# Invert selected blocks |
| 913 |
# ---------------------------------------------------------------------- |
| 914 |
def invertBlocks(self, event=None): |
| 915 |
blocks = self.getSelectedBlocks() |
| 916 |
if not blocks: return |
| 917 |
self.gcode.addUndo(self.gcode.invertBlocksUndo(blocks)) |
| 918 |
self.fill() |
| 919 |
# do not send a modified message, no need to redraw |
| 920 |
return "break" |
| 921 |
|
| 922 |
# ---------------------------------------------------------------------- |
| 923 |
# Dump list and code, check for mismatch |
| 924 |
# ---------------------------------------------------------------------- |
| 925 |
def dump(self, event=None): |
| 926 |
if not CNC.developer: return |
| 927 |
print("*** LIST ***") |
| 928 |
for i,sel in enumerate(self.get(0,END)): |
| 929 |
print(i,sel.encode("ascii","replace")) #TODO: do we need .encode()??? |
| 930 |
|
| 931 |
print("\n*** ITEMS ***") |
| 932 |
for i,item in enumerate(self._items): |
| 933 |
print(i,item) |
| 934 |
|
| 935 |
print("\n*** CODE ***") |
| 936 |
for i, block in enumerate(self.gcode.blocks): |
| 937 |
print("Block:",i,block.name()) |
| 938 |
for j,line in enumerate(block): |
| 939 |
print(" %3d %s"%(j,line)) |
| 940 |
|
| 941 |
print("\nBLOCKPOS=",self._blockPos) |