From cab775e05b7b65e64242c06253dac9a7df8a20c5 Mon Sep 17 00:00:00 2001 From: shangxiaoyang Date: Mon, 15 Apr 2024 11:03:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 9 +- debian/control | 4 +- widgets/toolbars/kpAiDrawingBar.cpp | 162 +++++++++++++++++++++------- widgets/toolbars/kpAiDrawingBar.h | 15 ++- 4 files changed, 144 insertions(+), 46 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c2c0664..d32ecd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,7 +72,12 @@ include_directories(${AIBASE_INCLUDE_DIRS}) pkg_check_modules(Gsetting REQUIRED gsettings-qt) include_directories(${Gsetting_INCLUDE_DIRS}) - +pkg_check_modules(Jsoncpp REQUIRED jsoncpp) +include_directories(${Jsoncpp_INCLUDE_DIRS}) + +pkg_check_modules(JsonGlib REQUIRED json-glib-1.0) +include_directories(${JsonGlib_INCLUDE_DIRS}) + ## Generate header with version number ecm_setup_version(${RELEASE_SERVICE_VERSION} VARIABLE_PREFIX KOLOURPAINT @@ -357,6 +362,8 @@ target_link_libraries(kolourpaint ${KSANE_LIBRARIES} ${AIBASE_LIBRARIES} ${Gsetting_LIBRARIES} + ${Jsoncpp_LIBRARIES} + ${JsonGlib_LIBRARIES} # ${LIBUKUI_LOG4QT_PATH} kolourpaint_lgpl ) diff --git a/debian/control b/debian/control index 02ec5eb..d8eaae5 100644 --- a/debian/control +++ b/debian/control @@ -20,6 +20,8 @@ Build-Depends: cmake (>= 3.16.0~), qtbase5-dev (>= 5.15.2~), libkylin-ai-base-dev, libgsettings-qt-dev, + libjsoncpp-dev, + libjson-glib-dev, Standards-Version: 4.6.1 Rules-Requires-Root: no Homepage: https://apps.kde.org/en/kolourpaint @@ -29,7 +31,7 @@ Vcs-Git: https://gitee.com/openkylin/kolourpaint.git Package: kolourpaint Section: graphics Architecture: any -Depends: ${misc:Depends}, ${shlibs:Depends}, libkylin-ai-base0 +Depends: ${misc:Depends}, ${shlibs:Depends}, kylin-aiassistant Breaks: kolourpaint4 (<< 4:17.04) Replaces: kolourpaint4 (<< 4:17.04) Description: simple image editor and drawing application diff --git a/widgets/toolbars/kpAiDrawingBar.cpp b/widgets/toolbars/kpAiDrawingBar.cpp index 37fe4f6..27b8aac 100644 --- a/widgets/toolbars/kpAiDrawingBar.cpp +++ b/widgets/toolbars/kpAiDrawingBar.cpp @@ -44,9 +44,13 @@ kpAIDrawingBar::kpAIDrawingBar(QWidget *parent) setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable); setAcceptDrops (false); + AiSettings = nullptr; + //取消登录界面绘画 + /* if(!getConfigStatus()) loginConfigurationPage(); else + */ initUI(); setFixedWidth(250); @@ -311,7 +315,6 @@ void kpAIDrawingBar::initUI() buildButton->setFixedSize(224,36); buildButton->setStyleSheet(colorGrayButtonStyle); buildButton->setEnabled(false); - buildButtonFlag = false; /* 分页功能 */ stackedWidget = new QStackedWidget(centralWidget); @@ -322,21 +325,30 @@ void kpAIDrawingBar::initUI() QPushButton *previousButton = new QPushButton(this); pageLabel = new QLabel(tr("1")); QPushButton *nextButton = new QPushButton(this); + deleteButton = new QPushButton(this); resultLabel->setFixedSize(80,30); - previousButton->setIcon(QIcon::fromTheme("go-previous-symbolic")); - previousButton->setFixedSize(30,30); + + previousButton->setIcon(QIcon::fromTheme("ukui-start-symbolic")); + previousButton->setFixedSize(20,20); previousButton->setStyleSheet("QPushButton { background-color: transparent; }"); + pageLabel->setFixedSize(50,30); pageLabel->setAlignment(Qt::AlignCenter); - nextButton->setFixedSize(30,30); - nextButton->setIcon(QIcon::fromTheme("go-previous-symbolic-rtl")); + + nextButton->setFixedSize(20,20); + nextButton->setIcon(QIcon::fromTheme("ukui-end-symbolic")); nextButton->setStyleSheet("QPushButton { background-color: transparent; }"); + deleteButton->setIcon(QIcon::fromTheme("edit-delete-symbolic")); + deleteButton->setFixedSize(24,24); + deleteButton->setStyleSheet("QPushButton { background-color: transparent; }"); + resultGrid->addWidget(resultLabel); resultGrid->addStretch(); resultGrid->addWidget(previousButton); resultGrid->addWidget(pageLabel); resultGrid->addWidget(nextButton); + resultGrid->addWidget(deleteButton); resultGrid->setAlignment(pageLabel, Qt::AlignRight); /* 设置整体布局 */ @@ -380,8 +392,11 @@ void kpAIDrawingBar::initUI() connect(inputBox,&QTextEdit::textChanged, this, &kpAIDrawingBar::handleInputTextChanged); connect(previousButton, &QPushButton::clicked, this, &kpAIDrawingBar::showPreviousPage); connect(nextButton, &QPushButton::clicked, this, &kpAIDrawingBar::showNextPage); + connect(deleteButton, &QPushButton::clicked, this, &kpAIDrawingBar::deleteCurrentPage); connect(&aiThread, &AIModeDataThread::finishedSignal, this, &kpAIDrawingBar::onThreadFinished); + centralWidget->setFixedWidth(240); + scrollArea->setWidget(centralWidget); scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -411,7 +426,7 @@ void kpAIDrawingBar::addStyleWidget() "style-realistic","style-ukiyoe"}; QStringList styleMoreList ={ "style-futurism", "style-pixel", "style-conceptual", "style-cyberpunk", - "style-lolita", "style-baroque", "style-surrealism", "style-watercolour", + "style-baroque", "style-surrealism", "style-watercolour", //头部删除"style-lolita" "style-vaporwave", "style-oil-painting","style-cartoon"}; @@ -470,7 +485,7 @@ void kpAIDrawingBar::onStyleMoreButtonClick() styleGrid->removeWidget(moreButton); addStyleGrid(widget_low_poly, 5); - styleGrid->addWidget(moreButton, 5, 2,Qt::AlignTop); + styleGrid->addWidget(moreButton, 5, 1,Qt::AlignTop); } else { for (int i = 5; i < styleGrid->count()-1; ++i) { @@ -482,7 +497,7 @@ void kpAIDrawingBar::onStyleMoreButtonClick() styleGrid->removeWidget(moreButton); styleGrid->removeWidget(widget_low_poly); - addStyleGrid(widget_low_poly, 17); + addStyleGrid(widget_low_poly, 16); styleGrid->addWidget(moreButton, 1, 2, Qt::AlignTop); } @@ -626,6 +641,9 @@ void kpAIDrawingBar::handleLabelClicked(QObject *clickedObject) selectedIconLabel = iconLabel; selectedTextLabel = textLabel; int identifier = textLabel->property("labelStyle").toInt(); + if (identifier > 9){ + identifier += 1; + } ImageStyle = (VisionImageStyle)identifier; } @@ -767,15 +785,12 @@ void kpAIDrawingBar::onGenerateButtonClicked() buildButton->setEnabled(false); buildButton->setText(i18n("In formation...")); buildButton->setStyleSheet(colorGrayButtonStyle); - buildButtonFlag = true; + if(!deleteButton->isEnabled()) + deleteButton->setEnabled(true); + + // 更新结果显示 + updateResultGrid(); - // 显示resultGrid中的所有子控件 - for (int i = 0; i < resultGrid->count(); ++i) - { - QLayoutItem *item = resultGrid->itemAt(i); - if (item && item->widget()) - item->widget()->setVisible(true); - } // 更新图像 updateImageGrid(); } @@ -789,7 +804,7 @@ void kpAIDrawingBar::handleInputTextChanged() //考虑添加生成中判断 QString userText = static_cast(findChild())->toPlainText().trimmed(); bool enableButton = !userText.isEmpty(); - if (enableButton && !buildButton->isEnabled() && !buildButtonFlag) { + if (enableButton && !buildButton->isEnabled()) { qDebug()<<"buildButton set true"; buildButton->setEnabled(enableButton); buildButton->setStyleSheet(colorfulButtonStyle); @@ -871,11 +886,15 @@ QPixmap kpAIDrawingBar::createPixmapWithText(const QString &text) QColor color("#B8B8B8"); QPainter painter(&pixmap); - QFontMetrics metrics(painter.font()); - QString wrappedText = metrics.elidedText(i18n(text.toLocal8Bit().data()), Qt::ElideRight, pixmap.rect().width()); + // 设置字体和字体大小,这里假设使用默认字体,根据实际需求可自定义 + QFont font = painter.font(); + // 若需要调整字体大小以适应换行,请在此处设置,例如:font.setPointSize(10); + + // 计算文本在给定宽度下的自动换行字符串 + QString wrappedText = fontMetrics().elidedText(text, Qt::ElideNone, pixmap.rect().width(), Qt::TextWordWrap); painter.setPen(color); - painter.drawText(pixmap.rect(), Qt::AlignCenter, wrappedText); + painter.drawText(pixmap.rect(), Qt::AlignCenter | Qt::TextWordWrap, wrappedText); painter.end(); qDebug()<<"Error message:"<setEnabled(true); return; } if (imageIndex == 0){ @@ -964,7 +985,6 @@ void kpAIDrawingBar::onThreadFinished(bool flag, QPixmap pixmap, int imageIndex, buildButton->setEnabled(true); buildButton->setText(i18n("Generate now")); buildButton->setStyleSheet(colorfulButtonStyle);//设置立即生成按钮为立即生成 - buildButtonFlag = false; addPageAndStoreImages(); } aiThread.exit(); @@ -980,11 +1000,22 @@ bool AIModeDataThread::someFunctionInThread() VisionSession session = nullptr; VisionResult ret = vision_create_session(&session); + if (ret != VISION_SUCCESS) { - qDebug()<<"VisionResult 创建失败 VisionResult = "<= 2) { + else if (show_quantityNum >= 2) { if(configVersion == 2){ - if(ratioNum == 0) + if(show_ratioNum == 0) labelSize = QSize(108, 60); - else if (ratioNum == 1) + else if (show_ratioNum == 1) labelSize = QSize(108, 108); - else if (ratioNum == 2) + else if (show_ratioNum == 2) labelSize = QSize(108, 192); else labelSize = QSize(108, 60); } else if(configVersion == 1){ - if(ratioNum == 0) + if(show_ratioNum == 0) labelSize = QSize(108, 72); - else if (ratioNum == 1) + else if (show_ratioNum == 1) labelSize = QSize(108, 108); - else if (ratioNum == 2) + else if (show_ratioNum == 2) labelSize = QSize(108, 162); else labelSize = QSize(108, 72); @@ -1095,7 +1128,7 @@ void kpAIDrawingBar::deleteImageGridWidget() { // 清空之前的图像 if(stackedWidget->count() > 0 ){ - QWidget *w = stackedWidget->currentWidget(); + QWidget *w = stackedWidget->widget(stackedWidget->count() - 1); stackedWidget->removeWidget(w); w->deleteLater(); } @@ -1128,6 +1161,7 @@ void kpAIDrawingBar::showPreviousPage() stackedWidget->setCurrentIndex(currentPageIndx); updatePageLabel(); } + setEnabledDeleteButton(); qDebug()<<"stackedWidget->count() = "<count()<<"currentPageIndx="<setCurrentIndex(currentPageIndx); updatePageLabel(); } + setEnabledDeleteButton(); qDebug()<<"stackedWidget->count() = "<count()<<"currentPageIndx="<removeWidget(stackedWidget->currentWidget()); + updatePageLabel(); + if(stackedWidget->count() <= 1){ + updateResultGrid(); + if (currentPageIndx == -1) + deleteButton->setEnabled(false); + else + setEnabledDeleteButton(); + } + else + setEnabledDeleteButton(); +} + +void kpAIDrawingBar::setEnabledDeleteButton() +{ + if(!deleteButtonFlag && currentPageIndx == stackedWidget->count() - 1){ + deleteButton->setEnabled(false); + } + else { + deleteButton->setEnabled(true); + } +} +void kpAIDrawingBar::updateResultGrid() +{ + qDebug()<<"updateResultGrid: stackedWidget->count() === "<count(); + // 显示resultGrid中的所有子控件 + for (int i = 0; i < resultGrid->count(); ++i) + { + QLayoutItem *item = resultGrid->itemAt(i);; + if (stackedWidget->count() < 1 && (i == 2 || i == 3 || i ==4)){ + if (item && item->widget()){ + item->widget()->setVisible(false); + } + continue; + } + if (item && item->widget()){ + item->widget()->setVisible(true); + } + } +} + /** * @brief kpAIDrawingBar::updateImageGrid * 更新、等待QLabel相关界面,并开启线程运行 */ void kpAIDrawingBar::updateImageGrid() { + show_quantityNum = quantityNum; + show_ratioNum = ratioNum; + QSize labelSize = getImageLabelSize(); QWidget *newWidget = new QWidget(); @@ -1189,6 +1270,9 @@ void kpAIDrawingBar::updateImageGrid() qDebug()<<"updateImageGrid : currentPageIndx = "<count(); updatePageLabel(); + deleteButtonFlag = false; + deleteButton->setEnabled(false); + qDebug()< #include #include - #include #include @@ -166,6 +165,8 @@ private slots: void showPreviousPage(); //显示上一页 void showNextPage(); //显示下一页 + void deleteCurrentPage(); + void setEnabledDeleteButton(); void handleLabelClick(const QPixmap &pixmap); void addPageAndStoreImages(); @@ -180,6 +181,7 @@ private slots: private: void updateImageGrid(); + void updateResultGrid(); void deleteImageGridWidget(); void addRatioButtons(QGridLayout *layout, QString name, int line); @@ -193,14 +195,16 @@ private: void addStyleGrid(QWidget *widget, int line); private: - int quantityNum = 4; //数量按钮 - int ratioNum = 0; //比例按钮 int multipleNum = 1; //倍数按钮 int imageColumn = 2;//显示列表列数 - int currentPageIndx = 0;//切换页第几页 + int quantityNum = 4; //数量按钮 + int ratioNum = 0; //比例按钮 + int show_quantityNum = 4; //数量按钮 + int show_ratioNum = 0; //比例按钮 + int currentPageIndx = -1;//切换页第几页 bool styleFlag; //风格标志位,用于判断按下哪一个风格 - bool buildButtonFlag; //生成按钮是否打开 + bool deleteButtonFlag = true; QGSettings *AiSettings; //监听配置信息 QSize AIImageSize; //图片大小 @@ -225,6 +229,7 @@ private: QScrollArea *scrollArea; //滚动窗口 QPushButton *buildButton; //立即生成按钮 + QPushButton *deleteButton; //删除按钮 QButtonGroup *ratioButtonGroup; //比例按钮 QButtonGroup *quantityButtonGroup; //数量按钮