# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets
import random
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from pandas import DataFrame
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider,
QVBoxLayout, QApplication)
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

#ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
#맵에 Label을 각 좌표마다 넣어 해당 위치정보를 얻을려면 Text를 split으로 해서 알아야 한다. delemmiter = ','
#ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

# 고객 class
class Customer:
    def __init__(self, cus_num, start_x, start_y, dest_x, dest_y, share, people_num, hour, minute, second, state):
        self.cus_num = cus_num
        self.start_x = start_x
        self.start_y = start_y
        self.dest_x = dest_x
        self.dest_y = dest_y
        self.share = share
        self.people_num = people_num
        self.hour = hour
        self.minute = minute
        self.second = second
        self.state = state

    def get_cus_num(self):
        return self.cus_num

    def get_start_position(self):
        return int(float(self.start_x)), int(float(self.start_y))

    def get_dest_position(self):
        return self.dest_x, self.dest_y

    def get_share(self):
        return self.share

    def get_people_num(self):
        return self.people_num

    def get_time(self):
        return self.hour, self.minute, self.second

    def get_state(self):
        return self.state
# 고객 발생 시나리오
def call_scenario(Customer):
    customers = []
    cus_num = 0
    hour = 9
    minute = 0
    second = 0
    state = 0

    # 9시 ~ 5시 랜덤 고객 발생
    for i in range(28800):  # 8시간 = 28800초

        if second == 60:
            minute += 1
            second = 0

        if minute == 60:
            hour += 1
            minute = 0

        if hour == 13:
            hour = 1

        # 9am, 2pm 랜덤 고객 발생
        if (hour == 9 or hour == 2) and (random.randrange(1, 1001) <= 30):
            cus_num += 1
            start_x, start_y, dest_x, dest_y, share, people_num = random_customer()
            customers.append(
                Customer(cus_num, start_x, start_y, dest_x, dest_y, share, people_num, hour, minute, second, state))

        # 10am ~ 1pm, 3pm ~ 5pm 랜덤 고객 발생
        if (hour != 9 and hour != 2) and (random.randrange(1, 1001) <= 10):
            cus_num += 1
            start_x, start_y, dest_x, dest_y, share, people_num = random_customer()
            customers.append(
                Customer(cus_num, start_x, start_y, dest_x, dest_y, share, people_num, hour, minute, second, state))

        second += 1

    # end for

    # 결과 출력


    # 총 8시간 평균 432건
    # 1시간당 평균 36건 / 9시, 2시 시간당 평균 108건
    return customers
# 랜덤한 고객 정보 생성
def random_customer():
    start_x = random.randint(1, 13)
    start_y = random.randint(1, 11)
    dest_x = random.randint(1, 13)
    dest_y = random.randint(1, 11)
    share = random.randrange(2)

    # 탑승인원 1명 70% / 2명 20% / 3명 10% 확률로 발생
    temp_people_num = random.randrange(1, 11)
    if temp_people_num <= 7:
        people_num = 1
    elif 8 <= temp_people_num <= 9:
        people_num = 2
    else:
        people_num = 3

    return start_x, start_y, dest_x, dest_y, share, people_num
#데이터 UI QTableView에 표시모델 dataframe -> QTableView
class pandasModel(QAbstractTableModel):
    def __init__(self, data):
        QAbstractTableModel.__init__(self)
        self._data = data

    def rowCount(self, parent=None):
        return self._data.shape[0]

    def columnCount(self, parnet=None):
        return self._data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self._data.columns[col]
        return None
