Top > OOoBasic > Dialog > Example27

* ツリーでドラッグ先を描写 [#vb5e707f]
ツリーコントロールでドラッグしてノードを移動させるときに、移動先の場所に位置が描かれると分かりやすくなります。

次のような感じにします。実際には移動操作やポインタの変更なども必要です。

#code(python){{
# Python
import unohelper

from com.sun.star.awt import XMouseMotionListener
class MouseMotionListener(unohelper.Base, XMouseMotionListener):
	def __init__(self, act):
		self.act = act
		self.node = node
	def disposing(self, ev):
		self.act = None
		self.node = None
	def mouseDragged(self, ev):
		try:
			self.act.dragged(ev)
			# check near is a child of the same parent
			tree = ev.Source
			node = tree.getSelection()
			near = tree.getClosestNodeForLocation(ev.X, ev.Y)
			if self.node != near:
				r = tree.getNodeRect(near)
				self.act.dragged(tree, r)
				self.node = near
		except Exception, e:
			print(e)
	def mouseMoved(self, ev):
		pass


class TreeTest(object):
	
	DIALOG = "vnd.sun.star.script:Standard.Dialog16?location=application"
	
	def __init__(self, ctx):
		self.ctx = ctx
	
	def create(self, name, args=None):
		if args:
			return self.ctx.getServiceManager().createInstanceWithArgumentsAndContext(
			name, args, self.ctx)
		else:
			return self.ctx.getServiceManager().createInstanceWithContext(
			name, self.ctx)
	
	def execute(self):
		dlg = self.create("com.sun.star.awt.DialogProvider").\
			createDialog(self.DIALOG)
		dc = dlg.getControl
		tree = dc("tree")
		tree.addMouseMotionListener(MouseMotionListener(self))
		data_model = self.create("com.sun.star.awt.tree.MutableTreeDataModel")
		tree.getModel().DataModel = data_model
		tree.getModel().SelectionType = 1
		root = data_model.createNode("Root", False)
		data_model.setRoot(root)
		
		for i in range(6):
			root.appendChild(
				data_model.createNode("Node %d" % (i + 1), False))
		
		#self.create("mytools.Mri", (dlg,))
		dlg.execute()
		dlg.dispose()
	
	def dragged(self, ev):
		tree = ev.Source
		node = tree.getSelection()
		near = tree.getClosestNodeForLocation(ev.X, ev.Y)
		# check near is a child of the same parent
		
		r = tree.getNodeRect(near)
	def dragged(self, tree, r):
		x = r.X
		y = r.Y
		if ev.X < (r.Y + r.Height/2):
			y += r.Height
		
		g = tree.getGraphics()
		if not g:
			g = tree.getPeer().createGraphics()
			tree.setGraphics(g)
		
		tree.getPeer().invalidate(8)
		g.push()
		g.setLineColor(0)
		g.drawLine(x - 10, y, x + 15, y)
		g.pop()


def show_tree(*args):
	try:
		ctx = XSCRIPTCONTEXT.getComponentContext()
		TreeTest(ctx).execute()
	except Exception, e:
		print(e)
		import traceback
		traceback.print_exc()
}}
描写する前に表示を更新させて以前の描写を消します。setLineColor メソッドで色を変更するまえに以前の設定を push メソッドで保存してください。必要な描写終了後に pop メソッドで設定を復元します。これをしなければツリーのノードの描写時に枠線が書かれてしまいます。

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup   Help   RSS of recent changes