def new_campaign(self): dialog = QFileDialog(self) dialog.setDirectory(helper.one_up(self.current_dir)) dialog.setAcceptMode(QFileDialog.AcceptSave) dialog.setFileMode(QFileDialog.AnyFile) dialog.setOption(QFileDialog.ShowDirsOnly) dialog.setWindowTitle('Select folder name') if dialog.exec_(): directory = dialog.selectedFiles()[0] qdir = QDir(directory) if not qdir.exists(): self.current_dir = qdir.path() for folder_name in resource.folders: qdir.mkpath('./' + folder_name) helper.save_json_data( '{}/{}/health{}'.format(self.current_dir, resource.health_stat.folder, resource.health_stat.ext), resource.health_stat.default) qdir.mkpath('./.settings/std') qdir.mkpath('./.settings/debug') resource.create_config_files(self.current_dir, qdir.dirName()) self.refresh_tree_view() else: helper.display_error('Directory for campaign already exists.' ) # This shouldn't happen
def on_btnDir_dirName_clicked(self): self.__showBtnInfo(self.sender()) sous = self.ui.editDir.text() ## if sous=="": ## self.ui.textEdit.appendPlainText("请先选择一个目录") ## return dirObj = QDir(sous) #若sous为空,则使用其当前目录 text = dirObj.dirName() self.ui.textEdit.appendPlainText(text + "\n")
def on_qAction1_triggered(self): # 添加目录 dirStr = QFileDialog.getExistingDirectory() # if dirStr == "": # return parItem = self.ui.qTreeWidget.currentItem() if parItem == None: parItem = self.ui.qTreeWidget.topLevelItem(0) dirObj = QDir(dirStr) nodeText = dirObj.dirName() item = QtWidgets.QTreeWidgetItem(1002) item.setIcon(0, QIcon("../image/open3.bmp")) item.setText(0, nodeText) item.setText(1, "Group") item.setFlags(self.itemFlags) item.setData(0, Qt.UserRole, dirStr) parItem.addChild(item) parItem.setExpanded(True)
def on_action_OpenFolder_triggered(self): dirStr = QFileDialog.getExistingDirectory() if not dirStr: return parItem = self._ui.treeWidget.currentItem() if not parItem: parItem = self._ui.treeWidget.topLevelItem(0) icon = QIcon(':/icons/icons/open3.bmp') dirObj = QDir(dirStr) nodeText = dirObj.dirName() item = QTreeWidgetItem(TreeItemType.ItGroupItem.value) item.setIcon(TreeColNum.ColItem.value, icon) item.setText(TreeColNum.ColItem.value, nodeText) item.setText(TreeColNum.ColItemType.value, 'Group') item.setFlags(self.itemFlags) item.setCheckState(TreeColNum.ColItem.value, Qt.Checked) item.setData(TreeColNum.ColItem.value, Qt.UserRole, dirStr) parItem.addChild(item) parItem.setExpanded(True)
def on_actTree_AddFolder_triggered(self): ''' 向顶层节点内添加分组节点,分组节点的具体方式是文件夹 :return: ''' dirStr = QFileDialog.getExistingDirectory() #选择需要添加的目录,值是选择的文件夹的路径 # if dirStr == "":#是否选择路径,如果没有选中则系统默认选中程序所在的根目录 # return#从程序运行结果来看,该语句多余 parItem = self.ui.treeFiles.currentItem( ) #获得当前节点(选中的节点),选中则是PyQt5.QtWidgets.QTreeWidgetItem类型,未选中则为None if parItem == None: #如果没有选中节点,即鼠标未点击界面中的目录层级,则令程序选中最顶层节点 parItem = self.ui.treeFiles.topLevelItem(0) #1001 icon = QIcon(":/icons/images/open3.bmp") dirObj = QDir(dirStr) nodeText = dirObj.dirName() #最后一级目录的名称 ''' item = QTreeWidgetItem(TreeItemType.itGroupItem.value) 参数值为1002,即添加的是一个分组节点,这样设置的结果会导致在顶层节点下添加节点时, 无论选中哪一级的节点添加的都是1002参数的分组节点,即全部都是文件夹类型 item = QTreeWidgetItem(parItem.type()+1) ''' item = QTreeWidgetItem(TreeItemType.itGroupItem.value) item.setFlags(self.itemFlags) #设置状态 item.setIcon(TreeColNum.colItem.value, icon) #给节点列(第一列)添加图标 item.setIcon(TreeColNum.colItemType.value, icon) #给节点列(第一列)添加图标 item.setText(TreeColNum.colItem.value, nodeText) #给节点列添加名称 item.setText(TreeColNum.colItemType.value, "Group") #给节点类型列添加名称 item.setCheckState(TreeColNum.colItem.value, Qt.Checked) #添加一个多选框 item.setData(TreeColNum.colItem.value, Qt.UserRole, dirStr) parItem.addChild(item) #添加子节点 parItem.setExpanded(True) #展开模式
def on_actTree_AddFolder_triggered(self): dirStr = QFileDialog.getExistingDirectory() #选择目录 if (dirStr == ""): return parItem = self.ui.treeFiles.currentItem() #当前节点 if (parItem == None): parItem = self.ui.treeFiles.topLevelItem(0) icon = QIcon(":/images/icons/open3.bmp") dirObj = QDir(dirStr) #QDir对象 nodeText = dirObj.dirName() #最后一级目录的名称 item = QTreeWidgetItem(TreeItemType.itGroupItem.value) #节点类型 item.setIcon(TreeColNum.colItem.value, icon) item.setText(TreeColNum.colItem.value, nodeText) #第1列 item.setText(TreeColNum.colItemType.value, "Group") #第2列 item.setFlags(self.itemFlags) item.setCheckState(TreeColNum.colItem.value, Qt.Checked) item.setData(TreeColNum.colItem.value, Qt.UserRole, dirStr) #关联数据为目录全名 parItem.addChild(item) parItem.setExpanded(True) #展开节点
class LayoutBox(QVBoxLayout): def __init__(self): super().__init__() self.profileGroup = QGroupBox("Profile") self.profileLabel = QLabel("none") self.profileSelect = QPushButton("Open...") self.profileSelect.clicked.connect(self.openProfileClick) self.profileSave = QPushButton("Save") self.profileSave.clicked.connect(self.saveProfileClick) self.profileSave.setEnabled(False) self.profileBox = QHBoxLayout() self.profileBox.addWidget(self.profileLabel) self.profileBox.addStretch() self.profileBox.addWidget(self.profileSelect) self.profileBox.addWidget(self.profileSave) self.profileGroup.setLayout(self.profileBox) self.filesBox = FilesBox() self.filesBox.buttonBox.setEnable(False) # self.filesBox.availableTweaks.setEnabled(False) # self.filesBox.selectedTweaks.setEnabled(False) # self.filesBox.availableTweaks.itemClicked.connect(self.availClick) self.filesBox.selectedTweaks.itemClicked.connect(self.selectClick) self.descriptionGroup = QGroupBox("Tweak description") self.descriptionGroup.setMinimumHeight(50) self.description = QLabel("") self.description.setTextInteractionFlags(Qt.TextInteractionFlags(5)) self.descriptionBox = QHBoxLayout() self.descriptionBox.addWidget(self.description) self.descriptionGroup.setLayout(self.descriptionBox) self.addWidget(self.profileGroup) self.addItem(self.filesBox) self.addWidget(self.descriptionGroup) self.profileDir = QDir() self.profilePathDialog = QFileDialog() self.profilePath = "" self.appDir = QDir().current() def openProfileClick(self): #Button Open... click self.profilePath = self.profilePathDialog.getExistingDirectory( self.profileSelect, "Select Firefox profile") # self.profilePath = "~/Library/Application Support/Firefox/Profiles/rpk2uobe.default" logging.debug("Selected directory: \"%s\"", self.profilePath) if self.profilePath != "": self.selectedProfile = QDir(self.profilePath) self.firefoxDir = QDir(self.profilePath) logging.debug("Selected profile qdir: %s", self.selectedProfile.path()) self.firefoxDir.cdUp() self.profilesFile = QFile(self.firefoxDir.path() + "/profiles.ini") logging.debug("Profiles file: %s", self.profilesFile.fileName()) logging.debug("Profiles file exists: %s", self.profilesFile.exists()) logging.debug("Firefox folder: %s", self.firefoxDir.dirName()) # Basic check if parent directory is named 'firefox' and contains a file named 'profiles.ini' #if self.firefoxDir.dirName() == "firefox" and self.profilesFile.exists(): if True: self.profilePath = self.profilePath self.profileLabel.setText(self.profilePath) self.filesBox.buttonBox.setEnable(True) self.filesBox.availableTweaks.setEnabled(True) self.filesBox.selectedTweaks.setEnabled(True) self.profileSave.setEnabled(True) self.profileDir.setPath(self.profilePath) logging.debug("Profile dirs: %s", self.profileDir.entryList()) self.userChrome = QFile( self.profilePath + "/chrome/userChrome.css") self.userFFSettings = QFile( self.profilePath + "/chrome/userFirefox.json") logging.debug("userChrome exists: %s", self.userChrome.exists()) if self.userChrome.exists( ) and not self.userFFSettings.exists(): self.backupChrome = QMessageBox() self.backupChrome.setIcon(QMessageBox.Question) self.backupChrome.setText( "userChrome.css file already exists in this profile. This may be overwritten. Do you want to make a backup?" ) self.backupChrome.setStandardButtons( QMessageBox.StandardButtons(81920)) #yes, no self.backupChrome.exec() logging.debug("Dialog result: %s", self.backupChrome.result()) if self.backupChrome.result() == 16384: #yes logging.debug("Backing up userChrome") self.backupDone = QMessageBox() self.backupFile = QFile( self.profilePath + "/chrome/userChrome.css~") logging.debug("Backup file: %s", self.backupFile.fileName()) logging.debug("Backup exists: %s", self.backupFile.exists()) if self.backupFile.exists(): logging.debug("Backup already exists") self.backupDone.setIcon(QMessageBox.Warning) self.backupDone.setText( "Backup already exists. The file was NOT overwritten and new backup not made." ) else: if self.userChrome.copy(self.profilePath + "/chrome/userChrome.css~"): self.backupDone.setIcon( QMessageBox.Information) self.backupDone.setText( "Backed up to 'userChrome.css~'") else: self.backupDone.setIcon(QMessageBox.Critical) self.backupDone.setText("Backing up failed.") self.backupDone.exec() elif self.backupChrome.result() == 65536: #no logging.debug("Not backing up userChome") # Load existing settings try: with open(self.profilePath + "/chrome/userFirefox.json" ) as uFP: savedTweaks = json.load(uFP) logging.debug("Loaded json settings: %s", savedTweaks) for loadedTweak in savedTweaks: logging.debug( "Loaded tweak. check: %i, category: %s, name: %s", loadedTweak["Enabled"], loadedTweak["Category"], loadedTweak["Name"]) tweakCat = loadedTweak["Category"] tweakName = loadedTweak["Name"] self.filesBox.tweaksAdded[ tweakCat + tweakName] = QTreeWidgetItem( self.filesBox.selectedTweaks) self.filesBox.tweaksAdded[tweakCat + tweakName].setCheckState( 0, loadedTweak["Enabled"]) self.filesBox.tweaksAdded[tweakCat + tweakName].setText( 1, tweakCat) self.filesBox.tweaksAdded[tweakCat + tweakName].setText( 2, tweakName) except FileNotFoundError: pass self.filesBox.resizeColumns() else: self.noProfile = QMessageBox() self.noProfile.setIcon(QMessageBox.Warning) self.noProfile.setText( "The selected directory does not appear to be a valid Firefox profile. Profiles are usually located at '~/.mozilla/firefox/xxxx' and is most likely a hidden directory." ) self.noProfile.exec() def availClick(self): if self.filesBox.availableTweaks.currentItem().parent() is not None: tweakName = self.filesBox.availableTweaks.currentItem().text(0) tweakCat = self.filesBox.availableTweaks.currentItem().parent( ).text(0) self.showDesc(tweakCat, tweakName) def selectClick(self): try: tweakName = self.filesBox.selectedTweaks.currentItem().text(2) tweakCat = self.filesBox.selectedTweaks.currentItem().text(1) self.showDesc(tweakCat, tweakName) except: pass def showDesc(self, category, name): logging.debug("Showing description for %s/%s: ", category, name) currentDir = QDir().current().path() tweakPath = currentDir + "/" + category + "/" + name logging.debug("Loading %s", tweakPath) tweakDesc = "" descLines = 0 with open(tweakPath) as tweakContent: for line in tweakContent: if (re.search("^(\/\* ?)|( \* ?)|( \*\/)", line) is not None): if len(line) > 3: trimmedLine = line[3:] tweakDesc += trimmedLine descLines += 1 else: break logging.debug("Description: %s", tweakDesc) self.description.setText(tweakDesc) logging.debug("Desc lines: %i", descLines) def saveProfileClick(self): logging.debug("Saving profile") self.saveQuestion = QMessageBox() self.saveQuestion.setIcon(QMessageBox.Question) self.saveQuestion.setStandardButtons( QMessageBox.StandardButtons(81920)) #yes, no self.saveQuestion.setText( "Do you want to apply selected tweaks and overwrite userChrome?") self.saveQuestion.exec() self.saveCSS = "" if self.saveQuestion.result() == 16384: #yes logging.debug("Saving userChrome") selTweaksNumber = self.filesBox.selectedTweaks.topLevelItemCount() savingTweaks = [] for i in range(selTweaksNumber): currentSavingTweak = self.filesBox.selectedTweaks.topLevelItem( i) currentSavingData = {} currentSavingData["Enabled"] = currentSavingTweak.checkState(0) currentSavingData["Category"] = currentSavingTweak.text(1) currentSavingData["Name"] = currentSavingTweak.text(2) logging.debug("Tweak cat %s, name %s, selected %s", currentSavingData["Category"], currentSavingData["Name"], currentSavingData["Enabled"]) savingTweaks.append(currentSavingData) if currentSavingData["Enabled"] == 2: with open(self.appDir.path() + "/" + currentSavingData["Category"] + "/" + currentSavingData["Name"]) as twkFile: self.saveCSS += twkFile.read() + "\n" logging.debug("Selected tweaks: %s", savingTweaks) with open(self.profilePath + "/chrome/userFirefox.json", "w") as fp: json.dump(savingTweaks, fp) logging.debug("userChrome.css: %s", self.saveCSS) with open(self.profilePath + "/chrome/userChrome.css", "w") as fp: fp.write(self.saveCSS) elif self.saveQuestion.result() == 65536: #no logging.debug("Not saving userChrome.")