class Ui_Dialog(QDialog):
    def setupUi(self):
        #맵 크기 설정 및 세팅

        self.size = 15
        self.hour = 9
        self.min = 0
        self.second = 0
        self.personlist = 0 # 사용자가 등록되어 있는 수 count
        self.customer_list = call_scenario(Customer)
        # UI 구성------------------------------------------------------------------------
        Dialog.setObjectName("Dialog")
        Dialog.resize(1401, 555)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Dialog.sizePolicy().hasHeightForWidth())
        Dialog.setSizePolicy(sizePolicy)
        self.horizontalLayoutWidget = QtWidgets.QWidget(Dialog)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 1401, 531))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.horizontalLayout.addLayout(self.gridLayout)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
        self.verticalLayout.setObjectName("verticalLayout")
        self.gridLayout_2 = QtWidgets.QGridLayout()
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.label.setObjectName("label")
        self.gridLayout_2.addWidget(self.label, 1, 0, 1, 1)
        self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.pushButton.setObjectName("pushButton")

        self.gridLayout_2.addWidget(self.pushButton, 0, 1, 1, 1)
        self.label_2 = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.pushButton.clicked.connect(self.plus1)

        self.gridLayout_2.addWidget(self.label_2, 0, 0, 1, 1)
        self.verticalLayout.addLayout(self.gridLayout_2)
        self.tableView = QtWidgets.QTableView(self.horizontalLayoutWidget)
        self.tableView.setObjectName("tableView")
        #table header구성
        self.df = DataFrame({"고객번호":[],"출발x":[],"출발y":[],"도착x":[],"도착y":[],"합승여부":[],"탑승인원":[],"호출시각":[],"현재상태":[]})
        self.model1 = pandasModel(self.df)
        self.tableView.setModel(self.model1)
        header = self.tableView.horizontalHeader()
        # table header width 설정
        for i in range(9):
            header.setSectionResizeMode(i, QtWidgets.QHeaderView.ResizeToContents)

        self.verticalLayout.addWidget(self.tableView)
        self.horizontalLayout.addLayout(self.verticalLayout)
        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)
        # UI 구성------------------------------------------------------------------------

        #buttons은 label들의 list, 위치정보를 텍스트로 담고있다.
        self.buttons = {}
        self.create_map()

    # 지역 비활성화 state = 0
    def deactivate(self, x, y):
        self.buttons[(x, y)].setText("(%d,%d)" % (random.randint(1, 9), 0))
        self.buttons[(x, y)].setStyleSheet(
                "border-color: rgb(0, 0, 0); border-width : 1.2px; border-style:inset;background-color: rgb(222, 104, 104);")

    # 지역에 아무도 없을 때 state = 1
    def empty(self, x, y):
        temp = self.buttons[(x, y)].text().replace("(", "").replace(")", "").split(",")
        self.buttons[(x, y)].setText("(%d,%d)" % (int(temp[0]), 1))
        self.buttons[(x, y)].setStyleSheet(
            "border-color: rgb(0, 0, 0); border-width : 1.2px; border-style:inset;background-color: rgb(200, 200, 200);")

    # 지역에 사람이 있을 때 state = 2
    def person_here(self, x, y):
        self.buttons[(x, y)].setStyleSheet("border-color: rgb(0, 0, 0); border-width : 1.2px; border-style:inset; background-color: rgb(138, 139, 221);")
        temp = self.buttons[(x, y)].text().replace("(", "").replace(")", "").split(",")
        self.buttons[(x, y)].setText("(%d,%d)" % (int(temp[0]),2))

    # 지역에 자동차 있을 때 state = 3
    def car_here(self, x, y):
        self.buttons[(x, y)].setStyleSheet(
                "border-color: rgb(0, 0, 0); border-width : 1.2px; border-style:inset;background-color: rgb(204, 191, 255);")
        temp = self.buttons[(x, y)].text().replace("(", "").replace(")", "").split(",")
        self.buttons[(x, y)].setText("(%d,%d)" % (int(temp[0]), 3))

    #지역에 자동차가 사람을 태웠을 때 state = 4
    def carperson_here(self, x, y):
        self.buttons[(x, y)].setStyleSheet(
                "border-color: rgb(0, 0, 0); border-width : 1.2px; border-style:inset;background-color: rgb(164, 139, 255);")
        temp = self.buttons[(x, y)].text().replace("(", "").replace(")", "").split(",")
        self.buttons[(x, y)].setText("(%d,%d)" % (int(temp[0]), 4))

    def create_map(self):
        temp2 = "border-color: rgb(0, 0, 0); border-width : 1.2px; border-style:inset;"
        for i in range (self.size):
            for j in range (self.size):
                a=1
                temp1 =""
                if a == 0:
                    temp1 = "background-color: rgb(222, 104, 104);"
                else:
                    a=1
                    temp1 = "background-color: rgb(200, 200, 200);"
                self.buttons[(i,j)] = QtWidgets.QLabel('(%d,%d)' % (random.randint(1,9),a))
                self.buttons[(i,j)].setStyleSheet(temp1+temp2) #buttons의 색깔을 변환해준다.
                self.gridLayout.addWidget(self.buttons[(i,j)],i,j)
        #비활성화 위치
        self.deactivate(1, 1)
        self.deactivate(2, 5)
        self.deactivate(4, 5)
        self.deactivate(6, 2)
        self.deactivate(7, 9)

        #자동차 위치
        self.car_here(2, 2)
        self.car_here(3, 8)
        self.car_here(5, 4)

    #1초씩 버튼누를 때 마다 추가.
    def plus1(self):
        self.second += 1
        if self.second == 60:
            self.min += 1
            self.second = 0
        if self.min == 60:
            self.hour += 1
            self.min = 0
        self.label_2.setText("시간 : "+repr(self.hour) +":"+repr(self.min)+":"+repr(self.second))

        self.a,self.b,self.c=self.customer_list[self.personlist].get_time()

        #시간이 생성되어있던 call list와 같을 때
        if self.a == self.hour and self.b == self.min and self.c == self.second:
            d = self.customer_list[self.personlist]
            e, f =d.get_start_position()
            g, h =d.get_dest_position()

            #dataframe에 사용자 추가 및 table에 업데이트
            self.df.loc[self.personlist] = [d.get_cus_num(),e,f,g,h,d.get_share(),d.get_people_num(),repr(self.hour) +":"+repr(self.min)+":"+repr(self.second),d.get_state()]
            self.personlist+=1
            self.model1 = pandasModel(self.df)
            self.tableView.setModel(self.model1)
            #맵에 사용자 위치 표시
            self.person_here(e,f)
    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "자동차수 : "))
        self.pushButton.setText(_translate("Dialog", "시간 + 1"))
        self.label_2.setText(_translate("Dialog", "시간 : "+repr(self.hour) +":"+repr(self.min)))



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi()
    Dialog.show()
    sys.exit(app.exec_())

시간버튼 누를 시, second 단위로 +1

+ Recent posts