Hello all together
first of all sry for my bad english
Well i got a problem with some of my application.
First of all i had to write an application which can drag rooms from a source window to 4 destination windows.
That works fine .
But if i drag a room for example to window 1 the programm should tell the user that there is no more dragging possible because the room is already set.
Here is my complete Code (for one window at the moment)
# coding=utf-8
import wx # wxPython
import os # OS-spezifische Funktionen
import cPickle
class Room_draw(object):
#--------------------------------------
def __init__(self, room_name, size):
#-----------------------------------
self.room_name = room_name
self.size = size
self.pos = (0,0)
self.status = 0
def SetPosition(self, pos):
#-----------------------------------
self.pos = pos
#Zeichnen der Räume für die Belegung
def Draw_Room(self, dc):
#-----------------------------------
if self.status == 1:
actPen_o = dc.GetPen()
dc.SetPen(wx.Pen('black', 5, wx.SOLID))
dc.DrawRectangle(self.pos[0], self.pos[1], self.size[0], self.size[1])
dc.DrawText(self.room_name, self.pos[0], self.pos[1])
if self.status == 1:
dc.SetPen(actPen_o)
return dc
def Draw_Room_dragged(self, dc, x_pos, y_pos, size_x, size_y, name):
if self.status == 1:
actPen_o = dc.GetPen()
dc.SetPen(wx.Pen('black', 5, wx.SOLID))
dc.DrawRectangle(x_pos, y_pos, size_x, size_y)
dc.DrawText(name, x_pos, y_pos)
if self.status == 1:
dc.SetPen(actPen_o)
return dc
#Prüfen ob Raum ausgewählt
def HitTest(self, x_pos, y_pos):
#-----------------------------------
if x_pos > self.pos[0] and x_pos < (self.pos[0] + self.size[0]):
if y_pos > self.pos[1] and y_pos < (self.pos[1] + self.size[1]):
return True
return False
def HitTestXY(self, event_x_pos, event_y_pos, x_pos, y_pos, size_x, size_y):
if event_x_pos > x_pos and event_x_pos < (x_pos + size_x):
if event_y_pos > y_pos and event_y_pos < (y_pos + size_y):
return True
return False
def Select_Room(self):
#-----------------------------------
self.status = 1
def DeSelect_Room(self):
#-----------------------------------
self.status = 0
class DropTarget(wx.PyDropTarget):
def __init__(self, window):
wx.PyDropTarget.__init__(self)
self.data = wx.CustomDataObject("Doodle")
self.SetDataObject(self.data)
def OnDragOver(self, x, y, d):
return d
def OnDrop(self, x, y):
return True
def OnData(self, x, y, d):
return d
class DropTargetWestOst(wx.PyDropTarget):
def __init__(self, window):
wx.PyDropTarget.__init__(self)
self.window = window
self.data = wx.CustomDataObject("Raumplaner")
self.SetDataObject(self.data)
def OnDragOver(self, x, y, d):
return d
def OnDrop(self, x, y):
return True
def OnData(self, x, y, d):
if self.GetData():
dc = wx.ClientDC(self.window)
linesdata = self.data.GetData()
lines = cPickle.loads(linesdata)
self.window.DrawSavedLines(lines, dc, y)
print lines.size
return d
class DropSource(wx.Window):
def __init__(self, parent, statusbar):
#-----------------------------------
wx.Window.__init__(self, parent, size=(500,610), pos=(720,50))
data_DropSource = DropTarget(self)
self.SetDropTarget(data_DropSource)
self.statusbar = statusbar
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.rooms_list = [Room_draw('OW1',(90, 450)),Room_draw('OW2',(90, 280)),Room_draw('OW3',(90, 160)), Room_draw('OW4',(90, 110))
,Room_draw('SN1',(390, 160)),Room_draw('SN2',(290, 160)),Room_draw('SN3',(190, 160)), Room_draw('SN4',(90, 160))]
#Position der Räume festlegen
x_pos = 0
y_pos = 0
for room_each in self.rooms_list:
room_each.SetPosition((x_pos, y_pos))
x_pos = x_pos + 95
if self.rooms_list[4]:
self.rooms_list[4].SetPosition((0, 455))
if self.rooms_list[5]:
self.rooms_list[5].SetPosition((95, 290))
if self.rooms_list[6]:
self.rooms_list[6].SetPosition((285, 120))
if self.rooms_list[7]:
self.rooms_list[7].SetPosition((390, 285))
self.dragRoom_one = None
def OnPaint(self, event):
#-----------------------------------
dc = wx.PaintDC(self)
dc.SetPen(wx.Pen('black', 1, wx.SOLID))
font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
dc.SetFont(font)
#alle Räume darstellen
for room_each in self.rooms_list:
room_each.Draw_Room(dc)
def OnSize(self, event):
#-----------------------------------
size = self.GetParent().GetClientSize()
self.SetSize(size)
def OnLeftDown(self, event):
#-----------------------------------
# wurde ein Rechteck getroffen ?
x_pos = event.GetX()
y_pos = event.GetY()
for room_each in self.rooms_list:
if room_each.HitTest(x_pos, y_pos):
room_each.Select_Room()
linesdata = cPickle.dumps(room_each, 1)
ldata = wx.CustomDataObject("Raumplaner")
ldata.SetData(linesdata)
dropSource = wx.DropSource(self)
dropSource.SetData(ldata)
dropSource.DoDragDrop(wx.Drag_AllowMove)
# neu darstellen
self.Refresh()
self.dragRoom_one = room_each
else:
room_each.DeSelect_Room()
#Raumtyp für welchen Raum in Statusbar anzeigen
for room_each in self.rooms_list:
if room_each.room_name == 'OW1' and room_each.HitTest(x_pos, y_pos):
self.statusbar.SetStatusText(room_each.room_name+u' gehört zu Raum W1+W2+W3 oder O1+O2+O3', 0)
elif room_each.room_name == 'OW2' and room_each.HitTest(x_pos, y_pos):
self.statusbar.SetStatusText(room_each.room_name+u' gehört zu Raum W1+W2 oder W2+W3 oder O1+O2 oder O2+O3', 0)
elif room_each.room_name == 'OW3' and room_each.HitTest(x_pos, y_pos):
self.statusbar.SetStatusText(room_each.room_name+u' gehört zu Raum W1 oder W3 oder O1 oder O3', 0)
elif room_each.room_name == 'OW4' and room_each.HitTest(x_pos, y_pos):
self.statusbar.SetStatusText(room_each.room_name+u' gehört zu Raum W2 oder O2', 0)
elif room_each.room_name == 'SN1' and room_each.HitTest(x_pos, y_pos):
self.statusbar.SetStatusText(room_each.room_name+u' gehört zu Raum N1+N2+N3+N4 oder S1+S2+S3+S4', 0)
elif room_each.room_name == 'SN2' and room_each.HitTest(x_pos, y_pos):
self.statusbar.SetStatusText(room_each.room_name+u' gehört zu Raum N1+N2+N3 oder N2+N3+N4 oder S1+S2+S3 oder S2+S3+S4', 0)
elif room_each.room_name == 'SN3' and room_each.HitTest(x_pos, y_pos):
self.statusbar.SetStatusText(room_each.room_name+u' gehört zu Raum N1+N2 oder N2+N3 oder N3+N4 oder S1+S2 oder S2+S3 oder S3+S4', 0)
elif room_each.room_name == 'SN4' and room_each.HitTest(x_pos, y_pos):
self.statusbar.SetStatusText(room_each.room_name+u' gehört zu Raum N1 oder N2 oder N3 oder N4 oder S1 oder S2 oder S3 oder S4', 0)
else:
return
def OnLeftUp(self, event):
if self.dragRoom_one != None:
# Ziehen beenden
self.dragRoom_one = None
self.Refresh()
class West(wx.Window):
def __init__(self, parent, statusbar):
wx.Window.__init__(self, parent, pos=(0, 0), size=(90, 450), style = wx.TRANSPARENT)
self.SetForegroundColour(wx.WHITE)
self.statusbar = statusbar
self.room_instance_list = []
self.room_name_list = []
self.room_pos_list = []
self.dragRoom_one = None
self.dt = DropTargetWestOst(self)
self.SetDropTarget(self.dt)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, event):
#RaumRaster zeichnen
dc = wx.PaintDC(self)
dc.SetBrush(wx.Brush('GREEN YELLOW'))#, wx.CROSS_HATCH))
dc.DrawRectangle(0, 0, 90, 160)
dc.DrawText('W1', 35, 80)
dc.DrawRectangle(0, 170, 90, 110)
dc.DrawText('W2', 35, 210)
dc.DrawRectangle(0, 290, 90, 160)
dc.DrawText('W3', 35, 350)
def DrawSavedLines(self, lines, dc, y):
self.dc = dc
dc.SetBrush(wx.Brush('RED'))
#Prüfen welcher Raum gezogen wurde
if lines.room_name == 'OW1':
pos = wx.Size(0,0)
dc.DrawRectangle(pos[0], pos[1], lines.size[0], lines.size[1])
dc.DrawText(lines.room_name, pos[0], pos[1])
self.ShowTextFromRooms(dc)
self.room_name_list.append(lines.room_name)
self.room_instance_list.append(lines)
self.room_pos_list.append(pos)
self.Reminder(self.room_name_list, self.room_instance_list,self.room_pos_list)
if y > 226 and lines.room_name == 'OW2':
pos = wx.Size(0,170)
dc.DrawRectangle(pos[0], pos[1], lines.size[0], lines.size[1])
dc.DrawText(lines.room_name, pos[0], pos[1])
self.ShowTextFromRooms(dc)
self.room_name_list.append(lines.room_name)
self.room_instance_list.append(lines)
self.room_pos_list.append(pos)
self.Reminder(self.room_name_list, self.room_instance_list,self.room_pos_list)
if y < 225 and lines.room_name == 'OW2':
pos = wx.Size(0,0)
dc.DrawRectangle(pos[0], pos[1], lines.size[0], lines.size[1])
dc.DrawText(lines.room_name, pos[0], pos[1])
self.ShowTextFromRooms(dc)
self.room_name_list.append(lines.room_name)
self.room_instance_list.append(lines)
self.room_pos_list.append(pos)
self.Reminder(self.room_name_list, self.room_instance_list,self.room_pos_list)
if y < 170 and lines.room_name == 'OW3':
pos = wx.Size(0,0)
dc.DrawRectangle(pos[0], pos[1], lines.size[0], lines.size[1])
dc.DrawText(lines.room_name, pos[0], pos[1])
self.ShowTextFromRooms(dc)
self.room_name_list.append(lines.room_name)
self.room_instance_list.append(lines)
self.room_pos_list.append(pos)
self.Reminder(self.room_name_list, self.room_instance_list, self.room_pos_list)
if y > 290 and lines.room_name == 'OW3':
pos = wx.Size(0,290)
dc.DrawRectangle(pos[0], pos[1], lines.size[0], lines.size[1])
dc.DrawText(lines.room_name, pos[0], pos[1])
self.ShowTextFromRooms(dc)
self.room_name_list.append(lines.room_name)
self.room_instance_list.append(lines)
self.room_pos_list.append(pos)
self.Reminder(self.room_name_list, self.room_instance_list,self.room_pos_list)
if y < 290 and y > 170 and lines.room_name == 'OW4' :
pos = wx.Size(0,170)
dc.DrawRectangle(pos[0], pos[1], lines.size[0], lines.size[1])
dc.DrawText(lines.room_name, pos[0], pos[1])
self.ShowTextFromRooms(dc)
self.room_name_list.append(lines.room_name)
self.room_instance_list.append(lines)
self.room_pos_list.append(pos)
self.Reminder(self.room_name_list, self.room_instance_list, self.room_pos_list)
def Reminder(self, room_name_list, room_instance_list, position):
#merken welche Räume gezogen wurden
self.room_name_list = room_name_list
self.room_instance_list = room_instance_list
self.position = position
#self.Bind(wx.EVT_
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
def ShowTextFromRooms(self, dc):
dc.DrawText('W1', 35, 80)
dc.DrawText('W2', 35, 210)
dc.DrawText('W3', 35, 350)
def OnMouseOver(self, event):
return
def OnLeftDown(self, event):
#-----------------------------------
# wurde ein Rechteck getroffen ?
x_pos = event.GetX()
y_pos = event.GetY()
for j in range(0,len(self.position)):
for each_room in self.room_instance_list:
if each_room.HitTestXY(x_pos, y_pos, self.position[j].GetWidth(), self.position[j].GetHeight(),
self.room_instance_list[j].size[0], self.room_instance_list[j].size[1]) and self.room_instance_list[j] == each_room:
each_room.Draw_Room_dragged(self.dc, self.position[j].GetWidth(), self.position[j].GetHeight(),
self.room_instance_list[j].size[0], self.room_instance_list[j].size[1], self.room_instance_list[j].room_name)
self.remind_rooms = self.room_instance_list.index(each_room)
each_room.Select_Room()
linesdata = cPickle.dumps(each_room, 1)
ldata = wx.CustomDataObject("Doodle")
ldata.SetData(linesdata)
dropSource = wx.DropSource(self)
dropSource.SetData(ldata)
dropSource.DoDragDrop(wx.Drag_AllowMove)
#self.RefreshRect(wx.Rect(self.position[j].GetWidth(), self.position[j].GetHeight()), True)
#self.Update()
if self.remind_rooms == j:
#self.Refresh()
self.dc.SetBrush(wx.Brush('GREEN YELLOW'))
self.dc.DrawRectangle(self.position[j].GetWidth(),self.position[j].GetHeight(),self.room_instance_list[j].size[0], self.room_instance_list[j].size[1])
self.ShowTextFromRooms(self.dc)
self.dragRoom_one = each_room
else:
each_room.DeSelect_Room()
def OnLeftUp(self, event):
#-----------------------------------
if self.dragRoom_one != None:
# Ziehen beenden
self.dragRoom_one = None
#self.Refresh(wx.Rect(wx.Rect(self.position[j].GetWidth(), self.position[j].GetHeight())))
self.Refresh()
class Zeichnen(wx.Window):
def __init__(self, frame, statusbar):
wx.Window.__init__(self, frame, pos=(0,50), size=(700,450), style=wx.SUNKEN_BORDER)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.w1 = West(self, statusbar)
self.w2 = Ost(self, statusbar)
self.w3 = Sued(self, statusbar)
self.w4 = Nord(self, statusbar)
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.PrintRaster(dc)
def PrintRaster(self, dc):
point = [((95,0),(95,450)), #senkrechte linie links
((605,0),(605,450)), #senkrechte linie rechts
((0,165),(95,165)), #1. waagerechte
((0,285),(95,285)), #2. waagerechte
((605,165),(700,165)), #3. waagerechte
((605,285),(700,285)), #4.waagerechte
((155,165),(556,165)), #waagerechte 4er block oben
((155,285),(556,285)), #waagerechte 4er block unten
((155,0),(155,165)), #senkrechte linien oben
((255,0),(255,165)),
((355,0),(355,165)),
((455,0),(455,165)),
((555,0),(555,165)),
((155,285),(155,450)), #senkrechte linien unten
((255,285),(255,450)),
((355,285),(355,450)),
((455,285),(455,450)),
((555,285),(555,450))]
pen = wx.Pen('WHITE', 9, wx.SOLID)
for x in range(0,len(point)):
pen.SetCap(wx.CAP_ROUND)
dc.SetPen(pen)
dc.DrawLines(point[x])
dc.SetBrush(wx.Brush('GREEN YELLOW', wx.BDIAGONAL_HATCH))
dc.SetPen(wx.Pen('WHITE', 1, wx.TRANSPARENT))
dc.DrawRectangle(96, 0, 59, 450)
dc.DrawRectangle(556, 0, 49, 450)
dc.DrawRectangle(155, 166, 401, 119)
class ListCtrl(wx.Window):
def __init__(self, frame, bla, count,test):
wx.Window.__init__(self, frame, pos=(0, 510), size=(700,150))
self.bla = bla
self.count = count
self.test = test
self.listCtrl = wx.ListCtrl(self, -1, (0, 0), (700, 150),style = wx.LC_REPORT)
self.listCtrl.InsertColumn(0,u'Gebäude')
self.listCtrl.SetColumnWidth(0, 232)
self.listCtrl.InsertColumn(1,'Geschoss')
self.listCtrl.SetColumnWidth(1, 232)
self.listCtrl.InsertColumn(2,u'Freie Räume')
self.listCtrl.SetColumnWidth(2, 232)
self.listCtrl.InsertStringItem(0,u'Gebäude')
self.listCtrl.SetStringItem(0, 1, 'G1')
class Raum(wx.Frame):
#------------------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, -1,"Raumplaner", (0,200), (1300,700))
self.panel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
vbox = wx.BoxSizer(wx.VERTICAL)
sizer = wx.BoxSizer(wx.VERTICAL)
#Erstellen der Toolbar
self.toolbar = wx.ToolBar(self, -1, style=wx.NO_BORDER)
self.toolbar.SetToolBitmapSize((32,32))
self.toolbar.AddLabelTool( 0, 'Neu', wx.Bitmap('./icons/new_page.png'), wx.NullBitmap , wx.ITEM_NORMAL, 'Neu', 'Erstelle eine Neue Etage', None)
self.toolbar.AddLabelTool( 1, u'Öffnen', wx.Bitmap('./icons/open.png'), wx.NullBitmap , wx.ITEM_NORMAL, u'Öffnen', u'Wählt Datei zum öffnen aus', None)
self.toolbar.AddLabelTool( 2, 'Speichern', wx.Bitmap('./icons/save.png'), wx.NullBitmap , wx.ITEM_NORMAL, 'Speichern',
'Speichert aktuelle Raumaufteilung', None)
self.toolbar.AddLabelTool( 3, 'Speichern als', wx.Bitmap('./icons/save_as.png'), wx.NullBitmap , wx.ITEM_NORMAL, 'Speichern als',
'Speichert aktuelle Raumaufteilung in neuer Datei', None)
self.toolbar.AddLabelTool( 4, 'Löschen', wx.Bitmap('./icons/cut.png'), wx.NullBitmap , wx.ITEM_NORMAL, u'Etage löschen',
u'Löscht ausgewählte Etage', None)
self.toolbar.AddLabelTool( 5, 'Beenden', wx.Bitmap('./icons/exit.png'), wx.NullBitmap , wx.ITEM_NORMAL, 'Beenden', 'Beendet Programm', None)
self.toolbar.Realize()
self.toolbar.EnableTool(2,False)
self.toolbar.EnableTool(3,False)
self.toolbar.EnableTool(4,False)
sizer.Add(self.toolbar, 0, wx.EXPAND | wx.ALL | wx.TOP)
vbox.Add(sizer)
vbox.Add(self.panel, 1, wx.EXPAND | wx.ALL, 0)
self.SetSize((1250, 750))
self.SetSizer(sizer)
#Erstellen der Statusbar
self.statusbar = self.CreateStatusBar(2)
self.SetStatusBar(self.statusbar)
self.toolbar.Bind(wx.EVT_TOOL, self.OnNew, id = 0)
self.toolbar.Bind(wx.EVT_TOOL, self.OnOpen, id = 1)
#self.toolbar.Bind(wx.EVT_TOOL, self.OnSaveAs, id = 2)
self.toolbar.Bind(wx.EVT_TOOL, self.OnExit, id = 5)
#Erstellen der Menübar
self.menubar = wx.MenuBar()
datei = wx.Menu()
work = wx.Menu()
self.new = wx.MenuItem(datei, 000, '&Neue Etage\tCtrl+N', 'Neue Datei erstellen')
self.open = wx.MenuItem(datei, 001, u'&Öffnen\tCtrl+O', u'Datei öffnen')
self.worked = wx.MenuItem(datei, 002, 'B&earbeiten\tCtrl+B', 'Datei bearbeiten')
self.save = wx.MenuItem(datei, 003, '&Speichern\tCtrl+S', 'Datei speichern')
self.quit = wx.MenuItem(datei, 004, '&Beenden\tCtrl+Q', 'Programm Beenden')
self.back = wx.MenuItem(work, 010, '&Neue Etage\tCtrl+N', 'Neue Datei erstellen')
self.delete = wx.MenuItem(work, 011, u'&Öffnen\tCtrl+O', u'Datei öffnen')
self.test = wx.MenuItem(work, 012, 'B&earbeiten\tCtrl+B', 'Datei bearbeiten')
datei.AppendItem(self.new)
datei.AppendItem(self.open)
datei.AppendSeparator()
datei.AppendItem(self.worked)
self.worked.Enable(False)
datei.AppendItem(self.save)
self.save.Enable(False)
datei.AppendSeparator()
datei.AppendItem(self.quit)
#self.menubar.EnableTop(1, False)
work.AppendItem(self.back)
work.AppendItem(self.delete)
work.AppendItem(self.test)
self.Bind(wx.EVT_MENU, self.OnNew, id = 0)
self.Bind(wx.EVT_MENU, self.OnOpen, id = 1)
#self.Bind(wx.EVT_MENU, self.OnWorked, id = 2)
#self.Bind(wx.EVT_MENU, self.OnSaveAs, id = 3)
self.Bind(wx.EVT_MENU, self.OnExit, id = 004)
self.menubar.Append(datei, '&Datei')
self.menubar.Append(work, '&Bearbeiten')
self.SetMenuBar(self.menubar)
self.count = 1
def OnNew(self, event):
self.count = self.count + 1
self.test = 0
self.test = self.count
self.bla = 1
self.bla = self.bla + self.count
self.raster = Zeichnen(self, self.statusbar)
self.source = DropSource(self, self.statusbar)
self.listctrl = ListCtrl(self, self.bla, self.count, self.test)
self.worked.Enable(True)
self.save.Enable(True)
self.toolbar.EnableTool(2,True)
self.toolbar.EnableTool(3,True)
self.toolbar.EnableTool(4,True)
self.menubar.Refresh()
def OnOpen(self, event):
self.directory = os.getcwd()
self.save = wx.FileDialog(self, u'Bitte Datei zum Laden wählen', self.directory, '', '*.txt', wx.FD_OPEN | wx.FD_CHANGE_DIR)
self.choose_save = self.save.ShowModal()
if self.choose_save == wx.ID_OK:
try:
wx.MessageBox("Speichervorgang erfolgreich!", "Info", wx.OK | wx.ICON_INFORMATION)
except IOError, error:
wx.MessageBox("Fehler:\n" + str(error), "Info", wx.OK | wx.ICON_INFORMATION)
#def OnWorked(self, event):
#self.dc1.Clear()
#def OnSaveAs(self, event):
#self.Close()
def OnExit(self, event):
self.Close()
if __name__ == '__main__':
app = wx.PySimpleApp()
Raum().Show()
app.MainLoop()
Here Code for just one Window
Thanks for helping me