buttons.py 30.4 KB
Newer Older
1

Dan Pascu's avatar
Dan Pascu committed
2 3 4
from PyQt5.QtCore import Qt, QLineF, QPointF, QRectF, QSize, QTimer, pyqtSignal
from PyQt5.QtGui import QBrush, QColor, QLinearGradient, QIcon, QPainter, QPainterPath, QPalette, QPen, QPixmap, QPolygonF
from PyQt5.QtWidgets import QAction, QCommonStyle, QMenu, QPushButton, QStyle, QStyleOptionToolButton, QStylePainter, QToolButton
5 6

from blink.resources import Resources
7
from blink.widgets.color import ColorScheme, ColorUtils, ColorHelperMixin
8 9


10 11 12 13
__all__ = ['ToolButton', 'ConferenceButton', 'StreamButton', 'SegmentButton', 'SingleSegment', 'LeftSegment', 'MiddleSegment', 'RightSegment',
           'RecordButton', 'SwitchViewButton', 'StateButton', 'AccountState']


14 15
class ToolButton(QToolButton):
    """A custom QToolButton that doesn't show a menu indicator arrow"""
16

17 18 19 20 21 22 23 24
    def paintEvent(self, event):
        painter = QStylePainter(self)
        option = QStyleOptionToolButton()
        self.initStyleOption(option)
        option.features &= ~QStyleOptionToolButton.HasMenu
        painter.drawComplexControl(QStyle.CC_ToolButton, option)


25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
class ConferenceButton(ToolButton):
    makeConference  = pyqtSignal()
    breakConference = pyqtSignal()

    def __init__(self, parent=None):
        super(ConferenceButton, self).__init__(parent)
        self.make_conference_action = QAction(u'Conference all single sessions', self, triggered=self.makeConference.emit)
        self.break_conference_action = QAction(u'Break selected conference', self, triggered=self.breakConference.emit)
        self.toggled.connect(self._SH_Toggled)
        self.addAction(self.make_conference_action)

    def _SH_Toggled(self, checked):
        if checked:
            self.removeAction(self.make_conference_action)
            self.addAction(self.break_conference_action)
        else:
            self.removeAction(self.break_conference_action)
            self.addAction(self.make_conference_action)


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
class StreamButton(QToolButton):
    hidden = pyqtSignal()
    shown  = pyqtSignal()

    def __init__(self, parent=None):
        super(StreamButton, self).__init__(parent)
        self.default_icon = QIcon()
        self.alternate_icon = QIcon()
        self.clicked.connect(self._clicked)

    def _clicked(self):
        super(StreamButton, self).setIcon(self.default_icon)

    def _get_accepted(self):
        return not self.isChecked()

    def _set_accepted(self, accepted):
        super(StreamButton, self).setIcon(self.alternate_icon)
        self.setChecked(not accepted)

    accepted = property(_get_accepted, _set_accepted)
    del _get_accepted, _set_accepted

    def _get_active(self):
        return self.isEnabled()

    def _set_active(self, active):
        self.setEnabled(bool(active))

    active = property(_get_active, _set_active)
    del _get_active, _set_active

    @property
    def in_use(self):
        return self.isVisibleTo(self.parent())

    def setVisible(self, visible):
        super(StreamButton, self).setVisible(visible)
Dan Pascu's avatar
Dan Pascu committed
83 84
        signal = self.shown if visible else self.hidden
        signal.emit()
85 86 87 88 89 90 91 92 93 94

    def setIcon(self, icon):
        self.default_icon = icon
        self.alternate_icon = QIcon(icon)
        normal_sizes = icon.availableSizes(QIcon.Normal, QIcon.On)
        selected_sizes = icon.availableSizes(QIcon.Selected, QIcon.On)
        selected_additional_sizes = [size for size in selected_sizes if size not in normal_sizes]
        for size in normal_sizes + selected_additional_sizes:
            pixmap = icon.pixmap(size, QIcon.Selected, QIcon.On)
            self.alternate_icon.addPixmap(pixmap, QIcon.Normal, QIcon.On)
