207 lines
6.4 KiB
QML
207 lines
6.4 KiB
QML
|
|
import QtQuick
|
||
|
|
import QtQuick.Controls
|
||
|
|
import QtQuick.Layouts
|
||
|
|
|
||
|
|
// Редактор markdown-таблицы. По умолчанию 2×1.
|
||
|
|
// Справа — столбец «+» (добавить строку под строкой), снизу — ряд «+»
|
||
|
|
// (добавить столбец правее столбца). Полосы «+» растут вместе с таблицей.
|
||
|
|
Dialog {
|
||
|
|
id: root
|
||
|
|
|
||
|
|
signal markdownReady(string md)
|
||
|
|
|
||
|
|
title: qsTr("Таблица")
|
||
|
|
modal: true
|
||
|
|
width: 560
|
||
|
|
anchors.centerIn: parent
|
||
|
|
closePolicy: Dialog.NoAutoClose
|
||
|
|
standardButtons: Dialog.NoButton
|
||
|
|
|
||
|
|
property int rows: 1
|
||
|
|
property int cols: 2
|
||
|
|
property var cells: [] // плоский список строк (rows*cols)
|
||
|
|
|
||
|
|
readonly property int cellW: 120
|
||
|
|
readonly property int cellH: 34
|
||
|
|
|
||
|
|
function ensureCells() {
|
||
|
|
while (cells.length < rows * cols)
|
||
|
|
cells.push("")
|
||
|
|
while (cells.length > rows * cols)
|
||
|
|
cells.pop()
|
||
|
|
}
|
||
|
|
|
||
|
|
function cell(r, c) {
|
||
|
|
const i = r * cols + c
|
||
|
|
return i < cells.length ? cells[i] : ""
|
||
|
|
}
|
||
|
|
function setCell(r, c, v) {
|
||
|
|
const i = r * cols + c
|
||
|
|
if (i < cells.length) cells[i] = v
|
||
|
|
}
|
||
|
|
|
||
|
|
function addRowAfter(r) {
|
||
|
|
const src = []
|
||
|
|
for (let c = 0; c < cols; ++c) src.push(cell(r, c))
|
||
|
|
const ins = r + 1
|
||
|
|
const flat = []
|
||
|
|
for (let rr = 0; rr <= rows; ++rr) {
|
||
|
|
if (rr === ins) for (const v of src) flat.push(v)
|
||
|
|
for (let c = 0; c < cols; ++c) {
|
||
|
|
if (rr < rows) flat.push(cell(rr, c))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
cells = flat
|
||
|
|
rows += 1
|
||
|
|
rebuildGrid()
|
||
|
|
}
|
||
|
|
|
||
|
|
function addColAfter(c) {
|
||
|
|
const src = []
|
||
|
|
for (let r = 0; r < rows; ++r) src.push(cell(r, c))
|
||
|
|
const ins = c + 1
|
||
|
|
const flat = []
|
||
|
|
for (let rr = 0; rr < rows; ++rr) {
|
||
|
|
for (let cc = 0; cc <= cols; ++cc) {
|
||
|
|
if (cc === ins) flat.push("")
|
||
|
|
if (cc < cols) flat.push(cell(rr, cc))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
cells = flat
|
||
|
|
cols += 1
|
||
|
|
rebuildGrid()
|
||
|
|
}
|
||
|
|
|
||
|
|
function buildMarkdown() {
|
||
|
|
let out = ""
|
||
|
|
for (let c = 0; c < cols; ++c)
|
||
|
|
out += (c === 0 ? "| " : " | ") + cell(0, c)
|
||
|
|
out += " |\n"
|
||
|
|
for (let c = 0; c < cols; ++c)
|
||
|
|
out += (c === 0 ? "| " : " | ") + "---"
|
||
|
|
out += " |\n"
|
||
|
|
for (let r = 1; r < rows; ++r) {
|
||
|
|
for (let c = 0; c < cols; ++c)
|
||
|
|
out += (c === 0 ? "| " : " | ") + cell(r, c)
|
||
|
|
out += " |\n"
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
|
||
|
|
function rebuildGrid() {
|
||
|
|
// Repeater-ы привязаны к root.rows/root.cols напрямую — ничего не делаем.
|
||
|
|
}
|
||
|
|
|
||
|
|
function resetDialog() {
|
||
|
|
rows = 1
|
||
|
|
cols = 2
|
||
|
|
cells = []
|
||
|
|
for (let i = 0; i < rows * cols; ++i) cells.push("")
|
||
|
|
}
|
||
|
|
|
||
|
|
Component.onCompleted: {
|
||
|
|
resetDialog()
|
||
|
|
}
|
||
|
|
|
||
|
|
ColumnLayout {
|
||
|
|
anchors.fill: parent
|
||
|
|
spacing: 8
|
||
|
|
|
||
|
|
Flickable {
|
||
|
|
Layout.fillWidth: true
|
||
|
|
Layout.fillHeight: true
|
||
|
|
contentWidth: cellGrid.width + 26
|
||
|
|
contentHeight: cellGrid.height + 30
|
||
|
|
clip: true
|
||
|
|
ScrollBar.horizontal: ScrollBar {}
|
||
|
|
ScrollBar.vertical: ScrollBar {}
|
||
|
|
|
||
|
|
Row {
|
||
|
|
id: tableRow
|
||
|
|
spacing: 0
|
||
|
|
|
||
|
|
Grid {
|
||
|
|
id: cellGrid
|
||
|
|
columns: root.cols
|
||
|
|
columnSpacing: 2
|
||
|
|
rowSpacing: 2
|
||
|
|
|
||
|
|
Repeater {
|
||
|
|
model: root.rows * root.cols
|
||
|
|
delegate: TextField {
|
||
|
|
width: root.cellW
|
||
|
|
height: root.cellH
|
||
|
|
text: root.cell(Math.floor(model / root.cols), model % root.cols)
|
||
|
|
onTextEdited: root.setCell(Math.floor(model / root.cols), model % root.cols, text)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Полоса «+» справа: добавить строку под строкой (20px шириной)
|
||
|
|
Grid {
|
||
|
|
columns: 1
|
||
|
|
columnSpacing: 2
|
||
|
|
rowSpacing: 2
|
||
|
|
anchors.leftMargin: 4
|
||
|
|
Repeater {
|
||
|
|
id: rowAddStrip
|
||
|
|
model: root.rows
|
||
|
|
delegate: Button {
|
||
|
|
width: 20
|
||
|
|
height: root.cellH
|
||
|
|
text: "+"
|
||
|
|
font.pixelSize: 12
|
||
|
|
ToolTip.visible: hovered
|
||
|
|
ToolTip.text: qsTr("Добавить строку под этой")
|
||
|
|
onClicked: root.addRowAfter(model)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Row {
|
||
|
|
anchors.top: tableRow.bottom
|
||
|
|
anchors.topMargin: 4
|
||
|
|
Grid {
|
||
|
|
columns: root.cols
|
||
|
|
columnSpacing: 2
|
||
|
|
rowSpacing: 2
|
||
|
|
Repeater {
|
||
|
|
id: colAddStrip
|
||
|
|
model: root.cols
|
||
|
|
delegate: Button {
|
||
|
|
width: root.cellW
|
||
|
|
height: 20
|
||
|
|
text: "+"
|
||
|
|
font.pixelSize: 12
|
||
|
|
ToolTip.visible: hovered
|
||
|
|
ToolTip.text: qsTr("Добавить столбец правее этого")
|
||
|
|
onClicked: root.addColAfter(model)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
RowLayout {
|
||
|
|
Layout.fillWidth: true
|
||
|
|
Item { Layout.fillWidth: true }
|
||
|
|
Label {
|
||
|
|
text: qsTr("Строк: %1 Столбцов: %2").arg(root.rows).arg(root.cols)
|
||
|
|
color: "#888"
|
||
|
|
}
|
||
|
|
Button {
|
||
|
|
text: qsTr("Отмена")
|
||
|
|
onClicked: root.reject()
|
||
|
|
}
|
||
|
|
Button {
|
||
|
|
text: qsTr("Вставить")
|
||
|
|
highlighted: true
|
||
|
|
onClicked: {
|
||
|
|
root.markdownReady(buildMarkdown())
|
||
|
|
root.accept()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|