swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 1 | import QtQuick 2.5 |
swissChili | 23958ca | 2022-02-21 19:23:34 -0800 | [diff] [blame] | 2 | import QtQuick.Controls 2.15 |
3 | import QtQuick.Controls.Material 2.0 | ||||
4 | import QtQuick.Layouts 1.0 | ||||
5 | |||||
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 6 | import sh.swisschili.REFAL 1.0 |
7 | |||||
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 8 | Item { |
swissChili | 23958ca | 2022-02-21 19:23:34 -0800 | [diff] [blame] | 9 | id: root |
10 | |||||
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 11 | required property string code |
12 | required property string result | ||||
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 13 | property int status: Cell.IDLE |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 14 | property int resultType: Cell.EXPRESSION |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 15 | property bool cellActive: false |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 16 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 17 | signal insertBelowClicked() |
18 | signal codeEditingFinished(string code) | ||||
19 | signal cellFocused() | ||||
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 20 | signal cellUnfocused() |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 21 | signal runClicked() |
swissChili | 8c5cefa | 2023-01-17 13:04:36 +0100 | [diff] [blame] | 22 | signal quitClicked() |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 23 | signal deleteClicked() |
swissChili | 23958ca | 2022-02-21 19:23:34 -0800 | [diff] [blame] | 24 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 25 | height: column.height |
swissChili | 23958ca | 2022-02-21 19:23:34 -0800 | [diff] [blame] | 26 | |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 27 | Keys.onEscapePressed: { |
28 | root.cellUnfocused() | ||||
29 | code.focus = false | ||||
30 | } | ||||
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 31 | |
32 | ColumnLayout { | ||||
33 | id: column | ||||
34 | |||||
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 35 | width: parent.width - 20 |
36 | anchors.centerIn: parent | ||||
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 37 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 38 | Item { |
39 | implicitWidth: row.implicitWidth | ||||
40 | implicitHeight: row.implicitHeight | ||||
swissChili | 23958ca | 2022-02-21 19:23:34 -0800 | [diff] [blame] | 41 | Layout.fillWidth: true |
swissChili | 23958ca | 2022-02-21 19:23:34 -0800 | [diff] [blame] | 42 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 43 | Pane { |
44 | anchors.fill: parent | ||||
45 | anchors.topMargin: -5 | ||||
46 | anchors.bottomMargin: -5 | ||||
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 47 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 48 | Material.elevation: root.cellActive ? 4 : 0 |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 49 | } |
50 | |||||
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 51 | MouseArea { |
52 | id: selectCell | ||||
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 53 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 54 | anchors.fill: row |
55 | |||||
56 | onClicked: root.cellFocused() | ||||
57 | } | ||||
58 | |||||
59 | RowLayout { | ||||
60 | anchors.fill: parent | ||||
61 | id: row | ||||
62 | |||||
63 | RoundButton { | ||||
64 | Layout.alignment: Qt.AlignTop | ||||
swissChili | 8c5cefa | 2023-01-17 13:04:36 +0100 | [diff] [blame] | 65 | icon.source: iconForState(root.status) |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 66 | icon.color: Material.color(Material.Grey, Material.Shade600) |
67 | flat: true | ||||
68 | |||||
swissChili | 8c5cefa | 2023-01-17 13:04:36 +0100 | [diff] [blame] | 69 | onClicked: { |
70 | if (root.status == Cell.IDLE) | ||||
71 | root.runClicked() | ||||
72 | else if (root.status == Cell.RUNNING) | ||||
73 | root.quitClicked() | ||||
74 | } | ||||
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 75 | |
76 | function iconForState(state) { | ||||
swissChili | 8c5cefa | 2023-01-17 13:04:36 +0100 | [diff] [blame] | 77 | console.log("iconForState", state); |
78 | |||||
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 79 | if (state === Cell.RUNNING) |
80 | return "qrc:///icons/square.svg" | ||||
81 | |||||
82 | return "qrc:///icons/play-circle.svg" | ||||
83 | } | ||||
84 | } | ||||
85 | |||||
86 | ColumnLayout { | ||||
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 87 | Layout.fillWidth: true |
88 | Layout.fillHeight: true | ||||
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 89 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 90 | TextArea { |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 91 | id: code |
92 | |||||
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 93 | Layout.fillWidth: true |
94 | Layout.fillHeight: true | ||||
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 95 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 96 | font.family: "monospace" |
97 | text: root.code | ||||
98 | selectByMouse: true | ||||
99 | wrapMode: TextEdit.WrapAtWordBoundaryOrAnywhere | ||||
100 | |||||
swissChili | d2af6ad | 2022-04-16 14:42:17 -0700 | [diff] [blame] | 101 | placeholderText: qsTr("Write some code...") |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 102 | |
103 | Keys.onTabPressed: { | ||||
104 | var pos = cursorPosition + 4 | ||||
105 | text = text.slice(0, cursorPosition) + " " + text.slice(cursorPosition); | ||||
106 | cursorPosition = pos | ||||
107 | } | ||||
108 | |||||
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 109 | onTextChanged: { |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 110 | root.codeEditingFinished(text) |
111 | } | ||||
112 | |||||
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 113 | onPressed: root.cellFocused() |
114 | |||||
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 115 | onFocusChanged: if (focus) root.cellFocused() |
116 | onActiveFocusChanged: if (activeFocus) root.cellFocused() | ||||
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 117 | } |
118 | |||||
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 119 | Label { |
120 | visible: root.result.length > 0 | ||||
121 | Layout.fillWidth: true | ||||
122 | font.family: "monospace" | ||||
123 | text: root.result | ||||
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 124 | textFormat: root.resultType === Cell.EXPRESSION ? Text.PlainText : Text.RichText |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 125 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 126 | Layout.bottomMargin: 5 |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 127 | } |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 128 | } |
129 | |||||
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 130 | RoundButton { |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 131 | Layout.alignment: Qt.AlignTop |
132 | |||||
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 133 | icon.source: "qrc:///icons/menu.svg" |
134 | icon.color: Constants.buttonGrey | ||||
135 | flat: true | ||||
swissChili | 23958ca | 2022-02-21 19:23:34 -0800 | [diff] [blame] | 136 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 137 | onClicked: cellContextMenu.popup() |
138 | |||||
139 | Menu { | ||||
140 | id: cellContextMenu | ||||
141 | |||||
142 | MenuItem { | ||||
143 | icon.source: "qrc:///icons/trash.svg" | ||||
144 | icon.color: Material.color(Material.Red) | ||||
swissChili | d2af6ad | 2022-04-16 14:42:17 -0700 | [diff] [blame] | 145 | text: qsTr("Delete") |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 146 | |
147 | onClicked: root.deleteClicked() | ||||
148 | } | ||||
149 | } | ||||
150 | } | ||||
swissChili | 23958ca | 2022-02-21 19:23:34 -0800 | [diff] [blame] | 151 | } |
152 | } | ||||
swissChili | 23958ca | 2022-02-21 19:23:34 -0800 | [diff] [blame] | 153 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 154 | InsertRow { |
155 | onInsertClicked: root.insertBelowClicked() | ||||
156 | } | ||||
swissChili | 23958ca | 2022-02-21 19:23:34 -0800 | [diff] [blame] | 157 | } |
158 | } |