diff options
author | obscuren <geffobscura@gmail.com> | 2014-02-23 06:19:38 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-02-23 06:19:38 +0800 |
commit | aa33a4b2fb9dc07468498decceb6fdb56d38f54d (patch) | |
tree | 995fdccb8b9c28aaf52e7f3889fc494aae19a878 /ui/gui.go | |
parent | 2b967558cebcef9d3ef9719cbb28a5e596982a5d (diff) | |
download | go-tangerine-aa33a4b2fb9dc07468498decceb6fdb56d38f54d.tar go-tangerine-aa33a4b2fb9dc07468498decceb6fdb56d38f54d.tar.gz go-tangerine-aa33a4b2fb9dc07468498decceb6fdb56d38f54d.tar.bz2 go-tangerine-aa33a4b2fb9dc07468498decceb6fdb56d38f54d.tar.lz go-tangerine-aa33a4b2fb9dc07468498decceb6fdb56d38f54d.tar.xz go-tangerine-aa33a4b2fb9dc07468498decceb6fdb56d38f54d.tar.zst go-tangerine-aa33a4b2fb9dc07468498decceb6fdb56d38f54d.zip |
Added some ui elements to make it easier to connect to nodes
Diffstat (limited to 'ui/gui.go')
-rw-r--r-- | ui/gui.go | 81 |
1 files changed, 59 insertions, 22 deletions
@@ -12,41 +12,56 @@ import ( "time" ) +// Block interface exposed to QML +type Block struct { + Number int + Hash string +} + +// Creates a new QML Block from a chain block +func NewBlockFromBlock(block *ethchain.Block) *Block { + info := block.BlockInfo() + hash := hex.EncodeToString(block.Hash()) + + return &Block{Number: int(info.Number), Hash: hash} +} + type Gui struct { - win *qml.Window + // The main application window + win *qml.Window + // QML Engine engine *qml.Engine component *qml.Common - eth *eth.Ethereum + // The ethereum interface + eth *eth.Ethereum - // The Ethereum library + // The public Ethereum library lib *EthLib } +// Create GUI, but doesn't start it func New(ethereum *eth.Ethereum) *Gui { lib := &EthLib{blockManager: ethereum.BlockManager, blockChain: ethereum.BlockManager.BlockChain(), txPool: ethereum.TxPool} - return &Gui{eth: ethereum, lib: lib} -} + data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) + keyRing := ethutil.NewValueFromBytes(data) + addr := keyRing.Get(1).Bytes() -type Block struct { - Number int - Hash string -} - -func NewBlockFromBlock(block *ethchain.Block) *Block { - info := block.BlockInfo() - hash := hex.EncodeToString(block.Hash()) + ethereum.BlockManager.WatchAddr(addr) - return &Block{Number: int(info.Number), Hash: hash} + return &Gui{eth: ethereum, lib: lib} } func (ui *Gui) Start() { - qml.RegisterTypes("GoExtensions", 1, 0, []qml.TypeSpec{{ + // Register ethereum functions + qml.RegisterTypes("Ethereum", 1, 0, []qml.TypeSpec{{ Init: func(p *Block, obj qml.Object) { p.Number = 0; p.Hash = "" }, }}) ethutil.Config.Log.Infoln("[GUI] Starting GUI") + // Create a new QML engine ui.engine = qml.NewEngine() + // Load the main QML interface component, err := ui.engine.LoadFile("wallet.qml") if err != nil { panic(err) @@ -55,13 +70,18 @@ func (ui *Gui) Start() { ui.win = component.CreateWindow(nil) context := ui.engine.Context() + + // Expose the eth library and the ui library to QML context.SetVar("eth", ui.lib) context.SetVar("ui", &UiLib{engine: ui.engine, eth: ui.eth}) + // Register the ui as a block processor ui.eth.BlockManager.SecondaryBlockProcessor = ui + // Add the ui as a log system so we can log directly to the UGI ethutil.Config.Log.AddLogSystem(ui) + // Loads previous blocks go ui.setInitialBlockChain() go ui.updatePeers() @@ -70,6 +90,7 @@ func (ui *Gui) Start() { } func (ui *Gui) setInitialBlockChain() { + // Load previous 10 blocks chain := ui.eth.BlockManager.BlockChain().GetChain(ui.eth.BlockManager.BlockChain().CurrentBlock.Hash(), 10) for _, block := range chain { ui.ProcessBlock(block) @@ -81,17 +102,24 @@ func (ui *Gui) ProcessBlock(block *ethchain.Block) { ui.win.Root().Call("addBlock", NewBlockFromBlock(block)) } +// Logging functions that log directly to the GUI interface func (ui *Gui) Println(v ...interface{}) { - str := fmt.Sprintln(v...) - // remove last \n - ui.win.Root().Call("addLog", str[:len(str)-1]) + str := strings.TrimRight(fmt.Sprintln(v...), "\n") + lines := strings.Split(str, "\n") + for _, line := range lines { + ui.win.Root().Call("addLog", line) + } } func (ui *Gui) Printf(format string, v ...interface{}) { str := strings.TrimRight(fmt.Sprintf(format, v...), "\n") - ui.win.Root().Call("addLog", str) + lines := strings.Split(str, "\n") + for _, line := range lines { + ui.win.Root().Call("addLog", line) + } } +// Simple go routine function that updates the list of peers in the GUI func (ui *Gui) updatePeers() { for { ui.win.Root().Call("setPeers", fmt.Sprintf("%d / %d", ui.eth.Peers().Len(), ui.eth.MaxPeers)) @@ -99,11 +127,14 @@ func (ui *Gui) updatePeers() { } } +// UI Library that has some basic functionality exposed type UiLib struct { - engine *qml.Engine - eth *eth.Ethereum + engine *qml.Engine + eth *eth.Ethereum + connected bool } +// Opens a QML file (external application) func (ui *UiLib) Open(path string) { component, err := ui.engine.LoadFile(path[7:]) if err != nil { @@ -118,7 +149,13 @@ func (ui *UiLib) Open(path string) { } func (ui *UiLib) Connect() { - ui.eth.Start() + if !ui.connected { + ui.eth.Start() + } +} + +func (ui *UiLib) ConnectToPeer(addr string) { + ui.eth.ConnectToPeer(addr) } type Tester struct { |