/usr/share/pyshared/guidata/qthelpers.py is in python-guidata 1.6.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | # -*- coding: utf-8 -*-
#
# Copyright © 2009-2010 CEA
# Pierre Raybaut
# Licensed under the terms of the CECILL License
# (see guidata/__init__.py for details)
"""
qthelpers
---------
The ``guidata.qthelpers`` module provides helper functions for developing
easily Qt-based graphical user interfaces.
"""
from __future__ import print_function
import sys
from guidata.qt.QtGui import (QAction, QApplication, QColor, QHBoxLayout, QIcon,
QKeySequence, QLabel, QLineEdit, QMenu,
QPushButton, QStyle, QToolButton, QVBoxLayout,
QWidget, QGroupBox)
from guidata.qt.QtCore import SIGNAL, Qt
# Local imports:
from guidata.configtools import get_icon
from guidata.py3compat import is_text_string
def text_to_qcolor(text):
"""Create a QColor from specified string"""
color = QColor()
if not is_text_string(text): # testing for QString (PyQt API#1)
text = str(text)
if text.startswith('#') and len(text)==7:
correct = '#0123456789abcdef'
for char in text:
if char.lower() not in correct:
return color
elif text not in list(QColor.colorNames()):
return color
color.setNamedColor(text)
return color
def create_action(parent, title, triggered=None, toggled=None,
shortcut=None, icon=None, tip=None, checkable=None,
context=Qt.WindowShortcut, enabled=None):
"""
Create a new QAction
"""
action = QAction(title, parent)
if triggered:
parent.connect(action, SIGNAL("triggered(bool)"), triggered)
if checkable is not None:
# Action may be checkable even if the toggled signal is not connected
action.setCheckable(checkable)
if toggled:
parent.connect(action, SIGNAL("toggled(bool)"), toggled)
action.setCheckable(True)
if icon is not None:
assert isinstance(icon, QIcon)
action.setIcon( icon )
if shortcut is not None:
action.setShortcut(shortcut)
if tip is not None:
action.setToolTip(tip)
action.setStatusTip(tip)
if enabled is not None:
action.setEnabled(enabled)
action.setShortcutContext(context)
return action
def create_toolbutton(parent, icon=None, text=None, triggered=None, tip=None,
toggled=None, shortcut=None, autoraise=True,
enabled=None):
"""Create a QToolButton"""
if autoraise:
button = QToolButton(parent)
else:
button = QPushButton(parent)
if text is not None:
button.setText(text)
if icon is not None:
if is_text_string(icon):
icon = get_icon(icon)
button.setIcon(icon)
if text is not None or tip is not None:
button.setToolTip(text if tip is None else tip)
if autoraise:
button.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
button.setAutoRaise(True)
if triggered is not None:
parent.connect(button, SIGNAL('clicked()'), triggered)
if toggled is not None:
parent.connect(button, SIGNAL("toggled(bool)"), toggled)
button.setCheckable(True)
if shortcut is not None:
button.setShortcut(shortcut)
if enabled is not None:
button.setEnabled(enabled)
return button
def create_groupbox(parent, title=None, toggled=None, checked=None,
flat=False, layout=None):
"""Create a QGroupBox"""
if title is None:
group = QGroupBox(parent)
else:
group = QGroupBox(title, parent)
group.setFlat(flat)
if toggled is not None:
group.setCheckable(True)
if checked is not None:
group.setChecked(checked)
parent.connect(group, SIGNAL("toggled(bool)"), toggled)
if layout is not None:
group.setLayout(layout)
return group
def keybinding(attr):
"""Return keybinding"""
ks = getattr(QKeySequence, attr)
return QKeySequence.keyBindings(ks)[0].toString()
def add_separator(target):
"""Add separator to target only if last action is not a separator"""
target_actions = list(target.actions())
if target_actions:
if not target_actions[-1].isSeparator():
target.addSeparator()
def add_actions(target, actions):
"""
Add actions (list of QAction instances) to target (menu, toolbar)
"""
for action in actions:
if isinstance(action, QAction):
target.addAction(action)
elif isinstance(action, QMenu):
target.addMenu(action)
elif action is None:
add_separator(target)
def get_std_icon(name, size=None):
"""
Get standard platform icon
Call 'show_std_icons()' for details
"""
if not name.startswith('SP_'):
name = 'SP_'+name
icon = QWidget().style().standardIcon( getattr(QStyle, name) )
if size is None:
return icon
else:
return QIcon( icon.pixmap(size, size) )
class ShowStdIcons(QWidget):
"""
Dialog showing standard icons
"""
def __init__(self, parent):
QWidget.__init__(self, parent)
layout = QHBoxLayout()
row_nb = 14
cindex = 0
for child in dir(QStyle):
if child.startswith('SP_'):
if cindex == 0:
col_layout = QVBoxLayout()
icon_layout = QHBoxLayout()
icon = get_std_icon(child)
label = QLabel()
label.setPixmap(icon.pixmap(32, 32))
icon_layout.addWidget( label )
icon_layout.addWidget( QLineEdit(child.replace('SP_', '')) )
col_layout.addLayout(icon_layout)
cindex = (cindex+1) % row_nb
if cindex == 0:
layout.addLayout(col_layout)
self.setLayout(layout)
self.setWindowTitle('Standard Platform Icons')
self.setWindowIcon(get_std_icon('TitleBarMenuButton'))
def show_std_icons():
"""
Show all standard Icons
"""
app = QApplication(sys.argv)
dialog = ShowStdIcons(None)
dialog.show()
sys.exit(app.exec_())
if __name__ == "__main__":
from guidata.utils import pairs
print(list( pairs( list(range(5)) ) ))
show_std_icons()
|