-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_gui.py
145 lines (121 loc) · 4.65 KB
/
client_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from PyQt4 import QtGui, QtCore
import sys
import client
from libs.encrypt_hash import crypto
import time
class PopUpLogIn(QtGui.QWidget):
def __init__(self, client, p=None):
QtGui.QWidget.__init__(self, p)
self.__logedin = False
self.client = client
self.init_ui()
def closeEvent(self, QCloseEvent):
if self.is_loged_in():
self.client.log_out()
QCloseEvent.accept()
def set_status(self, text):
self.status.setText(text)
def is_loged_in(self):
return self.__logedin
def init_ui(self):
self.server_ip = QtGui.QLineEdit("127.0.0.1")
self.server_ip.setPlaceholderText("IP or hostname of the server")
self.server_port = QtGui.QLineEdit("54554")
self.server_port.setPlaceholderText("Port number of the server")
self.uname = QtGui.QLineEdit()
self.uname.setPlaceholderText("Username")
self.passwd = QtGui.QLineEdit()
self.passwd.setPlaceholderText("Password")
self.passwd.setEchoMode(2)
self.login_butt = QtGui.QPushButton("&Login", self)
self.cancel_butt = QtGui.QPushButton("&Cancel", self)
self.status = QtGui.QLabel()
self.layout = QtGui.QGridLayout()
self.layout.addWidget(self.server_ip, 0, 0, 1, 2)
self.layout.addWidget(self.server_port, 0, 2, 1, 2)
self.layout.addWidget(self.uname, 1, 0, 1, 2)
self.layout.addWidget(self.passwd, 1, 2, 1, 2)
self.layout.addWidget(self.login_butt, 2, 0, 1, 1)
self.layout.addWidget(self.cancel_butt, 2, 2, 1, 1)
self.layout.addWidget(self.status, 3, 0, 5, 4)
self.cancel_butt.clicked.connect(self.close)
self.login_butt.clicked.connect(self.login)
self.setGeometry(0, 0, 400, 300)
self.setLayout(self.layout)
self.setWindowTitle("Login Panel")
self.show()
def login(self):
if len(self.server_ip.text()) == 0:
self.set_status("Empty server address")
return False
if len(self.server_port.text()) == 0:
self.set_status("Empty port number")
return False
if int(self.server_port.text()) not in range(1, 2**16):
self.set_status("Incorrect port number")
return False
if len(self.uname.text()) == 0:
self.set_status("Please input username")
return False
if len(self.passwd.text()) == 0:
self.set_status("Please input password")
return False
self.client.set_server(self.server_ip.text(),
int(self.server_port.text()))
if not self.client.connect():
self.set_status("Connection problems\n"
"Check server address and port or try again later")
return False
uname = self.uname.text()
passwd = crypto(self.passwd.text())
self.set_status("asdasd")
self.__logedin, response = self.client.log_in(uname, passwd)
if self.is_loged_in():
self.set_status("Success\n" + response.get_body())
self.uname.setParent(None)
self.passwd.setParent(None)
self.server_port.setParent(None)
self.server_ip.setParent(None)
self.login_butt.setParent(None)
self.cancel_butt.setParent(None)
self.status.setParent(None)
else:
self.set_status("Failed\n" + response.get_body())
# self.cancel_butt.click()
self.login_butt.setEnabled(False)
return self.is_loged_in()
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == QtCore.Qt.Key_Escape:
self.cancel_butt.animateClick()
elif QKeyEvent.key() == QtCore.Qt.Key_Return\
or QKeyEvent.key() == QtCore.Qt.Key_Enter:
self.login_butt.animateClick()
'''
class ClientGui(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.cent_widg = QtGui.QWidget(self)
self.setCentralWidget(self.cent_widg)
self.__cl = client.Client()
self.init_ui()
def init_ui(self):
self.setGeometry(200, 200, 800, 600)
self.show()
self.pop()
def pop(self):
self.login_popup = PopUpLogIn(self.__cl, self)
def closeEvent(self, QCloseEvent):
numWindows = len(widgetList)
if numWindows > 1:
self.run()
QCloseEvent.ignore()
else:
QCloseEvent.accept()
def run(self):
print("hahaha"*10)
'''
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
cl = client.Client()
chat = PopUpLogIn(cl)
sys.exit(app.exec_())