95 96 97 98 99
        disabled_sizes = icon.availableSizes(QIcon.Disabled, QIcon.On)
        selected_additional_sizes = [size for size in selected_sizes if size not in disabled_sizes]
        for size in disabled_sizes + selected_additional_sizes:
            pixmap = icon.pixmap(size, QIcon.Selected, QIcon.On)
            self.alternate_icon.addPixmap(pixmap, QIcon.Disabled, QIcon.On)
100 101 102
        super(StreamButton, self).setIcon(icon)


Dan Pascu's avatar
Dan Pascu committed
103 104 105 106
class SegmentTypeMeta(type):
    def __repr__(cls):
        return cls.__name__

107

Dan Pascu's avatar
Dan Pascu committed
108 109 110 111
class SegmentType(object):
    __metaclass__ = SegmentTypeMeta
    style_sheet = ''

112

Dan Pascu's avatar
Dan Pascu committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
class SingleSegment(SegmentType):
    style_sheet = """
                     QToolButton {
                         background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #fafafa, stop:1 #bababa);
                         border-color: #545454;
                         border-radius: 4px;
                         border-width: 1px;
                         border-style: solid;
                     }
                     
                     QToolButton:pressed {
                         background: qradialgradient(cx:0.5, cy:0.5, radius:1, fx:0.5, fy:0.5, stop:0 #dddddd, stop:1 #777777);
                     }
                  """

128

Dan Pascu's avatar
Dan Pascu committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
class LeftSegment(SegmentType):
    style_sheet = """
                     QToolButton {
                         background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #fafafa, stop:1 #bababa);
                         border-color: #545454;
                         border-top-left-radius: 4px;
                         border-bottom-left-radius: 4px;
                         border-width: 1px;
                         border-style: solid;
                     }
                     
                     QToolButton:pressed {
                         background: qradialgradient(cx:0.5, cy:0.5, radius:1, fx:0.5, fy:0.5, stop:0 #dddddd, stop:1 #777777);
                     }
                  """

145

Dan Pascu's avatar
Dan Pascu committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
class MiddleSegment(SegmentType):
    style_sheet = """
                     QToolButton {
                         background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #fafafa, stop:1 #bababa);
                         border-color: #545454;
                         border-width: 1px;
                         border-left-width: 0px;
                         border-style: solid;
                     }
                     
                     QToolButton:pressed {
                         background: qradialgradient(cx:0.5, cy:0.5, radius:1, fx:0.5, fy:0.5, stop:0 #dddddd, stop:1 #777777);
                     }
                  """

161

Dan Pascu's avatar
Dan Pascu committed
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
class RightSegment(SegmentType):
    style_sheet = """
                     QToolButton {
                         background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #fafafa, stop:1 #bababa);
                         border-color: #545454;
                         border-top-right-radius: 4px;
                         border-bottom-right-radius: 4px;
                         border-width: 1px;
                         border-left-width: 0px;
                         border-style: solid;
                     }
                     
                     QToolButton:pressed {
                         background: qradialgradient(cx:0.5, cy:0.5, radius:1, fx:0.5, fy:0.5, stop:0 #dddddd, stop:1 #777777);
                     }
                  """


class SegmentButton(QToolButton):
    SingleSegment = SingleSegment
    LeftSegment   = LeftSegment
    MiddleSegment = MiddleSegment
    RightSegment  = RightSegment

    hidden = pyqtSignal()
    shown  = pyqtSignal()

    def __init__(self, parent=None):
190
        super(SegmentButton, self).__init__(parent)
Dan Pascu's avatar
Dan Pascu committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204
        self.type = SingleSegment

    def _get_type(self):
        return self.__dict__['type']

    def _set_type(self, value):
        if not issubclass(value, SegmentType):
            raise ValueError("Invalid type: %r" % value)
        self.__dict__['type'] = value
        self.setStyleSheet(value.style_sheet)

    type = property(_get_type, _set_type)
    del _get_type, _set_type

