wang преди 2 седмици
родител
ревизия
1b9d773c60
променени са 3 файла, в които са добавени 474 реда и са изтрити 0 реда
  1. 333 0
      src/algorithmprocessdialog.cpp
  2. 39 0
      src/algorithmprocessdialog.h
  3. 102 0
      src/algorithmprocessdialog.ui

+ 333 - 0
src/algorithmprocessdialog.cpp

@@ -0,0 +1,333 @@
+#include "algorithmprocessdialog.h"
+#include "ui_algorithmprocessdialog.h"
+#include <QInputDialog>
+#include <QMessageBox>
+#include <QFileDialog>
+#include <QSpinBox>
+#include <QDoubleSpinBox>
+#include <QComboBox>
+#include <QLabel>
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+#include <QPushButton>
+#include <QDialog>
+#include <QJsonObject>
+#include <QTreeWidget>
+#include <QTreeWidgetItem>
+#include <QAbstractItemView>
+#include <Qt>
+#include <opencv2/opencv.hpp>
+#include "imageprocessor.h"
+#include "algorithmparamfactory.h"
+#include "algorithmregistry.h"
+
+
+
+AlgorithmProcessDialog::AlgorithmProcessDialog(QWidget *parent) :
+    QDialog(parent),
+    ui(new Ui::AlgorithmProcessDialog),
+    algorithmProcess(nullptr)
+{
+    ui->setupUi(this);
+    
+    // Connect save and load buttons
+    connect(ui->saveButton, &QPushButton::clicked, this, &AlgorithmProcessDialog::on_saveButton_clicked);
+    connect(ui->loadButton, &QPushButton::clicked, this, &AlgorithmProcessDialog::on_loadButton_clicked);
+}
+
+AlgorithmProcessDialog::~AlgorithmProcessDialog()
+{
+    delete ui;
+}
+
+void AlgorithmProcessDialog::setAlgorithmProcess(AlgorithmProcess *process)
+{
+    algorithmProcess = process;
+    updateAlgorithmList();
+}
+
+void AlgorithmProcessDialog::updateAlgorithmList()
+{
+    if (!algorithmProcess) return;
+    
+    ui->algorithmListWidget->clear();
+    for (const auto& step : algorithmProcess->getSteps()) {
+        ui->algorithmListWidget->addItem(step.name);
+    }
+}
+
+void AlgorithmProcessDialog::on_addButton_clicked()
+{
+    if (!algorithmProcess) return;
+    
+    // Create a dialog for algorithm selection with categories
+    QDialog dialog(this);
+    dialog.setWindowTitle("Select Algorithm");
+    dialog.resize(400, 400);
+    
+    QVBoxLayout *layout = new QVBoxLayout(&dialog);
+    
+    // Create a tree widget to display algorithm categories
+    QTreeWidget *treeWidget = new QTreeWidget(&dialog);
+    treeWidget->setHeaderLabel("Algorithm Categories");
+    treeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
+    
+    // Get all algorithms from AlgorithmRegistry
+    std::vector<std::string> algorithmNames = AlgorithmRegistry::instance().getAllAlgorithmNames();
+    
+    // Create a single category for all algorithms
+    QTreeWidgetItem *categoryItem = new QTreeWidgetItem(treeWidget);
+    categoryItem->setText(0, "All Algorithms");
+    categoryItem->setExpanded(true);
+    
+    // Add algorithms to the category
+    for (const std::string& name : algorithmNames) {
+        QTreeWidgetItem *algorithmItem = new QTreeWidgetItem(categoryItem);
+        algorithmItem->setText(0, QString::fromStdString(name));
+        algorithmItem->setData(0, Qt::UserRole, QString::fromStdString(name));
+    }
+    
+    layout->addWidget(treeWidget);
+    
+    // Add buttons
+    QHBoxLayout *buttonLayout = new QHBoxLayout();
+    QPushButton *okButton = new QPushButton("OK", &dialog);
+    QPushButton *cancelButton = new QPushButton("Cancel", &dialog);
+    
+    buttonLayout->addStretch();
+    buttonLayout->addWidget(okButton);
+    buttonLayout->addWidget(cancelButton);
+    
+    layout->addLayout(buttonLayout);
+    
+    // Connect buttons
+    connect(okButton, &QPushButton::clicked, &dialog, &QDialog::accept);
+    connect(cancelButton, &QPushButton::clicked, &dialog, &QDialog::reject);
+    
+    // Execute the dialog
+    if (dialog.exec() == QDialog::Accepted) {
+        QTreeWidgetItem *selectedItem = treeWidget->currentItem();
+        if (selectedItem && selectedItem->parent()) {
+            // Get algorithm name and type
+            QString algorithmName = selectedItem->text(0);
+            QString algorithmType = selectedItem->data(0, Qt::UserRole).toString();
+            
+            // Create parameters JSON object
+            QJsonObject params;
+            
+            // Add algorithm with parameters
+            algorithmProcess->addStep(algorithmName, params);
+            updateAlgorithmList();
+        }
+    }
+}
+
+void AlgorithmProcessDialog::on_removeButton_clicked()
+{
+    if (!algorithmProcess) return;
+    
+    int currentRow = ui->algorithmListWidget->currentRow();
+    if (currentRow >= 0) {
+        algorithmProcess->removeStep(currentRow);
+        updateAlgorithmList();
+    }
+}
+
+void AlgorithmProcessDialog::on_editButton_clicked()
+{
+
+    if (!algorithmProcess) return;
+    
+    int currentRow = ui->algorithmListWidget->currentRow();
+    if (currentRow >= 0) {
+        // Get current algorithm step
+        QVector<AlgorithmProcess::AlgorithmStep> steps = algorithmProcess->getSteps();
+        AlgorithmProcess::AlgorithmStep currentStep = steps[currentRow];
+        QString selectedAlgorithm = currentStep.name;
+        QJsonObject params = currentStep.params;
+        
+        // Create parameter dialog
+        QDialog paramDialog(this);
+        paramDialog.setWindowTitle("Edit Algorithm Parameters");
+        paramDialog.resize(400, 300);
+        
+        QVBoxLayout *paramLayout = new QVBoxLayout(&paramDialog);
+        
+        // Get algorithm meta information from AlgorithmRegistry
+        AlgorithmMeta algMeta;
+        std::shared_ptr<AlgorithmBase> algorithm = AlgorithmRegistry::instance().create(selectedAlgorithm.toStdString());
+        if (algorithm) {
+            algMeta = algorithm->getParams();
+            algMeta.type = selectedAlgorithm;
+            algMeta.name = selectedAlgorithm;
+        }
+        
+        // Create parameter widget using factory
+        QWidget* paramWidget = AlgorithmParamWidgetFactory::createParamWidget(algMeta, &paramDialog);
+        paramLayout->addWidget(paramWidget);
+        
+        // OK button
+        QPushButton *okButton = new QPushButton("OK", &paramDialog);
+        paramLayout->addWidget(okButton);
+        connect(okButton, &QPushButton::clicked, &paramDialog, &QDialog::accept);
+        
+        if (paramDialog.exec() == QDialog::Accepted) {
+            // Get parameter values
+            QMap<QString, QVariant> paramValues = AlgorithmParamWidgetFactory::getParamValues(paramWidget);
+            
+            // Update params
+            for (auto it = paramValues.begin(); it != paramValues.end(); ++it) {
+                QString paramName = it.key();
+                QVariant paramValue = it.value();
+                
+                if (paramValue.type() == QVariant::Int) {
+                    params[paramName] = paramValue.toInt();
+                } else if (paramValue.type() == QVariant::Double) {
+                    params[paramName] = paramValue.toDouble();
+                } else if (paramValue.type() == QVariant::Bool) {
+                    params[paramName] = paramValue.toBool();
+                }
+            }
+        }
+        
+        // Update algorithm step with new parameters
+        steps[currentRow].params = params;
+        
+        // Clear and re-add all steps to update the algorithm process
+        algorithmProcess->clearSteps();
+        for (const auto& step : steps) {
+            algorithmProcess->addStep(step.name, step.params);
+        }
+        
+        updateAlgorithmList();
+    }
+}
+
+void AlgorithmProcessDialog::on_moveUpButton_clicked()
+{
+    if (!algorithmProcess) return;
+    
+    int currentRow = ui->algorithmListWidget->currentRow();
+    if (currentRow > 0) {
+        // Get current steps
+        QVector<AlgorithmProcess::AlgorithmStep> steps = algorithmProcess->getSteps();
+        
+        // Swap steps
+        std::swap(steps[currentRow], steps[currentRow - 1]);
+        
+        // Clear and re-add all steps
+        algorithmProcess->clearSteps();
+        for (const auto& step : steps) {
+            algorithmProcess->addStep(step.name, step.params);
+        }
+        
+        // Update list and select the new position
+        updateAlgorithmList();
+        ui->algorithmListWidget->setCurrentRow(currentRow - 1);
+    }
+}
+
+void AlgorithmProcessDialog::on_moveDownButton_clicked()
+{
+    if (!algorithmProcess) return;
+    
+    int currentRow = ui->algorithmListWidget->currentRow();
+    QVector<AlgorithmProcess::AlgorithmStep> steps = algorithmProcess->getSteps();
+    if (currentRow < steps.size() - 1) {
+        // Swap steps
+        std::swap(steps[currentRow], steps[currentRow + 1]);
+        
+        // Clear and re-add all steps
+        algorithmProcess->clearSteps();
+        for (const auto& step : steps) {
+            algorithmProcess->addStep(step.name, step.params);
+        }
+        
+        // Update list and select the new position
+        updateAlgorithmList();
+        ui->algorithmListWidget->setCurrentRow(currentRow + 1);
+    }
+}
+
+void AlgorithmProcessDialog::on_executeButton_clicked()
+{
+    if (!algorithmProcess) return;
+    
+    // Open file dialog to select image
+    QString imagePath = QFileDialog::getOpenFileName(
+        this,
+        "Select Image",
+        "",
+        "Image Files (*.png *.jpg *.bmp *.jpeg)"
+    );
+    
+    if (!imagePath.isEmpty()) {
+        // Read image using OpenCV
+        cv::Mat image = cv::imread(imagePath.toStdString());
+        if (image.empty()) {
+            QMessageBox::warning(this, "Error", "Failed to load image!");
+            return;
+        }
+        
+        // Execute algorithm process
+        cv::Mat result = algorithmProcess->execute(image);
+        
+        // Save result
+        QString savePath = QFileDialog::getSaveFileName(
+            this,
+            "Save Result",
+            "",
+            "Image Files (*.png *.jpg *.bmp *.jpeg)"
+        );
+        
+        if (!savePath.isEmpty()) {
+            cv::imwrite(savePath.toStdString(), result);
+            QMessageBox::information(this, "Success", "Image processed and saved successfully!");
+        }
+    }
+}
+
+void AlgorithmProcessDialog::on_saveButton_clicked()
+{
+    if (!algorithmProcess) return;
+    
+    // Open file dialog to select save location
+    QString filePath = QFileDialog::getSaveFileName(
+        this,
+        "Save Algorithm Process",
+        "",
+        "Algorithm Process Files (*.vm);;All Files (*.*)"
+    );
+    
+    if (!filePath.isEmpty()) {
+        // Save algorithm process
+        if (algorithmProcess->saveToFile(filePath)) {
+            QMessageBox::information(this, "Success", "Algorithm process saved successfully!");
+        } else {
+            QMessageBox::warning(this, "Error", "Failed to save algorithm process!");
+        }
+    }
+}
+
+void AlgorithmProcessDialog::on_loadButton_clicked()
+{
+    if (!algorithmProcess) return;
+    
+    // Open file dialog to select file to load
+    QString filePath = QFileDialog::getOpenFileName(
+        this,
+        "Load Algorithm Process",
+        "",
+        "Algorithm Process Files (*.vm);;All Files (*.*)"
+    );
+    
+    if (!filePath.isEmpty()) {
+        // Load algorithm process
+        if (algorithmProcess->loadFromFile(filePath)) {
+            updateAlgorithmList();
+            QMessageBox::information(this, "Success", "Algorithm process loaded successfully!");
+        } else {
+            QMessageBox::warning(this, "Error", "Failed to load algorithm process!");
+        }
+    }
+}

