blob: 24eaf17c0a915dd829a8ff1a2edb3d9dfd4686bf [file] [log] [blame]
swissChiliece1ac82022-02-25 11:20:42 -08001import QtQuick 2.5
swissChili23958ca2022-02-21 19:23:34 -08002import 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
swissChiliece1ac82022-02-25 11:20:42 -080014 property int resultType: Cell.EXPRESSION
swissChili06cec4e2022-02-24 19:04:32 -080015 property bool cellActive: false
swissChili4b3105a2022-02-22 16:34:39 -080016
swissChili25620b02022-02-23 17:15:16 -080017 signal insertBelowClicked()
18 signal codeEditingFinished(string code)
19 signal cellFocused()
swissChili06cec4e2022-02-24 19:04:32 -080020 signal cellUnfocused()
swissChilid85daa92022-02-24 15:29:02 -080021 signal runClicked()
swissChili06cec4e2022-02-24 19:04:32 -080022 signal deleteClicked()
swissChili23958ca2022-02-21 19:23:34 -080023
swissChili25620b02022-02-23 17:15:16 -080024 height: column.height
swissChili23958ca2022-02-21 19:23:34 -080025
swissChili732628e2022-02-25 10:35:56 -080026 Keys.onEscapePressed: {
27 root.cellUnfocused()
28 code.focus = false
29 }
swissChili25620b02022-02-23 17:15:16 -080030
31 ColumnLayout {
32 id: column
33
swissChili06cec4e2022-02-24 19:04:32 -080034 width: parent.width - 20
35 anchors.centerIn: parent
swissChili25620b02022-02-23 17:15:16 -080036
swissChili06cec4e2022-02-24 19:04:32 -080037 Item {
38 implicitWidth: row.implicitWidth
39 implicitHeight: row.implicitHeight
swissChili23958ca2022-02-21 19:23:34 -080040 Layout.fillWidth: true
swissChili23958ca2022-02-21 19:23:34 -080041
swissChili06cec4e2022-02-24 19:04:32 -080042 Pane {
43 anchors.fill: parent
44 anchors.topMargin: -5
45 anchors.bottomMargin: -5
swissChilid85daa92022-02-24 15:29:02 -080046
swissChili06cec4e2022-02-24 19:04:32 -080047 Material.elevation: root.cellActive ? 4 : 0
swissChili25620b02022-02-23 17:15:16 -080048 }
49
swissChili06cec4e2022-02-24 19:04:32 -080050 MouseArea {
51 id: selectCell
swissChili4b3105a2022-02-22 16:34:39 -080052
swissChili06cec4e2022-02-24 19:04:32 -080053 anchors.fill: row
54
55 onClicked: root.cellFocused()
56 }
57
58 RowLayout {
59 anchors.fill: parent
60 id: row
61
62 RoundButton {
63 Layout.alignment: Qt.AlignTop
64 icon.source: iconForState(root.state)
65 icon.color: Material.color(Material.Grey, Material.Shade600)
66 flat: true
67
68 onClicked: root.runClicked()
69
70 function iconForState(state) {
71 if (state === Cell.RUNNING)
72 return "qrc:///icons/square.svg"
73
74 return "qrc:///icons/play-circle.svg"
75 }
76 }
77
78 ColumnLayout {
swissChili25620b02022-02-23 17:15:16 -080079 Layout.fillWidth: true
80 Layout.fillHeight: true
swissChili25620b02022-02-23 17:15:16 -080081
swissChili06cec4e2022-02-24 19:04:32 -080082 TextArea {
swissChili732628e2022-02-25 10:35:56 -080083 id: code
84
swissChili06cec4e2022-02-24 19:04:32 -080085 Layout.fillWidth: true
86 Layout.fillHeight: true
swissChili732628e2022-02-25 10:35:56 -080087
swissChili06cec4e2022-02-24 19:04:32 -080088 font.family: "monospace"
89 text: root.code
90 selectByMouse: true
91 wrapMode: TextEdit.WrapAtWordBoundaryOrAnywhere
92
93 placeholderText: "Write some code..."
94
95 Keys.onTabPressed: {
96 var pos = cursorPosition + 4
97 text = text.slice(0, cursorPosition) + " " + text.slice(cursorPosition);
98 cursorPosition = pos
99 }
100
swissChiliece1ac82022-02-25 11:20:42 -0800101 onTextChanged: {
swissChili06cec4e2022-02-24 19:04:32 -0800102 root.codeEditingFinished(text)
103 }
104
swissChiliece1ac82022-02-25 11:20:42 -0800105 onPressed: root.cellFocused()
106
swissChili06cec4e2022-02-24 19:04:32 -0800107 onFocusChanged: if (focus) root.cellFocused()
108 onActiveFocusChanged: if (activeFocus) root.cellFocused()
swissChili25620b02022-02-23 17:15:16 -0800109 }
110
swissChili06cec4e2022-02-24 19:04:32 -0800111 Label {
112 visible: root.result.length > 0
113 Layout.fillWidth: true
114 font.family: "monospace"
115 text: root.result
swissChiliece1ac82022-02-25 11:20:42 -0800116 textFormat: root.resultType === Cell.EXPRESSION ? Text.PlainText : Text.RichText
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}