205 206
    def setVisible(self, visible):
        super(SegmentButton, self).setVisible(visible)
Dan Pascu's avatar
Dan Pascu committed
207 208
        signal = self.shown if visible else self.hidden
        signal.emit()
Dan Pascu's avatar
Dan Pascu committed
209 210


211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
class RecordButton(SegmentButton):
    def __init__(self, parent=None):
        super(RecordButton, self).__init__(parent)
        self.timer_id = None
        self.toggled.connect(self._SH_Toggled)
        self.animation_icons = []
        self.animation_icon_index = 0

    def _get_animation_icon_index(self):
        return self.__dict__['animation_icon_index']

    def _set_animation_icon_index(self, index):
        self.__dict__['animation_icon_index'] = index
        self.update()

    animation_icon_index = property(_get_animation_icon_index, _set_animation_icon_index)
    del _get_animation_icon_index, _set_animation_icon_index

    def setIcon(self, icon):
        super(RecordButton, self).setIcon(icon)
        on_icon = QIcon(icon)
        off_icon = QIcon(icon)
        for size in off_icon.availableSizes(QIcon.Normal, QIcon.On):
            pixmap = off_icon.pixmap(size, QIcon.Normal, QIcon.Off)
            off_icon.addPixmap(pixmap, QIcon.Normal, QIcon.On)
        self.animation_icons = [on_icon, off_icon]

    def paintEvent(self, event):
        painter = QStylePainter(self)
        option = QStyleOptionToolButton()
        self.initStyleOption(option)
        option.icon = self.animation_icons[self.animation_icon_index]
        painter.drawComplexControl(QStyle.CC_ToolButton, option)

    def timerEvent(self, event):
        self.animation_icon_index = (self.animation_icon_index+1) % len(self.animation_icons)

    def _SH_Toggled(self, checked):
        if checked:
            self.timer_id = self.startTimer(1000)
            self.animation_icon_index = 0
        else:
            self.killTimer(self.timer_id)
            self.timer_id = None


257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
class SwitchViewButton(QPushButton):
    ContactView = 0
    SessionView = 1

    viewChanged = pyqtSignal(int)

    button_text = {ContactView: u'Switch to Calls', SessionView: u'Switch to Contacts'}
    button_dnd_text = {ContactView: u'Drag here to add to a conference', SessionView: u'Drag here to go back to contacts'}

    dnd_style_sheet1 = """
                          QPushButton {
                              background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ffffff, stop:1 #d3ffdc);
                              border-color: #237523;
                              border-radius: 4px;
                              border-width: 2px;
                              border-style: solid;
                          }
                       """

    dnd_style_sheet2 = """
                          QPushButton {
                              background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ffffff, stop:1 #c2ffce);
                              border-color: #dc3169;
                              border-radius: 4px;
                              border-width: 2px;
                              border-style: solid;
                          }
                       """

    def __init__(self, parent=None):
        super(SwitchViewButton, self).__init__(parent)
        self.setAcceptDrops(True)
        self.__dict__['dnd_active'] = False
        self.view = self.ContactView
291
        self.original_height = 20  # used to restore button size after DND
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
        self.dnd_timer = QTimer(self)
        self.dnd_timer.setInterval(100)
        self.dnd_timer.timeout.connect(self._update_dnd)
        self.dnd_timer.phase = 0
        self.clicked.connect(self._change_view)

    def _get_view(self):
        return self.__dict__['view']

    def _set_view(self, value):
        if self.__dict__.get('view', None) == value:
            return
        if value not in (self.ContactView, self.SessionView):
            raise ValueError("invalid view value: %r" % value)
        self.__dict__['view'] = value
        if self.dnd_active:
            text = self.button_dnd_text[value]
        else:
            text = self.button_text[value]
        self.setText(text)
        self.viewChanged.emit(value)

    view = property(_get_view, _set_view)
    del _get_view, _set_view

    def _get_dnd_active(self):
        return self.__dict__['dnd_active']

    def _set_dnd_active(self, value):
        if self.__dict__.get('dnd_active', None) == value:
            return
        self.__dict__['dnd_active'] = value
        if value is True:
            self.dnd_timer.phase = 0
