增加了AI智能生图模块

This commit is contained in:
杨胜毅 2024-09-30 17:52:02 +08:00
parent aeaa45ac2c
commit b19f1351ca
6 changed files with 1580 additions and 30 deletions

View File

@ -1,10 +1,15 @@
QT += core gui svg KWindowSystem multimedia multimediawidgets concurrent
QT += core gui svg KWindowSystem multimedia multimediawidgets concurrent dbus printsupport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11 link_pkgconfig
PKGCONFIG += kysdk-qtwidgets kysdk-waylandhelper kysdk-sysinfo kysdk-datacollect gsettings-qt
KF5 += i18n doctools guiaddons widgetsaddons kio xmlgui textwidgets jobwidgets
PKGCONFIG += kysdk-qtwidgets kysdk-waylandhelper kysdk-sysinfo kysdk-datacollect gsettings-qt kylin-ai-base jsoncpp json-glib-1.0
INCLUDEPATH += /usr/include/KF5
INCLUDEPATH += /usr/include/KF5/KI18n
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
@ -50,7 +55,8 @@ SOURCES += \
src/module/plymouththemefeature.cpp \
src/module/plymouththemewidget.cpp \
src/module/savepathdialog.cpp \
src/titlebar.cpp
src/titlebar.cpp \
src/aithemebar.cpp
HEADERS += \
src/build/build.h \
@ -84,7 +90,8 @@ HEADERS += \
src/module/plymouththemefeature.h \
src/module/plymouththemewidget.h \
src/module/savepathdialog.h \
src/titlebar.h
src/titlebar.h \
src/aithemebar.h
RESOURCES += \
resource.qrc
@ -103,4 +110,5 @@ appdesktop.files += kylin-theme-builder.desktop
appdesktop.path = /usr/share/applications/
INSTALLS +=target qm_files appdesktop
LIBS += -lukui-log4qt
LIBS += -lukui-log4qt -L/usr/lib/x86_64-linux-gnu -lKF5I18n

1242
src/aithemebar.cpp Normal file

File diff suppressed because it is too large Load Diff

253
src/aithemebar.h Normal file
View File

