def load_playlist(self): """ファイルを開いて、タブを追加する""" url, ok = QFileDialog.getOpenFileUrl(self, 'open a playlist files', filter='*.m3u *.m3u8') if not ok: return file_name = url.fileName() file_name = file_name[:file_name.find( '.')] # remove extension from file_name if self.is_used(file_name): return new_playlist = self.create_new() self.addTab(new_playlist, file_name) new_playlist.load(url.toLocalFile()) self.setCurrentIndex(self.count() - 1)
def file_chooser_open_file(parent, f_filter=None): dialog = QFileDialog(parent) url = dialog.getOpenFileUrl(filter=f_filter) return url
def action_triggered(self): sender = self.sender() if sender == self.ui.action_gen: narray = genTable2array() self.narray = narray self.setArray(narray) v = '(无解)' if checkIfSolvable(narray): v = '(有解)' self.ui.statusbar.showMessage('预计Cost为:' + str(calc_h(self.narray)) + v, 5000) elif sender == self.ui.action_solve: # 判断是否有解 cnt = 0 narray_tmp = np.copy(self.narray).reshape(1, 16) posy = int(np.where(narray_tmp == 0)[1][0]) narray_tmp = np.delete(narray_tmp, posy) length = narray_tmp.shape[0] for i in range(length): for p in range(i + 1, length): if narray_tmp[i] > narray_tmp[p]: cnt = cnt + 1 if cnt % 2 == 0: # inversion为偶数,另外还需要空白在从下往上数第奇数行 if int(self.narray.shape[0] - np.where(self.narray == 0)[0][0]) % 2 != 0: self.startSolve() else: self.fail() else: # inversion为奇数,另外还需要空白在从下往上数第偶数行 if int(self.narray.shape[0] - np.where(self.narray == 0)[0][0]) % 2 == 0: self.startSolve() else: self.fail() elif sender == self.ui.action_load: fileDialog = QFileDialog() fileDialog.setDirectory('.') # fileDialog.setFilter("15-Puzzle Data File(*.data)") url = fileDialog.getOpenFileUrl(filter='15-Puzzle Data File(*.data)') # 载入data narray_backup = np.copy(self.narray) if url != None: path = url[0].path() try: if os.path.exists(os.path.dirname(path)): narray = np.loadtxt(path).astype(int) self.setArray(narray) self.ui.statusbar.showMessage('预计Cost为:' + str(calc_h(self.narray)), 5000) else: self.ui.statusbar.showMessage('载入失败', 3000) except: message = QMessageBox() message.setText(path + ' 不是一个有效的文件') message.exec_() self.narray = narray_backup elif sender == self.ui.action_storeLayout: self.saveLayout() pass elif sender == self.ui.action_storeResult: # 保存当前状态 self.saveResult()