Beispiel #1
0
    def __init__(self, text='', width=0, font=None, img=None, max_height=100, align=Qt.AlignCenter):
        self.layouts = []
        self._position = Point(0, 0)
        self.leading = self.line_spacing = 0
        if font is not None:
            fm = QFontMetrics(font, img)
            self.leading = fm.leading()
            self.line_spacing = fm.lineSpacing()
        for text in text.split('<br>') if text else ():
            text, formats = parse_text_formatting(sanitize(text))
            l = QTextLayout(unescape_formatting(text), font, img)
            l.setAdditionalFormats(formats)
            to = QTextOption(align)
            to.setWrapMode(QTextOption.WrapAtWordBoundaryOrAnywhere)
            l.setTextOption(to)

            l.beginLayout()
            height = 0
            while height + 3*self.leading < max_height:
                line = l.createLine()
                if not line.isValid():
                    break
                line.setLineWidth(width)
                height += self.leading
                line.setPosition(QPointF(0, height))
                height += line.height()
            max_height -= height
            l.endLayout()
            if self.layouts:
                self.layouts.append(self.leading)
            else:
                self._position = Point(l.position().x(), l.position().y())
            self.layouts.append(l)
        if self.layouts:
            self.layouts.append(self.leading)
Beispiel #2
0
 def __init__(self, develop=False):
     self.drawn_once = False
     self.develop = develop
     self.title_font = f = QFont()
     f.setPointSize(self.TITLE_SIZE)
     f.setBold(True)
     self.title_height = QFontMetrics(f).lineSpacing() + 2
     self.body_font = f = QFont()
     f.setPointSize(self.BODY_SIZE)
     self.line_height = QFontMetrics(f).lineSpacing()
     self.total_height = max(self.LOGO_SIZE, self.title_height + 3 * self.line_height)
     self.num_font = f = QFont()
     f.setPixelSize(self.total_height)
     f.setItalic(True), f.setBold(True)
     f = QFontMetrics(f)
     self.num_ch = str(max(3, numeric_version[0]))
     self.footer_font = f = QFont()
     f.setPointSize(self.FOOTER_SIZE)
     f.setItalic(True)
     self.dpr = QApplication.instance().devicePixelRatio()
     self.pmap = QPixmap(I('library.png', allow_user_override=False))
     self.pmap.setDevicePixelRatio(self.dpr)
     self.pmap = self.pmap.scaled(int(self.dpr * self.LOGO_SIZE), int(self.dpr * self.LOGO_SIZE), transformMode=Qt.SmoothTransformation)
     self.light_brush = QBrush(QColor('#F6F3E9'))
     self.dark_brush = QBrush(QColor('#39322B'))
     pmap = QPixmap(int(self.WIDTH * self.dpr), int(self.WIDTH * self.dpr))
     pmap.setDevicePixelRatio(self.dpr)
     pmap.fill(Qt.transparent)
     QSplashScreen.__init__(self, pmap)
     self.setWindowTitle(__appname__)
 def formatText(self, font):
     metrics = QFontMetrics(font)
     width = metrics.width(self.text)
     
     need_to_modify = False
     
     while(width > Words.MAXIMUM_TEXT_WIDTH):
         need_to_modify = True
         self.text = self.text[0:-1]
         width = metrics.width(self.text)
     
     if need_to_modify:
         self.text = self.text[0:-3]
         self.text = self.text + "..."
Beispiel #4
0
 def __init__(self, button, parent=None):
     QWidget.__init__(self, parent)
     self.mouse_over = False
     self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
     self.button = button
     self.text = button.label
     self.setCursor(Qt.PointingHandCursor)
     self.fm = QFontMetrics(self.font())
     self._bi = self._di = None
Beispiel #5
0
    def __init__(self, tts_client, initial_backend_settings=None, parent=None):
        QWidget.__init__(self, parent)
        self.l = l = QFormLayout(self)
        self.tts_client = tts_client

        with BusyCursor():
            self.voice_data = self.tts_client.get_voice_data()
            self.default_system_rate = self.tts_client.default_system_rate
            self.all_sound_outputs = self.tts_client.get_sound_outputs()

        self.speed = s = QSlider(Qt.Orientation.Horizontal, self)
        s.setMinimumWidth(200)
        l.addRow(_('&Speed of speech (words per minute):'), s)
        s.setRange(self.tts_client.min_rate, self.tts_client.max_rate)
        s.setSingleStep(1)
        s.setPageStep(2)

        self.voices = v = QTableView(self)
        self.voices_model = VoicesModel(self.voice_data, parent=v)
        self.proxy_model = p = QSortFilterProxyModel(self)
        p.setFilterCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
        p.setSourceModel(self.voices_model)
        v.setModel(p)
        v.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
        v.setSortingEnabled(True)
        v.horizontalHeader().resizeSection(
            0,
            QFontMetrics(self.font()).averageCharWidth() * 25)
        v.horizontalHeader().resizeSection(
            1,
            QFontMetrics(self.font()).averageCharWidth() * 30)
        v.verticalHeader().close()
        v.verticalHeader().close()
        v.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
        v.sortByColumn(0, Qt.SortOrder.AscendingOrder)
        l.addRow(v)

        self.sound_outputs = so = QComboBox(self)
        so.addItem(_('System default'), '')
        for x in self.all_sound_outputs:
            so.addItem(x.get('description') or x['id'], x['id'])
        l.addRow(_('Sound output:'), so)

        self.backend_settings = initial_backend_settings or {}
Beispiel #6
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, index)
     result = index.data(Qt.UserRole)
     if not isinstance(result, SearchResult):
         return
     painter.save()
     try:
         p = option.palette
         c = p.HighlightedText if option.state & QStyle.State_Selected else p.Text
         group = (p.Active if option.state & QStyle.State_Active else p.Inactive)
         c = p.color(group, c)
         painter.setPen(c)
         font = option.font
         emphasis_font = QFont(font)
         emphasis_font.setBold(True)
         flags = Qt.AlignTop | Qt.TextSingleLine | Qt.TextIncludeTrailingSpaces
         rect = option.rect.adjusted(option.decorationSize.width() + 4 if result.is_hidden else 0, 0, 0, 0)
         painter.setClipRect(rect)
         before = re.sub(r'\s+', ' ', result.before)
         before_width = 0
         if before:
             before_width = painter.boundingRect(rect, flags, before).width()
         after = re.sub(r'\s+', ' ', result.after.rstrip())
         after_width = 0
         if after:
             after_width = painter.boundingRect(rect, flags, after).width()
         ellipsis_width = painter.boundingRect(rect, flags, '...').width()
         painter.setFont(emphasis_font)
         text = re.sub(r'\s+', ' ', result.text)
         match_width = painter.boundingRect(rect, flags, text).width()
         if match_width >= rect.width() - 3 * ellipsis_width:
             efm = QFontMetrics(emphasis_font)
             text = efm.elidedText(text, Qt.ElideRight, rect.width())
             painter.drawText(rect, flags, text)
         else:
             self.draw_match(
                 painter, flags, before, text, after, rect, before_width, match_width, after_width, ellipsis_width, emphasis_font, font)
     except Exception:
         import traceback
         traceback.print_exc()
     painter.restore()
Beispiel #7
0
    def __init__(self, title, msg=u'\u00a0', min=0, max=99, parent=None, cancelable=True, icon=None):
        QDialog.__init__(self, parent)
        if icon is None:
            self.l = l = QVBoxLayout(self)
        else:
            self.h = h = QHBoxLayout(self)
            self.icon = i = QLabel(self)
            if not isinstance(icon, QIcon):
                icon = QIcon(I(icon))
            i.setPixmap(icon.pixmap(64))
            h.addWidget(i, alignment=Qt.AlignTop | Qt.AlignHCenter)
            self.l = l = QVBoxLayout()
            h.addLayout(l)
            self.setWindowIcon(icon)

        self.title_label = t = QLabel(title)
        self.setWindowTitle(title)
        t.setStyleSheet('QLabel { font-weight: bold }'), t.setAlignment(Qt.AlignCenter), t.setTextFormat(Qt.PlainText)
        l.addWidget(t)

        self.bar = b = QProgressBar(self)
        b.setMinimum(min), b.setMaximum(max), b.setValue(min)
        l.addWidget(b)

        self.message = m = QLabel(self)
        fm = QFontMetrics(self.font())
        m.setAlignment(Qt.AlignCenter), m.setMinimumWidth(fm.averageCharWidth() * 80), m.setTextFormat(Qt.PlainText)
        l.addWidget(m)
        self.msg = msg

        self.button_box = bb = QDialogButtonBox(QDialogButtonBox.Abort, self)
        bb.rejected.connect(self._canceled)
        l.addWidget(bb)

        self.setWindowModality(Qt.ApplicationModal)
        self.canceled = False

        if not cancelable:
            bb.setVisible(False)
        self.cancelable = cancelable
        self.resize(self.sizeHint())
