StyleDesignerTab.qml 18.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
import QtQuick
import QtQuick.Controls.Basic
import QtQuick.Dialogs
import QtQuick.Layouts
import "components"
import "." as App

Item {
    id: tab

    // ===== 状态 =====
    property var formData: ({
        "主石形状": "",
        "主石材质": "",
        "金属": "",
        "花头形式": "",
        "戒臂结构": "",
        "戒臂处理": "",
        "辅石镶嵌": "",
        "特殊元素": "",
    })
    property var lockedFields: []           // 被 🔓 锁定的字段名(不参与随机)
    property string assembledPrompt: ""
    property var myTaskIds: []              // 多任务支持:本 tab 提交过且还在跑/排队的 ids
    property string lastTaskId: ""          // 最近一次提交的 task — 进度显示锚点
    property string lastResultPath: ""

    function isMyTask(tid) { return tab.myTaskIds.indexOf(tid) >= 0 }
    function dropMyTask(tid) {
        var copy = tab.myTaskIds.slice()
        var i = copy.indexOf(tid)
        if (i >= 0) copy.splice(i, 1)
        tab.myTaskIds = copy
    }
    property string statusText: "● 就绪"
    property color statusColor: App.Theme.success

    // ➕ 添加词条对话框传参
    property string addingCategory: ""

    function updateField(category, value) {
        var copy = Object.assign({}, tab.formData)
        copy[category] = value || ""
        tab.formData = copy
        tab.assembledPrompt = jewelry.previewPrompt(copy)
    }

    function reassemble() {
        tab.assembledPrompt = jewelry.previewPrompt(tab.formData)
    }

    function isLocked(category) {
        return tab.lockedFields.indexOf(category) >= 0
    }

    function toggleLock(category) {
        var copy = tab.lockedFields.slice()
        var i = copy.indexOf(category)
        if (i >= 0) copy.splice(i, 1)
        else copy.push(category)
        tab.lockedFields = copy
    }

    function randomize() {
        var copy = Object.assign({}, tab.formData)
        for (var i = 0; i < jewelry.categories.length; i++) {
            var cat = jewelry.categories[i]
            if (tab.isLocked(cat)) continue
            var opts = jewelry.getOptions(cat)
            if (opts.length === 0) continue
            copy[cat] = opts[Math.floor(Math.random() * opts.length)]
        }
        tab.formData = copy
        tab.assembledPrompt = jewelry.previewPrompt(copy)
        // 同步 ComboBox 显示(model 列表里找到 currentIndex)
        comboReloadTrigger++
    }

    function resetFields() {
        var fresh = {}
        for (var i = 0; i < jewelry.categories.length; i++) {
            fresh[jewelry.categories[i]] = ""
        }
        tab.formData = fresh
        tab.reassemble()
        comboReloadTrigger++
    }

    function submit() {
        if (tab.assembledPrompt.trim().length === 0) {
            tab.statusText = "● 选几个字段先"
            tab.statusColor = App.Theme.danger
            return
        }
        try {
            // submitStyleTask 而非 submitTask:标记 task.type=STYLE_DESIGN,
            // sidebar 点击该任务时 TaskQueueBridge.loadTask 才知道回填到款式设计 tab
            var taskId = imageGen.submitStyleTask(
                tab.assembledPrompt, [], "1:1", "2K", "慢速模式"
            )
            tab.myTaskIds = tab.myTaskIds.concat([taskId])
            tab.lastTaskId = taskId
            tab.statusText = "● 已提交(我的队列 " + tab.myTaskIds.length + " 条)"
            tab.statusColor = App.Theme.accent
        } catch (e) {
            tab.statusText = "● " + e
            tab.statusColor = App.Theme.danger
        }
    }

    // 触发 ComboBox model 刷新(随机 / 重置 / 词库变化时让 currentIndex 重算)
    property int comboReloadTrigger: 0

    Component.onCompleted: reassemble()

    Connections {
        target: jewelry
        function onLibraryChanged(category) {
            tab.comboReloadTrigger++
        }
    }

    // sidebar 点击任务项 → 回填到本 tab(旧 _load_task_to_main_window 等价)
    // 注意:款式设计的 prompt 是 PromptAssembler 拼出的长串,无法反向解析回 8 字段,
    // 只回填 assembledPrompt 显示 + 已完成的结果图(与旧版 prompt_preview 行为一致)
    Connections {
        target: taskQueue
        function onTaskLoadRequested(payload) {
            if (!payload || payload.type !== "style_design") return
            appState.setTab(1)  // 切到款式设计 tab

            tab.assembledPrompt = payload.prompt || ""

            if (payload.resultPath) {
                tab.lastResultPath = payload.resultPath
            }

            tab.statusText = "● 已加载任务 " + (payload.taskId || "").substring(0, 8)
            tab.statusColor = App.Theme.accent
        }
    }

    Connections {
        target: imageGen
        function onTaskProgress(taskId, progress, msg) {
            if (taskId !== tab.lastTaskId) return
            tab.statusText = "● " + (msg || "生成中…")
            tab.statusColor = App.Theme.accent
        }
        function onTaskCompleted(taskId, resultPath, prompt, model) {
            if (!tab.isMyTask(taskId)) return
            tab.lastResultPath = resultPath
            tab.dropMyTask(taskId)
            if (tab.lastTaskId === taskId) tab.lastTaskId = ""
            tab.statusText = tab.myTaskIds.length === 0
                ? "● 已完成"
                : "● 已完成(队列还剩 " + tab.myTaskIds.length + " 条)"
            tab.statusColor = App.Theme.success
        }
        function onTaskFailed(taskId, error) {
            if (!tab.isMyTask(taskId)) return
            tab.dropMyTask(taskId)
            if (tab.lastTaskId === taskId) tab.lastTaskId = ""
            tab.statusText = "● " + (error || "失败")
            tab.statusColor = App.Theme.danger
        }
    }

    // ➕ 添加词条对话框
    Dialog {
        id: addItemDialog
        title: "添加词条 - " + tab.addingCategory
        anchors.centerIn: parent
        modal: true
        width: 400
        standardButtons: Dialog.Ok | Dialog.Cancel

        ColumnLayout {
            anchors.fill: parent
            spacing: App.Theme.space2

            Label {
                text: "请输入新的「" + tab.addingCategory + "」词条(纯中文):"
                font.family: App.Theme.fontFamily
                font.pointSize: App.Theme.fontSm
                color: App.Theme.textPrimary
            }
            ThemedTextField {
                id: addItemField
                Layout.fillWidth: true
                placeholderText: "例如:圆形"
            }
        }

        onAccepted: {
            var v = addItemField.text.trim()
            if (v) jewelry.addItem(tab.addingCategory, v)
            addItemField.text = ""
        }
        onRejected: addItemField.text = ""
    }

    // 💾 下载图片
    FileDialog {
        id: saveImageDialog
        title: "保存生成的图片"
        fileMode: FileDialog.SaveFile
        nameFilters: ["PNG 图片 (*.png)"]
        defaultSuffix: "png"
        onAccepted: {
            var dest = selectedFile.toString().replace("file:///", "")
            var ok = imageGen.saveFile(tab.lastResultPath, dest)
            if (ok) {
                tab.statusText = "● 已保存到 " + dest
                tab.statusColor = App.Theme.success
            } else {
                tab.statusText = "● 保存失败"
                tab.statusColor = App.Theme.danger
            }
        }
    }

    RowLayout {
        anchors.fill: parent
        anchors.margins: App.Theme.space5
        spacing: App.Theme.space4

        // ===== 左侧:8 字段表单 =====
        Card {
            Layout.preferredWidth: 420
            Layout.fillHeight: true

            ColumnLayout {
                anchors.fill: parent
                anchors.margins: App.Theme.space4
                spacing: App.Theme.space3

                // header
                RowLayout {
                    spacing: App.Theme.space3
                    CaptionLabel {
                        text: "款式字段"
                        font.pointSize: App.Theme.fontLg
                    }
                    Item { Layout.fillWidth: true }
                }

                // 工具按钮行:随机 / 恢复默认词库 / 重置字段
                RowLayout {
                    Layout.fillWidth: true
                    spacing: App.Theme.space2
                    SecondaryButton {
                        text: "🎲 随机"
                        onClicked: tab.randomize()
                    }
                    SecondaryButton {
                        text: "🔄 恢复默认词库"
                        onClicked: jewelry.resetAll()
                    }
                    SecondaryButton {
                        text: "重置字段"
                        onClicked: tab.resetFields()
                    }
                    Item { Layout.fillWidth: true }
                }

                ScrollView {
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    clip: true

                    ColumnLayout {
                        width: parent.width
                        spacing: App.Theme.space3

                        Repeater {
                            model: jewelry.categories  // 8 个类别名

                            delegate: ColumnLayout {
                                Layout.fillWidth: true
                                spacing: 4

                                CaptionLabel {
                                    text: modelData
                                    font.pointSize: App.Theme.fontBase
                                    color: tab.isLocked(modelData)
                                        ? App.Theme.textTertiary
                                        : App.Theme.textPrimary
                                }
                                RowLayout {
                                    Layout.fillWidth: true
                                    spacing: App.Theme.space2

                                    ThemedComboBox {
                                        id: cb
                                        // 固定宽度:fillWidth + maximumWidth 锁住,避免内容长度变化撑大 RowLayout
                                        Layout.fillWidth: true
                                        Layout.preferredWidth: 240
                                        Layout.maximumWidth: 240
                                        // 头插 "(不选)",用 trigger 强制每次随机/重置后刷新
                                        property var fullModel: {
                                            tab.comboReloadTrigger
                                            return ["(不选)"].concat(jewelry.getOptions(modelData))
                                        }
                                        model: fullModel
                                        // 同步 currentIndex 到 formData[modelData]
                                        currentIndex: {
                                            tab.comboReloadTrigger
                                            var v = tab.formData[modelData] || ""
                                            if (!v) return 0  // "(不选)"
                                            var idx = fullModel.indexOf(v)
                                            return idx >= 0 ? idx : 0
                                        }
                                        enabled: !tab.isLocked(modelData)
                                        onActivated: {
                                            var v = currentText === "(不选)" ? "" : currentText
                                            tab.updateField(modelData, v)
                                        }
                                    }

                                    // ➕ 添加词条
                                    SecondaryButton {
                                        text: "➕"
                                        Layout.preferredWidth: 36
                                        onClicked: {
                                            tab.addingCategory = modelData
                                            addItemDialog.open()
                                        }
                                    }
                                    // 🗑️ 删除当前选中词条
                                    SecondaryButton {
                                        text: "🗑️"
                                        Layout.preferredWidth: 36
                                        enabled: (tab.formData[modelData] || "").length > 0
                                        onClicked: {
                                            var v = tab.formData[modelData]
                                            if (v) {
                                                jewelry.removeItem(modelData, v)
                                                tab.updateField(modelData, "")
                                            }
                                        }
                                    }
                                    // 🔓 / 🔒 锁定字段(锁定的字段不参与随机)
                                    SecondaryButton {
                                        text: tab.isLocked(modelData) ? "🔒" : "🔓"
                                        Layout.preferredWidth: 36
                                        onClicked: tab.toggleLock(modelData)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // ===== 右侧:Prompt 预览 + 生成 + 结果 =====
        ColumnLayout {
            Layout.fillWidth: true
            Layout.fillHeight: true
            spacing: App.Theme.space4

            // Prompt 预览卡
            Card {
                Layout.fillWidth: true
                Layout.preferredHeight: 200

                ColumnLayout {
                    anchors.fill: parent
                    anchors.margins: App.Theme.space4
                    spacing: App.Theme.space3

                    RowLayout {
                        spacing: App.Theme.space3
                        CaptionLabel {
                            text: "Prompt 预览"
                            font.pointSize: App.Theme.fontLg
                        }
                        Item { Layout.fillWidth: true }
                        Label {
                            text: tab.assembledPrompt.length + " 字"
                            font.family: App.Theme.fontFamily
                            font.pointSize: App.Theme.fontXs
                            color: App.Theme.textTertiary
                        }
                    }

                    ScrollView {
                        Layout.fillWidth: true
                        Layout.fillHeight: true

                        Rectangle {
                            anchors.fill: parent
                            color: App.Theme.bgSubtle
                            radius: App.Theme.radiusMd

                            Text {
                                anchors.fill: parent
                                anchors.margins: App.Theme.space3
                                text: tab.assembledPrompt
                                color: App.Theme.textPrimary
                                font.family: App.Theme.fontFamily
                                font.pointSize: App.Theme.fontSm
                                wrapMode: Text.Wrap
                            }
                        }
                    }
                }
            }

            // 操作行:生成 + 下载 + 状态
            RowLayout {
                spacing: App.Theme.space3
                Layout.fillWidth: true

                PrimaryButton {
                    text: "🎨 生成图片"
                    onClicked: tab.submit()
                }
                SecondaryButton {
                    text: "💾 下载图片"
                    enabled: tab.lastResultPath !== ""
                    onClicked: saveImageDialog.open()
                }
                Label {
                    text: tab.statusText
                    font.family: App.Theme.fontFamily
                    font.pointSize: App.Theme.fontSm
                    color: tab.statusColor
                }
                Item { Layout.fillWidth: true }
            }

            // 预览大图
            Card {
                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.minimumHeight: 240

                ColumnLayout {
                    anchors.fill: parent
                    anchors.margins: App.Theme.space4
                    spacing: App.Theme.space3

                    CaptionLabel {
                        text: "预览"
                        font.pointSize: App.Theme.fontLg
                    }

                    Rectangle {
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        color: App.Theme.bgSubtle
                        radius: App.Theme.radiusMd

                        Image {
                            anchors.fill: parent
                            anchors.margins: App.Theme.space3
                            source: tab.lastResultPath ? "file:///" + tab.lastResultPath : ""
                            fillMode: Image.PreserveAspectFit
                            smooth: true
                            asynchronous: true
                            cache: false
                            visible: tab.lastResultPath !== ""

                            MouseArea {
                                anchors.fill: parent
                                cursorShape: Qt.PointingHandCursor
                                onDoubleClicked: {
                                    if (tab.lastResultPath) {
                                        Qt.openUrlExternally("file:///" + tab.lastResultPath)
                                    }
                                }
                            }
                        }

                        Text {
                            anchors.centerIn: parent
                            visible: tab.lastResultPath === ""
                            text: "点 \"🎲 随机\" 试手气,或选字段后点 \"🎨 生成图片\""
                            color: App.Theme.textTertiary
                            font.family: App.Theme.fontFamily
                            font.pointSize: App.Theme.fontSm
                        }
                    }
                }
            }
        }
    }
}