source: orange/Orange/OrangeCanvas/application/welcomedialog.py @ 11512:4aade1f630e1

Revision 11512:4aade1f630e1, 6.5 KB checked in by Ales Erjavec <ales.erjavec@…>, 11 months ago (diff)

Changed "Don't show at ..." check box text to "Show at ...".

Line 
1"""
2Orange Canvas Welcome Dialog
3
4"""
5
6from PyQt4.QtGui import (
7    QDialog, QWidget, QToolButton, QCheckBox, QAction,
8    QHBoxLayout, QVBoxLayout, QFont, QSizePolicy,
9    QPixmap, QIcon, QPainter, QColor, QBrush
10)
11
12from PyQt4.QtCore import Qt, QRect, QPoint
13from PyQt4.QtCore import pyqtSignal as Signal
14
15from ..canvas.items.utils import radial_gradient
16from ..registry import NAMED_COLORS
17
18
19def decorate_welcome_icon(icon, background_color):
20    """Return a `QIcon` with a circle shaped background.
21    """
22    welcome_icon = QIcon()
23    sizes = [32, 48, 64, 80]
24    background_color = NAMED_COLORS.get(background_color, background_color)
25    background_color = QColor(background_color)
26    grad = radial_gradient(background_color)
27    for size in sizes:
28        icon_pixmap = icon.pixmap(5 * size / 8, 5 * size / 8)
29        icon_size = icon_pixmap.size()
30        icon_rect = QRect(QPoint(0, 0), icon_size)
31
32        pixmap = QPixmap(size, size)
33        pixmap.fill(QColor(0, 0, 0, 0))
34        p = QPainter(pixmap)
35        p.setRenderHint(QPainter.Antialiasing, True)
36        p.setBrush(QBrush(grad))
37        p.setPen(Qt.NoPen)
38        ellipse_rect = QRect(0, 0, size, size)
39        p.drawEllipse(ellipse_rect)
40        icon_rect.moveCenter(ellipse_rect.center())
41        p.drawPixmap(icon_rect.topLeft(), icon_pixmap)
42        p.end()
43
44        welcome_icon.addPixmap(pixmap)
45
46    return welcome_icon
47
48
49WELCOME_WIDGET_BUTTON_STYLE = \
50"""
51
52WelcomeActionButton {
53    border: none;
54    icon-size: 75px;
55    /*font: bold italic 14px "Helvetica";*/
56}
57
58WelcomeActionButton:pressed {
59    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
60                                      stop: 0 #dadbde, stop: 1 #f6f7fa);
61    border-radius: 10px;
62}
63
64WelcomeActionButton:focus {
65    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
66                                      stop: 0 #dadbde, stop: 1 #f6f7fa);
67    border-radius: 10px;
68}
69
70"""
71
72
73class WelcomeActionButton(QToolButton):
74    def __init__(self, parent=None):
75        QToolButton.__init__(self, parent)
76
77    def paintEvent(self, event):
78        QToolButton.paintEvent(self, event)
79
80
81class WelcomeDialog(QDialog):
82    """A welcome widget shown at startup presenting a series
83    of buttons (actions) for a beginner to choose from.
84
85    """
86    triggered = Signal(QAction)
87
88    def __init__(self, *args, **kwargs):
89        QDialog.__init__(self, *args, **kwargs)
90
91        self.__triggeredAction = None
92
93        self.setupUi()
94
95    def setupUi(self):
96        self.setLayout(QVBoxLayout())
97        self.layout().setContentsMargins(0, 0, 0, 0)
98        self.layout().setSpacing(0)
99
100        self.__mainLayout = QVBoxLayout()
101        self.__mainLayout.setContentsMargins(0, 40, 0, 40)
102        self.__mainLayout.setSpacing(65)
103
104        self.layout().addLayout(self.__mainLayout)
105
106        self.setStyleSheet(WELCOME_WIDGET_BUTTON_STYLE)
107
108        bottom_bar = QWidget(objectName="bottom-bar")
109        bottom_bar_layout = QHBoxLayout()
110        bottom_bar_layout.setContentsMargins(20, 10, 20, 10)
111        bottom_bar.setLayout(bottom_bar_layout)
112        bottom_bar.setSizePolicy(QSizePolicy.MinimumExpanding,
113                                 QSizePolicy.Maximum)
114
115        check = QCheckBox(self.tr("Show at startup"), bottom_bar)
116        check.setChecked(False)
117
118        self.__showAtStartupCheck = check
119
120        bottom_bar_layout.addWidget(check, alignment=Qt.AlignVCenter | \
121                                    Qt.AlignLeft)
122
123        self.layout().addWidget(bottom_bar, alignment=Qt.AlignBottom,
124                                stretch=1)
125
126        self.setSizeGripEnabled(False)
127        self.setFixedSize(620, 390)
128
129    def setShowAtStartup(self, show):
130        """
131        Set the 'Show at startup' check box state.
132        """
133        if self.__showAtStartupCheck.isChecked() != show:
134            self.__showAtStartupCheck.setChecked(show)
135
136    def showAtStartup(self):
137        """
138        Return the 'Show at startup' check box state.
139        """
140        return self.__showAtStartupCheck.isChecked()
141
142    def addRow(self, actions, background="light-orange"):
143        """Add a row with `actions`.
144        """
145        count = self.__mainLayout.count()
146        self.insertRow(count, actions, background)
147
148    def insertRow(self, index, actions, background="light-orange"):
149        """Insert a row with `actions` at `index`.
150        """
151        widget = QWidget(objectName="icon-row")
152        layout = QHBoxLayout()
153        layout.setContentsMargins(40, 0, 40, 0)
154        layout.setSpacing(65)
155        widget.setLayout(layout)
156
157        self.__mainLayout.insertWidget(index, widget, stretch=10,
158                                       alignment=Qt.AlignCenter)
159
160        for i, action in enumerate(actions):
161            self.insertAction(index, i, action, background)
162
163    def insertAction(self, row, index, action,
164                      background="light-orange"):
165        """Insert `action` in `row` in position `index`.
166        """
167        button = self.createButton(action, background)
168        self.insertButton(row, index, button)
169
170    def insertButton(self, row, index, button):
171        """Insert `button` in `row` in position `index`.
172        """
173        item = self.__mainLayout.itemAt(row)
174        layout = item.widget().layout()
175        layout.insertWidget(index, button)
176        button.triggered.connect(self.__on_actionTriggered)
177
178    def createButton(self, action, background="light-orange"):
179        """Create a tool button for action.
180        """
181        button = WelcomeActionButton(self)
182        button.setDefaultAction(action)
183        button.setText(action.iconText())
184        button.setIcon(decorate_welcome_icon(action.icon(), background))
185        button.setToolTip(action.toolTip())
186        button.setFixedSize(100, 100)
187        button.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
188        font = QFont(button.font())
189        font.setPointSize(13)
190        button.setFont(font)
191
192        return button
193
194    def buttonAt(self, i, j):
195        """Return the button at i-t row and j-th column.
196        """
197        item = self.__mainLayout.itemAt(i)
198        row = item.widget()
199        item = row.layout().itemAt(j)
200        return item.widget()
201
202    def triggeredAction(self):
203        """Return the action that was triggered by the user.
204        """
205        return self.__triggeredAction
206
207    def showEvent(self, event):
208        # Clear the triggered action before show.
209        self.__triggeredAction = None
210        QDialog.showEvent(self, event)
211
212    def __on_actionTriggered(self, action):
213        """Called when the button action is triggered.
214        """
215        self.triggered.emit(action)
216        self.__triggeredAction = action
Note: See TracBrowser for help on using the repository browser.