Commit 9ccebfad authored by Agrigor's avatar Agrigor

Code cleaned up, Start GUI finished, Main GUI started with testcode

parent f7a1b92e
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="Button">
<property name="geometry">
<rect>
<x>210</x>
<y>170</y>
<width>99</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QLabel" name="O_Msg">
<property name="geometry">
<rect>
<x>100</x>
<y>340</y>
<width>551</width>
<height>51</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="O_Msg_2">
<property name="geometry">
<rect>
<x>120</x>
<y>250</y>
<width>551</width>
<height>51</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QPushButton" name="Button_2">
<property name="geometry">
<rect>
<x>510</x>
<y>160</y>
<width>99</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
......@@ -2,9 +2,6 @@
<ui version="4.0">
<class>StartGUI</class>
<widget class="QDialog" name="StartGUI">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
......@@ -13,34 +10,30 @@
<height>299</height>
</rect>
</property>
<property name="windowTitle">
<string>Hacker Jeopardy</string>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<widget class="QPushButton" name="I_Start">
<property name="enabled">
<bool>false</bool>
<property name="minimumSize">
<size>
<width>339</width>
<height>299</height>
</size>
</property>
<property name="geometry">
<rect>
<x>10</x>
<y>220</y>
<width>321</width>
<height>31</height>
</rect>
<property name="maximumSize">
<size>
<width>339</width>
<height>299</height>
</size>
</property>
<property name="text">
<string>Start Game</string>
<property name="windowTitle">
<string>Hacker Jeopardy</string>
</property>
</widget>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QSplitter" name="splitter">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>321</width>
<height>41</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
......@@ -59,10 +52,10 @@
<string>IPv4-Adresse</string>
</property>
<property name="inputMask">
<string/>
<string>000.000.000.000;_</string>
</property>
<property name="text">
<string/>
<string>...</string>
</property>
<property name="maxLength">
<number>15</number>
......@@ -74,7 +67,7 @@
<set>Qt::AlignCenter</set>
</property>
<property name="placeholderText">
<string/>
<string>000.000.000.000</string>
</property>
</widget>
</item>
......@@ -86,15 +79,52 @@
</property>
</widget>
</widget>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>110</y>
<width>321</width>
<height>102</height>
</rect>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="O_Name_NumberOfPlayers_Edit">
<property name="text">
<string>Anzahl Spieler:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="O_NumberOfPlayers">
<property name="text">
<string>1</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QSlider" name="I_NumberOfPlayers">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>4</number>
</property>
<property name="pageStep">
<number>1</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="Input_PlayerNames">
<item>
<widget class="QLineEdit" name="I_NamePlayer1">
......@@ -149,68 +179,28 @@
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>11</x>
<y>60</y>
<width>321</width>
<height>45</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="O_Name_NumberOfPlayers_Edit">
<property name="text">
<string>Anzahl Spieler:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="O_NumberOfPlayers">
<property name="text">
<string>1</string>
<widget class="QPushButton" name="I_Start">
<property name="enabled">
<bool>false</bool>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
<property name="text">
<string>Start Game</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QSlider" name="I_NumberOfPlayers">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>4</number>
</property>
<property name="pageStep">
<number>1</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="KSeparator" name="kseparator">
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
</widget>
</item>
</layout>
</widget>
<item>
<widget class="QLabel" name="O_Msg">
<property name="geometry">
<rect>
<x>10</x>
<y>260</y>
<width>321</width>
<height>31</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
......@@ -224,19 +214,10 @@
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="KSeparator" name="kseparator">
<property name="geometry">
<rect>
<x>10</x>
<y>250</y>
<width>321</width>
<height>20</height>
</rect>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
</widget>
</item>
</layout>
</item>
</layout>
<zorder>splitter</zorder>
<zorder>layoutWidget</zorder>
<zorder>layoutWidget</zorder>
......
......@@ -6,13 +6,14 @@
import sys
#import psutil
import threading
import socket
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import *
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ GLOBAL CONSTANTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -30,6 +31,7 @@ class Comunication:
def __init__(self, ip, port):
self.__ip = ip
self.__port = port
self.socketCreated = False
def check_ip(self):
try:
......@@ -50,21 +52,9 @@ class Comunication:
class Player:
def __init__(self, name):
self.Points = 0
self.Name = name
def get_Points(self):
return self.Points
def set_Points(self, newPoints):
self.Points = newPoints
def get_Name(self):
return self.Name
def set_Name(self, newName):
self.Name = newName
def __init__(self):
self.points = 0
self.name = ""
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -74,29 +64,26 @@ class Player:
class GUI:
def __init__(self):
self.socketCreated = False
self.players = []
self.players = [Player() for i in range(PLAYERS_MAX)]
self.__app = QApplication(sys.argv)
self.__start = StartGUI(self.players)
self.__app.exec_()
#self.kill_proc_tree(os.getpid())
self.app = QApplication(sys.argv)
start = StartGUI(self.socketCreated, self.players)
self.app.exec_()
self.app.aboutToQuit(self.cleanup())
#def kill_proc_tree(pid, including_parent=True):
#parent = psutil.Process(pid)
#if including_parent:
#parent.kill()
def cleanup(self):
if self.socketCreated:
self.UDP.cleanup()
print("Bye Bye")
sys.exit(0)
class StartGUI:
def __init__(self, socketCreated, players):
def __init__(self, players):
self.__UI_Start = loadUi("GUIs/start.ui")
self.socketCreated = socketCreated
self.players = players
self.players_ready = [False] * PLAYERS_MAX
self.playerInputs = [self.__UI_Start.I_NamePlayer1,
self.__player_name_entered = [False] * PLAYERS_MAX
self.__playerNameInputs = [self.__UI_Start.I_NamePlayer1,
self.__UI_Start.I_NamePlayer2,
self.__UI_Start.I_NamePlayer3,
self.__UI_Start.I_NamePlayer4]
......@@ -111,59 +98,95 @@ class StartGUI:
self.__UI_Start.show()
def name_player_changed(self, name):
for i in range(len(self.playerInputs)):
if i + 1 > int(self.__UI_Start.O_NumberOfPlayers.text()) or len(self.playerInputs[i].text()) > 1:
self.players_ready[i] = True
else:
self.players_ready[i] = False
if all(self.players_ready):
self.__UI_Start.I_Start.setDisabled(False)
def connect(self):
self.socket = Comunication(str(self.__UI_Start.I_IPAdress.text()), PORT)
if self.socket.check_ip() and not self.socket.socketCreated:
self.socket.create_client_socket()
self.socket.socketCreated = True
self.__UI_Start.I_Connect.setDisabled(True)
self.__UI_Start.I_IPAdress.setDisabled(True)
self.show_msg("Connected")
self.__UI_Start.I_NumberOfPlayers.setDisabled(False)
self.__UI_Start.I_NamePlayer1.setDisabled(False)
else:
self.__UI_Start.I_Start.setDisabled(True)
self.show_msg("No valid IP-address")
self.socket.socketCreated = False
def show_names_input_field(self, number):
number -= 1
for i in range(len(self.playerInputs)):
if i <= number and self.socketCreated:
self.playerInputs[i].setDisabled(False)
for i in range(len(self.__playerNameInputs)):
if i <= number and self.socket.socketCreated:
self.__playerNameInputs[i].setDisabled(False)
else:
self.playerInputs[i].setDisabled(True)
self.__playerNameInputs[i].setDisabled(True)
if number >= 1:
self.show_msg(str(number + 1) + " Players")
else:
self.show_msg("1 Player")
def name_player_changed(self, name):
for i in range(len(self.__playerNameInputs)):
if i + 1 > int(self.__UI_Start.O_NumberOfPlayers.text()) or len(self.__playerNameInputs[i].text()) > 1:
self.__player_name_entered[i] = True
else:
self.__player_name_entered[i] = False
if all(self.__player_name_entered):
self.__UI_Start.I_Start.setDisabled(False)
else:
self.__UI_Start.I_Start.setDisabled(True)
def start(self):
namesOfPlayers = [str(self.__UI_Start.I_NamePlayer1.text()),
str(self.__UI_Start.I_NamePlayer2.text()),
str(self.__UI_Start.I_NamePlayer3.text()),
str(self.__UI_Start.I_NamePlayer4.text())]
for i in range(0, int(self.__UI_Start.O_NumberOfPlayers.text())):
self.players.append(Player(namesOfPlayers[i]))
def connect(self):
self.UDP = Comunication(str(self.__UI_Start.I_IPAdress.text()), PORT)
if self.UDP.check_ip() and not self.socketCreated:
self.UDP.create_client_socket()
self.socketCreated = True
self.__UI_Start.I_Connect.setDisabled(True)
self.__UI_Start.I_IPAdress.setDisabled(True)
self.show_msg("Connected")
self.__UI_Start.I_NumberOfPlayers.setDisabled(False)
self.__UI_Start.I_NamePlayer1.setDisabled(False)
else:
self.show_msg("No valid IP-address")
self.socketCreated = False
self.players[i].name = namesOfPlayers[i]
self.main = MainGUI(self.socket, self.players)
self.__UI_Start.close()
def show_msg(self, msg):
self.__UI_Start.O_Msg.setText(msg)
# TODO Include this method if window is closed by user
def cleanup(self):
if self.socketCreated:
self.UDP.cleanup()
if self.socket.socketCreated:
self.socket.cleanup()
class MainGUI:
def __init__(self, socket, players):
self.UI_Main = loadUi("GUIs/main.ui")
self.socket = socket
self.players = players
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.show()
def button_2(self):
self.stop_event.set()
def button(self):
self.stop_event = threading.Event()
self.c_thread = threading.Thread(target=self.listen_communication, args=(self.stop_event,))
self.c_thread.start()
def listen_communication(self, stop_event):
while True and not stop_event.isSet():
daten, addr = self.socket.receive(1024)
self.show_msg(addr[0] + " -> " + daten.decode())
time.sleep(0.1)
def show_msg(self, msg):
self.UI_Main.O_Msg.setText(msg)
# TODO Include this method if window is closed by user
def cleanup(self):
if self.socket.socketCreated:
self.socket.cleanup()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......
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