Beispiel #8
0
    def __init__(self,
                 text='',
                 width=0,
                 font=None,
                 img=None,
                 max_height=100,
                 align=Qt.AlignCenter):
        self.layouts = []
        self._position = Point(0, 0)
        self.leading = self.line_spacing = 0
        if font is not None:
            fm = QFontMetrics(font, img)
            self.leading = fm.leading()
            self.line_spacing = fm.lineSpacing()
        for text in text.split('<br>') if text else ():
            text, formats = parse_text_formatting(sanitize(text))
            l = QTextLayout(unescape_formatting(text), font, img)
            l.setAdditionalFormats(formats)
            to = QTextOption(align)
            to.setWrapMode(QTextOption.WrapAtWordBoundaryOrAnywhere)
            l.setTextOption(to)

            l.beginLayout()
            height = 0
            while height + 3 * self.leading < max_height:
                line = l.createLine()
                if not line.isValid():
                    break
                line.setLineWidth(width)
                height += self.leading
                line.setPosition(QPointF(0, height))
                height += line.height()
            max_height -= height
            l.endLayout()
            if self.layouts:
                self.layouts.append(self.leading)
            else:
                self._position = Point(l.position().x(), l.position().y())
            self.layouts.append(l)
        if self.layouts:
            self.layouts.append(self.leading)
Beispiel #9
0
 def cellSize(self, leafIndex: QModelIndex, hv: QHeaderView,
              styleOptions: QStyleOptionHeader) -> QSize:
     res = QSize()
     variant = leafIndex.data(Qt.SizeHintRole)
     if variant:
         res = variant
     fnt = QFont(hv.font())
     var = leafIndex.data(Qt.FontRole)
     if var:
         fnt = var
     fnt.setBold(True)
     fm = QFontMetrics(fnt)
     size = QSize(
         fm.size(0, leafIndex.data(Qt.DisplayRole)) +
         QSize(4, 0))  # WA: add more horizontal size (4px)
     if leafIndex.data(Qt.UserRole):
         size.transpose()
     decorationsSize = QSize(hv.style().sizeFromContents(
         QStyle.CT_HeaderSection, styleOptions, QSize(), hv))
     emptyTextSize = QSize(fm.size(0, ""))
     return res.expandedTo(size + decorationsSize - emptyTextSize)
Beispiel #10
0
    def populate(self, phrase, ts, process_space=True):
        phrase_pos = 0
        processed = False
        matches = self.__class__.whitespace.finditer(phrase)
        font = QFont(ts.font)
        if self.valign is not None:
            font.setPixelSize(font.pixelSize() / 1.5)
        fm = QFontMetrics(font)
        single_space_width = fm.width(' ')
        height, descent = fm.height(), fm.descent()
        for match in matches:
            processed = True
            left, right = match.span()
            if not process_space:
                right = left
            space_width = single_space_width * (right - left)
            word = phrase[phrase_pos:left]
            width = fm.width(word)
            if self.current_width + width < self.line_length:
                self.commit(word, width, height, descent, ts, font)
                if space_width > 0 and self.current_width + space_width < self.line_length:
                    self.add_space(space_width)
                phrase_pos = right
                continue

            # Word doesn't fit on line
            if self.hyphenate and len(word) > 3:
                tokens = hyphenate_word(word)
                for i in range(len(tokens) - 2, -1, -1):
                    word = ''.join(tokens[0:i + 1]) + '-'
                    width = fm.width(word)
                    if self.current_width + width < self.line_length:
                        self.commit(word, width, height, descent, ts, font)
                        return phrase_pos + len(word) - 1, True
            if self.current_width < 5:  # Force hyphenation as word is longer than line
                for i in range(len(word) - 5, 0, -5):
                    part = word[:i] + '-'
                    width = fm.width(part)
                    if self.current_width + width < self.line_length:
                        self.commit(part, width, height, descent, ts, font)
                        return phrase_pos + len(part) - 1, True
            # Failed to add word.
            return phrase_pos, True

        if not processed:
            return self.populate(phrase + ' ', ts, False)

        return phrase_pos, False
Beispiel #11
0
    def __init__(self, tts_client, initial_backend_settings=None, parent=None):
        QWidget.__init__(self, parent)
        self.l = l = QFormLayout(self)
        self.tts_client = tts_client

        with BusyCursor():
            self.voice_data = self.tts_client.get_voice_data()
            self.default_system_rate = self.tts_client.default_system_rate

        self.speed = s = QSlider(Qt.Orientation.Horizontal, self)
        s.setMinimumWidth(200)
        l.addRow(_('&Speed of speech (words per minute):'), s)
        delta = self.default_system_rate - 50
        s.setRange(self.default_system_rate - delta,
                   self.default_system_rate + delta)
        s.setSingleStep(10)

        self.voices = v = QTableView(self)
        self.voices_model = VoicesModel(self.voice_data, parent=v)
        self.proxy_model = p = QSortFilterProxyModel(self)
        p.setFilterCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
        p.setSourceModel(self.voices_model)
        v.setModel(p)
        v.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
        v.setSortingEnabled(True)
        v.horizontalHeader().resizeSection(
            0,
            QFontMetrics(self.font()).averageCharWidth() * 20)
        v.horizontalHeader().resizeSection(
            1,
            QFontMetrics(self.font()).averageCharWidth() * 30)
        v.verticalHeader().close()
        v.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
        v.sortByColumn(0, Qt.SortOrder.AscendingOrder)
        l.addRow(v)

        self.backend_settings = initial_backend_settings or {}
