~ubuntu-branches/ubuntu/utopic/dogtail/utopic

« back to all changes in this revision

Viewing changes to examples/nautilus-test-icon-view-collage.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-12-21 13:33:47 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20061221133347-xo9jg11afp5plcka
Tags: upstream-0.6.1
ImportĀ upstreamĀ versionĀ 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# Dogtail demo script
3
 
__author__ = 'David Malcolm <dmalcolm@redhat.com>'
4
 
 
5
 
 
6
 
# Nautilus test
7
 
# Takes an image file, uses Imagemagick to chop it into tiles,
8
 
# then arranges the Nautilus thumbnail icons into a collage
9
 
# FIXME: somewhat unstable, requires a 640x480 image called victim.png
10
 
 
11
 
from dogtail.apps.wrappers.nautilus import *
12
 
from math import sin, cos, pi
13
 
 
14
 
import os
15
 
 
16
 
import dogtail.config
17
 
#dogtail.config.config.debugSearching=True
18
 
#dogtail.config.config.debugSleep = True
19
 
 
20
 
n = NautilusApp()
21
 
 
22
 
class Jigsaw:
23
 
  def __init__(self, sourceImage):
24
 
    self.sourceImage = os.path.abspath(sourceImage)
25
 
    self.basePath = os.curdir
26
 
    self.piecesPath = os.path.abspath(os.path.join(self.basePath, "JigsawPieces"))
27
 
    self.resultPath = os.path.abspath(os.path.join(self.basePath, "JigsawResult"))
28
 
    self.pieceArg = os.path.join(self.piecesPath, "piece.png")
29
 
 
30
 
    self.baseWidth = 640
31
 
    self.baseHeight = 480
32
 
    self.divisor = 5
33
 
    self.pieceWidth = self.baseWidth/self.divisor
34
 
    self.pieceHeight = self.baseHeight/self.divisor
35
 
    self.iconWidth = 70.0 # self.baseWidth/self.divisor
36
 
    self.iconHeight = 54.0 # self.baseHeight/self.divisor
37
 
 
38
 
  def makeCropCommand(self):
39
 
    return 'convert -crop %sx%s %s %s'%(self.pieceWidth, self.pieceHeight, self.sourceImage, self.pieceArg)
40
 
 
41
 
  def targetLocation(self, baseXY, index):
42
 
    (baseX, baseY) = baseXY
43
 
    (pieceX,pieceY) = ((float(index)%self.divisor)*self.iconWidth, (float(index)/self.divisor)*self.iconHeight)
44
 
    return (baseX+pieceX,baseY+pieceY)
45
 
 
46
 
  def makeDirectories(self):
47
 
    try: os.mkdir(self.piecesPath)
48
 
    except OSError: pass
49
 
    
50
 
    try: os.mkdir(self.resultPath)
51
 
    except OSError: pass
52
 
 
53
 
class Drag:
54
 
  def __init__(self, startXY, endXY):
55
 
    self.startXY = startXY
56
 
    self.endXY = endXY
57
 
 
58
 
  def doDrag(self, duration, steps):
59
 
    ev = atspi.EventGenerator()
60
 
 
61
 
    (x,y) = self.calcCoord(0.0)
62
 
    ev.absoluteMotion(x, y)
63
 
    doDelay(0.1)
64
 
    ev.press (x,y,1)
65
 
    
66
 
    for step in range(steps):
67
 
      frac = float(step)/float(steps)
68
 
      (x,y) = self.calcCoord(frac)
69
 
      #doDelay(duration/steps)
70
 
      ev.absoluteMotion(x, y)
71
 
 
72
 
    (x,y) = self.calcCoord(1.0)
73
 
    ev.absoluteMotion(x,y)
74
 
    doDelay(0.5)
75
 
    ev.release (x,y,1)
76
 
 
77
 
  def calcCoord(self, frac):
78
 
    raise NotImplementedError
79
 
    
80
 
class LinearDrag(Drag):
81
 
  def calcCoord(self, frac):
82
 
    (startX, startY) = self.startXY
83
 
    (endX, endY) = self.endXY
84
 
    #(deltaX, deltaY) = (endX-startX, endY-startY)
85
 
 
86
 
    return ( (startX*(1.0-frac)) + (endX*frac), (startY*(1.0-frac)) + (endY*frac))
87
 
 
88
 
jigsaw = Jigsaw('victim.png')
89
 
 
90
 
jigsaw.makeDirectories()
91
 
 
92
 
command = jigsaw.makeCropCommand()
93
 
print command
94
 
os.system(command)
95
 
#raise "foo"
96
 
#os.exit()
97
 
 
98
 
def prepareNautilusWindow(w):
99
 
  viewMenu = w.menu("View")
100
 
 
101
 
  # Ensure it's in icon view mode:
102
 
  viewMenu.menuItem("View as Icons").click()
103
 
 
104
 
  viewMenu.menuItem("Normal Size").click()
105
 
  viewMenu.menuItem("Zoom In").click()
106
 
 
107
 
  # Ensure we're in manual layout mode:
108
 
  viewMenu.menu("Arrange Items").menuItem("Manually").click()
109
 
 
110
 
  viewMenu.menuItem("Clean Up by Name").click()
111
 
 
112
 
 
113
 
 
114
 
piecesWindow = n.openPath(jigsaw.piecesPath)
115
 
resultWindow = n.openPath(jigsaw.resultPath)
116
 
 
117
 
prepareNautilusWindow (piecesWindow)
118
 
prepareNautilusWindow (resultWindow)
119
 
 
120
 
#FIXME: now need to get both on top, with sane sizes
121
 
 
122
 
# Find the icons:
123
 
iv = piecesWindow.iconView()
124
 
icons = iv.findChildren(IsAnIcon(), recursive=True)
125
 
 
126
 
matcher = re.compile('piece-(.*).png')
127
 
 
128
 
for icon in icons:
129
 
  #print icon
130
 
  index = int(matcher.match(icon.name).group(1))
131
 
  (x,y,w,h) = icon.extents
132
 
 
133
 
  (dstX, dstY, dstW, dstH) = resultWindow.iconView().extents
134
 
  
135
 
  (targetX, targetY) = jigsaw.targetLocation((dstX+100, dstY+100), index)
136
 
  
137
 
  # FIXME: different drag routes!
138
 
  drag = LinearDrag((x+w/2,y+h/2), (targetX+w/2,targetY+h/2))
139
 
  drag.doDrag(2.0, 50)
140
 
 
141
 
# Drag out to select the entire collage:
142
 
(dstX, dstY, dstW, dstH) = resultWindow.iconView().extents
143
 
drag = LinearDrag((dstX+50, dstY+50), (dstX+500, dstY+500))
144
 
drag.doDrag(2.0, 500)
145
 
 
146
 
sleep(0.5)
147
 
 
148
 
# Drag it somewhere:
149
 
drag = LinearDrag((dstX+300, dstY+300), (dstX+600, dstY+600))
150
 
drag.doDrag(2.0, 50)
151
 
 
152
 
sleep(0.5)
153
 
 
154
 
click(dstX+50, dstY+50, 1)
155
 
#viewMenu = resultWindow.menu("View")
156
 
#viewMenu.menuItem("Zoom In").click()
157
 
#viewMenu.menuItem("Zoom In").click()
158
 
 
159