Skip to content

Commit

Permalink
- more types
Browse files Browse the repository at this point in the history
- fixes type confusion of color system in recent roasts (is it a str or int?). Thanks Frans for the report!
  • Loading branch information
MAKOMO committed Oct 20, 2023
1 parent 4a4840d commit 8376cc4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/artisanlib/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -14179,7 +14179,7 @@ def formatLoadLabel(i):
emin = toInt(self.loadevent_zeropcts[i])
emax = toInt(self.loadevent_hundpcts[i])
scaled = (val - emin) / (emax - emin) #emax > emin enforced by energy.py
load_pct = min(1,max(0,scaled)) * 100
load_pct = min(1,max(0, scaled)) * 100
if self.presssure_percents[i] and self.sourcetypes[i] in {0, 1}: # gas loads only
# convert pressure to heat
factor = math.sqrt(load_pct / 100)
Expand Down
9 changes: 7 additions & 2 deletions src/artisanlib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4811,7 +4811,7 @@ def createRecentRoast(
return d

# recentRoast activated via NEW
def setRecentRoast(self,rr):
def setRecentRoast(self, rr:'RecentRoast') -> None:
if 'title' in rr and rr['title'] is not None:
self.qmc.title = rr['title']
if not self.qmc.flagstart or self.qmc.title_show_always:
Expand Down Expand Up @@ -4855,7 +4855,12 @@ def setRecentRoast(self,rr):
else:
self.qmc.ground_color = 0
if 'colorSystem' in rr and rr['colorSystem'] is not None:
self.qmc.color_system_idx = rr['colorSystem']
if rr['colorSystem'] in self.qmc.color_systems:
self.qmc.color_system_idx = self.qmc.color_systems.index(rr['colorSystem'])
elif isinstance(rr['colorSystem'], int) and rr['colorSystem'] < len(self.qmc.color_systems): # type: ignore
# to stay compatible with older versions were rr['colorSystem'] was an index instead of the name of a system
self.qmc.color_system_idx = rr['colorSystem'] # type: ignore[unreachable]

# Note: the background profile will not be changed if recent roast is activated from Roast Properties
#PLUS
if self.plus_account is not None and 'plus_account' in rr and self.plus_account == rr['plus_account']:
Expand Down
46 changes: 28 additions & 18 deletions src/artisanlib/roast_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

if TYPE_CHECKING:
from artisanlib.main import ApplicationWindow # noqa: F401 # pylint: disable=unused-import
from artisanlib.types import RecentRoast, BTU
from artisanlib.ble import BleInterface # noqa: F401 # pylint: disable=unused-import
from PyQt6.QtWidgets import QLayout, QAbstractItemView, QCompleter # pylint: disable=unused-import
from PyQt6.QtGui import QClipboard # pylint: disable=unused-import
from PyQt6.QtCore import QObject # pylint: disable=unused-import


# import artisan.plus modules
import plus.config # @UnusedImport
import plus.util
Expand Down Expand Up @@ -1018,7 +1018,10 @@ def __init__(self, parent:QWidget, aw:'ApplicationWindow', activeTab:int = 0) ->
self.bean_size_max_edit.setAlignment(Qt.AlignmentFlag.AlignRight)
self.colorSystemComboBox = QComboBox()
self.colorSystemComboBox.addItems(self.aw.qmc.color_systems)
self.colorSystemComboBox.setCurrentIndex(self.aw.qmc.color_system_idx)
if isinstance(self.aw.qmc.color_system_idx, int):
self.colorSystemComboBox.setCurrentIndex(self.aw.qmc.color_system_idx)
else: # in older versions this could have been a string
self.aw.qmc.color_system_idx = 0 # type: ignore[unreachable]
#Greens Temp
greens_temp_label = QLabel('<b>' + QApplication.translate('Label', 'Beans') + '</b>')
greens_temp_unit_label = QLabel(self.aw.qmc.mode)
Expand Down Expand Up @@ -2242,7 +2245,7 @@ def getBlendDictCurrentWeight(self,blend):
return plus.stock.getBlendBlendDict(blend,v)

@pyqtSlot(int)
def blendSelectionChanged(self,n):
def blendSelectionChanged(self, n:int) -> None:
# check for previously selected blend label
prev_coffee_label = self.plus_coffee_selected_label
prev_blend_label = self.plus_blend_selected_label
Expand Down Expand Up @@ -2290,10 +2293,10 @@ def blendSelectionChanged(self,n):
self.updatePlusSelectedLine()

# recentRoast activated from within RoastProperties dialog
def recentRoastActivated(self,n):
def recentRoastActivated(self, n:int) -> None:
# note, the first item is the edited text!
if 0 < n <= len(self.aw.recentRoasts):
rr = self.aw.recentRoasts[n-1]
rr:'RecentRoast' = self.aw.recentRoasts[n-1]
if 'title' in rr and rr['title'] is not None:
self.titleedit.textEdited(rr['title'])
self.titleedit.setEditText(rr['title'])
Expand Down Expand Up @@ -2337,7 +2340,14 @@ def recentRoastActivated(self,n):
else:
self.ground_color_edit.setText('0')
if 'colorSystem' in rr and rr['colorSystem'] is not None:
self.colorSystemComboBox.setCurrentIndex(rr['colorSystem'])
if rr['colorSystem'] in self.aw.qmc.color_systems:
self.aw.qmc.color_system_idx = self.aw.qmc.color_systems.index(rr['colorSystem'])
self.colorSystemComboBox.setCurrentIndex(self.aw.qmc.color_system_idx)
elif isinstance(rr['colorSystem'], int) and rr['colorSystem'] < len(self.aw.qmc.color_systems): # type: ignore
# to stay compatible with older versions were rr['colorSystem'] was an index instead of the name of a system
self.aw.qmc.color_system_idx = rr['colorSystem'] # type: ignore[unreachable]
self.colorSystemComboBox.setCurrentIndex(self.aw.qmc.color_system_idx) # type: ignore[unreachable]

# items added in v1.4 might not be in the data set of previous stored recent roasts
if 'beanSize_min' in rr and rr['beanSize_min'] is not None:
self.bean_size_min_edit.setText(str(int(rr['beanSize_min'])))
Expand Down Expand Up @@ -2424,7 +2434,7 @@ def recentRoastEnabled(self,_:str='') -> None:
self.delRecentButton.setEnabled(False)

@pyqtSlot(bool)
def delRecentRoast(self,_):
def delRecentRoast(self, _:bool) -> None:
try:
title = ' '.join(self.titleedit.currentText().split())
weightIn = float(comma2dot(self.weightinedit.text()))
Expand All @@ -2434,7 +2444,7 @@ def delRecentRoast(self,_):
_log.exception(e)

@pyqtSlot(bool)
def addRecentRoast(self,_):
def addRecentRoast(self, __:bool) -> None:
try:
title = ' '.join(self.titleedit.currentText().split())
weightIn = float(comma2dot(str(self.weightinedit.text())))
Expand Down Expand Up @@ -2538,7 +2548,7 @@ def closeEvent(self, _):

# triggered via the cancel button
@pyqtSlot()
def cancel_dialog(self):
def cancel_dialog(self) -> None:
self.disconnecting = True
if self.ble is not None:
try:
Expand Down Expand Up @@ -2588,11 +2598,11 @@ def cancel_dialog(self):

# calcs volume (in ml) from density (in g/l) and weight (in g)
@staticmethod
def calc_volume(density, weight):
def calc_volume(density:float, weight:float) -> float:
return (1./density) * weight * 1000

#keyboard presses. There must not be widgets (pushbuttons, comboboxes, etc) in focus in order to work
def keyPressEvent(self,event):
def keyPressEvent(self, event):
key = int(event.key())
#print(key)
modifiers = event.modifiers()
Expand All @@ -2615,7 +2625,7 @@ def keyPressEvent(self,event):
self.resetScaleSet()

@pyqtSlot(int)
def tareChanged(self,i):
def tareChanged(self, i:int) -> None:
if i == 0 and self.tarePopupEnabled:
tareDLG = tareDlg(self,self.aw,self)
tareDLG.show()
Expand All @@ -2625,7 +2635,7 @@ def tareChanged(self,i):
self.update_scale_weight()

@pyqtSlot(int)
def changeWeightUnit(self,i):
def changeWeightUnit(self, i:int) -> None:
o = self.aw.qmc.weight_units.index(self.aw.qmc.weight[2]) # previous unit index
self.aw.qmc.weight = (self.aw.qmc.weight[0],self.aw.qmc.weight[1],self.unitsComboBox.currentText())
for le in [self.weightinedit,self.weightoutedit]:
Expand All @@ -2652,7 +2662,7 @@ def changeWeightUnit(self,i):


@pyqtSlot(int)
def changeVolumeUnit(self,i):
def changeVolumeUnit(self, i:int) -> None:
o = self.aw.qmc.volume_units.index(self.aw.qmc.volume[2]) # previous unit index
self.aw.qmc.volume = (self.aw.qmc.volume[0],self.aw.qmc.volume[1],self.volumeUnitsComboBox.currentText())
for le in [self.volumeinedit,self.volumeoutedit]:
Expand All @@ -2664,7 +2674,7 @@ def changeVolumeUnit(self,i):
# self.calculated_density() # if just the unit changes, the density will not change as it is fixed now

@pyqtSlot(int)
def tabSwitched(self,i):
def tabSwitched(self, i:int) -> None:
if i == 0: # Roast (always initialized in __init__())
self.saveEventTable()
elif i == 1: # Notes (always initialized in __init__())
Expand All @@ -2685,14 +2695,14 @@ def tabSwitched(self,i):
###### ENERGY TAB #####


def initEnergyTab(self):
def initEnergyTab(self) -> None:
# pylint: disable=attribute-defined-outside-init
if not self.tabInitialized[4]:
# fill Energy tab
self.energy_ui = EnergyWidget.Ui_EnergyWidget()
self.energy_ui.setupUi(self.C5Widget)

self.btu_list = []
self.btu_list:List['BTU'] = []

# remember parameters to enable a Cancel action
self.org_loadlabels = self.aw.qmc.loadlabels.copy()
Expand Down Expand Up @@ -2921,7 +2931,7 @@ def initEnergyTab(self):
# we always set the batch position on tab switch as it might have been changed in the first tab of the Roast Properties dialog
self.energy_ui.roastbatchposLabel.setText(f"{QApplication.translate('Label','Batch')} #{self.aw.qmc.roastbatchpos}")

def createEnergyDataTable(self):
def createEnergyDataTable(self) -> None:
self.updateEnergyConfig()
ndata = len(self.btu_list)
self.energy_ui.datatable.setSortingEnabled(False) # deactivate sorting while populating not to mess up things
Expand Down
2 changes: 1 addition & 1 deletion src/artisanlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __lt__(self, other):

class MyTableWidgetItemNumber(QTableWidgetItem): # pylint: disable= too-few-public-methods # pyright: ignore [reportGeneralTypeIssues] # Argument to class must be a base class
__slots__ = ['sortKey'] # save some memory by using slots
def __init__(self, text:str, sortKey:int) -> None:
def __init__(self, text:str, sortKey:float) -> None:
super().__init__(text, 1003) #QTableWidgetItem.ItemType.UserType)
self.sortKey = sortKey

Expand Down

0 comments on commit 8376cc4

Please sign in to comment.