one object to rule them all -_-;; adding identities is now ugly but live

This commit is contained in:
Dan Ballard 2018-10-22 22:08:52 -07:00
parent dbd646230c
commit d6317563ea
7 changed files with 122 additions and 165 deletions

View File

@ -1,138 +0,0 @@
package main
import (
"github.com/therecipe/qt/core"
"fmt"
)
func init() { QIdentityListModel_QmlRegisterType2("CustomQmlTypes", 1, 0, "QIdentityListModel") }
// TODO: register QObj of Item
const (
Name = int(core.Qt__UserRole) + 1<<iota
Onion
Color
ButtonType
)
const (
identity = iota
unlock
add
)
type IdentityListItem struct {
name, onion, color string
buttonType int
/*core.QObject
//name, onion, color string
//buttonType int
_ string `property:"name"`
_ string `property:"onion"`
_ string `property:"color"`
_ int `property:"buttonType"`
_ func() `signal:"identityClick,auto"`
_ func() `signal:"unlockClick,auto"`
_ func() `signal:"addClick,auto"`
_ func() `signal:"unknownClick,auto"`*/
}
/*
func NewIdentityListItemValues(name, onion, color string, buttonType int) *IdentityListItem {
i := NewIdentityListItem(nil)
i.SetName(name)
i.SetOnion(onion)
i.SetColor(color)
i.SetButtonType(buttonType)
return i
}
*/
type QIdentityListModel struct {
core.QAbstractListModel
_ int `property:"typeIdentity"`
_ int `property:"typeUnlock"`
_ int `property:"typeAdd"`
_ func() `constructor:"init"`
_ func(string) `signal:"clickIdentity,auto"`
//_ func() `signal:"remove,auto"`
//_ func(obj []*core.QVariant) `signal:"add,auto"`
//_ func(name string, onion string, color string) `signal:"edit,auto"`
modelData []IdentityListItem
}
func (m *QIdentityListModel) init() {
m.modelData = []IdentityListItem{}
m.SetTypeIdentity(identity)
m.SetTypeUnlock(unlock)
m.SetTypeAdd(add)
m.ConnectRoleNames(m.roleNames)
m.ConnectRowCount(m.rowCount)
m.ConnectData(m.data)
}
func (m *QIdentityListModel) roleNames() map[int]*core.QByteArray {
return map[int]*core.QByteArray{
Name: core.NewQByteArray2("Name", -1),
Onion: core.NewQByteArray2("Onion", -1),
Color: core.NewQByteArray2("Color", -1),
ButtonType: core.NewQByteArray2("ButtonType", -1),
}
}
func (m *QIdentityListModel) rowCount(*core.QModelIndex) int {
return len(m.modelData)
}
func (m *QIdentityListModel) data(index *core.QModelIndex, role int) *core.QVariant {
item := m.modelData[index.Row()]
switch role {
case Name:
return core.NewQVariant14(item.name)
case Onion:
return core.NewQVariant14(item.onion)
case Color:
return core.NewQVariant14(item.color)
case ButtonType:
return core.NewQVariant7(item.buttonType)
}
return core.NewQVariant()
}
/*func (m *QIdentityListModel) remove() {
if len(m.modelData) == 0 {
return
}
m.BeginRemoveRows(core.NewQModelIndex(), len(m.modelData)-1, len(m.modelData)-1)
m.modelData = m.modelData[:len(m.modelData)-1]
m.EndRemoveRows()
}*/
func (m *QIdentityListModel) Add(i IdentityListItem) {
m.BeginInsertRows(core.NewQModelIndex(), len(m.modelData), len(m.modelData))
m.modelData = append(m.modelData, i)
m.EndInsertRows()
}
func (m *QIdentityListModel) clickIdentity(s string) {
fmt.Println("clickIdentity: '" + s + "'")
}
/*func (m *QIdentityListModel) edit(name string, onion string, pcolor string) {
if len(m.modelData) == 0 {
return
}
m.modelData[len(m.modelData)-1] = IdentityListItem{name, onion, pcolor}
m.DataChanged(m.Index(len(m.modelData)-1, 0, core.NewQModelIndex()), m.Index(len(m.modelData)-1, 1, core.NewQModelIndex()), []int{Name, Onion, Pcolor})
}*/