Beispiel #12
0
 def draw_match(self, painter, flags, before, text, after, rect,
                before_width, match_width, after_width, ellipsis_width,
                emphasis_font, normal_font):
     extra_width = int(rect.width() - match_width)
     if before_width < after_width:
         left_width = min(extra_width // 2, before_width)
         right_width = extra_width - left_width
     else:
         right_width = min(extra_width // 2, after_width)
         left_width = min(before_width, extra_width - right_width)
     x = rect.left()
     nfm = QFontMetrics(normal_font)
     if before_width and left_width:
         r = rect.adjusted(0, 0, 0, 0)
         r.setRight(x + left_width)
         painter.setFont(normal_font)
         ebefore = nfm.elidedText(before, Qt.TextElideMode.ElideLeft,
                                  left_width)
         if self.add_ellipsis and ebefore == before:
             ebefore = '…' + before[1:]
         r.setLeft(x)
         x += painter.drawText(r, flags, ebefore).width()
     painter.setFont(emphasis_font)
     r = rect.adjusted(0, 0, 0, 0)
     r.setLeft(x)
     painter.drawText(r, flags, text).width()
     x += match_width
     if after_width and right_width:
         painter.setFont(normal_font)
         r = rect.adjusted(0, 0, 0, 0)
         r.setLeft(x)
         eafter = nfm.elidedText(after, Qt.TextElideMode.ElideRight,
                                 right_width)
         if self.add_ellipsis and eafter == after:
             eafter = after[:-1] + '…'
         painter.setFont(normal_font)
         painter.drawText(r, flags, eafter)
Beispiel #13
0
    def _paintGroupBox(self, rect, title):
        painter = QPainter(self)
        qDrawShadeRect(painter, rect, self.palette(), True)

        if len(title) > 0:
            fnt = self.font()
            fnt.setPointSize(fnt.pointSize() - 1)
            offset = 5
            asterix = ' *'
            twidth = QFontMetrics(fnt).width(title)
            awidth = QFontMetrics(fnt).width(asterix)
            width = twidth + awidth + 2 * offset
            height = QFontMetrics(fnt).height()
            rect = QRect(rect.left() + 2 * offset,
                         rect.top() - height / 2 + 1, width, height)
            painter.fillRect(rect, self.palette().color(self.backgroundRole()))
            painter.setFont(fnt)
            painter.drawText(
                QRect(rect.left() + offset, rect.top(), twidth, rect.height()),
                Qt.AlignLeft, title)
            painter.setPen(Qt.red)
            painter.drawText(
                QRect(rect.left() + offset + twidth, rect.top(), awidth,
                      rect.height()), Qt.AlignRight, asterix)
Beispiel #14
0
    def _MakeTabBar(self, shap, text, icons, fancy):
        '''
        @param: shap QTabBar::Shap
        @param: text bool
        @param: icons bool
        @param: fancy bool
        '''
        bar = QTabBar(self)
        bar.setShape(shap)
        bar.setDocumentMode(True)
        bar.setUsesScrollButtons(True)

        if shap == QTabBar.RoundedWest:
            bar.setIconSize(QSize(22, 22))

        if fancy:
            bar.setStyle(self._proxy_style)

        if shap == QTabBar.RoundedNorth:
            self._top_layout.insertWidget(0, bar)
        else:
            self._side_layout.insertWidget(0, bar)

        # Item
        for item in self._items:
            if item.type != self.Item.Type_Tab:
                continue

            label = item.tab_label
            if shap == QTabBar.RoundedWest:
                label = QFontMetrics(self.font()).elidedText(
                    label, Qt.ElideMiddle, 100)

            tab_id = -1
            if icons and text:
                tab_id = bar.addTab(item.tab_icon, label)
            elif icons:
                tab_id = bar.addTab(item.tab_icon, '')
            elif text:
                tab_id = bar.addTab(label)

            bar.setTabToolTip(tab_id, item.tab_label)

        bar.setCurrentIndex(self._stack.currentIndex())
        bar.currentChanged.connect(self._ShowWidget)
        self._tab_bar = bar
Beispiel #15
0
    def populate(self, phrase, ts, process_space=True):
        phrase_pos = 0
        processed = False
        matches = self.__class__.whitespace.finditer(phrase)
        font = QFont(ts.font)
        if self.valign is not None:
            font.setPixelSize(font.pixelSize()/1.5)
        fm = QFontMetrics(font)
        single_space_width = fm.width(' ')
        height, descent = fm.height(), fm.descent()
        for match in matches:
            processed = True
            left, right = match.span()
            if not process_space:
                right = left
            space_width = single_space_width * (right-left)
            word = phrase[phrase_pos:left]
            width = fm.width(word)
            if self.current_width + width < self.line_length:
                self.commit(word, width, height, descent, ts, font)
                if space_width > 0 and self.current_width + space_width < self.line_length:
                    self.add_space(space_width)
                phrase_pos = right
                continue

            # Word doesn't fit on line
            if self.hyphenate and len(word) > 3:
                tokens = hyphenate_word(word)
                for i in range(len(tokens)-2, -1, -1):
                    word = ''.join(tokens[0:i+1])+'-'
                    width = fm.width(word)
                    if self.current_width + width < self.line_length:
                        self.commit(word, width, height, descent, ts, font)
                        return phrase_pos + len(word)-1, True
            if self.current_width < 5:  # Force hyphenation as word is longer than line
                for i in range(len(word)-5, 0, -5):
                    part = word[:i] + '-'
                    width = fm.width(part)
                    if self.current_width + width < self.line_length:
                        self.commit(part, width, height, descent, ts, font)
                        return phrase_pos + len(part)-1, True
            # Failed to add word.
            return phrase_pos, True

        if not processed:
            return self.populate(phrase+' ', ts, False)

        return phrase_pos, False
Beispiel #16
0
def elided_text(text, font=None, width=300, pos='middle'):
    ''' Return a version of text that is no wider than width pixels when
    rendered, replacing characters from the left, middle or right (as per pos)
    of the string with an ellipsis. Results in a string much closer to the
    limit than Qt's elidedText().'''
    from PyQt5.Qt import QFontMetrics, QApplication
    fm = QApplication.fontMetrics() if font is None else QFontMetrics(font)
    delta = 4
    ellipsis = u'\u2026'

    def remove_middle(x):
        mid = len(x) // 2
        return x[:max(0, mid - (delta//2))] + ellipsis + x[mid + (delta//2):]

    chomp = {'middle':remove_middle, 'left':lambda x:(ellipsis + x[delta:]), 'right':lambda x:(x[:-delta] + ellipsis)}[pos]
    while len(text) > delta and fm.width(text) > width:
        text = chomp(text)
    return unicode(text)
Beispiel #17
0
    def __init__(self, tts_client, initial_backend_settings=None, parent=None):
        QWidget.__init__(self, parent)
        self.l = l = QFormLayout(self)
        self.tts_client = tts_client

        self.speed = s = QSlider(Qt.Orientation.Horizontal, self)
        s.setTickPosition(QSlider.TickPosition.TicksAbove)
        s.setMinimumWidth(200)
        l.addRow(_('&Speed of speech:'), s)
        s.setRange(self.tts_client.min_rate, self.tts_client.max_rate)
        s.setSingleStep(10)
        s.setTickInterval((s.maximum() - s.minimum()) // 2)

        self.output_modules = om = QComboBox(self)
        with BusyCursor():
            self.voice_data = self.tts_client.get_voice_data()
            self.system_default_output_module = self.tts_client.system_default_output_module
        om.addItem(_('System default'), self.system_default_output_module)
        for x in self.voice_data:
            om.addItem(x, x)
        l.addRow(_('Speech s&ynthesizer:'), om)

        self.voices = v = QTableView(self)
        self.voices_model = VoicesModel(self.voice_data,
                                        self.system_default_output_module,
                                        parent=v)
        self.proxy_model = p = QSortFilterProxyModel(self)
        p.setFilterCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
        p.setSourceModel(self.voices_model)
        v.setModel(p)
        v.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
        v.setSortingEnabled(True)
        v.horizontalHeader().resizeSection(
            0,
            QFontMetrics(self.font()).averageCharWidth() * 30)
        v.verticalHeader().close()
        v.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
        v.sortByColumn(0, Qt.SortOrder.AscendingOrder)
        om.currentIndexChanged.connect(self.output_module_changed)
        l.addRow(v)

        self.backend_settings = initial_backend_settings or {}
    def addFolderToMenu(cls, receiver, menu, folder):
        '''
        @param: receiver QObject
        @param: menu Menu
        @param: folder BookmarkItem
        '''
        assert(menu)
        assert(folder)
        assert(folder.isFolder())

        subMenu = Menu(menu)
        title = QFontMetrics(subMenu.font()).elidedText(folder.title(), Qt.ElideRight, 250)
        subMenu.setTitle(title)
        subMenu.setIcon(folder.icon())

        cls.addFolderContentsToMenu(receiver, subMenu, folder)

        # QAction
        act = menu.addMenu(subMenu)
        act.setData(folder)
        act.setIconVisibleInMenu(True)
    def addUrlToMenu(cls, receiver, menu, bookmark):
        '''
        @param: receiver QObject
        @param: menu Menu
        @param: bookmark BookmarkItem
        '''
        assert(menu)
        assert(bookmark)
        assert(bookmark.isUrl())

        act = Action(menu)
        title = QFontMetrics(act.font()).elidedText(bookmark.title(), Qt.ElideRight, 250)
        act.setText(title)
        act.setData(bookmark)
        act.setIconVisibleInMenu(True)

        act.triggered.connect(receiver._bookmarkActivated)
        act.ctrlTriggered.connect(receiver._bookmarkCtrlActivated)
        act.shiftTriggered.connect(receiver._bookmarkShiftActivated)

        menu.addAction(act)
Beispiel #20
0
    def __init__(self, tts_client, initial_backend_settings, parent=None):
        QWidget.__init__(self, parent)
        self.l = l = QFormLayout(self)
        self.tts_client = tts_client

        self.output_modules = om = QComboBox(self)
        with BusyCursor():
            self.voice_data = self.tts_client.get_voice_data()
            self.system_default_output_module = self.tts_client.system_default_output_module
        om.addItem(_('System default'), self.system_default_output_module)
        l.addRow(_('Speech synthesizer:'), om)

        self.voices = v = QTableView(self)
        self.voices_model = VoicesModel(self.voice_data,
                                        self.system_default_output_module,
                                        parent=v)
        v.setModel(self.voices_model)
        v.horizontalHeader().resizeSection(
            0,
            QFontMetrics(self.font()).averageCharWidth() * 30)
        l.addRow(v)
Beispiel #21
0
 def sizeHint(self):
     fm = QFontMetrics(self.font())
     ans = QPlainTextEdit.sizeHint(self)
     ans.setWidth(fm.averageCharWidth() * 50)
     return ans
Beispiel #22
0
 def sizeHint(self):
     fm = QFontMetrics(self.font())
     return QSize(fm.averageCharWidth() * 120, 600)
    def __init__(self, parent, book_list, get_annotations_as_HTML, source):
        self.opts = parent.opts
        self.parent = parent
        self.get_annotations_as_HTML = get_annotations_as_HTML
        self.show_confidence_colors = self.opts.prefs.get(
            'annotated_books_dialog_show_confidence_as_bg_color', True)
        self.source = source

        #         QDialog.__init__(self, parent=self.opts.gui)
        SizePersistedDialog.__init__(
            self, self.opts.gui,
            'Annotations plugin:import annotations dialog')
        self.setWindowTitle(_('Import Annotations'))
        self.setWindowIcon(self.opts.icon)
        self.l = QVBoxLayout(self)
        self.setLayout(self.l)
        self.perfect_width = 0

        from calibre_plugins.annotations.appearance import default_timestamp
        friendly_timestamp_format = plugin_prefs.get(
            'appearance_timestamp_format', default_timestamp)

        # Are we collecting News clippings?
        collect_news_clippings = self.opts.prefs.get(
            'cfg_news_clippings_checkbox', False)
        news_clippings_destination = self.opts.prefs.get(
            'cfg_news_clippings_lineEdit', None)

        # Populate the table data
        self.tabledata = []
        for book_data in book_list:
            enabled = QCheckBox()
            enabled.setChecked(True)

            # last_annotation sorts by timestamp
            last_annotation = SortableTableWidgetItem(
                strftime(friendly_timestamp_format,
                         localtime(book_data['last_update'])),
                book_data['last_update'])

            # reader_app sorts case-insensitive
            reader_app = SortableTableWidgetItem(
                book_data['reader_app'], book_data['reader_app'].upper())

            # title, author sort by title_sort, author_sort
            if not book_data['title_sort']:
                book_data['title_sort'] = book_data['title']
            title = SortableTableWidgetItem(book_data['title'],
                                            book_data['title_sort'].upper())

            if not book_data['author_sort']:
                book_data['author_sort'] = book_data['author']
            author = SortableTableWidgetItem(book_data['author'],
                                             book_data['author_sort'].upper())

            genres = book_data['genre'].split(', ')
            if 'News' in genres and collect_news_clippings:
                cid = get_clippings_cid(self, news_clippings_destination)
                confidence = 5
            else:
                cid, confidence = parent.generate_confidence(book_data)

            # List order matches self.annotations_header
            this_book = [
                book_data['uuid'], book_data['book_id'], book_data['genre'],
                enabled, reader_app, title, author, last_annotation,
                book_data['annotations'], confidence
            ]
            self.tabledata.append(this_book)

        self.tv = QTableView(self)
        self.l.addWidget(self.tv)
        self.annotations_header = [
            'uuid', 'book_id', 'genre', '',
            _('Reader App'),
            _('Title'),
            _('Author'),
            _('Last Annotation'),
            _('Annotations'),
            _('Confidence')
        ]
        self.ENABLED_COL = 3
        self.READER_APP_COL = 4
        self.TITLE_COL = 5
        self.AUTHOR_COL = 6
        self.LAST_ANNOTATION_COL = 7
        self.CONFIDENCE_COL = 9
        columns_to_center = [8]
        self.tm = MarkupTableModel(self, columns_to_center=columns_to_center)
        self.tv.setModel(self.tm)
        self.tv.setShowGrid(False)
        self.tv.setFont(self.FONT)
        self.tvSelectionModel = self.tv.selectionModel()
        self.tv.setAlternatingRowColors(not self.show_confidence_colors)
        self.tv.setShowGrid(False)
        self.tv.setWordWrap(False)
        self.tv.setSelectionBehavior(self.tv.SelectRows)

        # Connect signals
        self.tv.doubleClicked.connect(self.getTableRowDoubleClick)
        self.tv.horizontalHeader().sectionClicked.connect(
            self.capture_sort_column)

        # Hide the vertical self.header
        self.tv.verticalHeader().setVisible(False)

        # Hide uuid, book_id, genre, confidence
        self.tv.hideColumn(self.annotations_header.index('uuid'))
        self.tv.hideColumn(self.annotations_header.index('book_id'))
        self.tv.hideColumn(self.annotations_header.index('genre'))
        #         self.tv.hideColumn(self.annotations_header.index(_('Confidence')))
        self.tv.hideColumn(self.CONFIDENCE_COL)

        # Set horizontal self.header props
        self.tv.horizontalHeader().setStretchLastSection(True)

        narrow_columns = [
            _('Last Annotation'),
            _('Reader App'),
            _('Annotations')
        ]
        extra_width = 10
        breathing_space = 20

        # Set column width to fit contents
        self.tv.resizeColumnsToContents()
        perfect_width = 10 + (len(narrow_columns) * extra_width)
        for i in range(3, 8):
            perfect_width += self.tv.columnWidth(i) + breathing_space
        self.tv.setMinimumSize(perfect_width, 100)
        self.perfect_width = perfect_width

        # Add some width to narrow columns
        for nc in narrow_columns:
            cw = self.tv.columnWidth(self.annotations_header.index(nc))
            self.tv.setColumnWidth(self.annotations_header.index(nc),
                                   cw + extra_width)

        # Set row height
        fm = QFontMetrics(self.FONT)
        nrows = len(self.tabledata)
        for row in xrange(nrows):
            self.tv.setRowHeight(row, fm.height() + 4)

        self.tv.setSortingEnabled(True)
        sort_column = self.opts.prefs.get('annotated_books_dialog_sort_column',
                                          self.CONFIDENCE_COL)
        sort_order = self.opts.prefs.get('annotated_books_dialog_sort_order',
                                         Qt.DescendingOrder)
        self.tv.sortByColumn(sort_column, sort_order)

        # ~~~~~~~~ Create the ButtonBox ~~~~~~~~
        self.dialogButtonBox = QDialogButtonBox(QDialogButtonBox.Cancel
                                                | QDialogButtonBox.Help)
        self.dialogButtonBox.setOrientation(Qt.Horizontal)
        self.import_button = self.dialogButtonBox.addButton(
            self.dialogButtonBox.Ok)
        self.import_button.setText(_('Import Annotations'))

        # Action buttons
        self.toggle_checkmarks_button = self.dialogButtonBox.addButton(
            _('Clear All'), QDialogButtonBox.ActionRole)
        self.toggle_checkmarks_button.setObjectName('toggle_checkmarks_button')

        scb_text = _('Show match status')
        if self.show_confidence_colors:
            scb_text = _("Hide match status")
        self.show_confidence_button = self.dialogButtonBox.addButton(
            scb_text, QDialogButtonBox.ActionRole)
        self.show_confidence_button.setObjectName('confidence_button')
        if self.show_confidence_colors:
            self.show_confidence_button.setIcon(
                get_icon('images/matches_hide.png'))
        else:
            self.show_confidence_button.setIcon(
                get_icon('images/matches_show.png'))

        self.preview_button = self.dialogButtonBox.addButton(
            _('Preview'), QDialogButtonBox.ActionRole)
        self.preview_button.setObjectName('preview_button')

        self.dialogButtonBox.clicked.connect(
            self.show_annotated_books_dialog_clicked)
        self.l.addWidget(self.dialogButtonBox)

        # Cause our dialog size to be restored from prefs or created on first usage
        self.resize_dialog()
    def __init__(self, parent, book_list, get_annotations_as_HTML, source):
        self.opts = parent.opts
        self.parent = parent
        self.get_annotations_as_HTML = get_annotations_as_HTML
        self.show_confidence_colors = self.opts.prefs.get('annotated_books_dialog_show_confidence_as_bg_color', True)
        self.source = source

#         QDialog.__init__(self, parent=self.opts.gui)
        SizePersistedDialog.__init__(self, self.opts.gui, 'Annotations plugin:import annotations dialog')
        self.setWindowTitle(u'Import Annotations')
        self.setWindowIcon(self.opts.icon)
        self.l = QVBoxLayout(self)
        self.setLayout(self.l)
        self.perfect_width = 0

        from calibre_plugins.annotations.appearance import default_timestamp
        friendly_timestamp_format = plugin_prefs.get('appearance_timestamp_format', default_timestamp)

        # Are we collecting News clippings?
        collect_news_clippings = self.opts.prefs.get('cfg_news_clippings_checkbox', False)
        news_clippings_destination = self.opts.prefs.get('cfg_news_clippings_lineEdit', None)

        # Populate the table data
        self.tabledata = []
        for book_data in book_list:
            enabled = QCheckBox()
            enabled.setChecked(True)

            # last_annotation sorts by timestamp
            last_annotation = SortableTableWidgetItem(
                strftime(friendly_timestamp_format,
                         localtime(book_data['last_update'])),
                book_data['last_update'])

            # reader_app sorts case-insensitive
            reader_app = SortableTableWidgetItem(
                book_data['reader_app'],
                book_data['reader_app'].upper())

            # title, author sort by title_sort, author_sort
            if not book_data['title_sort']:
                book_data['title_sort'] = book_data['title']
            title = SortableTableWidgetItem(
                book_data['title'],
                book_data['title_sort'].upper())

            if not book_data['author_sort']:
                book_data['author_sort'] = book_data['author']
            author = SortableTableWidgetItem(
                book_data['author'],
                book_data['author_sort'].upper())

            genres = book_data['genre'].split(', ')
            if 'News' in genres and collect_news_clippings:
                cid = get_clippings_cid(self, news_clippings_destination)
                confidence = 5
            else:
                cid, confidence = parent.generate_confidence(book_data)

            # List order matches self.annotations_header
            this_book = [
                book_data['uuid'],
                book_data['book_id'],
                book_data['genre'],
                enabled,
                reader_app,
                title,
                author,
                last_annotation,
                book_data['annotations'],
                confidence]
            self.tabledata.append(this_book)

        self.tv = QTableView(self)
        self.l.addWidget(self.tv)
        self.annotations_header = ['uuid', 'book_id', 'genre', '', 'Reader App', 'Title',
                                   'Author', 'Last Annotation', 'Annotations', 'Confidence']
        self.ENABLED_COL = 3
        self.READER_APP_COL = 4
        self.TITLE_COL = 5
        self.AUTHOR_COL = 6
        self.LAST_ANNOTATION_COL = 7
        self.CONFIDENCE_COL = 9
        columns_to_center = [8]
        self.tm = MarkupTableModel(self, columns_to_center=columns_to_center)
        self.tv.setModel(self.tm)
        self.tv.setShowGrid(False)
        self.tv.setFont(self.FONT)
        self.tvSelectionModel = self.tv.selectionModel()
        self.tv.setAlternatingRowColors(not self.show_confidence_colors)
        self.tv.setShowGrid(False)
        self.tv.setWordWrap(False)
        self.tv.setSelectionBehavior(self.tv.SelectRows)

        # Connect signals
        self.tv.doubleClicked.connect(self.getTableRowDoubleClick)
        self.tv.horizontalHeader().sectionClicked.connect(self.capture_sort_column)

        # Hide the vertical self.header
        self.tv.verticalHeader().setVisible(False)

        # Hide uuid, book_id, genre, confidence
        self.tv.hideColumn(self.annotations_header.index('uuid'))
        self.tv.hideColumn(self.annotations_header.index('book_id'))
        self.tv.hideColumn(self.annotations_header.index('genre'))
        self.tv.hideColumn(self.annotations_header.index('Confidence'))

        # Set horizontal self.header props
        self.tv.horizontalHeader().setStretchLastSection(True)

        narrow_columns = ['Last Annotation', 'Reader App', 'Annotations']
        extra_width = 10
        breathing_space = 20

        # Set column width to fit contents
        self.tv.resizeColumnsToContents()
        perfect_width = 10 + (len(narrow_columns) * extra_width)
        for i in range(3, 8):
            perfect_width += self.tv.columnWidth(i) + breathing_space
        self.tv.setMinimumSize(perfect_width, 100)
        self.perfect_width = perfect_width

        # Add some width to narrow columns
        for nc in narrow_columns:
            cw = self.tv.columnWidth(self.annotations_header.index(nc))
            self.tv.setColumnWidth(self.annotations_header.index(nc), cw + extra_width)

        # Set row height
        fm = QFontMetrics(self.FONT)
        nrows = len(self.tabledata)
        for row in xrange(nrows):
            self.tv.setRowHeight(row, fm.height() + 4)

        self.tv.setSortingEnabled(True)
        sort_column = self.opts.prefs.get('annotated_books_dialog_sort_column',
                                          self.annotations_header.index('Confidence'))
        sort_order = self.opts.prefs.get('annotated_books_dialog_sort_order',
                                         Qt.DescendingOrder)
        self.tv.sortByColumn(sort_column, sort_order)

        # ~~~~~~~~ Create the ButtonBox ~~~~~~~~
        self.dialogButtonBox = QDialogButtonBox(QDialogButtonBox.Cancel | QDialogButtonBox.Help)
        self.dialogButtonBox.setOrientation(Qt.Horizontal)
        self.import_button = self.dialogButtonBox.addButton(self.dialogButtonBox.Ok)
        self.import_button.setText('Import Annotations')

        # Action buttons
        self.toggle_checkmarks_button = self.dialogButtonBox.addButton('Clear All', QDialogButtonBox.ActionRole)
        self.toggle_checkmarks_button.setObjectName('toggle_checkmarks_button')

        scb_text = 'Show match status'
        if self.show_confidence_colors:
            scb_text = "Hide match status"
        self.show_confidence_button = self.dialogButtonBox.addButton(scb_text, QDialogButtonBox.ActionRole)
        self.show_confidence_button.setObjectName('confidence_button')
        if self.show_confidence_colors:
            self.show_confidence_button.setIcon(get_icon('images/matches_hide.png'))
        else:
            self.show_confidence_button.setIcon(get_icon('images/matches_show.png'))

        self.preview_button = self.dialogButtonBox.addButton('Preview', QDialogButtonBox.ActionRole)
        self.preview_button.setObjectName('preview_button')

        self.dialogButtonBox.clicked.connect(self.show_annotated_books_dialog_clicked)
        self.l.addWidget(self.dialogButtonBox)

        # Cause our dialog size to be restored from prefs or created on first usage
        self.resize_dialog()
Beispiel #25
0
 def do_size_hint(self, option, index):
     text = index.data(Qt.DisplayRole) or ''
     font = QFont(option.font)
     font.setPointSize(QFontInfo(font).pointSize() * 1.5)
     m = QFontMetrics(font)
     return QSize(m.width(text), m.height())
    def paint(self, painter, option, index):  # noqa C901
        '''
        @param painter QPainter
        @param option index QStyleOptionViewItem
        @param option index QModelIndex
        '''
        from ..LocationBar import LocationBar
        opt = QStyleOptionViewItem(option)
        self.initStyleOption(opt, index)

        w = opt.widget
        if w:
            style = w.style()
        else:
            style = QApplication.style()

        height = opt.rect.height()
        center = height / 2 + opt.rect.top()

        # Prepare link font
        # QFont
        linkFont = opt.font
        linkFont.setPointSize(linkFont.pointSize() - 1)

        linkMetrics = QFontMetrics(linkFont)

        leftPosition = self._padding * 2
        rightPosition = opt.rect.right() - self._padding

        opt.state |= QStyle.State_Active

        if opt.state & QStyle.State_Selected:
            iconMode = QIcon.Selected
            colorRole = QPalette.HighlightedText
            colorLinkRole = QPalette.HighlightedText
        else:
            iconMode = QIcon.Normal
            colorRole = QPalette.Text
            colorLinkRole = QPalette.Link

        if const.OS_WIN:
            opt.palette.setColor(QPalette.All, QPalette.HighlightedText,
                opt.palette.color(QPalette.Active, QPalette.Text))
            opt.palette.setColor(QPalette.All, QPalette.Highlight,
                opt.palette.base().color().darker(108))

        textPalette = QPalette(opt.palette)
        if opt.state & QStyle.State_Enabled:
            textPalette.setCurrentColorGroup(QPalette.Normal)
        else:
            textPalette.setCurrentColorGroup(QPalette.Disabled)

        # Draw background
        style.drawPrimitive(QStyle.PE_PanelItemViewItem, opt, painter, w)

        isVisitSearchItem = index.data(LocationCompleterModel.VisitSearchItemRole)
        isSearchSuggestion = index.data(LocationCompleterModel.SearchSuggestionRole)

        loadAction = LocationBar.LoadAction()
        isWebSearch = isSearchSuggestion

        # BookmarkItem
        bookmark = index.data(LocationCompleterModel.BookmarkItemRole)

        if isVisitSearchItem:
            text = index.data(LocationCompleterModel.SearchStringRole)
            loadAction = LocationBar.loadAction(text)
            isWebSearch = loadAction.type == LocationBar.LoadAction.Search
            if not self._forceVisitItem:
                bookmark = loadAction.bookmark

        # Draw icon
        iconSize = 16
        iconYPos = center - iconSize / 2
        iconRect = QRect(leftPosition, iconYPos, iconSize, iconSize)
        icon = index.data(Qt.DecorationRole)
        if not icon:
            icon = QIcon()
        pixmap = icon.pixmap(iconSize)
        if isSearchSuggestion or (isVisitSearchItem and isWebSearch):
            pixmap = QIcon.fromTheme('edit-find', QIcon(':/icons/menu/search-icon.svg')).pixmap(iconSize, iconMode)
        if isVisitSearchItem and bookmark:
            pixmap = bookmark.icon().pixmap(iconSize)
        elif loadAction.type == LocationBar.LoadAction.Search:
            if loadAction.searchEngine.name != LocationBar.searchEngine().name:
                pixmap = loadAction.searchEngine.icon.pixmap(iconSize)

        painter.drawPixmap(iconRect, pixmap)
        leftPosition = iconRect.right() + self._padding * 2

        # Draw star to bookmark items
        starPixmapWidth = 0
        if bookmark:
            icon = IconProvider.instance().bookmarkIcon
            starSize = QSize(16, 16)
            starPixmapWidth = starSize.width()
            pos = QPoint(rightPosition - starPixmapWidth, center - starSize.height() / 2)
            starRect = QRect(pos, starSize)
            painter.drawPixmap(starRect, icon.pixmap(starSize, iconMode))

        searchText = index.data(LocationCompleterModel.SearchStringRole)

        # Draw title
        leftPosition += 2
        titleRect = QRect(leftPosition, center - opt.fontMetrics.height() / 2,
                opt.rect.width() * 0.6, opt.fontMetrics.height())
        title = index.data(LocationCompleterModel.TitleRole)
        painter.setFont(opt.font)

        if isVisitSearchItem:
            if bookmark:
                title = bookmark.title()
            else:
                title = index.data(LocationCompleterModel.SearchStringRole)
                searchText = ''

        leftPosition += self.viewItemDrawText(painter, opt, titleRect, title,
                textPalette.color(colorRole), searchText)
        leftPosition += self._padding * 2

        # Trim link to maximum number characters that can be visible,
        # otherwise there may be perf issue with huge URLs
        maxChars = int((opt.rect.width() - leftPosition) / opt.fontMetrics.width('i'))
        link = index.data(Qt.DisplayRole)
        if not link.startswith('data') and not link.startswith('javascript'):
            link = unquote(link)[:maxChars]
        else:
            link = link[:maxChars]

        if isVisitSearchItem or isSearchSuggestion:
            if not (opt.state & QStyle.State_Selected) and not (opt.state & QStyle.State_MouseOver):
                link = ''
            elif isVisitSearchItem and (not isWebSearch or self._forceVisitItem):
                link = _('Visit')
            else:
                searchEngineName = loadAction.searchEngine.name
                if not searchEngineName:
                    searchEngineName = LocationBar.searchEngine().name
                link = _('Search with %s') % searchEngineName

        if bookmark:
            link = bookmark.url().toString()

        # Draw separator
        if link:
            separator = '-'
            separatorRect = QRect(leftPosition, center - linkMetrics.height() / 2,
                    linkMetrics.width(separator), linkMetrics.height())
            style.drawItemText(painter, separatorRect, Qt.AlignCenter, textPalette,
                    True, separator, colorRole)
            leftPosition += separatorRect.width() + self._padding * 2

        # Draw link
        leftLinkEdge = leftPosition
        rightLinkEdge = rightPosition - self._padding - starPixmapWidth
        linkRect = QRect(leftLinkEdge, center - linkMetrics.height() / 2,
                rightLinkEdge - leftLinkEdge, linkMetrics.height())
        painter.setFont(linkFont)

        # Darw url (or switch to tab)
        tabPos = index.data(LocationCompleterModel.TabPositionTabRole)

        if gVar.appSettings.showSwitchTab and not self._forceVisitItem and tabPos != -1:
            tabIcon = QIcon(':/icons/menu/tab.svg')
            iconRect = QRect(linkRect)
            iconRect.setX(iconRect.x())
            iconRect.setWidth(16)
            painter.drawPixmap(iconRect, tabIcon.pixmap(iconRect.size(), iconMode))

            textRect = QRect(linkRect)
            textRect.setX(textRect.x() + self._padding + 16 + self._padding)
            self.viewItemDrawText(painter, opt, textRect, _('Switch to tab'),
                    textPalette.color(colorLinkRole))
        elif isVisitSearchItem or isSearchSuggestion:
            self.viewItemDrawText(painter, opt, linkRect, link, textPalette.color(colorLinkRole))
        else:
            self.viewItemDrawText(painter, opt, linkRect, link, textPalette.color(colorLinkRole),
                    searchText)
Beispiel #27
0
    def __init__(self, args, force_calibre_style=False, override_program_name=None, headless=False, color_prefs=gprefs):
        self.file_event_hook = None
        if override_program_name:
            args = [override_program_name] + args[1:]
        if headless:
            if not args:
                args = sys.argv[:1]
            args.extend(['-platformpluginpath', sys.extensions_location, '-platform', 'headless'])
        self.headless = headless
        qargs = [i.encode('utf-8') if isinstance(i, unicode) else i for i in args]
        self.pi = plugins['progress_indicator'][0]
        if not isosx and not headless:
            # On OS X high dpi scaling is turned on automatically by the OS, so we dont need to set it explicitly
            setup_hidpi()
        QApplication.setOrganizationName('calibre-ebook.com')
        QApplication.setOrganizationDomain(QApplication.organizationName())
        QApplication.setApplicationVersion(__version__)
        QApplication.setApplicationName(APP_UID)
        QApplication.__init__(self, qargs)
        self.setAttribute(Qt.AA_UseHighDpiPixmaps)
        self.setAttribute(Qt.AA_SynthesizeTouchForUnhandledMouseEvents, False)
        try:
            base_dir()
        except EnvironmentError as err:
            if not headless:
                show_temp_dir_error(err)
            raise SystemExit('Failed to create temporary directory')
        if DEBUG and not headless:
            prints('devicePixelRatio:', self.devicePixelRatio())
            s = self.primaryScreen()
            if s:
                prints('logicalDpi:', s.logicalDotsPerInchX(), 'x', s.logicalDotsPerInchY())
                prints('physicalDpi:', s.physicalDotsPerInchX(), 'x', s.physicalDotsPerInchY())
        if not iswindows:
            self.setup_unix_signals()
        if islinux or isbsd:
            self.setAttribute(Qt.AA_DontUseNativeMenuBar, 'CALIBRE_NO_NATIVE_MENUBAR' in os.environ)
        self.setup_styles(force_calibre_style)
        self.setup_ui_font()
        if not self.using_calibre_style and self.style().objectName() == 'fusion':
            # Since Qt is using the fusion style anyway, specialize it
            self.load_calibre_style()
        fi = gprefs['font']
        if fi is not None:
            font = QFont(*(fi[:4]))
            s = gprefs.get('font_stretch', None)
            if s is not None:
                font.setStretch(s)
            QApplication.setFont(font)
        self.line_height = max(12, QFontMetrics(self.font()).lineSpacing())

        dl = QLocale(get_lang())
        if unicode(dl.bcp47Name()) != u'C':
            QLocale.setDefault(dl)
        global gui_thread, qt_app
        gui_thread = QThread.currentThread()
        self._translator = None
        self.load_translations()
        qt_app = self
        self._file_open_paths = []
        self._file_open_lock = RLock()

        if not isosx:
            # OS X uses a native color dialog that does not support custom
            # colors
            self.color_prefs = color_prefs
            self.read_custom_colors()
            self.lastWindowClosed.connect(self.save_custom_colors)

        if isxp:
            error_dialog(None, _('Windows XP not supported'), '<p>' + _(
                'calibre versions newer than 2.0 do not run on Windows XP. This is'
                ' because the graphics toolkit calibre uses (Qt 5) crashes a lot'
                ' on Windows XP. We suggest you stay with <a href="%s">calibre 1.48</a>'
                ' which works well on Windows XP.') % 'http://download.calibre-ebook.com/1.48.0/', show=True)
            raise SystemExit(1)

        if iswindows:
            # On windows the highlighted colors for inactive widgets are the
            # same as non highlighted colors. This is a regression from Qt 4.
            # https://bugreports.qt-project.org/browse/QTBUG-41060
            p = self.palette()
            for role in (p.Highlight, p.HighlightedText, p.Base, p.AlternateBase):
                p.setColor(p.Inactive, role, p.color(p.Active, role))
            self.setPalette(p)

            # Prevent text copied to the clipboard from being lost on quit due to
            # Qt 5 bug: https://bugreports.qt-project.org/browse/QTBUG-41125
            self.aboutToQuit.connect(self.flush_clipboard)
Beispiel #28
0
    def initialize(self, parent):
        '''
        __init__ is called on SizePersistedDialog()
        '''
        #self.connected_device = parent.opts.gui.device_manager.device
        self.parent = parent
        self.prefs = parent.prefs
        self.verbose = parent.verbose

        self.setupUi(self)
        self._log_location()

        # Subscribe to Marvin driver change events
        #self.connected_device.marvin_device_signals.reader_app_status_changed.connect(
        #    self.marvin_status_changed)

        self.setWindowTitle("Edit CSS")

        # Remove the placeholder
        self.placeholder.setParent(None)
        self.placeholder.deleteLater()
        self.placeholder = None

        # Replace the placeholder
        self.html_wv = QWebView()
        self.html_wv.sizeHint = self.wv_sizeHint
        self.html_wv.setSizePolicy(QSizePolicy.MinimumExpanding,
                                   QSizePolicy.MinimumExpanding)
        self.html_wv.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
        self.html_wv.linkClicked.connect(self.link_clicked)
        self.splitter.insertWidget(0, self.html_wv)

        # Add the Accept button
        self.accept_button = self.bb.addButton('Update',
                                               QDialogButtonBox.AcceptRole)
        self.accept_button.setDefault(True)

        # ~~~~~~~~ Configure the CSS control ~~~~~~~~
        if isosx:
            FONT = QFont('Monaco', 11)
        elif iswindows:
            FONT = QFont('Lucida Console', 9)
        elif islinux:
            FONT = QFont('Monospace', 9)
            FONT.setStyleHint(QFont.TypeWriter)
        self.css_pte.setFont(FONT)

        # Tab width
        width = QFontMetrics(FONT).width(" ") * 4
        self.css_pte.setTabStopWidth(width)

        # Restore/init the stored CSS
        self.css_pte.setPlainText(self.prefs.get('injected_css', ''))

        # Populate the HTML content
        rendered_html = self.inject_css(SAMPLE_HTML)
        self.html_wv.setHtml(rendered_html)

        # Restore the splitter
        split_points = self.prefs.get('css_editor_split_points')
        if split_points:
            self.splitter.setSizes(split_points)

        # Hook the QPlainTextEdit box
        self.css_pte.textChanged.connect(self.preview_css)

        # Hook the button events
        self.bb.clicked.connect(self.dispatch_button_click)

        self.resize_dialog()
Beispiel #29
0
 def sizeHint(self):
     fm = QFontMetrics(self.font())
     ans = QPlainTextEdit.sizeHint(self)
     ans.setWidth(fm.averageCharWidth() * 50)
     return ans
Beispiel #30
0
 def do_size_hint(self, option, index):
     text = index.data(Qt.DisplayRole) or ''
     font = QFont(option.font)
     font.setPointSize(QFontInfo(font).pointSize() * 1.5)
     m = QFontMetrics(font)
     return QSize(m.width(text), m.height())
Beispiel #31
0
class LayoutItem(QWidget):

    def __init__(self, button, parent=None):
        QWidget.__init__(self, parent)
        self.mouse_over = False
        self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
        self.button = button
        self.text = button.label
        self.setCursor(Qt.CursorShape.PointingHandCursor)
        self.fm = QFontMetrics(self.font())
        self._bi = self._di = None

    @property
    def bright_icon(self):
        if self._bi is None:
            self._bi = self.button.icon().pixmap(ICON_SZ, ICON_SZ)
        return self._bi

    @property
    def dull_icon(self):
        if self._di is None:
            self._di = self.button.icon().pixmap(ICON_SZ, ICON_SZ, mode=QIcon.Mode.Disabled)
        return self._di

    def event(self, ev):
        m = None
        et = ev.type()
        if et == ev.Enter:
            m = True
        elif et == ev.Leave:
            m = False
        if m is not None and m != self.mouse_over:
            self.mouse_over = m
            self.update()
        return QWidget.event(self, ev)

    def sizeHint(self):
        br = self.fm.boundingRect(self.text)
        w = max(br.width(), ICON_SZ) + 10
        h = 2 * self.fm.lineSpacing() + ICON_SZ + 8
        return QSize(w, h)

    def paintEvent(self, ev):
        shown = self.button.isChecked()
        ls = self.fm.lineSpacing()
        painter = QPainter(self)
        if self.mouse_over:
            tool = QStyleOption()
            tool.rect = self.rect()
            tool.state = QStyle.StateFlag.State_Raised | QStyle.StateFlag.State_Active | QStyle.StateFlag.State_MouseOver
            s = self.style()
            s.drawPrimitive(QStyle.PrimitiveElement.PE_PanelButtonTool, tool, painter, self)
        painter.drawText(
            0, 0,
            self.width(),
            ls, Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine, self.text)
        text = _('Hide') if shown else _('Show')
        f = self.font()
        f.setBold(True)
        painter.setFont(f)
        painter.drawText(
            0, self.height() - ls,
            self.width(),
            ls, Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine, text)
        x = (self.width() - ICON_SZ) // 2
        y = ls + (self.height() - ICON_SZ - 2 * ls) // 2
        pmap = self.bright_icon if shown else self.dull_icon
        painter.drawPixmap(x, y, pmap)
        painter.end()
Beispiel #32
0
 def fontMetrics(self):
     return QFontMetrics(self.document().defaultFont())
Beispiel #33
0
class LayoutItem(QWidget):

    def __init__(self, button, parent=None):
        QWidget.__init__(self, parent)
        self.mouse_over = False
        self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.button = button
        self.text = button.label
        self.setCursor(Qt.PointingHandCursor)
        self.fm = QFontMetrics(self.font())
        self._bi = self._di = None

    @property
    def bright_icon(self):
        if self._bi is None:
            self._bi = self.button.icon().pixmap(ICON_SZ, ICON_SZ)
        return self._bi

    @property
    def dull_icon(self):
        if self._di is None:
            self._di = self.button.icon().pixmap(ICON_SZ, ICON_SZ, mode=QIcon.Disabled)
        return self._di

    def event(self, ev):
        m = None
        et = ev.type()
        if et == ev.Enter:
            m = True
        elif et == ev.Leave:
            m = False
        if m is not None and m != self.mouse_over:
            self.mouse_over = m
            self.update()
        return QWidget.event(self, ev)

    def sizeHint(self):
        br = self.fm.boundingRect(self.text)
        w = max(br.width(), ICON_SZ) + 10
        h = 2 * self.fm.lineSpacing() + ICON_SZ + 8
        return QSize(w, h)

    def paintEvent(self, ev):
        shown = self.button.isChecked()
        ls = self.fm.lineSpacing()
        painter = QPainter(self)
        if self.mouse_over:
            tool = QStyleOption()
            tool.rect = self.rect()
            tool.state = QStyle.State_Raised | QStyle.State_Active | QStyle.State_MouseOver
            s = self.style()
            s.drawPrimitive(QStyle.PE_PanelButtonTool, tool, painter, self)
        painter.drawText(
            0, 0,
            self.width(),
            ls, Qt.AlignCenter | Qt.TextSingleLine, self.text)
        text = _('Hide') if shown else _('Show')
        f = self.font()
        f.setBold(True)
        painter.setFont(f)
        painter.drawText(
            0, self.height() - ls,
            self.width(),
            ls, Qt.AlignCenter | Qt.TextSingleLine, text)
        x = (self.width() - ICON_SZ) // 2
        y = ls + (self.height() - ICON_SZ - 2 * ls) // 2
        pmap = self.bright_icon if shown else self.dull_icon
        painter.drawPixmap(x, y, pmap)
        painter.end()
    def __init__(self, parent=None):
        QTextEdit.__init__(self, parent)
        self.setTabChangesFocus(True)
        self.document().setDefaultStyleSheet(css())
        font = self.font()
        f = QFontInfo(font)
        delta = tweaks['change_book_details_font_size_by'] + 1
        if delta:
            font.setPixelSize(f.pixelSize() + delta)
            self.setFont(font)
        f = QFontMetrics(self.font())
        self.em_size = f.horizontalAdvance('m')
        self.base_url = None
        self._parent = weakref.ref(parent)
        self.comments_pat = re.compile(r'<!--.*?-->', re.DOTALL)

        extra_shortcuts = {
            'bold': 'Bold',
            'italic': 'Italic',
            'underline': 'Underline',
        }

        for rec in (
            ('bold', 'format-text-bold', _('Bold'), True),
            ('italic', 'format-text-italic', _('Italic'), True),
            ('underline', 'format-text-underline', _('Underline'), True),
            ('strikethrough', 'format-text-strikethrough', _('Strikethrough'), True),
            ('superscript', 'format-text-superscript', _('Superscript'), True),
            ('subscript', 'format-text-subscript', _('Subscript'), True),
            ('ordered_list', 'format-list-ordered', _('Ordered list'), True),
            ('unordered_list', 'format-list-unordered', _('Unordered list'), True),

            ('align_left', 'format-justify-left', _('Align left'), True),
            ('align_center', 'format-justify-center', _('Align center'), True),
            ('align_right', 'format-justify-right', _('Align right'), True),
            ('align_justified', 'format-justify-fill', _('Align justified'), True),
            ('undo', 'edit-undo', _('Undo'), ),
            ('redo', 'edit-redo', _('Redo'), ),
            ('remove_format', 'edit-clear', _('Remove formatting'), ),
            ('copy', 'edit-copy', _('Copy'), ),
            ('paste', 'edit-paste', _('Paste'), ),
            ('paste_and_match_style', 'edit-paste', _('Paste and match style'), ),
            ('cut', 'edit-cut', _('Cut'), ),
            ('indent', 'format-indent-more', _('Increase indentation'), ),
            ('outdent', 'format-indent-less', _('Decrease indentation'), ),
            ('select_all', 'edit-select-all', _('Select all'), ),

            ('color', 'format-text-color', _('Foreground color')),
            ('background', 'format-fill-color', _('Background color')),
            ('insert_link', 'insert-link', _('Insert link or image'),),
            ('insert_hr', 'format-text-hr', _('Insert separator'),),
            ('clear', 'trash', _('Clear')),
        ):
            name, icon, text = rec[:3]
            checkable = len(rec) == 4
            ac = QAction(QIcon(I(icon + '.png')), text, self)
            if checkable:
                ac.setCheckable(checkable)
            setattr(self, 'action_'+name, ac)
            ss = extra_shortcuts.get(name)
            if ss is not None:
                ac.setShortcut(QKeySequence(getattr(QKeySequence, ss)))
            ac.triggered.connect(getattr(self, 'do_' + name))

        self.action_block_style = QAction(QIcon(I('format-text-heading.png')),
                _('Style text block'), self)
        self.action_block_style.setToolTip(
                _('Style the selected text block'))
        self.block_style_menu = QMenu(self)
        self.action_block_style.setMenu(self.block_style_menu)
        self.block_style_actions = []
        h = _('Heading {0}')
        for text, name in (
            (_('Normal'), 'p'),
            (h.format(1), 'h1'),
            (h.format(2), 'h2'),
            (h.format(3), 'h3'),
            (h.format(4), 'h4'),
            (h.format(5), 'h5'),
            (h.format(6), 'h6'),
            (_('Blockquote'), 'blockquote'),
        ):
            ac = QAction(text, self)
            self.block_style_menu.addAction(ac)
            ac.block_name = name
            ac.setCheckable(True)
            self.block_style_actions.append(ac)
            ac.triggered.connect(self.do_format_block)

        self.setHtml('')
        self.copyAvailable.connect(self.update_clipboard_actions)
        self.update_clipboard_actions(False)
        self.selectionChanged.connect(self.update_selection_based_actions)
        self.update_selection_based_actions()
        connect_lambda(self.undoAvailable, self, lambda self, yes: self.action_undo.setEnabled(yes))
        connect_lambda(self.redoAvailable, self, lambda self, yes: self.action_redo.setEnabled(yes))
        self.action_undo.setEnabled(False), self.action_redo.setEnabled(False)
        self.textChanged.connect(self.update_cursor_position_actions)
        self.cursorPositionChanged.connect(self.update_cursor_position_actions)
        self.textChanged.connect(self.data_changed)
        self.update_cursor_position_actions()
Beispiel #35
0
 def initStyleOption(self, option, index):
     QStyledItemDelegate.initStyleOption(self, option, index)
     option.font = QApplication.instance().font(
     ) if index.row() <= 0 else self.parent().rating_font
     option.fontMetrics = QFontMetrics(option.font)
Beispiel #36
0
 def sizeHint(self):
     fm = QFontMetrics(self.font())
     return QSize(fm.averageCharWidth() * 120, 600)
    def viewItemDrawText(self, painter, option, rect, text, color, searchText=''):  # noqa C901
        '''
        @note: most of codes taken from QCommonStylePrivate::viewItemDrawText
            added highlighting and simplified for single-line textlayouts
        @param painter QPainter
        @param option QStyleOptionViewItem
        @param rect QRect
        @param text QString
        @param color QColor
        @param searchText QString
        @return: int
        '''
        if not text:
            return 0

        fontMetrics = QFontMetrics(painter.font())
        elidedText = fontMetrics.elidedText(text, option.textElideMode, rect.width())
        textOption = QTextOption()
        textOption.setWrapMode(QTextOption.NoWrap)
        textOption.setAlignment(QStyle.visualAlignment(textOption.textDirection(),
            option.displayAlignment))
        textLayout = QTextLayout()
        textLayout.setFont(painter.font())
        textLayout.setText(elidedText)
        textLayout.setTextOption(textOption)

        if searchText:
            # QList<int>
            delimiters = []
            searchStrings = [ item.strip() for item in searchText.split(' ') ]
            searchStrings = [ item for item in searchStrings if item ]
            # Look for longer parts first
            searchStrings.sort(key=lambda x: len(x), reverse=True)

            text0 = text.lower()
            for string in searchStrings:
                string0 = string.lower()
                delimiter = text0.find(string0)

                while delimiter != -1:
                    start = delimiter
                    end = delimiter + len(string)
                    alreadyContains = False
                    for idx in range(0, len(delimiters), 2):
                        dStart = delimiters[idx]
                        dEnd = delimiters[idx+1]

                        if dStart <= start and dEnd >= end:
                            alreadyContains = True
                            break

                    if not alreadyContains:
                        delimiters.append(start)
                        delimiters.append(end)

                    delimiter = text0.find(string0, end)

            # We need to sort delimiters to properly paint all parts that user typed
            delimiters.sort()

            # If we don't find any match, just paint it withoutany highlight
            if delimiters and len(delimiters) % 2 == 0:
                highlightParts = []  # QList<QTextLayout::FormatRange>

                while delimiters:
                    highlightedPart = QTextLayout.FormatRange()
                    start = delimiters.pop(0)
                    end = delimiters.pop(0)
                    highlightedPart.start = start
                    highlightedPart.length = end - start
                    highlightedPart.format.setFontWeight(QFont.Bold)
                    highlightedPart.format.setUnderlineStyle(QTextCharFormat.SingleUnderline)
                    highlightParts.append(highlightedPart)

                textLayout.setAdditionalFormats(highlightParts)

        # do layout
        self._s_viewItemDrawText(textLayout, rect.width())

        if textLayout.lineCount() <= 0:
            return 0

        textLine = textLayout.lineAt(0)

        # if elidedText after highlighting is longer than available width then
        # re-elide it and redo layout
        diff = textLine.naturalTextWidth() - rect.width()
        if diff > 0:
            # TODO: ?
            elidedText = fontMetrics.elidedText(elidedText, option.textElideMode,
                    rect.width() - diff)
            textLayout.setText(elidedText)
            # redo layout
            self._s_viewItemDrawText(textLayout, rect.width())

            if textLayout.lineCount() <= 0:
                return 0
            textLine = textLayout.lineAt(0)

        # draw line
        painter.setPen(color)
        # qreal
        width = max(rect.width(), textLayout.lineAt(0).width())
        # QRect
        layoutRect = QStyle.alignedRect(option.direction, option.displayAlignment,
                QSize(int(width), int(textLine.height())), rect)
        # QPointF
        position = layoutRect.topLeft()

        textLine.draw(painter, position)

        return int(min(rect.width(), textLayout.lineAt(0).naturalTextWidth()))