Skip to content

Commit

Permalink
feat: [ut] New plugin for unit test
Browse files Browse the repository at this point in the history
as title

Log: unit test plugin
  • Loading branch information
Kakueeen committed Jan 3, 2025
1 parent 983b499 commit 3f3179f
Show file tree
Hide file tree
Showing 50 changed files with 3,829 additions and 17 deletions.
85 changes: 85 additions & 0 deletions src/common/util/spinnerpainter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "spinnerpainter.h"

#include <QtMath>

Check warning on line 7 in src/common/util/spinnerpainter.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QtMath> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 7 in src/common/util/spinnerpainter.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QtMath> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QPainterPath>

Check warning on line 8 in src/common/util/spinnerpainter.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPainterPath> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 8 in src/common/util/spinnerpainter.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QPainterPath> not found. Please note: Cppcheck does not need standard library headers to get proper results.

SpinnerPainter::SpinnerPainter()
{
refreshTimer.setInterval(30);
QObject::connect(&refreshTimer, &QTimer::timeout, &refreshTimer,
[=]() {
currentDegree += 14;
callback();
});
}

void SpinnerPainter::paint(QPainter &painter, const QColor &color, const QRect &rect)
{
painter.save();
if (currentColor != color) {
currentColor = color;
indicatorColors.clear();
}

if (indicatorColors.isEmpty()) {
for (int i = 0; i < 3; ++i)
indicatorColors << createDefaultIndicatorColorList(color);
}

painter.setRenderHints(QPainter::Antialiasing);
auto center = QRectF(rect).center();
auto radius = qMin(rect.width(), rect.height()) / 2.0;
auto indicatorRadius = radius / 2 / 2 * 1.1;
auto indicatorDegreeDelta = 360 / indicatorColors.count();

for (int i = 0; i < indicatorColors.count(); ++i) {
auto colors = indicatorColors.value(i);
for (int j = 0; j < colors.count(); ++j) {
double degreeCurrent = currentDegree - j * indicatorShadowOffset + indicatorDegreeDelta * i;
auto x = (radius - indicatorRadius) * qCos(qDegreesToRadians(degreeCurrent));
auto y = (radius - indicatorRadius) * qSin(qDegreesToRadians(degreeCurrent));

x = center.x() + x;
y = center.y() + y;
auto tl = QPointF(x - 1 * indicatorRadius, y - 1 * indicatorRadius);
QRectF rf(tl.x(), tl.y(), indicatorRadius * 2, indicatorRadius * 2);

QPainterPath path;
path.addEllipse(rf);

painter.fillPath(path, colors.value(j));
}
}
painter.restore();
}

void SpinnerPainter::setUpdateCallback(const UpdateCallback &cb)
{
callback = cb;
}

void SpinnerPainter::startAnimation()
{
refreshTimer.start();
}

void SpinnerPainter::stopAnimation()

Check warning on line 70 in src/common/util/spinnerpainter.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'stopAnimation' is never used.

Check warning on line 70 in src/common/util/spinnerpainter.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

The function 'stopAnimation' is never used.
{
refreshTimer.stop();
}

QList<QColor> SpinnerPainter::createDefaultIndicatorColorList(QColor color)
{
QList<QColor> colors;
QList<int> opacitys;
opacitys << 100 << 30 << 15 << 10 << 5 << 4 << 3 << 2 << 1;
for (int i = 0; i < opacitys.count(); ++i) {
color.setAlpha(255 * opacitys.value(i) / 100);
colors << color;
}
return colors;
}
40 changes: 40 additions & 0 deletions src/common/util/spinnerpainter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef SPINNERPAINTER_H
#define SPINNERPAINTER_H

#include <QPainter>

Check warning on line 8 in src/common/util/spinnerpainter.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPainter> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 8 in src/common/util/spinnerpainter.h

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QPainter> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QTimer>

Check warning on line 9 in src/common/util/spinnerpainter.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTimer> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 9 in src/common/util/spinnerpainter.h

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QTimer> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <functional>

