source: orange/Orange/OrangeCanvas/application/welcomedialog.py @ 11147:5ddec5f677c1

Revision 11147:5ddec5f677c1, 6.4 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Fixed rejected open/browse actions in welcome dialog.

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("Dont'show again 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        if self.__showAtStartupCheck.isChecked() != (not show):
131            self.__showAtStartupCheck.setChecked(not show)
132
133    def showAtStartup(self):
134        return not self.__showAtStartupCheck.isChecked()
135
136    def addRow(self, actions, background="light-orange"):
137        """Add a row with `actions`.
138        """
139        count = self.__mainLayout.count()
140        self.insertRow(count, actions, background)
141
142    def insertRow(self, index, actions, background="light-orange"):
143        """Insert a row with `actions` at `index`.
144        """
145        widget = QWidget(objectName="icon-row")
146        layout = QHBoxLayout()
147        layout.setContentsMargins(40, 0, 40, 0)
148        layout.setSpacing(65)
149        widget.setLayout(layout)
150
151        self.__mainLayout.insertWidget(index, widget, stretch=10,
152                                       alignment=Qt.AlignCenter)
153
154        for i, action in enumerate(actions):
155            self.insertAction(index, i, action, background)
156
157    def insertAction(self, row, index, action,
158                      background="light-orange"):
159        """Insert `action` in `row` in position `index`.
160        """
161        button = self.createButton(action, background)
162        self.insertButton(row, index, button)
163
164    def insertButton(self, row, index, button):
165        """Insert `button` in `row` in position `index`.
166        """
167        item = self.__mainLayout.itemAt(row)
168        layout = item.widget().layout()
169        layout.insertWidget(index, button)
170        button.triggered.connect(self.__on_actionTriggered)
171
172    def createButton(self, action, background="light-orange"):
173        """Create a tool button for action.
174        """
175        button = WelcomeActionButton(self)
176        button.setDefaultAction(action)
177        button.setText(action.iconText())
178        button.setIcon(decorate_welcome_icon(action.icon(), background))
179        button.setToolTip(action.toolTip())
180        button.setFixedSize(100, 100)
181        button.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
182        font = QFont(button.font())
183        font.setPointSize(13)
184        button.setFont(font)
185
186        return button
187
188    def buttonAt(self, i, j):
189        """Return the button at i-t row and j-th column.
190        """
191        item = self.__mainLayout.itemAt(i)
192        row = item.widget()
193        item = row.layout().itemAt(j)
194        return item.widget()
195
196    def triggeredAction(self):
197        """Return the action that was triggered by the user.
198        """
199        return self.__triggeredAction
200
201    def showEvent(self, event):
202        # Clear the triggered action before show.
203        self.__triggeredAction = None
204        QDialog.showEvent(self, event)
205
206    def __on_actionTriggered(self, action):
207        """Called when the button action is triggered.
208        """
209        self.triggered.emit(action)
210        self.__triggeredAction = action
Note: See TracBrowser for help on using the repository browser.