54
main.go
View File

@ -6,7 +6,6 @@ import (
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/qml"
"github.com/therecipe/qt/quickcontrols2"
"git.openprivacy.ca/openprivacy/asaur"
"cwtch.im/cwtch/app"
"os/exec"
@ -15,26 +14,58 @@ import (
"path"
"time"
"strconv"
"fmt"
)
const (
identity = iota
unlock
add
)
type QmlCwtchApp struct {
core.QObject
_ int `property:"torStatusProgress"`
_ string `property:"torStatusSummary"`
//_ QIdentityListModel `property:"identities"`
_ int `property:"typeIdentity"`
_ int `property:"typeUnlock"`
_ int `property:"typeAdd"`
_ func(string) `signal:"identityClicked,auto"`
_ func(string, string) `signal:"identityCreate,auto"`
_ func(string, string, string, int) `signal:"identityAddList"`
_ func() `constructor:"init"`
}
func (qCwtchApp *QmlCwtchApp) init() {
func (ca *QmlCwtchApp) init() {
ca.SetTypeIdentity(identity)
ca.SetTypeUnlock(unlock)
ca.SetTypeAdd(add)
}
func (ca *QmlCwtchApp) identityClicked(s string) {
fmt.Println("clickIdentity: '" + s + "'")
}
func (ca *QmlCwtchApp) identityCreate(name, password string) {
fmt.Printf("identityCreate(%v)\n", name)
peer, err := cwtchApp.CreatePeer(name, password)
if err != nil {
fmt.Printf("ERROR creating new identity: %v\n", err)
return
}
ca.IdentityAddList(peer.GetProfile().Name, peer.GetProfile().Onion, "red", identity)
}
var cwtchApp app.Application
func InitCwtch(engine *qml.QQmlApplicationEngine, qCwtchApp *QmlCwtchApp, identityList *QIdentityListModel) error {
func InitCwtch(engine *qml.QQmlApplicationEngine, qCwtchApp *QmlCwtchApp) error {
torPath, err := exec.LookPath("tor")
if err != nil {
@ -76,9 +107,8 @@ func InitCwtch(engine *qml.QQmlApplicationEngine, qCwtchApp *QmlCwtchApp, identi
identityList.Add( *NewIdentityListItemValues( "unlock", "", "#af921d", unlock))
identityList.Add( *NewIdentityListItemValues("add", "", "#e0e0e0", add))*/
identityList.Add( IdentityListItem{"alice", "1234567890abcdef", "red", identity})
identityList.Add( IdentityListItem{"unlock", "", "#af921d", unlock})
identityList.Add( IdentityListItem{"add", "", "#e0e0e0", add})
//cwtchApp.i( IdentityListItem{"alice", "1234567890abcdef", "red", identity})
<-doneChan
@ -98,11 +128,9 @@ func main() {
// use the material style
// the other inbuild styles are:
// Default, Fusion, Imagine, Universal
quickcontrols2.QQuickStyle_SetStyle("Material")
//quickcontrols2.QQuickStyle_SetStyle("Material")
var qCwtchApp = NewQmlCwtchApp(nil)
var identityList = NewQIdentityListModel(nil)
//qCwtchApp.SetIdentities(append(qCwtchApp.Identities(), alice))*/
@ -112,7 +140,6 @@ func main() {
//engine.QmlRegisterType()
engine.RootContext().SetContextProperty("cwtchApp", qCwtchApp)
engine.RootContext().SetContextProperty("identityList", identityList)
// load the embeeded qml file
// created by either qtrcc or qtdeploy
@ -122,8 +149,9 @@ func main() {
// you can also load a local file like this instead:
//engine.Load(core.QUrl_FromLocalFile("./qml/main.qml"))
engine.Load(core.NewQUrl3("qrc:/qml/main.qml", 0))
engine.Load(core.NewQUrl3("qrc:/qml/AddIdentity.qml", 0))
go InitCwtch(engine, qCwtchApp, identityList)
go InitCwtch(engine, qCwtchApp)
// start the main Qt event loop
// and block until app.Exit() is called
// or the window is closed by the user

View File

@ -6,5 +6,6 @@
<file>qml/ProfilesColumn.qml</file>
<file>qml/main.qml</file>
<file>assets/cwtch-logo.png</file>
<file>qml/AddIdentity.qml</file>
</qresource>
</RCC>

46
qml/AddIdentity.qml Normal file
View File

@ -0,0 +1,46 @@
import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
//import CustomQmlTypes 1.0
Window {
visible: false
id: addIdentityWindow
width: 600
//minimumWidth: 600
height: 400
//minimumHeight: 400
title: "Add Identity"
signal closed
onVisibleChanged: if (!visible) closed()
Item {
anchors.left: parent.left
anchors.leftMargin: 200
Column {
Text { text: "Name:" }
TextField { id: nameText }
TextField { id: password; echoMode: TextInput.Password }
TextField { id: passconfirm; echoMode: TextInput.Password }
Text { id: passError; visible: false; text: "Passwords do not match"; color: "red" }
Button { text: "Create"; isDefault: true;
onClicked: {
if (password.text !== passconfirm.text) {
passError.visible = true
} else {
passError.visible = false
cwtchApp.identityCreate(nameText.text, password.text)
addIdentityWindow.visible = false
}
}
}
}
}
}

View File

@ -1,6 +1,5 @@
import QtQuick 2.9
import QtQuick.Window 2.2
import CustomQmlTypes 1.0
Window {
//visible: false
@ -9,11 +8,8 @@ Window {
height: 800
title: qsTr("Bulletin")
property alias model: profilesColumn.model
ProfilesColumn {
id: profilesColumn
model: identityList
anchors.top: parent.top
anchors.left: parent.left

View File

@ -1,18 +1,22 @@
import QtQuick 2.0
import CustomQmlTypes 1.0
Rectangle {
color: "#381F47"
property alias model: list.model
ListView {
id: list
//height: parent.height
//height: parent.height
width: parent.width
anchors.fill: parent
ListModel {
id: identitiesModel
ListElement { name: "unlock"; onion: ""; color: "#af921d"; buttonType: 1}
ListElement { name: "add"; onion: ""; color: "#e0e0e0"; buttonType: 2}
}
model: identitiesModel
highlight: Item { Rectangle { width: 4; height: 100; color: "white"; radius: 2} }
//highlight: highlightBar
@ -20,7 +24,19 @@ Rectangle {
delegate: profileDelegate
Connections {
target: cwtchApp
onIdentityAddList: function (name, onion, color, buttonType) {
console.log("on Identity-Add-List(" + name + "," + onion +","+ color+ "," + buttonType+")")
identitiesModel.insert(identitiesModel.count-2, {
"name": name,
"onion": onion,
"color": color,
"buttonType": buttonType
})
}
}
}
Component {
@ -35,8 +51,8 @@ Rectangle {
anchors.left: parent.left
anchors.leftMargin: 10
Rectangle { y: 10; width: 80; height: 80; color: model.Color }
Text { color: "#a0a0a0"; text: Name }
Rectangle { y: 10; width: 80; height: 80; color: model.color }
Text { color: "#a0a0a0"; text: name }
}
@ -47,7 +63,18 @@ Rectangle {
anchors.fill: parent
onClicked: {
list.currentIndex = index
list.model.clickIdentity(model.Name)
// var addIWin = AddIdentity{ }
console.log("onClicked " + model.name + " type " + model.buttonType)
if (model.buttonType === cwtchApp.typeAdd) {
var component = Qt.createComponent("AddIdentity.qml")
var object = component.createObject(root)
object.show()
//object.closed.connect(function() { object.destroy() })
} else
if (model.ButtonType === cwtchApp.typeIdentity) {
cwtchApp.identityClicked(model.name + " " + model.buttonType)
}
}
}
}

View File

@ -2,7 +2,6 @@ import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
import QtQuick.Window 2.0
import CustomQmlTypes 1.0
// Root non-graphical object providing window management and other logic.
QtObject {
@ -12,8 +11,6 @@ QtObject {
onVisibleChanged: if (!visible) Qt.quit()
}
property QIdentityListModel model: identityList
property LoadingWindow loadingWindow: LoadingWindow { visible: true }
property Timer timer: Timer {