def close_tab(self, tab: Tab) -> None: del self._metas[tab] if tab.loaded: tab.editable.close() if not self.widget(1): self.new_deck(DeckModel()) self.removeTab(self.indexOf(tab))
def _load(self) -> None: with self._loading_lock: try: self._editable = load_editable(self._serialized, self._undo_stack) except Exception: if Context.debug: import traceback traceback.print_exc() Context.notification_message.emit('Failed loading tab') self._editable = DeckView(DeckModel(), self._undo_stack) self._editable.tab = self self._loading = False self._loading_event.set() self.editable_loaded.emit(self._editable)
def load(cls, state: t.Any, undo_stack: QUndoStack) -> DeckView: deck_model = DeckModel.load(state['deck_model']) deck_view = DeckView( deck_model, maindeck_cube_view = CubeView.load( state['maindeck_view'], deck_model.maindeck, undo_stack = undo_stack, ), sideboard_cube_view = CubeView.load( state['sideboard_view'], deck_model.sideboard, undo_stack = undo_stack, ), undo_stack = undo_stack, ) deck_view._horizontal_splitter.restoreState(state['splitter']) return deck_view
def new_limited_deck(self, deck: LimitedDeck) -> None: key = f'pool-deck-{deck.id}' if self._check_for_key(key): return tab = self.add_editable( DeckView( DeckModel( list(map(PhysicalCard.from_cubeable, deck.deck.maindeck)), list(map(PhysicalCard.from_cubeable, deck.deck.sideboard)), ), undo_stack=Context.get_undo_stack(), ), TabMeta( deck.name, key=key, ), ) self.setCurrentWidget(tab) self._sort_opened_view(tab.editable)
def __init__(self): super().__init__() self._match: t.Optional[ScheduledMatch] = None self._participants_model = ListTableModel(ParticipantsSchema()) self._participants_view: ReadOnlyListTableView[ TournamentParticipant] = ReadOnlyListTableView() self.setFocusProxy(self._participants_view) self._participants_view.setModel(self._participants_model) self._participants_view.item_selected.connect( lambda p: Context.editor.open_limited_deck(p.deck.id)) self._name_label = QtWidgets.QLabel() self._name_label.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed) self._round_label = QtWidgets.QLabel() self._round_label.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed) self._deck_cache: t.MutableMapping[int, t.Optional[LimitedDeck]] = {} self._deck_preview_model = DeckModel(mode=CubeEditMode.CLOSED) self._deck_preview = DeckView( self._deck_preview_model, Context.get_undo_stack(), ) self._no_preview_view = QtWidgets.QLabel('No preview selected') self._no_preview_view.setAlignment(Qt.AlignCenter) self._preview_unavailable_view = QtWidgets.QLabel( 'Preview unavailable') self._preview_unavailable_view.setAlignment(Qt.AlignCenter) self._preview_stack = QtWidgets.QStackedWidget() self._preview_stack.addWidget(self._deck_preview) self._preview_stack.addWidget(self._preview_unavailable_view) self._preview_stack.addWidget(self._no_preview_view) self._preview_stack.setCurrentWidget(self._no_preview_view) self.preview_fetched.connect(self._on_retrieved_deck) self._participants_view.current_item_changed.connect( self._on_participant_selected) self._open_button = QtWidgets.QPushButton('Open') self._open_button.clicked.connect(self._on_open_clicked) self._open_all_button = QtWidgets.QPushButton('Open All') self._open_all_button.clicked.connect(self._on_open_all_clicked) layout = QtWidgets.QVBoxLayout(self) layout.setContentsMargins(0, 0, 0, 0) top_bar = QtWidgets.QHBoxLayout() top_bar.setContentsMargins(5, 0, 5, 0) top_bar.addWidget(self._name_label) top_bar.addStretch() top_bar.addWidget(self._round_label) splitter = QtWidgets.QSplitter(Qt.Horizontal) participants_view = QtWidgets.QWidget() participants_layout = QtWidgets.QVBoxLayout(participants_view) participants_layout.setContentsMargins(0, 0, 0, 0) participants_control_bar_layout = QtWidgets.QHBoxLayout() participants_control_bar_layout.setContentsMargins(0, 0, 0, 0) participants_control_bar_layout.addWidget(self._open_button) participants_control_bar_layout.addWidget(self._open_all_button) participants_layout.addWidget(self._participants_view) participants_layout.addLayout(participants_control_bar_layout) splitter.addWidget(participants_view) splitter.addWidget(self._preview_stack) layout.addLayout(top_bar) layout.addWidget(splitter)
def open_file(self, path: str, target: t.Type[TabModel] = Deck) -> None: for editable, meta in self._metas.items(): if meta.path == path: self.setCurrentWidget(editable) return file_name = os.path.split(path)[1] name, extension = os.path.splitext(file_name) extension = extension[1:] meta = TabMeta(file_name, path) editable = None if extension.lower() == 'embd': with open(path, 'rb') as f: try: tab = self.load_file(pickle.load(f), meta) except UnpicklingError: raise FileOpenException('corrupt file') elif extension.lower() == 'embp': with open(path, 'rb') as f: try: tab = self.load_file(pickle.load(f), meta) except UnpicklingError: raise FileOpenException('corrupt file') else: with open(path, 'r') as f: try: serializer: TabModelSerializer[t.Union[ Deck, Pool]] = TabModelSerializer.extension_to_serializer[( extension, target)] except KeyError: raise FileOpenException( 'unsupported file type "{}"'.format(extension)) try: tab_model = serializer.deserialize(f.read()) except SerializationException: raise FileOpenException() if target == Deck: editable = DeckView( DeckModel( list( map(PhysicalCard.from_cubeable, tab_model.maindeck)), list( map(PhysicalCard.from_cubeable, tab_model.sideboard)), ), Context.get_undo_stack(), ) elif target == Pool: editable = PoolView( PoolModel(list(map(PhysicalCard.from_cubeable, tab_model)), ), Context.get_undo_stack(), ) else: raise FileOpenException( 'invalid load target "{}"'.format(target)) tab = self.add_editable(editable, meta) self.setCurrentWidget(tab) if editable is not None: self._sort_opened_view(editable) if not Context.main_window.isActiveWindow() and Context.settings.value( 'focus_on_open_file', True, bool): Context.main_window.raise_() Context.main_window.show() Context.main_window.activateWindow()
def _new_deck(self) -> None: self._main_view.editables_tabs.setCurrentWidget( self._main_view.editables_tabs.new_deck( DeckModel() ) )