326
            self.original_height = self.height()
327 328
            self.setStyleSheet(self.dnd_style_sheet1)
            self.setText(self.button_dnd_text[self.view])
329
            self.setFixedHeight(40)
330 331 332
        else:
            self.setStyleSheet('')
            self.setText(self.button_text[self.view])
333
            self.setFixedHeight(self.original_height)
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

    dnd_active = property(_get_dnd_active, _set_dnd_active)
    del _get_dnd_active, _set_dnd_active

    def _change_view(self):
        self.view = self.ContactView if self.view is self.SessionView else self.SessionView

    def _update_dnd(self):
        self.dnd_timer.phase += 1
        if self.dnd_timer.phase == 11:
            self.dnd_timer.stop()
            self.click()
            self.setStyleSheet(self.dnd_style_sheet1)
        else:
            style_sheet = (self.dnd_style_sheet1, self.dnd_style_sheet2)[self.dnd_timer.phase % 2]
            self.setStyleSheet(style_sheet)

    def dragEnterEvent(self, event):
352
        if self.dnd_active:
353 354 355 356 357 358 359 360
            event.accept()
            self._update_dnd()
            self.dnd_timer.start()
        else:
            event.ignore()

    def dragLeaveEvent(self, event):
        if self.dnd_active:
361 362 363
            self.dnd_timer.stop()
            self.dnd_timer.phase = 0
            self.setStyleSheet(self.dnd_style_sheet1)
364 365 366 367
        super(SwitchViewButton, self).dragLeaveEvent(event)

    def dropEvent(self, event):
        if self.dnd_active:
368 369 370
            self.dnd_timer.stop()
            self.dnd_timer.phase = 0
            self.setStyleSheet(self.dnd_style_sheet1)
371 372 373
        event.ignore()


374
class StateButtonStyle(QCommonStyle, ColorHelperMixin):
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    _pixel_metrics = {QStyle.PM_MenuButtonIndicator: 11, QStyle.PM_DefaultFrameWidth: 3, QStyle.PM_ButtonMargin: 1, QStyle.PM_ButtonShiftHorizontal: 0, QStyle.PM_ButtonShiftVertical: 0,
                      QStyle.PM_ButtonIconSize: 32}

    def polish(self, widget):
        widget.setAttribute(Qt.WA_Hover)
        super(StateButtonStyle, self).polish(widget)

    def pixelMetric(self, metric, option=None, widget=None):
        return self._pixel_metrics[metric]

    def sizeFromContents(self, element, option, size, widget=None):
        if element == QStyle.CT_ToolButton:
            return self.toolButtonSizeFromContents(option, size, widget)
        else:
            return super(StateButtonStyle, self).sizeFromContents(element, option, size, widget)

    def toolButtonSizeFromContents(self, option, size, widget):
