Skip to content
This repository was archived by the owner on Aug 28, 2020. It is now read-only.

Commit 416229d

Browse files
committed
Close tabs by middle clicking
1 parent be5e6af commit 416229d

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

pugdebug/gui/documents.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
from PyQt5.QtWidgets import QTabWidget
14-
14+
from pugdebug.gui.widgets.tabbar import PugdebugTabBar;
1515

1616
class PugdebugDocumentViewer(QTabWidget):
1717

@@ -20,6 +20,11 @@ class PugdebugDocumentViewer(QTabWidget):
2020
def __init__(self):
2121
super(PugdebugDocumentViewer, self).__init__()
2222

23+
# Use the extended tab bar wiget to have middle click close tabs
24+
self.tab_bar = PugdebugTabBar()
25+
self.tab_bar.middle_clicked_signal.connect(self.close_tab)
26+
self.setTabBar(self.tab_bar)
27+
2328
self.setTabsClosable(True)
2429

2530
def add_tab(self, document_widget, filename, path):

pugdebug/gui/widgets/__init__.py

Whitespace-only changes.

pugdebug/gui/widgets/tabbar.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)