Commit 42e0ebb2 authored by Agrigor's avatar Agrigor

Fixed closeEvent, Code cleanup, ready to program the main gui next :)

parent 9ccebfad
...@@ -51,14 +51,11 @@ ...@@ -51,14 +51,11 @@
<property name="toolTip"> <property name="toolTip">
<string>IPv4-Adresse</string> <string>IPv4-Adresse</string>
</property> </property>
<property name="inputMask">
<string>000.000.000.000;_</string>
</property>
<property name="text"> <property name="text">
<string>...</string> <string/>
</property> </property>
<property name="maxLength"> <property name="maxLength">
<number>15</number> <number>32767</number>
</property> </property>
<property name="cursorPosition"> <property name="cursorPosition">
<number>0</number> <number>0</number>
...@@ -66,9 +63,6 @@ ...@@ -66,9 +63,6 @@
<property name="alignment"> <property name="alignment">
<set>Qt::AlignCenter</set> <set>Qt::AlignCenter</set>
</property> </property>
<property name="placeholderText">
<string>000.000.000.000</string>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>
...@@ -192,13 +186,6 @@ ...@@ -192,13 +186,6 @@
</item> </item>
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="KSeparator" name="kseparator">
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
</widget>
</item>
<item> <item>
<widget class="QLabel" name="O_Msg"> <widget class="QLabel" name="O_Msg">
<property name="frameShape"> <property name="frameShape">
...@@ -218,20 +205,7 @@ ...@@ -218,20 +205,7 @@
</layout> </layout>
</item> </item>
</layout> </layout>
<zorder>splitter</zorder>
<zorder>layoutWidget</zorder>
<zorder>layoutWidget</zorder>
<zorder>I_Start</zorder>
<zorder>O_Msg</zorder>
<zorder>kseparator</zorder>
</widget> </widget>
<customwidgets>
<customwidget>
<class>KSeparator</class>
<extends>QFrame</extends>
<header>kseparator.h</header>
</customwidget>
</customwidgets>
<tabstops> <tabstops>
<tabstop>I_IPAdress</tabstop> <tabstop>I_IPAdress</tabstop>
<tabstop>I_Connect</tabstop> <tabstop>I_Connect</tabstop>
......
...@@ -4,9 +4,7 @@ ...@@ -4,9 +4,7 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~ IMPORT LIBRARIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~ IMPORT LIBRARIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import sys import sys
#import psutil
import threading import threading
import socket import socket
import time import time
...@@ -29,26 +27,25 @@ PLAYERS_MAX = 4 ...@@ -29,26 +27,25 @@ PLAYERS_MAX = 4
class Comunication: class Comunication:
def __init__(self, ip, port): def __init__(self, ip, port):
self.__ip = ip self.ip = ip
self.__port = port self.port = port
self.socketCreated = False
def check_ip(self): def check_ip(self):
try: try:
socket.inet_aton(self.__ip) socket.inet_aton(self.ip)
return True return True
except OSError: except OSError:
return False return False
def create_client_socket(self): def create_client_socket(self):
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.__socket.bind(("", self.__port)) self.socket.bind(("", self.port))
def receive(self, bytes): def receive(self, bytes):
return self.__socket.recvfrom(bytes) return self.socket.recvfrom(bytes)
def cleanup(self): def cleanup(self):
self.__socket.close() self.socket.close()
class Player: class Player:
...@@ -64,105 +61,105 @@ class Player: ...@@ -64,105 +61,105 @@ class Player:
class GUI: class GUI:
def __init__(self): def __init__(self):
self.players = [Player() for i in range(PLAYERS_MAX)] self.app = QApplication(sys.argv)
self.__app = QApplication(sys.argv) self.start = StartGUI()
self.__start = StartGUI(self.players) sys.exit(self.app.exec_())
self.__app.exec_()
#self.kill_proc_tree(os.getpid())
#def kill_proc_tree(pid, including_parent=True):
#parent = psutil.Process(pid)
#if including_parent:
#parent.kill()
class StartGUI: class StartGUI(QDialog):
def __init__(self, players): def __init__(self):
self.__UI_Start = loadUi("GUIs/start.ui") QDialog.__init__(self)
self.UI_Start = loadUi("GUIs/start.ui", self)
self.players = players self.players = [Player() for i in range(PLAYERS_MAX)]
self.__player_name_entered = [False] * PLAYERS_MAX self.player_name_entered = [False] * PLAYERS_MAX
self.__playerNameInputs = [self.__UI_Start.I_NamePlayer1, self.playerNameInputs = [self.UI_Start.I_NamePlayer1,
self.__UI_Start.I_NamePlayer2, self.UI_Start.I_NamePlayer2,
self.__UI_Start.I_NamePlayer3, self.UI_Start.I_NamePlayer3,
self.__UI_Start.I_NamePlayer4] self.UI_Start.I_NamePlayer4]
self.__UI_Start.connect(self.__UI_Start.I_Connect, SIGNAL("clicked()"), self.connect) self.UI_Start.connect(self.UI_Start.I_Connect, SIGNAL("clicked()"), self.connect_server)
self.__UI_Start.connect(self.__UI_Start.I_Start, SIGNAL("clicked()"), self.start) self.UI_Start.connect(self.UI_Start.I_Start, SIGNAL("clicked()"), self.start)
self.__UI_Start.connect(self.__UI_Start.I_NumberOfPlayers, SIGNAL("valueChanged(int)"), self.show_names_input_field) self.UI_Start.connect(self.UI_Start.I_NumberOfPlayers, SIGNAL("valueChanged(int)"), self.show_names_input_field)
self.__UI_Start.connect(self.__UI_Start.I_NamePlayer1, SIGNAL("textChanged(QString)"), self.name_player_changed) self.UI_Start.connect(self.UI_Start.I_NamePlayer1, SIGNAL("textChanged(QString)"), self.name_player_changed)
self.__UI_Start.connect(self.__UI_Start.I_NamePlayer2, SIGNAL("textChanged(QString)"), self.name_player_changed) self.UI_Start.connect(self.UI_Start.I_NamePlayer2, SIGNAL("textChanged(QString)"), self.name_player_changed)
self.__UI_Start.connect(self.__UI_Start.I_NamePlayer3, SIGNAL("textChanged(QString)"), self.name_player_changed) self.UI_Start.connect(self.UI_Start.I_NamePlayer3, SIGNAL("textChanged(QString)"), self.name_player_changed)
self.__UI_Start.connect(self.__UI_Start.I_NamePlayer4, SIGNAL("textChanged(QString)"), self.name_player_changed) self.UI_Start.connect(self.UI_Start.I_NamePlayer4, SIGNAL("textChanged(QString)"), self.name_player_changed)
self.__UI_Start.show() self.killSocket = True
def connect(self): self.UI_Start.show()
self.socket = Comunication(str(self.__UI_Start.I_IPAdress.text()), PORT)
if self.socket.check_ip() and not self.socket.socketCreated: def connect_server(self):
self.socket = Comunication(str(self.UI_Start.I_IPAdress.text()), PORT)
if self.socket.check_ip():
self.socket.create_client_socket() self.socket.create_client_socket()
self.socket.socketCreated = True self.UI_Start.I_Connect.setDisabled(True)
self.__UI_Start.I_Connect.setDisabled(True) self.UI_Start.I_IPAdress.setDisabled(True)
self.__UI_Start.I_IPAdress.setDisabled(True)
self.show_msg("Connected") self.show_msg("Connected")
self.__UI_Start.I_NumberOfPlayers.setDisabled(False) self.UI_Start.I_NumberOfPlayers.setDisabled(False)
self.__UI_Start.I_NamePlayer1.setDisabled(False) self.UI_Start.I_NamePlayer1.setDisabled(False)
else: else:
self.show_msg("No valid IP-address") self.show_msg("No valid IP-address")
self.socket.socketCreated = False
def show_names_input_field(self, number): def show_names_input_field(self, number):
number -= 1 number -= 1
for i in range(len(self.__playerNameInputs)): for i in range(len(self.playerNameInputs)):
if i <= number and self.socket.socketCreated: if i <= number:
self.__playerNameInputs[i].setDisabled(False) self.playerNameInputs[i].setDisabled(False)
else: else:
self.__playerNameInputs[i].setDisabled(True) self.playerNameInputs[i].setDisabled(True)
if number >= 1: if number >= 1:
self.show_msg(str(number + 1) + " Players") self.show_msg(str(number + 1) + " Players")
else: else:
self.show_msg("1 Player") self.show_msg("1 Player")
def name_player_changed(self, name): def name_player_changed(self, name):
for i in range(len(self.__playerNameInputs)): for i in range(len(self.playerNameInputs)):
if i + 1 > int(self.__UI_Start.O_NumberOfPlayers.text()) or len(self.__playerNameInputs[i].text()) > 1: if i + 1 > int(self.UI_Start.O_NumberOfPlayers.text()) or len(self.playerNameInputs[i].text()) > 1:
self.__player_name_entered[i] = True self.player_name_entered[i] = True
else: else:
self.__player_name_entered[i] = False self.player_name_entered[i] = False
if all(self.__player_name_entered): if all(self.player_name_entered):
self.__UI_Start.I_Start.setDisabled(False) self.UI_Start.I_Start.setDisabled(False)
else: else:
self.__UI_Start.I_Start.setDisabled(True) self.UI_Start.I_Start.setDisabled(True)
def start(self): def start(self):
namesOfPlayers = [str(self.__UI_Start.I_NamePlayer1.text()), namesOfPlayers = [str(self.UI_Start.I_NamePlayer1.text()),
str(self.__UI_Start.I_NamePlayer2.text()), str(self.UI_Start.I_NamePlayer2.text()),
str(self.__UI_Start.I_NamePlayer3.text()), str(self.UI_Start.I_NamePlayer3.text()),
str(self.__UI_Start.I_NamePlayer4.text())] str(self.UI_Start.I_NamePlayer4.text())]
for i in range(0, int(self.__UI_Start.O_NumberOfPlayers.text())): for i in range(0, int(self.UI_Start.O_NumberOfPlayers.text())):
self.players[i].name = namesOfPlayers[i] self.players[i].name = namesOfPlayers[i]
self.main = MainGUI(self.socket, self.players) self.main = MainGUI(self.socket, self.players)
self.__UI_Start.close() self.killSocket = False
self.UI_Start.close()
def show_msg(self, msg): def show_msg(self, msg):
self.__UI_Start.O_Msg.setText(msg) self.UI_Start.O_Msg.setText(msg)
# TODO Include this method if window is closed by user def closeEvent(self, event):
def cleanup(self): if self.killSocket:
if self.socket.socketCreated: try:
self.socket.cleanup() self.socket.cleanup()
except:
pass
class MainGUI: class MainGUI(QMainWindow):
def __init__(self, socket, players): def __init__(self, socket, players):
self.UI_Main = loadUi("GUIs/main.ui") QMainWindow.__init__(self)
self.UI_Main = loadUi("GUIs/main.ui", self)
self.socket = socket self.socket = socket
self.players = players self.players = players
self.UI_Main.connect(self.UI_Main.Button, SIGNAL("clicked()"), self.button) self.UI_Main.connect(self.UI_Main.Button, SIGNAL("clicked()"), self.button)
self.UI_Main.connect(self.UI_Main.Button_2, SIGNAL("clicked()"), self.button_2) self.UI_Main.connect(self.UI_Main.Button_2, SIGNAL("clicked()"), self.button_2)
self.show_msg("No communication")
self.UI_Main.show() self.UI_Main.show()
def button_2(self): def button_2(self):
...@@ -178,14 +175,16 @@ class MainGUI: ...@@ -178,14 +175,16 @@ class MainGUI:
daten, addr = self.socket.receive(1024) daten, addr = self.socket.receive(1024)
self.show_msg(addr[0] + " -> " + daten.decode()) self.show_msg(addr[0] + " -> " + daten.decode())
time.sleep(0.1) time.sleep(0.1)
self.show_msg("No communication")
def show_msg(self, msg): def show_msg(self, msg):
self.UI_Main.O_Msg.setText(msg) self.UI_Main.O_Msg.setText(msg)
# TODO Include this method if window is closed by user def closeEvent(self, event):
def cleanup(self): try:
if self.socket.socketCreated:
self.socket.cleanup() self.socket.cleanup()
except:
pass
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...@@ -193,7 +192,8 @@ class MainGUI: ...@@ -193,7 +192,8 @@ class MainGUI:
# Start Client # Start Client
client = GUI() if __name__ == "__main__":
GUI()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......
class Player:
def __init__(self):
self.punkte = 0
self.name = "nix"
class GUI:
def __init__(self):
self.players = [Player() for i in range(4)]
self.players[1].punkte = 1337
self.players[1].name = "bla"
self.main = MAIN(self.players)
class MAIN:
def __init__(self, foo):
self.foo = foo
self.show()
def show(self):
print(self.foo[0].name)
print(self.foo[1].name)
bar = GUI()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment