def __createListWidget(self): iconList=QListWidget() iconList.setAlternatingRowColors(True) iconList.setContextMenuPolicy(Qt.CustomContextMenu) #iconList.setStyleSheet("QListWidget { background: red; } QListWidget::item { background: yellow; } QListWidget::item:selected { background: blue; }") iconList.setStyleSheet("QListWidget::item:selected:active { background: #28D9FF; color:#FFFFFF; } ")#that text color seems not to work! return iconList
def initUI(self, engine): splitter = QSplitter(Qt.Horizontal) # make the right panel right_panel = QSplitter(Qt.Vertical) player = Player() track_queue = QListWidget() ## set play on double click def play_track(): player.set_track(track_queue.currentItem().get_track()) player.play() track_queue.doubleClicked.connect(play_track) ## advance list on track finish def track_finish_task(): print 'track finished' if track_queue.currentRow() == track_queue.count() - 1: track_queue.setCurrentRow(0) else: track_queue.setCurrentRow(track_queue.currentRow() + 1) play_track() player.track_finished.connect(track_finish_task) ## put some entry controls on menu track_queue.setContextMenuPolicy(Qt.CustomContextMenu) def onContext(point): ## make action qaction = QAction('Delete item', track_queue) def remove_entry(): track_queue.takeItem(track_queue.currentRow()) qaction.triggered.connect(remove_entry) # Create a menu menu = QMenu("Menu", track_queue) menu.addAction(qaction) # Show the context menu. menu.exec_(track_queue.mapToGlobal(point)) track_queue.connect(track_queue, QtCore.SIGNAL("customContextMenuRequested(QPoint)"), onContext) utils.add_widgets_to_component(right_panel, [player, track_queue]) ## make the left panel left_panel = QWidget() vbox = QVBoxLayout() utils.add_widgets_to_component(vbox, [PlaylistsPanel(engine, [player], [track_queue]), SearchPanel(engine, [player], [track_queue])]) left_panel.setLayout(vbox) utils.add_widgets_to_component(splitter, [left_panel, right_panel]) splitter.setSizes([1, 10]) bottomBar = self.make_bottom_bar() # Application UI layout vBox = QVBoxLayout(self) vBox.addWidget(splitter) vBox.addWidget(bottomBar) self.setLayout(vBox)
class LateralWidget(QDockWidget): def __init__(self): super(LateralWidget, self).__init__() self._list_widget = QListWidget() self.setWidget(self._list_widget) Pireal.load_service("lateral", self) self._list_widget.setContextMenuPolicy(Qt.CustomContextMenu) self.connect(self._list_widget, SIGNAL("currentRowChanged(int)"), self._change_item) self.connect(self._list_widget, SIGNAL("customContextMenuRequested(const QPoint)"), self.__show_context_menu) def __show_context_menu(self, point): """ Context menu """ menu = QMenu() remove_table_act = menu.addAction(QIcon(":img/remove-rel"), self.tr("Eliminar Relación")) self.connect(remove_table_act, SIGNAL("triggered()"), self.remove_table) menu.exec_(self.mapToGlobal(point)) def _change_item(self, index): table = Pireal.get_service("container").table_widget table.stacked.setCurrentIndex(index) def add_item_list(self, items): for i in items: item = QListWidgetItem(i) item.setTextAlignment(Qt.AlignHCenter) self._list_widget.addItem(item) def remove_table(self): table_widget = Pireal.get_service("container").table_widget current_index = self._list_widget.currentRow() table_widget.remove_table(current_index) self._list_widget.takeItem(current_index)
class CheckList(QWidget): def __init__(self, model, label="", help_link="", custom_filter_button=None): """ :param custom_filter_button: if needed, add a button that opens a custom filter menu. Useful when search alone isn't enough to filter the list. :type custom_filter_button: QToolButton """ QWidget.__init__(self) self._model = model if help_link != "": addHelpToWidget(self, help_link) layout = QVBoxLayout() self._createCheckButtons() self._list = QListWidget() self._list.setContextMenuPolicy(Qt.CustomContextMenu) self._list.setSelectionMode(QAbstractItemView.ExtendedSelection) self._search_box = SearchBox() check_button_layout = QHBoxLayout() check_button_layout.setMargin(0) check_button_layout.setSpacing(0) check_button_layout.addWidget(QLabel(label)) check_button_layout.addStretch(1) check_button_layout.addWidget(self._checkAllButton) check_button_layout.addWidget(self._uncheckAllButton) layout.addLayout(check_button_layout) layout.addWidget(self._list) """ Inserts the custom filter button, if provided. The caller is responsible for all related actions. """ if custom_filter_button is not None: search_bar_layout = QHBoxLayout() search_bar_layout.addWidget(self._search_box) search_bar_layout.addWidget(custom_filter_button) layout.addLayout(search_bar_layout) else: layout.addWidget(self._search_box) self.setLayout(layout) self._checkAllButton.clicked.connect(self.checkAll) self._uncheckAllButton.clicked.connect(self.uncheckAll) self._list.itemChanged.connect(self.itemChanged) self._search_box.filterChanged.connect(self.filterList) self._list.customContextMenuRequested.connect(self.showContextMenu) self._model.selectionChanged.connect(self.modelChanged) self._model.modelChanged.connect(self.modelChanged) self.modelChanged() def _createCheckButtons(self): self._checkAllButton = QToolButton() self._checkAllButton.setIcon(resourceIcon("checked")) self._checkAllButton.setIconSize(QSize(16, 16)) self._checkAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self._checkAllButton.setAutoRaise(True) self._checkAllButton.setToolTip("Select all") self._uncheckAllButton = QToolButton() self._uncheckAllButton.setIcon(resourceIcon("notchecked")) self._uncheckAllButton.setIconSize(QSize(16, 16)) self._uncheckAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self._uncheckAllButton.setAutoRaise(True) self._uncheckAllButton.setToolTip("Unselect all") def itemChanged(self, item): """@type item: QListWidgetItem""" if item.checkState() == Qt.Checked: self._model.selectValue(str(item.text())) elif item.checkState() == Qt.Unchecked: self._model.unselectValue(str(item.text())) else: raise AssertionError("Unhandled checkstate!") def modelChanged(self): self._list.clear() items = self._model.getList() for item in items: list_item = QListWidgetItem(item) list_item.setFlags(list_item.flags() | Qt.ItemIsUserCheckable) if self._model.isValueSelected(item): list_item.setCheckState(Qt.Checked) else: list_item.setCheckState(Qt.Unchecked) self._list.addItem(list_item) self.filterList(self._search_box.filter()) def setSelectionEnabled(self, enabled): self.setEnabled(enabled) self._checkAllButton.setEnabled(enabled) self._uncheckAllButton.setEnabled(enabled) def filterList(self, filter): filter = filter.lower() for index in range(0, self._list.count()): item = self._list.item(index) text = str(item.text()).lower() if filter == "": item.setHidden(False) elif filter in text: item.setHidden(False) else: item.setHidden(True) def checkAll(self): """ Checks all visible items in the list. """ for index in range(0, self._list.count()): item = self._list.item(index) if not item.isHidden(): self._model.selectValue(str(item.text())) def uncheckAll(self): """ Unchecks all items in the list, visible or not """ self._model.unselectAll() def checkSelected(self): items = [] for item in self._list.selectedItems(): items.append(str(item.text())) for item in items: self._model.selectValue(item) def uncheckSelected(self): items = [] for item in self._list.selectedItems(): items.append(str(item.text())) for item in items: self._model.unselectValue(item) def showContextMenu(self, point): p = self._list.mapToGlobal(point) menu = QMenu() check_selected = menu.addAction("Check selected") uncheck_selected = menu.addAction("Uncheck selected") menu.addSeparator() clear_selection = menu.addAction("Clear selection") selected_item = menu.exec_(p) if selected_item == check_selected: self.checkSelected() elif selected_item == uncheck_selected: self.uncheckSelected() elif selected_item == clear_selection: self._list.clearSelection()
class Window(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.media = Phonon.MediaObject(self) ### video widget #### self.video = Phonon.VideoWidget(self) self.video.setMinimumSize(320,200) self.videoCuts = [] self.myfilename = "" self.extension = "" self.t1 = "" self.t2 = "" self.t3 = "" self.t4 = "" self.t5 = "" self.t6 = "" ### open button ### self.button = QtGui.QPushButton('Choose Video', self) self.button.setFixedWidth(90) self.button.clicked.connect(self.handleButton) self.button.setStyleSheet(stylesheet(self)) ### context menu #### self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.popup2) ### play / pause button ### self.playbutton = QtGui.QPushButton('Play', self) self.playbutton.setFixedWidth(70) self.playbutton.clicked.connect(self.handlePlayButton) self.playbutton.setStyleSheet(stylesheet(self)) self.connect(QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Space), self), QtCore.SIGNAL('activated()'), self.handlePlayButton) self.connect(QtGui.QShortcut(QtGui.QKeySequence("Ctrl+o"), self), QtCore.SIGNAL('activated()'), self.handleButton) self.connect(QtGui.QShortcut(QtGui.QKeySequence("Ctrl+s"), self), QtCore.SIGNAL('activated()'), self.handleSaveVideo) self.connect(QtGui.QShortcut(QtGui.QKeySequence("Ctrl+q"), self), QtCore.SIGNAL('activated()'), self.handleQuit) ### save button ### self.savebutton = QtGui.QPushButton('Save Video', self) self.savebutton.setFixedWidth(90) self.savebutton.clicked.connect(self.handleSaveVideo) self.savebutton.setStyleSheet(stylesheet(self)) ### seek slider ### self.slider = Phonon.SeekSlider(self.media) self.slider.setStyleSheet(stylesheet(self)) isize = QSize(16,16) self.slider.setIconSize(isize) self.slider.setFocus() # self.slider.connect(self.handleLabel) ### connection position to label ### self.media.isSeekable() self.media.tick.connect(self.handleLabel) self.media.seekableChanged.connect(self.handleLabel) #self.slider.wheel.connect(self.handleLabel) ### table view ### self.iconList = QListWidget() self.iconList.setAlternatingRowColors(True) self.iconList.setFixedWidth(200) self.iconList.setContextMenuPolicy(Qt.CustomContextMenu) self.iconList.setStyleSheet("QListWidget::item:selected:active { background: #7D8ED9; color:#FFFFFF; } ") self.iconList.setViewMode(0) self.iconList.setSelectionBehavior(1) self.iconList.setIconSize(QSize(80, 80/1.78)) self._hookListActions() self.iconList.customContextMenuRequested.connect(self._openListMenu) ### set start button ### self.startbutton = QtGui.QPushButton('set Start', self) self.startbutton.setFixedWidth(70) self.startbutton.clicked.connect(self.handleStartButton) self.startbutton.setStyleSheet(stylesheet(self)) self.connect(QtGui.QShortcut(QtGui.QKeySequence("s"), self), QtCore.SIGNAL('activated()'), self.handleStartButton) ### set end button ### self.endbutton = QtGui.QPushButton('set End', self) self.endbutton.setFixedWidth(70) self.endbutton.clicked.connect(self.handleEndButton) self.endbutton.setStyleSheet(stylesheet(self)) self.connect(QtGui.QShortcut(QtGui.QKeySequence("e"), self), QtCore.SIGNAL('activated()'), self.handleEndButton) ### label ### self.mlabel = QtGui.QLabel('Frame', self) self.mlabel.setStyleSheet('QLabel \ {background-color: transparent; color: white;}\nQLabel{color: darkcyan; font-size: 12px; background-color: transparent; border-radius: 5px; padding: 6px; text-align: center;}\n QLabel:hover{color: red;}') #self.mlabel.setFixedWidth(80) ### layout ### layout = QtGui.QGridLayout(self) layout.addWidget(self.iconList, 0, 0, 1, 1) layout.addWidget(self.video, 0, 1, 1, 6) layout.addWidget(self.slider, 1, 1, 1, 6) layout.addWidget(self.button, 2, 0, 1, 1) layout.addWidget(self.savebutton, 2, 1, 1, 1) layout.addWidget(self.playbutton, 2, 3, 1, 1) layout.addWidget(self.startbutton, 2, 5, 1, 1) layout.addWidget(self.endbutton, 2, 6, 1, 1) layout.addWidget(self.mlabel, 2, 4, 1, 1) def popup2(self, pos): contextmenu = QMenu() contextmenu.addAction("Play / Pause (SPACE)", self.handlePlayButton) contextmenu.addSeparator() contextmenu.addAction("Load Video (Ctrl-O)", self.handleButton) contextmenu.addAction("Save Video (Ctrl-S)", self.handleSaveVideo) contextmenu.addSeparator() contextmenu.addAction("Info", self.handleInfo) contextmenu.addSeparator() contextmenu.addAction("Exit (q)", self.handleQuit) contextmenu.exec_(QCursor.pos()) def handleInfo(self): msg = QMessageBox() #msg.setFixedSize(500, 300) #msg.setGeometry(100,100, 400, 200) msg.setIcon(QMessageBox.Information) msg.setText("Axel Schneider") msg.setInformativeText(unicode(u"©2016")) msg.setWindowTitle("Cut Video") msg.setDetailedText("Python Qt4") msg.setStandardButtons(QMessageBox.Ok) retval = msg.exec_() print "value of pressed message box button:", retval def handleQuit(self): app.quit() def handleButton(self): if self.media.state() == Phonon.PlayingState: self.media.stop() else: path = QtGui.QFileDialog.getOpenFileName(self, ("Video laden"), '/Axel_1/Filme', "Videos (*.ts *.mp4)") if path: self.myfilename = unicode(path) #.encode("utf-8") window.setWindowTitle(self.myfilename.split("/")[-1]) self.extension = path.split(".")[1] print(self.extension) self.media.setCurrentSource(Phonon.MediaSource(path)) self.video.setScaleMode(1) self.video.setAspectRatio(1) self.audio = Phonon.AudioOutput(Phonon.VideoCategory, self) Phonon.createPath(self.media, self.audio) Phonon.createPath(self.media, self.video) self.media.play() self.playbutton.setText('Pause') def handleSaveVideo(self): result = QFileDialog.getSaveFileName(self, ("Video speichern"), '/tmp/film.' + str(self.extension), "Videos (*.ts *.mp4)") if result: target = unicode(result) self.t1 = float(self.videoCuts[0]) self.t2 = float(self.videoCuts[1]) ffmpeg_extract_subclip(self.myfilename, self.t1, self.t2, targetname=target) window.setWindowTitle("Film gespeichert") self.purgeMarker() def handlePlayButton(self): if self.media.state() == Phonon.PlayingState: self.media.pause() self.playbutton.setText('Play') else: #print(self.iconList.count()) self.media.play() self.playbutton.setText('Pause') def handleStartButton(self): if self.iconList.count() < 2: rm = str(self.media.currentTime() / 100.00 / 10.00) item = QListWidgetItem() img = QImage(self.video.snapshot()) pix = QtGui.QPixmap.fromImage(img) item.setIcon(QIcon(pix)) item.setText("Start: " + rm) self.iconList.addItem(item) self.videoCuts.append(rm) else: return def handleEndButton(self): if self.iconList.count() < 2: rm = str(self.media.currentTime() / 100.00 / 10.00) item = QListWidgetItem() #item.setSizeHint(QSize(150, 40)) img = QImage(self.video.snapshot()) pix = QtGui.QPixmap.fromImage(img) item.setIcon(QIcon(pix)) item.setText("End: " + rm) self.iconList.addItem(item) self.videoCuts.append(rm) self.t3 = float(str(self.media.remainingTime())) print(self.t3) self.media.stop() self.playbutton.setText('Play') else: return def handleLabel(self): ms = self.media.currentTime() seconds=str((ms/1000)%60) minutes=str((ms/(1000*60))%60) hours=str((ms/(1000*60*60))%24) if int(seconds) < 10: seconds = "0" + seconds if int(minutes) < 10: minutes = "0" + minutes if int(hours) < 10: hours = "0" + hours s = hours + ":" + minutes + ":" + seconds self.mlabel.setText(s) def _hookListActions(self): #TOO bad-the list model -should be here... rmAction = QtGui.QAction(QtGui.QIcon('icons/close-x.png'), 'entfernen', self) rmAction.triggered.connect(self._removeMarker) rmAllAction = QtGui.QAction(QtGui.QIcon('icons/clear-all.png'), 'alle entfernen', self) rmAllAction.triggered.connect(self.purgeMarker) self.gotoAction = QtGui.QAction(QtGui.QIcon('icons/go-next.png'), 'zu dieser Position springen', self) self.gotoAction.triggered.connect(self._gotoFromMarker) #menus self._listMenu = QMenu() self._listMenu.addAction(self.gotoAction) self._listMenu.addSeparator() self._listMenu.addAction(rmAction) self._listMenu.addAction(rmAllAction) #---List widget context menu def _removeMarker(self,whatis): selectionList = self.iconList.selectedIndexes() if len(selectionList)==0: return item = selectionList[0] self.iconList.takeItem(item.row()) #self.videoCuts.remove[1]) def clearMarkerList(self): self.iconList.clear() #remove contents, remove file def purgeMarker(self): self.iconList.clear() self.videoCuts = [] def _gotoFromMarker(self,whatis): selectionList = self.iconList.selectedIndexes() if len(selectionList)==0: return item = selectionList[0] pos = item.data().toString().replace("Start: ", "").replace("End: ", "") #frame = pos.ToInt() #self.video.currentTime = 1589 self.setWindowTitle(pos) def _openListMenu(self,position): selectionList = self.iconList.selectedIndexes() if len(selectionList)==0: return self._listMenu.exec_(self.iconList.viewport().mapToGlobal(position))
class CheckList(HelpedWidget): def __init__(self, model, label="", help_link=""): HelpedWidget.__init__(self, "", help_link) layout = QVBoxLayout() widget = QWidget() widget.setLayout(layout) self.checkAllButton = QToolButton() self.checkAllButton.setIcon(resourceIcon("checked")) self.checkAllButton.setIconSize(QSize(16, 16)) self.checkAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.checkAllButton.setAutoRaise(True) self.checkAllButton.setToolTip("Select all") self.uncheckAllButton = QToolButton() self.uncheckAllButton.setIcon(resourceIcon("notchecked")) self.uncheckAllButton.setIconSize(QSize(16, 16)) self.uncheckAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.uncheckAllButton.setAutoRaise(True) self.uncheckAllButton.setToolTip("Unselect all") self.list = QListWidget() self.list.setContextMenuPolicy(Qt.CustomContextMenu) self.list.setSelectionMode(QAbstractItemView.ExtendedSelection) self.search_box = SearchBox() check_button_layout = QHBoxLayout() check_button_layout.setMargin(0) check_button_layout.setSpacing(0) check_button_layout.addWidget(QLabel(label)) check_button_layout.addStretch(1) check_button_layout.addWidget(self.checkAllButton) check_button_layout.addWidget(self.uncheckAllButton) layout.addLayout(check_button_layout) layout.addWidget(self.list) layout.addWidget(self.search_box) self.addWidget(widget) self.connect(self.checkAllButton, SIGNAL('clicked()'), self.checkAll) self.connect(self.uncheckAllButton, SIGNAL('clicked()'), self.uncheckAll) self.connect(self.list, SIGNAL('itemChanged(QListWidgetItem*)'), self.itemChanged) self.search_box.filterChanged.connect(self.filterList) # self.connect(self.search_box, SIGNAL('filterChanged(str)'), self.filterList) self.connect(self.list, SIGNAL('customContextMenuRequested(QPoint)'), self.showContextMenu) assert isinstance(model, (SelectableModelMixin, ListModelMixin)) self.model = model self.model.observable().attach( SelectableModelMixin.SELECTION_CHANGED_EVENT, self.modelChanged) self.model.observable().attach(ListModelMixin.LIST_CHANGED_EVENT, self.modelChanged) self.modelChanged() def itemChanged(self, item): """@type item: QListWidgetItem""" if item.checkState() == Qt.Checked: self.model.selectValue(str(item.text())) elif item.checkState() == Qt.Unchecked: self.model.unselectValue(str(item.text())) else: raise AssertionError("Unhandled checkstate!") def modelChanged(self): self.list.clear() items = self.model.getList() for item in items: list_item = QListWidgetItem(item) list_item.setFlags(list_item.flags() | Qt.ItemIsUserCheckable) if self.model.isValueSelected(item): list_item.setCheckState(Qt.Checked) else: list_item.setCheckState(Qt.Unchecked) self.list.addItem(list_item) self.filterList(self.search_box.filter()) def setSelectionEnabled(self, enabled): self.setEnabled(enabled) self.checkAllButton.setEnabled(enabled) self.uncheckAllButton.setEnabled(enabled) def filterList(self, filter): filter = filter.lower() for index in range(0, self.list.count()): item = self.list.item(index) text = str(item.text()).lower() if filter == "": item.setHidden(False) elif filter in text: item.setHidden(False) else: item.setHidden(True) def checkAll(self): self.model.selectAll() def uncheckAll(self): self.model.unselectAll() def checkSelected(self): items = [] for item in self.list.selectedItems(): items.append(str(item.text())) for item in items: self.model.selectValue(item) def uncheckSelected(self): items = [] for item in self.list.selectedItems(): items.append(str(item.text())) for item in items: self.model.unselectValue(item) def showContextMenu(self, point): p = self.list.mapToGlobal(point) menu = QMenu() check_selected = menu.addAction("Check selected") uncheck_selected = menu.addAction("Uncheck selected") menu.addSeparator() clear_selection = menu.addAction("Clear selection") selected_item = menu.exec_(p) if selected_item == check_selected: self.checkSelected() elif selected_item == uncheck_selected: self.uncheckSelected() elif selected_item == clear_selection: self.list.clearSelection()
class MyForm(QWidget): def __init__(self): QWidget.__init__(self) # setGeometry(x_pos, y_pos, width, height) self.setGeometry(100, 150, 500, 460) self.readSettings() self.setWindowTitle(APPNAME + ' ' + VERSION + " - " + self.chanells_path ) self.icon_path = get_icon_resource(imgdata_png_main) # self.icon_path = os.path.join(os.path.dirname(sys.argv[0]), 'chanchan.ico') self.icon = QIcon(self.icon_path) self.setWindowIcon(self.icon) self.chanells = None self.chanells_all = None self.num_channels = 0 # the player subprocess process self.proc = None self.proc_sopcast = None self.is_sopcast = False self.is_playlist = False self.on_top = False self.cache_size = CACHE_SIZE_DEFAULT if not self.haveSeenFirstTime: copy_default_playlist() self.haveSeenFirstTime = True # saving settings should be done in closeEvent() instead! # settings = QSettings(QSettings.IniFormat, QSettings.UserScope, "xh", "chanchan") # settings.setValue("seen_first_time", QVariant(self.haveSeenFirstTime)) # settings.sync() # use a grid layout for the widgets grid = QGridLayout() # bind the button click to a function reference # new connect style, needs PyQt 4.5+ ###btn_load.clicked.connect(self.load_channels_data) btn_play = QPushButton("&Play") btn_play.setToolTip("Click to play selected stream") btn_play.clicked.connect(self.on_button_play) btn_kill = QPushButton("&Stop") btn_kill.setToolTip("Click to stop current player") btn_kill.clicked.connect(self.kill_proc) self.listbox = QListWidget() # new connect style, needs PyQt 4.5+ self.listbox.clicked.connect(self.on_select) self.listbox.doubleClicked.connect(self.on_double_click) # attach right-click handler self.listbox.setContextMenuPolicy(Qt.ActionsContextMenu) #self.listbox.setContextMenuPolicy(Qt.CustomContextMenu) #http://talk.maemo.org/showthread.php?t=64034 self.actionCopyUrl = QAction("Copy URL", self.listbox) self.connect(self.actionCopyUrl, SIGNAL("triggered()"), self.copy_to_clipboard) self.actionPlay = QAction("Play", self.listbox) self.actionPlay.setShortcut("Ctrl+P") self.actionRestartPlayer = QAction("Restart SopPlayer", self.listbox) self.actionReloadChannels = QAction("Reload List", self.listbox) self.actionEditChannels = QAction("Edit Playlist", self.listbox) self.actionEditChannels.setShortcut("Ctrl+E") self.actionOpenChannelsFile = QAction("Open Playlist File", self.listbox) self.actionEditSource = QAction("Edit Source", self.listbox) self.actionAbout = QAction("About %s" % APPNAME, self.listbox) self.actionQuit = QAction("Quit", self) self.actionQuit.setShortcut("Ctrl+Q") self.search = QLineEdit() self.connect(self.search, SIGNAL("textChanged(QString)"), self.on_search_text_change) # clear button self.clear_button = QToolButton() self.clear_button.setIcon(get_icon_resource(imgdata_png_clear)) self.clear_button.setIconSize(QSize(16, 16)) self.clear_button.setCursor(Qt.ArrowCursor) self.clear_button.setAutoRaise(True) self.clear_button.setEnabled(False) # self.main_layout.addWidget(self.clear_button) self.connect(self.clear_button, SIGNAL("clicked()"), self.clear_search_text) self.listbox.addAction(self.actionPlay) self.listbox.addAction(self.actionRestartPlayer) self.listbox.addAction(self.actionCopyUrl) self.listbox.addAction(self.actionOpenChannelsFile) self.listbox.addAction(self.actionReloadChannels) self.listbox.addAction(self.actionEditChannels) self.listbox.addAction(self.actionEditSource) self.listbox.addAction(self.actionAbout) self.addAction(self.actionQuit) self.connect(self.actionPlay, SIGNAL("triggered()"), self.on_double_click) self.connect(self.actionRestartPlayer, SIGNAL("triggered()"), self.restart_sopplayer) self.connect(self.actionReloadChannels, SIGNAL("triggered()"), lambda: self.load_channels_data(self.chanells_path)) self.connect(self.actionEditChannels, SIGNAL("triggered()"), lambda: self.edit_file(str(self.chanells_path))) self.connect(self.actionOpenChannelsFile, SIGNAL("triggered()"), lambda: self.load_channels_data()) self.connect(self.actionEditSource, SIGNAL("triggered()"), lambda: self.edit_file(path=sys.argv[0], editor=EDITOR)) self.connect(self.actionQuit, SIGNAL("triggered()"), self.close) self.connect(self.actionAbout, SIGNAL("triggered()"), lambda: QMessageBox.about(self, 'About %s' % APPNAME, ''' <h4>%s version %s</h4> <p> Created by <i>%s</i></p> <p><a href="mailto:%s">%s</a></p> <p><a href="%s">chanchantv.googlecode.com</a></p> ''' % (APPNAME, VERSION, AUTHOR, EMAIL.decode('base64'), EMAIL, WEB)) # warning(self, APPNAME, 'No playlist selected') ) # self.listbox.connect(self.listbox, SIGNAL("customContextMenuRequested(QPoint)"), # self.on_right_click) # self.txtChanInfo = QLineEdit() # self.txtChanInfo.setReadOnly(True) # self.logWindow = QTextEdit() # self.logWindow.setSizePolicyx(QSizePolicy.) self.status = QLabel() self.status.setText('channels') # ADD BEVELED BORDER::self.status.setFrameStyle(QFrame.Panel | QFrame.Sunken) self.groupBox = QGroupBox("Engine") sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth()) self.groupBox.setSizePolicy(sizePolicy) # self.groupBox.setAutoFillBackground(True) self.rbMplayer = QRadioButton('&Mplayer', self.groupBox) self.rbMplayer.setChecked(True) self.rbMplayer.setToolTip("Play with Mplayer") #self.rbGstreamer = QRadioButton('gst&123', self.groupBox) self.rbVlc = QRadioButton('&Vlc', self.groupBox) self.rbVlc.setToolTip("Play with VLC") self.rbTotem = QRadioButton('&Totem', self.groupBox) self.rbTotem.setToolTip("Play with Totem") self.rbBrowser = QRadioButton('&Browser', self.groupBox) self.rbBrowser.setToolTip("Open URL in web browser") self.hBoxTop = QHBoxLayout() self.hBoxTop.addWidget(self.rbMplayer) #self.hBoxTop.addWidget(self.rbGstreamer) self.hBoxTop.addWidget(self.rbVlc) self.hBoxTop.addWidget(self.rbTotem) self.hBoxTop.addWidget(self.rbBrowser) self.groupBox.setLayout(self.hBoxTop) self.cbPlaylistFlag = QCheckBox('Playlist') self.cbPlaylistFlag.setToolTip('Resource is a M3U, ASX or PLS playlist') self.cbFullScreen = QCheckBox('Full Screen') self.cbFullScreen.setToolTip('Start video in full screen') self.cbFullScreen.setChecked(self.is_full_screen) self.cbInhibitScreensaver = QCheckBox('Inhibit Screensaver') self.cbInhibitScreensaver.setToolTip('Disable screensaver while playing stream') self.cbInhibitScreensaver.setChecked(self.is_inhibit_screen) # addWidget(widget, row, column, rowSpan, columnSpan) grid.addWidget(self.groupBox, 0, 0, 1, 3) grid.addWidget(btn_play, 0, 4, 1, 1) grid.addWidget(btn_kill, 0, 5, 1, 1) grid.addWidget(self.search, 1, 0, 1, 4) grid.addWidget(self.clear_button, 1, 3, 1, 1) grid.addWidget(self.status, 1, 5, 1, 1) # listbox spans over 5 rows and 2 columns grid.addWidget(self.listbox, 2, 0, 5, 6) ## BAD grid.addWidget(self.hBoxFlags, 6, 0, 1, 1) grid.addWidget(self.cbPlaylistFlag, 7, 0, 1, 1) grid.addWidget(self.cbFullScreen, 7, 1, 1, 1) grid.addWidget(self.cbInhibitScreensaver, 7, 2, 1, 1) # grid.addWidget(self.txtChanInfo, 7, 0, 1, 6) # grid.addWidget(self.logWindow, 8, 0, 1, 6) self.setLayout(grid) self.search.setFocus() self.load_channels_data(self.chanells_path) def clear_search_text(self): print '------clear-search-text---------' self.search.clear() def on_search_text_change(self): if not self.chanells_all: # only need to do this once self.chanells_all = list(self.chanells) text = str(self.search.text()).strip() print 'DBG', len(text), len(self.chanells_all) if len(text) > 1: self.clear_button.setEnabled(True) filtered_list = self.get_matching_items(text.lower(), self.chanells_all) if len(filtered_list): self.chanells = filtered_list else: self.chanells = [] else: self.chanells = list(self.chanells_all) self.clear_button.setEnabled(False) self.load_channels_data(None, False) def get_matching_items(self, needle, haystack): 'search for a substring in channel list' matches = [] found_in_meta = False last_meta_item = None for ch in haystack: is_meta = ch.startswith('#') if is_meta and not needle in ch.lower(): last_meta_item = ch if needle in ch.lower(): if is_meta: found_in_meta = True elif not found_in_meta and last_meta_item not in matches: matches.append(last_meta_item) matches.append(ch) elif found_in_meta: if not is_meta: matches.append(ch) else: found_in_meta = False return matches def closeEvent(self, event): self.writeSettings() print 'closeEvent: Saving settings and exiting...' return quit_msg = "Are you sure you want to exit the program?" reply = QMessageBox.question(self, 'Message', quit_msg, QMessageBox.Yes, QMessageBox.No) if reply == QMessageBox.Yes: self.writeSettings() event.accept() QApplication.instance().quit() else: event.ignore() def keyPressEvent(self, event): if event.key() == Qt.Key_Escape: self.search.setFocus() self.search.selectAll() def copy_to_clipboard(self): clipboard = QApplication.clipboard() clipboard.setText(self.listbox.currentItem().text()) def load_channels_data(self, new_path=None, read_from_file=True): MediaItem.num_items = 0 if read_from_file: if not new_path: new_path = str(self.get_new_filename()) if not new_path: # QMessageBox.warning(self, APPNAME, 'No playlist selected') return try: fh = codecs.open(new_path, 'r', 'utf8') self.chanells = [ch.strip() for ch in fh.readlines()] except Exception as e: show_gui_error(e, 'File not found', 'Error opening playlist "%s" \n\n%s' % (new_path, str(e))) return self.chanells_path = new_path self.chanells = [ch.strip() for ch in self.chanells] self.chanells_all = None self.listbox.clear() current_params = None for chan in self.chanells: if not len(chan) or chan.strip() == '#EXTM3U': continue item = MediaItem(chan, self.icon) if item.is_meta: '''if a metadata line, then store and apply to all the following non-metadata items ''' current_params = item.params elif current_params: item.params = current_params item.setStatusTip(chan) self.listbox.addItem(item) self.setWindowTitle(APPNAME + ' ' + VERSION + ' - ' + self.chanells_path) self.status.setText(str(MediaItem.num_items) + ' channels') def edit_file(self, path, editor=EDITOR): if ' ' in editor: editor = editor.split(' ') subprocess.Popen([editor, path]) def get_new_filename(self): return QFileDialog.getOpenFileName(self, 'Load Playlist file', '', "Playlist files (*.m3u);;All Files (*.*);") def on_button_play(self): # self.on_select() self.play_media() def on_select(self): """an item in the listbox has been clicked/selected""" current_item = self.listbox.currentItem() if not current_item: return current_channel = current_item and str(current_item.text()) or '<no channel>' self.is_playlist = current_channel[-4:].lower() in ['.m3u', '.asx', '.pls'] # if current_channel.startswith('sop:'): #self.rbMplayer.setChecked(True) # self.cache_size = '1024' if current_item.params: 'set params for current channel according to metadata line' myparams = current_item.params.keys() if 'player' in myparams: player = current_item.params['player'].lower() if player == 'totem': self.rbTotem.setChecked(True) elif player == MPLAYER: self.rbMplayer.setChecked(True) #elif player == 'gst123': # self.rbGstreamer.setChecked(True) elif player == VLC: self.rbVlc.setChecked(True) elif player in ('browser', 'web'): self.rbBrowser.setChecked(True) if 'playlist' in myparams or 'pl' in myparams: self.is_playlist = current_item.params['playlist'].lower() in GOOD_VALUES if 'fullscreen' in myparams or 'fs' in myparams: self.is_full_screen = current_item.params['fullscreen'].lower() in GOOD_VALUES else: self.is_full_screen = IS_FULLSCREEN_DEFAULT if 'ontop' in myparams or 'top' in myparams: self.on_top = current_item.params['top'].lower() in GOOD_VALUES else: self.on_top = IS_ON_TOP_DEFAULT if 'cache' in myparams: self.cache_size = current_item.params['cache'] else: self.cache_size = CACHE_SIZE_DEFAULT if 'exec' in myparams or 'shell' in myparams: # set shell options: console or no console self.exec_shell_command = current_item.params['exec'] # # if 'exec' in myparams: # self.executable_name = current_item.params['exec'] self.cbPlaylistFlag.setChecked(self.is_playlist) # only setting True state if self.is_full_screen: self.cbFullScreen.setChecked(True) def on_double_click(self): self.play_media() """an item in the listbox has been double-clicked""" def restart_sopplayer(self): # if self.rbVlc.isChecked(): # if vlc_remote_command('testing if vlc remote is running...'): # vlc_remote_command('add %s' % SOPCAST_SERVER_URL) # vlc_remote_command('volume 200') # else: self.play_media(start_sopcast_server=False) def play_media(self, start_sopcast_server=True): current_item = self.listbox.currentItem() if not current_item: return current_channel = str(current_item.text()) if self.proc and self.proc.pid: self.kill_proc() args = [] if self.cbInhibitScreensaver.isChecked(): suspend_screensaver() ################ RUN SHELL COMMAND ############# if 'exec' in current_item.params: show_console = current_item.params['exec'].lower() == 'console' if show_console: args += ['xterm', '-geometry', '45x8-20+400', '-e'] args += [current_channel.strip()] self.proc = subprocess.Popen(args, stdout=subprocess.PIPE) print 'DBG:', self.proc else: args.insert(0, current_item.params['exec']) args += [current_channel.strip()] self.proc = subprocess.Popen(args, shell=True) return # don't use xterm for vlc, totem if (self.rbMplayer.isChecked()): if not is_win32 and not is_osx: args += ['xterm', '-geometry', '45x8-20+150', '-e'] self.is_sopcast = current_channel.lower().startswith('sop://') if self.is_sopcast: if start_sopcast_server: # args_sopcast = ['xterm', '-geometry', '45x8-20+400', '-e', sopcast_binary, current_channel, SOPCAST_LISTEN_PORT, SOPCAST_SERVER_PORT] try: print 'Waiting for sopcast server starup at %s ...' % current_channel self.proc_sopcast = run_command_in_new_terminal( sopcast_binary, current_channel, SOPCAST_LISTEN_PORT, SOPCAST_SERVER_PORT) # except Exception as e: show_gui_error(e, """ERROR! Sopcast executable not found or other error: To install sopcast support on Linux, run: %s""" % (SOPCAST_INSTALL_HOWTO)) return current_channel = SOPCAST_SERVER_URL time.sleep(SOPCAST_SERVER_WAIT_SECS) if self.rbMplayer.isChecked(): if is_win32: args = ['cmd', '/c', MPLAYER_PATH_WIN32, '-cache-min', CACHE_SIZE_MIN, '-cache', self.cache_size] else: args += [MPLAYER, '-cache-min', CACHE_SIZE_MIN, '-cache', self.cache_size] self.on_top and args.append('-ontop') self.cbFullScreen.isChecked() and args.append('-fs') self.cbPlaylistFlag.isChecked() and args.append('-playlist') #elif self.rbGstreamer.isChecked(): # args.append('gst123') # if '.m3u' in current_channel: # current_channel = getFirstUrl(current_channel) elif self.rbVlc.isChecked(): if is_win32: if os.path.exists(VLC_PATH_WIN32): args = [VLC_PATH_WIN32] elif os.path.exists(VLC_PATH_WIN32_CUSTOM): args = [VLC_PATH_WIN32_CUSTOM] elif is_osx: args = [VLC_PATH_OSX]#, '--one-instance'] else: args = ['vlc']#, '--one-instance'] # if vlc_remote_command('testing if vlc remote is running...'): # # print 'VLC Remote Control not running, starting new VLC instance...' # vlc_remote_command('add %s' % current_channel) # vlc_remote_command('volume 150') # return # else: # print 'VLC Remote Control not running, starting new VLC instance...' self.cbFullScreen.isChecked() and args.append('--fullscreen') args.append('--video-on-top') elif self.rbTotem.isChecked(): args += ['totem', '--replace'] # FIXME!!! totem segfaults when started with the --fullscreen switch # self.cbFullScreen.isChecked() and args.append('--fullscreen') elif self.rbBrowser.isChecked(): open_webbrowser(current_channel) return args.append(current_channel) print args try: if is_win32: #self.proc = subprocess.Popen(args, creationflags=subprocess.STARTF_USESHOWWINDOW, cwd=os.path.dirname(sys.argv[0])) ## TODO: get right options on win32 #http://stackoverflow.com/questions/7006238/how-do-i-hide-the-console-when-i-use-os-system-or-subprocess-call #startupinfo = subprocess.STARTUPINFO() #startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW #subprocess.call('taskkill /F /IM exename.exe', startupinfo=startupinfo) self.proc = subprocess.Popen(args, shell=True, cwd=os.path.dirname(sys.argv[0])) else: self.proc = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE) print 'DBG:', type(self.proc), self.proc # console_data = self.proc.stdout.read() # self.logWindow.setText(console_data) except Exception as e: show_gui_error(e, "ERROR! Selected player not available:\n") def kill_proc(self): if self.cbInhibitScreensaver.isChecked(): resume_screensaver() if self.proc and not self.rbVlc.isChecked(): try: self.proc.kill() except: pass if self.is_sopcast and self.proc_sopcast: try: self.proc_sopcast.kill() os.system('killall sopcast') except: pass def readSettings(self): # store settings object self.settings = QSettings(QSettings.IniFormat, QSettings.UserScope, "xh", "chanchan") pos = self.settings.value("pos", QVariant(QPoint(200, 200))).toPoint() size = self.settings.value("size", QVariant(QSize(400, 400))).toSize() self.resize(size) self.move(pos) self.chanells_path = self.settings.contains('channels_file') and str(self.settings.value("channels_file").toString()) or get_default_channels_path() self.is_inhibit_screen = self.settings.contains('inhibit_screen') and self.settings.value("inhibit_screen").toBool() self.is_full_screen = self.settings.contains('fullscreen') and self.settings.value("fullscreen").toBool() self.haveSeenFirstTime = self.settings.contains('seen_first_time') and self.settings.value("seen_first_time").toBool() def writeSettings(self): settings = QSettings(QSettings.IniFormat, QSettings.UserScope, "xh", "chanchan") settings.setValue("pos", QVariant(self.pos())) settings.setValue("size", QVariant(self.size())) settings.setValue("channels_file", QVariant(self.chanells_path)) settings.setValue("inhibit_screen", QVariant(self.cbInhibitScreensaver.isChecked())) settings.setValue("fullscreen", QVariant(self.cbFullScreen.isChecked())) settings.setValue("seen_first_time", QVariant(self.haveSeenFirstTime)) settings.sync()
class SeriesPreview(QDialog): def __init__(self, parent=None): super(SeriesPreview, self).__init__(parent) self.data = None self.workingSet = None self.list = QListWidget() self.cancel = QPushButton('Close') self.apply = QPushButton('Update') self.layout = QGridLayout() self.layout.addWidget(self.list, 0, 0, 1, 2) self.layout.addWidget(self.apply, 1, 0) self.layout.addWidget(self.cancel, 1, 1) self.setLayout(self.layout) self.initComponents() self.initActions() def initComponents(self): self.setWindowFlags(Qt.Tool) self.setWindowTitle("Time series") self.setStyleSheet('''QPushButton { color: #333; border: 1px solid #555; border-radius: 11px; padding: 2px; background: qradialgradient(cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4, radius: 1.35, stop: 0 #fff, stop: 1 #888); min-width: 80px; } QPushButton:hover { color: #fff; background: qradialgradient(cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4, radius: 1.35, stop: 0 #fff, stop: 1 #bbb);} QPushButton:pressed { background: qradialgradient(cx: 0.4, cy: -0.1, fx: 0.4, fy: -0.1, radius: 1.35, stop: 0 #fff, stop: 1 #ddd);} QPushButton:checked { background: qradialgradient(cx: 0.4, cy: -0.1, fx: 0.4, fy: -0.1, radius: 1.35, stop: 0 #fff, stop: 1 #ddd);} QListView::focus { border: 2px solid black; border-radius: 6px; } QScrollBar:vertical { width: 20px; border: 1px solid grey; border-radius: 6px; background-color: transparent; margin: 28px 0 28px 0; } QScrollBar::add-line:vertical { background: transparent; height: 32px; subcontrol-position: bottom; subcontrol-origin: margin; } QScrollBar::sub-line:vertical { background: transparent; height: 32px; subcontrol-position: top; subcontrol-origin: margin; } QScrollBar::up-arrow:vertical { width: 20px; height: 32px; background: transparent; image: url(../res/icons/arrow_up.png); } QScrollBar::up-arrow:hover { bottom: 2px; } QScrollBar::down-arrow:vertical { width: 20px; height: 32px; background: transparent; image: url(../res/icons/arrow_down.png); } QScrollBar::down-arrow:hover { top: 2px; } QScrollBar::handle:vertical { border-radius: 6px; background: url(../res/icons/handle.png) 0% center no-repeat; background-color: white; min-height: 32px; } QScrollBar::handle:hover { background: url(../res/icons/handle_hover.png) 0% center no-repeat; background-color: white; border: 1px solid gray; }''') self.list.setAlternatingRowColors(True) self.list.setStyleSheet('''QListView::item:selected:active { background: qlineargradient(x1: 1, y1: 0, x2: 0, y2: 3, stop: 0 #cbdaf1, stop: 1 #bfcde4); } QListView::item { border: 1px solid #d9d9d9; border-top-color: transparent; border-bottom-color: transparent; } QListView::item:hover { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1); border: 1px solid #bfcde4; }''') self.list.setSelectionMode(QAbstractItemView.ExtendedSelection) self.list.setContextMenuPolicy(Qt.ActionsContextMenu) def initActions(self): self.apply.clicked.connect(self.applyChanges) self.cancel.clicked.connect(self.close) self.list.itemDoubleClicked.connect(self.removeFromList) self.list.addAction(QAction('&Remove selected', self, triggered=self.removeItems)) #--- actions ---# def updateData(self, data): self.data = data self.workingSet = data[0][:] self.updateList() def updateList(self): self.list.clear() for item in self.workingSet: item = QListWidgetItem(str(item)) item.setTextAlignment(Qt.AlignCenter) self.list.addItem(item) def applyChanges(self): self.data = (self.workingSet, self.data[1]) def removeFromList(self, item): self.workingSet.remove(float(item.text())) self.list.takeItem(self.list.indexFromItem(item).row()) def removeItems(self): for item in self.list.selectedItems(): self.workingSet.remove(float(item.text())) self.list.takeItem(self.list.indexFromItem(item).row())
class CheckList(HelpedWidget): def __init__(self, model, label="", help_link=""): HelpedWidget.__init__(self, "", help_link) layout = QVBoxLayout() widget = QWidget() widget.setLayout(layout) self.checkAllButton = QToolButton() self.checkAllButton.setIcon(resourceIcon("checked")) self.checkAllButton.setIconSize(QSize(16, 16)) self.checkAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.checkAllButton.setAutoRaise(True) self.checkAllButton.setToolTip("Select all") self.uncheckAllButton = QToolButton() self.uncheckAllButton.setIcon(resourceIcon("notchecked")) self.uncheckAllButton.setIconSize(QSize(16, 16)) self.uncheckAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.uncheckAllButton.setAutoRaise(True) self.uncheckAllButton.setToolTip("Unselect all") self.list = QListWidget() self.list.setContextMenuPolicy(Qt.CustomContextMenu) self.list.setSelectionMode(QAbstractItemView.ExtendedSelection) self.search_box = SearchBox() check_button_layout = QHBoxLayout() check_button_layout.setMargin(0) check_button_layout.setSpacing(0) check_button_layout.addWidget(QLabel(label)) check_button_layout.addStretch(1) check_button_layout.addWidget(self.checkAllButton) check_button_layout.addWidget(self.uncheckAllButton) layout.addLayout(check_button_layout) layout.addWidget(self.list) layout.addWidget(self.search_box) self.addWidget(widget) self.connect(self.checkAllButton, SIGNAL('clicked()'), self.checkAll) self.connect(self.uncheckAllButton, SIGNAL('clicked()'), self.uncheckAll) self.connect(self.list, SIGNAL('itemChanged(QListWidgetItem*)'), self.itemChanged) self.search_box.filterChanged.connect(self.filterList) # self.connect(self.search_box, SIGNAL('filterChanged(str)'), self.filterList) self.connect(self.list, SIGNAL('customContextMenuRequested(QPoint)'), self.showContextMenu) assert isinstance(model, (SelectableModelMixin, ListModelMixin)) self.model = model self.model.observable().attach(SelectableModelMixin.SELECTION_CHANGED_EVENT, self.modelChanged) self.model.observable().attach(ListModelMixin.LIST_CHANGED_EVENT, self.modelChanged) self.modelChanged() def itemChanged(self, item): """@type item: QListWidgetItem""" if item.checkState() == Qt.Checked: self.model.selectValue(str(item.text())) elif item.checkState() == Qt.Unchecked: self.model.unselectValue(str(item.text())) else: raise AssertionError("Unhandled checkstate!") def modelChanged(self): self.list.clear() items = self.model.getList() for item in items: list_item = QListWidgetItem(item) list_item.setFlags(list_item.flags() | Qt.ItemIsUserCheckable) if self.model.isValueSelected(item): list_item.setCheckState(Qt.Checked) else: list_item.setCheckState(Qt.Unchecked) self.list.addItem(list_item) self.filterList(self.search_box.filter()) def setSelectionEnabled(self, enabled): self.setEnabled(enabled) self.checkAllButton.setEnabled(enabled) self.uncheckAllButton.setEnabled(enabled) def filterList(self, filter): filter = filter.lower() for index in range(0, self.list.count()): item = self.list.item(index) text = str(item.text()).lower() if filter == "": item.setHidden(False) elif filter in text: item.setHidden(False) else: item.setHidden(True) def checkAll(self): self.model.selectAll() def uncheckAll(self): self.model.unselectAll() def checkSelected(self): items = [] for item in self.list.selectedItems(): items.append(str(item.text())) for item in items: self.model.selectValue(item) def uncheckSelected(self): items = [] for item in self.list.selectedItems(): items.append(str(item.text())) for item in items: self.model.unselectValue(item) def showContextMenu(self, point): p = self.list.mapToGlobal(point) menu = QMenu() check_selected = menu.addAction("Check selected") uncheck_selected = menu.addAction("Uncheck selected") menu.addSeparator() clear_selection = menu.addAction("Clear selection") selected_item = menu.exec_(p) if selected_item == check_selected: self.checkSelected() elif selected_item == uncheck_selected: self.uncheckSelected() elif selected_item == clear_selection: self.list.clearSelection()
class LateralWidget(QDockWidget): def __init__(self): super(LateralWidget, self).__init__() self._list_widget = QListWidget() self.setWidget(self._list_widget) Pireal.load_service("lateral", self) self._list_widget.setContextMenuPolicy(Qt.CustomContextMenu) self.connect(self._list_widget, SIGNAL("currentRowChanged(int)"), self._change_item) self.connect(self._list_widget, SIGNAL("customContextMenuRequested(const QPoint)"), self.__show_context_menu) def __show_context_menu(self, point): """ Context menu """ menu = QMenu() remove_table_act = menu.addAction(QIcon(":img/remove-rel"), self.tr("Eliminar Relación")) self.connect(remove_table_act, SIGNAL("triggered()"), self.remove_table) menu.exec_(self.mapToGlobal(point)) def _change_item(self, index): table = Pireal.get_service("container").table_widget table.stacked.setCurrentIndex(index) def add_item_list(self, items): if not self.isVisible(): self.show() for i in items: item = QListWidgetItem(i) item.setTextAlignment(Qt.AlignHCenter) self._list_widget.addItem(item) def remove_table(self): container = Pireal.get_service("container") container.remove_relation() def clear_items(self): """ Remove all items and selections in the view """ self._list_widget.clear() def get_relation_name(self): """ Returns the text of the item """ item = self._list_widget.currentItem() if item is None: return False return item.text() def current_index(self): """ Returns the current index """ return self._list_widget.currentRow() def remove_item(self, index): """ Removes the item from the given index in the list widget""" self._list_widget.takeItem(index)