def _add_float_line_edit(self,
                          text="0",
                          min_val=float('-inf'),
                          max_val=float('inf'),
                          decimals=100):
     validator = QDoubleValidator(min_val, max_val, decimals)
     validator.setLocale(QLocale.English)
     numedit = self._add_line_edit(text)
     numedit.setValidator(validator)
     return numedit
Exemplo n.º 2
0
    def __init__(self,
                 title: str = '',
                 value: float = 0,
                 min_value: float = 0,
                 max_value: float = 100,
                 displayed_value_factor: float = 1,
                 parent: QWidget = None):
        super().__init__(parent)

        self._value = value
        self._min_value = min_value
        self._max_value = max_value
        self._displayed_value_factor = displayed_value_factor

        self._title_label = QLabel(title)

        self._color = DEFAULT_BAR_COLOR

        self._locale = QLocale(QLocale.English)
        self._locale.setNumberOptions(self._locale.numberOptions()
                                      | QLocale.RejectGroupSeparator)

        validator = QDoubleValidator(self._min_value, self._max_value, 2)
        validator.setNotation(QDoubleValidator.StandardNotation)
        validator.setLocale(self._locale)

        self._value_line_edit = SliderValueLineEdit()
        self._value_line_edit.setValidator(validator)
        max_label_width = self._value_line_edit.fontMetrics().width(
            self._value_to_str(self.max_value))
        self._value_line_edit.setFixedWidth(6 + max_label_width)
        self._value_line_edit.editingFinished.connect(
            self._on_value_line_edit_editing_finished)

        h_layout = QHBoxLayout(self)
        h_layout.setContentsMargins(4, 0, 4, 0)
        # h_layout.setSpacing(0)

        h_layout.addWidget(self._title_label, 0, Qt.AlignLeft)
        h_layout.addWidget(self._value_line_edit, 0, Qt.AlignRight)

        self._update_value_line_edit()
Exemplo n.º 3
0
class NumericListValidator(QValidator):
    """
    QValidator for space-separated list of numbers. Works with float or ints
    """

    def __init__(self, float_int: type, parent=None):
        super().__init__(parent)
        if float_int is int:
            self.__numValidator = QIntValidator(self)
        elif float_int is float:
            self.__numValidator = QDoubleValidator(self)

    def validate(self, inputString: str, pos: int) -> QValidator.State:
        self.__numValidator.setLocale(QLocale(QLocale.English, QLocale.UnitedStates))
        inputString = inputString.strip(' ')
        stringList: List[str] = inputString.split(' ')
        for string in stringList:
            if self.__numValidator.validate(string, 0)[0] == QValidator.Invalid:
                return QValidator.Invalid
        return QValidator.Acceptable
Exemplo n.º 4
0
class SummaryWidget(QWidget):

    def __init__(self, plugin_manager):
        super(SummaryWidget, self).__init__()
        self.plugin_manager = plugin_manager

        icon_path = os.path.join(c.ICON_PATH, "neutral", "quote.png")
        self.setWindowIcon(QIcon(icon_path))
        self.setWindowTitle("Summary")

        self.settings = QSettings(c.SETTINGS_PATH, QSettings.IniFormat)
        self.font = QFont(self.settings.value(c.FONT, defaultValue="Arial", type=str))
        self.font.setPointSize(12)

        self.summary_text = QPlainTextEdit()
        self.summary_text.setReadOnly(True)
        self.summary_text.setFont(self.font)


        self.refresh_btn = QPushButton("Refresh")
        self.refresh_btn.clicked.connect(self.get_summary)

        self.threshold_value = 1.2
        self.threshold = QLineEdit(str(self.threshold_value))
        self.threshold.setFixedSize(40, 25)
        self.validator = QDoubleValidator()
        self.validator.setLocale(QLocale.English)
        self.threshold.setValidator(self.validator)
        self.threshold.textChanged.connect(self.threshold_changed)

        self.h_box = QHBoxLayout()
        self.h_box.addWidget(self.refresh_btn)
        self.h_box.addWidget(self.threshold)

        self.v_box = QVBoxLayout()
        self.v_box.addWidget(self.summary_text)
        self.v_box.addLayout(self.h_box)
        self.setLayout(self.v_box)
        self.resize(500, 500)

    def get_summary(self):
        if "de" in self.plugin_manager.get_language():
            language = "german"
        elif "en" in self.plugin_manager.get_language():
            language = "english"
        else:
            return

        text = self.plugin_manager.get_text()
        stop_words = set(stopwords.words(language))
        words = word_tokenize(text=text, language=language)

        # count word frequency
        freq = dict()
        for word in words:
            word = word.lower()
            if word in stop_words:
                continue
            if word in freq:
                freq[word] += 1
            else:
                freq[word] = 1

        sentences = sent_tokenize(text, language)
        sentence_freq = dict()

        # sum frequency of words in each sentence
        for word, freq in freq.items():
            for sentence in sentences:
                if word in sentence.lower():
                    if sentence in sentence_freq:
                        sentence_freq[sentence] += freq
                    else:
                        sentence_freq[sentence] = freq

        # calculate avg freq per word in sentence so long sentences dont have an advantage
        for sentence in sentences:
            sentence_freq[sentence] = sentence_freq[sentence] / len(sentence.split())

        # calc average word-frequency per sentence
        sum_sentece_freq = 0
        for sentence in sentence_freq:
            sum_sentece_freq += sentence_freq[sentence]

        avg = int(sum_sentece_freq / len(sentence_freq))

        # filter sentences that doesn't reach the threshold
        summary = ""
        for sentence in sentence_freq:
            if sentence_freq[sentence] > (self.threshold_value * avg):
                summary += " " + sentence

        self.summary_text.setPlainText(summary.strip())

    def threshold_changed(self):
        validator = self.threshold.validator()
        validator_state = validator.validate(self.threshold.text(), 0)[0]

        if validator_state == QValidator.Acceptable:
            color = '#006600'
            self.threshold_value = float(self.threshold.text())
        else:
            color = '#800000'

        self.threshold.setStyleSheet('QLineEdit { background-color: %s }' % color)

    def show(self):
        super(SummaryWidget, self).show()
        self.get_summary()