Skip to content

Commit

Permalink
gui: some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
YoRyan committed Oct 13, 2019
1 parent 19f6466 commit e9ff738
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 116 deletions.
9 changes: 4 additions & 5 deletions nuxhash/gui/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import wx
from wx.lib.agw.hyperlink import HyperLinkCtrl

from nuxhash.version import __version__
from nuxhash.version import __copyright__, __version__


WEBSITE = 'https://github.com/YoRyan/nuxhash'
Expand All @@ -30,8 +30,8 @@ def __init__(self, parent, *args, **kwargs):

v_sizer.AddSpacer(15)

appName = wx.StaticText(self, label=f'nuxhash {__version__}',
style=wx.ALIGN_CENTER)
appName = wx.StaticText(
self, label=f'nuxhash {__version__}', style=wx.ALIGN_CENTER)
appName.SetFont(self.GetFont().Scale(2.0))
v_sizer.Add(appName, wx.SizerFlags().Expand())

Expand All @@ -43,8 +43,7 @@ def __init__(self, parent, *args, **kwargs):

v_sizer.AddSpacer(15)

copyright = wx.StaticText(self, label='Copyright © 2018\nRyan Young',
style=wx.ALIGN_CENTER)
copyright = wx.StaticText(self, label=__copyright__, style=wx.ALIGN_CENTER)
copyright.SetFont(self.GetFont().Scale(0.8))
v_sizer.Add(copyright, wx.SizerFlags().Expand())

Expand Down
15 changes: 8 additions & 7 deletions nuxhash/gui/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ def run(self):
for target in self._targets:
def report(sample, secs_remaining):
main.sendMessage(
self._window, 'benchmarking.status',
target=target, speeds=sample, time=abs(secs_remaining),
warmup=(secs_remaining < 0))
self._window, 'benchmarking.status',
target=target, speeds=sample, time=abs(secs_remaining),
warmup=(secs_remaining < 0))
device, algorithm = target
speeds = utils.run_benchmark(
algorithm, device, algorithm.warmup_secs, BENCHMARK_SECS,
Expand Down Expand Up @@ -306,9 +306,9 @@ def deselect(self):
class SpeedCtrl(wx.TextCtrl):

def __init__(self, parent, *args, **kwargs):
wx.StaticText.__init__(self, parent, *args,
style=wx.BORDER_NONE|wx.TE_CENTRE,
size=(-1, 20), **kwargs)
wx.StaticText.__init__(
self, parent, *args, style=wx.BORDER_NONE|wx.TE_CENTRE,
size=(-1, 20), **kwargs)
self._StatusPos = 0
self.Bind(wx.EVT_KILL_FOCUS, self._OnUnfocus)

Expand All @@ -317,7 +317,8 @@ def SetValues(self, values):
if all(speed == 0.0 for speed in values):
s = '--'
else:
s = '; '.join(utils.format_speed(speed).strip() for speed in values)
speeds = (utils.format_speed(speed).strip() for speed in values)
s = '; '.join(speeds)
self.ChangeValue(s)

def SetWarmup(self, remaining):
Expand Down
37 changes: 18 additions & 19 deletions nuxhash/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ def __init__(self, parent, *args, **kwargs):
# Create notebook and its pages.
notebook = wx.Notebook(self)
notebook.AddPage(
MiningScreen(notebook, devices=self._Devices),
text='Mining')
MiningScreen(notebook, devices=self._Devices),
text='Mining')
notebook.AddPage(
BenchmarksScreen(notebook, devices=self._Devices),
text='Benchmarks')
BenchmarksScreen(notebook, devices=self._Devices),
text='Benchmarks')
notebook.AddPage(
SettingsScreen(notebook),
text='Settings')
SettingsScreen(notebook),
text='Settings')
notebook.AddPage(
AboutScreen(notebook),
text='About')
AboutScreen(notebook),
text='About')

