Commits

Henning Schröder committed 7f3e3fe

new dockwidgets without titlebar if tabbed

Comments (0)

Files changed (1)

codeaide/ide/base.py

 import atexit
 import gettext
 from PyQt4.QtCore import (
-    Qt, SIGNAL, QObject,
+    Qt, SIGNAL, QObject, QCoreApplication, QEvent, QPoint, QSize,
     QVariant, QTimer)
 from PyQt4.QtGui import (
-    QDockWidget, QMenu, QToolBar, QAction, QToolButton,
-    QKeySequence, QLabel, QFileDialog,
+    QTabWidget, QTabBar, QDockWidget, QMenu, QToolBar, QAction, QToolButton,
+    QKeySequence, QLabel, QFileDialog, QMouseEvent,
     QStylePainter, QStyleOptionDockWidget, QStyle)
 
 from codeaide.interface import implements, classproperty
 
 
 
-    
+
 def sorted_by_priority(l):
-    return sorted(l, 
+    return sorted(l,
                   cmp=lambda a, b: cmp(b.priority, a.priority))
 
 
         self.mainwin = mainwin
         self.toolbars = {}
         self.menu = QMenu(mainwin)
-        
-            
+
+
     def get_toolbar(self, title):
         tb = self.toolbars.get(title)
         return tb
         self.parent = parent
         self.action.connect(self.action, SIGNAL("triggered()"), self.execute)
         self.init()
-        
+
 
     def init(self):
         pass
         raise NotImplementedError, self.execute
 
 
-    
+
 class DocumentCommandBase(CommandBase):
-    
+
 
     def __init__(self, parent):
         CommandBase.__init__(self, parent)
         self.doc = None
         parent.connect(parent.window(), SIGNAL("active_document_changed(PyQt_PyObject)"), self.on_active_document_changed)
 
-        
+
     def check_enabled(self):
         return True
-        
-        
+
+
     def on_active_document_changed(self, doc):
         self.doc = doc
         if doc is None:
             is_enabled = self.check_enabled()
         self.action.setEnabled(is_enabled)
 
-    
-    
-    
+
+
+
 
 class CheckCommandBase(CommandBase):
 
 
 
 
+_installed_filters = []
+
+
+class TitleBar(QLabel):
+
+    def __init__(self, *args):
+        QLabel.__init__(self, *args)
+        self.setFixedHeight(2)
+
+
+    def sizeHint(self):
+        return QSize(2,2)
+
+
+    def minimumSizeHint(self):
+        return QSize(2,2)
+
+
 
 class DockWidget(QDockWidget):
 
     def __init__(self, *args):
         QDockWidget.__init__(self, *args)
+        self._titlebar = TitleBar(self)
+        self.setTitleBarWidget(self._titlebar)
+        self.visibilityChanged.connect(self.on_visibility_changed)
         self.setStyleSheet("""
-          QDockWidget {
-          border: 1px solid lightgray;
-          }
+         QDockWidget {
+             border: 3px solid black;
+         }
 
-          QDockWidget::title {
-          text-align: left; /* align the text to the left */
-          background: lightgray;
-          padding-left: 5px;
-          }
-          
-          QDockWidget::close-button, QDockWidget::float-button {
-            border: 1px solid transparent;
-            background: darkgray;
-            padding: 0px;
-          }
-          
-          QDockWidget::close-button:hover, QDockWidget::float-button:hover {
-          background: gray;
-          }
+         QDockWidget::title {
+             text-align: left;
+             background: grey;
+             padding-left: 35px;
+         }
 
-          QDockWidget::close-button:pressed, QDockWidget::float-button:pressed {
-          padding: 1px -1px -1px 1px;
-          }
-          """)
+        """)
 
-    def paintEvent(self, event):
-        QDockWidget.paintEvent(self, event)
-        if self.isFloating():
-            painter = QStylePainter(self)
-            styleOption = QStyleOptionDockWidget()
-            styleOption.initFrom(self)
-            painter.drawPrimitive(QStyle.PE_FrameWindow, styleOption)
+
+    def restore_titlebar(self):
+        self.setTitleBarWidget(None)
+
+
+    def hide_titlebar(self):
+        self.setTitleBarWidget(self._titlebar)
+
+
+    def style_tabbar(self, tab):
+        tab.setStyleSheet("""
+        /* Style the tab using the tab sub-control. Note that it reads QTabBar _not_ QTabWidget */
+        QTabBar {
+            background-color: grey;
+        }
+        QTabBar::tab {
+            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
+            stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
+            stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
+            border: 1px solid #C4C4C3;
+            border-bottom-color: #C2C7CB; /* same as the pane color */
+            border-top-left-radius: 4px;
+            border-top-right-radius: 4px;
+            min-width: 8ex;
+            padding: 2px;
+        }
+
+        QTabBar::tab:selected, QTabBar::tab:hover {
+            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
+            stop: 0 #fafafa, stop: 0.4 #f4f4f4,
+            stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
+        }
+
+        QTabBar::tab:selected {
+            border-color: #9B9B9B;
+            border-bottom-color: #C2C7CB; /* same as pane color */
+        }
+
+        QTabBar::tab:!selected {
+            margin-top: 2px; /* make non-selected tabs look smaller */
+        }
+        """)
+
+
+    def on_visibility_changed(self, is_visible):
+        tab = self.dock_tab(self)
+        if tab:
+            if not tab in _installed_filters:
+                tab.installEventFilter(self)
+                _installed_filters.append(tab)
+                self.style_tabbar(tab)
+            if not self.titleBarWidget():
+                QTimer.singleShot(0, self.hide_titlebar)
+        else:
+            if self.titleBarWidget():
+                QTimer.singleShot(0, self.restore_titlebar)
+
+
+
+    def dock_tab(self, dock):
+        title = dock.windowTitle()
+        for child in self.parent().children():
+            if isinstance(child, QTabBar):
+                for i in range(child.count()):
+                    if child.tabText(i) == title:
+                        return child
+
+
+    def tab_dock(self, tab):
+        title = tab.tabText(tab.currentIndex())
+        for child in self.parent().children():
+            if isinstance(child, QDockWidget):
+                if child.windowTitle() == title:
+                    return child
+
+
+    def eventFilter(self, obj, event):
+        if isinstance(obj, QTabBar) and isinstance(event, QMouseEvent):
+            ev_type = event.type()
+            if ev_type == QEvent.MouseButtonPress:
+                pos = event.pos()
+                self._pos = pos
+                if obj.tabRect(obj.currentIndex()).contains(pos):
+                    mev = QMouseEvent(ev_type, QPoint(1, 1),
+                                      event.button(), event.buttons(), event.modifiers())
+                    dock = self.tab_dock(obj)
+                    QCoreApplication.sendEvent(dock, mev)
+
+            elif ev_type == QEvent.MouseMove:
+                if (event.pos() - self._pos).manhattanLength() > 2:
+                    dock = self.tab_dock(obj)
+                    QCoreApplication.sendEvent(dock, event)
+
+        return QDockWidget.eventFilter(self, obj, event)
+
 
 
 
 
     def __init__(self, parent):
         QObject.__init__(self, parent)
- 
+
 
     def create_widget(self, parent):
         raise NotImplementedError, self.create_widget
 
-    
+
     def switch_to(self):
         self.widget.show()
         self.widget.widget().setFocus()
 
-        
+
     def set_focus(self, flag):
         if flag:
             self.widget.widget().setFocus()
 
 
 class DockManager(object):
-    
-    
+
+
     dockwidget_factory = DockWidget
 
 
     def __init__(self, mainwin):
         self.mainwin = mainwin
+        mainwin.setTabPosition(Qt.AllDockWidgetAreas, QTabWidget.North)
+        mainwin.setDockNestingEnabled(True)
         mainwin.setCorner(Qt.TopLeftCorner, Qt.LeftDockWidgetArea)
         mainwin.setCorner(Qt.TopRightCorner, Qt.RightDockWidgetArea)
         mainwin.setCorner(Qt.BottomLeftCorner, Qt.LeftDockWidgetArea)
         self.menu = QMenu(mainwin)
         self.docks = []
         self.widgets = []
-    
-        
+
+
     def create_widget(self, dock):
         child_widget = dock.create_widget(dock.widget)
         if dock.switch_shortcut:
         dock.widget.setWidget(child_widget)
         self.widgets.append(child_widget)
 
-        
+
     def add_dockwidget(self, dock):
         dock.widget = self.dockwidget_factory(self.mainwin)
         cls_name = dock.__class__.__name__
         #self.create_widget(dock)
         if dock.statusbar:
             self.mainwin.statusbar_manager.add_action(dock.widget.toggleViewAction())
-            
+
         self.mainwin.addDockWidget(dock.area, dock.widget)
 
         action = dock.widget.toggleViewAction()
 
 
 class StatusbarManager(object):
-    
-    
+
+
     def __init__(self, mainwin):
         self.mainwin = mainwin
         self.bar = mainwin.statusBar()
 
     def show_message(self, msg):
         self.bar.showMessage(msg)
-    
+
 
     def add_label(self, name, stretch=0):
         l = QLabel(self.bar)
         self.labels.append(name)
         self.add_widget(name, l, stretch)
 
-        
+
     def add_widget(self, name, widget, stretch=0):
         self.widgets[name] = widget
         self.bar.addWidget(widget, stretch)