Exemple #1
0
class Introduction(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def Style(self, path):
        # load css
        with open(path, "r") as fh:
            self.setStyleSheet(fh.read())

    def initUI(self):
        self.Style("./css/Aqua.qss")
        # set icon
        self.setWindowIcon(QIcon('./images/icon.ico'))

        # Actions
        # Open Data from Excel Action
        self.actOpen = QAction(QIcon('./images/open.ico'), "Ochish", self)
        self.actOpen.setShortcut("Ctrl+O")
        self.actOpen.triggered.connect(self.openFile)

        # centeral layout
        self.vbox = QVBoxLayout(self)

        self.vbox.setAlignment(Qt.AlignTop)

        # Name of app
        lbl = QLabel(
            'Yaqin qo‘shni usuli algortimi asosida o‘rgatuvchi tanlamani saralash'
        )
        lbl.setWordWrap(True)
        from PyQt5.QtWidgets import QStyle
        #lbl.setStyle(QStyle=QStyle("font-size: 28px;"))

        self.vbox.addChildWidget(lbl)

        # Select file
        hbox = QHBoxLayout()
        self.edit = QLineEdit()
        self.edit.setEnabled(False)
        hbox.addWidget(self.edit)

        # Browise button
        btnBrowse = QPushButton("Fayl")
        btnBrowse.clicked.connect(self.openFile)
        hbox.addWidget(btnBrowse)
        self.vbox.addLayout(hbox)

        btnOpenMain = QPushButton("Ochish")
        btnOpenMain.clicked.connect(self.openMain)
        self.vbox.addWidget(btnOpenMain)
        self.vbox.addStretch(4)

        # set properteis of window
        self.setWindowTitle("Yaqin qo‘shni usuli")
        self.setGeometry(300, 300, 640, 400)

    def colnum_string(self, n):
        string = ""
        while n > 0:
            n, remainder = divmod(n - 1, 26)
            string = chr(65 + remainder) + string
        return string

    def openMain(self):
        try:
            self.window = MainWindow(fpath=self.fnames[0])
            self.window.showMaximized()
        except Exception as exc:
            QMessageBox.about(self, "Hisoblashda xatolik bor: ", str(exc))

    def openFile(self):
        try:
            # open file dialog
            self.fnames = QFileDialog().getOpenFileName(
                self, 'Fayl ochish', '/Home')
            if len(self.fnames[0]) == 0:
                return
            self.edit.setText(self.fnames[0])
        except Exception as exc:
            print(exc)
            QMessageBox.about(self, "Fayl ochishda xatolik bor", str(exc))
Exemple #2
0
class MapBox(QWidget):
    def __init__(self, message_box):
        super().__init__()
        # 定义布局
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        # 定义地图
        self.map = QLabel()
        self.init_map()

        # 定义无人机
        self.uvas = []
        self.init_uvas()

        # 消息
        self.message_box = message_box

        self.rw = 0
        self.rh = 0
        self.start_point = -1
        self.end_point = -1
        self.pass_point = []
        self.locations = []
        self.locations_button = []
        self.uva_num = 1

    def resizeEvent(self, a0: QResizeEvent):
        self.update_map()
        self.update_location()

    def init_map(self):
        self.map.setObjectName("none")
        self.map.setScaledContents(True)
        self.map.resize(self.width(), self.height())
        self.layout.addChildWidget(self.map)

    def init_uvas(self):
        for i in range(10):
            r = random.randint(100, 255)
            g = random.randint(100, 255)
            b = random.randint(100, 255)
            self.uvas.append(
                UVA([(500, 500), (10, 10), (r, g)],
                    self,
                    pen=QPen(QColor(r, g, b), 2, Qt.DashLine)))
            self.layout.addChildWidget(self.uvas[-1])
            self.uvas[-1].hide()

    def init_location(self):
        self.locations.clear()
        self.locations_button.clear()

        tp = json.load(open("./conf.json", "r"))

        name = tp["map"]

        with open("./maps/" + name + ".txt", "r") as f:
            n = 0
            for row in f:
                x, y = row.split()
                x = float(x)
                y = float(y)
                if n == 0:
                    self.rw = x
                    self.rh = y
                else:
                    self.locations.append((x, y))
                n += 1

        for i in range(len(self.locations)):
            location_button = LocationButton(str(i + 1), 0, 0, self, "none")
            location_button.setIcon(location_button.icon_black)
            self.locations_button.append(location_button)

    def load_map(self, file):
        l = file.rfind("NJU")
        r = file.rfind("-")
        self.map_name = file[l:r]
        self.map_height = file[r + 1]
        self.map_path = file[:r]
        self.clear()
        conf = json.load(open("./conf.json", "r"))
        conf["map"] = self.map_name
        conf["height"] = self.map_height
        json.dump(conf, open("./conf.json", "w"))
        self.map.setPixmap(QPixmap(file))
        self.map.setObjectName(file)
        self.show_map()

    def show_map(self):
        if self.map.objectName() == "none":
            self.message_box.append("未加载地图")
            return False
        else:
            self.map.show()
            return True

    def hide_map(self):
        self.map.hide()

    def update_map(self, h=None):
        if h is not None:
            self.map_height = h
            print(self.map_path + "-" + h + ".png")
            self.map.setPixmap(QPixmap(self.map_path + "-" + h + ".png"))
        self.map.resize(self.width(), self.height())

    def update_location(self):
        w = self.width()
        h = self.height()
        self.map.resize(w, h)
        pts = []
        for i in range(len(self.locations)):
            px = int(self.locations[i][0] * w / self.rw)
            py = int((self.rh - self.locations[i][1]) * h / self.rh)
            self.locations_button[i].move(px, py)
            pts.append((px, h - py))
        # self.locations = list(pts)
        # self.rw = w
        # self.rh = h

    def show_location(self, kind):
        self.update_location()
        for i in range(len(self.locations)):
            self.locations_button[i].kind = kind
            self.locations_button[i].show()

    def hide_location(self):
        for i in range(len(self.locations)):
            self.locations_button[i].kind = "none"
            self.locations_button[i].hide()

    def plan_path(self):
        self.hide_location()
        self.update_location()
        if self.start_point < 0:
            self.message_box.append("规划失败,未设置起点!!")
            return
        if self.end_point < 0:
            self.message_box.append("未设置终点,默认为起点!!")
            self.end_point = self.start_point

        tp = set(self.pass_point)
        self.pass_point = []
        for pid in tp:
            if pid < 0:
                continue
            if pid == self.start_point or pid == self.end_point:
                continue
            self.pass_point.append(pid)

        # 显示选中点
        self.locations_button[self.start_point].setIcon(
            self.locations_button[self.start_point].icon_blue)
        self.locations_button[self.start_point].show()

        self.locations_button[self.end_point].setIcon(
            self.locations_button[self.start_point].icon_purple)
        self.locations_button[self.end_point].show()

        for i in range(len(self.pass_point)):
            pid = self.pass_point[i]
            self.locations_button[pid].setIcon(
                self.locations_button[pid].icon_green)
            self.locations_button[pid].show()

        # 规划路径
        self.message_box.append("路径规划中...")

        conf = json.load(open("./conf.json", "r"))
        conf["start_location"] = self.start_point
        conf["end_location"] = self.end_point
        conf["pass_location"] = [pid for pid in self.pass_point]
        json.dump(conf, open("./conf.json", "w"))

        print("start_location:", conf["start_location"])
        print("pass_location:", conf["pass_location"])
        print("map_height:", self.map_height)
        print("uva_num:", self.uva_num)
        paths = plan_route(conf["start_location"], conf["pass_location"],
                           int(self.map_height), int(self.uva_num))
        print(paths)

        # 读入规划路线
        #paths = [[self.start_point] + self.pass_point + [self.end_point], [0, 1, 2, 3, 4]]
        #  = [[0, 5, 6, 7], [0, 1, 2, 3]]
        # paths = [[0, 5, 6, 7]]
        t = random.random() * 60
        # time.sleep(t)
        self.message_box.append("路径规划完成,一共耗时%.2f秒,规划结果共需要%d架无人机" %
                                (t, len(paths)))

        # 显示规划路径
        for i in range(len(paths)):
            self.uvas[i].update_locations(
                [(self.rw, self.rh)] +
                [self.locations[paths[i][j]] for j in range(len(paths[i]))])
            self.uvas[i].show()

    def run_path(self):
        for i in range(len(self.uvas)):
            self.uvas[i].run()

    def clear(self):
        # self.hide_map()
        self.hide_location()
        for x in self.uvas:
            x.hide()