| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585 |
- import Felgo
- import QtQuick
- import com.example.database 1.0
- import "../controls"
- // ===========================================================================
- // 数据库管理
- // ---------------------------------------------------------------------------
- // 一个页面完成"选库 -> 选表 -> 看数据 -> 改数据 -> 改表结构"的闭环:
- // · 顶部固定: 数据库切换、数据表切换、搜索、编辑开关
- // · 数据页 : 表头显示"中文名 + 字段名", 点单元格即可改, 保存即时生效
- // · 结构页 : 逐列展示 类型/主键/非空/默认值 + 中文说明, 支持加列/改名/删列
- // 所有写操作都走绑定参数接口, 并且只影响目标行(rowid)。
- // ===========================================================================
- AppPage {
- id: page
- title: "数据库管理"
- // 数据库访问入口(重写时遗漏, 导致下面所有 db.* 都是 undefined):
- // 与旧版 DatabaseInterface{id: db_cfg} 等价, 由 QML 拥有生命周期;
- // 内部使用独立命名连接 qt_sql_dbmanager_connection, 不影响业务默认连接。
- DatabaseInterface {
- id: db
- }
- // ---------------------------------------------------------------
- // 内联按钮(原 qml/controls/DbButton.qml)
- // 为什么内联: CMakeLists 用 file(GLOB) 在 **configure 时**枚举 qml 文件,
- // 新增 qml 文件后若不重新 configure 就不会被 deploy_resources 拷进 APK,
- // 运行时表现为 "DbButton is not a type", 进而让 SqlScreen -> ProfilePage
- // -> Main 整条类型链解析失败(ProfilePage 是 Main.qml 的 initialPage),
- // 启动即崩。本页改成单文件自包含, 不再依赖任何新增外部类型。
- // ---------------------------------------------------------------
- component DbButton: Rectangle {
- id: btn
- property string text: ""
- property string variant: "normal" // primary / normal / danger / ghost
- property bool checked: false // 切换类按钮的选中状态
- property bool isCheckedStyle: false // true 时选中态用主色填充
- property bool compact: false
- property color primaryColor: "#2563EB"
- property color dangerColor: "#DC2626"
- signal clicked()
- implicitWidth: label.implicitWidth + (compact ? 16 : 26)
- implicitHeight: compact ? 30 : 36
- radius: 8
- opacity: enabled ? 1.0 : 0.4
- readonly property bool filled: variant === "primary"
- || variant === "danger"
- || (isCheckedStyle && checked)
- color: {
- if (variant === "primary") return primaryColor
- if (variant === "danger") return dangerColor
- if (isCheckedStyle && checked) return primaryColor
- if (variant === "ghost") return "transparent"
- return "#FFFFFF"
- }
- border.width: (variant === "normal" || variant === "ghost")
- && !(isCheckedStyle && checked) ? 1 : 0
- border.color: variant === "ghost" ? "transparent" : "#D8DEE6"
- Text {
- id: label
- anchors.centerIn: parent
- text: btn.text
- font.pixelSize: btn.compact ? 12 : 14
- font.bold: btn.filled
- color: btn.filled ? "#FFFFFF"
- : (btn.variant === "ghost" ? "#2563EB" : "#374151")
- }
- MouseArea {
- anchors.fill: parent
- enabled: btn.enabled
- cursorShape: Qt.PointingHandCursor
- onClicked: btn.clicked()
- }
- }
- // ---------------- 主题色 ----------------
- readonly property color cCard: "#FFFFFF"
- readonly property color cText: "#1F2937"
- readonly property color cSub: "#6B7280"
- readonly property color cLine: "#E5E7EB"
- readonly property color cHeader: "#F8FAFC"
- readonly property color cAccent: "#2563EB"
- readonly property color cDanger: "#DC2626"
- readonly property color cOk: "#16A34A"
- readonly property color cRowAlt: "#FAFBFD"
- readonly property color cSelected: "#EAF2FE"
- // ---------------- 数据状态 ----------------
- property string curDb: "config.db"
- property var tableNames: []
- property string curTable: ""
- property var schema: [] // [{name,type,notnull,dflt,pk}]
- property var colKeys: []
- property var colLabels: []
- property var colWidths: []
- property var rows: []
- property int totalRows: 0
- property int pageIndex: 0
- property int pageSize: 20
- property string keyword: ""
- property bool editEnabled: false // 编辑开关: 关闭时点单元格不会误改数据
- property int tabIndex: 0 // 0=数据 1=表结构
- property int selectedRow: -1
- // 弹窗状态
- property string dialogMode: "" // cell / row / column / confirm / sql
- property string dialogTitle: ""
- property string dialogSubtitle: ""
- property string dialogOkText: "保存"
- property string editColumn: ""
- property string editHint: ""
- property string editRowId: ""
- property string editOldValue: ""
- property var formValues: ({})
- property var formRows: [] // 新增行表单里要填的列(每次重新赋值以清空输入框)
- property string columnMode: "add" // add / rename
- property string columnNameOld: ""
- property string columnTypeNew: "TEXT"
- property string confirmText: ""
- property string confirmKind: "" // row / column
- property string sqlText: ""
- // 轻提示
- property string toastText: ""
- property bool toastOk: true
- // ---------------- 字段中文说明字典 ----------------
- // 结构: 表名 -> 列名 -> [中文名, 说明]
- readonly property var fieldDict: ({
- "mqtt": {
- "id": ["记录ID", "主键。1=主服务器配置,2=备用服务器配置"],
- "dtype": ["数据类型", "平台下发的数据类型标识,一般由服务器写入,不建议手动修改"],
- "ip": ["服务器地址", "MQTT 服务器 IP 或域名,例如 47.100.x.x 或 iot.example.com"],
- "port": ["服务器端口", "明文通常 1883,TLS 加密通常 8883"],
- "imei": ["设备IMEI", "设备唯一编号,用于拼接主题和平台识别设备"],
- "uid": ["用户名", "连接 MQTT 服务器的用户名,可为空"],
- "pwd": ["密码", "连接 MQTT 服务器的密码,可为空"],
- "clientid": ["客户端ID", "MQTT ClientId,留空时程序按 IMEI 自动生成"],
- "qos": ["服务质量", "0=最多一次,1=至少一次,2=只有一次,通常用 1"],
- "keepalive": ["心跳间隔", "与服务器保持连接的间隔,单位秒,通常 60"],
- "sub": ["订阅主题", "接收平台指令的主题前缀,实际主题会拼接 IMEI"],
- "pub": ["发布主题", "上报设备数据的主题前缀,实际主题会拼接 IMEI"],
- "offline": ["离线主题", "MQTT 遗嘱消息主题,设备掉线时由服务器发布"],
- "dat_f": ["上报频率", "数据上报频率,单位分钟。例如 10 表示每 10 分钟上报一次;填 0 或更小时程序按 10 分钟处理"]
- },
- "ftp": {
- "id": ["记录ID", "主键,用于区分多组 FTP 配置"],
- "ip": ["FTP地址", "FTP 服务器 IP 或域名"],
- "port": ["FTP端口", "FTP 服务端口,默认 21"],
- "dir": ["上传目录", "图片上传到 FTP 服务器上的相对路径,例如 /images/"],
- "uid": ["用户名", "FTP 登录用户名"],
- "pwd": ["密码", "FTP 登录密码"],
- "mode": ["传输模式", "0=被动模式(PASV,推荐),1=主动模式(PORT)"]
- },
- "location": {
- "id": ["记录ID", "主键"],
- "lbs_lat": ["基站纬度", "LBS 基站定位纬度,GPS 不可用时作为备用"],
- "lbs_lng": ["基站经度", "LBS 基站定位经度"],
- "gps_lat": ["GPS纬度", "卫星定位纬度,优先使用"],
- "gps_lng": ["GPS经度", "卫星定位经度"],
- "gps_elevation": ["海拔高度", "GPS 海拔高度,单位米"]
- },
- "camera": {
- "camera_type": ["相机类型", "相机型号/接口标识,决定拍照流程走哪个分支"],
- "mode": ["拍摄模式", "拍照工作模式,与拼接张数配合使用"],
- "stitch_photonum": ["拼接张数", "一次任务采集并拼接的照片数量"],
- "photo_width": ["照片宽度", "输出照片宽度,单位像素"],
- "photo_height": ["照片高度", "输出照片高度,单位像素"],
- "auto_focus": ["自动对焦", "自动对焦流程开关,开启后拍照前先对焦"],
- "movz_f": ["前组Z轴位置", "前镜头组 Z 轴(对焦)初始位置,单位步进"],
- "movz_b": ["后组Z轴位置", "后镜头组 Z 轴(对焦)初始位置"],
- "movx_f": ["前组X轴位置", "前镜头组 X 轴初始位置"],
- "movx_b": ["后组X轴位置", "后镜头组 X 轴初始位置"]
- },
- "data": {
- "id": ["记录ID", "主键,自增序号"],
- "datetime": ["采集时间", "数据写入时间,格式 yyyy-MM-dd HH:mm:ss"],
- "Json_data": ["业务数据", "设备上报的完整 JSON 报文"],
- "Json_warn": ["告警数据", "病害/告警识别结果 JSON"]
- },
- "config": {
- "jsonCacheEnable": ["JSON缓存开关", "1=启用本地 JSON 数据缓存,0=关闭"],
- "imageCacheEnable": ["图片缓存开关", "1=启用本地图片缓存,0=关闭"],
- "maxJsonEntries": ["JSON缓存上限", "本地最多保留的 JSON 记录条数,超出后循环覆盖"],
- "maxImageEntries": ["图片缓存上限", "本地最多保留的图片数量,超出后循环覆盖"],
- "x_init": ["初始X坐标", "云台/相机的初始 X 位置"],
- "y_init": ["初始Y坐标", "云台/相机的初始 Y 位置"],
- "fusion_enable": ["图像融合开关", "1=开启多图融合处理,0=关闭"],
- "focus_algo": ["对焦算法", "对焦算法类型,按现场标定结果选择"]
- }
- })
- // ftp_other / mqtt_other 与主表结构一致, 复用同一份说明
- readonly property var dictAlias: ({ "mqtt_other": "mqtt", "ftp_other": "ftp" })
- function fieldInfo(table, col) {
- var t = fieldDict[dictAlias[table] !== undefined ? dictAlias[table] : table]
- if (t) {
- if (t[col] !== undefined)
- return { label: t[col][0], hint: t[col][1] }
- // 数据库列名大小写不统一(如 movZ_f / movz_f), 做一次忽略大小写匹配
- var lower = String(col).toLowerCase()
- for (var k in t) {
- if (k.toLowerCase() === lower)
- return { label: t[k][0], hint: t[k][1] }
- }
- }
- return { label: col, hint: "自定义字段:数据库中新增的列,暂无内置中文说明,可直接查看和修改取值" }
- }
- function fieldLabel(table, col) { return fieldInfo(table, col).label }
- function fieldHint(table, col) { return fieldInfo(table, col).hint }
- function colDefaultWidth(name, type) {
- var n = String(name).toLowerCase()
- if (n === "json_data" || n === "json_warn") return 300
- if (n === "datetime") return 165
- if (n === "ip" || n === "sub" || n === "pub" || n === "offline" || n === "dir") return 200
- if (n === "pwd" || n === "uid" || n === "clientid") return 140
- if (type === "INT" || type === "INTEGER" || type === "REAL") return 105
- return 135
- }
- // 空值也要能分辨: 显示为空字符串, 由界面画灰色"空"占位
- function cellText(rowData, col) {
- if (!rowData) return ""
- var v = rowData[col]
- if (v === undefined || v === null) return ""
- return String(v)
- }
- function totalPages() {
- return Math.max(1, Math.ceil(totalRows / Math.max(1, pageSize)))
- }
- function toast(msg, ok) {
- toastText = msg
- toastOk = (ok === undefined ? true : ok)
- toastTimer.restart()
- }
- // ---------------- 数据加载 ----------------
- function openDb(name) {
- curDb = name
- if (!db.openDatabase(name)) {
- tableNames = []
- curTable = ""
- rows = []
- schema = []
- totalRows = 0
- toast("打开 " + name + " 失败:" + db.lastError(), false)
- return
- }
- tableNames = db.tableList()
- keyword = ""
- pageIndex = 0
- selectedRow = -1
- if (tableNames.length > 0)
- selectTable(tableNames[0])
- else
- toast(name + " 中没有数据表", false)
- }
- function selectTable(name) {
- if (curTable === name && rows.length > 0)
- return
- curTable = name
- pageIndex = 0
- selectedRow = -1
- reloadSchema()
- reloadRows()
- }
- function reloadSchema() {
- if (curTable === "") { schema = []; colKeys = []; colLabels = []; colWidths = []; return }
- var info = db.tableInfo(curTable)
- var keys = [], labels = [], widths = []
- for (var i = 0; i < info.length; ++i) {
- keys.push(info[i].name)
- labels.push(fieldLabel(curTable, info[i].name))
- widths.push(colDefaultWidth(info[i].name, info[i].type))
- }
- schema = info
- colKeys = keys
- colLabels = labels
- colWidths = widths
- }
- function reloadRows() {
- if (curTable === "") { rows = []; totalRows = 0; return }
- totalRows = db.tableRowCount(curTable)
- rows = db.queryRows(curTable, pageSize, pageIndex * pageSize, keyword)
- selectedRow = -1
- }
- function reloadAll() {
- reloadSchema()
- reloadRows()
- }
- function applyKeyword(text) {
- keyword = text
- pageIndex = 0
- reloadRows()
- }
- function setColWidth(index, width) {
- var list = colWidths.slice()
- list[index] = Math.max(70, width)
- colWidths = list
- }
- function goPage(delta) {
- var target = pageIndex + delta
- if (target < 0 || target >= totalPages()) return
- pageIndex = target
- reloadRows()
- }
- // ---------------- 弹窗: 单元格 ----------------
- function openCellEditor(rowData, col) {
- if (!editEnabled) {
- toast("先打开右上角「允许编辑」再修改数据")
- return
- }
- if (rowData.__rowid === undefined) return
- editRowId = Number(rowData.__rowid)
- editColumn = col
- editOldValue = cellText(rowData, col)
- editHint = fieldHint(curTable, col)
- dialogTitle = fieldLabel(curTable, col) + "(" + col + ")"
- dialogSubtitle = "行 rowid=" + editRowId + " · 修改后立即写入数据库"
- dialogOkText = "保存修改"
- dialogMode = "cell"
- overlay.visible = true
- cellInput.text = editOldValue
- cellInput.forceActiveFocus()
- }
- function applyCellEdit() {
- var value = cellInput.text
- if (value === editOldValue) { overlay.visible = false; return }
- if (db.updateCell(curTable, editColumn, value, editRowId)) {
- overlay.visible = false
- toast("已保存:" + fieldLabel(curTable, editColumn) + " = " + value)
- reloadRows()
- } else {
- toast("保存失败:" + db.lastError(), false)
- }
- }
- // ---------------- 弹窗: 新增行 ----------------
- function openRowEditor() {
- if (curTable === "") { toast("请先选择数据表", false); return }
- var values = ({})
- var names = []
- for (var i = 0; i < schema.length; ++i) {
- values[schema[i].name] = ""
- names.push(schema[i].name)
- }
- formValues = values
- formRows = names
- dialogTitle = "新增一行 · " + curTable
- dialogSubtitle = "带「必填」标记的列不能为空;留空的列写入空字符串"
- dialogOkText = "写入数据库"
- dialogMode = "row"
- overlay.visible = true
- }
- function setFormValue(name, text) {
- formValues[name] = text
- }
- function applyRowInsert() {
- var values = ({})
- for (var i = 0; i < schema.length; ++i) {
- var name = schema[i].name
- var v = formValues[name]
- if (schema[i].notnull === 1 && (v === undefined || v === "")) {
- toast("「" + fieldLabel(curTable, name) + "」不能为空", false)
- return
- }
- if (v !== undefined && v !== "")
- values[name] = v
- }
- if (db.insertRow(curTable, values)) {
- overlay.visible = false
- toast("新增成功")
- totalRows = db.tableRowCount(curTable)
- pageIndex = totalPages() - 1
- reloadRows()
- if (tabIndex === 0) selectedRow = rows.length - 1
- } else {
- toast("新增失败:" + db.lastError(), false)
- }
- }
- // ---------------- 弹窗: 表结构 ----------------
- function openColumnEditor(mode, name, type) {
- columnMode = mode
- columnNameOld = (name === undefined ? "" : name)
- columnTypeNew = (type === undefined || type === "" ? "TEXT" : type)
- formValues = ({ name: columnNameOld, dflt: "" })
- // 弹窗里的输入框是常驻实例, 打开时手动回填, 避免沿用上次的残留内容
- columnNameInput.text = columnNameOld
- columnDefaultInput.text = ""
- if (mode === "add") {
- dialogTitle = "新增字段 · " + curTable
- dialogSubtitle = "字段名只允许字母/数字/下划线;新增列不会影响已有数据"
- dialogOkText = "添加字段"
- } else {
- dialogTitle = "重命名字段 · " + name
- dialogSubtitle = fieldHint(curTable, name) + "。改名后引用该列的代码需同步调整"
- dialogOkText = "确认改名"
- }
- dialogMode = "column"
- overlay.visible = true
- }
- function applyColumnEdit() {
- var name = formValues["name"]
- var dflt = formValues["dflt"]
- if (columnMode === "add") {
- if (db.addColumn(curTable, name, columnTypeNew, dflt)) {
- overlay.visible = false
- toast("已添加字段:" + name)
- reloadAll()
- } else {
- toast("添加失败:" + db.lastError(), false)
- }
- } else {
- if (db.renameColumn(curTable, columnNameOld, name)) {
- overlay.visible = false
- toast("已重命名:" + columnNameOld + " → " + name)
- reloadAll()
- } else {
- toast("重命名失败:" + db.lastError(), false)
- }
- }
- }
- function confirmDeleteRow() {
- if (selectedRow < 0 || selectedRow >= rows.length) {
- toast("先在左侧行号上点一下,选中要删除的行", false)
- return
- }
- var indent = rows[selectedRow].id !== undefined ? ("id=" + rows[selectedRow].id) : ("rowid=" + rows[selectedRow].__rowid)
- confirmKind = "row"
- confirmText = "确定删除 " + curTable + " 中 " + indent + " 这一行吗?删除后不可恢复。"
- dialogTitle = "删除整行"
- dialogSubtitle = ""
- dialogOkText = "确认删除"
- dialogMode = "confirm"
- overlay.visible = true
- }
- function confirmDeleteColumn(name) {
- confirmKind = "column"
- confirmText = "确定删除字段「" + name + "」吗?该列所有数据都会丢失,且不可恢复。"
- dialogTitle = "删除字段"
- dialogSubtitle = fieldHint(curTable, name)
- dialogOkText = "确认删除"
- columnNameOld = name
- dialogMode = "confirm"
- overlay.visible = true
- }
- function applyConfirm() {
- if (confirmKind === "row") {
- var rowId = Number(rows[selectedRow].__rowid)
- if (db.deleteRow(curTable, rowId)) {
- overlay.visible = false
- toast("已删除该行")
- reloadRows()
- } else {
- toast("删除失败:" + db.lastError(), false)
- }
- } else {
- if (db.dropColumn(curTable, columnNameOld)) {
- overlay.visible = false
- toast("已删除字段:" + columnNameOld)
- reloadAll()
- } else {
- toast("删除字段失败:" + db.lastError(), false)
- }
- }
- }
- function showTableSql() {
- sqlText = db.tableSql(curTable)
- dialogTitle = "建表语句 · " + curTable
- dialogSubtitle = "数据库中的真实表结构定义,可对照字段说明一起看"
- dialogMode = "sql"
- overlay.visible = true
- }
- Rectangle { anchors.fill: parent; color: "#F2F4F7"; z: -1 }
- Column {
- id: rootCol
- anchors.fill: parent
- anchors.margins: 10
- spacing: 10
- // ================= 顶部: 选库 / 选表 / 搜索 / 编辑开关 =================
- Rectangle {
- id: headerCard
- width: parent.width
- height: headerCol.height + 24
- radius: 12
- color: page.cCard
- border.color: page.cLine
- Column {
- id: headerCol
- anchors.left: parent.left
- anchors.right: parent.right
- anchors.top: parent.top
- anchors.margins: 12
- spacing: 10
- // 第一行: 数据库切换 + 当前表信息 + 刷新
- Item {
- width: parent.width
- height: 34
- Row {
- anchors.left: parent.left
- anchors.verticalCenter: parent.verticalCenter
- spacing: 8
- Text {
- height: 32
- text: "数据库"
- verticalAlignment: Text.AlignVCenter
- font.pixelSize: 13
- color: page.cSub
- }
- Repeater {
- model: ["config.db", "data.db"]
- delegate: DbButton {
- text: modelData
- compact: true
- isCheckedStyle: true
- checked: page.curDb === modelData
- onClicked: page.openDb(modelData)
- }
- }
- }
- Row {
- anchors.right: parent.right
- anchors.verticalCenter: parent.verticalCenter
- spacing: 8
- Text {
- height: 32
- text: page.curTable === "" ? "未选择数据表"
- : (page.curTable + " · 共 " + page.totalRows + " 行")
- verticalAlignment: Text.AlignVCenter
- font.pixelSize: 12
- color: page.cSub
- }
- DbButton {
- text: "刷新"
- compact: true
- onClicked: page.reloadAll()
- }
- }
- }
- // 第二行: 数据表切换(横向滚动)
- Item {
- width: parent.width
- height: 32
- Text {
- id: tableListLabel
- anchors.left: parent.left
- anchors.verticalCenter: parent.verticalCenter
- text: "数据表"
- font.pixelSize: 13
- color: page.cSub
- }
- Flickable {
- anchors.left: tableListLabel.right
- anchors.leftMargin: 10
- anchors.right: parent.right
- anchors.top: parent.top
- anchors.bottom: parent.bottom
- contentWidth: tableChips.width
- clip: true
- flickableDirection: Flickable.HorizontalFlick
- boundsBehavior: Flickable.StopAtBounds
- Row {
- id: tableChips
- height: parent.height
- spacing: 6
- Repeater {
- model: page.tableNames
- delegate: DbButton {
- text: modelData
- compact: true
- isCheckedStyle: true
- checked: page.curTable === modelData
- onClicked: {
- page.tabIndex = 0
- page.selectTable(modelData)
- }
- }
- }
- }
- }
- }
- // 第三行: 数据/表结构 切换 + 搜索 + 编辑开关
- Item {
- width: parent.width
- height: 34
- Row {
- anchors.left: parent.left
- anchors.verticalCenter: parent.verticalCenter
- spacing: 6
- DbButton {
- text: "数据"
- compact: true
- isCheckedStyle: true
- checked: page.tabIndex === 0
- onClicked: page.tabIndex = 0
- }
- DbButton {
- text: "表结构"
- compact: true
- isCheckedStyle: true
- checked: page.tabIndex === 1
- onClicked: page.tabIndex = 1
- }
- }
- Row {
- anchors.right: parent.right
- anchors.verticalCenter: parent.verticalCenter
- spacing: 6
- Rectangle {
- width: 190
- height: 32
- radius: 8
- color: "#F8FAFC"
- border.color: "#CBD5E1"
- TextInput {
- id: searchInput
- anchors.fill: parent
- anchors.leftMargin: 10
- anchors.rightMargin: 10
- verticalAlignment: TextInput.AlignVCenter
- font.pixelSize: 13
- color: page.cText
- clip: true
- selectByMouse: true
- onAccepted: page.applyKeyword(text)
- }
- Text {
- anchors.left: parent.left
- anchors.leftMargin: 10
- anchors.verticalCenter: parent.verticalCenter
- visible: searchInput.text.length === 0
- text: "搜索本表内容(回车)"
- font.pixelSize: 13
- color: "#9CA3AF"
- }
- }
- DbButton {
- text: "搜索"
- compact: true
- onClicked: page.applyKeyword(searchInput.text)
- }
- DbButton {
- text: "清空"
- compact: true
- variant: "ghost"
- visible: page.keyword !== ""
- onClicked: {
- searchInput.text = ""
- page.applyKeyword("")
- }
- }
- DbButton {
- text: page.editEnabled ? "允许编辑:开" : "允许编辑:关"
- compact: true
- isCheckedStyle: true
- checked: page.editEnabled
- onClicked: {
- page.editEnabled = !page.editEnabled
- page.toast(page.editEnabled ? "已开启编辑,点击单元格即可修改"
- : "已关闭编辑,单元格为只读")
- }
- }
- }
- }
- }
- }
- // ================= 内容卡片 =================
- Rectangle {
- id: contentCard
- width: parent.width
- height: rootCol.height - headerCard.height - rootCol.spacing
- radius: 12
- color: page.cCard
- border.color: page.cLine
- clip: true
- // ================= 数据页 =================
- Item {
- id: dataView
- anchors.fill: parent
- visible: page.tabIndex === 0
- Item {
- id: gridArea
- anchors.left: parent.left
- anchors.right: parent.right
- anchors.top: parent.top
- anchors.bottom: dataFooter.top
- clip: true
- // 表头: 中文名 + 字段名, 右边缘可拖动调整列宽
- Rectangle {
- id: gridHeader
- width: parent.width
- height: 46
- color: page.cHeader
- clip: true
- Row {
- id: gridHeaderRow
- x: -gridFlick.contentX
- height: parent.height
- Rectangle {
- width: 46
- height: gridHeader.height
- color: "transparent"
- Text {
- anchors.centerIn: parent
- text: "#"
- font.pixelSize: 12
- color: page.cSub
- }
- }
- Repeater {
- model: page.colKeys.length
- delegate: Rectangle {
- property int ci: index
- width: page.colWidths[ci] !== undefined ? page.colWidths[ci] : 120
- height: gridHeader.height
- color: "transparent"
- Column {
- anchors.left: parent.left
- anchors.leftMargin: 8
- anchors.right: parent.right
- anchors.rightMargin: 8
- anchors.verticalCenter: parent.verticalCenter
- spacing: 1
- Text {
- width: parent.width
- text: page.colLabels[ci]
- font.pixelSize: 12
- font.bold: true
- color: page.cText
- elide: Text.ElideRight
- }
- Text {
- width: parent.width
- text: page.colKeys[ci]
- font.pixelSize: 10
- color: page.cSub
- elide: Text.ElideRight
- }
- }
- Rectangle {
- width: 8
- anchors.right: parent.right
- anchors.top: parent.top
- anchors.bottom: parent.bottom
- color: "transparent"
- MouseArea {
- anchors.fill: parent
- cursorShape: Qt.SplitHCursor
- property real pressX: 0
- onPressed: pressX = mouseX
- onPositionChanged: {
- if (pressed)
- page.setColWidth(ci, page.colWidths[ci] + (mouse.x - pressX))
- }
- }
- }
- }
- }
- }
- Rectangle {
- anchors.bottom: parent.bottom
- width: parent.width
- height: 1
- color: page.cLine
- }
- }
- // 数据行
- Flickable {
- id: gridFlick
- anchors.left: parent.left
- anchors.right: parent.right
- anchors.top: gridHeader.bottom
- anchors.bottom: parent.bottom
- contentWidth: Math.max(gridContent.width, width)
- contentHeight: Math.max(gridContent.height, height)
- clip: true
- boundsBehavior: Flickable.StopAtBounds
- Column {
- id: gridContent
- Repeater {
- model: page.rows
- delegate: Rectangle {
- id: rowItem
- property var rowData: modelData
- property int rowIndex: index
- width: Math.max(rowCells.width, gridFlick.width)
- height: 38
- color: page.selectedRow === rowIndex ? page.cSelected
- : (rowIndex % 2 === 0 ? "#FFFFFF" : page.cRowAlt)
- Row {
- id: rowCells
- height: rowItem.height
- Rectangle {
- width: 46
- height: rowItem.height
- color: "transparent"
- Text {
- anchors.centerIn: parent
- text: page.pageIndex * page.pageSize + rowItem.rowIndex + 1
- font.pixelSize: 11
- font.bold: page.selectedRow === rowItem.rowIndex
- color: page.selectedRow === rowItem.rowIndex ? page.cAccent : page.cSub
- }
- MouseArea {
- anchors.fill: parent
- cursorShape: Qt.PointingHandCursor
- onClicked: page.selectedRow =
- (page.selectedRow === rowItem.rowIndex ? -1 : rowItem.rowIndex)
- }
- }
- Repeater {
- model: page.colKeys.length
- delegate: Rectangle {
- property int ci: index
- width: page.colWidths[ci] !== undefined ? page.colWidths[ci] : 120
- height: rowItem.height
- color: "transparent"
- Rectangle {
- anchors.right: parent.right
- anchors.top: parent.top
- anchors.bottom: parent.bottom
- width: 1
- color: "#F1F3F6"
- }
- Rectangle {
- anchors.left: parent.left
- anchors.right: parent.right
- anchors.bottom: parent.bottom
- height: 1
- color: "#F1F3F6"
- }
- Text {
- anchors.fill: parent
- anchors.leftMargin: 8
- anchors.rightMargin: 8
- verticalAlignment: Text.AlignVCenter
- elide: Text.ElideRight
- font.pixelSize: 12
- color: page.cText
- text: page.cellText(rowItem.rowData, page.colKeys[ci])
- }
- Text {
- anchors.fill: parent
- anchors.leftMargin: 8
- verticalAlignment: Text.AlignVCenter
- visible: page.cellText(rowItem.rowData, page.colKeys[ci]) === ""
- text: "空"
- font.pixelSize: 11
- font.italic: true
- color: "#C3C9D4"
- }
- MouseArea {
- anchors.fill: parent
- enabled: page.editEnabled
- cursorShape: page.editEnabled ? Qt.PointingHandCursor : Qt.ArrowCursor
- onClicked: page.openCellEditor(rowItem.rowData, page.colKeys[ci])
- }
- }
- }
- }
- }
- }
- }
- }
- Text {
- anchors.centerIn: parent
- visible: page.rows.length === 0
- horizontalAlignment: Text.AlignHCenter
- text: page.curTable === "" ? "请选择数据表"
- : (page.keyword !== "" ? "没有匹配「" + page.keyword + "」的记录"
- : "该表暂无数据,可点下方「新增行」")
- font.pixelSize: 13
- color: page.cSub
- }
- }
- // 底部操作条: 新增 / 删除 / 分页
- Rectangle {
- id: dataFooter
- anchors.left: parent.left
- anchors.right: parent.right
- anchors.bottom: parent.bottom
- height: 50
- color: page.cCard
- Rectangle {
- anchors.top: parent.top
- width: parent.width
- height: 1
- color: page.cLine
- }
- Row {
- anchors.left: parent.left
- anchors.leftMargin: 12
- anchors.verticalCenter: parent.verticalCenter
- spacing: 8
- DbButton {
- text: "新增行"
- compact: true
- variant: "primary"
- onClicked: page.openRowEditor()
- }
- DbButton {
- text: "删除选中行"
- compact: true
- variant: "danger"
- enabled: page.selectedRow >= 0
- onClicked: page.confirmDeleteRow()
- }
- DbButton {
- text: page.selectedRow >= 0 ? "取消选中" : "点行号可选中"
- compact: true
- variant: "ghost"
- onClicked: page.selectedRow = -1
- }
- }
- Row {
- anchors.right: parent.right
- anchors.rightMargin: 12
- anchors.verticalCenter: parent.verticalCenter
- spacing: 6
- Text {
- height: 32
- verticalAlignment: Text.AlignVCenter
- text: "第 " + (page.pageIndex + 1) + " / " + page.totalPages() + " 页"
- font.pixelSize: 12
- color: page.cSub
- }
- DbButton {
- text: "上一页"
- compact: true
- enabled: page.pageIndex > 0
- onClicked: page.goPage(-1)
- }
- DbButton {
- text: "下一页"
- compact: true
- enabled: page.pageIndex + 1 < page.totalPages()
- onClicked: page.goPage(1)
- }
- Repeater {
- model: [20, 50, 100]
- delegate: DbButton {
- text: modelData + "行"
- compact: true
- isCheckedStyle: true
- checked: page.pageSize === modelData
- onClicked: {
- page.pageSize = modelData
- page.pageIndex = 0
- page.reloadRows()
- }
- }
- }
- }
- }
- }
- // ================= 表结构页 =================
- Item {
- id: structView
- anchors.fill: parent
- visible: page.tabIndex === 1
- ListView {
- id: structList
- anchors.left: parent.left
- anchors.right: parent.right
- anchors.top: parent.top
- anchors.bottom: structFooter.top
- clip: true
- model: page.schema
- boundsBehavior: Flickable.StopAtBounds
- delegate: Rectangle {
- width: structList.width
- height: 80
- color: index % 2 === 0 ? "#FFFFFF" : page.cRowAlt
- Rectangle {
- anchors.left: parent.left
- anchors.right: parent.right
- anchors.bottom: parent.bottom
- height: 1
- color: "#F1F3F6"
- }
- Column {
- anchors.left: parent.left
- anchors.leftMargin: 14
- anchors.right: actionRow.left
- anchors.rightMargin: 10
- anchors.verticalCenter: parent.verticalCenter
- spacing: 5
- Row {
- height: 22
- spacing: 8
- Text {
- height: 22
- verticalAlignment: Text.AlignVCenter
- text: modelData.name
- font.pixelSize: 14
- font.bold: true
- color: page.cText
- }
- Rectangle {
- height: 18
- width: typeText.implicitWidth + 12
- radius: 9
- color: "#EEF2F7"
- Text {
- id: typeText
- anchors.centerIn: parent
- text: modelData.type === "" ? "未声明类型" : modelData.type
- font.pixelSize: 10
- color: page.cSub
- }
- }
- Rectangle {
- height: 18
- width: pkText.implicitWidth + 12
- radius: 9
- visible: modelData.pk > 0
- color: "#E8F0FE"
- Text {
- id: pkText
- anchors.centerIn: parent
- text: "主键"
- font.pixelSize: 10
- color: page.cAccent
- }
- }
- Rectangle {
- height: 18
- width: nnText.implicitWidth + 12
- radius: 9
- visible: modelData.notnull === 1
- color: "#FEF3E2"
- Text {
- id: nnText
- anchors.centerIn: parent
- text: "非空"
- font.pixelSize: 10
- color: "#B45309"
- }
- }
- Text {
- height: 22
- verticalAlignment: Text.AlignVCenter
- visible: modelData.dflt !== ""
- text: "默认值:" + modelData.dflt
- font.pixelSize: 11
- color: page.cSub
- }
- }
- Text {
- width: parent.width
- text: page.fieldLabel(page.curTable, modelData.name)
- + " · " + page.fieldHint(page.curTable, modelData.name)
- font.pixelSize: 12
- color: page.cSub
- elide: Text.ElideRight
- }
- }
- Row {
- id: actionRow
- anchors.right: parent.right
- anchors.rightMargin: 12
- anchors.verticalCenter: parent.verticalCenter
- spacing: 6
- DbButton {
- text: "重命名"
- compact: true
- onClicked: page.openColumnEditor("rename", modelData.name, modelData.type)
- }
- DbButton {
- text: "删除"
- compact: true
- variant: "danger"
- onClicked: page.confirmDeleteColumn(modelData.name)
- }
- }
- }
- }
- Text {
- anchors.centerIn: parent
- visible: page.schema.length === 0
- text: page.curTable === "" ? "请选择数据表" : "没有读取到字段信息"
- font.pixelSize: 13
- color: page.cSub
- }
- Rectangle {
- id: structFooter
- anchors.left: parent.left
- anchors.right: parent.right
- anchors.bottom: parent.bottom
- height: 50
- color: page.cCard
- Rectangle {
- anchors.top: parent.top
- width: parent.width
- height: 1
- color: page.cLine
- }
- Row {
- anchors.left: parent.left
- anchors.leftMargin: 12
- anchors.verticalCenter: parent.verticalCenter
- spacing: 10
- DbButton {
- text: "新增字段"
- compact: true
- variant: "primary"
- onClicked: page.openColumnEditor("add", "", "TEXT")
- }
- Text {
- height: 32
- verticalAlignment: Text.AlignVCenter
- text: "字段后面的说明为该列的中文含义,删除/改名会立即修改数据库结构"
- font.pixelSize: 11
- color: page.cSub
- }
- }
- Row {
- anchors.right: parent.right
- anchors.rightMargin: 12
- anchors.verticalCenter: parent.verticalCenter
- DbButton {
- text: "查看建表语句"
- compact: true
- onClicked: page.showTableSql()
- }
- }
- }
- }
- }
- }
- // ================= 弹窗(编辑单元格 / 新增行 / 字段结构 / 确认删除 / SQL) =================
- Rectangle {
- id: overlay
- anchors.fill: parent
- color: "#66000000"
- visible: false
- z: 1000
- MouseArea {
- anchors.fill: parent
- onClicked: overlay.visible = false
- }
- Rectangle {
- id: dialogCard
- width: Math.min(parent.width - 32, 480)
- height: Math.min(dialogCol.height + 28, page.height - 60)
- anchors.centerIn: parent
- radius: 14
- color: "#FFFFFF"
- border.color: page.cLine
- MouseArea { anchors.fill: parent }
- Flickable {
- anchors.fill: parent
- anchors.margins: 14
- contentWidth: width
- contentHeight: dialogCol.height
- clip: true
- boundsBehavior: Flickable.StopAtBounds
- Column {
- id: dialogCol
- width: parent.width
- spacing: 10
- Text {
- width: parent.width
- text: page.dialogTitle
- font.pixelSize: 16
- font.bold: true
- color: page.cText
- wrapMode: Text.Wrap
- }
- Text {
- width: parent.width
- visible: page.dialogSubtitle !== ""
- text: page.dialogSubtitle
- font.pixelSize: 12
- color: page.cSub
- wrapMode: Text.Wrap
- }
- // ---- 单元格编辑 ----
- Column {
- width: parent.width
- spacing: 8
- visible: page.dialogMode === "cell"
- Text {
- width: parent.width
- text: page.editHint
- font.pixelSize: 12
- color: page.cSub
- wrapMode: Text.Wrap
- }
- Rectangle {
- width: parent.width
- height: 46
- radius: 8
- color: "#F8FAFC"
- border.color: "#CBD5E1"
- TextInput {
- id: cellInput
- anchors.fill: parent
- anchors.leftMargin: 10
- anchors.rightMargin: 10
- verticalAlignment: TextInput.AlignVCenter
- font.pixelSize: 14
- color: page.cText
- selectByMouse: true
- onAccepted: page.applyCellEdit()
- }
- }
- }
- // ---- 新增行 ----
- Column {
- width: parent.width
- spacing: 10
- visible: page.dialogMode === "row"
- Repeater {
- model: page.formRows
- delegate: Column {
- width: dialogCol.width
- spacing: 4
- Row {
- height: 20
- spacing: 6
- Text {
- height: 20
- verticalAlignment: Text.AlignVCenter
- text: modelData
- font.pixelSize: 12
- font.bold: true
- color: page.cText
- }
- Text {
- height: 20
- verticalAlignment: Text.AlignVCenter
- text: page.fieldLabel(page.curTable, modelData)
- font.pixelSize: 12
- color: page.cAccent
- }
- Text {
- height: 20
- verticalAlignment: Text.AlignVCenter
- text: page.schemaOf(modelData).notnull === 1 ? "必填" : "可留空"
- font.pixelSize: 11
- color: page.schemaOf(modelData).notnull === 1 ? page.cDanger : page.cSub
- }
- }
- Rectangle {
- width: parent.width
- height: 38
- radius: 6
- color: "#F8FAFC"
- border.color: "#CBD5E1"
- TextInput {
- anchors.fill: parent
- anchors.margins: 8
- font.pixelSize: 13
- color: page.cText
- selectByMouse: true
- Component.onCompleted: text = page.formValueOf(modelData)
- onTextChanged: page.setFormValue(modelData, text)
- }
- }
- }
- }
- }
- // ---- 新增/重命名字段 ----
- Column {
- width: parent.width
- spacing: 8
- visible: page.dialogMode === "column"
- Text {
- text: "字段名(英文,字母/数字/下划线)"
- font.pixelSize: 12
- color: page.cSub
- }
- Rectangle {
- width: parent.width
- height: 40
- radius: 6
- color: "#F8FAFC"
- border.color: "#CBD5E1"
- TextInput {
- id: columnNameInput
- anchors.fill: parent
- anchors.margins: 8
- font.pixelSize: 13
- color: page.cText
- selectByMouse: true
- onTextChanged: page.formValues["name"] = text
- }
- }
- Text {
- visible: page.columnMode === "add"
- text: "字段类型"
- font.pixelSize: 12
- color: page.cSub
- }
- Row {
- visible: page.columnMode === "add"
- spacing: 6
- Repeater {
- model: ["TEXT", "INTEGER", "REAL"]
- delegate: DbButton {
- text: modelData
- compact: true
- isCheckedStyle: true
- checked: page.columnTypeNew === modelData
- onClicked: page.columnTypeNew = modelData
- }
- }
- }
- Text {
- visible: page.columnMode === "add"
- text: "默认值(可留空)"
- font.pixelSize: 12
- color: page.cSub
- }
- Rectangle {
- width: parent.width
- height: 40
- radius: 6
- visible: page.columnMode === "add"
- color: "#F8FAFC"
- border.color: "#CBD5E1"
- TextInput {
- id: columnDefaultInput
- anchors.fill: parent
- anchors.margins: 8
- font.pixelSize: 13
- color: page.cText
- selectByMouse: true
- onTextChanged: page.formValues["dflt"] = text
- }
- }
- Text {
- width: parent.width
- visible: page.columnMode === "add"
- text: "提示:SQLite 新增列不能带 NOT NULL 约束;需要更复杂的结构请用 SQL 工具调整。"
- font.pixelSize: 11
- color: page.cSub
- wrapMode: Text.Wrap
- }
- }
- // ---- 确认删除 ----
- Text {
- width: parent.width
- visible: page.dialogMode === "confirm"
- text: page.confirmText
- font.pixelSize: 13
- color: page.cText
- wrapMode: Text.Wrap
- }
- // ---- 建表语句 ----
- Text {
- width: parent.width
- visible: page.dialogMode === "sql"
- text: page.sqlText
- font.pixelSize: 12
- font.family: "monospace"
- color: page.cText
- wrapMode: Text.WrapAnywhere
- }
- Row {
- width: parent.width
- spacing: 8
- Item {
- width: Math.max(0, parent.width - 300)
- height: 1
- }
- DbButton {
- text: "关闭"
- width: 80
- visible: page.dialogMode === "sql"
- onClicked: overlay.visible = false
- }
- DbButton {
- text: "取消"
- width: 80
- visible: page.dialogMode !== "sql"
- onClicked: overlay.visible = false
- }
- DbButton {
- text: page.dialogOkText
- width: 110
- visible: page.dialogMode !== "sql"
- variant: page.dialogMode === "confirm" ? "danger" : "primary"
- onClicked: page.applyDialog()
- }
- }
- }
- }
- }
- }
- // ================= 轻提示 =================
- Rectangle {
- id: toastBox
- anchors.horizontalCenter: parent.horizontalCenter
- anchors.bottom: parent.bottom
- anchors.bottomMargin: 18
- width: Math.min(parent.width - 40, toastLabel.implicitWidth + 32)
- height: 40
- radius: 20
- color: page.toastOk ? "#1F2937" : "#B91C1C"
- opacity: page.toastText === "" ? 0 : 0.96
- visible: opacity > 0
- z: 2000
- Behavior on opacity { NumberAnimation { duration: 140 } }
- Text {
- id: toastLabel
- anchors.centerIn: parent
- width: Math.min(parent.width - 32, implicitWidth)
- text: page.toastText
- color: "#FFFFFF"
- font.pixelSize: 13
- horizontalAlignment: Text.AlignHCenter
- elide: Text.ElideRight
- }
- }
- Timer {
- id: toastTimer
- interval: 2200
- onTriggered: page.toastText = ""
- }
- // ================= 弹窗统一提交 =================
- function applyDialog() {
- if (dialogMode === "cell") page.applyCellEdit()
- else if (dialogMode === "row") page.applyRowInsert()
- else if (dialogMode === "column") page.applyColumnEdit()
- else if (dialogMode === "confirm") page.applyConfirm()
- }
- // 新增行表单: formRows 每次重新赋值 -> Repeater 重建, 输入框回到初始值
- function schemaOf(name) {
- for (var i = 0; i < schema.length; ++i) {
- if (schema[i].name === name) return schema[i]
- }
- return { name: name, type: "", notnull: 0, dflt: "", pk: 0 }
- }
- function formValueOf(name) {
- var v = formValues[name]
- return v === undefined ? "" : v
- }
- Component.onCompleted: page.openDb(page.curDb)
- Component.onDestruction: db.closeDatabase()
- }
|