def setup_ui(self): self.l = l = QVBoxLayout(self) self.setLayout(l) self.msg = m = QLabel(self.msg or _( 'Choose the folder into which the files will be placed')) l.addWidget(m) m.setWordWrap(True) self.folders = f = QTreeWidget(self) f.setHeaderHidden(True) f.itemDoubleClicked.connect(self.accept) l.addWidget(f) f.setContextMenuPolicy(Qt.CustomContextMenu) f.customContextMenuRequested.connect(self.show_context_menu) self.root = QTreeWidgetItem(f, ('/',)) def process(node, parent): parent.setIcon(0, QIcon(I('mimetypes/dir.png'))) for child in sorted(node, key=numeric_sort_key): c = QTreeWidgetItem(parent, (child,)) process(node[child], c) process(create_folder_tree(current_container()), self.root) self.root.setSelected(True) f.expandAll() l.addWidget(self.bb)
def create_item(self, parent, child, idx=-1): if idx == -1: c = QTreeWidgetItem(parent) else: c = QTreeWidgetItem() parent.insertChild(idx, c) self.populate_item(c, child) return c
def update_widget(self, services): """ Update the QTreeWidget and its items :param services: list of :class:`Services <alignak_app.items.service.Service>` items :type services: list """ self.services = services # Update services dashboard self.services_dashboard.update_widget(self.services) # Clear QTreeWidget self.services_tree_widget.clear() self.services_tree_widget.setIconSize(QSize(16, 16)) if self.services: # Set as "Global" aggregation who are empty for service in self.services: if not service.data['aggregation']: service.data['aggregation'] = 'Global' # First sort list by state then by aggregation newlist = sorted(self.services, key=lambda s: itemgetter( 'ls_state', 'ls_acknowledged', 'aggregation') (s.data)) self.services = newlist # Get list of aggregations aggregations = [] for service in self.services: if service.data['aggregation'] not in aggregations: aggregations.append(service.data['aggregation']) # Add QTreeWidgetItems for aggregation in aggregations: main_tree = QTreeWidgetItem() main_tree.setText(0, aggregation) main_tree.setIcon(0, QIcon(settings.get_image('tree'))) main_tree.setToolTip(0, aggregation) for service in self.services: if service.data['aggregation'] == aggregation: service_tree = ServiceTreeItem() service_tree.initialize(service) service_tree.setToolTip(0, service.get_tooltip()) self.services_tree_widget.clicked.connect( self.update_service_data) main_tree.addChild(service_tree) self.services_tree_widget.addTopLevelItem(main_tree) self.service_data_widget.hide() else: # If no services, reset service item to None and hide data widget self.service_data_widget.service_item = None self.service_data_widget.hide()
def create_item(self, f, parent): name = f.name ans = QTreeWidgetItem(parent, [name]) ans.setData(0, Qt.UserRole, '/'.join(f.full_path[1:])) ans.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) ans.setCheckState(0, Qt.Unchecked if self.dev.is_folder_ignored(f.storage_id, f.full_path[1:]) else Qt.Checked) ans.setData(0, Qt.DecorationRole, file_icon_provider().icon_from_ext('dir')) return ans
def setupQuestTree(self, questMan, treeWd): activeRoot = QTreeWidgetItem(treeWd, [StrUtil.tStr(StrUtil.QUEST_ACTIVE)]) for q in questMan.activeQuests: item = QTreeWidgetItem(activeRoot, [StrUtil.tStr(q.getTitle())]) item.setData(0, Qt.UserRole, (q,)) completedRoot = QTreeWidgetItem(treeWd, [StrUtil.tStr(StrUtil.QUEST_COMPLETED)]) for q in questMan.finishedQuests: item = QTreeWidgetItem(completedRoot, [StrUtil.tStr(q.getTitle())]) item.setData(0, Qt.UserRole, (q,)) treeWd.expandItem(activeRoot)
def __init__(self, tab, loading_status_changed): global tab_item_counter QTreeWidgetItem.__init__(self) tab_item_counter += 1 self.uid = tab_item_counter self.loading_status_changed = loading_status_changed self.setFlags(self.flags() | Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled) self.tabref = lambda: None self.set_view(tab)
def process_duplicates(self, db, duplicates): ta = _('%(title)s by %(author)s [%(formats)s]') bf = QFont(self.dup_list.font()) bf.setBold(True) itf = QFont(self.dup_list.font()) itf.setItalic(True) for mi, cover, formats in duplicates: # formats is a list of file paths # Grab just the extension and display to the user # Based only off the file name, no file type tests are done. incoming_formats = ', '.join( os.path.splitext(path)[-1].replace('.', '').upper() for path in formats) item = QTreeWidgetItem([ ta % dict(title=mi.title, author=mi.format_field('authors')[1], formats=incoming_formats) ], 0) item.setCheckState(0, Qt.CheckState.Checked) item.setFlags(Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsUserCheckable) item.setData(0, Qt.ItemDataRole.FontRole, bf) item.setData(0, Qt.ItemDataRole.UserRole, (mi, cover, formats)) matching_books = db.books_with_same_title(mi) def add_child(text): c = QTreeWidgetItem([text], 1) c.setFlags(Qt.ItemFlag.ItemIsEnabled) item.addChild(c) return c add_child(_('Already in calibre:')).setData( 0, Qt.ItemDataRole.FontRole, itf) author_text = {} for book_id in matching_books: author_text[book_id] = authors_to_string([ a.replace('|', ',') for a in ( db.authors(book_id, index_is_id=True) or '').split(',') ]) def key(x): return primary_sort_key(unicode_type(author_text[x])) for book_id in sorted(matching_books, key=key): add_child( ta % dict(title=db.title(book_id, index_is_id=True), author=author_text[book_id], formats=db.formats( book_id, index_is_id=True, verify_formats=False))) add_child('') yield item
def __init__(self, parent, device): QTreeWidgetItem.__init__(self, parent) self._device = device self.setText(0, device.lvname) mountpoint = getattr(device.format, "mountpoint", "") if not mountpoint: mountpoint = "" elif not (device.format and device.format.mountable): mountpoint = "N/A" self.setText(1, mountpoint) self.setText(2, "%Ld" % device.size)
def update_widget(self, services): """ Update the QTreeWidget and its items :param services: list of :class:`Services <alignak_app.items.service.Service>` items :type services: list """ self.services = services # Update services dashboard self.services_dashboard.update_widget(self.services) # Clear QTreeWidget self.services_tree_widget.clear() self.services_tree_widget.setIconSize(QSize(16, 16)) if self.services: # Set as "Global" aggregation who are empty for service in self.services: if not service.data['aggregation']: service.data['aggregation'] = 'Global' # First sort list by state then by aggregation newlist = sorted( self.services, key=lambda s: itemgetter('ls_state', 'ls_acknowledged', 'aggregation')(s.data) ) self.services = newlist # Get list of aggregations aggregations = [] for service in self.services: if service.data['aggregation'] not in aggregations: aggregations.append(service.data['aggregation']) # Add QTreeWidgetItems for aggregation in aggregations: main_tree = QTreeWidgetItem() main_tree.setText(0, aggregation) main_tree.setIcon(0, QIcon(settings.get_image('tree'))) main_tree.setToolTip(0, aggregation) for service in self.services: if service.data['aggregation'] == aggregation: service_tree = ServiceTreeItem() service_tree.initialize(service) service_tree.setToolTip(0, service.get_tooltip()) self.services_tree_widget.clicked.connect(self.update_service_data) main_tree.addChild(service_tree) self.services_tree_widget.addTopLevelItem(main_tree) self.service_data_widget.hide() else: # If no services, reset service item to None and hide data widget self.service_data_widget.service_item = None self.service_data_widget.hide()
def browser_item(f, parent): name = f.name if not f.is_folder: name += ' [%s]'%f.last_mod_string ans = QTreeWidgetItem(parent, [name]) ans.setData(0, Qt.UserRole, f.full_path) if f.is_folder: ext = 'dir' else: ext = f.name.rpartition('.')[-1] ans.setData(0, Qt.DecorationRole, file_icon_provider().icon_from_ext(ext)) return ans
def __init__(self, src, *args): QTreeWidgetItem.__init__(self, *args) self._src = src # fonts self._initFonts() # array of actual (i.e. numeric) column values self._values = [0.0] * NumColumns # set text alignment for icol in range(NumColumns): self.setTextAlignment(icol, Qt.AlignLeft) self.setTextAlignment(ColumnR, Qt.AlignRight) self.setTextAlignment(ColumnType, Qt.AlignHCenter) # setup source self._highlighted = self._highlighted_visual = False self.setSource(src)
def process_node(toc, parent): for child in toc: node = QTreeWidgetItem(parent) node.setText(0, child.title or '') node.setData(0, DEST_ROLE, child.dest or '') node.setData(0, FRAG_ROLE, child.frag or '') tt = _('File: {0}\nAnchor: {1}').format( child.dest or '', child.frag or _('Top of file')) node.setData(0, Qt.ToolTipRole, tt) process_node(child, node)
def __init__(self, tab, loading_status_changed): global tab_item_counter QTreeWidgetItem.__init__(self) tab_item_counter += 1 self.uid = tab_item_counter self.loading_status_changed = loading_status_changed self.setFlags(self.flags() | Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled) self.set_data(LOADING_ROLE, False) self.set_data(DISPLAY_ROLE, tab.title() or _('Loading...')) self.set_data(DECORATION_ROLE, missing_icon()) self.set_data(ANGLE_ROLE, 0) self.set_data(HOVER_ROLE, False) self.set_data(URL_ROLE, '') self.tabref = weakref.ref(tab) tab.title_changed.connect(partial(self.set_data, DISPLAY_ROLE)) tab.icon_changed.connect(self.icon_changed) tab.loading_status_changed.connect(self._loading_status_changed) tab.urlChanged.connect(partial(self.set_data, URL_ROLE))
def _connectServer(self): self.ui.connectBtn.setEnabled(False) host = self.ui.mongoHost.text() if host == "": host = "localhost" try: indexOfSep = host.index(":") hostName, portStr = host.split(":") port = int(portStr) except ValueError as e: hostName = host port = 27017 try: self.conn = pymongo.MongoClient(hostName, port) self.showMsg("connect " + host + " success!") self.databases = self.conn.database_names() colsMap = {} for db in self.databases: colsMap[db] = self.conn[db].collection_names() self.ui.treeWidget.clear() treeItems = [] for db in self.databases: dbItem = QTreeWidgetItem(self.ui.treeWidget, [db]) treeItems.append(dbItem) cols = colsMap[db] for col in cols: colItem = QTreeWidgetItem(dbItem, [col]) self.ui.treeWidget.insertTopLevelItems(0, treeItems) self.log.info(self.databases) except Exception as e: self.log.error(e) errorMsg = host + " connect failed:" self.showMsg(errorMsg) traceback.print_exc(file=sys.stdout) self.ui.connectBtn.setEnabled(True)
def update_motor_tree(self, baud_for_ids): self.window.motor_tree.clear() for b, ids in baud_for_ids.items(): baud_root = QTreeWidgetItem(self.window.motor_tree, [str(b)]) baud_root.setExpanded(True) f = int(baud_root.flags()) - int(QtCore.Qt.ItemIsSelectable) baud_root.setFlags(QtCore.Qt.ItemFlags(f)) dxl_io = get_dxl_connection(self.port, b, self.protocol) models = dxl_io.get_model(ids) release_dxl_connection() for id, model in zip(ids, models): QTreeWidgetItem(baud_root, ['', str(id), model])
def _init(self): handlers = gVar.app.protocolHandlerManager().protocolHandlers() for name, handler in handlers.items(): item = QTreeWidgetItem(self._ui.treeWidget) item.setText(0, name) item.setText(1, handler.host()) self._ui.treeWidget.addTopLevelItem(item)
def create_treeview_nodes(self, nodelist=None): ''' append nodes into datatable treeview ''' if nodelist: for table in nodelist: child = QTreeWidgetItem(self.__tb_treeview_root) child.setText(0,table) child.setText(1,'child')
def create_folder(self, item): text, ok = QInputDialog.getText(self, _('Folder name'), _('Enter a name for the new folder')) if ok and unicode_type(text): c = QTreeWidgetItem(item, (unicode_type(text),)) c.setIcon(0, QIcon(I('mimetypes/dir.png'))) for item in self.folders.selectedItems(): item.setSelected(False) c.setSelected(True) self.folders.setCurrentItem(c)
def setMeshGroups(self, groups): """ Sets the mesh group list Arguments: groups (dict[int, list[tuple[str, int]]]): Mesh groups info. """ self._list.clear() grp_types = sorted(groups.keys()) for typ in grp_types: names = groups[typ] if not names: continue title = MeshElemType.value2str(typ) item = QTreeWidgetItem(self._list, [title]) for name, size in names: sub_item = QTreeWidgetItem(item, [name, str(size)]) sub_item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) sub_item.setCheckState(0, Qt.Unchecked) sub_item.setTextAlignment(1, Qt.AlignRight) self._list.expandAll()
def process_duplicates(self, db, duplicates): ta = _('%(title)s by %(author)s [%(formats)s]') bf = QFont(self.dup_list.font()) bf.setBold(True) itf = QFont(self.dup_list.font()) itf.setItalic(True) for mi, cover, formats in duplicates: # formats is a list of file paths # Grab just the extension and display to the user # Based only off the file name, no file type tests are done. incoming_formats = ', '.join(os.path.splitext(path)[-1].replace('.', '').upper() for path in formats) item = QTreeWidgetItem([ta%dict( title=mi.title, author=mi.format_field('authors')[1], formats=incoming_formats)] , 0) item.setCheckState(0, Qt.Checked) item.setFlags(Qt.ItemIsEnabled|Qt.ItemIsUserCheckable) item.setData(0, Qt.FontRole, bf) item.setData(0, Qt.UserRole, (mi, cover, formats)) matching_books = db.books_with_same_title(mi) def add_child(text): c = QTreeWidgetItem([text], 1) c.setFlags(Qt.ItemIsEnabled) item.addChild(c) return c add_child(_('Already in calibre:')).setData(0, Qt.FontRole, itf) for book_id in matching_books: aut = [a.replace('|', ',') for a in (db.authors(book_id, index_is_id=True) or '').split(',')] add_child(ta%dict( title=db.title(book_id, index_is_id=True), author=authors_to_string(aut), formats=db.formats(book_id, index_is_id=True, verify_formats=False))) add_child('') yield item
def browser_item(f, parent): name = f.name if not f.is_folder: name += ' [%s]' % f.last_mod_string ans = QTreeWidgetItem(parent, [name]) ans.setData(0, Qt.UserRole, f.full_path) if f.is_folder: ext = 'dir' else: ext = f.name.rpartition('.')[-1] ans.setData(0, Qt.DecorationRole, file_icon_provider().icon_from_ext(ext)) return ans
def run(self): for b in self.baudrates: baud_root = QTreeWidgetItem(self.motor_tree, [str(b)]) baud_root.setExpanded(True) f = int(baud_root.flags()) - int(QtCore.Qt.ItemIsSelectable) baud_root.setFlags(QtCore.Qt.ItemFlags(f)) dxl_io = get_dxl_connection(self.port, b, self.protocol) for id in self.id_range: if not self.running.is_set(): break if dxl_io.ping(id): model = dxl_io.get_model((id, ))[0] QTreeWidgetItem(baud_root, ['', str(id), model]) self.part_done.emit(self.scan_progress.value() + 1) release_dxl_connection() self.done.emit()
class ChooseFolder(Dialog): # {{{ def __init__(self, msg=None, parent=None): self.msg = msg Dialog.__init__(self, _('Choose folder'), 'choose-folder', parent=parent) def setup_ui(self): self.l = l = QVBoxLayout(self) self.setLayout(l) self.msg = m = QLabel(self.msg or _( 'Choose the folder into which the files will be placed')) l.addWidget(m) m.setWordWrap(True) self.folders = f = QTreeWidget(self) f.setHeaderHidden(True) f.itemDoubleClicked.connect(self.accept) l.addWidget(f) f.setContextMenuPolicy(Qt.CustomContextMenu) f.customContextMenuRequested.connect(self.show_context_menu) self.root = QTreeWidgetItem(f, ('/',)) def process(node, parent): parent.setIcon(0, QIcon(I('mimetypes/dir.png'))) for child in sorted(node, key=numeric_sort_key): c = QTreeWidgetItem(parent, (child,)) process(node[child], c) process(create_folder_tree(current_container()), self.root) self.root.setSelected(True) f.expandAll() l.addWidget(self.bb) def show_context_menu(self, point): item = self.folders.itemAt(point) if item is None: return m = QMenu(self) m.addAction(QIcon(I('mimetypes/dir.png')), _('Create new folder'), partial(self.create_folder, item)) m.popup(self.folders.mapToGlobal(point)) def create_folder(self, item): text, ok = QInputDialog.getText(self, _('Folder name'), _('Enter a name for the new folder')) if ok and unicode_type(text): c = QTreeWidgetItem(item, (unicode_type(text),)) c.setIcon(0, QIcon(I('mimetypes/dir.png'))) for item in self.folders.selectedItems(): item.setSelected(False) c.setSelected(True) self.folders.setCurrentItem(c) def folder_path(self, item): ans = [] while item is not self.root: ans.append(unicode_type(item.text(0))) item = item.parent() return tuple(reversed(ans)) @property def chosen_folder(self): try: return '/'.join(self.folder_path(self.folders.selectedItems()[0])) except IndexError: return ''
def unserialize_node(dict_node, parent): n = QTreeWidgetItem(parent) n.setData(0, Qt.DisplayRole, dict_node['title']) n.setData(0, Qt.UserRole, dict_node['toc_node']) n.setFlags(NODE_FLAGS) n.setData(0, Qt.DecorationRole, dict_node['icon']) n.setData(0, Qt.ToolTipRole, dict_node['tooltip']) self.update_status_tip(n) n.setExpanded(dict_node['is_expanded']) n.setSelected(dict_node['is_selected']) for c in dict_node['children']: unserialize_node(c, n)
def add_child(text): c = QTreeWidgetItem([text], 1) c.setFlags(Qt.ItemIsEnabled) item.addChild(c) return c
def build(self, container, preserve_state=True): if container is None: return if preserve_state: state = self.get_state() self.clear() self.root = self.invisibleRootItem() self.root.setFlags(Qt.ItemIsDragEnabled) self.categories = {} for category, text, __ in CATEGORIES: self.categories[category] = i = QTreeWidgetItem(self.root, 0) i.setText(0, text) i.setData(0, Qt.DecorationRole, self.top_level_pixmap_cache[category]) f = i.font(0) f.setBold(True) i.setFont(0, f) i.setData(0, NAME_ROLE, category) flags = Qt.ItemIsEnabled if category == 'text': flags |= Qt.ItemIsDropEnabled i.setFlags(flags) processed, seen = {}, {} cover_page_name = get_cover_page_name(container) cover_image_name = get_raster_cover_name(container) manifested_names = set() for names in container.manifest_type_map.itervalues(): manifested_names |= set(names) def get_category(name, mt): category = 'misc' if mt.startswith('image/'): category = 'images' elif mt in OEB_FONTS: category = 'fonts' elif mt in OEB_STYLES: category = 'styles' elif mt in OEB_DOCS: category = 'text' ext = name.rpartition('.')[-1].lower() if ext in {'ttf', 'otf', 'woff'}: # Probably wrong mimetype in the OPF category = 'fonts' return category def set_display_name(name, item): if tprefs['file_list_shows_full_pathname']: text = name else: if name in processed: # We have an exact duplicate (can happen if there are # duplicates in the spine) item.setText(0, processed[name].text(0)) item.setText(1, processed[name].text(1)) return parts = name.split('/') text = parts.pop() while text in seen and parts: text = parts.pop() + '/' + text seen[text] = item item.setText(0, text) item.setText(1, hexlify(sort_key(text))) def render_emblems(item, emblems): emblems = tuple(emblems) if not emblems: return icon = self.rendered_emblem_cache.get(emblems, None) if icon is None: pixmaps = [] for emblem in emblems: pm = self.emblem_cache.get(emblem, None) if pm is None: pm = self.emblem_cache[emblem] = QIcon(I(emblem)).pixmap(self.iconSize()) pixmaps.append(pm) num = len(pixmaps) w, h = pixmaps[0].width(), pixmaps[0].height() if num == 1: icon = self.rendered_emblem_cache[emblems] = QIcon(pixmaps[0]) else: canvas = QPixmap((num * w) + ((num-1)*2), h) canvas.setDevicePixelRatio(pixmaps[0].devicePixelRatio()) canvas.fill(Qt.transparent) painter = QPainter(canvas) for i, pm in enumerate(pixmaps): painter.drawPixmap(int(i * (w + 2)/canvas.devicePixelRatio()), 0, pm) painter.end() icon = self.rendered_emblem_cache[emblems] = canvas item.setData(0, Qt.DecorationRole, icon) cannot_be_renamed = container.names_that_must_not_be_changed ncx_mime = guess_type('a.ncx') nav_items = frozenset(container.manifest_items_with_property('nav')) def create_item(name, linear=None): imt = container.mime_map.get(name, guess_type(name)) icat = get_category(name, imt) category = 'text' if linear is not None else ({'text':'misc'}.get(icat, icat)) item = QTreeWidgetItem(self.categories['text' if linear is not None else category], 1) flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable if category == 'text': flags |= Qt.ItemIsDragEnabled if name not in cannot_be_renamed: flags |= Qt.ItemIsEditable item.setFlags(flags) item.setStatusTip(0, _('Full path: ') + name) item.setData(0, NAME_ROLE, name) item.setData(0, CATEGORY_ROLE, category) item.setData(0, LINEAR_ROLE, bool(linear)) item.setData(0, MIME_ROLE, imt) set_display_name(name, item) tooltips = [] emblems = [] if name in {cover_page_name, cover_image_name}: emblems.append('default_cover.png') tooltips.append(_('This file is the cover %s for this book') % (_('image') if name == cover_image_name else _('page'))) if name in container.opf_name: emblems.append('metadata.png') tooltips.append(_('This file contains all the metadata and book structure information')) if imt == ncx_mime or name in nav_items: emblems.append('toc.png') tooltips.append(_('This file contains the metadata table of contents')) if name not in manifested_names and not container.ok_to_be_unmanifested(name): emblems.append('dialog_question.png') tooltips.append(_('This file is not listed in the book manifest')) if linear is False: emblems.append('arrow-down.png') tooltips.append(_('This file is marked as non-linear in the spine\nDrag it to the top to make it linear')) if linear is None and icat == 'text': # Text item outside spine emblems.append('dialog_warning.png') tooltips.append(_('This file is a text file that is not referenced in the spine')) if category == 'text' and name in processed: # Duplicate entry in spine emblems.append('dialog_error.png') tooltips.append(_('This file occurs more than once in the spine')) render_emblems(item, emblems) if tooltips: item.setData(0, Qt.ToolTipRole, '\n'.join(tooltips)) return item for name, linear in container.spine_names: processed[name] = create_item(name, linear=linear) for name in container.name_path_map: if name in processed: continue processed[name] = create_item(name) for name, c in self.categories.iteritems(): c.setExpanded(True) if name != 'text': c.sortChildren(1, Qt.AscendingOrder) if preserve_state: self.set_state(state) if self.current_edited_name: item = self.item_from_name(self.current_edited_name) if item is not None: self.mark_item_as_current(item)
def load(self, highlights): s = self.style() icon_size = s.pixelMetric(QStyle.PixelMetric.PM_SmallIconSize, None, self) dpr = self.devicePixelRatioF() is_dark = is_dark_theme() self.clear() self.uuid_map = {} highlights = (h for h in highlights if not h.get('removed') and h.get('highlighted_text')) section_map = defaultdict(list) section_tt_map = {} for h in self.sorted_highlights(highlights): tfam = h.get('toc_family_titles') or () if tfam: tsec = tfam[0] lsec = tfam[-1] else: tsec = h.get('top_level_section_title') lsec = h.get('lowest_level_section_title') sec = lsec or tsec or _('Unknown') if len(tfam) > 1: lines = [] for i, node in enumerate(tfam): lines.append('\xa0\xa0' * i + '➤ ' + node) tt = ngettext('Table of Contents section:', 'Table of Contents sections:', len(lines)) tt += '\n' + '\n'.join(lines) section_tt_map[sec] = tt section_map[sec].append(h) for secnum, (sec, items) in enumerate(section_map.items()): section = QTreeWidgetItem([sec], 1) section.setFlags(Qt.ItemFlag.ItemIsEnabled) section.setFont(0, self.section_font) tt = section_tt_map.get(sec) if tt: section.setToolTip(0, tt) self.addTopLevelItem(section) section.setExpanded(True) for itemnum, h in enumerate(items): txt = h.get('highlighted_text') txt = txt.replace('\n', ' ') if h.get('notes'): txt = '•' + txt if len(txt) > 100: txt = txt[:100] + '…' item = QTreeWidgetItem(section, [txt], 2) item.setFlags(Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemNeverHasChildren) item.setData(0, Qt.ItemDataRole.UserRole, h) try: dec = decoration_for_style(self.palette(), h.get('style') or {}, icon_size, dpr, is_dark) except Exception: import traceback traceback.print_exc() dec = None if dec is None: dec = self.default_decoration item.setData(0, Qt.ItemDataRole.DecorationRole, dec) self.uuid_map[h['uuid']] = secnum, itemnum self.num_of_items += 1
def __init__(self, window, msg, formats): ''' formats is a list of tuples: [(format, exists, convertible)]. format: Lower case format identifier. E.G. mobi exists: String representing the number of books that exist in the format. convertible: True if the format is a convertible format. formats should be ordered in the device's preferred format ordering. ''' QDialog.__init__(self, window) Ui_ChooseFormatDeviceDialog.__init__(self) self.setupUi(self) self.formats.activated[QModelIndex].connect(self.activated_slot) self.msg.setText(msg) for i, (format, exists, convertible) in enumerate(formats): t_item = QTreeWidgetItem() t_item.setIcon(0, file_icon_provider().icon_from_ext(format.lower())) t_item.setText(0, format.upper()) t_item.setText(1, exists) if convertible: t_item.setIcon(2, QIcon(I('ok.png'))) self.formats.addTopLevelItem(t_item) if i == 0: self.formats.setCurrentItem(t_item) t_item.setSelected(True) self.formats.resizeColumnToContents(2) self.formats.resizeColumnToContents(1) self.formats.resizeColumnToContents(0) self.formats.header().resizeSection( 0, self.formats.header().sectionSize(0) * 2) self._format = None
def process(node, parent): parent.setIcon(0, QIcon(I('mimetypes/dir.png'))) for child in sorted(node, key=numeric_sort_key): c = QTreeWidgetItem(parent, (child,)) process(node[child], c)
def create_item(name, linear=None): imt = container.mime_map.get(name, guess_type(name)) icat = get_category(name, imt) category = 'text' if linear is not None else ({'text':'misc'}.get(icat, icat)) item = QTreeWidgetItem(self.categories['text' if linear is not None else category], 1) flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable if category == 'text': flags |= Qt.ItemIsDragEnabled if name not in cannot_be_renamed: flags |= Qt.ItemIsEditable item.setFlags(flags) item.setStatusTip(0, _('Full path: ') + name) item.setData(0, NAME_ROLE, name) item.setData(0, CATEGORY_ROLE, category) item.setData(0, LINEAR_ROLE, bool(linear)) item.setData(0, MIME_ROLE, imt) set_display_name(name, item) tooltips = [] emblems = [] if name in {cover_page_name, cover_image_name}: emblems.append('default_cover.png') tooltips.append(_('This file is the cover %s for this book') % (_('image') if name == cover_image_name else _('page'))) if name in container.opf_name: emblems.append('metadata.png') tooltips.append(_('This file contains all the metadata and book structure information')) if imt == ncx_mime or name in nav_items: emblems.append('toc.png') tooltips.append(_('This file contains the metadata table of contents')) if name not in manifested_names and not container.ok_to_be_unmanifested(name): emblems.append('dialog_question.png') tooltips.append(_('This file is not listed in the book manifest')) if linear is False: emblems.append('arrow-down.png') tooltips.append(_('This file is marked as non-linear in the spine\nDrag it to the top to make it linear')) if linear is None and icat == 'text': # Text item outside spine emblems.append('dialog_warning.png') tooltips.append(_('This file is a text file that is not referenced in the spine')) if category == 'text' and name in processed: # Duplicate entry in spine emblems.append('dialog_error.png') tooltips.append(_('This file occurs more than once in the spine')) render_emblems(item, emblems) if tooltips: item.setData(0, Qt.ToolTipRole, '\n'.join(tooltips)) return item
def __init__(self, parent, device=None): QTreeWidgetItem.__init__(self, parent) self.device = device
def set_results(self, results, emphasize_text): self.clear() self.delegate.emphasize_text = emphasize_text self.number_of_results = 0 self.item_map = [] book_id_map = {} db = current_db() for result in results: book_id = result['book_id'] if book_id not in book_id_map: book_id_map[book_id] = { 'title': db.field_for('title', book_id), 'matches': [] } book_id_map[book_id]['matches'].append(result) for book_id, entry in book_id_map.items(): section = QTreeWidgetItem([entry['title']], 1) section.setFlags(Qt.ItemIsEnabled) section.setFont(0, self.section_font) self.addTopLevelItem(section) section.setExpanded(True) for result in entry['matches']: item = QTreeWidgetItem(section, [' '], 2) self.item_map.append(item) item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemNeverHasChildren) item.setData(0, Qt.UserRole, result) item.setData(0, Qt.UserRole + 1, self.number_of_results) self.number_of_results += 1 if self.item_map: self.setCurrentItem(self.item_map[0])
def __init__(self, window, msg, formats): ''' formats is a list of tuples: [(format, exists, convertible)]. format: Lower case format identifier. E.G. mobi exists: String representing the number of books that exist in the format. convertible: True if the format is a convertible format. formats should be ordered in the device's preferred format ordering. ''' QDialog.__init__(self, window) Ui_ChooseFormatDeviceDialog.__init__(self) self.setupUi(self) self.formats.activated[QModelIndex].connect(self.activated_slot) self.msg.setText(msg) for i, (format, exists, convertible) in enumerate(formats): t_item = QTreeWidgetItem() t_item.setIcon(0, file_icon_provider().icon_from_ext(format.lower())) t_item.setText(0, format.upper()) t_item.setText(1, exists) if convertible: t_item.setIcon(2, QIcon(I('ok.png'))) self.formats.addTopLevelItem(t_item) if i == 0: self.formats.setCurrentItem(t_item) t_item.setSelected(True) self.formats.resizeColumnToContents(2) self.formats.resizeColumnToContents(1) self.formats.resizeColumnToContents(0) self.formats.header().resizeSection(0, self.formats.header().sectionSize(0) * 2) self._format = None
def _refresh(self): self._ui.treeWidget.clear() sessions = gVar.app.sessionManager()._sessionMetaData() for session in sessions: item = QTreeWidgetItem() item.setText(0, session.name) item.setText(1, QFileInfo(session.filePath).lastModified().toString(Qt.DefaultLocaleShortDate)) item.setData(0, self._SessionFileRole, session.filePath) item.setData(0, self._IsBackupSessionRole, session.isBackup) item.setData(0, self._IsActiveSessionRole, session.isActive) item.setData(0, self._IsDefaultSessionRole, session.isDefault) self._updateItem(item) self._ui.treeWidget.addTopLevelItem(item) self._updateButtons()
def add_result(self, result): section_title = _('Unknown') section_id = -1 toc_nodes = getattr(result, 'toc_nodes', ()) or () if toc_nodes: section_title = toc_nodes[-1].get('title') or _('Unknown') section_id = toc_nodes[-1].get('id') if section_id is None: section_id = -1 section_key = section_id section = self.section_map.get(section_key) if section is None: section = QTreeWidgetItem([section_title], 1) section.setFlags(Qt.ItemIsEnabled) section.setFont(0, self.section_font) lines = [] for i, node in enumerate(toc_nodes): lines.append('\xa0\xa0' * i + '➤ ' + (node.get('title') or _('Unknown'))) if lines: tt = ngettext('Table of Contents section:', 'Table of Contents sections:', len(lines)) tt += '\n' + '\n'.join(lines) section.setToolTip(0, tt) self.section_map[section_key] = section self.addTopLevelItem(section) section.setExpanded(True) item = QTreeWidgetItem(section, [' '], 2) item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemNeverHasChildren) item.setData(0, Qt.UserRole, result) item.setData(0, Qt.UserRole + 1, len(self.search_results)) item.setIcon(0, self.blank_icon) self.item_map[len(self.search_results)] = item self.search_results.append(result) n = self.number_of_results self.count_changed.emit(n) return n