SqlScreen.qml 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. import Felgo
  2. import QtQuick
  3. import com.example.database 1.0
  4. import "../controls"
  5. // ===========================================================================
  6. // 数据库管理
  7. // ---------------------------------------------------------------------------
  8. // 一个页面完成"选库 -> 选表 -> 看数据 -> 改数据 -> 改表结构"的闭环:
  9. // · 顶部固定: 数据库切换、数据表切换、搜索、编辑开关
  10. // · 数据页 : 表头显示"中文名 + 字段名", 点单元格即可改, 保存即时生效
  11. // · 结构页 : 逐列展示 类型/主键/非空/默认值 + 中文说明, 支持加列/改名/删列
  12. // 所有写操作都走绑定参数接口, 并且只影响目标行(rowid)。
  13. // ===========================================================================
  14. AppPage {
  15. id: page
  16. title: "数据库管理"
  17. // 数据库访问入口(重写时遗漏, 导致下面所有 db.* 都是 undefined):
  18. // 与旧版 DatabaseInterface{id: db_cfg} 等价, 由 QML 拥有生命周期;
  19. // 内部使用独立命名连接 qt_sql_dbmanager_connection, 不影响业务默认连接。
  20. DatabaseInterface {
  21. id: db
  22. }
  23. // ---------------------------------------------------------------
  24. // 内联按钮(原 qml/controls/DbButton.qml)
  25. // 为什么内联: CMakeLists 用 file(GLOB) 在 **configure 时**枚举 qml 文件,
  26. // 新增 qml 文件后若不重新 configure 就不会被 deploy_resources 拷进 APK,
  27. // 运行时表现为 "DbButton is not a type", 进而让 SqlScreen -> ProfilePage
  28. // -> Main 整条类型链解析失败(ProfilePage 是 Main.qml 的 initialPage),
  29. // 启动即崩。本页改成单文件自包含, 不再依赖任何新增外部类型。
  30. // ---------------------------------------------------------------
  31. component DbButton: Rectangle {
  32. id: btn
  33. property string text: ""
  34. property string variant: "normal" // primary / normal / danger / ghost
  35. property bool checked: false // 切换类按钮的选中状态
  36. property bool isCheckedStyle: false // true 时选中态用主色填充
  37. property bool compact: false
  38. property color primaryColor: "#2563EB"
  39. property color dangerColor: "#DC2626"
  40. signal clicked()
  41. implicitWidth: label.implicitWidth + (compact ? 16 : 26)
  42. implicitHeight: compact ? 30 : 36
  43. radius: 8
  44. opacity: enabled ? 1.0 : 0.4
  45. readonly property bool filled: variant === "primary"
  46. || variant === "danger"
  47. || (isCheckedStyle && checked)
  48. color: {
  49. if (variant === "primary") return primaryColor
  50. if (variant === "danger") return dangerColor
  51. if (isCheckedStyle && checked) return primaryColor
  52. if (variant === "ghost") return "transparent"
  53. return "#FFFFFF"
  54. }
  55. border.width: (variant === "normal" || variant === "ghost")
  56. && !(isCheckedStyle && checked) ? 1 : 0
  57. border.color: variant === "ghost" ? "transparent" : "#D8DEE6"
  58. Text {
  59. id: label
  60. anchors.centerIn: parent
  61. text: btn.text
  62. font.pixelSize: btn.compact ? 12 : 14
  63. font.bold: btn.filled
  64. color: btn.filled ? "#FFFFFF"
  65. : (btn.variant === "ghost" ? "#2563EB" : "#374151")
  66. }
  67. MouseArea {
  68. anchors.fill: parent
  69. enabled: btn.enabled
  70. cursorShape: Qt.PointingHandCursor
  71. onClicked: btn.clicked()
  72. }
  73. }
  74. // ---------------- 主题色 ----------------
  75. readonly property color cCard: "#FFFFFF"
  76. readonly property color cText: "#1F2937"
  77. readonly property color cSub: "#6B7280"
  78. readonly property color cLine: "#E5E7EB"
  79. readonly property color cHeader: "#F8FAFC"
  80. readonly property color cAccent: "#2563EB"
  81. readonly property color cDanger: "#DC2626"
  82. readonly property color cOk: "#16A34A"
  83. readonly property color cRowAlt: "#FAFBFD"
  84. readonly property color cSelected: "#EAF2FE"
  85. // ---------------- 数据状态 ----------------
  86. property string curDb: "config.db"
  87. property var tableNames: []
  88. property string curTable: ""
  89. property var schema: [] // [{name,type,notnull,dflt,pk}]
  90. property var colKeys: []
  91. property var colLabels: []
  92. property var colWidths: []
  93. property var rows: []
  94. property int totalRows: 0
  95. property int pageIndex: 0
  96. property int pageSize: 20
  97. property string keyword: ""
  98. property bool editEnabled: false // 编辑开关: 关闭时点单元格不会误改数据
  99. property int tabIndex: 0 // 0=数据 1=表结构
  100. property int selectedRow: -1
  101. // 弹窗状态
  102. property string dialogMode: "" // cell / row / column / confirm / sql
  103. property string dialogTitle: ""
  104. property string dialogSubtitle: ""
  105. property string dialogOkText: "保存"
  106. property string editColumn: ""
  107. property string editHint: ""
  108. property string editRowId: ""
  109. property string editOldValue: ""
  110. property var formValues: ({})
  111. property var formRows: [] // 新增行表单里要填的列(每次重新赋值以清空输入框)
  112. property string columnMode: "add" // add / rename
  113. property string columnNameOld: ""
  114. property string columnTypeNew: "TEXT"
  115. property string confirmText: ""
  116. property string confirmKind: "" // row / column
  117. property string sqlText: ""
  118. // 轻提示
  119. property string toastText: ""
  120. property bool toastOk: true
  121. // ---------------- 字段中文说明字典 ----------------
  122. // 结构: 表名 -> 列名 -> [中文名, 说明]
  123. readonly property var fieldDict: ({
  124. "mqtt": {
  125. "id": ["记录ID", "主键。1=主服务器配置,2=备用服务器配置"],
  126. "dtype": ["数据类型", "平台下发的数据类型标识,一般由服务器写入,不建议手动修改"],
  127. "ip": ["服务器地址", "MQTT 服务器 IP 或域名,例如 47.100.x.x 或 iot.example.com"],
  128. "port": ["服务器端口", "明文通常 1883,TLS 加密通常 8883"],
  129. "imei": ["设备IMEI", "设备唯一编号,用于拼接主题和平台识别设备"],
  130. "uid": ["用户名", "连接 MQTT 服务器的用户名,可为空"],
  131. "pwd": ["密码", "连接 MQTT 服务器的密码,可为空"],
  132. "clientid": ["客户端ID", "MQTT ClientId,留空时程序按 IMEI 自动生成"],
  133. "qos": ["服务质量", "0=最多一次,1=至少一次,2=只有一次,通常用 1"],
  134. "keepalive": ["心跳间隔", "与服务器保持连接的间隔,单位秒,通常 60"],
  135. "sub": ["订阅主题", "接收平台指令的主题前缀,实际主题会拼接 IMEI"],
  136. "pub": ["发布主题", "上报设备数据的主题前缀,实际主题会拼接 IMEI"],
  137. "offline": ["离线主题", "MQTT 遗嘱消息主题,设备掉线时由服务器发布"],
  138. "dat_f": ["上报频率", "数据上报频率,单位分钟。例如 10 表示每 10 分钟上报一次;填 0 或更小时程序按 10 分钟处理"]
  139. },
  140. "ftp": {
  141. "id": ["记录ID", "主键,用于区分多组 FTP 配置"],
  142. "ip": ["FTP地址", "FTP 服务器 IP 或域名"],
  143. "port": ["FTP端口", "FTP 服务端口,默认 21"],
  144. "dir": ["上传目录", "图片上传到 FTP 服务器上的相对路径,例如 /images/"],
  145. "uid": ["用户名", "FTP 登录用户名"],
  146. "pwd": ["密码", "FTP 登录密码"],
  147. "mode": ["传输模式", "0=被动模式(PASV,推荐),1=主动模式(PORT)"]
  148. },
  149. "location": {
  150. "id": ["记录ID", "主键"],
  151. "lbs_lat": ["基站纬度", "LBS 基站定位纬度,GPS 不可用时作为备用"],
  152. "lbs_lng": ["基站经度", "LBS 基站定位经度"],
  153. "gps_lat": ["GPS纬度", "卫星定位纬度,优先使用"],
  154. "gps_lng": ["GPS经度", "卫星定位经度"],
  155. "gps_elevation": ["海拔高度", "GPS 海拔高度,单位米"]
  156. },
  157. "camera": {
  158. "camera_type": ["相机类型", "相机型号/接口标识,决定拍照流程走哪个分支"],
  159. "mode": ["拍摄模式", "拍照工作模式,与拼接张数配合使用"],
  160. "stitch_photonum": ["拼接张数", "一次任务采集并拼接的照片数量"],
  161. "photo_width": ["照片宽度", "输出照片宽度,单位像素"],
  162. "photo_height": ["照片高度", "输出照片高度,单位像素"],
  163. "auto_focus": ["自动对焦", "自动对焦流程开关,开启后拍照前先对焦"],
  164. "movz_f": ["前组Z轴位置", "前镜头组 Z 轴(对焦)初始位置,单位步进"],
  165. "movz_b": ["后组Z轴位置", "后镜头组 Z 轴(对焦)初始位置"],
  166. "movx_f": ["前组X轴位置", "前镜头组 X 轴初始位置"],
  167. "movx_b": ["后组X轴位置", "后镜头组 X 轴初始位置"]
  168. },
  169. "data": {
  170. "id": ["记录ID", "主键,自增序号"],
  171. "datetime": ["采集时间", "数据写入时间,格式 yyyy-MM-dd HH:mm:ss"],
  172. "Json_data": ["业务数据", "设备上报的完整 JSON 报文"],
  173. "Json_warn": ["告警数据", "病害/告警识别结果 JSON"]
  174. },
  175. "config": {
  176. "jsonCacheEnable": ["JSON缓存开关", "1=启用本地 JSON 数据缓存,0=关闭"],
  177. "imageCacheEnable": ["图片缓存开关", "1=启用本地图片缓存,0=关闭"],
  178. "maxJsonEntries": ["JSON缓存上限", "本地最多保留的 JSON 记录条数,超出后循环覆盖"],
  179. "maxImageEntries": ["图片缓存上限", "本地最多保留的图片数量,超出后循环覆盖"],
  180. "x_init": ["初始X坐标", "云台/相机的初始 X 位置"],
  181. "y_init": ["初始Y坐标", "云台/相机的初始 Y 位置"],
  182. "fusion_enable": ["图像融合开关", "1=开启多图融合处理,0=关闭"],
  183. "focus_algo": ["对焦算法", "对焦算法类型,按现场标定结果选择"]
  184. }
  185. })
  186. // ftp_other / mqtt_other 与主表结构一致, 复用同一份说明
  187. readonly property var dictAlias: ({ "mqtt_other": "mqtt", "ftp_other": "ftp" })
  188. function fieldInfo(table, col) {
  189. var t = fieldDict[dictAlias[table] !== undefined ? dictAlias[table] : table]
  190. if (t) {
  191. if (t[col] !== undefined)
  192. return { label: t[col][0], hint: t[col][1] }
  193. // 数据库列名大小写不统一(如 movZ_f / movz_f), 做一次忽略大小写匹配
  194. var lower = String(col).toLowerCase()
  195. for (var k in t) {
  196. if (k.toLowerCase() === lower)
  197. return { label: t[k][0], hint: t[k][1] }
  198. }
  199. }
  200. return { label: col, hint: "自定义字段:数据库中新增的列,暂无内置中文说明,可直接查看和修改取值" }
  201. }
  202. function fieldLabel(table, col) { return fieldInfo(table, col).label }
  203. function fieldHint(table, col) { return fieldInfo(table, col).hint }
  204. function colDefaultWidth(name, type) {
  205. var n = String(name).toLowerCase()
  206. if (n === "json_data" || n === "json_warn") return 300
  207. if (n === "datetime") return 165
  208. if (n === "ip" || n === "sub" || n === "pub" || n === "offline" || n === "dir") return 200
  209. if (n === "pwd" || n === "uid" || n === "clientid") return 140
  210. if (type === "INT" || type === "INTEGER" || type === "REAL") return 105
  211. return 135
  212. }
  213. // 空值也要能分辨: 显示为空字符串, 由界面画灰色"空"占位
  214. function cellText(rowData, col) {
  215. if (!rowData) return ""
  216. var v = rowData[col]
  217. if (v === undefined || v === null) return ""
  218. return String(v)
  219. }
  220. function totalPages() {
  221. return Math.max(1, Math.ceil(totalRows / Math.max(1, pageSize)))
  222. }
  223. function toast(msg, ok) {
  224. toastText = msg
  225. toastOk = (ok === undefined ? true : ok)
  226. toastTimer.restart()
  227. }
  228. // ---------------- 数据加载 ----------------
  229. function openDb(name) {
  230. curDb = name
  231. if (!db.openDatabase(name)) {
  232. tableNames = []
  233. curTable = ""
  234. rows = []
  235. schema = []
  236. totalRows = 0
  237. toast("打开 " + name + " 失败:" + db.lastError(), false)
  238. return
  239. }
  240. tableNames = db.tableList()
  241. keyword = ""
  242. pageIndex = 0
  243. selectedRow = -1
  244. if (tableNames.length > 0)
  245. selectTable(tableNames[0])
  246. else
  247. toast(name + " 中没有数据表", false)
  248. }
  249. function selectTable(name) {
  250. if (curTable === name && rows.length > 0)
  251. return
  252. curTable = name
  253. pageIndex = 0
  254. selectedRow = -1
  255. reloadSchema()
  256. reloadRows()
  257. }
  258. function reloadSchema() {
  259. if (curTable === "") { schema = []; colKeys = []; colLabels = []; colWidths = []; return }
  260. var info = db.tableInfo(curTable)
  261. var keys = [], labels = [], widths = []
  262. for (var i = 0; i < info.length; ++i) {
  263. keys.push(info[i].name)
  264. labels.push(fieldLabel(curTable, info[i].name))
  265. widths.push(colDefaultWidth(info[i].name, info[i].type))
  266. }
  267. schema = info
  268. colKeys = keys
  269. colLabels = labels
  270. colWidths = widths
  271. }
  272. function reloadRows() {
  273. if (curTable === "") { rows = []; totalRows = 0; return }
  274. totalRows = db.tableRowCount(curTable)
  275. rows = db.queryRows(curTable, pageSize, pageIndex * pageSize, keyword)
  276. selectedRow = -1
  277. }
  278. function reloadAll() {
  279. reloadSchema()
  280. reloadRows()
  281. }
  282. function applyKeyword(text) {
  283. keyword = text
  284. pageIndex = 0
  285. reloadRows()
  286. }
  287. function setColWidth(index, width) {
  288. var list = colWidths.slice()
  289. list[index] = Math.max(70, width)
  290. colWidths = list
  291. }
  292. function goPage(delta) {
  293. var target = pageIndex + delta
  294. if (target < 0 || target >= totalPages()) return
  295. pageIndex = target
  296. reloadRows()
  297. }
  298. // ---------------- 弹窗: 单元格 ----------------
  299. function openCellEditor(rowData, col) {
  300. if (!editEnabled) {
  301. toast("先打开右上角「允许编辑」再修改数据")
  302. return
  303. }
  304. if (rowData.__rowid === undefined) return
  305. editRowId = Number(rowData.__rowid)
  306. editColumn = col
  307. editOldValue = cellText(rowData, col)
  308. editHint = fieldHint(curTable, col)
  309. dialogTitle = fieldLabel(curTable, col) + "(" + col + ")"
  310. dialogSubtitle = "行 rowid=" + editRowId + " · 修改后立即写入数据库"
  311. dialogOkText = "保存修改"
  312. dialogMode = "cell"
  313. overlay.visible = true
  314. cellInput.text = editOldValue
  315. cellInput.forceActiveFocus()
  316. }
  317. function applyCellEdit() {
  318. var value = cellInput.text
  319. if (value === editOldValue) { overlay.visible = false; return }
  320. if (db.updateCell(curTable, editColumn, value, editRowId)) {
  321. overlay.visible = false
  322. toast("已保存:" + fieldLabel(curTable, editColumn) + " = " + value)
  323. reloadRows()
  324. } else {
  325. toast("保存失败:" + db.lastError(), false)
  326. }
  327. }
  328. // ---------------- 弹窗: 新增行 ----------------
  329. function openRowEditor() {
  330. if (curTable === "") { toast("请先选择数据表", false); return }
  331. var values = ({})
  332. var names = []
  333. for (var i = 0; i < schema.length; ++i) {
  334. values[schema[i].name] = ""
  335. names.push(schema[i].name)
  336. }
  337. formValues = values
  338. formRows = names
  339. dialogTitle = "新增一行 · " + curTable
  340. dialogSubtitle = "带「必填」标记的列不能为空;留空的列写入空字符串"
  341. dialogOkText = "写入数据库"
  342. dialogMode = "row"
  343. overlay.visible = true
  344. }
  345. function setFormValue(name, text) {
  346. formValues[name] = text
  347. }
  348. function applyRowInsert() {
  349. var values = ({})
  350. for (var i = 0; i < schema.length; ++i) {
  351. var name = schema[i].name
  352. var v = formValues[name]
  353. if (schema[i].notnull === 1 && (v === undefined || v === "")) {
  354. toast("「" + fieldLabel(curTable, name) + "」不能为空", false)
  355. return
  356. }
  357. if (v !== undefined && v !== "")
  358. values[name] = v
  359. }
  360. if (db.insertRow(curTable, values)) {
  361. overlay.visible = false
  362. toast("新增成功")
  363. totalRows = db.tableRowCount(curTable)
  364. pageIndex = totalPages() - 1
  365. reloadRows()
  366. if (tabIndex === 0) selectedRow = rows.length - 1
  367. } else {
  368. toast("新增失败:" + db.lastError(), false)
  369. }
  370. }
  371. // ---------------- 弹窗: 表结构 ----------------
  372. function openColumnEditor(mode, name, type) {
  373. columnMode = mode
  374. columnNameOld = (name === undefined ? "" : name)
  375. columnTypeNew = (type === undefined || type === "" ? "TEXT" : type)
  376. formValues = ({ name: columnNameOld, dflt: "" })
  377. // 弹窗里的输入框是常驻实例, 打开时手动回填, 避免沿用上次的残留内容
  378. columnNameInput.text = columnNameOld
  379. columnDefaultInput.text = ""
  380. if (mode === "add") {
  381. dialogTitle = "新增字段 · " + curTable
  382. dialogSubtitle = "字段名只允许字母/数字/下划线;新增列不会影响已有数据"
  383. dialogOkText = "添加字段"
  384. } else {
  385. dialogTitle = "重命名字段 · " + name
  386. dialogSubtitle = fieldHint(curTable, name) + "。改名后引用该列的代码需同步调整"
  387. dialogOkText = "确认改名"
  388. }
  389. dialogMode = "column"
  390. overlay.visible = true
  391. }
  392. function applyColumnEdit() {
  393. var name = formValues["name"]
  394. var dflt = formValues["dflt"]
  395. if (columnMode === "add") {
  396. if (db.addColumn(curTable, name, columnTypeNew, dflt)) {
  397. overlay.visible = false
  398. toast("已添加字段:" + name)
  399. reloadAll()
  400. } else {
  401. toast("添加失败:" + db.lastError(), false)
  402. }
  403. } else {
  404. if (db.renameColumn(curTable, columnNameOld, name)) {
  405. overlay.visible = false
  406. toast("已重命名:" + columnNameOld + " → " + name)
  407. reloadAll()
  408. } else {
  409. toast("重命名失败:" + db.lastError(), false)
  410. }
  411. }
  412. }
  413. function confirmDeleteRow() {
  414. if (selectedRow < 0 || selectedRow >= rows.length) {
  415. toast("先在左侧行号上点一下,选中要删除的行", false)
  416. return
  417. }
  418. var indent = rows[selectedRow].id !== undefined ? ("id=" + rows[selectedRow].id) : ("rowid=" + rows[selectedRow].__rowid)
  419. confirmKind = "row"
  420. confirmText = "确定删除 " + curTable + " 中 " + indent + " 这一行吗?删除后不可恢复。"
  421. dialogTitle = "删除整行"
  422. dialogSubtitle = ""
  423. dialogOkText = "确认删除"
  424. dialogMode = "confirm"
  425. overlay.visible = true
  426. }
  427. function confirmDeleteColumn(name) {
  428. confirmKind = "column"
  429. confirmText = "确定删除字段「" + name + "」吗?该列所有数据都会丢失,且不可恢复。"
  430. dialogTitle = "删除字段"
  431. dialogSubtitle = fieldHint(curTable, name)
  432. dialogOkText = "确认删除"
  433. columnNameOld = name
  434. dialogMode = "confirm"
  435. overlay.visible = true
  436. }
  437. function applyConfirm() {
  438. if (confirmKind === "row") {
  439. var rowId = Number(rows[selectedRow].__rowid)
  440. if (db.deleteRow(curTable, rowId)) {
  441. overlay.visible = false
  442. toast("已删除该行")
  443. reloadRows()
  444. } else {
  445. toast("删除失败:" + db.lastError(), false)
  446. }
  447. } else {
  448. if (db.dropColumn(curTable, columnNameOld)) {
  449. overlay.visible = false
  450. toast("已删除字段:" + columnNameOld)
  451. reloadAll()
  452. } else {
  453. toast("删除字段失败:" + db.lastError(), false)
  454. }
  455. }
  456. }
  457. function showTableSql() {
  458. sqlText = db.tableSql(curTable)
  459. dialogTitle = "建表语句 · " + curTable
  460. dialogSubtitle = "数据库中的真实表结构定义,可对照字段说明一起看"
  461. dialogMode = "sql"
  462. overlay.visible = true
  463. }
  464. Rectangle { anchors.fill: parent; color: "#F2F4F7"; z: -1 }
  465. Column {
  466. id: rootCol
  467. anchors.fill: parent
  468. anchors.margins: 10
  469. spacing: 10
  470. // ================= 顶部: 选库 / 选表 / 搜索 / 编辑开关 =================
  471. Rectangle {
  472. id: headerCard
  473. width: parent.width
  474. height: headerCol.height + 24
  475. radius: 12
  476. color: page.cCard
  477. border.color: page.cLine
  478. Column {
  479. id: headerCol
  480. anchors.left: parent.left
  481. anchors.right: parent.right
  482. anchors.top: parent.top
  483. anchors.margins: 12
  484. spacing: 10
  485. // 第一行: 数据库切换 + 当前表信息 + 刷新
  486. Item {
  487. width: parent.width
  488. height: 34
  489. Row {
  490. anchors.left: parent.left
  491. anchors.verticalCenter: parent.verticalCenter
  492. spacing: 8
  493. Text {
  494. height: 32
  495. text: "数据库"
  496. verticalAlignment: Text.AlignVCenter
  497. font.pixelSize: 13
  498. color: page.cSub
  499. }
  500. Repeater {
  501. model: ["config.db", "data.db"]
  502. delegate: DbButton {
  503. text: modelData
  504. compact: true
  505. isCheckedStyle: true
  506. checked: page.curDb === modelData
  507. onClicked: page.openDb(modelData)
  508. }
  509. }
  510. }
  511. Row {
  512. anchors.right: parent.right
  513. anchors.verticalCenter: parent.verticalCenter
  514. spacing: 8
  515. Text {
  516. height: 32
  517. text: page.curTable === "" ? "未选择数据表"
  518. : (page.curTable + " · 共 " + page.totalRows + " 行")
  519. verticalAlignment: Text.AlignVCenter
  520. font.pixelSize: 12
  521. color: page.cSub
  522. }
  523. DbButton {
  524. text: "刷新"
  525. compact: true
  526. onClicked: page.reloadAll()
  527. }
  528. }
  529. }
  530. // 第二行: 数据表切换(横向滚动)
  531. Item {
  532. width: parent.width
  533. height: 32
  534. Text {
  535. id: tableListLabel
  536. anchors.left: parent.left
  537. anchors.verticalCenter: parent.verticalCenter
  538. text: "数据表"
  539. font.pixelSize: 13
  540. color: page.cSub
  541. }
  542. Flickable {
  543. anchors.left: tableListLabel.right
  544. anchors.leftMargin: 10
  545. anchors.right: parent.right
  546. anchors.top: parent.top
  547. anchors.bottom: parent.bottom
  548. contentWidth: tableChips.width
  549. clip: true
  550. flickableDirection: Flickable.HorizontalFlick
  551. boundsBehavior: Flickable.StopAtBounds
  552. Row {
  553. id: tableChips
  554. height: parent.height
  555. spacing: 6
  556. Repeater {
  557. model: page.tableNames
  558. delegate: DbButton {
  559. text: modelData
  560. compact: true
  561. isCheckedStyle: true
  562. checked: page.curTable === modelData
  563. onClicked: {
  564. page.tabIndex = 0
  565. page.selectTable(modelData)
  566. }
  567. }
  568. }
  569. }
  570. }
  571. }
  572. // 第三行: 数据/表结构 切换 + 搜索 + 编辑开关
  573. Item {
  574. width: parent.width
  575. height: 34
  576. Row {
  577. anchors.left: parent.left
  578. anchors.verticalCenter: parent.verticalCenter
  579. spacing: 6
  580. DbButton {
  581. text: "数据"
  582. compact: true
  583. isCheckedStyle: true
  584. checked: page.tabIndex === 0
  585. onClicked: page.tabIndex = 0
  586. }
  587. DbButton {
  588. text: "表结构"
  589. compact: true
  590. isCheckedStyle: true
  591. checked: page.tabIndex === 1
  592. onClicked: page.tabIndex = 1
  593. }
  594. }
  595. Row {
  596. anchors.right: parent.right
  597. anchors.verticalCenter: parent.verticalCenter
  598. spacing: 6
  599. Rectangle {
  600. width: 190
  601. height: 32
  602. radius: 8
  603. color: "#F8FAFC"
  604. border.color: "#CBD5E1"
  605. TextInput {
  606. id: searchInput
  607. anchors.fill: parent
  608. anchors.leftMargin: 10
  609. anchors.rightMargin: 10
  610. verticalAlignment: TextInput.AlignVCenter
  611. font.pixelSize: 13
  612. color: page.cText
  613. clip: true
  614. selectByMouse: true
  615. onAccepted: page.applyKeyword(text)
  616. }
  617. Text {
  618. anchors.left: parent.left
  619. anchors.leftMargin: 10
  620. anchors.verticalCenter: parent.verticalCenter
  621. visible: searchInput.text.length === 0
  622. text: "搜索本表内容(回车)"
  623. font.pixelSize: 13
  624. color: "#9CA3AF"
  625. }
  626. }
  627. DbButton {
  628. text: "搜索"
  629. compact: true
  630. onClicked: page.applyKeyword(searchInput.text)
  631. }
  632. DbButton {
  633. text: "清空"
  634. compact: true
  635. variant: "ghost"
  636. visible: page.keyword !== ""
  637. onClicked: {
  638. searchInput.text = ""
  639. page.applyKeyword("")
  640. }
  641. }
  642. DbButton {
  643. text: page.editEnabled ? "允许编辑:开" : "允许编辑:关"
  644. compact: true
  645. isCheckedStyle: true
  646. checked: page.editEnabled
  647. onClicked: {
  648. page.editEnabled = !page.editEnabled
  649. page.toast(page.editEnabled ? "已开启编辑,点击单元格即可修改"
  650. : "已关闭编辑,单元格为只读")
  651. }
  652. }
  653. }
  654. }
  655. }
  656. }
  657. // ================= 内容卡片 =================
  658. Rectangle {
  659. id: contentCard
  660. width: parent.width
  661. height: rootCol.height - headerCard.height - rootCol.spacing
  662. radius: 12
  663. color: page.cCard
  664. border.color: page.cLine
  665. clip: true
  666. // ================= 数据页 =================
  667. Item {
  668. id: dataView
  669. anchors.fill: parent
  670. visible: page.tabIndex === 0
  671. Item {
  672. id: gridArea
  673. anchors.left: parent.left
  674. anchors.right: parent.right
  675. anchors.top: parent.top
  676. anchors.bottom: dataFooter.top
  677. clip: true
  678. // 表头: 中文名 + 字段名, 右边缘可拖动调整列宽
  679. Rectangle {
  680. id: gridHeader
  681. width: parent.width
  682. height: 46
  683. color: page.cHeader
  684. clip: true
  685. Row {
  686. id: gridHeaderRow
  687. x: -gridFlick.contentX
  688. height: parent.height
  689. Rectangle {
  690. width: 46
  691. height: gridHeader.height
  692. color: "transparent"
  693. Text {
  694. anchors.centerIn: parent
  695. text: "#"
  696. font.pixelSize: 12
  697. color: page.cSub
  698. }
  699. }
  700. Repeater {
  701. model: page.colKeys.length
  702. delegate: Rectangle {
  703. property int ci: index
  704. width: page.colWidths[ci] !== undefined ? page.colWidths[ci] : 120
  705. height: gridHeader.height
  706. color: "transparent"
  707. Column {
  708. anchors.left: parent.left
  709. anchors.leftMargin: 8
  710. anchors.right: parent.right
  711. anchors.rightMargin: 8
  712. anchors.verticalCenter: parent.verticalCenter
  713. spacing: 1
  714. Text {
  715. width: parent.width
  716. text: page.colLabels[ci]
  717. font.pixelSize: 12
  718. font.bold: true
  719. color: page.cText
  720. elide: Text.ElideRight
  721. }
  722. Text {
  723. width: parent.width
  724. text: page.colKeys[ci]
  725. font.pixelSize: 10
  726. color: page.cSub
  727. elide: Text.ElideRight
  728. }
  729. }
  730. Rectangle {
  731. width: 8
  732. anchors.right: parent.right
  733. anchors.top: parent.top
  734. anchors.bottom: parent.bottom
  735. color: "transparent"
  736. MouseArea {
  737. anchors.fill: parent
  738. cursorShape: Qt.SplitHCursor
  739. property real pressX: 0
  740. onPressed: pressX = mouseX
  741. onPositionChanged: {
  742. if (pressed)
  743. page.setColWidth(ci, page.colWidths[ci] + (mouse.x - pressX))
  744. }
  745. }
  746. }
  747. }
  748. }
  749. }
  750. Rectangle {
  751. anchors.bottom: parent.bottom
  752. width: parent.width
  753. height: 1
  754. color: page.cLine
  755. }
  756. }
  757. // 数据行
  758. Flickable {
  759. id: gridFlick
  760. anchors.left: parent.left
  761. anchors.right: parent.right
  762. anchors.top: gridHeader.bottom
  763. anchors.bottom: parent.bottom
  764. contentWidth: Math.max(gridContent.width, width)
  765. contentHeight: Math.max(gridContent.height, height)
  766. clip: true
  767. boundsBehavior: Flickable.StopAtBounds
  768. Column {
  769. id: gridContent
  770. Repeater {
  771. model: page.rows
  772. delegate: Rectangle {
  773. id: rowItem
  774. property var rowData: modelData
  775. property int rowIndex: index
  776. width: Math.max(rowCells.width, gridFlick.width)
  777. height: 38
  778. color: page.selectedRow === rowIndex ? page.cSelected
  779. : (rowIndex % 2 === 0 ? "#FFFFFF" : page.cRowAlt)
  780. Row {
  781. id: rowCells
  782. height: rowItem.height
  783. Rectangle {
  784. width: 46
  785. height: rowItem.height
  786. color: "transparent"
  787. Text {
  788. anchors.centerIn: parent
  789. text: page.pageIndex * page.pageSize + rowItem.rowIndex + 1
  790. font.pixelSize: 11
  791. font.bold: page.selectedRow === rowItem.rowIndex
  792. color: page.selectedRow === rowItem.rowIndex ? page.cAccent : page.cSub
  793. }
  794. MouseArea {
  795. anchors.fill: parent
  796. cursorShape: Qt.PointingHandCursor
  797. onClicked: page.selectedRow =
  798. (page.selectedRow === rowItem.rowIndex ? -1 : rowItem.rowIndex)
  799. }
  800. }
  801. Repeater {
  802. model: page.colKeys.length
  803. delegate: Rectangle {
  804. property int ci: index
  805. width: page.colWidths[ci] !== undefined ? page.colWidths[ci] : 120
  806. height: rowItem.height
  807. color: "transparent"
  808. Rectangle {
  809. anchors.right: parent.right
  810. anchors.top: parent.top
  811. anchors.bottom: parent.bottom
  812. width: 1
  813. color: "#F1F3F6"
  814. }
  815. Rectangle {
  816. anchors.left: parent.left
  817. anchors.right: parent.right
  818. anchors.bottom: parent.bottom
  819. height: 1
  820. color: "#F1F3F6"
  821. }
  822. Text {
  823. anchors.fill: parent
  824. anchors.leftMargin: 8
  825. anchors.rightMargin: 8
  826. verticalAlignment: Text.AlignVCenter
  827. elide: Text.ElideRight
  828. font.pixelSize: 12
  829. color: page.cText
  830. text: page.cellText(rowItem.rowData, page.colKeys[ci])
  831. }
  832. Text {
  833. anchors.fill: parent
  834. anchors.leftMargin: 8
  835. verticalAlignment: Text.AlignVCenter
  836. visible: page.cellText(rowItem.rowData, page.colKeys[ci]) === ""
  837. text: "空"
  838. font.pixelSize: 11
  839. font.italic: true
  840. color: "#C3C9D4"
  841. }
  842. MouseArea {
  843. anchors.fill: parent
  844. enabled: page.editEnabled
  845. cursorShape: page.editEnabled ? Qt.PointingHandCursor : Qt.ArrowCursor
  846. onClicked: page.openCellEditor(rowItem.rowData, page.colKeys[ci])
  847. }
  848. }
  849. }
  850. }
  851. }
  852. }
  853. }
  854. }
  855. Text {
  856. anchors.centerIn: parent
  857. visible: page.rows.length === 0
  858. horizontalAlignment: Text.AlignHCenter
  859. text: page.curTable === "" ? "请选择数据表"
  860. : (page.keyword !== "" ? "没有匹配「" + page.keyword + "」的记录"
  861. : "该表暂无数据,可点下方「新增行」")
  862. font.pixelSize: 13
  863. color: page.cSub
  864. }
  865. }
  866. // 底部操作条: 新增 / 删除 / 分页
  867. Rectangle {
  868. id: dataFooter
  869. anchors.left: parent.left
  870. anchors.right: parent.right
  871. anchors.bottom: parent.bottom
  872. height: 50
  873. color: page.cCard
  874. Rectangle {
  875. anchors.top: parent.top
  876. width: parent.width
  877. height: 1
  878. color: page.cLine
  879. }
  880. Row {
  881. anchors.left: parent.left
  882. anchors.leftMargin: 12
  883. anchors.verticalCenter: parent.verticalCenter
  884. spacing: 8
  885. DbButton {
  886. text: "新增行"
  887. compact: true
  888. variant: "primary"
  889. onClicked: page.openRowEditor()
  890. }
  891. DbButton {
  892. text: "删除选中行"
  893. compact: true
  894. variant: "danger"
  895. enabled: page.selectedRow >= 0
  896. onClicked: page.confirmDeleteRow()
  897. }
  898. DbButton {
  899. text: page.selectedRow >= 0 ? "取消选中" : "点行号可选中"
  900. compact: true
  901. variant: "ghost"
  902. onClicked: page.selectedRow = -1
  903. }
  904. }
  905. Row {
  906. anchors.right: parent.right
  907. anchors.rightMargin: 12
  908. anchors.verticalCenter: parent.verticalCenter
  909. spacing: 6
  910. Text {
  911. height: 32
  912. verticalAlignment: Text.AlignVCenter
  913. text: "第 " + (page.pageIndex + 1) + " / " + page.totalPages() + " 页"
  914. font.pixelSize: 12
  915. color: page.cSub
  916. }
  917. DbButton {
  918. text: "上一页"
  919. compact: true
  920. enabled: page.pageIndex > 0
  921. onClicked: page.goPage(-1)
  922. }
  923. DbButton {
  924. text: "下一页"
  925. compact: true
  926. enabled: page.pageIndex + 1 < page.totalPages()
  927. onClicked: page.goPage(1)
  928. }
  929. Repeater {
  930. model: [20, 50, 100]
  931. delegate: DbButton {
  932. text: modelData + "行"
  933. compact: true
  934. isCheckedStyle: true
  935. checked: page.pageSize === modelData
  936. onClicked: {
  937. page.pageSize = modelData
  938. page.pageIndex = 0
  939. page.reloadRows()
  940. }
  941. }
  942. }
  943. }
  944. }
  945. }
  946. // ================= 表结构页 =================
  947. Item {
  948. id: structView
  949. anchors.fill: parent
  950. visible: page.tabIndex === 1
  951. ListView {
  952. id: structList
  953. anchors.left: parent.left
  954. anchors.right: parent.right
  955. anchors.top: parent.top
  956. anchors.bottom: structFooter.top
  957. clip: true
  958. model: page.schema
  959. boundsBehavior: Flickable.StopAtBounds
  960. delegate: Rectangle {
  961. width: structList.width
  962. height: 80
  963. color: index % 2 === 0 ? "#FFFFFF" : page.cRowAlt
  964. Rectangle {
  965. anchors.left: parent.left
  966. anchors.right: parent.right
  967. anchors.bottom: parent.bottom
  968. height: 1
  969. color: "#F1F3F6"
  970. }
  971. Column {
  972. anchors.left: parent.left
  973. anchors.leftMargin: 14
  974. anchors.right: actionRow.left
  975. anchors.rightMargin: 10
  976. anchors.verticalCenter: parent.verticalCenter
  977. spacing: 5
  978. Row {
  979. height: 22
  980. spacing: 8
  981. Text {
  982. height: 22
  983. verticalAlignment: Text.AlignVCenter
  984. text: modelData.name
  985. font.pixelSize: 14
  986. font.bold: true
  987. color: page.cText
  988. }
  989. Rectangle {
  990. height: 18
  991. width: typeText.implicitWidth + 12
  992. radius: 9
  993. color: "#EEF2F7"
  994. Text {
  995. id: typeText
  996. anchors.centerIn: parent
  997. text: modelData.type === "" ? "未声明类型" : modelData.type
  998. font.pixelSize: 10
  999. color: page.cSub
  1000. }
  1001. }
  1002. Rectangle {
  1003. height: 18
  1004. width: pkText.implicitWidth + 12
  1005. radius: 9
  1006. visible: modelData.pk > 0
  1007. color: "#E8F0FE"
  1008. Text {
  1009. id: pkText
  1010. anchors.centerIn: parent
  1011. text: "主键"
  1012. font.pixelSize: 10
  1013. color: page.cAccent
  1014. }
  1015. }
  1016. Rectangle {
  1017. height: 18
  1018. width: nnText.implicitWidth + 12
  1019. radius: 9
  1020. visible: modelData.notnull === 1
  1021. color: "#FEF3E2"
  1022. Text {
  1023. id: nnText
  1024. anchors.centerIn: parent
  1025. text: "非空"
  1026. font.pixelSize: 10
  1027. color: "#B45309"
  1028. }
  1029. }
  1030. Text {
  1031. height: 22
  1032. verticalAlignment: Text.AlignVCenter
  1033. visible: modelData.dflt !== ""
  1034. text: "默认值:" + modelData.dflt
  1035. font.pixelSize: 11
  1036. color: page.cSub
  1037. }
  1038. }
  1039. Text {
  1040. width: parent.width
  1041. text: page.fieldLabel(page.curTable, modelData.name)
  1042. + " · " + page.fieldHint(page.curTable, modelData.name)
  1043. font.pixelSize: 12
  1044. color: page.cSub
  1045. elide: Text.ElideRight
  1046. }
  1047. }
  1048. Row {
  1049. id: actionRow
  1050. anchors.right: parent.right
  1051. anchors.rightMargin: 12
  1052. anchors.verticalCenter: parent.verticalCenter
  1053. spacing: 6
  1054. DbButton {
  1055. text: "重命名"
  1056. compact: true
  1057. onClicked: page.openColumnEditor("rename", modelData.name, modelData.type)
  1058. }
  1059. DbButton {
  1060. text: "删除"
  1061. compact: true
  1062. variant: "danger"
  1063. onClicked: page.confirmDeleteColumn(modelData.name)
  1064. }
  1065. }
  1066. }
  1067. }
  1068. Text {
  1069. anchors.centerIn: parent
  1070. visible: page.schema.length === 0
  1071. text: page.curTable === "" ? "请选择数据表" : "没有读取到字段信息"
  1072. font.pixelSize: 13
  1073. color: page.cSub
  1074. }
  1075. Rectangle {
  1076. id: structFooter
  1077. anchors.left: parent.left
  1078. anchors.right: parent.right
  1079. anchors.bottom: parent.bottom
  1080. height: 50
  1081. color: page.cCard
  1082. Rectangle {
  1083. anchors.top: parent.top
  1084. width: parent.width
  1085. height: 1
  1086. color: page.cLine
  1087. }
  1088. Row {
  1089. anchors.left: parent.left
  1090. anchors.leftMargin: 12
  1091. anchors.verticalCenter: parent.verticalCenter
  1092. spacing: 10
  1093. DbButton {
  1094. text: "新增字段"
  1095. compact: true
  1096. variant: "primary"
  1097. onClicked: page.openColumnEditor("add", "", "TEXT")
  1098. }
  1099. Text {
  1100. height: 32
  1101. verticalAlignment: Text.AlignVCenter
  1102. text: "字段后面的说明为该列的中文含义,删除/改名会立即修改数据库结构"
  1103. font.pixelSize: 11
  1104. color: page.cSub
  1105. }
  1106. }
  1107. Row {
  1108. anchors.right: parent.right
  1109. anchors.rightMargin: 12
  1110. anchors.verticalCenter: parent.verticalCenter
  1111. DbButton {
  1112. text: "查看建表语句"
  1113. compact: true
  1114. onClicked: page.showTableSql()
  1115. }
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. // ================= 弹窗(编辑单元格 / 新增行 / 字段结构 / 确认删除 / SQL) =================
  1122. Rectangle {
  1123. id: overlay
  1124. anchors.fill: parent
  1125. color: "#66000000"
  1126. visible: false
  1127. z: 1000
  1128. MouseArea {
  1129. anchors.fill: parent
  1130. onClicked: overlay.visible = false
  1131. }
  1132. Rectangle {
  1133. id: dialogCard
  1134. width: Math.min(parent.width - 32, 480)
  1135. height: Math.min(dialogCol.height + 28, page.height - 60)
  1136. anchors.centerIn: parent
  1137. radius: 14
  1138. color: "#FFFFFF"
  1139. border.color: page.cLine
  1140. MouseArea { anchors.fill: parent }
  1141. Flickable {
  1142. anchors.fill: parent
  1143. anchors.margins: 14
  1144. contentWidth: width
  1145. contentHeight: dialogCol.height
  1146. clip: true
  1147. boundsBehavior: Flickable.StopAtBounds
  1148. Column {
  1149. id: dialogCol
  1150. width: parent.width
  1151. spacing: 10
  1152. Text {
  1153. width: parent.width
  1154. text: page.dialogTitle
  1155. font.pixelSize: 16
  1156. font.bold: true
  1157. color: page.cText
  1158. wrapMode: Text.Wrap
  1159. }
  1160. Text {
  1161. width: parent.width
  1162. visible: page.dialogSubtitle !== ""
  1163. text: page.dialogSubtitle
  1164. font.pixelSize: 12
  1165. color: page.cSub
  1166. wrapMode: Text.Wrap
  1167. }
  1168. // ---- 单元格编辑 ----
  1169. Column {
  1170. width: parent.width
  1171. spacing: 8
  1172. visible: page.dialogMode === "cell"
  1173. Text {
  1174. width: parent.width
  1175. text: page.editHint
  1176. font.pixelSize: 12
  1177. color: page.cSub
  1178. wrapMode: Text.Wrap
  1179. }
  1180. Rectangle {
  1181. width: parent.width
  1182. height: 46
  1183. radius: 8
  1184. color: "#F8FAFC"
  1185. border.color: "#CBD5E1"
  1186. TextInput {
  1187. id: cellInput
  1188. anchors.fill: parent
  1189. anchors.leftMargin: 10
  1190. anchors.rightMargin: 10
  1191. verticalAlignment: TextInput.AlignVCenter
  1192. font.pixelSize: 14
  1193. color: page.cText
  1194. selectByMouse: true
  1195. onAccepted: page.applyCellEdit()
  1196. }
  1197. }
  1198. }
  1199. // ---- 新增行 ----
  1200. Column {
  1201. width: parent.width
  1202. spacing: 10
  1203. visible: page.dialogMode === "row"
  1204. Repeater {
  1205. model: page.formRows
  1206. delegate: Column {
  1207. width: dialogCol.width
  1208. spacing: 4
  1209. Row {
  1210. height: 20
  1211. spacing: 6
  1212. Text {
  1213. height: 20
  1214. verticalAlignment: Text.AlignVCenter
  1215. text: modelData
  1216. font.pixelSize: 12
  1217. font.bold: true
  1218. color: page.cText
  1219. }
  1220. Text {
  1221. height: 20
  1222. verticalAlignment: Text.AlignVCenter
  1223. text: page.fieldLabel(page.curTable, modelData)
  1224. font.pixelSize: 12
  1225. color: page.cAccent
  1226. }
  1227. Text {
  1228. height: 20
  1229. verticalAlignment: Text.AlignVCenter
  1230. text: page.schemaOf(modelData).notnull === 1 ? "必填" : "可留空"
  1231. font.pixelSize: 11
  1232. color: page.schemaOf(modelData).notnull === 1 ? page.cDanger : page.cSub
  1233. }
  1234. }
  1235. Rectangle {
  1236. width: parent.width
  1237. height: 38
  1238. radius: 6
  1239. color: "#F8FAFC"
  1240. border.color: "#CBD5E1"
  1241. TextInput {
  1242. anchors.fill: parent
  1243. anchors.margins: 8
  1244. font.pixelSize: 13
  1245. color: page.cText
  1246. selectByMouse: true
  1247. Component.onCompleted: text = page.formValueOf(modelData)
  1248. onTextChanged: page.setFormValue(modelData, text)
  1249. }
  1250. }
  1251. }
  1252. }
  1253. }
  1254. // ---- 新增/重命名字段 ----
  1255. Column {
  1256. width: parent.width
  1257. spacing: 8
  1258. visible: page.dialogMode === "column"
  1259. Text {
  1260. text: "字段名(英文,字母/数字/下划线)"
  1261. font.pixelSize: 12
  1262. color: page.cSub
  1263. }
  1264. Rectangle {
  1265. width: parent.width
  1266. height: 40
  1267. radius: 6
  1268. color: "#F8FAFC"
  1269. border.color: "#CBD5E1"
  1270. TextInput {
  1271. id: columnNameInput
  1272. anchors.fill: parent
  1273. anchors.margins: 8
  1274. font.pixelSize: 13
  1275. color: page.cText
  1276. selectByMouse: true
  1277. onTextChanged: page.formValues["name"] = text
  1278. }
  1279. }
  1280. Text {
  1281. visible: page.columnMode === "add"
  1282. text: "字段类型"
  1283. font.pixelSize: 12
  1284. color: page.cSub
  1285. }
  1286. Row {
  1287. visible: page.columnMode === "add"
  1288. spacing: 6
  1289. Repeater {
  1290. model: ["TEXT", "INTEGER", "REAL"]
  1291. delegate: DbButton {
  1292. text: modelData
  1293. compact: true
  1294. isCheckedStyle: true
  1295. checked: page.columnTypeNew === modelData
  1296. onClicked: page.columnTypeNew = modelData
  1297. }
  1298. }
  1299. }
  1300. Text {
  1301. visible: page.columnMode === "add"
  1302. text: "默认值(可留空)"
  1303. font.pixelSize: 12
  1304. color: page.cSub
  1305. }
  1306. Rectangle {
  1307. width: parent.width
  1308. height: 40
  1309. radius: 6
  1310. visible: page.columnMode === "add"
  1311. color: "#F8FAFC"
  1312. border.color: "#CBD5E1"
  1313. TextInput {
  1314. id: columnDefaultInput
  1315. anchors.fill: parent
  1316. anchors.margins: 8
  1317. font.pixelSize: 13
  1318. color: page.cText
  1319. selectByMouse: true
  1320. onTextChanged: page.formValues["dflt"] = text
  1321. }
  1322. }
  1323. Text {
  1324. width: parent.width
  1325. visible: page.columnMode === "add"
  1326. text: "提示:SQLite 新增列不能带 NOT NULL 约束;需要更复杂的结构请用 SQL 工具调整。"
  1327. font.pixelSize: 11
  1328. color: page.cSub
  1329. wrapMode: Text.Wrap
  1330. }
  1331. }
  1332. // ---- 确认删除 ----
  1333. Text {
  1334. width: parent.width
  1335. visible: page.dialogMode === "confirm"
  1336. text: page.confirmText
  1337. font.pixelSize: 13
  1338. color: page.cText
  1339. wrapMode: Text.Wrap
  1340. }
  1341. // ---- 建表语句 ----
  1342. Text {
  1343. width: parent.width
  1344. visible: page.dialogMode === "sql"
  1345. text: page.sqlText
  1346. font.pixelSize: 12
  1347. font.family: "monospace"
  1348. color: page.cText
  1349. wrapMode: Text.WrapAnywhere
  1350. }
  1351. Row {
  1352. width: parent.width
  1353. spacing: 8
  1354. Item {
  1355. width: Math.max(0, parent.width - 300)
  1356. height: 1
  1357. }
  1358. DbButton {
  1359. text: "关闭"
  1360. width: 80
  1361. visible: page.dialogMode === "sql"
  1362. onClicked: overlay.visible = false
  1363. }
  1364. DbButton {
  1365. text: "取消"
  1366. width: 80
  1367. visible: page.dialogMode !== "sql"
  1368. onClicked: overlay.visible = false
  1369. }
  1370. DbButton {
  1371. text: page.dialogOkText
  1372. width: 110
  1373. visible: page.dialogMode !== "sql"
  1374. variant: page.dialogMode === "confirm" ? "danger" : "primary"
  1375. onClicked: page.applyDialog()
  1376. }
  1377. }
  1378. }
  1379. }
  1380. }
  1381. }
  1382. // ================= 轻提示 =================
  1383. Rectangle {
  1384. id: toastBox
  1385. anchors.horizontalCenter: parent.horizontalCenter
  1386. anchors.bottom: parent.bottom
  1387. anchors.bottomMargin: 18
  1388. width: Math.min(parent.width - 40, toastLabel.implicitWidth + 32)
  1389. height: 40
  1390. radius: 20
  1391. color: page.toastOk ? "#1F2937" : "#B91C1C"
  1392. opacity: page.toastText === "" ? 0 : 0.96
  1393. visible: opacity > 0
  1394. z: 2000
  1395. Behavior on opacity { NumberAnimation { duration: 140 } }
  1396. Text {
  1397. id: toastLabel
  1398. anchors.centerIn: parent
  1399. width: Math.min(parent.width - 32, implicitWidth)
  1400. text: page.toastText
  1401. color: "#FFFFFF"
  1402. font.pixelSize: 13
  1403. horizontalAlignment: Text.AlignHCenter
  1404. elide: Text.ElideRight
  1405. }
  1406. }
  1407. Timer {
  1408. id: toastTimer
  1409. interval: 2200
  1410. onTriggered: page.toastText = ""
  1411. }
  1412. // ================= 弹窗统一提交 =================
  1413. function applyDialog() {
  1414. if (dialogMode === "cell") page.applyCellEdit()
  1415. else if (dialogMode === "row") page.applyRowInsert()
  1416. else if (dialogMode === "column") page.applyColumnEdit()
  1417. else if (dialogMode === "confirm") page.applyConfirm()
  1418. }
  1419. // 新增行表单: formRows 每次重新赋值 -> Repeater 重建, 输入框回到初始值
  1420. function schemaOf(name) {
  1421. for (var i = 0; i < schema.length; ++i) {
  1422. if (schema[i].name === name) return schema[i]
  1423. }
  1424. return { name: name, type: "", notnull: 0, dflt: "", pk: 0 }
  1425. }
  1426. function formValueOf(name) {
  1427. var v = formValues[name]
  1428. return v === undefined ? "" : v
  1429. }
  1430. Component.onCompleted: page.openDb(page.curDb)
  1431. Component.onDestruction: db.closeDatabase()
  1432. }