dcs_liberation/qt_ui/windows/newgame/QCampaignList.py
Dan Albert 306971230b Update to PySide6.
It sounds like PySide2 will not be moving to Python 3.11, so we're stuck
on 3.10 without this. Upgrading to a newer Qt also fixes some high DPI
bugs (the file browser dialog for save/load is no longer tiny on 4k).

https://github.com/pyinstaller/pyinstaller/issues/5414 previously
blocked this, but the bug appears to be fixed now.
2022-12-29 16:26:50 -08:00

60 lines
2.2 KiB
Python

from __future__ import annotations
from typing import Optional
from PySide6 import QtGui
from PySide6.QtCore import QItemSelectionModel, QModelIndex, Qt
from PySide6.QtGui import QPixmap, QStandardItem, QStandardItemModel
from PySide6.QtWidgets import QAbstractItemView, QListView
from game.campaignloader.campaign import Campaign
from qt_ui.liberation_install import get_dcs_install_directory
class QCampaignItem(QStandardItem):
def __init__(self, campaign: Campaign) -> None:
super(QCampaignItem, self).__init__()
self.setData(campaign, QCampaignList.CampaignRole)
dcs_path = get_dcs_install_directory()
icon_path = dcs_path / campaign.menu_thumbnail_dcs_relative_path
self.setIcon(QtGui.QIcon(QPixmap(str(icon_path))))
self.setEditable(False)
if campaign.is_compatible:
name = campaign.name
else:
name = f"[INCOMPATIBLE] {campaign.name}"
self.setText(name)
class QCampaignList(QListView):
CampaignRole = Qt.UserRole
def __init__(self, campaigns: list[Campaign], show_incompatible: bool) -> None:
super(QCampaignList, self).__init__()
self.campaign_model = QStandardItemModel(self)
self.setModel(self.campaign_model)
self.setMinimumWidth(250)
self.setMinimumHeight(350)
self.campaigns = campaigns
self.setSelectionBehavior(QAbstractItemView.SelectItems)
self.setup_content(show_incompatible)
@property
def selected_campaign(self) -> Optional[Campaign]:
return self.currentIndex().data(QCampaignList.CampaignRole)
def setup_content(self, show_incompatible: bool) -> None:
self.selectionModel().blockSignals(True)
try:
self.campaign_model.clear()
for campaign in self.campaigns:
if show_incompatible or campaign.is_compatible:
item = QCampaignItem(campaign)
self.campaign_model.appendRow(item)
finally:
self.selectionModel().blockSignals(False)
self.selectionModel().setCurrentIndex(
self.campaign_model.index(0, 0, QModelIndex()), QItemSelectionModel.Select
)