def __init__(self, parent=None): super(ListView, self).__init__(parent) self.parent = parent # Settings/design self.setLayoutMode(QtGui.QListView.SinglePass) self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) self.setDragEnabled(False) self.setAcceptDrops(False) self.setAlternatingRowColors(True) self.setViewMode(QtGui.QListView.ListMode) self.setResizeMode(QtGui.QListView.Adjust) self.setStyleSheet('QListView::item {padding:10px;}') # Toggle horizontal scroll bar on and off if word wrap enabled self.set_horiz_scrollbar(settings.get_word_wrap()) # Set view delegate delegate = ItemDelegate(self) self.setItemDelegate(delegate) # List item right click menu self._create_context_menu() # Item double clicked is set to clipboard self.doubleClicked.connect(self._emit_set_clipboard)
def _on_open_settings(self): """Open settings dialog. Prior to opening dialog, the global hot key is unbinded and then binded in case user changes it. The model view is updated to reflect changes in lines to display and if word wrap is enabled. """ # Windows allow's the user to open extra settings dialogs from system # tray menu even though dialog is modal try: self.key_binder.unbind(settings.get_global_hot_key()) except AttributeError: return None # PreviewDialog(self) so it opens at main window settings_dialog = dialogs.SettingsDialog(self) settings_dialog.exec_() self.setCursor(QtCore.Qt.BusyCursor) # Attempt to set new hot key self._set_hot_key() # Update scroll bars and refresh view set_word_wrap = settings.get_word_wrap() self.main_widget.view_main.set_horiz_scrollbar(set_word_wrap) self.main_widget.model_main.select() self.unsetCursor()
def paint(self, painter, option, index): """Subclass of paint function. Args: painter: QtGui.QPainter option: QtGui.QStyleOptionViewItem index: QtCore.QModelIndex Returns: QtGui.QStyledItemDelegate.paint() if QModelIndex is not valid. References: http://pydoc.net/Python/gayeogi/0.6/gayeogi.plugins.player/ """ if not index.isValid(): return QtGui.QStyledItemDelegate.paint(self, painter, option, index) painter.save() # Draw selection highlight if option.state & QtGui.QStyle.State_Selected: painter.setPen(QtGui.QPen(option.palette.highlightedText(), 0)) painter.fillRect(option.rect, option.palette.highlight()) # if index.data() == 'application/x-qt-image': # parent_index = QtCore.QModelIndex(self.model().index(0, ID)) # print parent_index.column() # Set alignment and enable word wrap if applicable text_option = QtGui.QTextOption() text_option.setAlignment(QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter) if settings.get_word_wrap(): text_option.setWrapMode(QtGui.QTextOption.WrapAnywhere) else: text_option.setWrapMode(QtGui.QTextOption.NoWrap) # Add padding to left and right side of text text_rect = option.rect text_rect.setLeft(text_rect.left() + 5) text_rect.setRight(text_rect.right() - 5) painter.drawText(text_rect, index.data(), o=text_option) painter.restore()
def sizeHint(self, option, index): """Option size is calculated by creating a QTextDocument with modified text and determining the dimensions. Args: option: QtGui.QStyleOptionViewItem index: QtCore.QModelIndex Returns: QtGui.QStyledItemDelegate.sizeHint() if QModelIndex is invalid. References: http://qt-project.org/forums/viewthread/12186 Todo: Look into using font metrics bounding rect. Handle lines to display in relation to word wrap. """ if not index.isValid(): return QtGui.QStyledItemDelegate.sizeHint(self, option, index) doc = QtGui.QTextDocument() # Inserting self creates a memory leak! # Set alignment and enable word wrap if applicable text_option = QtGui.QTextOption() text_option.setAlignment(QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter) # Reimplement as lines to display canFetchMore be ignored if word wrap # forces an extra line to be created if settings.get_word_wrap(): text_option.setWrapMode(QtGui.QTextOption.WrapAnywhere) else: text_option.setWrapMode(QtGui.QTextOption.NoWrap) doc.setDefaultTextOption(text_option) doc.setPlainText(index.data()) # Add some padding to each item, + 10 return QtCore.QSize(doc.size().width(), doc.size().height() + 5) # def sizeHint(self, option, index): # if not index.isValid(): # return QtGui.QStyledItemDelegate.sizeHint(self, option, index) # fake_text = 'Line1\nLine2\nLine3\n' # fake_fm = option.fontMetrics # fake_font_rect = fake_fm.boundingRect(option.rect, QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop|QtCore.Qt.TextWordWrap, fake_text) # real_text = index.data() # real_fm = option.fontMetrics # real_font_rect = real_fm.boundingRect(option.rect, QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop|QtCore.Qt.TextWordWrap, real_text) # if real_font_rect.height() < fake_font_rect.height(): # height = real_font_rect.height() # else: # height = fake_font_rect.height() # return QtCore.QSize(real_font_rect.width(), height+10) # def flags(self, index): # """Sublass of flags method. # Args: # index: QtCore.QModelIndex # """ # if not index.isValid(): # return QtCore.Qt.ItemFlags() # return QtCore.Qt.ItemFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
def setup_ui(self): """Display each setting widget with saved values from registry/ini. Todo: Renable word_wrap widget when QDelegate Sizing issue fixed. """ self.key_combo_edit = HotKeyEdit(self) # Allow user to insert <SUPER> on a Win OS self.super_check = QtGui.QCheckBox('Win') self.super_check.setToolTip('Insert <SUPER>') if '<SUPER>' in self.key_combo_edit.text().upper(): self.super_check.setCheckState(QtCore.Qt.Checked) # Number of lines to display self.line_count_spin = QtGui.QSpinBox(self) self.line_count_spin.setRange(1, 10) self.line_count_spin.setValue(settings.get_lines_to_display()) # Where to open the dialog self.open_at_pos_combo = QtGui.QComboBox(self) self.open_at_pos_combo.addItem('Mouse cursor', 0) self.open_at_pos_combo.addItem('Last position', 1) self.open_at_pos_combo.addItem('System tray', 2) # Word wrap display text self.word_wrap = QtGui.QCheckBox('Word wrap') self.word_wrap.setCheckState(_check_state(settings.get_word_wrap())) # Send paste key stroke when content set to clipboard self.paste_check = QtGui.QCheckBox('Paste in active window after ' 'selection') self.paste_check.setCheckState(_check_state(settings.get_send_paste())) # Ignore applications group_box = QtGui.QGroupBox('Ignore the following applications') self.exclude_list = QtGui.QLineEdit(self) self.exclude_list.setPlaceholderText('KeePass.exe;binaryname') self.exclude_list.setText(settings.get_exclude()) # Create seperate layout for ignore applications vbox = QtGui.QVBoxLayout() vbox.addWidget(self.exclude_list) group_box.setLayout(vbox) # Save and cancel buttons button_box = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Save| QtGui.QDialogButtonBox.Cancel) # Create form layout to align widgets layout = QtGui.QFormLayout() layout.setFieldGrowthPolicy(QtGui.QFormLayout.FieldsStayAtSizeHint) layout.addRow('Global shortcut:', self.key_combo_edit) layout.addRow('', self.super_check) layout.addRow('Open window at:', self.open_at_pos_combo) layout.addRow('Lines to display:', self.line_count_spin) # Set main layout main_layout = QtGui.QVBoxLayout(self) main_layout.addLayout(layout) # main_layout.addWidget(self.word_wrap) main_layout.addWidget(self.paste_check) main_layout.addWidget(group_box) main_layout.addWidget(button_box) self.setLayout(main_layout) # LINUX: I use Windows key to move windows with my wm self.setFocus(QtCore.Qt.PopupFocusReason) button_box.accepted.connect(self.save) button_box.rejected.connect(self.cancel) self.connect(self.super_check, QtCore.SIGNAL('stateChanged(int)'), self.insert_win_key)