392
        # Make width >= height to avoid super-skinny buttons
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
        margin = 2 * (self._pixel_metrics[QStyle.PM_DefaultFrameWidth] + self._pixel_metrics[QStyle.PM_ButtonMargin])
        if option.features & QStyleOptionToolButton.MenuButtonPopup:
            margin_size = QSize(margin+1, margin)
            menu_width = self._pixel_metrics[QStyle.PM_MenuButtonIndicator]
        else:
            margin_size = QSize(margin, margin)
            menu_width = 0
        if size.width() - menu_width < size.height():
            size.setWidth(size.height() + menu_width)
        return size + margin_size

    def drawComplexControl(self, control, option, painter, widget=None):
        if control == QStyle.CC_ToolButton:
            painter.save()
            self.drawToolButtonComplexControl(option, painter, widget)
            painter.restore()
        else:
            super(StateButtonStyle, self).drawComplexControl(control, option, painter, widget)

    def drawToolButtonComplexControl(self, option, painter, widget):
        button_color = option.palette.color(QPalette.Button)

        if option.state & (QStyle.State_On|QStyle.State_Sunken):
            self.drawToolButtonSunkenBezel(painter, QRectF(option.rect).adjusted(1, 1, -1, -1), button_color)
        else:
            enabled = bool(option.state & QStyle.State_Enabled)
            hoover = enabled and bool(option.state & QStyle.State_MouseOver)
            has_focus = enabled and bool(option.state & QStyle.State_HasFocus)
            self.drawToolButtonBezel(painter, QRectF(option.rect), button_color, hoover=hoover, has_focus=has_focus)
        if option.features & QStyleOptionToolButton.MenuButtonPopup:
            self.drawToolButtonMenuIndicator(option, painter, widget)
        self.drawToolButtonContent(option, painter, widget)

    def drawToolButtonBezel(self, painter, rect, color, hoover=False, has_focus=False):
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setPen(Qt.NoPen)

        glow_rect = rect
        shadow_rect  = rect.adjusted(1, 1, -1, -1)
        border_rect  = rect.adjusted(2, 2, -2, -2)
        content_rect = rect.adjusted(3, 3, -3, -3)

        focus_color = QColor('#3aa7dd')
        hoover_color = QColor('#6ed6ff')
        shadow_color = ColorScheme.shade(self.background_bottom_color(color), ColorScheme.ShadowShade, 0.0)
        border_color_top = ColorScheme.shade(self.background_top_color(color), ColorScheme.LightShade, 0.0)
        border_color_bottom = ColorScheme.shade(self.background_bottom_color(color), ColorScheme.MidlightShade, 0.5)

        # glow
        painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
        blend = QLinearGradient(glow_rect.topLeft(), glow_rect.bottomLeft())
        if hoover:
            blend.setColorAt(0.0, self.color_with_alpha(hoover_color, 0x45))
            blend.setColorAt(0.9, self.color_with_alpha(hoover_color, 0x45))
            blend.setColorAt(1.0, self.color_with_alpha(ColorUtils.mix(hoover_color, shadow_color, 0.4), 0x55))
        elif has_focus:
            blend.setColorAt(0.0, self.color_with_alpha(focus_color, 0x45))
            blend.setColorAt(0.9, self.color_with_alpha(focus_color, 0x45))
            blend.setColorAt(1.0, self.color_with_alpha(ColorUtils.mix(focus_color, shadow_color, 0.4), 0x55))
        else:
453
            blend.setColorAt(0.0, Qt.transparent)  # or @0.5
454
            blend.setColorAt(0.9, self.color_with_alpha(shadow_color, 0x10))
455
            # blend.setColorAt(1-4.0/glow_rect.height(), self.color_with_alpha(shadow_color, 0x10))  # this is for exactly 4 pixels from bottom
456 457
            blend.setColorAt(1.0, self.color_with_alpha(shadow_color, 0x30)) # 0x25, 0x30 or 0x35
        painter.setBrush(blend)
458
        painter.drawRoundedRect(glow_rect, 5, 5)  # 5 or 6
459 460 461 462 463 464 465 466 467 468 469 470

        # shadow
        painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
        if hoover:
            painter.setBrush(hoover_color)
        elif has_focus:
            painter.setBrush(focus_color)
        else:
            blend = QLinearGradient(shadow_rect.topLeft(), shadow_rect.bottomLeft())
            blend.setColorAt(0.00, self.color_with_alpha(shadow_color, 0x10))
            blend.setColorAt(1.00, self.color_with_alpha(shadow_color, 0x80))
            painter.setBrush(blend)
