1 """Miscellaneous Qt utility functions."""
2 from __future__
import absolute_import
, division
, print_function
, unicode_literals
5 from qtpy
import compat
7 from qtpy
import QtCore
8 from qtpy
import QtWidgets
9 from qtpy
.QtCore
import Qt
10 from qtpy
.QtCore
import Signal
17 from .compat
import int_types
18 from .compat
import ustr
19 from .models
import prefs
20 from .widgets
import defs
28 """Return the active window for the current application"""
29 return QtWidgets
.QApplication
.activeWindow()
32 def connect_action(action
, fn
):
33 """Connect an action to a function"""
34 action
.triggered
[bool].connect(lambda x
: fn())
37 def connect_action_bool(action
, fn
):
38 """Connect a triggered(bool) action to a function"""
39 action
.triggered
[bool].connect(fn
)
42 def connect_button(button
, fn
):
43 """Connect a button to a function"""
44 # Some versions of Qt send the `bool` argument to the clicked callback,
45 # and some do not. The lambda consumes all callback-provided arguments.
46 button
.clicked
.connect(lambda *args
, **kwargs
: fn())
49 def connect_checkbox(widget
, fn
):
50 """Connect a checkbox to a function taking bool"""
51 widget
.clicked
.connect(lambda *args
, **kwargs
: fn(get(checkbox
)))
54 def connect_released(button
, fn
):
55 """Connect a button to a function"""
56 button
.released
.connect(fn
)
59 def button_action(button
, action
):
60 """Make a button trigger an action"""
61 connect_button(button
, action
.trigger
)
64 def connect_toggle(toggle
, fn
):
65 """Connect a toggle button to a function"""
66 toggle
.toggled
.connect(fn
)
69 def disconnect(signal
):
70 """Disconnect signal from all slots"""
73 except TypeError: # allow unconnected slots
78 """Query a widget for its python value"""
79 if hasattr(widget
, 'isChecked'):
80 value
= widget
.isChecked()
81 elif hasattr(widget
, 'value'):
82 value
= widget
.value()
83 elif hasattr(widget
, 'text'):
85 elif hasattr(widget
, 'toPlainText'):
86 value
= widget
.toPlainText()
87 elif hasattr(widget
, 'sizes'):
88 value
= widget
.sizes()
89 elif hasattr(widget
, 'date'):
90 value
= widget
.date().toString(Qt
.ISODate
)
96 def hbox(margin
, spacing
, *items
):
97 """Create an HBoxLayout with the specified sizes and items"""
98 return box(QtWidgets
.QHBoxLayout
, margin
, spacing
, *items
)
101 def vbox(margin
, spacing
, *items
):
102 """Create a VBoxLayout with the specified sizes and items"""
103 return box(QtWidgets
.QVBoxLayout
, margin
, spacing
, *items
)
106 def buttongroup(*items
):
107 """Create a QButtonGroup for the specified items"""
108 group
= QtWidgets
.QButtonGroup()
114 def set_margin(layout
, margin
):
115 """Set the content margins for a layout"""
116 layout
.setContentsMargins(margin
, margin
, margin
, margin
)
119 def box(cls
, margin
, spacing
, *items
):
120 """Create a QBoxLayout with the specified sizes and items"""
124 layout
.setSpacing(spacing
)
125 set_margin(layout
, margin
)
128 if isinstance(i
, QtWidgets
.QWidget
):
133 QtWidgets
.QHBoxLayout
,
134 QtWidgets
.QVBoxLayout
,
135 QtWidgets
.QFormLayout
,
144 elif isinstance(i
, int_types
):
150 def form(margin
, spacing
, *widgets
):
151 """Create a QFormLayout with the specified sizes and items"""
152 layout
= QtWidgets
.QFormLayout()
153 layout
.setSpacing(spacing
)
154 layout
.setFieldGrowthPolicy(QtWidgets
.QFormLayout
.ExpandingFieldsGrow
)
155 set_margin(layout
, margin
)
157 for idx
, (name
, widget
) in enumerate(widgets
):
158 if isinstance(name
, (str, ustr
)):
159 layout
.addRow(name
, widget
)
161 layout
.setWidget(idx
, QtWidgets
.QFormLayout
.LabelRole
, name
)
162 layout
.setWidget(idx
, QtWidgets
.QFormLayout
.FieldRole
, widget
)
167 def grid(margin
, spacing
, *widgets
):
168 """Create a QGridLayout with the specified sizes and items"""
169 layout
= QtWidgets
.QGridLayout()
170 layout
.setSpacing(spacing
)
171 set_margin(layout
, margin
)
175 if isinstance(item
, QtWidgets
.QWidget
):
176 layout
.addWidget(*row
)
177 elif isinstance(item
, QtWidgets
.QLayoutItem
):
183 def splitter(orientation
, *widgets
):
184 """Create a spliter over the specified widgets
186 :param orientation: Qt.Horizontal or Qt.Vertical
189 layout
= QtWidgets
.QSplitter()
190 layout
.setOrientation(orientation
)
191 layout
.setHandleWidth(defs
.handle_width
)
192 layout
.setChildrenCollapsible(True)
194 for idx
, widget
in enumerate(widgets
):
195 layout
.addWidget(widget
)
196 layout
.setStretchFactor(idx
, 1)
198 # Workaround for Qt not setting the WA_Hover property for QSplitter
199 # Cf. https://bugreports.qt.io/browse/QTBUG-13768
200 layout
.handle(1).setAttribute(Qt
.WA_Hover
)
205 def label(text
=None, align
=None, fmt
=None, selectable
=True):
206 """Create a QLabel with the specified properties"""
207 widget
= QtWidgets
.QLabel()
208 if align
is not None:
209 widget
.setAlignment(align
)
211 widget
.setTextFormat(fmt
)
213 widget
.setTextInteractionFlags(Qt
.TextBrowserInteraction
)
214 widget
.setOpenExternalLinks(True)
220 class ComboBox(QtWidgets
.QComboBox
):
221 """Custom read-only combobox with a convenient API"""
223 def __init__(self
, items
=None, editable
=False, parent
=None, transform
=None):
224 super(ComboBox
, self
).__init
__(parent
)
225 self
.setEditable(editable
)
226 self
.transform
= transform
230 self
.item_data
.extend(items
)
232 def set_index(self
, idx
):
233 idx
= utils
.clamp(idx
, 0, self
.count() - 1)
234 self
.setCurrentIndex(idx
)
236 def add_item(self
, text
, data
):
238 self
.item_data
.append(data
)
240 def current_data(self
):
241 return self
.item_data
[self
.currentIndex()]
243 def set_value(self
, value
):
245 value
= self
.transform(value
)
247 index
= self
.item_data
.index(value
)
250 self
.setCurrentIndex(index
)
253 def combo(items
, editable
=False, parent
=None):
254 """Create a readonly (by default) combobox from a list of items"""
255 return ComboBox(editable
=editable
, items
=items
, parent
=parent
)
258 def combo_mapped(data
, editable
=False, transform
=None, parent
=None):
259 """Create a readonly (by default) combobox from a list of items"""
260 widget
= ComboBox(editable
=editable
, transform
=transform
, parent
=parent
)
262 widget
.add_item(k
, v
)
266 def textbrowser(text
=None):
267 """Create a QTextBrowser for the specified text"""
268 widget
= QtWidgets
.QTextBrowser()
269 widget
.setOpenExternalLinks(True)
275 def add_completer(widget
, items
):
276 """Add simple completion to a widget"""
277 completer
= QtWidgets
.QCompleter(items
, widget
)
278 completer
.setCaseSensitivity(Qt
.CaseInsensitive
)
279 completer
.setCompletionMode(QtWidgets
.QCompleter
.InlineCompletion
)
280 widget
.setCompleter(completer
)
283 def prompt(msg
, title
=None, text
='', parent
=None):
284 """Presents the user with an input widget and returns the input."""
288 parent
= active_window()
289 result
= QtWidgets
.QInputDialog
.getText(
290 parent
, title
, msg
, QtWidgets
.QLineEdit
.Normal
, text
292 return (result
[0], result
[1])
295 def prompt_n(msg
, inputs
):
296 """Presents the user with N input widgets and returns the results"""
297 dialog
= QtWidgets
.QDialog(active_window())
298 dialog
.setWindowModality(Qt
.WindowModal
)
299 dialog
.setWindowTitle(msg
)
303 if len(k
+ v
) > len(long_value
):
306 metrics
= QtGui
.QFontMetrics(dialog
.font())
307 min_width
= metrics
.width(long_value
) + 100
310 dialog
.setMinimumWidth(min_width
)
312 ok_b
= ok_button(msg
, enabled
=False)
313 close_b
= close_button()
318 return [pair
[1].text().strip() for pair
in form_widgets
]
320 for name
, value
in inputs
:
321 lineedit
= QtWidgets
.QLineEdit()
322 # Enable the OK button only when all fields have been populated
323 # pylint: disable=no-member
324 lineedit
.textChanged
.connect(lambda x
: ok_b
.setEnabled(all(get_values())))
326 lineedit
.setText(value
)
327 form_widgets
.append((name
, lineedit
))
330 form_layout
= form(defs
.no_margin
, defs
.button_spacing
, *form_widgets
)
331 button_layout
= hbox(defs
.no_margin
, defs
.button_spacing
, STRETCH
, close_b
, ok_b
)
332 main_layout
= vbox(defs
.margin
, defs
.button_spacing
, form_layout
, button_layout
)
333 dialog
.setLayout(main_layout
)
336 connect_button(ok_b
, dialog
.accept
)
337 connect_button(close_b
, dialog
.reject
)
339 accepted
= dialog
.exec_() == QtWidgets
.QDialog
.Accepted
341 ok
= accepted
and all(text
)
345 class TreeWidgetItem(QtWidgets
.QTreeWidgetItem
):
347 TYPE
= QtGui
.QStandardItem
.UserType
+ 101
349 def __init__(self
, path
, icon
, deleted
):
350 QtWidgets
.QTreeWidgetItem
.__init
__(self
)
352 self
.deleted
= deleted
353 self
.setIcon(0, icons
.from_name(icon
))
354 self
.setText(0, path
)
360 def paths_from_indexes(model
, indexes
, item_type
=TreeWidgetItem
.TYPE
, item_filter
=None):
361 """Return paths from a list of QStandardItemModel indexes"""
362 items
= [model
.itemFromIndex(i
) for i
in indexes
]
363 return paths_from_items(items
, item_type
=item_type
, item_filter
=item_filter
)
366 def _true_filter(_x
):
370 def paths_from_items(items
, item_type
=TreeWidgetItem
.TYPE
, item_filter
=None):
371 """Return a list of paths from a list of items"""
372 if item_filter
is None:
373 item_filter
= _true_filter
374 return [i
.path
for i
in items
if i
.type() == item_type
and item_filter(i
)]
377 def tree_selection(tree_item
, items
):
378 """Returns an array of model items that correspond to the selected
379 QTreeWidgetItem children"""
381 count
= min(tree_item
.childCount(), len(items
))
382 for idx
in range(count
):
383 if tree_item
.child(idx
).isSelected():
384 selected
.append(items
[idx
])
389 def tree_selection_items(tree_item
):
390 """Returns selected widget items"""
392 for idx
in range(tree_item
.childCount()):
393 child
= tree_item
.child(idx
)
394 if child
.isSelected():
395 selected
.append(child
)
400 def selected_item(list_widget
, items
):
401 """Returns the model item that corresponds to the selected QListWidget
403 widget_items
= list_widget
.selectedItems()
406 widget_item
= widget_items
[0]
407 row
= list_widget
.row(widget_item
)
415 def selected_items(list_widget
, items
):
416 """Returns an array of model items that correspond to the selected
418 item_count
= len(items
)
420 for widget_item
in list_widget
.selectedItems():
421 row
= list_widget
.row(widget_item
)
423 selected
.append(items
[row
])
427 def open_file(title
, directory
=None):
428 """Creates an Open File dialog and returns a filename."""
429 result
= compat
.getopenfilename(
430 parent
=active_window(), caption
=title
, basedir
=directory
435 def open_files(title
, directory
=None, filters
=''):
436 """Creates an Open File dialog and returns a list of filenames."""
437 result
= compat
.getopenfilenames(
438 parent
=active_window(), caption
=title
, basedir
=directory
, filters
=filters
443 def opendir_dialog(caption
, path
):
444 """Prompts for a directory path"""
447 QtWidgets
.QFileDialog
.ShowDirsOnly | QtWidgets
.QFileDialog
.DontResolveSymlinks
449 return compat
.getexistingdirectory(
450 parent
=active_window(), caption
=caption
, basedir
=path
, options
=options
454 def save_as(filename
, title
='Save As...'):
455 """Creates a Save File dialog and returns a filename."""
456 result
= compat
.getsavefilename(
457 parent
=active_window(), caption
=title
, basedir
=filename
462 def copy_path(filename
, absolute
=True):
463 """Copy a filename to the clipboard"""
467 filename
= core
.abspath(filename
)
468 set_clipboard(filename
)
471 def set_clipboard(text
):
472 """Sets the copy/paste buffer to text."""
475 clipboard
= QtWidgets
.QApplication
.clipboard()
476 clipboard
.setText(text
, QtGui
.QClipboard
.Clipboard
)
477 if not utils
.is_darwin() and not utils
.is_win32():
478 clipboard
.setText(text
, QtGui
.QClipboard
.Selection
)
482 # pylint: disable=line-too-long
483 def persist_clipboard():
484 """Persist the clipboard
486 X11 stores only a reference to the clipboard data.
487 Send a clipboard event to force a copy of the clipboard to occur.
488 This ensures that the clipboard is present after git-cola exits.
489 Otherwise, the reference is destroyed on exit.
491 C.f. https://stackoverflow.com/questions/2007103/how-can-i-disable-clear-of-clipboard-on-exit-of-pyqt4-application
494 clipboard
= QtWidgets
.QApplication
.clipboard()
495 event
= QtCore
.QEvent(QtCore
.QEvent
.Clipboard
)
496 QtWidgets
.QApplication
.sendEvent(clipboard
, event
)
499 def add_action_bool(widget
, text
, fn
, checked
, *shortcuts
):
501 action
= _add_action(widget
, text
, tip
, fn
, connect_action_bool
, *shortcuts
)
502 action
.setCheckable(True)
503 action
.setChecked(checked
)
507 def add_action(widget
, text
, fn
, *shortcuts
):
509 return _add_action(widget
, text
, tip
, fn
, connect_action
, *shortcuts
)
512 def add_action_with_status_tip(widget
, text
, tip
, fn
, *shortcuts
):
513 return _add_action(widget
, text
, tip
, fn
, connect_action
, *shortcuts
)
516 def _add_action(widget
, text
, tip
, fn
, connect
, *shortcuts
):
517 action
= QtWidgets
.QAction(text
, widget
)
518 if hasattr(action
, 'setIconVisibleInMenu'):
519 action
.setIconVisibleInMenu(True)
521 action
.setStatusTip(tip
)
524 action
.setShortcuts(shortcuts
)
525 if hasattr(Qt
, 'WidgetWithChildrenShortcut'):
526 action
.setShortcutContext(Qt
.WidgetWithChildrenShortcut
)
527 widget
.addAction(action
)
531 def set_selected_item(widget
, idx
):
532 """Sets a the currently selected item to the item at index idx."""
533 if isinstance(widget
, QtWidgets
.QTreeWidget
):
534 item
= widget
.topLevelItem(idx
)
536 item
.setSelected(True)
537 widget
.setCurrentItem(item
)
540 def add_items(widget
, items
):
541 """Adds items to a widget."""
548 def set_items(widget
, items
):
549 """Clear the existing widget contents and set the new items."""
551 add_items(widget
, items
)
554 def create_treeitem(filename
, staged
=False, deleted
=False, untracked
=False):
555 """Given a filename, return a TreeWidgetItem for a status widget
557 "staged", "deleted, and "untracked" control which icon is used.
560 icon_name
= icons
.status(filename
, deleted
, staged
, untracked
)
561 icon
= icons
.name_from_basename(icon_name
)
562 return TreeWidgetItem(filename
, icon
, deleted
=deleted
)
565 def add_close_action(widget
):
566 """Adds close action and shortcuts to a widget."""
567 return add_action(widget
, N_('Close...'), widget
.close
, hotkeys
.CLOSE
, hotkeys
.QUIT
)
571 """Return the current application"""
572 return QtWidgets
.QApplication
.instance()
576 """Return the desktop"""
577 return app().desktop()
582 rect
= desk
.screenGeometry(QtGui
.QCursor().pos())
583 return (rect
.width(), rect
.height())
586 def center_on_screen(widget
):
587 """Move widget to the center of the default screen"""
588 width
, height
= desktop_size()
591 widget
.move(cx
- widget
.width() // 2, cy
- widget
.height() // 2)
594 def default_size(parent
, width
, height
, use_parent_height
=True):
595 """Return the parent's size, or the provided defaults"""
596 if parent
is not None:
597 width
= parent
.width()
598 if use_parent_height
:
599 height
= parent
.height()
600 return (width
, height
)
603 def default_monospace_font():
604 if utils
.is_darwin():
608 mfont
= QtGui
.QFont()
609 mfont
.setFamily(family
)
613 def diff_font_str(context
):
615 font_str
= cfg
.get(prefs
.FONTDIFF
)
617 font_str
= default_monospace_font().toString()
621 def diff_font(context
):
622 return font(diff_font_str(context
))
626 qfont
= QtGui
.QFont()
627 qfont
.fromString(string
)
632 text
='', layout
=None, tooltip
=None, icon
=None, enabled
=True, default
=False
634 """Create a button, set its title, and add it to the parent."""
635 button
= QtWidgets
.QPushButton()
636 button
.setCursor(Qt
.PointingHandCursor
)
637 button
.setFocusPolicy(Qt
.NoFocus
)
639 button
.setText(' ' + text
)
642 button
.setIconSize(QtCore
.QSize(defs
.small_icon
, defs
.small_icon
))
643 if tooltip
is not None:
644 button
.setToolTip(tooltip
)
645 if layout
is not None:
646 layout
.addWidget(button
)
648 button
.setEnabled(False)
650 button
.setDefault(True)
655 """Create a flat border-less button"""
656 button
= QtWidgets
.QToolButton()
657 button
.setPopupMode(QtWidgets
.QToolButton
.InstantPopup
)
658 button
.setCursor(Qt
.PointingHandCursor
)
659 button
.setFocusPolicy(Qt
.NoFocus
)
661 palette
= QtGui
.QPalette()
662 highlight
= palette
.color(QtGui
.QPalette
.Highlight
)
663 highlight_rgb
= rgb_css(highlight
)
665 button
.setStyleSheet(
670 background-color: none;
672 /* Hide the menu indicator */
673 QToolButton::menu-indicator {
677 border: %(border)spx solid %(highlight_rgb)s;
680 % dict(border
=defs
.border
, highlight_rgb
=highlight_rgb
)
685 def create_action_button(tooltip
=None, icon
=None):
686 """Create a small toolbutton for use in dock title widgets"""
687 button
= tool_button()
688 if tooltip
is not None:
689 button
.setToolTip(tooltip
)
692 button
.setIconSize(QtCore
.QSize(defs
.small_icon
, defs
.small_icon
))
696 def ok_button(text
, default
=True, enabled
=True, icon
=None):
699 return create_button(text
=text
, icon
=icon
, default
=default
, enabled
=enabled
)
702 def close_button(text
=None, icon
=None):
703 text
= text
or N_('Close')
704 icon
= icons
.mkicon(icon
, icons
.close
)
705 return create_button(text
=text
, icon
=icon
)
708 def edit_button(enabled
=True, default
=False):
709 return create_button(
710 text
=N_('Edit'), icon
=icons
.edit(), enabled
=enabled
, default
=default
714 def refresh_button(enabled
=True, default
=False):
715 return create_button(
716 text
=N_('Refresh'), icon
=icons
.sync(), enabled
=enabled
, default
=default
720 def checkbox(text
='', tooltip
='', checked
=None):
721 """Create a checkbox"""
722 return _checkbox(QtWidgets
.QCheckBox
, text
, tooltip
, checked
)
725 def radio(text
='', tooltip
='', checked
=None):
726 """Create a radio button"""
727 return _checkbox(QtWidgets
.QRadioButton
, text
, tooltip
, checked
)
730 def _checkbox(cls
, text
, tooltip
, checked
):
731 """Create a widget and apply properties"""
736 widget
.setToolTip(tooltip
)
737 if checked
is not None:
738 widget
.setChecked(checked
)
742 class DockTitleBarWidget(QtWidgets
.QFrame
):
743 def __init__(self
, parent
, title
, stretch
=True):
744 QtWidgets
.QFrame
.__init
__(self
, parent
)
745 self
.setAutoFillBackground(True)
746 self
.label
= qlabel
= QtWidgets
.QLabel(title
, self
)
747 qfont
= qlabel
.font()
749 qlabel
.setFont(qfont
)
750 qlabel
.setCursor(Qt
.OpenHandCursor
)
752 self
.close_button
= create_action_button(
753 tooltip
=N_('Close'), icon
=icons
.close()
756 self
.toggle_button
= create_action_button(
757 tooltip
=N_('Detach'), icon
=icons
.external()
760 self
.corner_layout
= hbox(defs
.no_margin
, defs
.spacing
)
767 self
.main_layout
= hbox(
769 defs
.titlebar_spacing
,
776 self
.setLayout(self
.main_layout
)
778 connect_button(self
.toggle_button
, self
.toggle_floating
)
779 connect_button(self
.close_button
, self
.toggle_visibility
)
781 def toggle_floating(self
):
782 self
.parent().setFloating(not self
.parent().isFloating())
783 self
.update_tooltips()
785 def toggle_visibility(self
):
786 self
.parent().toggleViewAction().trigger()
788 def set_title(self
, title
):
789 self
.label
.setText(title
)
791 def add_corner_widget(self
, widget
):
792 self
.corner_layout
.addWidget(widget
)
794 def update_tooltips(self
):
795 if self
.parent().isFloating():
796 tooltip
= N_('Attach')
798 tooltip
= N_('Detach')
799 self
.toggle_button
.setToolTip(tooltip
)
802 def create_dock(title
, parent
, stretch
=True, widget
=None, fn
=None):
803 """Create a dock widget and set it up accordingly."""
804 dock
= QtWidgets
.QDockWidget(parent
)
805 dock
.setWindowTitle(title
)
806 dock
.setObjectName(title
)
807 titlebar
= DockTitleBarWidget(dock
, title
, stretch
=stretch
)
808 dock
.setTitleBarWidget(titlebar
)
809 dock
.setAutoFillBackground(True)
810 if hasattr(parent
, 'dockwidgets'):
811 parent
.dockwidgets
.append(dock
)
814 assert isinstance(widget
, QtWidgets
.QFrame
), "Docked widget has to be a QFrame"
816 dock
.setWidget(widget
)
820 def hide_dock(widget
):
821 widget
.toggleViewAction().setChecked(False)
825 def create_menu(title
, parent
):
826 """Create a menu and set its title."""
827 qmenu
= DebouncingMenu(title
, parent
)
831 class DebouncingMenu(QtWidgets
.QMenu
):
832 """Menu that debounces mouse release action ie. stops it if occurred
833 right after menu creation.
835 Disables annoying behaviour when RMB is pressed to show menu, cursor is
836 moved accidentally 1px onto newly created menu and released causing to
842 def __init__(self
, title
, parent
):
843 QtWidgets
.QMenu
.__init
__(self
, title
, parent
)
844 self
.created_at
= utils
.epoch_millis()
845 if hasattr(self
, 'setToolTipsVisible'):
846 self
.setToolTipsVisible(True)
848 def mouseReleaseEvent(self
, event
):
849 threshold
= DebouncingMenu
.threshold_ms
850 if (utils
.epoch_millis() - self
.created_at
) > threshold
:
851 QtWidgets
.QMenu
.mouseReleaseEvent(self
, event
)
854 def add_menu(title
, parent
):
855 """Create a menu and set its title."""
856 menu
= create_menu(title
, parent
)
857 if hasattr(parent
, 'addMenu'):
860 parent
.addAction(menu
.menuAction())
864 def create_toolbutton(text
=None, layout
=None, tooltip
=None, icon
=None):
865 button
= tool_button()
868 button
.setIconSize(QtCore
.QSize(defs
.default_icon
, defs
.default_icon
))
870 button
.setText(' ' + text
)
871 button
.setToolButtonStyle(Qt
.ToolButtonTextBesideIcon
)
872 if tooltip
is not None:
873 button
.setToolTip(tooltip
)
874 if layout
is not None:
875 layout
.addWidget(button
)
879 # pylint: disable=line-too-long
880 def mimedata_from_paths(context
, paths
):
881 """Return mimedata with a list of absolute path URLs
883 The text/x-moz-list format is always included by Qt, and doing
884 mimedata.removeFormat('text/x-moz-url') has no effect.
885 C.f. http://www.qtcentre.org/threads/44643-Dragging-text-uri-list-Qt-inserts-garbage
887 gnome-terminal expects utf-16 encoded text, but other terminals,
888 e.g. terminator, prefer utf-8, so allow cola.dragencoding
889 to override the default.
893 abspaths
= [core
.abspath(path
) for path
in paths
]
894 urls
= [QtCore
.QUrl
.fromLocalFile(path
) for path
in abspaths
]
896 mimedata
= QtCore
.QMimeData()
897 mimedata
.setUrls(urls
)
899 paths_text
= core
.list2cmdline(abspaths
)
900 encoding
= cfg
.get('cola.dragencoding', 'utf-16')
901 moz_text
= core
.encode(paths_text
, encoding
=encoding
)
902 mimedata
.setData('text/x-moz-url', moz_text
)
907 def path_mimetypes():
908 return ['text/uri-list', 'text/x-moz-url']
911 class BlockSignals(object):
912 """Context manager for blocking a signals on a widget"""
914 def __init__(self
, *widgets
):
915 self
.widgets
= widgets
919 """Block Qt signals for all of the captured widgets"""
920 self
.values
= [widget
.blockSignals(True) for widget
in self
.widgets
]
923 def __exit__(self
, exc_type
, exc_val
, exc_tb
):
924 """Restore Qt signals when we exit the scope"""
925 for (widget
, value
) in zip(self
.widgets
, self
.values
):
926 widget
.blockSignals(value
)
929 class Channel(QtCore
.QObject
):
930 finished
= Signal(object)
931 result
= Signal(object)
934 class Task(QtCore
.QRunnable
):
935 """Disable auto-deletion to avoid gc issues
937 Python's garbage collector will try to double-free the task
938 once it's finished, so disable Qt's auto-deletion as a workaround.
942 def __init__(self
, parent
):
943 QtCore
.QRunnable
.__init
__(self
)
945 self
.channel
= Channel(parent
)
947 self
.setAutoDelete(False)
950 self
.result
= self
.task()
951 self
.channel
.result
.emit(self
.result
)
952 self
.channel
.finished
.emit(self
)
954 # pylint: disable=no-self-use
958 def connect(self
, handler
):
959 self
.channel
.result
.connect(handler
, type=Qt
.QueuedConnection
)
962 class SimpleTask(Task
):
963 """Run a simple callable as a task"""
965 def __init__(self
, parent
, fn
, *args
, **kwargs
):
966 Task
.__init
__(self
, parent
)
973 return self
.fn(*self
.args
, **self
.kwargs
)
976 class RunTask(QtCore
.QObject
):
977 """Runs QRunnable instances and transfers control when they finish"""
979 def __init__(self
, parent
=None):
980 QtCore
.QObject
.__init
__(self
, parent
)
982 self
.task_details
= {}
983 self
.threadpool
= QtCore
.QThreadPool
.globalInstance()
984 self
.result_fn
= None
986 def start(self
, task
, progress
=None, finish
=None, result
=None):
987 """Start the task and register a callback"""
988 self
.result_fn
= result
989 if progress
is not None:
991 # prevents garbage collection bugs in certain PyQt4 versions
992 self
.tasks
.append(task
)
994 self
.task_details
[task_id
] = (progress
, finish
, result
)
995 task
.channel
.finished
.connect(self
.finish
, type=Qt
.QueuedConnection
)
996 self
.threadpool
.start(task
)
998 def finish(self
, task
):
1001 self
.tasks
.remove(task
)
1005 progress
, finish
, result
= self
.task_details
[task_id
]
1006 del self
.task_details
[task_id
]
1008 finish
= progress
= result
= None
1010 if progress
is not None:
1013 if result
is not None:
1016 if finish
is not None:
1020 # Syntax highlighting
1024 color
= QtGui
.QColor()
1025 color
.setRgb(r
, g
, b
)
1029 def rgba(r
, g
, b
, a
=255):
1030 color
= rgb(r
, g
, b
)
1040 """Convert a QColor into an rgb(int, int, int) CSS string"""
1041 return 'rgb(%d, %d, %d)' % (color
.red(), color
.green(), color
.blue())
1045 """Convert a QColor into a hex aabbcc string"""
1046 return '%02x%02x%02x' % (color
.red(), color
.green(), color
.blue())
1049 def hsl(h
, s
, light
):
1050 return QtGui
.QColor
.fromHslF(
1051 utils
.clamp(h
, 0.0, 1.0), utils
.clamp(s
, 0.0, 1.0), utils
.clamp(light
, 0.0, 1.0)
1055 def hsl_css(h
, s
, light
):
1056 return rgb_css(hsl(h
, s
, light
))
1059 def make_format(fg
=None, bg
=None, bold
=False):
1060 fmt
= QtGui
.QTextCharFormat()
1062 fmt
.setForeground(fg
)
1064 fmt
.setBackground(bg
)
1066 fmt
.setFontWeight(QtGui
.QFont
.Bold
)
1070 class ImageFormats(object):
1072 # returns a list of QByteArray objects
1073 formats_qba
= QtGui
.QImageReader
.supportedImageFormats()
1074 # portability: python3 data() returns bytes, python2 returns str
1075 decode
= core
.decode
1076 formats
= [decode(x
.data()) for x
in formats_qba
]
1077 self
.extensions
= set(['.' + fmt
for fmt
in formats
])
1079 def ok(self
, filename
):
1080 _
, ext
= os
.path
.splitext(filename
)
1081 return ext
.lower() in self
.extensions
1084 def set_scrollbar_values(widget
, hscroll_value
, vscroll_value
):
1085 """Set scrollbars to the specified values"""
1086 hscroll
= widget
.horizontalScrollBar()
1087 if hscroll
and hscroll_value
is not None:
1088 hscroll
.setValue(hscroll_value
)
1090 vscroll
= widget
.verticalScrollBar()
1091 if vscroll
and vscroll_value
is not None:
1092 vscroll
.setValue(vscroll_value
)
1095 def get_scrollbar_values(widget
):
1096 """Return the current (hscroll, vscroll) scrollbar values for a widget"""
1097 hscroll
= widget
.horizontalScrollBar()
1099 hscroll_value
= get(hscroll
)
1101 hscroll_value
= None
1102 vscroll
= widget
.verticalScrollBar()
1104 vscroll_value
= get(vscroll
)
1106 vscroll_value
= None
1107 return (hscroll_value
, vscroll_value
)
1110 def scroll_to_item(widget
, item
):
1111 """Scroll to an item while retaining the horizontal scroll position"""
1113 hscrollbar
= widget
.horizontalScrollBar()
1115 hscroll
= get(hscrollbar
)
1116 widget
.scrollToItem(item
)
1117 if hscroll
is not None:
1118 hscrollbar
.setValue(hscroll
)
1121 def select_item(widget
, item
):
1122 """Scroll to and make a QTreeWidget item selected and current"""
1123 scroll_to_item(widget
, item
)
1124 widget
.setCurrentItem(item
)
1125 item
.setSelected(True)
1128 def get_selected_values(widget
, top_level_idx
, values
):
1129 """Map the selected items under the top-level item to the values list"""
1130 # Get the top-level item
1131 item
= widget
.topLevelItem(top_level_idx
)
1132 return tree_selection(item
, values
)
1135 def get_selected_items(widget
, idx
):
1136 """Return the selected items under the top-level item"""
1137 item
= widget
.topLevelItem(idx
)
1138 return tree_selection_items(item
)