Skip to content

Commit

Permalink
feat: add translate
Browse files Browse the repository at this point in the history
allow translate selected code
  • Loading branch information
Lighto-Ku authored and deepin-mozart committed Nov 16, 2023
1 parent e530e2c commit a1ed3bb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/plugins/codegeex/codegeexmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "codegeexmanager.h"
#include "copilot.h"
#include "common/util/custompaths.h"

#include <QDebug>
Expand Down Expand Up @@ -141,13 +142,20 @@ void CodeGeeXManager::recevieLoginState(AskApi::LoginState loginState)
qWarning() << "CodeGeeX login failed!";
// switch to login ui.
} else if (loginState == AskApi::LoginState::kLoginSuccess) {
isLogin = true;
Q_EMIT loginSuccessed();
// switch to ask page.
queryTimer->stop();
queryTimer->deleteLater();
}
}

void CodeGeeXManager::recevieToTranslate(const QString &codeText)
{
if (isLogin && !codeText.isEmpty())
Q_EMIT requestToTransCode(codeText);
}

CodeGeeXManager::CodeGeeXManager(QObject *parent)
: QObject(parent)
{
Expand All @@ -161,6 +169,7 @@ void CodeGeeXManager::initConnections()
connect(&askApi, &AskApi::response, this, &CodeGeeXManager::onResponse);
connect(&askApi, &AskApi::loginState, this, &CodeGeeXManager::recevieLoginState);
connect(&askApi, &AskApi::sessionCreated, this, &CodeGeeXManager::onSessionCreated);
connect(Copilot::instance(), &Copilot::translatingText, this, &CodeGeeXManager::recevieToTranslate);
}

void CodeGeeXManager::queryLoginState()
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/codegeex/codegeexmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ class CodeGeeXManager : public QObject
void loginSuccessed();
void createdNewSession();
void requestMessageUpdate(const MessageData &msg);
void requestToTransCode(const QString &code);
void chatFinished();

public Q_SLOTS:
void onSessionCreated(const QString &talkId, bool isSuccessful);
void onResponse(const QString &msgID, const QString &data, const QString &event);
void recevieLoginState(CodeGeeX::AskApi::LoginState loginState);
void recevieToTranslate(const QString &codeText);

private:
explicit CodeGeeXManager(QObject *parent = nullptr);
Expand All @@ -57,6 +59,7 @@ public Q_SLOTS:
QString sessionId {};
QString userId {};
QString currentTalkID {};
bool isLogin { false };

QMap<QString, MessageData> curSessionMsg {};

Expand Down
14 changes: 14 additions & 0 deletions src/plugins/codegeex/widgets/codegeexwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "askpagewidget.h"
#include "translationpagewidget.h"
#include "codegeexmanager.h"
#include "copilot.h"

#include <DLabel>
#include <DStackedWidget>
Expand Down Expand Up @@ -61,6 +62,17 @@ void CodeGeeXWidget::onCreateNewBtnClicked()
CodeGeeXManager::instance()->createNewSession();
}

void CodeGeeXWidget::toTranslateCode(const QString &code)
{
currentState = TrasnlatePage;
resetHeaderBtns();

transPage->setInputEditText(code);
transPage->cleanOutputEdit();

stackWidget->setCurrentIndex(2);
}

void CodeGeeXWidget::initUI()
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
Expand Down Expand Up @@ -106,11 +118,13 @@ void CodeGeeXWidget::initConnection()
{
connect(CodeGeeXManager::instance(), &CodeGeeXManager::loginSuccessed, this, &CodeGeeXWidget::onLoginSuccessed);
connect(CodeGeeXManager::instance(), &CodeGeeXManager::createdNewSession, this, &CodeGeeXWidget::onNewSessionCreated);
connect(CodeGeeXManager::instance(), &CodeGeeXManager::requestToTransCode, this, &CodeGeeXWidget::toTranslateCode);
}

void CodeGeeXWidget::initAskWidget()
{
tabBar = new DTabBar(this);
tabBar->setVisibleAddButton(false);
tabBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
tabBar->setContentsMargins(0, 0, 0, 0);
stackWidget = new QStackedWidget(this);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/codegeex/widgets/codegeexwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public Q_SLOTS:
void onDeleteBtnClicked();
void onHistoryBtnClicked();
void onCreateNewBtnClicked();
void toTranslateCode(const QString &code);

private:
void initUI();
Expand Down
12 changes: 12 additions & 0 deletions src/plugins/codegeex/widgets/translationpagewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ TranslationPageWidget::TranslationPageWidget(QWidget *parent)
initConnection();
}

void TranslationPageWidget::setInputEditText(const QString &text)
{
if (inputEdit)
inputEdit->updateCode(text);
}

void TranslationPageWidget::cleanOutputEdit()
{
if (outputEdit)
outputEdit->updateCode("");
}

void TranslationPageWidget::onTranslateBtnClicked()
{
QString dstLang = langComboBox->currentText();
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/codegeex/widgets/translationpagewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class TranslationPageWidget : public DWidget
Q_OBJECT
public:
explicit TranslationPageWidget(QWidget *parent = nullptr);
void setInputEditText(const QString &text);
void cleanOutputEdit();

public Q_SLOTS:
void onTranslateBtnClicked();
Expand Down

0 comments on commit a1ed3bb

Please sign in to comment.