471
        painter.drawRoundedRect(shadow_rect, 4, 4)  # 4 or 5
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

        # border
        painter.setCompositionMode(QPainter.CompositionMode_Source)
        blend = QLinearGradient(border_rect.topLeft(), border_rect.bottomLeft())
        blend.setColorAt(0.0, border_color_top)
        blend.setColorAt(1.0, border_color_bottom)
        painter.setBrush(blend)
        painter.drawRoundedRect(border_rect, 4, 4)

        # content
        painter.setCompositionMode(QPainter.CompositionMode_Source)
        grad = QLinearGradient(content_rect.topLeft(), content_rect.bottomLeft())
        grad.setColorAt(0.0, self.background_top_color(color))
        grad.setColorAt(1.0, self.background_bottom_color(color))
        painter.setBrush(QBrush(grad))
        painter.drawRoundedRect(content_rect, 4, 4)

    def drawToolButtonSunkenBezel(self, painter, rect, color):
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setPen(Qt.NoPen)

        hole_rect    = rect.adjusted(1, 1, -1, -1)
        shadow_rect  = rect.adjusted(2, 2, -2, -2)
        content_rect = rect.adjusted(3, 3, -3, -3)

        shade_color  = ColorScheme.shade(self.background_bottom_color(color), ColorScheme.MidlightShade, 0.5)
        shadow_color = ColorScheme.shade(self.background_bottom_color(color), ColorScheme.ShadowShade, 0.0)

        if self.calc_shadow_color(color).value() > color.value():
            content_grad = QLinearGradient(0, content_rect.top(), 0, content_rect.bottom()+content_rect.height()*0.2)
            content_grad.setColorAt(0.0, self.background_bottom_color(color))
            content_grad.setColorAt(1.0, self.background_top_color(color))
        else:
            content_grad = QLinearGradient(0, content_rect.top()-content_rect.height()*0.2, 0, content_rect.bottom())
            content_grad.setColorAt(0.0, self.background_top_color(color))
            content_grad.setColorAt(1.0, self.background_bottom_color(color))

        # hole edge
        painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
        blend = QLinearGradient(hole_rect.topLeft(), hole_rect.bottomLeft())
        blend.setColorAt(0.0, self.color_with_alpha(shadow_color, 0x80))
        blend.setColorAt(1.0, self.color_with_alpha(shadow_color, 0x20))
        painter.setBrush(blend)
515
        painter.drawRoundedRect(hole_rect, 4, 4)  # 4 or 5
516 517 518 519

        # shadow
        painter.setCompositionMode(QPainter.CompositionMode_Source)
        painter.setBrush(content_grad)
520
        painter.drawRoundedRect(shadow_rect, 4, 4)  # 5 or 6
521 522 523 524 525 526 527
        painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
        blend = QLinearGradient(shadow_rect.topLeft(), shadow_rect.bottomLeft())
        blend.setColorAt(0.0, self.color_with_alpha(shadow_color, 0x40))
        blend.setColorAt(0.1, self.color_with_alpha(shadow_color, 0x07))
        blend.setColorAt(0.9, self.color_with_alpha(shadow_color, 0x07))
        blend.setColorAt(1.0, shade_color)
        painter.setBrush(blend)
