Main.qml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. import QtQuick
  2. import Felgo
  3. import QtQuick.Controls
  4. import "pages"
  5. import "common"
  6. import "logic"
  7. import "model"
  8. App {
  9. id: app
  10. licenseKey: "6BBDF824AF729C33ECAA6DC917319EDB8A7F76A9EB5217C380F81C28203CF84078DBCEEC5DB44C7DB60373C0FFE65240DA17EAC1C8376F5939F26782D2A3110CFCDB938C557DDAC8C63F942AF917CD7BCD4235973E76002E69BE740BD2FE99BB7F22AD12408FB5EE3D3A538E604803F0947C372A8486E63B5CF101A18B6CEDF7AEDC615273B15ADBCF1DCC54F123FA093609F650378D2F2BC11C74421663CAB7C178C4123AF6C6B6E44EE45A5A790448F85D4039D7EB1C23C3CA97B42E0724B2987C185114E732662A4EFD507EDC0E5DC2DA7A9FF7754678F48BFC8AAFD40B21926B5A1FD427E279FDE44EC0315229371F4D77B918CC4663CB71572051E1E84DCBF8E183C44981C9D921DB7C5A02879E2D9352ECEC03FF21D99F941B9E31E10D6D625D2603135C101658FEA17FB7B397"
  11. property bool themeReady: false
  12. // ========== 主题相关 ==========
  13. property string theme_md: "day"
  14. property string theme_select: "day"
  15. property color theme_backgroundColor: theme_select === "day" ? "#d8d8d8" : "#1a1a1a"
  16. property color cardColor: theme_select == "day" ? "#f5f5f5" :(theme_select == "night" ? "#2b2b2b":"#f5f5f5") //背景色 //前景色
  17. property color fontColor: theme_select == "day" ? "black" :(theme_select == "night" ? "white":"black")
  18. property bool floatPanel: false
  19. property bool showTxData: false
  20. property bool showRxData: false
  21. property bool showPubData: false
  22. property bool showSubData: false
  23. property string floatPanel_title: "串口数据"
  24. property string txDataLog: ""
  25. property string rxDataLog: ""
  26. // 初始为纯黑色,强制设置为不透明,防止花屏
  27. color: "#000000"
  28. property string boardversionText: ""
  29. property string appversionText: ""
  30. property bool tempProtectionState: false // 温度保护状态,true表示处于保护状态
  31. property int logoutcnt
  32. signal logoutClicked
  33. // ========== 主题初始化(最早执行) ==========
  34. onInitTheme: {
  35. console.log("onInitTheme")
  36. // 1. 先读取保存的主题,再设置任何颜色
  37. var savedMode = "day"
  38. try {
  39. savedMode = settings.getValue("theme_mode")
  40. if (savedMode === undefined || savedMode === "" || savedMode === null) {
  41. savedMode = "day"
  42. settings.setValue("theme_mode", "day")
  43. }
  44. } catch(e) {
  45. savedMode = "day"
  46. }
  47. app.theme_md = savedMode
  48. if (savedMode !== "auto") {
  49. app.theme_select = savedMode
  50. } else {
  51. var h = new Date().getHours()
  52. app.theme_select = (h >= 6 && h <= 18) ? "day" : "night"
  53. }
  54. // 2. 根据主题设置颜色(此时颜色属性会自动更新)
  55. // 无需手动赋值,绑定表达式会自动计算
  56. // 3. 设置Felgo主题
  57. Theme.platform = "ios"
  58. // TabBar样式:统一由 applyNavTabStyle() 设置(与 32 位工程 app_bzy_armhf_7 保持一致),
  59. // 避免主题切换时这里和 ProfilePage 各写一套、样式互相覆盖
  60. applyNavTabStyle()
  61. // 先按横排给个默认厚度,Navigation 起来后再用真实几何复算
  62. applyNavTabMetrics(0, 0)
  63. // NavigationBar样式
  64. Theme.navigationBar.backgroundColor = app.cardColor
  65. Theme.navigationBar.titleColor = app.fontColor
  66. if (!Theme.navigationBar.height) {
  67. Theme.navigationBar.height = dp(56)
  68. }
  69. if (!Theme.statusBarHeight) {
  70. Theme.statusBarHeight = dp(24)
  71. }
  72. // 4. 标记主题就绪
  73. themeReady = true
  74. }
  75. // ========== 导航项(tab)样式:唯一的样式出口 ==========
  76. // 目标:把 tab 从"小图标+小字"改成"明确的按钮/页面切换器":
  77. // ① 不显示图标(iconSize=0 与 showIcon=false 双保险),只留文字,字号加大;
  78. // ② 提高 tab 条高度/宽度,文字与边缘留出呼吸感,看起来像一排按钮;
  79. // ③ 三个状态给出不同颜色:选中(主题绿) / 未选中(弱化灰) / 按下·悬停(亮绿);
  80. // ④ 颜色全部跟随 app.theme_select,日间/夜间以及运行时切换主题都保持一致。
  81. function applyNavTabStyle() {
  82. Theme.navigationTabBar.textSize = Theme.isAndroid ? 18 : 17
  83. Theme.navigationTabBar.iconSize = 0 // 兜底:即使某处仍画图标也看不见
  84. // 【关键】Felgo 的 tab 栏从这里开始"隐身":底色和分隔线一律透明,
  85. // 只保留它的占位(内容区高度、tabPosition、页面切换机制都不变)。
  86. // 可见部分由下面的 navBarOverlay 自绘 —— 真机上验证:叠加在 tab 栏上的轮廓层不显示、
  87. // dividerColor 也不画竖线(Felgo 的 TabBar 根本不画分隔线),
  88. // 与其继续和 C++ 绘制的 tab 栏较劲,不如让它彻底透明、外观全部自己画。
  89. Theme.navigationTabBar.backgroundColor = "transparent"
  90. Theme.navigationTabBar.dividerColor = "transparent"
  91. // 文字颜色仍然保留(不设透明):这一层是"兜底"——
  92. // 万一 navBarOverlay 因任何原因没显示,导航项至少还有可读文字和选中状态,
  93. // 不会出现"底部一片空白、点了也不知道在哪一页"。
  94. // 叠加层正常显示时,它的不透明底色会把这里的文字完全盖住,不会重影。
  95. Theme.navigationTabBar.titleColor = app.theme_select === "day" ? "#2e7d32" : "#81c784"
  96. Theme.navigationTabBar.titleOffColor = app.theme_select === "day" ? "#757575" : "#9e9e9e"
  97. Theme.navigationTabBar.titlePressedColor = "#4caf50"
  98. Theme.navigationTabBar.titleDisabledColor = app.theme_select === "day" ? "#c0c0c0" : "#5a5a5a"
  99. Theme.navigationTabBar.showOnlySelectedLabel = false
  100. }
  101. // tab 条"厚度":这个字段在横排时是高度、竖排时是宽度,靠 tabPosition 判断不可靠
  102. // (两个工程都写 Navigation.Left,实际 Felgo 渲染在底部),所以用 tabBar 的真实几何:
  103. // width > height 即横排 → 给 52/56 的按钮高度;否则按竖排宽度给 96,保证四字标题不被截断。
  104. function applyNavTabMetrics(barW, barH) {
  105. var horizontal = (barW > 0 && barH > 0) ? (barW > barH) : true
  106. Theme.navigationTabBar.height = horizontal
  107. ? dp(Theme.isAndroid ? 56 : 52)
  108. : dp(96)
  109. }
  110. // 新增一个函数来接收 HomePage 发送的文本
  111. function bvReceived(datbv){
  112. boardversionText = datbv
  113. }
  114. function appReceived(datbv){
  115. appversionText = datbv
  116. }
  117. onLogoutClicked: {
  118. logic.logout()
  119. // jump to main page after logout
  120. navigation.currentIndex = 0
  121. //navigation.currentNavigationItem.navigationStack.popAllExceptFirst()
  122. }
  123. //business logic}
  124. Logic {
  125. id: logic
  126. }
  127. // model
  128. DataModel {
  129. id: dataModel
  130. dispatcher: logic // data model handles actions sent by logic
  131. }
  132. // view
  133. Navigation {
  134. objectName: "Navigation_main"
  135. id: navigation
  136. enabled: themeReady
  137. navigationMode: Theme.isAndroid ? navigationModeTabs : navigationModeDefault
  138. drawerLogoBackgroundColor: "red"
  139. opacity: themeReady ? 1.0 : 0.0
  140. Behavior on opacity { NumberAnimation { duration: 200 } }
  141. // ---- 导航项改为"纯文字按钮"----
  142. // showIcon: false 是 Felgo TabControl 的开关,关掉后 tab 只留文字(图标不再占位置);
  143. // textFont 让文字加粗、字号与 Theme.navigationTabBar.textSize 一致,按钮感更强。
  144. tabs.showIcon: false
  145. tabs.textFont: Qt.font({ pixelSize: Theme.isAndroid ? 18 : 17, bold: true })
  146. Component.onCompleted: {
  147. console.log("Navigation Component.onCompleted")
  148. // tabBar 已经建出来了,用它的真实宽高判断横排/竖排,再决定 tab 条厚度:
  149. // 靠 tabPosition 猜(写的是 Navigation.Left,实际渲染在底部)会把"宽度"当"高度"用,
  150. // 导致底部导航条被撑到 96px 高。
  151. var tb = tabs ? tabs.tabBar : null
  152. applyNavTabMetrics(tb ? tb.width : 0, tb ? tb.height : 0)
  153. applyNavTabStyle()
  154. initializeNavigation()
  155. }
  156. function initializeNavigation() {
  157. console.log("Initializing navigation...")
  158. for(var i = 0;i < navigation.count; i++){
  159. navigation.currentIndex = i
  160. }
  161. navigation.currentIndex = 0
  162. }
  163. function setCurrentTab(index){
  164. navigation.currentIndex = index
  165. }
  166. // only enable if user is logged in
  167. // login page below overlays navigation then
  168. // first tab
  169. NavigationItem {
  170. id: uiComponentsItem
  171. title: "主界面"
  172. iconType: IconType.home
  173. Loader{
  174. asynchronous: true
  175. active: true
  176. source: "pages/HomePage.qml"
  177. }
  178. }
  179. NavigationItem {
  180. title: "测试维护"
  181. iconType: IconType.gears
  182. objectName: "autofocus_Item"
  183. TestPage {
  184. //id: autofocus_id
  185. }
  186. }
  187. // second tab
  188. NavigationItem {
  189. title: qsTr("图像查看") // use qsTr for strings you want to translate
  190. iconType: IconType.qrcode
  191. NavigationStack {
  192. PhotostitcherPage {
  193. onVisibleChanged: {
  194. if(visible){
  195. console.log("进入图像查看界面,开始初始化加载...")
  196. loadRootDirectory()
  197. }
  198. }
  199. }
  200. }
  201. }
  202. // fourth tab
  203. NavigationItem {
  204. objectName: "systemset_Item"
  205. title: qsTr("系统设置") // use qsTr for strings you want to translate
  206. iconType: IconType.gears
  207. NavigationStack {
  208. initialPage: ProfilePage {
  209. id: page_sys
  210. onThemChange: function(mode) { console.log("配置主题模式:",mode)
  211. app.theme_md = mode
  212. if (mode !== "auto") {
  213. app.theme_select = mode
  214. } else {
  215. var h = new Date().getHours()
  216. app.theme_select = (h >= 6 && h <= 18) ? "day" : "night"
  217. }
  218. app.theme_select = mode
  219. app.theme_md = mode
  220. // 主题切换后重新套用导航项样式:以前这里只改 3 个颜色、
  221. // 还把分隔线设成绿色,会把上面的大小/三态/无图标样式覆盖掉。
  222. app.applyNavTabStyle()
  223. console.log("主题已更新:",app.theme_select)
  224. }
  225. // handle logout
  226. onLogoutClicked: {
  227. //loginpage.pushed()
  228. logic.logout()
  229. // jump to main page after logout
  230. navigation.currentIndex = 0
  231. serial.setLightness(-255)
  232. //navigation.currentNavigationItem.navigationStack.popAllExceptFirst()
  233. }
  234. }
  235. }
  236. }
  237. tabPosition: Navigation.Left
  238. }
  239. // ================= 自绘底部导航条(取代 Felgo tab 栏的可见部分)=================
  240. // 为什么不用 Felgo 的 tab 栏画轮廓:
  241. // 多次实测在真机上叠加层始终不可见(tabBar alias 取不到 → 绑定失效;
  242. // Theme 读回 0 → 高度为 0;层级被 Felgo 压住),而 dividerColor 也不画竖线。
  243. // 与其继续和 C++ 绘制的 tab 栏较劲,不如让它"隐身占位",外观与点击全部自己画:
  244. // · Felgo tab 栏仍在(保证内容区高度、页面切换机制不变),但背景/文字/分隔线全部透明;
  245. // · 这一层叠在它正上方(同一位置、同一高度),画 4 个带边框的按钮 + 文字 + 点击。
  246. // 这样不依赖 mapToItem、不依赖 Theme 读回值,任何 Felgo 版本/设备上都一定看得见。
  247. Item {
  248. id: navBarOverlay
  249. anchors.left: parent.left
  250. anchors.right: parent.right
  251. anchors.bottom: parent.bottom
  252. // 高度与 Felgo tab 栏一致;读不到就兜 dp(52)(横排默认厚度)
  253. height: (typeof Theme.navigationTabBar.height !== "undefined"
  254. && Theme.navigationTabBar.height > 0)
  255. ? Theme.navigationTabBar.height : dp(52)
  256. z: 1000
  257. // 未登录(锁屏/休眠)时导航条一并隐藏:
  258. // 锁屏页本身 z 更高已能盖住它,但隐藏后不会在锁屏淡出瞬间露出可点击的按钮,
  259. // 也省掉一层不必要的绘制。
  260. visible: navigation.count > 0 && dataModel.userLoggedIn
  261. // 标题必须与上面 NavigationItem 的 title 一一对应(改动时两处一起改)
  262. readonly property var titles: ["主界面", "测试维护", "图像查看", "系统设置"]
  263. readonly property int itemCount: Math.min(titles.length, navigation.count)
  264. readonly property int curIndex: Math.max(0, Math.min(navigation.currentIndex, itemCount - 1))
  265. readonly property bool isDay: app.theme_select === "day"
  266. readonly property color accent: isDay ? "#2e7d32" : "#81c784"
  267. readonly property color offText: isDay ? "#5f6368" : "#9e9e9e"
  268. readonly property color frameColor: isDay ? "#38000000" : "#38ffffff"
  269. // 底色:浅绿/深蓝灰,与页面内容拉开色相差(原来和页面同色,看不出导航条)
  270. Rectangle {
  271. anchors.fill: parent
  272. color: navBarOverlay.isDay ? "#e8f5e9" : "#263238"
  273. }
  274. // 与内容区的边界线
  275. Rectangle {
  276. width: parent.width
  277. height: dp(1)
  278. anchors.top: parent.top
  279. color: navBarOverlay.isDay ? "#40000000" : "#40ffffff"
  280. }
  281. Row {
  282. anchors.fill: parent
  283. Repeater {
  284. model: navBarOverlay.itemCount
  285. Item {
  286. id: navBtn
  287. readonly property bool active: index === navBarOverlay.curIndex
  288. width: navBarOverlay.width / Math.max(1, navBarOverlay.itemCount)
  289. height: navBarOverlay.height
  290. // 按钮轮廓:选中绿框+浅填充,未选中淡灰框 —— 每个 tab 都是独立按钮
  291. Rectangle {
  292. anchors.fill: parent
  293. anchors.margins: dp(5)
  294. radius: dp(6)
  295. color: navBtn.active
  296. ? (navBarOverlay.isDay ? "#1a2e7d32" : "#2e81c784")
  297. : "transparent"
  298. border.width: navBtn.active ? dp(1.5) : dp(1)
  299. border.color: navBtn.active ? navBarOverlay.accent
  300. : navBarOverlay.frameColor
  301. }
  302. Text {
  303. anchors.centerIn: parent
  304. text: navBarOverlay.titles[index]
  305. color: navBtn.active ? navBarOverlay.accent : navBarOverlay.offText
  306. font.pixelSize: Theme.isAndroid ? 18 : 17
  307. font.bold: navBtn.active
  308. }
  309. MouseArea {
  310. anchors.fill: parent
  311. // 直接切 currentIndex:Felgo 的页面切换、NavigationStack 行为都不变
  312. onClicked: navigation.currentIndex = index
  313. }
  314. }
  315. }
  316. }
  317. }
  318. // ========== 业务定时器 ==========
  319. Timer{
  320. interval: 1000
  321. running: true
  322. repeat: true
  323. onTriggered: {
  324. if(navigation.currentIndex === 0&&loginpage.logintimeout%5===0){
  325. serial.readBoardData()
  326. }
  327. loginpage.logintimeout--
  328. ProfilePage.boardversion = "123"
  329. console.log("休眠计数:",loginpage.logintimeout)
  330. if(loginpage.logintimeout===0){ //?秒后进入休眠
  331. loginpage.logintimeout = 300
  332. logoutClicked()
  333. //关闭屏幕(亮度设置为0)
  334. serial.setLightness(-255)
  335. }
  336. }
  337. }
  338. Connections{
  339. target: curl
  340. function onDownloadFinished(result) {
  341. if(result){
  342. navigation.setCurrentTab(1)
  343. }
  344. }
  345. }
  346. // 远程校准指令: {"cmd":"slide","ext":{"calibrate":"y","speed":"50"}} → 跳转相机界面(Tab 1 测试维护)
  347. Connections {
  348. target: mqtt
  349. function onSignal_open_camera() {
  350. console.log("[Main] 收到远程相机开启指令,跳转相机界面!")
  351. navigation.setCurrentTab(1)
  352. }
  353. }
  354. Component.onCompleted: {
  355. console.log("App Component.onCompleted, starting phased loading...")
  356. }
  357. LocalNotificationPage {
  358. id: notificationPage
  359. visible:false
  360. }
  361. // ========== 登录页(Phase2: 导航加载完成后才加载) ==========
  362. LoginPage {
  363. id: loginpage
  364. title: "植物病菌孢子捕捉分析系统"
  365. Connections{
  366. target: serial
  367. }
  368. // 锁屏必须盖住自绘导航条(navBarOverlay 是 z:1000 的兄弟节点)。
  369. // 这里给 2000:高于导航条、低于串口调试浮窗(rxFloatingWindow, z:9999)。
  370. z: 2000
  371. visible: opacity > 0
  372. enabled: visible
  373. opacity: dataModel.userLoggedIn ? 0 : 1
  374. Behavior on opacity { NumberAnimation { duration: 250 } } // page fade in/out
  375. }
  376. // ========== 启动过渡(纯色底 + "启动中...") ==========
  377. // 只在"Qt 已起来、锁屏页还没画完"的那一小段显示,提示文字为"启动中..."。
  378. // 退出条件收紧:只等 themeReady(onInitTheme 在 App 创建时同步执行完,通常几十毫秒),
  379. // 最短 500ms 防止一闪而过,1.5 秒硬兜底 —— 不会再像上一版那样顶到 6 秒把锁屏页挡住。
  380. // 用 100ms 轮询 + 一次性淡出动画,而不是给 opacity 绑 Behavior:
  381. // 后者淡出结束后绑定会重新算出 1,出现"淡出后又闪回"。
  382. property bool splashMinElapsed: false
  383. property bool splashForceDone: false
  384. property bool splashFinished: false
  385. Timer {
  386. interval: 500
  387. running: true
  388. onTriggered: app.splashMinElapsed = true
  389. }
  390. Timer {
  391. interval: 1500
  392. running: true
  393. onTriggered: app.splashForceDone = true
  394. }
  395. Timer {
  396. interval: 100
  397. running: !app.splashFinished
  398. repeat: true
  399. onTriggered: {
  400. var ready = app.splashForceDone || (app.splashMinElapsed && app.themeReady)
  401. if (ready) {
  402. running = false
  403. app.splashFinished = true
  404. splashFadeOut.start()
  405. }
  406. }
  407. }
  408. StartupSplash {
  409. id: splashScreen
  410. anchors.fill: parent
  411. z: 3000
  412. night: app.theme_select === "night"
  413. versionText: app.appversionText
  414. }
  415. NumberAnimation {
  416. id: splashFadeOut
  417. target: splashScreen
  418. property: "opacity"
  419. to: 0
  420. duration: 250
  421. easing.type: Easing.OutCubic
  422. onFinished: splashScreen.visible = false
  423. }
  424. Rectangle {
  425. id: rxFloatingWindow
  426. visible: floatPanel
  427. width: 400
  428. height: 300
  429. x: parent.width - 420
  430. y: 20
  431. color: "white"
  432. opacity: 0.8 // 50% 透明度
  433. radius: 10
  434. z: 9999
  435. Column {
  436. anchors.fill: parent
  437. anchors.margins: 10
  438. spacing: 5
  439. Row {
  440. width: parent.width
  441. spacing: 10
  442. AppText {
  443. text: floatPanel_title
  444. color: "#00ffff"
  445. fontSize: 16
  446. font.bold: true
  447. anchors.verticalCenter: appbutton_clear.verticalCenter
  448. }
  449. AppButton {
  450. id: appbutton_clear
  451. text: "清空"
  452. textColor: "white"
  453. backgroundColor: "#ff4444"
  454. flat: false
  455. onClicked: {
  456. rxDataLog = ""
  457. txDataLog = ""
  458. }
  459. }
  460. // AppButton {
  461. // text: "X"
  462. // textColor: "white"
  463. // backgroundColor: "#ff4444"
  464. // flat: false
  465. // onClicked: {
  466. // rxSwitch.checked = false
  467. // }
  468. // }
  469. }
  470. Flickable {
  471. width: parent.width
  472. height: parent.height - 40
  473. contentWidth: width
  474. contentHeight: txt_logdata.implicitHeight
  475. clip: true
  476. AppText {
  477. id: txt_logdata
  478. width: parent.width
  479. text: (showRxData ? rxDataLog : (showTxData ? txDataLog : "")) || (showSubData ? rxDataLog : (showPubData ? txDataLog : ""))
  480. color: "green"
  481. fontSize: 12
  482. wrapMode: Text.WrapAtWordBoundaryOrAnywhere
  483. }
  484. }
  485. }
  486. // 拖拽功能
  487. MouseArea {
  488. anchors.fill: parent
  489. drag.target: rxFloatingWindow
  490. drag.axis: Drag.XAndYAxis
  491. }
  492. }
  493. }