Sat, 31 Dec 2016 13:51:24 +0100
Updated copyright for 2017.
# -*- coding: utf-8 -*- # Copyright (c) 2013 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the Print Remover configuration page. """ from __future__ import unicode_literals # __IGNORE_WARNING__ import os from PyQt5.QtCore import pyqtSlot from PyQt5.QtWidgets import QListWidgetItem, QInputDialog, QLineEdit from Preferences.ConfigurationPages.ConfigurationPageBase import \ ConfigurationPageBase from .Ui_PrintRemoverPage import Ui_PrintRemoverPage import UI.PixmapCache class PrintRemoverPage(ConfigurationPageBase, Ui_PrintRemoverPage): """ Class implementing the Print Remover configuration page. """ def __init__(self, plugin): """ Constructor @param plugin reference to the plugin object """ super(PrintRemoverPage, self).__init__() self.setupUi(self) self.setObjectName("PrintRemoverPage") self.editButton.setIcon(UI.PixmapCache.getIcon( os.path.join("PrintRemover", "icons", "edit.png"))) self.addButton.setIcon(UI.PixmapCache.getIcon("plus.png")) self.addSeparatorButton.setIcon(UI.PixmapCache.getIcon( os.path.join("PrintRemover", "icons", "separatorAdd.png"))) self.deleteButton.setIcon(UI.PixmapCache.getIcon("minus.png")) self.upButton.setIcon(UI.PixmapCache.getIcon("1uparrow.png")) self.downButton.setIcon(UI.PixmapCache.getIcon("1downarrow.png")) self.editButton.setEnabled(False) self.deleteButton.setEnabled(False) self.upButton.setEnabled(False) self.downButton.setEnabled(False) self.__plugin = plugin # set initial values for pattern in self.__plugin.getPreferences("StartswithStrings"): if pattern == "--Separator--": pattern = self.tr("--Separator--") QListWidgetItem(pattern, self.patternList) def save(self): """ Public slot to save the Print Remover configuration. """ patterns = [] for row in range(self.patternList.count()): itm = self.patternList.item(row) pattern = itm.text() if pattern == self.tr("--Separator--"): pattern = "--Separator--" patterns.append(pattern) self.__plugin.setPreferences("StartswithStrings", patterns) @pyqtSlot() def on_patternList_itemSelectionChanged(self): """ Private slot to handle the selection of patterns. """ if len(self.patternList.selectedItems()) == 0: self.editButton.setEnabled(False) self.deleteButton.setEnabled(False) self.upButton.setEnabled(False) self.downButton.setEnabled(False) else: itm = self.patternList.selectedItems()[0] self.editButton.setEnabled(itm.text() != self.tr("--Separator--")) self.deleteButton.setEnabled(True) self.upButton.setEnabled(self.patternList.row(itm) > 0) self.downButton.setEnabled( self.patternList.row(itm) < self.patternList.count() - 1) @pyqtSlot() def on_editButton_clicked(self): """ Private slot to edit the selected entry. """ itm = self.patternList.selectedItems()[0] pattern, ok = QInputDialog.getText( self, self.tr("Line Start Pattern"), self.tr("Enter a line start pattern:"), QLineEdit.Normal, itm.text()) if ok and pattern: itm.setText(pattern) @pyqtSlot() def on_addButton_clicked(self): """ Private slot add a pattern to the list. """ pattern, ok = QInputDialog.getText( self, self.tr("Line Start Pattern"), self.tr("Enter a line start pattern:"), QLineEdit.Normal) if ok and pattern: itm = QListWidgetItem(pattern) if len(self.patternList.selectedItems()): row = self.patternList.row( self.patternList.selectedItems()[0]) + 1 self.patternList.insertItem(row, itm) for sitm in self.patternList.selectedItems(): sitm.setSelected(False) else: self.patternList.addItem(itm) itm.setSelected(True) @pyqtSlot() def on_addSeparatorButton_clicked(self): """ Private slot add a separator to the list. """ itm = QListWidgetItem(self.tr("--Separator--")) if len(self.patternList.selectedItems()): row = self.patternList.row( self.patternList.selectedItems()[0]) + 1 self.patternList.insertItem(row, itm) for sitm in self.patternList.selectedItems(): sitm.setSelected(False) else: self.patternList.addItem(itm) itm.setSelected(True) @pyqtSlot() def on_deleteButton_clicked(self): """ Private slot to delete the selected entry. """ itm = self.patternList.selectedItems()[0] self.patternList.takeItem(self.patternList.row(itm)) del itm @pyqtSlot() def on_upButton_clicked(self): """ Private slot to move an entry up. """ self.__moveSelectedEntry(True) @pyqtSlot() def on_downButton_clicked(self): """ Private slot to move an entry down. """ self.__moveSelectedEntry(False) def __moveSelectedEntry(self, moveUp): """ Private method to move the selected entry up or down. @param moveUp flag indicating to move the entry up (boolean) """ itm = self.patternList.selectedItems()[0] row = self.patternList.row(itm) newRow = row - 1 if moveUp else row + 1 self.patternList.takeItem(row) self.patternList.insertItem(newRow, itm) for sitm in self.patternList.selectedItems(): sitm.setSelected(False) itm.setSelected(True)