解决自适应问题
This commit is contained in:
parent
6a16887dd8
commit
966a57302c
@ -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);
|
ctx->instance->send_stream(ctx->m_data, chunk);
|
||||||
}
|
}
|
||||||
qDebug() << "steam data chunk:" << chunk;
|
//qDebug() << "steam data chunk:" << chunk;
|
||||||
return sz * nmemb;
|
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<std::string *>(userdata);
|
std::string *psResponse = static_cast<std::string *>(userdata);
|
||||||
size_t size = sz * nmemb;
|
size_t size = sz * nmemb;
|
||||||
psResponse->append(static_cast<char *>(buffer), size);
|
psResponse->append(static_cast<char *>(buffer), size);
|
||||||
qDebug() << "not steam data chunk:" << *psResponse;
|
//qDebug() << "not steam data chunk:" << *psResponse;
|
||||||
return sz * nmemb;
|
return sz * nmemb;
|
||||||
}
|
}
|
||||||
void ctai_curl::set_send_post_headers(std::string head_str)
|
void ctai_curl::set_send_post_headers(std::string head_str)
|
||||||
@ -123,7 +123,6 @@ std::vector<std::string> ctai_curl::steam_extract(const std::string &input)
|
|||||||
// 提取捕获组中的内容
|
// 提取捕获组中的内容
|
||||||
result.push_back(match.str(1));
|
result.push_back(match.str(1));
|
||||||
}
|
}
|
||||||
qDebug() << "result:" << result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
std::string ctai_curl::send_timestamp_to_time(time_t timestamp)
|
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);
|
send_not_stream(data, str);
|
||||||
emit send_post_out_data(data);
|
emit send_post_out_data(data);
|
||||||
}
|
}
|
||||||
qDebug() << "send_stream:" << str;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void ctai_curl::send_not_stream(model_data &data, std::string response_data)
|
void ctai_curl::send_not_stream(model_data &data, std::string response_data)
|
||||||
{
|
{
|
||||||
json response = json::parse(response_data);
|
json response = json::parse(response_data);
|
||||||
qDebug()<<"msg_type_mode:"<<data.msg_type_mode;
|
|
||||||
if (data.postback_stream_mode)
|
if (data.postback_stream_mode)
|
||||||
{
|
{
|
||||||
if (response.contains("choices") && !response["choices"].empty())
|
if (response.contains("choices") && !response["choices"].empty())
|
||||||
@ -224,7 +221,7 @@ std::string ctai_curl::send_send_body(model_data m_data)
|
|||||||
{"messages", {{{"role", "user"}, {"content", m_data.send_user_data}}}},
|
{"messages", {{{"role", "user"}, {"content", m_data.send_user_data}}}},
|
||||||
{"temperature", 0.7},
|
{"temperature", 0.7},
|
||||||
{"stream", m_data.postback_stream_mode}};
|
{"stream", m_data.postback_stream_mode}};
|
||||||
qDebug() << "request_body_str:" << _request_body.dump();
|
qDebug() << "request_body_str:" << _request_body.dump(4);
|
||||||
return _request_body.dump();
|
return _request_body.dump();
|
||||||
}
|
}
|
||||||
void ctai_curl::set_send_post_option()
|
void ctai_curl::set_send_post_option()
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
ctai_history_textedit::ctai_history_textedit(QWidget *parent)
|
ctai_history_textedit::ctai_history_textedit(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
{
|
{
|
||||||
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||||
}
|
}
|
||||||
ctai_history_textedit::~ctai_history_textedit()
|
ctai_history_textedit::~ctai_history_textedit()
|
||||||
{
|
{
|
||||||
@ -92,9 +93,6 @@ void ctai_history_textedit::init_layout(msg_type msg_type_mode)
|
|||||||
m_msg_history->setAcceptRichText(true);
|
m_msg_history->setAcceptRichText(true);
|
||||||
m_msg_history->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
m_msg_history->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
m_msg_history->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
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->setObjectName("m_msg_history");
|
||||||
m_msg_history->setReadOnly(true);
|
m_msg_history->setReadOnly(true);
|
||||||
historyLayout->addWidget(m_msg_history);
|
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"));
|
m_msg_fold->setIcon(QIcon(m_is_folded ? ":res/img/btn/btn_info_down.png" : ":res/img/btn/btn_info_up.png"));
|
||||||
updateGeometry();
|
updateGeometry();
|
||||||
emit height_changed();
|
emit fold_row_height_changed();
|
||||||
}
|
}
|
||||||
void ctai_history_textedit::on_text_height()
|
void ctai_history_textedit::on_text_height()
|
||||||
{
|
{
|
||||||
// 防抖动处理:如果上次更新在100ms内,则延迟处理
|
// 防抖动处理:如果上次更新在100ms内,则延迟处理
|
||||||
static QTimer debounceTimer;
|
static QTimer debounceTimer;
|
||||||
debounceTimer.setSingleShot(true);
|
debounceTimer.setSingleShot(true);
|
||||||
|
|
||||||
if (debounceTimer.isActive())
|
if (debounceTimer.isActive())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 直接使用documentLayout获取高度,避免多次计算
|
// 直接使用documentLayout获取高度,避免多次计算
|
||||||
int textHeight = m_msg_history->document()->documentLayout()->documentSize().height();
|
int textHeight = m_msg_history->document()->documentLayout()->documentSize().height();
|
||||||
|
|
||||||
if (textHeight > 0)
|
if (textHeight > 0)
|
||||||
{
|
{
|
||||||
// 计算实际需要的高度 (包含边距和视口边框)
|
// 计算实际需要的高度 (包含边距和视口边框)
|
||||||
int newHeight = textHeight +
|
int newHeight = textHeight +
|
||||||
m_msg_history->document()->documentMargin() * 2 +
|
m_msg_history->document()->documentMargin() * 2 +
|
||||||
m_msg_history->frameWidth() * 2;
|
m_msg_history->frameWidth() * 2;
|
||||||
|
|
||||||
// 添加高度阈值判断,避免微小变化触发更新
|
// 添加高度阈值判断,避免微小变化触发更新
|
||||||
static const int HEIGHT_THRESHOLD = 5; // 5像素的阈值
|
static const int HEIGHT_THRESHOLD = 5; // 5像素的阈值
|
||||||
if (abs(m_msg_history->height() - newHeight) > HEIGHT_THRESHOLD)
|
if (abs(m_msg_history->height() - newHeight) > HEIGHT_THRESHOLD)
|
||||||
{
|
{
|
||||||
m_original_height = newHeight;
|
m_original_height = newHeight+15;
|
||||||
|
|
||||||
// 批量更新布局
|
// 批量更新布局
|
||||||
bool wasBlocked = m_msg_history->signalsBlocked();
|
bool wasBlocked = m_msg_history->signalsBlocked();
|
||||||
m_msg_history->blockSignals(true);
|
m_msg_history->blockSignals(true);
|
||||||
|
|
||||||
m_msg_history->setFixedHeight(newHeight);
|
m_msg_history->setFixedHeight(newHeight);
|
||||||
updateGeometry();
|
updateGeometry();
|
||||||
|
|
||||||
m_msg_history->blockSignals(wasBlocked);
|
m_msg_history->blockSignals(wasBlocked);
|
||||||
emit height_changed();
|
emit current_row_height_changed();
|
||||||
|
|
||||||
// 延迟发送布局更新请求
|
// 延迟发送布局更新请求
|
||||||
if (QWidget *parent = parentWidget())
|
if (QWidget *parent = parentWidget())
|
||||||
{
|
{
|
||||||
QTimer::singleShot(100, [parent]()
|
QTimer::singleShot(100, [parent]()
|
||||||
{
|
{
|
||||||
QEvent e(QEvent::LayoutRequest);
|
QEvent e(QEvent::LayoutRequest);
|
||||||
QApplication::sendEvent(parent, &e); });
|
QApplication::sendEvent(parent, &e); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 设置下一次检查的延迟
|
// 设置下一次检查的延迟
|
||||||
debounceTimer.start(100);
|
debounceTimer.start(100);
|
||||||
}
|
}
|
||||||
void ctai_history_textedit::add_user_message(const model_data &message)
|
void ctai_history_textedit::add_user_message(const model_data &message)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -67,7 +67,8 @@ private slots:
|
|||||||
void on_text_height();
|
void on_text_height();
|
||||||
signals:
|
signals:
|
||||||
void delete_requested(QString); // 请求删除此消息
|
void delete_requested(QString); // 请求删除此消息
|
||||||
void height_changed(); // 新增高度变化信号
|
void current_row_height_changed();
|
||||||
|
void fold_row_height_changed();
|
||||||
private:
|
private:
|
||||||
void connect_signals(msg_type msg_type_mode); // 连接信号和槽
|
void connect_signals(msg_type msg_type_mode); // 连接信号和槽
|
||||||
private:
|
private:
|
||||||
|
|||||||
@ -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, SIGNAL(delete_requested(QString)), this, SLOT(on_msg_remove(QString)));
|
||||||
// 连接高度变化信号
|
// 连接高度变化信号
|
||||||
connect(message_widget, &ctai_history_textedit::height_changed,
|
connect(message_widget, &ctai_history_textedit::current_row_height_changed,this, &ctai_history_widget::on_current_rows_height_changed);
|
||||||
this, &ctai_history_widget::on_widget_height_changed);
|
connect(message_widget, &ctai_history_textedit::fold_row_height_changed,this, &ctai_history_widget::on_rows_height_changed);
|
||||||
// 滚动到新消息
|
// 滚动到新消息
|
||||||
scrollToItem(item(row, 0));
|
scrollToItem(item(row, 0));
|
||||||
}
|
}
|
||||||
// 立即滚动到底部
|
// 立即滚动到底部
|
||||||
scrollToBottom();
|
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()) {
|
for (int row = 0; row < rowCount(); ++row) {
|
||||||
int currentHeight = rowHeight(row);
|
|
||||||
setRowHeight(row, currentHeight + 100);
|
|
||||||
resizeRowToContents(row);
|
resizeRowToContents(row);
|
||||||
}
|
}
|
||||||
|
scrollToBottom();
|
||||||
|
}
|
||||||
|
//适合自适应流式输出内容
|
||||||
|
void ctai_history_widget::on_current_rows_height_changed()
|
||||||
|
{
|
||||||
|
// 当前行高自适应
|
||||||
|
resizeRowToContents(rowCount());
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
}
|
}
|
||||||
void ctai_history_widget::on_msg_remove(QString send_id)
|
void ctai_history_widget::on_msg_remove(QString send_id)
|
||||||
|
|||||||
@ -24,6 +24,7 @@ private:
|
|||||||
void update_stored_data(model_data* stored_data, const model_data& message);
|
void update_stored_data(model_data* stored_data, const model_data& message);
|
||||||
private slots:
|
private slots:
|
||||||
void on_msg_remove(QString send_id);
|
void on_msg_remove(QString send_id);
|
||||||
void on_widget_height_changed(); // 新增槽函数处理高度变化
|
void on_rows_height_changed(); // 新增槽函数处理高度变化
|
||||||
|
void on_current_rows_height_changed();
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
@ -15,7 +15,7 @@ QString md_to_html(const QString& text) {
|
|||||||
// 转换回QString并处理代码块
|
// 转换回QString并处理代码块
|
||||||
QString result = QString::fromUtf8(html);
|
QString result = QString::fromUtf8(html);
|
||||||
free(html);
|
free(html);
|
||||||
qDebug()<<"处理的块:"<<result;
|
//qDebug()<<"处理的块:"<<result;
|
||||||
return process_code_blocks(result.toUtf8());
|
return process_code_blocks(result.toUtf8());
|
||||||
}
|
}
|
||||||
QString process_code_blocks(QString html) {
|
QString process_code_blocks(QString html) {
|
||||||
|
|||||||
@ -48,7 +48,7 @@ void ctai_session_info::init_sub_layout()
|
|||||||
m_sub_systeam_widget->setLayout(m_session_systeam_info_layout);
|
m_sub_systeam_widget->setLayout(m_session_systeam_info_layout);
|
||||||
|
|
||||||
m_session_user_group_layout = new QVBoxLayout();
|
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 = new QVBoxLayout();
|
||||||
m_session_user_edit_layout->addWidget(m_session_user_edit_info);
|
m_session_user_edit_layout->addWidget(m_session_user_edit_info);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user