blob: 3c59393e2fad3117b2297172ff6ebd951ea93adc [file] [log] [blame]
swissChili23958ca2022-02-21 19:23:34 -08001import QtQuick 2.0
2import QtQuick.Controls 2.15
3import QtQuick.Controls.Material 2.0
4import QtQuick.Layouts 1.0
5
swissChilid85daa92022-02-24 15:29:02 -08006import sh.swisschili.REFAL 1.0
7
swissChili25620b02022-02-23 17:15:16 -08008Item {
swissChili23958ca2022-02-21 19:23:34 -08009 id: root
10
swissChili4b3105a2022-02-22 16:34:39 -080011 required property string code
12 required property string result
swissChilid85daa92022-02-24 15:29:02 -080013 property int status: Cell.IDLE
swissChili06cec4e2022-02-24 19:04:32 -080014 property bool cellActive: false
swissChili4b3105a2022-02-22 16:34:39 -080015
swissChili25620b02022-02-23 17:15:16 -080016 signal insertBelowClicked()
17 signal codeEditingFinished(string code)
18 signal cellFocused()
swissChili06cec4e2022-02-24 19:04:32 -080019 signal cellUnfocused()
swissChilid85daa92022-02-24 15:29:02 -080020 signal runClicked()
swissChili06cec4e2022-02-24 19:04:32 -080021 signal deleteClicked()
swissChili23958ca2022-02-21 19:23:34 -080022
swissChili25620b02022-02-23 17:15:16 -080023 height: column.height
swissChili23958ca2022-02-21 19:23:34 -080024
swissChili732628e2022-02-25 10:35:56 -080025 Keys.onEscapePressed: {
26 root.cellUnfocused()
27 code.focus = false
28 }
swissChili25620b02022-02-23 17:15:16 -080029
30 ColumnLayout {
31 id: column
32
swissChili06cec4e2022-02-24 19:04:32 -080033 width: parent.width - 20
34 anchors.centerIn: parent
swissChili25620b02022-02-23 17:15:16 -080035
swissChili06cec4e2022-02-24 19:04:32 -080036 Item {
37 implicitWidth: row.implicitWidth
38 implicitHeight: row.implicitHeight
swissChili23958ca2022-02-21 19:23:34 -080039 Layout.fillWidth: true
swissChili23958ca2022-02-21 19:23:34 -080040
swissChili06cec4e2022-02-24 19:04:32 -080041 Pane {
42 anchors.fill: parent
43 anchors.topMargin: -5
44 anchors.bottomMargin: -5
swissChilid85daa92022-02-24 15:29:02 -080045
swissChili06cec4e2022-02-24 19:04:32 -080046 Material.elevation: root.cellActive ? 4 : 0
swissChili25620b02022-02-23 17:15:16 -080047 }
48
swissChili06cec4e2022-02-24 19:04:32 -080049 MouseArea {
50 id: selectCell
swissChili4b3105a2022-02-22 16:34:39 -080051
swissChili06cec4e2022-02-24 19:04:32 -080052 anchors.fill: row
53
54 onClicked: root.cellFocused()
55 }
56
57 RowLayout {
58 anchors.fill: parent
59 id: row
60
61 RoundButton {
62 Layout.alignment: Qt.AlignTop
63 icon.source: iconForState(root.state)
64 icon.color: Material.color(Material.Grey, Material.Shade600)
65 flat: true
66
67 onClicked: root.runClicked()
68
69 function iconForState(state) {
70 if (state === Cell.RUNNING)
71 return "qrc:///icons/square.svg"
72
73 return "qrc:///icons/play-circle.svg"
74 }
75 }
76
77 ColumnLayout {
swissChili25620b02022-02-23 17:15:16 -080078 Layout.fillWidth: true
79 Layout.fillHeight: true
swissChili25620b02022-02-23 17:15:16 -080080
swissChili06cec4e2022-02-24 19:04:32 -080081 TextArea {
swissChili732628e2022-02-25 10:35:56 -080082 id: code
83
swissChili06cec4e2022-02-24 19:04:32 -080084 Layout.fillWidth: true
85 Layout.fillHeight: true
swissChili732628e2022-02-25 10:35:56 -080086
swissChili06cec4e2022-02-24 19:04:32 -080087 font.family: "monospace"
88 text: root.code
89 selectByMouse: true
90 wrapMode: TextEdit.WrapAtWordBoundaryOrAnywhere
91
92 placeholderText: "Write some code..."
93
94 Keys.onTabPressed: {
95 var pos = cursorPosition + 4
96 text = text.slice(0, cursorPosition) + " " + text.slice(cursorPosition);
97 cursorPosition = pos
98 }
99
100// Keys.onEscapePressed: {
101// focus = false
102// }
103
104 onEditingFinished: {
105 root.codeEditingFinished(text)
106 }
107
108 onFocusChanged: if (focus) root.cellFocused()
109 onActiveFocusChanged: if (activeFocus) root.cellFocused()
swissChili25620b02022-02-23 17:15:16 -0800110 }
111
swissChili06cec4e2022-02-24 19:04:32 -0800112 Label {
113 visible: root.result.length > 0
114 Layout.fillWidth: true
115 font.family: "monospace"
116 text: root.result
swissChili25620b02022-02-23 17:15:16 -0800117
swissChili06cec4e2022-02-24 19:04:32 -0800118 Layout.bottomMargin: 5
swissChili25620b02022-02-23 17:15:16 -0800119 }
swissChili25620b02022-02-23 17:15:16 -0800120 }
121
swissChili06cec4e2022-02-24 19:04:32 -0800122 RoundButton {
swissChili732628e2022-02-25 10:35:56 -0800123 Layout.alignment: Qt.AlignTop
124
swissChili06cec4e2022-02-24 19:04:32 -0800125 icon.source: "qrc:///icons/menu.svg"
126 icon.color: Constants.buttonGrey
127 flat: true
swissChili23958ca2022-02-21 19:23:34 -0800128
swissChili06cec4e2022-02-24 19:04:32 -0800129 onClicked: cellContextMenu.popup()
130
131 Menu {
132 id: cellContextMenu
133
134 MenuItem {
135 icon.source: "qrc:///icons/trash.svg"
136 icon.color: Material.color(Material.Red)
137 text: "Delete"
138
139 onClicked: root.deleteClicked()
140 }
141 }
142 }
swissChili23958ca2022-02-21 19:23:34 -0800143 }
144 }
swissChili23958ca2022-02-21 19:23:34 -0800145
swissChili25620b02022-02-23 17:15:16 -0800146 InsertRow {
147 onInsertClicked: root.insertBelowClicked()
148 }
swissChili23958ca2022-02-21 19:23:34 -0800149 }
150}