|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | + pugdebug - a standalone PHP debugger |
| 5 | + ========================= |
| 6 | + copyright: (c) 2015 Robert Basic |
| 7 | + license: GNU GPL v3, see LICENSE for more details |
| 8 | +""" |
| 9 | + |
| 10 | +__author__ = "ihabunek" |
| 11 | + |
| 12 | +from PyQt5.QtCore import Qt, pyqtSignal |
| 13 | +from PyQt5.QtWidgets import QTabBar |
| 14 | + |
| 15 | + |
| 16 | +class PugdebugTabBar(QTabBar): |
| 17 | + """Adds a signal when the middle button is clicked to the QTabBar widget. |
| 18 | +
|
| 19 | + A click is defined as a press event followed by a release event over the |
| 20 | + same tab. |
| 21 | +
|
| 22 | + Stolen from: http://stackoverflow.com/a/9445581/84245 |
| 23 | + """ |
| 24 | + middle_clicked_signal = pyqtSignal(int) |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + super(QTabBar, self).__init__() |
| 28 | + self.previousMiddleIndex = -1 |
| 29 | + |
| 30 | + def mousePressEvent(self, mouseEvent): |
| 31 | + """Triggered when a mouse button is pressed. |
| 32 | +
|
| 33 | + If it's the middle button, remember which over which tab it was pressed. |
| 34 | + """ |
| 35 | + if mouseEvent.button() == Qt.MidButton: |
| 36 | + self.previousIndex = self.tabAt(mouseEvent.pos()) |
| 37 | + QTabBar.mousePressEvent(self, mouseEvent) |
| 38 | + |
| 39 | + def mouseReleaseEvent(self, mouseEvent): |
| 40 | + """Triggered when a mouse button is released. |
| 41 | +
|
| 42 | + If a middle button was pressed previously, and if it is released over |
| 43 | + the same tab, emit the signal. |
| 44 | + """ |
| 45 | + if mouseEvent.button() == Qt.MidButton and \ |
| 46 | + self.previousIndex == self.tabAt(mouseEvent.pos()): |
| 47 | + self.middle_clicked_signal.emit(self.previousIndex) |
| 48 | + self.previousIndex = -1 |
| 49 | + QTabBar.mouseReleaseEvent(self, mouseEvent) |
0 commit comments