Esempio n. 1
0
 def on_number_button_clicked(self, num):
     self.selectedNum = num
     for btn in self.numBtns:
         btn['state'] = "normal"
     self.numBtns[num]['state'] = "disabled"
     evtDispatcher.dispatch(EvtIds.EVT_INFORM_NUMBER_CHANGED,
                            {"number": self.selectedNum})
Esempio n. 2
0
 def handle_save_project(self, content):
     if content is None or content["filepath"] is None: return
     mapData = MapData(self.row, self.col, deepcopy(self.gridWeights),
                       deepcopy(self.imgBytes))
     DataParser().save_to_file(content["filepath"], mapData)
     evtDispatcher.dispatch(EvtIds.EVT_CONTENT_CHANGED,
                            {"isChanged": False})
Esempio n. 3
0
 def create_project_ok(self):
     filepath = self.projPath.get() + PROJECT_SUFFIX
     if os.path.exists(filepath):
         messagebox.showerror("Error", "project exists")
     else:
         open(filepath, "wb").close()
     self.close_new_project_panel()
     evtDispatcher.dispatch(EvtIds.EVT_GET_PROJECT_NAME, {"path": filepath})
     evtDispatcher.dispatch(EvtIds.EVT_PROJECT_CREATED, None)
Esempio n. 4
0
 def on_load_image_clicked(self):
     filename = filedialog.askopenfilename(initialdir=".",
                                           title="Select a picture",
                                           filetypes=(("png files",
                                                       "*.png"),
                                                      ("all files", "*.*")))
     if filename and len(filename) > 0:
         evtDispatcher.dispatch(EvtIds.EVT_INFORM_LOAD_PICTURE,
                                {"imgpath": filename})
Esempio n. 5
0
 def handle_load_image(self, content):
     if content is None or content["imgpath"] is None: return
     self.clear_all_numbers()
     self.clear_path()
     self.clear_grid_lines()
     self.show_background_image(content["imgpath"])
     self.row, self.col = 0, 0
     self.gridWeights = []
     evtDispatcher.dispatch(EvtIds.EVT_CONTENT_CHANGED, {"isChanged": True})
Esempio n. 6
0
 def on_ok_button_clicked(self):
     try:
         row = int(self.strRow.get())
         col = int(self.strCol.get())
         if row > 0 and col > 0:
             self.row = row
             self.col = col
             evtDispatcher.dispatch(EvtIds.EVT_INFORM_TO_DRAW_GRIDS, {
                 "row": self.row,
                 "col": self.col
             })
     except Exception as e:
         print(str(e))
Esempio n. 7
0
 def on_erase_grid(self, event):
     if not self.is_click_enabled(): return
     if self.isCalcDistanceMode:
         pass
     else:
         row, col = math.floor(event.y / self.heightPerGrid), math.floor(
             event.x / self.widthPerGrid)
         offset = int(row * self.col + col)
         if offset >= len(self.gridWeights
                          ) or self.gridWeights[offset] == EMPTY_NUMBER:
             return
         self.delete("num(%d,%d)" % (row, col))
         evtDispatcher.dispatch(EvtIds.EVT_CONTENT_CHANGED,
                                {"isChanged": True})
 def handle_second_position_selected(self, content):
     if content is None or content["row"] is None or content["col"] is None:
         return
     self.lblEndPos.config(text="POS2: (%d, %d)" %
                           (content["col"], content["row"]))
     self.lblDistance.config(text="calculating...")
     self.update()
     evtDispatcher.dispatch(
         EvtIds.EVT_INFORM_CALC_DISTANCE, {
             "row1": self.firstPosRow,
             "col1": self.firstPosCol,
             "row2": content["row"],
             "col2": content["col"],
             "isCriticalShortest": self.isCriticalShortest.get()
         })
