From c83b28b3c0db4024a140665607aaba3cf5d758e4 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Wed, 12 Oct 2011 23:24:45 -0500 Subject: [PATCH] More work on the hw4.py --- hw4/costmap.py | 1 - hw4/costmapwidget.py | 28 ++++++++++++++--- hw4/hw4.py | 73 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 hw4/hw4.py diff --git a/hw4/costmap.py b/hw4/costmap.py index ff4cee9..3560758 100644 --- a/hw4/costmap.py +++ b/hw4/costmap.py @@ -22,7 +22,6 @@ def __init__(self, width = 1, height = 1, dtype = 'int16', resolution = 1.0): self.data = np.zeros(self.width*self.height, dtype = dtype) self.data = self.data.reshape(self.width, self.height) - self.neighbors = np.zeros(8, dtype=self.dtype) self.on_update = None def __repr__(self): diff --git a/hw4/costmapwidget.py b/hw4/costmapwidget.py index e9b26b4..85e7f18 100644 --- a/hw4/costmapwidget.py +++ b/hw4/costmapwidget.py @@ -36,7 +36,7 @@ def __init__(self, costmap, parent=None, width=5.0, height=4.0, dpi=100, interpo FigureCanvas.__init__(self, self.fig) self.setParent(parent) - self.mpl_connect('pick_event', self.on_pick) + self.mpl_connect('button_release_event', self.on_mouse_release) FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding, @@ -44,6 +44,8 @@ def __init__(self, costmap, parent=None, width=5.0, height=4.0, dpi=100, interpo FigureCanvas.updateGeometry(self) + self.on_mouse_click = self.default_on_mouse_click + # Idle control self.idle = True @@ -53,9 +55,27 @@ def __init__(self, costmap, parent=None, width=5.0, height=4.0, dpi=100, interpo # Override costmap on change self.costmap.on_update = self.costmap_update_callback - def on_pick(self, pick_event): - """Gets called when an object is picked""" - print pick_event + def on_mouse_release(self, e): + """Gets called when a mouse button is released""" + if self.on_mouse_click != None: + self.on_mouse_click(e) + + def default_on_mouse_click(self, e): + """Default function for mouse clicking""" + if e.xdata == None or e.ydata == None: + return + from math import floor + coord = (floor(e.xdata+0.5)-0.5, floor(e.ydata+0.5)-0.5) + if -0.5 > coord[0] < self.costmap.width-1.5 or \ + -0.5 > coord[1] < self.costmap.height-1.5: + return + if e.button == 1: + self.start = Rectangle(coord, 1, 1, color='g') + elif e.button == 3: + self.goal = Rectangle(coord, 1, 1, color='k') + else: + return + self.on_map_update() def connect_stuff(self): """Make Qt connections""" diff --git a/hw4/hw4.py b/hw4/hw4.py new file mode 100644 index 0000000..d0b99ce --- /dev/null +++ b/hw4/hw4.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python + +import sys +from copy import copy + +from PySide import QtCore, QtGui + +from costmap import Costmap2D +from obstacle import Obstacle +from brushfire import BrushfireExpansion +from costmapwidget import Costmap2DWidget + +DEFAULT_WIDTH = 10 +DEFAULT_HEIGHT = 20 +DEFAULT_RESOLUTION = 1.0 + +class AlgorithmWidget(QtGui.QGroupBox): + def __init__(self, name="Unnamed Algorithm", parent=None): + QtGui.QGroupBox.__init__(self, name, parent=parent) + + self.costmap = Costmap2D(DEFAULT_WIDTH, DEFAULT_HEIGHT, resolution=DEFAULT_RESOLUTION) + Obstacle(3,3,3,3).draw(self.costmap) + Obstacle(5,9,3,3).draw(self.costmap) + Obstacle(4,16,3,3).draw(self.costmap) + + self.costmap_widget = Costmap2DWidget(self.costmap, parent = self) + + self.run_button = QtGui.QPushButton("Run") + self.step_button = QtGui.QPushButton("Step") + self.reset_button = QtGui.QPushButton("Reset") + self.buttons = [self.run_button, self.step_button, self.reset_button] + + self.make_connections() + + def make_connections(self): + """Makes the Qt connections""" + self.run_button.clicked.connect(self.on_run_clicked) + + @QtCore.Slot() + def on_run_clicked(self): + """Called when run is clicked""" + print '(run button) implement me!' + + def pack_buttons(self): + """Puts the buttons into the layout, this lets additional buttons to be inserted""" + button_layout = QtGui.QHBoxLayout() + for button in self.buttons: + button_layout.addWidget(button) + + layout = QtGui.QVBoxLayout() + layout.addWidget(self.costmap_widget) + layout.addLayout(button_layout) + layout.addStretch(1) + self.setLayout(layout) + + +class Homework4App(QtGui.QWidget): + def __init__(self, parent=None): + QtGui.QWidget.__init__(self, parent) + + example = AlgorithmWidget(parent=self) + example.pack_buttons() + + layout = QtGui.QHBoxLayout() + layout.addWidget(example) + self.setLayout(layout) + + +if __name__ == '__main__': + app = QtGui.QApplication(sys.argv) + hw4app = Homework4App() + hw4app.show() + sys.exit(app.exec_())