+ 39 - 0
src/algorithmprocessdialog.h

@@ -0,0 +1,39 @@
+#ifndef ALGORITHMPROCESSDIALOG_H
+#define ALGORITHMPROCESSDIALOG_H
+
+#include <QDialog>
+#include "algorithmprocess.h"
+
+namespace Ui {
+class AlgorithmProcessDialog;
+}
+
+class AlgorithmProcessDialog : public QDialog
+{
+    Q_OBJECT
+
+public:
+    explicit AlgorithmProcessDialog(QWidget *parent = nullptr);
+    ~AlgorithmProcessDialog();
+    
+    void setAlgorithmProcess(AlgorithmProcess *process);
+
+public  slots:
+    void on_addButton_clicked();
+    void on_removeButton_clicked();
+    void on_editButton_clicked();
+    void on_moveUpButton_clicked();
+    void on_moveDownButton_clicked();
+    void on_executeButton_clicked();
+    void on_saveButton_clicked();
+    void on_loadButton_clicked();
+
+private:
+    Ui::AlgorithmProcessDialog *ui;
+    AlgorithmProcess *algorithmProcess;
+    static QStringList algorithms;
+    
+    void updateAlgorithmList();
+};
+
+#endif // ALGORITHMPROCESSDIALOG_H

+ 102 - 0
src/algorithmprocessdialog.ui

@@ -0,0 +1,102 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>AlgorithmProcessDialog</class>
+ <widget class="QDialog" name="AlgorithmProcessDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>600</width>
+    <height>400</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>算法流程</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <widget class="QListWidget" name="algorithmListWidget">
+     <property name="font">
+      <font>
+       <family>Arial</family>
+       <pointsize>10</pointsize>
+      </font>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <layout class="QHBoxLayout" name="horizontalLayout">
+     <item>
+      <widget class="QPushButton" name="addButton">
+       <property name="text">
+        <string>添加算法</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="removeButton">
+       <property name="text">
+        <string>删除算法</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="editButton">
+       <property name="text">
+        <string>编辑参数</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="moveUpButton">
+       <property name="text">
+        <string>上移</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="moveDownButton">
+       <property name="text">
+        <string>下移</string>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+   <item>
+    <layout class="QHBoxLayout" name="horizontalLayout_2">
+     <item>
+      <widget class="QPushButton" name="saveButton">
+       <property name="text">
+        <string>保存流程</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="loadButton">
+       <property name="text">
+        <string>加载流程</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <spacer name="horizontalSpacer">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+    </layout>
+   </item>
+  
+ </layout>
+</widget>
+<resources/>
+<connections/>
+</ui>