class settingsDlg(QDialog): def __init__(self, settings, parent=None): super(settingsDlg, self).__init__(parent) self.setAttribute( Qt.WA_DeleteOnClose) #dialog will be deleted rather than hidden self.settingsDialog = self self.settings = settings self.create_widgets() self.layout_widgets() self.dump_dirSettingFrame.hide() self.barSettingFrame.hide() self.create_connections() self.readSettingsData() self.setWindowTitle("yavol settings") def create_widgets(self): self.TableLabel1 = QLabel("Settings") self.ListOfSettings = QListWidget() self.buttonBox = QDialogButtonBox(QDialogButtonBox.Apply | QDialogButtonBox.Cancel | QDialogButtonBox.Ok) self.buttonBox.layout().setDirection(QBoxLayout.RightToLeft) #yara scan settings frame self.yaraSettingFrame = QFrame() self.dump_dirSettingFrame = QFrame() self.barSettingFrame = QFrame() self.labelRulesPath = QLabel('Path to YARA rules:') self.inputRulesPath = QLineEdit() self.labelFoo = QLabel('Path to dumps:') self.inputDumpDirPath = QLineEdit() self.labelBar = QLabel('Just BAR as usual') def layout_widgets(self): hLayoutButton = QHBoxLayout() hLayoutButton.addWidget(self.buttonBox) hLayoutButton.addStretch() vLayoutSettingsLeft = QVBoxLayout() vLayoutSettingsLeft.addWidget(self.TableLabel1) vLayoutSettingsLeft.addWidget(self.ListOfSettings) #yara setting frame layout frameLayout = QGridLayout() frameLayout.addWidget(self.labelRulesPath, 0, 0) frameLayout.addWidget(self.inputRulesPath, 0, 1) self.yaraSettingFrame.setLayout(frameLayout) #foo settings frame frameLayoutFoo = QGridLayout() frameLayoutFoo.addWidget(self.labelFoo, 0, 0) frameLayoutFoo.addWidget(self.inputDumpDirPath, 0, 1) self.dump_dirSettingFrame.setLayout(frameLayoutFoo) #bar settings frame frameLayoutBar = QVBoxLayout() frameLayoutBar.addWidget(self.labelBar) self.barSettingFrame.setLayout(frameLayoutBar) settingWindowsLayout = QGridLayout() settingWindowsLayout.addLayout(vLayoutSettingsLeft, 0, 0) settingWindowsLayout.addWidget(self.yaraSettingFrame, 0, 1) settingWindowsLayout.addWidget(self.dump_dirSettingFrame, 0, 1) settingWindowsLayout.addWidget(self.barSettingFrame, 0, 1) settingWindowsLayout.addLayout(hLayoutButton, 1, 0) ''' ################################################# <-| # vbox # vbox # | # listOption# yaraframe # | grid # # # | # # # | ################################################# | # vbox button # | ################################################# <-| ''' self.setLayout(settingWindowsLayout) def create_connections(self): self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.buttonBox.button(QDialogButtonBox.Apply).clicked.connect( self.apply) self.ListOfSettings.selectionModel().currentChanged.connect( self.setFrameVisibility) def accept(self): self.saveSettingsData() QDialog.accept(self) def apply(self): self.saveSettingsData() def setFrameVisibility(self, current, previous): if not previous.row() == -1: #hide previous frame and set current visible previous = str(self.ListOfSettings.item( previous.row()).text()) + "SettingFrame" previous = getattr(self, previous) previous.hide() #set the current visible current = str(self.ListOfSettings.item( current.row()).text()) + "SettingFrame" current = getattr(self, current) current.show() #print "Current: ", str(current.row()), self.ListOfSettings.item(current.row()).text() #print "Previous: ", str(previous.row()), self.ListOfSettings.item(previous.row()).text() #self.yaraSettingFrame.setVisible(False) def readSettingsData(self): settings = QSettings() settings_dict = settings.value('dictionary').toPyObject() # DEBUG #pp = pprint.PrettyPrinter(indent=4) #pp.pprint(settings_dict) for key in settings_dict: item = QListWidgetItem((QString("%1").arg(key))) self.ListOfSettings.addItem(item) if key == "yara": #set yara option to be 'pre-selected' self.ListOfSettings.setItemSelected(item, True) path_to_rules = settings_dict[QString('yara')][QString( 'rules_dir')][QString('path')] self.inputRulesPath.setText(path_to_rules) if key == "dump_dir": path_to_dump = settings_dict[QString('dump_dir')] self.inputDumpDirPath.setText(path_to_dump) def saveSettingsData(self): settings = QSettings() #get values of yara setting path_to_rules = self.inputRulesPath.text() #get value of the dump_dir input path_to_dump_dir = self.inputDumpDirPath.text() settings.setValue( 'dictionary', { 'yara': { 'rules_dir': { 'path': path_to_rules } }, 'dump_dir': path_to_dump_dir, 'bar': 2 })
class yarascanDlg(QDialog): def __init__(self, path_to_rules_dir, parent=None): super(yarascanDlg, self).__init__(parent) self.setAttribute( Qt.WA_DeleteOnClose) #dialog will be deleted rather than hidden self.rules_dir = path_to_rules_dir self.create_widgets() self.layout_widgets() self.create_connections() self.setWindowTitle("Select rules for scan") self.setFixedSize(800, 600) self.selected_rules = [] #empty set for rule's names def create_widgets(self): self.label1 = QLabel('Available rules') self.label2 = QLabel('Scan with') self.listWidget1 = CustListWidget(self) self.listWidget1.setAcceptDrops(True) self.listWidget1.setDragEnabled(True) self.listWidget1.setSelectionMode(QAbstractItemView.ExtendedSelection) for rule in sorted(os.listdir(self.rules_dir)): if rule.endswith(".yar"): item = QListWidgetItem(rule.split(".")[0]) self.listWidget1.addItem(item) self.listWidget2 = CustListWidget(self) self.listWidget2.setAcceptDrops(True) self.listWidget2.setDragEnabled(True) self.button1 = QPushButton(">") self.button1.setFixedWidth(40) self.button2 = QPushButton("<") self.button2.setFixedWidth(40) self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.buttonBox.layout().setDirection(QBoxLayout.RightToLeft) self.spacer = QSpacerItem(600, 0, QSizePolicy.Expanding, QSizePolicy.Minimum) def create_connections(self): self.button1.clicked.connect(partial(self.moveCurrentItems, 'button1')) self.button2.clicked.connect(partial(self.moveCurrentItems, 'button2')) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) def layout_widgets(self): vLayout1 = QVBoxLayout() vLayout1.addWidget(self.label1) vLayout1.addWidget(self.listWidget1) vLayout2 = QVBoxLayout() vLayout2.addWidget(self.button1) vLayout2.addWidget(self.button2) vLayout3 = QVBoxLayout() vLayout3.addWidget(self.label2) vLayout3.addWidget(self.listWidget2) hLayout1 = QHBoxLayout() hLayout1.addLayout(vLayout1) hLayout1.addLayout(vLayout2) hLayout1.addLayout(vLayout3) hButtonLayout = QHBoxLayout() hButtonLayout.addItem(self.spacer) hButtonLayout.addWidget(self.buttonBox) hFinalLayout = QGridLayout() hFinalLayout.addLayout(hLayout1, 0, 0) hFinalLayout.addLayout(hButtonLayout, 1, 0) self.setLayout(hFinalLayout) def moveCurrentItems(self, source): if source == 'button1': selected_all = self.listWidget1.selectedItems() else: selected_all = self.listWidget2.selectedItems() for selected_item in selected_all: if source == 'button1': self.listWidget1.takeItem(self.listWidget1.row(selected_item)) self.listWidget2.addItem(selected_item) else: self.listWidget2.takeItem(self.listWidget2.row(selected_item)) self.listWidget1.addItem(selected_item) def accept(self): #get the list of items in listWidget2 self.listWidget2.count() for i in range(self.listWidget2.count()): self.selected_rules.append(str(self.listWidget2.item(i).text())) QDialog.accept(self)