mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Show player slots in the overview.
This commit is contained in:
@@ -10,7 +10,7 @@ from PySide2.QtCore import (
|
||||
QSize,
|
||||
Qt,
|
||||
)
|
||||
from PySide2.QtGui import QFont, QFontMetrics, QPainter
|
||||
from PySide2.QtGui import QFont, QFontMetrics, QIcon, QPainter
|
||||
from PySide2.QtWidgets import (
|
||||
QAbstractItemView,
|
||||
QGroupBox,
|
||||
@@ -18,15 +18,109 @@ from PySide2.QtWidgets import (
|
||||
QListView,
|
||||
QPushButton,
|
||||
QSplitter,
|
||||
QStyleOptionViewItem, QStyledItemDelegate, QVBoxLayout,
|
||||
QStyle, QStyleOptionViewItem, QStyledItemDelegate, QVBoxLayout,
|
||||
)
|
||||
|
||||
from game import db
|
||||
from gen.ato import Package
|
||||
from gen.flights.flight import Flight
|
||||
from qt_ui.windows.GameUpdateSignal import GameUpdateSignal
|
||||
from ..models import AtoModel, GameModel, NullListModel, PackageModel
|
||||
|
||||
|
||||
class FlightDelegate(QStyledItemDelegate):
|
||||
FONT_SIZE = 10
|
||||
HMARGIN = 4
|
||||
VMARGIN = 4
|
||||
|
||||
def get_font(self, option: QStyleOptionViewItem) -> QFont:
|
||||
font = QFont(option.font)
|
||||
font.setPointSize(self.FONT_SIZE)
|
||||
return font
|
||||
|
||||
@staticmethod
|
||||
def flight(index: QModelIndex) -> Flight:
|
||||
return index.data(PackageModel.FlightRole)
|
||||
|
||||
def first_row_text(self, index: QModelIndex) -> str:
|
||||
flight = self.flight(index)
|
||||
task = flight.flight_type.name
|
||||
count = flight.count
|
||||
name = db.unit_type_name(flight.unit_type)
|
||||
delay = flight.scheduled_in
|
||||
return f"[{task}] {count} x {name} in {delay} minutes"
|
||||
|
||||
def second_row_text(self, index: QModelIndex) -> str:
|
||||
flight = self.flight(index)
|
||||
origin = flight.from_cp.name
|
||||
return f"From {origin}"
|
||||
|
||||
def paint(self, painter: QPainter, option: QStyleOptionViewItem,
|
||||
index: QModelIndex) -> None:
|
||||
# Draw the list item with all the default selection styling, but with an
|
||||
# invalid index so text formatting is left to us.
|
||||
super().paint(painter, option, QModelIndex())
|
||||
|
||||
rect = option.rect.adjusted(self.HMARGIN, self.VMARGIN, -self.HMARGIN,
|
||||
-self.VMARGIN)
|
||||
|
||||
with painter_context(painter):
|
||||
painter.setFont(self.get_font(option))
|
||||
|
||||
icon: Optional[QIcon] = index.data(Qt.DecorationRole)
|
||||
if icon is not None:
|
||||
icon.paint(painter, rect, Qt.AlignLeft | Qt.AlignVCenter,
|
||||
self.icon_mode(option),
|
||||
self.icon_state(option))
|
||||
|
||||
rect = rect.adjusted(self.icon_size(option).width() + self.HMARGIN,
|
||||
0, 0, 0)
|
||||
painter.drawText(rect, Qt.AlignLeft, self.first_row_text(index))
|
||||
line2 = rect.adjusted(0, rect.height() / 2, 0, rect.height() / 2)
|
||||
painter.drawText(line2, Qt.AlignLeft, self.second_row_text(index))
|
||||
|
||||
clients = self.num_clients(index)
|
||||
if clients:
|
||||
painter.drawText(rect, Qt.AlignRight,
|
||||
f"Player Slots: {clients}")
|
||||
|
||||
def num_clients(self, index: QModelIndex) -> int:
|
||||
flight = self.flight(index)
|
||||
return flight.client_count
|
||||
|
||||
@staticmethod
|
||||
def icon_mode(option: QStyleOptionViewItem) -> QIcon.Mode:
|
||||
if not (option.state & QStyle.State_Enabled):
|
||||
return QIcon.Disabled
|
||||
elif option.state & QStyle.State_Selected:
|
||||
return QIcon.Selected
|
||||
elif option.state & QStyle.State_Active:
|
||||
return QIcon.Active
|
||||
return QIcon.Normal
|
||||
|
||||
@staticmethod
|
||||
def icon_state(option: QStyleOptionViewItem) -> QIcon.State:
|
||||
return QIcon.On if option.state & QStyle.State_Open else QIcon.Off
|
||||
|
||||
@staticmethod
|
||||
def icon_size(option: QStyleOptionViewItem) -> QSize:
|
||||
icon_size: Optional[QSize] = option.decorationSize
|
||||
if icon_size is None:
|
||||
return QSize(0, 0)
|
||||
else:
|
||||
return icon_size
|
||||
|
||||
def sizeHint(self, option: QStyleOptionViewItem,
|
||||
index: QModelIndex) -> QSize:
|
||||
left = self.icon_size(option).width() + self.HMARGIN
|
||||
metrics = QFontMetrics(self.get_font(option))
|
||||
first = metrics.size(0, self.first_row_text(index))
|
||||
second = metrics.size(0, self.second_row_text(index))
|
||||
text_width = max(first.width(), second.width())
|
||||
return QSize(left + text_width + 2 * self.HMARGIN,
|
||||
first.height() + second.height() + 2 * self.VMARGIN)
|
||||
|
||||
|
||||
class QFlightList(QListView):
|
||||
"""List view for displaying the flights of a package."""
|
||||
|
||||
@@ -34,6 +128,7 @@ class QFlightList(QListView):
|
||||
super().__init__()
|
||||
self.package_model = model
|
||||
self.set_package(model)
|
||||
self.setItemDelegate(FlightDelegate())
|
||||
self.setIconSize(QSize(91, 24))
|
||||
self.setSelectionBehavior(QAbstractItemView.SelectItems)
|
||||
|
||||
@@ -206,6 +301,15 @@ class PackageDelegate(QStyledItemDelegate):
|
||||
line2 = rect.adjusted(0, rect.height() / 2, 0, rect.height() / 2)
|
||||
painter.drawText(line2, Qt.AlignLeft, self.right_text(index))
|
||||
|
||||
clients = self.num_clients(index)
|
||||
if clients:
|
||||
painter.drawText(rect, Qt.AlignRight,
|
||||
f"Player Slots: {clients}")
|
||||
|
||||
def num_clients(self, index: QModelIndex) -> int:
|
||||
package = self.package(index)
|
||||
return sum(f.client_count for f in package.flights)
|
||||
|
||||
def sizeHint(self, option: QStyleOptionViewItem,
|
||||
index: QModelIndex) -> QSize:
|
||||
metrics = QFontMetrics(self.get_font(option))
|
||||
|
||||
Reference in New Issue
Block a user