# Check miner downloads.
pub.subscribe(self._OnDownloadProgress, 'download.progress')
Expand All @@ -65,9 +65,8 @@ def __init__(self, parent, *args, **kwargs):
self._FirstRun()
pub.sendMessage('data.settings', settings=loaded_settings)

pub.sendMessage(
'data.benchmarks',
benchmarks=nuxhash.settings.load_benchmarks(CONFIG_DIR, self._Devices))
benchmarks = nuxhash.settings.load_benchmarks(CONFIG_DIR, self._Devices)
pub.sendMessage('data.benchmarks', benchmarks=benchmarks)

def _DownloadMiners(self):
to_download = [item for item in make_miners(CONFIG_DIR)
Expand All @@ -81,10 +80,10 @@ def _DownloadMiners(self):

def _FirstRun(self):
dialog = wx.MessageDialog(
self,
'Welcome to nuxhash!\n\nSet your NiceHash wallet address and run '
'some benchmarks, and then you can start mining.',
style=wx.OK)
self,
'Welcome to nuxhash!\n\nSet your NiceHash wallet address and run '
'some benchmarks, and then you can start mining.',
style=wx.OK)
dialog.ShowModal()

def _OnDownloadProgress(self, progress, message):
Expand Down Expand Up @@ -127,12 +126,12 @@ def run(self):
n_downloads = len(self._downloads)
for i, item in enumerate(self._downloads):
sendMessage(
self._frame, 'download.progress',
progress=i/n_downloads, message=f'Downloading {item.name}')
self._frame, 'download.progress',
progress=i/n_downloads, message=f'Downloading {item.name}')
item.download()
sendMessage(
self._frame, 'download.progress',
progress=(i+1)/n_downloads, message='')
self._frame, 'download.progress',
progress=(i+1)/n_downloads, message='')


def sendMessage(window, topic, **data):
Expand Down
63 changes: 28 additions & 35 deletions nuxhash/gui/mining.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, parent, *args, devices=[], **kwargs):

# Update balance periodically.
self._Timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, lambda event: self._UpdateBalance(), self._Timer)
self.Bind(wx.EVT_TIMER, self._OnBalanceTimer, self._Timer)
self._Timer.Start(milliseconds=BALANCE_UPDATE_MIN*60*1e3)

# Add mining panel.
Expand Down Expand Up @@ -130,6 +130,9 @@ def _OnClose(self):
if self._Thread:
self._Thread.stop()

def _OnBalanceTimer(self, event):
self._UpdateBalance()

def _UpdateBalance(self):
address = self._Settings['nicehash']['wallet']
if check_bc(address):
Expand All @@ -150,10 +153,10 @@ def OnStartStop(self, event):
def _StartMining(self):
pub.sendMessage('mining.start')
self._StartStop.SetLabel('Stop Mining')
self._Thread = MiningThread(devices=self._Devices,
window=self,
settings=deepcopy(self._Settings),
benchmarks=deepcopy(self._Benchmarks))
self._Thread = MiningThread(
devices=self._Devices, window=self,
settings=deepcopy(self._Settings),
benchmarks=deepcopy(self._Benchmarks))
self._Thread.start()

def _StopMining(self):
Expand Down Expand Up @@ -184,10 +187,10 @@ def __init__(self, parent, *args, **kwargs):
self.Disable()
self.AppendTextColumn('Algorithm', width=wx.COL_WIDTH_AUTOSIZE)
self.AppendColumn(
wx.dataview.DataViewColumn('Devices', DeviceListRenderer(),
1, align=wx.ALIGN_LEFT,
width=wx.COL_WIDTH_AUTOSIZE),
'string')
wx.dataview.DataViewColumn('Devices', DeviceListRenderer(),
1, align=wx.ALIGN_LEFT,
width=wx.COL_WIDTH_AUTOSIZE),
'string')
self.AppendTextColumn('Speed', width=wx.COL_WIDTH_AUTOSIZE)
self.AppendTextColumn('Revenue')

