프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / DTI_PID / Extends / TcpServer.py @ 61ea0a61

이력 | 보기 | 이력해설 | 다운로드 (4.02 KB)

1
import sys
2
from PyQt5.QtCore import QByteArray, QDataStream, QIODevice
3
from PyQt5.QtWidgets import QApplication, QDialog
4
from PyQt5.QtNetwork import QHostAddress, QTcpServer
5

    
6

    
7
class TcpServer(QDialog):
8
    def __init__(self):
9
        super().__init__()
10
        self.tcpServer = None
11

    
12
    def sessionOpened(self):
13
        self.tcpServer = QTcpServer(self)
14
        PORT = 2549
15
        address = QHostAddress('127.0.0.1')
16
        if not self.tcpServer.listen(address, PORT):
17
            print("cant listen!")
18
            self.close()
19
            return
20
        self.tcpServer.newConnection.connect(self.dealCommunication)
21

    
22
    def sessionClosed(self):
23
        self.tcpServer.close()
24

    
25
    def dealCommunication(self):
26
        from App import App
27

    
28
        # Get a QTcpSocket from the QTcpServer
29
        clientConnection = self.tcpServer.nextPendingConnection()
30
        # wait until the connection is ready to read
31
        clientConnection.waitForReadyRead()
32
        # read incomming data
33
        instr = str(clientConnection.readAll().data(), encoding='utf-8')
34
        # TODO: do something
35
        mainWnd = App.mainWnd()
36
        data = instr
37
        if data:
38
            sData = data
39
            if sData != 'ProcessEnd':
40
                drawing_top = mainWnd.treeWidgetDrawingList.topLevelItem(0)
41
                count = drawing_top.childCount()
42
                for idx in range(count):
43
                    child = drawing_top.child(idx)
44
                    child.setCheckState(0, Qt.Unchecked)
45
                    drawing = child.data(Qt.UserRole, 0)
46
                    if drawing:
47
                        name = drawing.name
48
                        if sData == name.rsplit('.', 1)[0]:
49
                            self.open_image_drawing(drawing)
50
                            child.setCheckState(0, Qt.Checked)
51

    
52
                            HighLight()
53
            else:
54
                pass
55
        else:
56
            print('no data from')
57

    
58
        # get the connection ready for clean up
59
        clientConnection.disconnected.connect(clientConnection.deleteLater)
60

    
61
        # instantiate a QByteArray
62
        block = QByteArray()
63
        # QDataStream class provides serialization of binary data to a QIODevice
64
        out = QDataStream(block, QIODevice.ReadWrite)
65
        # We are using PyQt4 so set the QDataStream version accordingly.
66
        out.setVersion(QDataStream.Qt_5_0)
67
        # out.writeUInt15(0)
68
        # this is the message we will send it could come from a widget.
69
        '''
70
        if data and sData != 'ProcessEnd':
71
            message = "End".encode('utf-8')
72
        else:
73
            message = "Fail".encode('utf-8')
74
        '''
75
        message = "End".encode('utf-8')
76

    
77
        # get a byte array of the message encoded appropriately.
78
        # message = bytes(message, encoding='utf-8')
79
        # now use the QDataStream and write the byte array to it.
80
        out.writeString(message)
81
        # out.device().seek(-1)
82
        # out.writeUInt15(block.size() - 2)
83
        # now send the QByteArray.
84
        clientConnection.write(block)
85
        # now disconnect connection.
86
        clientConnection.disconnectFromHost()
87

    
88
        if data and sData == 'ProcessEnd':
89
            self.tcpServer.close()
90
            data_pane = mainWnd.ribbon.get_pane('Data')
91
            data_pane.ui.toolButtonPSN.setChecked(False)
92

    
93
        def HighLight():
94
            from App import App
95

    
96
            try:
97
                docData = AppDocData.instance()
98
                itemList = docData.selectView()
99
                text_items = [item for item in App.mainWnd().graphicsView.scene().items() 
100
                                if hasattr(item, 'uid') and str(item.uid) in itemList]
101

    
102
                if text_items:
103
                    from HighlightCommand import HighlightCommand
104
                    #for item in text_items:
105
                    #    HighlightCommand(self.graphicsView).executePSN(item)
106
                    HighlightCommand(App.mainWnd().graphicsView).executeForPSN(text_items)
107
                        
108
            finally:
109
                # 연결 모두 지움
110
                print("HighLight")