528
        painter.drawRoundedRect(shadow_rect, 4, 4)  # 5 or 6
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641

        # content
        painter.setCompositionMode(QPainter.CompositionMode_Source)
        painter.setBrush(content_grad)
        painter.drawRoundedRect(content_rect, 4, 4)

    def drawToolButtonMenuIndicator(self, option, painter, widget=None):
        arrow_rect = self.proxy().subControlRect(QStyle.CC_ToolButton, option, QStyle.SC_ToolButtonMenu, widget)

        text_color = option.palette.color(QPalette.WindowText if option.state & QStyle.State_AutoRaise else QPalette.ButtonText)
        button_color = option.palette.color(QPalette.Button)
        background_color = self.background_color(button_color, 0.5)

        painter.save()

        # draw separating vertical line
        if option.state & (QStyle.State_On|QStyle.State_Sunken):
            top_offset, bottom_offset = 4, 3
        else:
            top_offset, bottom_offset = 2, 2

        if option.direction == Qt.LeftToRight:
            separator_line = QLineF(arrow_rect.x()-3, arrow_rect.top()+top_offset, arrow_rect.x()-3, arrow_rect.bottom()-bottom_offset)
        else:
            separator_line = QLineF(arrow_rect.right()+3, arrow_rect.top()+top_offset, arrow_rect.right()+3, arrow_rect.bottom()-bottom_offset)

        light_gradient = QLinearGradient(separator_line.p1(), separator_line.p2())
        light_gradient.setColorAt(0.0, ColorScheme.shade(self.background_top_color(button_color), ColorScheme.LightShade, 0.0))
        light_gradient.setColorAt(1.0, ColorScheme.shade(self.background_bottom_color(button_color), ColorScheme.MidlightShade, 0.5))
        separator_color = ColorScheme.shade(self.background_bottom_color(button_color), ColorScheme.MidShade, 0.0)

        painter.setRenderHint(QPainter.Antialiasing, False)
        painter.setPen(QPen(light_gradient, 1))
        painter.drawLine(separator_line.translated(-1, 0))
        painter.drawLine(separator_line.translated(+1, 0))
        painter.setPen(QPen(separator_color, 1))
        painter.drawLine(separator_line)

        # draw arrow
        arrow = QPolygonF([QPointF(-3, -1.5), QPointF(0.5, 2.5), QPointF(4, -1.5)])
        if option.direction == Qt.LeftToRight:
            arrow.translate(-2, 1)
        else:
            arrow.translate(+2, 1)
        pen_thickness = 1.6

        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.translate(arrow_rect.center())

        painter.translate(0, +1)
        painter.setPen(QPen(self.calc_light_color(background_color), pen_thickness, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
        painter.drawPolyline(arrow)
        painter.translate(0, -1)
        painter.setPen(QPen(self.deco_color(background_color, text_color), pen_thickness, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
        painter.drawPolyline(arrow)

        painter.restore()

    def drawToolButtonContent(self, option, painter, widget):
        if option.state & QStyle.State_Enabled:
            pixmap = widget.pixmap(QIcon.Normal)
        else:
            pixmap = widget.pixmap(QIcon.Disabled)
        if not pixmap.isNull():
            margin = self._pixel_metrics[QStyle.PM_DefaultFrameWidth] + self._pixel_metrics[QStyle.PM_ButtonMargin]
            if option.features & QStyleOptionToolButton.MenuButtonPopup and option.direction == Qt.LeftToRight:
                right_offset = 1
            else:
                right_offset = 0
            content_rect = QRectF(self.proxy().subControlRect(QStyle.CC_ToolButton, option, QStyle.SC_ToolButton, widget)).adjusted(margin, margin, -margin-right_offset, -margin)
            pixmap_rect  = QRectF(pixmap.rect())
            pixmap_rect.moveCenter(content_rect.center())
            painter.setRenderHint(QPainter.Antialiasing, True)
            painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
            painter.drawPixmap(pixmap_rect.topLeft(), pixmap)


class StateButton(QToolButton):
    default_color = QColor('#efedeb')

    def __init__(self, parent=None):
        super(StateButton, self).__init__(parent)
        self.setPopupMode(QToolButton.MenuButtonPopup)
        self.setToolButtonStyle(Qt.ToolButtonIconOnly)
        palette = self.palette()
        palette.setColor(QPalette.Button, self.default_color)
        self.setPalette(palette)
        self.setStyle(StateButtonStyle())

    def pixmap(self, mode=QIcon.Normal, state=QIcon.Off):
        pixmap = self.icon().pixmap(self.iconSize(), mode, state)
        if pixmap.isNull():
            return pixmap

        size = max(pixmap.width(), pixmap.height())
        offset_x = (size - pixmap.width())/2
        offset_y = (size - pixmap.height())/2

        new_pixmap = QPixmap(size, size)
        new_pixmap.fill(Qt.transparent)
        path = QPainterPath()
        path.addRoundedRect(0, 0, size, size, 3.7, 3.7)
        painter = QPainter(new_pixmap)
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
        painter.setClipPath(path)
        painter.drawPixmap(offset_x, offset_y, pixmap)
        painter.end()

        return new_pixmap


class PresenceState(object):
642
    def __init__(self, name, color, icon, internal=False):
643 644 645
        self.name = name
        self.color = color
        self.icon = icon
646 647 648 649 650 651 652 653 654 655
        self.internal = internal

    def __eq__(self, other):
        if isinstance(other, PresenceState):
            return self.name == other.name
        return NotImplemented

    def __ne__(self, other):
        equal = self.__eq__(other)
        return NotImplemented if equal is NotImplemented else not equal
656 657 658 659

    def __repr__(self):
        return self.name

660 661 662 663
    @property
    def Internal(self):
        return PresenceState(self.name, self.color, self.icon, True)

664 665 666 667 668 669 670

class AccountState(StateButton):
    Invisible = PresenceState('Invisible', '#efedeb', Resources.get('icons/state-invisible.svg'))
    Available = PresenceState('Available', '#00ff00', Resources.get('icons/state-available.svg'))
    Away = PresenceState('Away', '#ffff00', Resources.get('icons/state-away.svg'))
    Busy = PresenceState('Busy', '#ff0000', Resources.get('icons/state-busy.svg'))

671
    stateChanged = pyqtSignal()
672 673 674 675 676 677 678 679 680 681 682 683 684 685

    history_size = 7

    def __init__(self, parent=None):
        super(AccountState, self).__init__(parent)
        menu = QMenu(self)
        for state in (self.Available, self.Away, self.Busy, self.Invisible):
            action = menu.addAction(QIcon(state.icon), state.name)
            action.state = state
            action.note = None
        menu.addSeparator()
        menu.triggered.connect(self._SH_MenuTriggered)
        self.setMenu(menu)
        self.state = self.Invisible
686 687 688 689
        self.note = None

    def _get_history(self):
        return [(action.state.name, action.note) for action in self.menu().actions()[5:]]
690

691 692 693 694 695 696 697 698 699
    def _set_history(self, values):
        menu = self.menu()
        for action in menu.actions()[5:]:
            menu.removeAction(action)
        for state_name, note in values:
            try:
                state = getattr(self, state_name)
            except AttributeError:
                continue
700
            action = menu.addAction(QIcon(state.icon), note)
701 702
            action.state = state
            action.note = note
703

704 705
    history = property(_get_history, _set_history)
    del _get_history, _set_history
706 707 708 709 710

    def _SH_MenuTriggered(self, action):
        if hasattr(action, 'state'):
            self.setState(action.state, action.note)

711 712 713 714 715 716 717 718
    def mousePressEvent_no(self, event):
        if event.button() == Qt.LeftButton and self.popupMode() == QToolButton.MenuButtonPopup:
            option = QStyleOptionToolButton()
            self.initStyleOption(option)
            position = self.style().subControlRect(QStyle.CC_ToolButton, option, QStyle.SC_ToolButtonMenu, self).center()
            event = event.__class__(event.type(), position, self.mapToGlobal(position), event.button(), event.buttons(), event.modifiers())
        return super(AccountState, self).mousePressEvent(event)

719
    def setState(self, state, note=None):
720 721
        if state == self.state and note == self.note:
            return
722
        self.state = state
723
        self.note = note
724 725 726
        palette = self.palette()
        palette.setColor(QPalette.Button, QColor(state.color))
        self.setPalette(palette)
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
        if note and not state.internal:
            menu = self.menu()
            actions = menu.actions()[5:]
            try:
                action = next(action for action in actions if action.state is state and action.note == note)
            except StopIteration:
                action = QAction(QIcon(state.icon), note, menu)
                if len(actions) == 0:
                    menu.addAction(action)
                else:
                    if len(actions) >= self.history_size:
                        menu.removeAction(actions[-1])
                    menu.insertAction(actions[0], action)
                action.state = state
                action.note = note
742
            else:
743 744 745 746
                if action is not actions[0]:
                    menu.removeAction(action)
                    menu.insertAction(actions[0], action)
        self.stateChanged.emit()
747

748