src/eric7/EricWidgets/EricSqueezeLabels.py

branch
eric7
changeset 10933
95a15b70f7bb
parent 10439
21c28b0f9e41
child 11090
f5f5f5803935
--- a/src/eric7/EricWidgets/EricSqueezeLabels.py	Fri Sep 27 11:00:27 2024 +0200
+++ b/src/eric7/EricWidgets/EricSqueezeLabels.py	Fri Sep 27 17:27:11 2024 +0200
@@ -8,11 +8,11 @@
 label.
 """
 
+import os
+
 from PyQt6.QtCore import Qt
 from PyQt6.QtWidgets import QLabel
 
-from eric7.SystemUtilities import FileSystemUtilities
-
 
 class EricSqueezeLabel(QLabel):
     """
@@ -118,13 +118,7 @@
         @type QPaintEvent
         """
         if self.length(self.__path) > self.contentsRect().width():
-            super().setText(
-                self.__surrounding.format(
-                    FileSystemUtilities.compactPath(
-                        self.__path, self.contentsRect().width(), self.length
-                    )
-                )
-            )
+            super().setText(self.__surrounding.format(self.__compactPath()))
         else:
             super().setText(self.__surrounding.format(self.__path))
         super().paintEvent(event)
@@ -140,3 +134,43 @@
         """
         fm = self.fontMetrics()
         return fm.horizontalAdvance(self.__surrounding.format(txt))
+
+    ############################################################################
+    ## Internal path handling methods.
+    ############################################################################
+
+    def __compactPath(self):
+        """
+        Private method to return a compacted path fitting inside the label width.
+
+        @return compacted path
+        @rtype str
+        """
+        width = self.contentsRect().width()
+        path = self.__path
+
+        if self.length(path) <= width:
+            return path
+
+        ellipsis = "..."
+
+        head, tail = os.path.split(path)
+        mid = len(head) // 2
+        head1 = head[:mid]
+        head2 = head[mid:]
+        while head1:
+            # head1 is same size as head2 or one shorter
+            path = os.path.join("{0}{1}{2}".format(head1, ellipsis, head2), tail)
+            if self.length(path) <= width:
+                return path
+            head1 = head1[:-1]
+            head2 = head2[1:]
+        path = os.path.join(ellipsis, tail)
+        if self.length(path) <= width:
+            return path
+        while tail:
+            path = "{0}{1}".format(ellipsis, tail)
+            if self.length(path) <= width:
+                return path
+            tail = tail[1:]
+        return ""

eric ide

mercurial