Expand Down Expand Up @@ -233,32 +236,28 @@ def __init__(self, *args, **kwargs):
self._ColorDb = wx.ColourDatabase()

def SetValue(self, value):
vendors = {
'N': 'nvidia'
}
vendors = { 'N': 'nvidia' }
self._Devices = [{ 'name': s[2:], 'vendor': vendors[s[0]] }
for s in value.split(',')]
return True

def GetValue(self):
tags = {
'nvidia': 'N'
}
tags = { 'nvidia': 'N' }
return ','.join([f"{tags[device['vendor']]}:{device['name']}"
for device in self._Devices])

def GetSize(self):
boxes = [self.GetTextExtent(device['name']) for device in self._Devices]
return wx.Size((max(box.GetWidth() for box in boxes)
+ DeviceListRenderer.CORNER_RADIUS*2),
RADIUS = DeviceListRenderer.CORNER_RADIUS
return wx.Size(max(box.GetWidth() for box in boxes) + RADIUS*2,
(sum(box.GetHeight() for box in boxes)
+ DeviceListRenderer.CORNER_RADIUS*2*len(boxes)
+ DeviceListRenderer.CORNER_RADIUS*(len(boxes) - 1)))
+ RADIUS*2*len(boxes) + RADIUS*(len(boxes) - 1)))

def Render(self, cell, dc, state):
position = cell.GetPosition()
for device in self._Devices:
box = self.GetTextExtent(device['name'])
RADIUS = DeviceListRenderer.CORNER_RADIUS

if device['vendor'] == 'nvidia':
color = self._ColorDb.Find('LIME GREEN')
Expand All @@ -267,21 +266,16 @@ def Render(self, cell, dc, state):
dc.SetBrush(wx.Brush(color))
dc.SetPen(wx.TRANSPARENT_PEN)
shadeRect = wx.Rect(
position,
wx.Size(box.GetWidth() + DeviceListRenderer.CORNER_RADIUS*2,
box.GetHeight() + DeviceListRenderer.CORNER_RADIUS*2))
dc.DrawRoundedRectangle(shadeRect, DeviceListRenderer.CORNER_RADIUS)
position,
wx.Size(box.GetWidth() + RADIUS*2, box.GetHeight() + RADIUS*2))
dc.DrawRoundedRectangle(shadeRect, RADIUS)

textRect = wx.Rect(
wx.Point(position.x + DeviceListRenderer.CORNER_RADIUS,
position.y + DeviceListRenderer.CORNER_RADIUS),
box)
wx.Point(position.x + RADIUS, position.y + RADIUS), box)
self.RenderText(device['name'], 0, textRect, dc, state)

position = wx.Point(position.x,
(position.y + box.GetHeight()
+ DeviceListRenderer.CORNER_RADIUS*2
+ DeviceListRenderer.CORNER_RADIUS))
position = wx.Point(
position.x, (position.y + box.GetHeight() + RADIUS*2 + RADIUS))
return True

def _DeviceToString(device):
Expand Down Expand Up @@ -402,11 +396,10 @@ def _read_status(self):
running_algorithms = self._assignments.values()
speeds = {algorithm: algorithm.current_speeds()
for algorithm in running_algorithms}
revenue = {algorithm: sum([payrates[multialgorithm]
*speeds[algorithm][i]
for i, multialgorithm
in enumerate(algorithm.algorithms)])
for algorithm in running_algorithms}
revenue = {algorithm: sum([payrates[sub_algo]*speeds[algorithm][i]
for i, sub_algo
in enumerate(algorithm.algorithms)])
for algorithm in running_algorithms}
devices = {algorithm: [device for device, this_algorithm
in self._assignments.items()
if this_algorithm == algorithm]
Expand Down
Loading

0 comments on commit e9ff738

Please sign in to comment.