6 """ |
6 """ |
7 Module implementing a sidebar class. |
7 Module implementing a sidebar class. |
8 """ |
8 """ |
9 |
9 |
10 import enum |
10 import enum |
11 |
11 import json |
12 from PyQt6.QtCore import ( |
12 |
13 QEvent, QSize, Qt, QByteArray, QDataStream, QIODevice, QTimer |
13 from PyQt6.QtCore import QEvent, QSize, Qt, QTimer |
14 ) |
|
15 from PyQt6.QtWidgets import ( |
14 from PyQt6.QtWidgets import ( |
16 QTabBar, QWidget, QStackedWidget, QBoxLayout, QToolButton, QSizePolicy |
15 QTabBar, QWidget, QStackedWidget, QBoxLayout, QToolButton, QSizePolicy |
17 ) |
16 ) |
18 |
17 |
19 from E5Gui.E5Application import e5App |
18 from E5Gui.E5Application import e5App |
578 self.__maxSize = self.maximumHeight() |
577 self.__maxSize = self.maximumHeight() |
579 else: |
578 else: |
580 self.__minSize = self.minimumSizeHint().width() |
579 self.__minSize = self.minimumSizeHint().width() |
581 self.__maxSize = self.maximumWidth() |
580 self.__maxSize = self.maximumWidth() |
582 |
581 |
583 data = QByteArray() |
582 dataDict = { |
584 stream = QDataStream(data, QIODevice.OpenModeFlag.WriteOnly) |
583 "version": self.Version, |
585 stream.setVersion(QDataStream.Version.Qt_6_1) |
584 "minimized": self.__minimized, |
586 |
585 "big_size": [self.__bigSize.width(), self.__bigSize.height()], |
587 stream.writeUInt16(self.Version) |
586 "min_size": self.__minSize, |
588 stream.writeBool(self.__minimized) |
587 "max_size": self.__maxSize, |
589 stream << self.__bigSize |
588 "splitter_sizes": self.splitterSizes, |
590 stream.writeUInt32(self.__minSize) |
589 "auto_hide": self.__autoHide |
591 stream.writeUInt32(self.__maxSize) |
590 } |
592 stream.writeUInt32(len(self.splitterSizes)) |
591 data = json.dumps(dataDict) |
593 for size in self.splitterSizes: |
|
594 stream.writeUInt32(size) |
|
595 stream.writeBool(self.__autoHide) |
|
596 |
592 |
597 return data |
593 return data |
598 |
594 |
599 def restoreState(self, state): |
595 def restoreState(self, state): |
600 """ |
596 """ |
601 Public method to restore the state of the sidebar. |
597 Public method to restore the state of the sidebar. |
602 |
598 |
603 @param state byte array containing the saved state (QByteArray) |
599 @param state byte array containing the saved state (QByteArray) |
604 @return flag indicating success (boolean) |
600 @return flag indicating success (boolean) |
605 """ |
601 """ |
606 if state.isEmpty(): |
602 if not isinstance(state, str) or state == "": |
|
603 return False |
|
604 |
|
605 try: |
|
606 stateDict = json.loads(state) |
|
607 except json.JSONDecodeError: |
|
608 return False |
|
609 |
|
610 if not stateDict: |
607 return False |
611 return False |
608 |
612 |
609 if self.__orientation in (E5SideBarSide.NORTH, E5SideBarSide.SOUTH): |
613 if self.__orientation in (E5SideBarSide.NORTH, E5SideBarSide.SOUTH): |
610 minSize = self.layout.minimumSize().height() |
614 minSize = self.layout.minimumSize().height() |
611 maxSize = self.maximumHeight() |
615 maxSize = self.maximumHeight() |
612 else: |
616 else: |
613 minSize = self.layout.minimumSize().width() |
617 minSize = self.layout.minimumSize().width() |
614 maxSize = self.maximumWidth() |
618 maxSize = self.maximumWidth() |
615 |
619 |
616 data = QByteArray(state) |
620 if stateDict["version"] == 2: |
617 stream = QDataStream(data, QIODevice.OpenModeFlag.ReadOnly) |
621 if stateDict["minimized"] and not self.__minimized: |
618 stream.setVersion(QDataStream.Version.Qt_6_1) |
622 self.shrink() |
619 version = stream.readUInt16() # version |
623 |
620 minimized = stream.readBool() |
624 self.__bigSize = QSize(*stateDict["big_size"]) |
621 |
625 self.__minSize = max(stateDict["min_size"], minSize) |
622 if minimized and not self.__minimized: |
626 self.__maxSize = max(stateDict["max_size"], maxSize) |
623 self.shrink() |
627 self.splitterSizes = stateDict["splitter_sizes"] |
624 |
628 |
625 stream >> self.__bigSize |
629 self.__autoHide = stateDict["auto_hide"] |
626 if version == 1: |
630 self.__autoHideButton.setChecked(not self.__autoHide) |
627 self.__minSize = max(stream.readUInt16(), minSize) |
631 |
628 self.__maxSize = max(stream.readUInt16(), maxSize) |
632 if not stateDict["minimized"]: |
629 count = stream.readUInt16() |
633 self.expand() |
630 self.splitterSizes = [] |
634 |
631 for _ in range(count): |
635 return True |
632 self.splitterSizes.append(stream.readUInt16()) |
636 |
633 elif version == 2: |
637 return False |
634 self.__minSize = max(stream.readUInt32(), minSize) |
|
635 self.__maxSize = max(stream.readUInt32(), maxSize) |
|
636 count = stream.readUInt32() |
|
637 self.splitterSizes = [] |
|
638 for _ in range(count): |
|
639 self.splitterSizes.append(stream.readUInt32()) |
|
640 else: |
|
641 # TODO: determine why version is always 0 |
|
642 # set some defaults for unknown versions |
|
643 self.__minSize = minSize |
|
644 self.__maxSize = maxSize |
|
645 self.splitterSizes = [] |
|
646 |
|
647 self.__autoHide = stream.readBool() |
|
648 self.__autoHideButton.setChecked(not self.__autoHide) |
|
649 |
|
650 if not minimized: |
|
651 self.expand() |
|
652 |
|
653 return True |
|
654 |
638 |
655 ####################################################################### |
639 ####################################################################### |
656 ## methods below implement the autohide functionality |
640 ## methods below implement the autohide functionality |
657 ####################################################################### |
641 ####################################################################### |
658 |
642 |