Esempio n. 9
0
 def on_grid_clicked(self, event):
     if not self.is_click_enabled(): return
     col = math.floor(event.x / self.widthPerGrid)
     row = math.floor(event.y / self.heightPerGrid)
     if self.isCalcDistanceMode:
         y = row * self.heightPerGrid + self.heightPerGrid / 2
         x = col * self.widthPerGrid + self.widthPerGrid / 2
         if not self.isFirstPosSelected:
             self.clear_path()
             self.create_text((x, y), text='@', fill="red", tag=PATH_TAG)
             evtDispatcher.dispatch(EvtIds.EVT_FIRST_POSITION_SELECTED, {
                 "row": row,
                 "col": col
             })
             self.isFirstPosSelected = True
             return
         self.isFirstPosSelected = False
         self.create_text((x, y), text='@', fill="red", tag=PATH_TAG)
         evtDispatcher.dispatch(EvtIds.EVT_SECOND_POSITION_SELECTED, {
             "row": row,
             "col": col
         })
     elif self.put_number_in_grid(self.num, row, col):
         evtDispatcher.dispatch(EvtIds.EVT_CONTENT_CHANGED,
                                {"isChanged": True})
Esempio n. 10
0
 def handle_calc_distance(self, content):
     if (content is None or content["row1"] is None
             or content["col1"] is None or content["row2"] is None
             or content["col2"] is None
             or content["isCriticalShortest"] is None):
         return
     mapPath = None
     if content["isCriticalShortest"]:
         mapPath = PathCalculator(self.gridWeights, self.row,
                                  self.col).get_the_shortest_path(
                                      content["row1"], content["col1"],
                                      content["row2"], content["col2"])
     else:
         mapPath = PathCalculator(self.gridWeights, self.row,
                                  self.col).get_maybe_shortest_path(
                                      content["row1"], content["col1"],
                                      content["row2"], content["col2"])
     if mapPath is not None:
         self.draw_path_nodes(mapPath.nodes)
     evtDispatcher.dispatch(
         EvtIds.EVT_SHORTEST_DISTANCE_CALCULATED, {
             "distance":
             mapPath.distance if mapPath is not None else UNREACH_DISTANCE
         })
Esempio n. 11
0
 def open_project(self):
     filename = filedialog.askopenfilename(initialdir=".",
                                           title="Select project",
                                           filetypes=(("mg files",
                                                       "*.mg"), ))
     mapdata = DataParser().parse_from_file(filename)
     if mapdata is None:
         messagebox.showerror("error", "open project error!")
         return
     evtDispatcher.dispatch(EvtIds.EVT_INFORM_GET_MAP_DATA,
                            {"mapdata": deepcopy(mapdata)})
     evtDispatcher.dispatch(EvtIds.EVT_GET_PROJECT_NAME, {"path": filename})
     evtDispatcher.dispatch(EvtIds.EVT_PROJECT_LOADED, None)
Esempio n. 12
0
 def on_save_button_clicked(self):
     evtDispatcher.dispatch(EvtIds.EVT_INFORM_SAVE_PROJECT,
                            {"filepath": self.parent.projectPath})
Esempio n. 13
0
 def on_clear_all_clicked(self):
     evtDispatcher.dispatch(EvtIds.EVT_INFORM_CLEAR_NUMBERS, None)
     evtDispatcher.dispatch(EvtIds.EVT_INFORM_CLEAR_PATH, None)
Esempio n. 14
0
 def handle_clear_numbers(self, content):
     self.clear_all_numbers()
     self.gridWeights = [EMPTY_NUMBER for _ in range(self.row * self.col)]
     evtDispatcher.dispatch(EvtIds.EVT_CONTENT_CHANGED, {"isChanged": True})
Esempio n. 15
0
 def handle_draw_grids(self, content):
     if content is None or content["row"] is None or content["col"] is None:
         return
     self.draw_grid(content["row"], content["col"])
     evtDispatcher.dispatch(EvtIds.EVT_CONTENT_CHANGED, {"isChanged": True})
 def on_calc_distance_mode_clicked(self):
     if not self.isCalcDistanceMode.get():
         self.clean()
         self.cbCriticalShortest.deselect()
     evtDispatcher.dispatch(EvtIds.EVT_SET_CALC_DISTANCE_MODE,
                            {"mode": self.isCalcDistanceMode.get()})