@ -0,0 +1,253 @@
/*
Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef KP_AI_DRAWINGBAR_H
#define KP_AI_DRAWINGBAR_H
#include <QWidget>
#include <QWidget>
#include <QPushButton>
#include <QButtonGroup>
#include <QLabel>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QDialog>
#include <QPixmap>
#include <QStackedWidget>
#include <QScrollArea>
#include <QScrollBar>
#include <QPair>
#include <QList>
#include <QComboBox>
#include <QThread>
#include <QMouseEvent>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDir>
#include <QGSettings>
#include <ai-base/modelconfig.h>
#include <ai-base/vision.h>
#include <kylin-ai/config.h>
#define LOADING_IMAGE "/usr/share/kolourpaint/pics/loading.gif"
#define BG_IMAGE "/usr/share/kolourpaint/pics/bg@2x.png"
#define SLOGO_IMAGE "/usr/share/kolourpaint/pics/slogo-Drawing.svg"
#define DRAWING_IMAGE "/usr/share/kolourpaint/pics/AI-Drawing@2x.png"
#define KYLIN_AI_DBUS "com.kylin.aiassistant"
#define KYLIN_AI_PATH "/com/kylin/aiassistant"
#define AI_GSETTINGS "org.kylin.aiassistant.settings"
#define AI_KEY "isVisionSetup"
/**
* @brief The AIModeDataThread class
* 线AI的服务
*/
class AIModeDataThread : public QObject
{
Q_OBJECT
public:
AIModeDataThread();
~AIModeDataThread();
bool setSessionStatus();
VisionSession getVisionSession(){
return session;
}
bool someFunctionInThread();
void setAIData (QString str, QSize size, int quantity, VisionImageStyle Style){
textBox = str;
AIImageSize = size;
quantityNum = quantity;
ImageStyle = Style;
}
signals:
void finishedSignal(bool flag, QPixmap pixmap, int imageIndex, int imageSum, int errorCode);
private:
void sendErrorSignal(int ret){
emit finishedSignal(false, QPixmap(), 0, 1, ret);
for(int i = 0; i < quantityNum; i++)
emit finishedSignal(true, QPixmap(), i, quantityNum, ret);
}
static void generateImageCallback(VisionImageData imageData, void* userData);
private:
QPixmap pixmap;
int quantityNum = 1; //数量按钮
QSize AIImageSize;
QString textBox;
VisionImageStyle ImageStyle;
bool m_sessionFlag = false;
VisionSession session;
};
/**
* @brief The imageClickableLabel class
* QLabel点击事件类
*/
class imageClickableLabel : public QLabel
{
Q_OBJECT
signals:
void labelClicked(const QPixmap &pixmap);
public:
imageClickableLabel(const QPixmap &pixmap, QWidget *parent = nullptr) : QLabel(parent), pixmap(pixmap){}
protected:
void mousePressEvent(QMouseEvent *event) override
{
Q_UNUSED(event);
emit labelClicked(pixmap);
}
private:
QPixmap pixmap;
};
class aithemebar : public QWidget
{
Q_OBJECT
signals:
void imageClicked(const QPixmap &pixmap);
public:
aithemebar(QWidget *parent);
~aithemebar();
public:
void initUI();
bool getConfigStatus();
void getconfigVersionData();
void loginConfigurationPage();
void setAiPixmap(QPixmap pixmap,int imageIndex, QGridLayout *pageLayout);
void updatePageLabel();
QSize getImageLabelSize();
bool eventFilter(QObject *watched, QEvent *event);
void handleLabelClicked(QObject *clickedObject);
QPixmap createPixmapWithText(const QString &text);
QPixmap AIGeneratesErrorHandling(int errorCode);
private slots:
void onConfigButtonClicked(); //配置按钮回调函数
void onConfigSettingsChange(const QString &key); //AI配置成功后回调
void onGenerateButtonClicked(); //生成按钮回调函数
void handleInputTextChanged(); //文字变化回调函数
void showPreviousPage(); //显示上一页
void showNextPage(); //显示下一页
void deleteCurrentPage();
void setEnabledDeleteButton();
void handleLabelClick(const QPixmap &pixmap);
void addPageAndStoreImages();
void onStyleMoreButtonClick();
void startThread() {
if (m_AiMode->setSessionStatus()){
m_AiMode->setAIData(textBox, AIImageSize, quantityNum, ImageStyle);
m_AiMode->someFunctionInThread();
}
}
void onThreadFinished(bool flag, QPixmap pixmap, int imageIndex, int imageSum, int errorCode);
private:
void updateImageGrid();
void updateResultGrid();
void deleteImageGridWidget();
void addResolutionComboBox();
void addQuantityComboBox();
QJsonArray readResolutionsFromJson(QString key);
int readNumFromJson(QString key);
QWidget *addAllStyleLabel(QString iconPath, QString labelText, int line);
QWidget *addStyleLabel(QString iconPath, QString labelText, int line);
void addStyleWidget();
void addStyleGrid(QWidget *widget, int line);
private:
int imageColumn = 2;//显示列表列数
int quantityNum = 1; //数量按钮
int show_quantityNum = 1; //数量按钮
int currentPageIndx = -1;//切换页第几页
bool styleFlag; //风格标志位,用于判断按下哪一个风格
bool deleteButtonFlag = true;
bool buildButtonFlag;
QGSettings *AiSettings; //监听配置信息
QSize AIImageSize; //图片大小
QSize showAIImageSize;
QMovie *movie = nullptr;//生成等待图标
QLabel *resultLabel; //生成结果 label
QLabel *pageLabel; //第几页面label
QLabel *selectedIconLabel;//风格图片选择Label
QLabel *selectedTextLabel;//风格文字选择Label
QWidget *configWidget; //立即配置页面
QWidget *widget_low_poly; //多边形风格
QString textBox; //图像描述文字
QString colorfulButtonStyle; //立即生成按钮渐变色
QString colorGrayButtonStyle;//立即生成按钮置灰
QBoxLayout *vbox; //页面布局
QHBoxLayout *resultGrid; //生成结果布局
QGridLayout *styleGrid; //风格布局
QPushButton *moreButton; //更多按钮
QScrollArea *scrollArea; //滚动窗口
QPushButton *buildButton; //立即生成按钮
QPushButton *deleteButton; //删除按钮
QComboBox *resolutionComboBox; //图像分辨率
QComboBox *quantityComboBox; //生成数量
QStackedWidget *stackedWidget; //生成结果的换页窗口
QVector<QVector<QPixmap>> imagesVector; //生成图像数据集成
VisionSession mSession = nullptr;
VisionImageStyle ImageStyle; //风格样式
AIModeDataThread *m_AiMode; //AI生成程序
};
#endif

View File

@ -153,8 +153,8 @@ void MainWindow::initUI()
// this->setPalette(palette);
this->setBackgroundRole(QPalette::Base);
this->setAutoFillBackground(true);
this->setMinimumSize(1280, 720);
// this->setMinimumSize(1280, 720);
this->setMinimumSize(1600, 900);
m_maininterface = new MainInterface();
m_entrywidget = new QWidget();

View File

