From 966a57302cc3c7454a72ff0c94c4f481fe44ab38 Mon Sep 17 00:00:00 2001 From: JackLee <809262979@qq.com> Date: Tue, 4 Mar 2025 21:02:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E8=87=AA=E9=80=82=E5=BA=94?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ctai_curl.cpp | 9 ++-- src/ctai_history_textedit.cpp | 94 +++++++++++++++++------------------ src/ctai_history_textedit.h | 3 +- src/ctai_history_widget.cpp | 22 +++++--- src/ctai_history_widget.h | 3 +- src/ctai_markdown.cpp | 2 +- src/ctai_session_info.cpp | 2 +- 7 files changed, 69 insertions(+), 66 deletions(-) diff --git a/src/ctai_curl.cpp b/src/ctai_curl.cpp index 3918ed3..eca7dfb 100644 --- a/src/ctai_curl.cpp +++ b/src/ctai_curl.cpp @@ -24,7 +24,7 @@ size_t stream_callback(void *buffer, size_t sz, size_t nmemb, void *userdata) { ctx->instance->send_stream(ctx->m_data, chunk); } - qDebug() << "steam data chunk:" << chunk; + //qDebug() << "steam data chunk:" << chunk; return sz * nmemb; } // 非流式输出回调 @@ -33,7 +33,7 @@ size_t not_stream_callback(void *buffer, size_t sz, size_t nmemb, void *userdata std::string *psResponse = static_cast(userdata); size_t size = sz * nmemb; psResponse->append(static_cast(buffer), size); - qDebug() << "not steam data chunk:" << *psResponse; + //qDebug() << "not steam data chunk:" << *psResponse; return sz * nmemb; } void ctai_curl::set_send_post_headers(std::string head_str) @@ -123,7 +123,6 @@ std::vector ctai_curl::steam_extract(const std::string &input) // 提取捕获组中的内容 result.push_back(match.str(1)); } - qDebug() << "result:" << result; return result; } std::string ctai_curl::send_timestamp_to_time(time_t timestamp) @@ -150,14 +149,12 @@ void ctai_curl::send_stream(model_data &data, std::string response_data) send_not_stream(data, str); emit send_post_out_data(data); } - qDebug() << "send_stream:" << str; } } } void ctai_curl::send_not_stream(model_data &data, std::string response_data) { json response = json::parse(response_data); - qDebug()<<"msg_type_mode:"<setAcceptRichText(true); m_msg_history->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_msg_history->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - // 添加性能优化设置 - m_msg_history->setUndoRedoEnabled(false); // 禁用撤销重做 - m_msg_history->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard); m_msg_history->setObjectName("m_msg_history"); m_msg_history->setReadOnly(true); historyLayout->addWidget(m_msg_history); @@ -206,56 +204,56 @@ void ctai_history_textedit::on_fold_clicked() m_msg_fold->setIcon(QIcon(m_is_folded ? ":res/img/btn/btn_info_down.png" : ":res/img/btn/btn_info_up.png")); updateGeometry(); - emit height_changed(); + emit fold_row_height_changed(); } void ctai_history_textedit::on_text_height() { - // 防抖动处理:如果上次更新在100ms内,则延迟处理 - static QTimer debounceTimer; - debounceTimer.setSingleShot(true); - - if (debounceTimer.isActive()) - { - return; - } - // 直接使用documentLayout获取高度,避免多次计算 - int textHeight = m_msg_history->document()->documentLayout()->documentSize().height(); - - if (textHeight > 0) - { - // 计算实际需要的高度 (包含边距和视口边框) - int newHeight = textHeight + - m_msg_history->document()->documentMargin() * 2 + - m_msg_history->frameWidth() * 2; - - // 添加高度阈值判断,避免微小变化触发更新 - static const int HEIGHT_THRESHOLD = 5; // 5像素的阈值 - if (abs(m_msg_history->height() - newHeight) > HEIGHT_THRESHOLD) - { - m_original_height = newHeight; - - // 批量更新布局 - bool wasBlocked = m_msg_history->signalsBlocked(); - m_msg_history->blockSignals(true); - - m_msg_history->setFixedHeight(newHeight); - updateGeometry(); - - m_msg_history->blockSignals(wasBlocked); - emit height_changed(); - - // 延迟发送布局更新请求 - if (QWidget *parent = parentWidget()) - { - QTimer::singleShot(100, [parent]() - { + // 防抖动处理:如果上次更新在100ms内,则延迟处理 + static QTimer debounceTimer; + debounceTimer.setSingleShot(true); + + if (debounceTimer.isActive()) + { + return; + } + // 直接使用documentLayout获取高度,避免多次计算 + int textHeight = m_msg_history->document()->documentLayout()->documentSize().height(); + + if (textHeight > 0) + { + // 计算实际需要的高度 (包含边距和视口边框) + int newHeight = textHeight + + m_msg_history->document()->documentMargin() * 2 + + m_msg_history->frameWidth() * 2; + + // 添加高度阈值判断,避免微小变化触发更新 + static const int HEIGHT_THRESHOLD = 5; // 5像素的阈值 + if (abs(m_msg_history->height() - newHeight) > HEIGHT_THRESHOLD) + { + m_original_height = newHeight+15; + + // 批量更新布局 + bool wasBlocked = m_msg_history->signalsBlocked(); + m_msg_history->blockSignals(true); + + m_msg_history->setFixedHeight(newHeight); + updateGeometry(); + + m_msg_history->blockSignals(wasBlocked); + emit current_row_height_changed(); + + // 延迟发送布局更新请求 + if (QWidget *parent = parentWidget()) + { + QTimer::singleShot(100, [parent]() + { QEvent e(QEvent::LayoutRequest); QApplication::sendEvent(parent, &e); }); - } - } - } - // 设置下一次检查的延迟 - debounceTimer.start(100); + } + } + } + // 设置下一次检查的延迟 + debounceTimer.start(100); } void ctai_history_textedit::add_user_message(const model_data &message) { diff --git a/src/ctai_history_textedit.h b/src/ctai_history_textedit.h index 859431c..6bc336b 100644 --- a/src/ctai_history_textedit.h +++ b/src/ctai_history_textedit.h @@ -67,7 +67,8 @@ private slots: void on_text_height(); signals: void delete_requested(QString); // 请求删除此消息 - void height_changed(); // 新增高度变化信号 + void current_row_height_changed(); + void fold_row_height_changed(); private: void connect_signals(msg_type msg_type_mode); // 连接信号和槽 private: diff --git a/src/ctai_history_widget.cpp b/src/ctai_history_widget.cpp index 619cee3..9915ccc 100644 --- a/src/ctai_history_widget.cpp +++ b/src/ctai_history_widget.cpp @@ -96,22 +96,28 @@ void ctai_history_widget::add_message(const model_data &message) // 连接删除信号 connect(message_widget, SIGNAL(delete_requested(QString)), this, SLOT(on_msg_remove(QString))); // 连接高度变化信号 - connect(message_widget, &ctai_history_textedit::height_changed, - this, &ctai_history_widget::on_widget_height_changed); + connect(message_widget, &ctai_history_textedit::current_row_height_changed,this, &ctai_history_widget::on_current_rows_height_changed); + connect(message_widget, &ctai_history_textedit::fold_row_height_changed,this, &ctai_history_widget::on_rows_height_changed); // 滚动到新消息 scrollToItem(item(row, 0)); } // 立即滚动到底部 scrollToBottom(); } -void ctai_history_widget::on_widget_height_changed() +//适合信息折叠 +void ctai_history_widget::on_rows_height_changed() { - int row = currentRow(); - if (row >= 0 && row < rowCount()) { - int currentHeight = rowHeight(row); - setRowHeight(row, currentHeight + 100); + //全部行高自适应 + for (int row = 0; row < rowCount(); ++row) { resizeRowToContents(row); - } + } + scrollToBottom(); +} +//适合自适应流式输出内容 +void ctai_history_widget::on_current_rows_height_changed() +{ + // 当前行高自适应 + resizeRowToContents(rowCount()); scrollToBottom(); } void ctai_history_widget::on_msg_remove(QString send_id) diff --git a/src/ctai_history_widget.h b/src/ctai_history_widget.h index 85899e2..99b9365 100644 --- a/src/ctai_history_widget.h +++ b/src/ctai_history_widget.h @@ -24,6 +24,7 @@ private: void update_stored_data(model_data* stored_data, const model_data& message); private slots: void on_msg_remove(QString send_id); - void on_widget_height_changed(); // 新增槽函数处理高度变化 + void on_rows_height_changed(); // 新增槽函数处理高度变化 + void on_current_rows_height_changed(); }; #endif \ No newline at end of file diff --git a/src/ctai_markdown.cpp b/src/ctai_markdown.cpp index 504ff93..f72eb46 100644 --- a/src/ctai_markdown.cpp +++ b/src/ctai_markdown.cpp @@ -15,7 +15,7 @@ QString md_to_html(const QString& text) { // 转换回QString并处理代码块 QString result = QString::fromUtf8(html); free(html); - qDebug()<<"处理的块:"<setLayout(m_session_systeam_info_layout); m_session_user_group_layout = new QVBoxLayout(); - m_session_user_edit_info = new QTextEdit(tr("写一篇关于天然气的论文")); + m_session_user_edit_info = new QTextEdit(tr("1+1为什么等于2")); m_session_user_edit_layout = new QVBoxLayout(); m_session_user_edit_layout->addWidget(m_session_user_edit_info);