mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Clean up more delegates.
https://github.com/dcs-liberation/dcs_liberation/issues/276
This commit is contained in:
parent
9c2bad85d5
commit
e7b8548698
@ -3,111 +3,36 @@ from typing import Optional
|
|||||||
from PySide2.QtCore import (
|
from PySide2.QtCore import (
|
||||||
QItemSelectionModel,
|
QItemSelectionModel,
|
||||||
QModelIndex,
|
QModelIndex,
|
||||||
QSize,
|
|
||||||
Qt,
|
Qt,
|
||||||
)
|
)
|
||||||
from PySide2.QtGui import QFont, QFontMetrics, QIcon, QPainter
|
|
||||||
from PySide2.QtWidgets import (
|
from PySide2.QtWidgets import (
|
||||||
QAbstractItemView,
|
QAbstractItemView,
|
||||||
QDialog,
|
QDialog,
|
||||||
QListView,
|
QListView,
|
||||||
QStyle,
|
|
||||||
QStyleOptionViewItem,
|
|
||||||
QStyledItemDelegate,
|
|
||||||
QVBoxLayout,
|
QVBoxLayout,
|
||||||
)
|
)
|
||||||
|
|
||||||
from game.squadrons import Squadron
|
from game.squadrons import Squadron
|
||||||
from qt_ui.delegates import painter_context
|
from qt_ui.delegates import TwoColumnRowDelegate
|
||||||
from qt_ui.models import GameModel, AirWingModel, SquadronModel
|
from qt_ui.models import GameModel, AirWingModel, SquadronModel
|
||||||
from qt_ui.windows.SquadronDialog import SquadronDialog
|
from qt_ui.windows.SquadronDialog import SquadronDialog
|
||||||
|
|
||||||
|
|
||||||
class SquadronDelegate(QStyledItemDelegate):
|
class SquadronDelegate(TwoColumnRowDelegate):
|
||||||
FONT_SIZE = 10
|
|
||||||
HMARGIN = 4
|
|
||||||
VMARGIN = 4
|
|
||||||
|
|
||||||
def __init__(self, air_wing_model: AirWingModel) -> None:
|
def __init__(self, air_wing_model: AirWingModel) -> None:
|
||||||
super().__init__()
|
super().__init__(rows=2, columns=1, font_size=12)
|
||||||
self.air_wing_model = air_wing_model
|
self.air_wing_model = air_wing_model
|
||||||
|
|
||||||
def get_font(self, option: QStyleOptionViewItem) -> QFont:
|
|
||||||
font = QFont(option.font)
|
|
||||||
font.setPointSize(self.FONT_SIZE)
|
|
||||||
return font
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def squadron(index: QModelIndex) -> Squadron:
|
def squadron(index: QModelIndex) -> Squadron:
|
||||||
return index.data(AirWingModel.SquadronRole)
|
return index.data(AirWingModel.SquadronRole)
|
||||||
|
|
||||||
def first_row_text(self, index: QModelIndex) -> str:
|
def text_for(self, index: QModelIndex, row: int, column: int) -> str:
|
||||||
return self.air_wing_model.data(index, Qt.DisplayRole)
|
if row == 0:
|
||||||
|
return self.air_wing_model.data(index, Qt.DisplayRole)
|
||||||
def second_row_text(self, index: QModelIndex) -> str:
|
elif row == 1:
|
||||||
return self.squadron(index).nickname
|
return self.squadron(index).nickname
|
||||||
|
return ""
|
||||||
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))
|
|
||||||
|
|
||||||
@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 SquadronList(QListView):
|
class SquadronList(QListView):
|
||||||
|
|||||||
@ -1,13 +1,10 @@
|
|||||||
from typing import Optional
|
|
||||||
|
|
||||||
from PySide2.QtCore import (
|
from PySide2.QtCore import (
|
||||||
QItemSelection,
|
QItemSelection,
|
||||||
QItemSelectionModel,
|
QItemSelectionModel,
|
||||||
QModelIndex,
|
QModelIndex,
|
||||||
QSize,
|
|
||||||
Qt,
|
Qt,
|
||||||
)
|
)
|
||||||
from PySide2.QtGui import QContextMenuEvent, QFont, QFontMetrics, QIcon, QPainter
|
from PySide2.QtGui import QContextMenuEvent
|
||||||
from PySide2.QtWidgets import (
|
from PySide2.QtWidgets import (
|
||||||
QAbstractItemView,
|
QAbstractItemView,
|
||||||
QAction,
|
QAction,
|
||||||
@ -16,102 +13,29 @@ from PySide2.QtWidgets import (
|
|||||||
QListView,
|
QListView,
|
||||||
QMenu,
|
QMenu,
|
||||||
QPushButton,
|
QPushButton,
|
||||||
QStyle,
|
|
||||||
QStyleOptionViewItem,
|
|
||||||
QStyledItemDelegate,
|
|
||||||
QVBoxLayout,
|
QVBoxLayout,
|
||||||
)
|
)
|
||||||
|
|
||||||
from game.transfers import TransferOrder
|
from game.transfers import TransferOrder
|
||||||
from qt_ui.delegates import painter_context
|
from qt_ui.delegates import TwoColumnRowDelegate
|
||||||
from qt_ui.models import GameModel, TransferModel
|
from qt_ui.models import GameModel, TransferModel
|
||||||
|
|
||||||
|
|
||||||
class TransferDelegate(QStyledItemDelegate):
|
class TransferDelegate(TwoColumnRowDelegate):
|
||||||
FONT_SIZE = 10
|
|
||||||
HMARGIN = 4
|
|
||||||
VMARGIN = 4
|
|
||||||
|
|
||||||
def __init__(self, transfer_model: TransferModel) -> None:
|
def __init__(self, transfer_model: TransferModel) -> None:
|
||||||
super().__init__()
|
super().__init__(rows=2, columns=1, font_size=12)
|
||||||
self.transfer_model = transfer_model
|
self.transfer_model = transfer_model
|
||||||
|
|
||||||
def get_font(self, option: QStyleOptionViewItem) -> QFont:
|
|
||||||
font = QFont(option.font)
|
|
||||||
font.setPointSize(self.FONT_SIZE)
|
|
||||||
return font
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def transfer(index: QModelIndex) -> TransferOrder:
|
def transfer(index: QModelIndex) -> TransferOrder:
|
||||||
return index.data(TransferModel.TransferRole)
|
return index.data(TransferModel.TransferRole)
|
||||||
|
|
||||||
def first_row_text(self, index: QModelIndex) -> str:
|
def text_for(self, index: QModelIndex, row: int, column: int) -> str:
|
||||||
return self.transfer_model.data(index, Qt.DisplayRole)
|
if row == 0:
|
||||||
|
return self.transfer_model.data(index, Qt.DisplayRole)
|
||||||
def second_row_text(self, index: QModelIndex) -> str:
|
elif row == 1:
|
||||||
return self.transfer(index).description
|
return self.transfer(index).description
|
||||||
|
return ""
|
||||||
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))
|
|
||||||
|
|
||||||
@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 PendingTransfersList(QListView):
|
class PendingTransfersList(QListView):
|
||||||
@ -190,7 +114,7 @@ class PendingTransfersDialog(QDialog):
|
|||||||
def can_cancel(self, index: QModelIndex) -> bool:
|
def can_cancel(self, index: QModelIndex) -> bool:
|
||||||
if not index.isValid():
|
if not index.isValid():
|
||||||
return False
|
return False
|
||||||
return self.transfer_model.pilot_at_index(index).player
|
return self.transfer_model.transfer_at_index(index).player
|
||||||
|
|
||||||
def on_selection_changed(
|
def on_selection_changed(
|
||||||
self, selected: QItemSelection, _deselected: QItemSelection
|
self, selected: QItemSelection, _deselected: QItemSelection
|
||||||
|
|||||||
@ -1,112 +1,35 @@
|
|||||||
from typing import Optional
|
|
||||||
|
|
||||||
from PySide2.QtCore import (
|
from PySide2.QtCore import (
|
||||||
QItemSelectionModel,
|
QItemSelectionModel,
|
||||||
QModelIndex,
|
QModelIndex,
|
||||||
QSize,
|
|
||||||
Qt,
|
Qt,
|
||||||
)
|
)
|
||||||
from PySide2.QtGui import QFont, QFontMetrics, QIcon, QPainter
|
|
||||||
from PySide2.QtWidgets import (
|
from PySide2.QtWidgets import (
|
||||||
QAbstractItemView,
|
QAbstractItemView,
|
||||||
QDialog,
|
QDialog,
|
||||||
QListView,
|
QListView,
|
||||||
QStyle,
|
|
||||||
QStyleOptionViewItem,
|
|
||||||
QStyledItemDelegate,
|
|
||||||
QVBoxLayout,
|
QVBoxLayout,
|
||||||
)
|
)
|
||||||
|
|
||||||
from game.squadrons import Pilot
|
from game.squadrons import Pilot
|
||||||
from qt_ui.delegates import painter_context
|
from qt_ui.delegates import TwoColumnRowDelegate
|
||||||
from qt_ui.models import SquadronModel
|
from qt_ui.models import SquadronModel
|
||||||
|
|
||||||
|
|
||||||
class PilotDelegate(QStyledItemDelegate):
|
class PilotDelegate(TwoColumnRowDelegate):
|
||||||
FONT_SIZE = 10
|
|
||||||
HMARGIN = 4
|
|
||||||
VMARGIN = 4
|
|
||||||
|
|
||||||
def __init__(self, squadron_model: SquadronModel) -> None:
|
def __init__(self, squadron_model: SquadronModel) -> None:
|
||||||
super().__init__()
|
super().__init__(rows=2, columns=1, font_size=12)
|
||||||
self.squadron_model = squadron_model
|
self.squadron_model = squadron_model
|
||||||
|
|
||||||
def get_font(self, option: QStyleOptionViewItem) -> QFont:
|
|
||||||
font = QFont(option.font)
|
|
||||||
font.setPointSize(self.FONT_SIZE)
|
|
||||||
return font
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def pilot(index: QModelIndex) -> Pilot:
|
def pilot(index: QModelIndex) -> Pilot:
|
||||||
return index.data(SquadronModel.PilotRole)
|
return index.data(SquadronModel.PilotRole)
|
||||||
|
|
||||||
def first_row_text(self, index: QModelIndex) -> str:
|
def text_for(self, index: QModelIndex, row: int, column: int) -> str:
|
||||||
return self.squadron_model.data(index, Qt.DisplayRole)
|
if row == 0:
|
||||||
|
return self.squadron_model.data(index, Qt.DisplayRole)
|
||||||
def second_row_text(self, index: QModelIndex) -> str:
|
elif row == 1:
|
||||||
return "Alive" if self.pilot(index).alive else "Dead"
|
return "Alive" if self.pilot(index).alive else "Dead"
|
||||||
|
return ""
|
||||||
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))
|
|
||||||
|
|
||||||
@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 PilotList(QListView):
|
class PilotList(QListView):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user