|
9 | 9 |
|
10 | 10 | __author__ = "robertbasic" |
11 | 11 |
|
| 12 | +from PyQt5.QtCore import Qt, pyqtSignal |
| 13 | +from PyQt5.QtWidgets import QTabWidget, QTabBar |
12 | 14 |
|
13 | | -from PyQt5.QtWidgets import QTabWidget |
14 | | -from pugdebug.gui.widgets.tabbar import PugdebugTabBar; |
15 | 15 |
|
16 | 16 | class PugdebugDocumentViewer(QTabWidget): |
17 | 17 |
|
@@ -75,3 +75,40 @@ def get_document_by_path(self, path): |
75 | 75 | def remove_line_highlights(self): |
76 | 76 | for index, path in self.tabs.items(): |
77 | 77 | self.widget(index).remove_line_highlights() |
| 78 | + |
| 79 | + |
| 80 | +class PugdebugTabBar(QTabBar): |
| 81 | + """Adds a signal when the middle button is clicked to the QTabBar widget. |
| 82 | +
|
| 83 | + A click is defined as a press event followed by a release event over the |
| 84 | + same tab. |
| 85 | +
|
| 86 | + Stolen from: http://stackoverflow.com/a/9445581/84245 |
| 87 | + """ |
| 88 | + middle_clicked_signal = pyqtSignal(int) |
| 89 | + |
| 90 | + def __init__(self): |
| 91 | + super(QTabBar, self).__init__() |
| 92 | + self.previousMiddleIndex = -1 |
| 93 | + |
| 94 | + def mousePressEvent(self, mouseEvent): |
| 95 | + """Triggered when a mouse button is pressed. |
| 96 | +
|
| 97 | + If it's the middle button, remember which over which tab it |
| 98 | + was pressed. |
| 99 | + """ |
| 100 | + if mouseEvent.button() == Qt.MidButton: |
| 101 | + self.previousIndex = self.tabAt(mouseEvent.pos()) |
| 102 | + QTabBar.mousePressEvent(self, mouseEvent) |
| 103 | + |
| 104 | + def mouseReleaseEvent(self, mouseEvent): |
| 105 | + """Triggered when a mouse button is released. |
| 106 | +
|
| 107 | + If a middle button was pressed previously, and if it is released over |
| 108 | + the same tab, emit the signal. |
| 109 | + """ |
| 110 | + if (mouseEvent.button() == Qt.MidButton and |
| 111 | + self.previousIndex == self.tabAt(mouseEvent.pos())): |
| 112 | + self.middle_clicked_signal.emit(self.previousIndex) |
| 113 | + self.previousIndex = -1 |
| 114 | + QTabBar.mouseReleaseEvent(self, mouseEvent) |
0 commit comments