HistoryTab.qml 19.8 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 491 492 493 494 495 496 497 498 499 500 501 502 503 504
import QtQuick
import QtQuick.Controls.Basic
import QtQuick.Dialogs
import QtQuick.Layouts
import "components"
import "." as App

Item {
    id: tab

    property string selectedTimestamp: ""
    property var selectedItem: ({})
    // 右键菜单当前操作的 timestamp
    property string contextTimestamp: ""

    // 单条删除确认
    MessageDialog {
        id: confirmDeleteDialog
        title: "确认删除"
        text: "确定要删除这条历史记录吗?这将删除相关的所有文件。"
        buttons: MessageDialog.Yes | MessageDialog.No
        onAccepted: {
            if (tab.contextTimestamp) {
                history.deleteItem(tab.contextTimestamp)
                tab.contextTimestamp = ""
            }
        }
    }

    // 全部清空确认
    MessageDialog {
        id: confirmClearDialog
        title: "确认清空"
        text: "确定要清空所有历史记录吗?这将删除所有历史图片文件,且无法恢复。"
        buttons: MessageDialog.Yes | MessageDialog.No
        onAccepted: history.clearAll()
    }

    // 列表项右键菜单
    Menu {
        id: itemContextMenu

        MenuItem {
            text: "在文件管理器中打开"
            onTriggered: {
                if (tab.contextTimestamp) {
                    var item = history.getItem(tab.contextTimestamp)
                    if (item.generatedImagePath) {
                        history.revealInExplorer(item.generatedImagePath)
                    }
                }
            }
        }
        MenuItem {
            text: "复制提示词"
            onTriggered: {
                if (tab.contextTimestamp) {
                    var item = history.getItem(tab.contextTimestamp)
                    if (item.prompt) history.copyToClipboard(item.prompt)
                }
            }
        }
        MenuSeparator {}
        MenuItem {
            text: "删除此项"
            onTriggered: confirmDeleteDialog.open()
        }
    }

    function selectTimestamp(ts) {
        if (!ts) {
            tab.selectedTimestamp = ""
            tab.selectedItem = ({})
            return
        }
        tab.selectedTimestamp = ts
        tab.selectedItem = history.getItem(ts) || ({})
    }

    // 列表数据变化时校正选中项
    Connections {
        target: history
        function onItemRemoved(ts) {
            if (tab.selectedTimestamp === ts) tab.selectTimestamp("")
        }
        function onCountChanged() {
            // 自动选中第一条(首次加载 / 新增时)
            if (tab.selectedTimestamp === "" && historyList.count > 0) {
                var idx = historyList.model.index(0, 0)
                var ts = historyList.model.data(idx, Qt.UserRole)
                if (ts) tab.selectTimestamp(ts)
            }
        }
    }

    Component.onCompleted: {
        if (historyList.count > 0) {
            var idx = historyList.model.index(0, 0)
            var ts = historyList.model.data(idx, Qt.UserRole)
            if (ts) tab.selectTimestamp(ts)
        }
    }

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

        // ===== 左侧:列表 =====
        Card {
            Layout.preferredWidth: 340
            Layout.fillHeight: true

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

                RowLayout {
                    spacing: App.Theme.space2
                    CaptionLabel {
                        text: "历史记录"
                        font.pointSize: App.Theme.fontLg
                    }
                    Label {
                        text: history.count + " 条"
                        font.family: App.Theme.fontFamily
                        font.pointSize: App.Theme.fontSm
                        color: App.Theme.textSecondary
                    }
                    Item { Layout.fillWidth: true }
                    SecondaryButton {
                        text: "🔄 刷新"
                        onClicked: history.refresh()
                    }
                    SecondaryButton {
                        text: "🗑️ 清空"
                        enabled: history.count > 0
                        onClicked: confirmClearDialog.open()
                    }
                }

                // 空态
                Label {
                    visible: historyList.count === 0
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    text: "暂无历史记录\n生成图片后会自动出现在这里"
                    font.family: App.Theme.fontFamily
                    font.pointSize: App.Theme.fontSm
                    color: App.Theme.textTertiary
                }

                ListView {
                    id: historyList
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    Layout.rightMargin: 4  // 给 ScrollBar 留视觉空间
                    visible: count > 0
                    spacing: 4
                    clip: true
                    model: history.model

                    ScrollBar.vertical: ScrollBar {
                        id: historyScrollBar
                        policy: ScrollBar.AlwaysOn
                        // 默认 Basic 主题滚动条太细 — 自定义 contentItem,
                        // 8px 常驻 + hover/press 加深,扫一眼就看见。
                        contentItem: Rectangle {
                            implicitWidth: 8
                            radius: 4
                            color: historyScrollBar.pressed
                                ? App.Theme.borderStrong
                                : (historyScrollBar.hovered
                                    ? App.Theme.borderDefault
                                    : Qt.rgba(0, 0, 0, 0.18))
                            Behavior on color { ColorAnimation { duration: 100 } }
                        }
                    }

                    delegate: Rectangle {
                        required property int index
                        required property string display
                        required property string toolTip
                        required property string timestamp

                        width: ListView.view.width
                        height: 76
                        radius: App.Theme.radiusSm
                        color: tab.selectedTimestamp === timestamp ? App.Theme.accentSubtle
                             : itemMouse.containsMouse                ? App.Theme.bgHover
                             : "transparent"
                        border.width: tab.selectedTimestamp === timestamp ? 1 : 0
                        border.color: App.Theme.accent

                        RowLayout {
                            anchors.fill: parent
                            anchors.margins: App.Theme.space2
                            spacing: App.Theme.space3

                            // 缩略图
                            Rectangle {
                                Layout.preferredWidth: 60
                                Layout.preferredHeight: 60
                                radius: App.Theme.radiusSm
                                color: App.Theme.bgSubtle

                                Image {
                                    id: thumb
                                    anchors.fill: parent
                                    anchors.margins: 1
                                    source: {
                                        var p = history.thumbnailPath(timestamp)
                                        return p ? "file:///" + p : ""
                                    }
                                    fillMode: Image.PreserveAspectCrop
                                    smooth: true
                                    asynchronous: true
                                }
                            }

                            // 文字
                            ColumnLayout {
                                Layout.fillWidth: true
                                Layout.fillHeight: true
                                spacing: 2

                                Text {
                                    Layout.fillWidth: true
                                    text: timestamp
                                    color: App.Theme.textPrimary
                                    font.family: App.Theme.fontFamily
                                    font.pointSize: App.Theme.fontXs
                                    font.weight: Font.DemiBold
                                }
                                Text {
                                    Layout.fillWidth: true
                                    text: display.split("\n").slice(1).join(" ")
                                    color: App.Theme.textSecondary
                                    font.family: App.Theme.fontFamily
                                    font.pointSize: App.Theme.fontXs
                                    elide: Text.ElideRight
                                    maximumLineCount: 2
                                    wrapMode: Text.Wrap
                                }
                            }
                        }

                        MouseArea {
                            id: itemMouse
                            anchors.fill: parent
                            hoverEnabled: true
                            cursorShape: Qt.PointingHandCursor
                            acceptedButtons: Qt.LeftButton | Qt.RightButton
                            onClicked: function(mouse) {
                                if (mouse.button === Qt.RightButton) {
                                    tab.contextTimestamp = timestamp
                                    itemContextMenu.popup()
                                } else {
                                    tab.selectTimestamp(timestamp)
                                }
                            }
                        }

                        ToolTip.text: toolTip
                        ToolTip.visible: itemMouse.containsMouse && toolTip.length > 0
                        ToolTip.delay: 600
                    }
                }
            }
        }

        // ===== 右侧:详情 =====
        Card {
            Layout.fillWidth: true
            Layout.fillHeight: true

            // 空态
            Label {
                anchors.centerIn: parent
                visible: tab.selectedTimestamp === ""
                text: "选择左侧记录查看详情"
                font.family: App.Theme.fontFamily
                font.pointSize: App.Theme.fontSm
                color: App.Theme.textTertiary
            }

            ColumnLayout {
                visible: tab.selectedTimestamp !== ""
                anchors.fill: parent
                anchors.margins: App.Theme.space4
                spacing: App.Theme.space3

                // header: 时间 + 操作按钮
                RowLayout {
                    Layout.fillWidth: true
                    spacing: App.Theme.space3

                    CaptionLabel {
                        text: tab.selectedItem.timestamp || ""
                        font.pointSize: App.Theme.fontLg
                    }
                    Label {
                        text: tab.selectedItem.createdAt || ""
                        font.family: App.Theme.fontFamily
                        font.pointSize: App.Theme.fontSm
                        color: App.Theme.textSecondary
                    }
                    Item { Layout.fillWidth: true }
                    SecondaryButton {
                        text: "在文件管理器中打开"
                        onClicked: {
                            if (tab.selectedItem.generatedImagePath) {
                                history.revealInExplorer(tab.selectedItem.generatedImagePath)
                            }
                        }
                    }
                    SecondaryButton {
                        text: "删除"
                        onClicked: {
                            if (tab.selectedTimestamp) {
                                tab.contextTimestamp = tab.selectedTimestamp
                                confirmDeleteDialog.open()
                            }
                        }
                    }
                }

                // 大图
                Rectangle {
                    Layout.fillWidth: true
                    Layout.preferredHeight: 320
                    color: App.Theme.bgSubtle
                    radius: App.Theme.radiusMd

                    Image {
                        anchors.fill: parent
                        anchors.margins: App.Theme.space3
                        source: tab.selectedItem.generatedImagePath
                            ? "file:///" + tab.selectedItem.generatedImagePath
                            : ""
                        fillMode: Image.PreserveAspectFit
                        smooth: true
                        asynchronous: true

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

                // 参考图(可能没有)
                ColumnLayout {
                    Layout.fillWidth: true
                    spacing: App.Theme.space2
                    visible: (tab.selectedItem.referenceImagePaths || []).length > 0

                    CaptionLabel {
                        text: "参考图(" + (tab.selectedItem.referenceImagePaths || []).length + " 张)"
                    }

                    Flow {
                        Layout.fillWidth: true
                        spacing: App.Theme.space2

                        Repeater {
                            model: tab.selectedItem.referenceImagePaths || []
                            delegate: Rectangle {
                                width: 80
                                height: 80
                                radius: App.Theme.radiusSm
                                color: App.Theme.bgSurface
                                border.width: 1
                                border.color: App.Theme.borderDefault

                                Image {
                                    anchors.fill: parent
                                    anchors.margins: 2
                                    source: "file:///" + modelData
                                    fillMode: Image.PreserveAspectCrop
                                    smooth: true
                                    asynchronous: true
                                }

                                // 左下角 "图 N" badge
                                Rectangle {
                                    anchors.left: parent.left
                                    anchors.bottom: parent.bottom
                                    anchors.margins: 3
                                    width: refIdx.implicitWidth + 8
                                    height: 16
                                    radius: App.Theme.radiusSm
                                    color: Qt.rgba(0, 0, 0, 0.6)

                                    Text {
                                        id: refIdx
                                        anchors.centerIn: parent
                                        text: "图 " + (index + 1)
                                        color: "white"
                                        font.family: App.Theme.fontFamily
                                        font.pointSize: App.Theme.fontXs
                                        font.weight: Font.DemiBold
                                    }
                                }

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

                // 元信息(不暴露具体模型 ID)
                GridLayout {
                    Layout.fillWidth: true
                    columns: 4
                    columnSpacing: App.Theme.space4
                    rowSpacing: App.Theme.space2

                    CaptionLabel { text: "宽高比" }
                    Label {
                        text: tab.selectedItem.aspectRatio || "—"
                        font.family: App.Theme.fontFamily
                        font.pointSize: App.Theme.fontSm
                        color: App.Theme.textPrimary
                    }
                    CaptionLabel { text: "尺寸" }
                    Label {
                        text: tab.selectedItem.imageSize || "—"
                        font.family: App.Theme.fontFamily
                        font.pointSize: App.Theme.fontSm
                        color: App.Theme.textPrimary
                    }
                }

                // prompt header + 重做 + 复制按钮
                RowLayout {
                    Layout.fillWidth: true
                    spacing: App.Theme.space2

                    CaptionLabel { text: "提示词" }
                    Item { Layout.fillWidth: true }
                    SecondaryButton {
                        text: "🔁 重做"
                        enabled: tab.selectedTimestamp.length > 0
                        onClicked: {
                            // 走桥层 redoRequested 信号 → ImageGenTab 接住回填,并切到 tab 0
                            history.redoToImageGen(tab.selectedTimestamp)
                        }
                    }
                    SecondaryButton {
                        id: copyBtn
                        property bool justCopied: false
                        text: justCopied ? "✓ 已复制" : "📋 复制"
                        enabled: (tab.selectedItem.prompt || "").length > 0
                        onClicked: {
                            if (history.copyToClipboard(tab.selectedItem.prompt || "")) {
                                copyBtn.justCopied = true
                                copyResetTimer.restart()
                            }
                        }

                        Timer {
                            id: copyResetTimer
                            interval: 1500
                            onTriggered: copyBtn.justCopied = false
                        }
                    }
                }
                ScrollView {
                    Layout.fillWidth: true
                    Layout.preferredHeight: 80
                    Rectangle {
                        anchors.fill: parent
                        color: App.Theme.bgSubtle
                        radius: App.Theme.radiusMd
                        Text {
                            anchors.fill: parent
                            anchors.margins: App.Theme.space3
                            text: tab.selectedItem.prompt || ""
                            color: App.Theme.textPrimary
                            font.family: App.Theme.fontFamily
                            font.pointSize: App.Theme.fontSm
                            wrapMode: Text.Wrap
                        }
                    }
                }

                Item { Layout.fillHeight: true }
            }
        }
    }
}