class MultipleFilesWidget(QWidget): """Widget that allows the selection of multiple files.""" def __init__(self, title, file_name_filter='', check_file_function=None): """Creates a new widget for selecting multiple files. - `title`: title of the file selection dialog that is opened when the user clicks on 'Add File'. - `file_name_filter`: filter to use for the selection of files (See the documentation of QFileDialog). - `check_file_function`: function that receives a file name and returns True if its contents are correct. If None, files are not checked. An error dialog is shown for the files that are not correct. The rest are just added. """ super(MultipleFilesWidget, self).__init__() self.title = title self.file_name_filter = file_name_filter self._check_file = check_file_function self.file_list = QListWidget() self.file_list.setSelectionMode(QAbstractItemView.ExtendedSelection) button_add = QPushButton(_('Add files')) self.button_remove = QPushButton(_('Remove selected')) self.button_remove.setEnabled(False) buttons = QWidget() buttons_layout = QVBoxLayout() buttons_layout.setAlignment(Qt.AlignTop) buttons.setLayout(buttons_layout) buttons_layout.addWidget(button_add) buttons_layout.addWidget(self.button_remove) main_layout = QHBoxLayout() self.setLayout(main_layout) main_layout.addWidget(self.file_list) main_layout.addWidget(buttons) button_add.clicked.connect(self._add_files) self.button_remove.clicked.connect(self._remove_files) self.file_list.selectionModel().selectionChanged.connect( \ self._selection_changed) def get_files(self): """Returns the list of selected file names.""" files = [] model = self.file_list.model() count = model.rowCount() for i in range(0, count): index = model.index(i, 0) files.append(unicode(model.data(index).toString())) return files def _add_files(self): file_list_q = QFileDialog.getOpenFileNames( self, self.title, '', self.file_name_filter, None, QFileDialog.DontUseNativeDialog) model = self.file_list.model() for file_name in file_list_q: valid = True if self._check_file is not None: valid, msg = self._check_file(unicode(file_name)) if valid: # Check if the file is already in the list: match = model.match(model.index(0, 0), 0, file_name, 1, Qt.MatchExactly) if len(match) == 0: self.file_list.addItem(file_name) def _remove_files(self): ranges = self.file_list.selectionModel().selection() model = self.file_list.model() to_remove = [] for r in ranges: to_remove.extend(range(r.top(), r.bottom() + 1)) for row in sorted(to_remove, reverse=True): model.removeRow(row) def _selection_changed(self, deselected, selected): if len(self.file_list.selectionModel().selection()) > 0: self.button_remove.setEnabled(True) else: self.button_remove.setEnabled(False)
class GameList(Page): def __init__(self, window): Page.__init__(self, window) self.display_all = True self.watch_all = False self.createWidgets(window) self.connectAsync('game_created', self.onGameCreated) self.connectAsync('game_started', self.onGameStarted) self.connectAsync('game_joined', self.onGameJoined) self.connectAsync('game_leaved', self.onGameLeaved) self.connectAsync('game_aborted', self.onGameAborted) self.connectAsync('game_destroyed', self.onGameDestroyed) def createWidgets(self, window): self.widget = QWidget() layout = QVBoxLayout() self.widget.setLayout(layout) checkbox = QCheckBox(tr("Display started games")) checkbox.setChecked(self.display_all) layout.addWidget(checkbox) window.connect(checkbox, SIGNAL('stateChanged(int)'), self.setDisplayAll) self.game_list = QListWidget() layout.addWidget(self.game_list) window.connect( self.game_list.selectionModel(), SIGNAL("selectionChanged(QItemSelection, QItemSelection)"), self.selectEvent) watch = QCheckBox(tr("Watch all game state changes")) watch.setChecked(self.watch_all) layout.addWidget(watch) window.connect(watch, SIGNAL('stateChanged(int)'), self.setWatch) self.watch_checkbox = watch button_layout = QHBoxLayout() layout.addLayout(button_layout) create = QPushButton('&Create') button_layout.addWidget(create) window.connect(create, SIGNAL('clicked()'), self.create) self.join_button = QPushButton('&Join') button_layout.addWidget(self.join_button) window.connect(self.join_button, SIGNAL('clicked()'), self.join) refresh = QPushButton('&Refresh') button_layout.addWidget(refresh) window.connect(refresh, SIGNAL('clicked()'), self.refresh) def selectEvent(self, selected, deselected): game_variant = getFirstSelected(selected, GAME_ROLE) if game_variant is None: return game = QVariant_toPyObject(game_variant) self.join_button.setEnabled(game['state'] == WAITING) def setDisplayAll(self, state): self.display_all = (state == Qt.Checked) self.fill() def startWatching(self, game): if game['state'] == ABORTED: return me = self.client.player_id if me in game['observers']: return self.client.command(u'game_watch', game['id'], me) game['observers'].append(me) def stopWatching(self, game): me = self.client.player_id if me not in game['observers']: return self.client.command(u'game_stop_watching', game['id'], me) game['observers'].remove(me) def setWatch(self, state): self.watch_all = (state == Qt.Checked) games = self.client.getGames() for game in games.itervalues(): if self.watch_all: self.startWatching(game) else: self.stopWatching(game) def create(self): self.window.gotoCreateGamePage() def join(self): self.watch_checkbox.setChecked(False) item = self.game_list.currentItem() if not item: return game = QVariant_toPyObject(item.data(GAME_ROLE)) game_id = game['id'] try: self.client.command(u'game_join', game_id, self.client.player_id) except Exception, err: self.window.exception(err) return self.window.gotoSetupGamePage(game)
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 })