source: orange/Orange/OrangeCanvas/application/welcomedialog.py @ 11119:9642f8570adf

Revision 11119:9642f8570adf, 6.4 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Added a Welcome screen dialog widget.

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