Check warning on line 11 in src/common/util/spinnerpainter.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <functional> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 11 in src/common/util/spinnerpainter.h

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <functional> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class SpinnerPainter
{
public:
SpinnerPainter();

void paint(QPainter &painter, const QColor &color, const QRect &rect);

using UpdateCallback = std::function<void()>;
void setUpdateCallback(const UpdateCallback &cb);

void startAnimation();
void stopAnimation();

protected:
QList<QColor> createDefaultIndicatorColorList(QColor color);

private:
QTimer refreshTimer;

double indicatorShadowOffset = 10;
double currentDegree = 0.0;

QList<QList<QColor>> indicatorColors;
QColor currentColor;
UpdateCallback callback;
};

#endif // SPINNERPAINTER_H
1 change: 1 addition & 0 deletions src/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ add_subdirectory(codegeex)
add_subdirectory(git)
add_subdirectory(linglong)
add_subdirectory(aimanager)
add_subdirectory(smartut)
23 changes: 12 additions & 11 deletions src/plugins/aimanager/openai/openaicompatiblellm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class OpenAiCompatibleLLMPrivate
QString modelPath { "" };
QString apiKey { "" };
double temprature { 1.0 };
int maxTokens = 0; // default not set
int maxTokens = 0; // default not set
bool stream { true };

QByteArray httpResult {};
Expand All @@ -66,7 +66,7 @@ class OpenAiCompatibleLLMPrivate
};

OpenAiCompatibleLLMPrivate::OpenAiCompatibleLLMPrivate(OpenAiCompatibleLLM *qq)
: q(qq)
: q(qq)
{
manager = new QNetworkAccessManager(qq);
currentConversation = new OpenAiCompatibleConversation();

Check warning on line 72 in src/plugins/aimanager/openai/openaicompatiblellm.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Class 'OpenAiCompatibleLLMPrivate' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).

Check warning on line 72 in src/plugins/aimanager/openai/openaicompatiblellm.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Class 'OpenAiCompatibleLLMPrivate' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).

Check warning on line 72 in src/plugins/aimanager/openai/openaicompatiblellm.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Class 'OpenAiCompatibleLLMPrivate' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).