@ -20,6 +20,8 @@
#include "grubthemewidget.h"
#include "../fileProcess/filecheck.h"
#include <QFileDialog>
#include "../aithemebar.h"
#include <QMainWindow>
bool GrubThemeWidget::g_themeChange;
/**
@ -33,20 +35,34 @@ GrubThemeWidget::GrubThemeWidget(QWidget *parent) : QWidget(parent)
{
initPreviewWidget();
initRightWidget();
QHBoxLayout *grubthemelayout = new QHBoxLayout();
QPushButton *line = new QPushButton();
line->setDisabled(true);
line->setFixedWidth(1);
line->setFixedHeight(2000);
grubthemelayout->addWidget(m_previewwidget);
grubthemelayout->addWidget(line);
grubthemelayout->addWidget(m_rightwidget);
m_grubthemelayout = new QHBoxLayout();
// grubthemelayout->setSizeConstraint(QLayout::SetMinimumSize);
QPushButton *line1 = new QPushButton();
line1->setDisabled(true);
line1->setFixedWidth(1);
line1->setFixedHeight(2000);
grubthemelayout->setContentsMargins(0,0,0,0);
grubthemelayout->setSpacing(0);
QPushButton *line2 = new QPushButton();
line2->setDisabled(true);
line2->setFixedWidth(1);
line2->setFixedHeight(2000);
this->setLayout(grubthemelayout);
// 主布局添加控件
// m_grubthemelayout->addStretch(1);
m_grubthemelayout->addWidget(m_previewwidget);
m_grubthemelayout->addWidget(line1); // 分割线
m_grubthemelayout->addWidget(m_rightwidget);
m_grubthemelayout->addWidget(line2); // 分割线
initAithemeBar();
// m_grubthemelayout->addWidget(m_aithemebar);
// m_grubthemelayout->addStretch(1);
m_grubthemelayout->setContentsMargins(0,0,0,0);
m_grubthemelayout->setSpacing(0);
this->setLayout(m_grubthemelayout);
}
/**
@ -73,6 +89,34 @@ void GrubThemeWidget::initPreviewWidget()
m_previewwidget->setLayout(m_previewlayout);
}
/**
* @brief Ai生图小部件
* QWidgetgrub布局
* label clicked
*/
void GrubThemeWidget::initAithemeBar()
{
// 创建 aithemebar 实例
m_aithemebar = new aithemebar(this);
m_aithemebar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// 设置最小和最大宽度
m_aithemebar->setMinimumSize(250, 620);
m_aithemebar->setMaximumWidth(250);
m_aithemebar->setBackgroundRole(QPalette::Base);
m_aithemebar->setAutoFillBackground(true);
// 设置 aithemebar 的固定宽度
m_aithemebar->setFixedWidth(250);
// 在布局中为 aithemebar 分配足够的空间
QSpacerItem *spacer = new QSpacerItem(15, 0, QSizePolicy::Fixed, QSizePolicy::Minimum);
m_grubthemelayout->addItem(spacer);
m_grubthemelayout->addWidget(m_aithemebar);
}
/**
* @brief
*
@ -134,7 +178,7 @@ void GrubThemeWidget::initEditWidget()
grubAddPicWidget *a = new grubAddPicWidget();
QHBoxLayout *importlayout = new QHBoxLayout();
importlayout->addStretch(1);
importlayout->addStretch(1);
importlayout->addWidget(m_showBtn);
importlayout->addStretch(1);
importlayout->addWidget(a);
@ -229,6 +273,14 @@ void GrubThemeWidget::refresh()
m_showBtn->setIconSize(QSize(178,122));
}
void GrubThemeWidget::updateAIImage()
{
m_preview->updateBackground(resourceGrubPath);
QPixmap pixmap(resourceGrubPath);
m_showBtn->setIcon(QIcon(pixmap));
m_showBtn->setIconSize(QSize(178,122));
}
void GrubThemeWidget::useMaximumScale()
{
m_preview->maxscale();
@ -250,15 +302,5 @@ void GrubThemeWidget::setBuildBtnText(bool isglobal)
void GrubThemeWidget::addspaceritem()
{
QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding);
QSpacerItem *spacer2 = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding);
spacer->changeSize(m_previewwidget->width(), 155, QSizePolicy::Maximum, QSizePolicy::Maximum);
spacer2->changeSize(m_previewwidget->width(), 159, QSizePolicy::Maximum, QSizePolicy::Maximum);
// m_previewlayout->addItem(spacer);
m_previewlayout->addWidget(m_preview);
// m_previewlayout->addItem(spacer2);
}

View File

@ -23,6 +23,7 @@
#include "grubthemefeature.h"
#include "historywidget.h"
#include "basewidget.h"
#include "../aithemebar.h"
#include <QObject>
#include <QWidget>
@ -39,12 +40,14 @@ public:
explicit GrubThemeWidget(QWidget *parent = nullptr);
void initPreviewWidget();
void initRightWidget();
void initAithemeBar();
void addspaceritem();
void initEditWidget();
void initEditWidget();
void initBuildWidget();
static bool g_themeChange;
void eidtInitWidget(const HistoryInfo &InfoData);
void refresh();
void updateAIImage();
void useMaximumScale();
void useRestoreScale();
void setBuildBtnText(bool isglobal);
@ -56,8 +59,10 @@ private:
BasePreviewWidget *m_previewwidget;
GrubImageWidget *m_preview;
QWidget *m_rightwidget;
QHBoxLayout *m_grubthemelayout;
QWidget *m_grubwidget;
QWidget *m_buildwidget;
aithemebar *m_aithemebar;
QVBoxLayout *m_previewlayout;
GrubEditButton *m_showBtn;
QPushButton *m_buildButton;