Check warning on line 72 in src/plugins/aimanager/openai/openaicompatiblellm.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Class 'OpenAiCompatibleLLMPrivate' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).
Expand All @@ -86,7 +86,7 @@ QNetworkReply *OpenAiCompatibleLLMPrivate::postMessage(const QString &url, const
request.setRawHeader("Authorization", "Bearer " + apiKey.toUtf8());

if (QThread::currentThread() != qApp->thread()) {
QNetworkAccessManager* threadManager(new QNetworkAccessManager);
QNetworkAccessManager *threadManager(new QNetworkAccessManager);
OpenAiCompatibleLLM::connect(QThread::currentThread(), &QThread::finished, threadManager, &QNetworkAccessManager::deleteLater);
return threadManager->post(request, body);
}
Expand All @@ -101,7 +101,7 @@ QNetworkReply *OpenAiCompatibleLLMPrivate::getMessage(const QString &url, const
request.setRawHeader("Authorization", "Bearer " + apiKey.toUtf8());

if (QThread::currentThread() != qApp->thread()) {
QNetworkAccessManager* threadManager(new QNetworkAccessManager);
QNetworkAccessManager *threadManager(new QNetworkAccessManager);
OpenAiCompatibleLLM::connect(QThread::currentThread(), &QThread::finished, threadManager, &QNetworkAccessManager::deleteLater);
return threadManager->get(request);
}
Expand Down Expand Up @@ -165,13 +165,13 @@ bool OpenAiCompatibleLLM::checkValid(QString *errStr)
bool valid = false;
QString errstr;

connect(this, &AbstractLLM::dataReceived, &loop, [&, this](const QString & data, ResponseState state){
connect(this, &AbstractLLM::dataReceived, &loop, [&, this](const QString &data, ResponseState state) {
if (state == ResponseState::Receiving)
return;

if (state == ResponseState::Success) {
valid = true;
} else if (errStr != nullptr){
} else if (errStr != nullptr) {
*errStr = data;
}
loop.quit();
Expand Down Expand Up @@ -203,7 +203,7 @@ void OpenAiCompatibleLLM::request(const QJsonObject &data)

QNetworkReply *reply = d->postMessage(modelPath() + "/v1/chat/completions", d->apiKey, body);
connect(this, &OpenAiCompatibleLLM::requstCancel, reply, &QNetworkReply::abort);
connect(reply, &QNetworkReply::finished, this, [=](){
connect(reply, &QNetworkReply::finished, this, [=]() {
d->waitingResponse = false;
if (!d->httpResult.isEmpty())
d->currentConversation->update(d->httpResult);
Expand Down Expand Up @@ -235,7 +235,7 @@ void OpenAiCompatibleLLM::request(const QString &prompt)

QNetworkReply *reply = d->postMessage(modelPath() + "/v1/completions", d->apiKey, QJsonDocument(dataObject).toJson());
connect(this, &OpenAiCompatibleLLM::requstCancel, reply, &QNetworkReply::abort);
connect(reply, &QNetworkReply::finished, this, [=](){
connect(reply, &QNetworkReply::finished, this, [=]() {
d->waitingResponse = false;
if (reply->error()) {
qWarning() << "NetWork Error: " << reply->errorString();
Expand Down Expand Up @@ -266,14 +266,15 @@ void OpenAiCompatibleLLM::generate(const QString &prompt, const QString &suffix)

QNetworkReply *reply = d->postMessage(modelPath() + "/api/generate", d->apiKey, QJsonDocument(dataObject).toJson());
connect(this, &OpenAiCompatibleLLM::requstCancel, reply, &QNetworkReply::abort);
connect(reply, &QNetworkReply::finished, this, [=](){
connect(reply, &QNetworkReply::finished, this, [=]() {
d->waitingResponse = false;
if (reply->error()) {
qWarning() << "NetWork Error: " << reply->errorString();
emit dataReceived(reply->errorString(), AbstractLLM::ResponseState::Failed);
return;
} else {
emit dataReceived("", AbstractLLM::ResponseState::Success);
}
emit dataReceived("", AbstractLLM::ResponseState::Success);
reply->deleteLater();
});

processResponse(reply);
Expand Down
16 changes: 10 additions & 6 deletions src/plugins/core/gui/workspacewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ void WorkspaceWidget::registerToolBtnToWidget(DToolButton *btn, const QString &t
if (!btn)
return;

if (!btn->parent())
btn->setParent(this);

if (btn->isHidden())
toolBtnState[btn] = false;
else
toolBtnState[btn] = true;

toolBtnOfWidget.insert(title, btn);
}

Expand All @@ -68,18 +76,14 @@ void WorkspaceWidget::switchWidgetWorkspace(const QString &title)

emit expandStateChange(widget->property("canExpand").toBool());

if (!addedToController || (lastTitle == title))
return;

//update toolbtn`s state at header
for (auto btn : getToolBtnByTitle(lastTitle)) {
toolBtnState[btn] = btn->isVisible();
toolBtnState[btn] = !btn->isHidden();
btn->setVisible(false);
}

for (auto btn : getToolBtnByTitle(title)) {
if (toolBtnState.contains(btn))
btn->setVisible(toolBtnState[btn]);
btn->setVisible(toolBtnState[btn]);
}

emit workSpaceWidgeSwitched(title);
Expand Down
30 changes: 30 additions & 0 deletions src/plugins/smartut/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.0.2)

project(smartut)

FILE(GLOB_RECURSE PROJECT_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/*/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/*/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/*.json"
)

add_library(${PROJECT_NAME}
SHARED
${PROJECT_SOURCES}
smartut.qrc
)

target_link_libraries(${PROJECT_NAME}
duc-framework
duc-base
duc-services
duc-common
${QtUseModules}
${PkgUserModules}
${DtkWidget_LIBRARIES}
)

install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${PLUGIN_INSTALL_PATH})

Loading

0 comments on commit 3f3179f

Please sign in to comment.