浏览代码

完善了新建项目

lal 2 年之前
父节点
当前提交
7f8531448a
共有 4 个文件被更改,包括 190 次插入112 次删除
  1. 15 1
      dialog.cpp
  2. 3 0
      dialog.h
  3. 165 107
      mainwindow.cpp
  4. 7 4
      mainwindow.h

+ 15 - 1
dialog.cpp

@@ -60,7 +60,7 @@ void Dialog::on_pushButton_clicked()
 void Dialog::on_pushButton_2_clicked()
 {
         // 执行相应的操作
-        QString dirPath = "~/Documents/RedInterstitialData/Hotupdate2/";
+        QString dirPath = "/users/lal/documents/RedInterstitialData/HotUpdate2/";
         QJsonObject newData;
 
         QString pro_name=ui->pro_name->toPlainText();
@@ -94,6 +94,20 @@ void Dialog::on_pushButton_2_clicked()
         newData.insert("file_serverpre", file_serverpre);
 
         addDataToJsonFile(filePath,newData);
+        QJsonObject rootObject;
+        QJsonArray childrenArray;
+
+        QJsonObject childObject;
+
+        rootObject["children"] = childrenArray;
+        QFile file(filePath+"/hotupdate.json");
+        if (file.open(QIODevice::WriteOnly)) {
+                        QJsonDocument jsonDocument(rootObject);
+                        file.write(jsonDocument.toJson());
+                        file.close();
+        }
+        emit _mySignal();
         this->close();
+
 }
 

+ 3 - 0
dialog.h

@@ -25,6 +25,9 @@ private slots:
 
     void on_pushButton_2_clicked();
 
+signals:
+    void _mySignal();
+
 private:
     Ui::Dialog *ui;
 

+ 165 - 107
mainwindow.cpp

@@ -11,6 +11,7 @@
 #include <QTextStream>
 #include <iostream>
 #include <QMimeData>
+#include <QToolTip>
 using namespace std;
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)
@@ -123,6 +124,13 @@ MainWindow::MainWindow(QWidget *parent)
 
     qDebug()<<"QJA"<<QJA;
 
+    QStandardItemModel *tree_model = qobject_cast<QStandardItemModel*>(ui->treeView->model());
+    connect(tree_model, &QStandardItemModel::dataChanged, this, &MainWindow::onDataChanged);
+    bool connected = connect(&_sender, &Dialog::_mySignal, this, &MainWindow::handleSignal);
+    if(!connected) {
+        // Handle the error
+        qDebug()<<"connect failed";
+    }
     //初始化右边
 //    initRight(QString parentNode,QString childNode);
 }
@@ -236,61 +244,9 @@ QJsonObject MainWindow::readJsonFile(const QString& filePath)
     // 处理其他字段...
 }
 
-void MainWindow::init()
-{
-    QString filePath = "~/Documents/RedInterstitialData/Hotupdate.tool/Hotupdate1.json";
-
-
-
-    QJA=tool::getJsanArra(filePath);
-    // 从JSON对象中提取数据
-    int num=QJA.count();
-        qDebug() << num;
-    QStringList pro_list;
-    for(int i=1;i<=num;i++)
-    {
-        pro_list.append(QString::number(i));
-    }
-    ui->comboBox->clear();
-    ui->comboBox->addItems(pro_list);
-
-    ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
-    connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu);
-    //初始化当前窗口
-    int index=ui->comboBox->currentData().toString().toInt();
-    inittreeview(QJA[index]);
-
-}
-
-void MainWindow::init2()
-{
-//    QString filePath = "~/Documents/RedInterstitialData/Hotupdate2/Hotupdate2.json";
-//    QString folder = "~/Documents/RedInterstitialData/Hotupdate2";
-//    tool::generateFolderJsonFile(folder,filePath);
-//    QString jsonPath = "~/Documents/RedInterstitialData/Hotupdate2/Hotupdate2.json";
 
 
 
-//    QJO=tool::readJsonFile(jsonPath);
-//    // 从JSON对象中提取数据
-//    QJA = QJO.value("children").toArray();
-//    ui->comboBox->clear();
-
-//    ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
-//    connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu);
-//    //初始化treeview 和 combobox
-//    QTreeView *treeView=ui->treeView;
-//    QStandardItemModel* model=new QStandardItemModel(treeView);
-//    model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("名称"));
-//    treeView->setModel(model);
-//    QStringList pro_list;
-
-//    ui->comboBox->clear();
-//    pro_list=initializeTreeView_conboBox(model->invisibleRootItem(),QJA);
-//    ui->comboBox->addItems(pro_list);
-
-
-}
 
 
 //初始化treeview 和combobox控件
@@ -328,35 +284,6 @@ void MainWindow::initTreeView(QStandardItem* parentItem,const QJsonArray& jsonAr
 
 
 
-void MainWindow::inittreeview(QJsonValueRef ref)
-{
-    //1,构造Model,这里示例具有3层关系的model构造过程
-    QStandardItemModel* model = new QStandardItemModel(ui->treeView);
-    model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("序号") << QStringLiteral("名称"));     //设置列头
-    for(int i=0;i<5;i++)
-    {
-        //一级节点,加入mModel
-        QList<QStandardItem*> items1;
-        QStandardItem* item1 = new QStandardItem(QString::number(i));
-        QStandardItem* item2 = new QStandardItem(QStringLiteral("一级节点"));
-        items1.append(item1);
-        items1.append(item2);
-        model->appendRow(items1);
-
-        for(int j=0;j<5;j++)
-        {
-            //二级节点,加入第1个一级节点
-            QList<QStandardItem*> items2;
-            QStandardItem* item3 = new QStandardItem(QString::number(j));
-            QStandardItem* item4 = new QStandardItem(QStringLiteral("二级节点"));
-            items2.append(item3);
-            items2.append(item4);
-            item1->appendRow(items2);
-        }
-    }
-    //2,给QTreeView应用model
-    ui->treeView->setModel(model);
-}
 
 
 
@@ -482,31 +409,7 @@ void MainWindow::on_treeView_clicked(const QModelIndex &index)
 
 }
 
-void MainWindow::mousePressEvent(QMouseEvent* event)
-{
-//    // 调用基类的函数处理其他的鼠标点击事件
-//    QMainWindow::mousePressEvent(event);
-
-
-//    // 检查点击的位置是否在 treeView 的边界内
-//    if(ui->treeView->rect().contains(event->pos()))
-//    {
-//        // 转换全局坐标为 treeView 的本地坐标
-//        QPoint treeViewPos = ui->treeView->mapFromGlobal(event->globalPos());
-//        // 检查是否有节点在点击的位置
-//        if(!ui->treeView->indexAt(treeViewPos).isValid())
-//        {
-//                // 在这里执行你的操作...
-//                qDebug() << "点击了 treeView 的空白部分";
-//                ui->treeView->clearSelection();
-//                ui->treeView->setCurrentIndex(QModelIndex());
-//        }
-//    }
-//    else{
-//        ui->treeView->clearSelection();
-//        ui->treeView->setCurrentIndex(QModelIndex());
-//    }
-}
+
 
 void MainWindow::initRightListwidget(QListWidget* qw,const QJsonArray& qja)
 {
@@ -762,6 +665,8 @@ void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
 
     initTreeView(model->invisibleRootItem(),QJA);
     qDebug()<<"QJA"<<QJA;
+    QStandardItemModel *tree_model = qobject_cast<QStandardItemModel*>(ui->treeView->model());
+    connect(tree_model, &QStandardItemModel::dataChanged, this, &MainWindow::onDataChanged);
 
 }
 
@@ -876,6 +781,7 @@ void MainWindow::on_pushButton_4_clicked()
             QJsonArray qaa=jsonObj["children"].toArray();
             qaa.append(newQ);
             jsonObj["children"]=qaa;
+            QJA=qaa;
             tool::writeJsonFile(filePath,jsonObj);
             items1.append(item2);
             model1->appendRow(items1);
@@ -927,7 +833,14 @@ void MainWindow::on_pushButton_5_clicked()
             //新建版本
             QDir dir;
             QString version_path=add_Path+"/"+"version"+folderCount;
-            dir.mkpath(version_path);
+            if (dir.exists(version_path))
+            {
+                return;
+            }
+            if(!dir.mkpath(version_path))
+            {
+                    return;
+            }
             //新建版本下的子文件夹等
             dir.mkpath(version_path+"/"+"group1");
             dir.mkpath(version_path+"/"+"group2");
@@ -974,6 +887,7 @@ void MainWindow::on_pushButton_5_clicked()
                     // 其他错误处理逻辑
             }
             QByteArray jsonData = file1.readAll();
+            file1.close();
             QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
             if (jsonDoc.isNull()) {
                     qDebug() << "Failed to parse JSON data.";
@@ -995,6 +909,7 @@ void MainWindow::on_pushButton_5_clicked()
             parentArray[index.row()] = childObject;
             jsonObj["children"] = parentArray;
             qDebug() << "Modified QJsonObject:" << jsonObj;
+            QJA=parentArray;
             tool::writeJsonFile(modify_pro_config,jsonObj);
             //
 
@@ -1063,6 +978,7 @@ void MainWindow::on_pushButton_9_clicked()
                     // 其他错误处理逻辑
             }
             QByteArray jsonData = file1.readAll();
+            file1.close();
             QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
             if (jsonDoc.isNull()) {
                     qDebug() << "Failed to parse JSON data.";
@@ -1126,6 +1042,7 @@ void MainWindow::on_pushButton_9_clicked()
                     // 其他错误处理逻辑
             }
             QByteArray jsonData = file1.readAll();
+            file1.close();
             QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
             if (jsonDoc.isNull()) {
                     qDebug() << "Failed to parse JSON data.";
@@ -1158,3 +1075,144 @@ void MainWindow::on_pushButton_9_clicked()
     }
 }
 
+void MainWindow::onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
+{
+    Q_UNUSED(bottomRight);
+    Q_UNUSED(roles);
+    // 获取节点的文本
+    QString text = topLeft.data().toString();
+    qDebug()<<"datachanged";
+    if (!topLeft.isValid())
+        return;
+
+    // 获取父节点
+    QModelIndex parentIndex = topLeft.parent();
+    if (!parentIndex.isValid()) {
+        // 如果父节点无效,那么这是一个一级节点
+        qDebug() << "First-level node text changed: " << topLeft.data().toString();
+        qDebug() << "Index in tree: " << topLeft.row();
+
+
+        QJsonObject temqjo=QJA[topLeft.row()].toObject();
+        QString lastname=temqjo["name"].toString();qDebug()<<lastname;
+
+
+        QString oldFolderPath = root_pro_path+"/"+ui->comboBox->currentText()+"/"+lastname;
+        QString newFolderPath = root_pro_path+"/"+ui->comboBox->currentText()+"/"+topLeft.data().toString();
+
+        QDir dir;
+        if (!dir.rename(oldFolderPath, newFolderPath)) {
+            QMessageBox::information(this, "提示", "命名重复");
+            qWarning() << "Failed to rename folder from" << oldFolderPath << "to" << newFolderPath;
+
+            QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
+            QStandardItem* selectedItem = model->itemFromIndex(topLeft);
+            selectedItem->setText(lastname);
+            return;
+
+        }
+        QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json";
+        qDebug()<<oldFolderPath<<newFolderPath;
+
+        //修改json文件
+        QFile file1(modify_pro_config);
+        if (!file1.open(QIODevice::ReadOnly))
+        {
+            QString errorString = file1.errorString();
+            qDebug() << "Failed to open file: " << errorString;
+            // 其他错误处理逻辑
+        }
+        QByteArray jsonData = file1.readAll();
+        file1.close();
+        QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
+        if (jsonDoc.isNull()) {
+            qDebug() << "Failed to parse JSON data.";
+            return;
+        }
+        file1.close();
+        // 将 JSON 数据转换为对象
+        QJsonObject jsonObj = jsonDoc.object();
+        QJsonArray parentArray = jsonObj["children"].toArray();
+        QJsonObject tarobj=parentArray[topLeft.row()].toObject();
+
+        tarobj["name"]=topLeft.data().toString();
+
+
+        parentArray[topLeft.row()]=tarobj;
+        jsonObj["children"] = parentArray;
+        qDebug() << "Modified QJsonObject:" << jsonObj;
+        tool::writeJsonFile(modify_pro_config,jsonObj);
+
+
+
+    } else {
+        // 如果需要获取父节点的信息,可以使用 parentIndex.data().toString()
+        // 如果父节点有效,那么这是一个二级节点
+        qDebug() << "Second-level node text changed: " << topLeft.data().toString();
+        qDebug() << "Parent index in tree: " << parentIndex.row();
+        qDebug() << "Index in parent's children: " << topLeft.row();
+
+
+        QJsonObject temqjo=QJA[parentIndex.row()].toObject();
+
+        QString lastname=temqjo["children"].toArray()[topLeft.row()].toObject()["name"].toString();qDebug()<<lastname;
+
+
+        QString oldFolderPath = root_pro_path+"/"+ui->comboBox->currentText()+"/"+parentIndex.data().toString()+"/"+lastname;
+        QString newFolderPath = root_pro_path+"/"+ui->comboBox->currentText()+"/"+ parentIndex.data().toString()+"/"+topLeft.data().toString();
+
+        QDir dir;
+        QString filename=topLeft.data().toString();
+        if (!dir.rename(oldFolderPath, newFolderPath)) {
+            QMessageBox::information(this, "提示", "命名重复");
+            qWarning() << "Failed to rename folder from" << oldFolderPath << "to" << newFolderPath;
+
+            QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
+            QStandardItem* selectedItem = model->itemFromIndex(topLeft);
+            selectedItem->setText(lastname);
+            return;
+        }
+        QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json";
+        qDebug()<<oldFolderPath<<newFolderPath;
+
+        //修改json文件
+        QFile file1(modify_pro_config);
+        if (!file1.open(QIODevice::ReadOnly))
+        {
+            QString errorString = file1.errorString();
+            qDebug() << "Failed to open file: " << errorString;
+            // 其他错误处理逻辑
+        }
+        QByteArray jsonData = file1.readAll();
+        file1.close();
+        QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
+        if (jsonDoc.isNull()) {
+            qDebug() << "Failed to parse JSON data.";
+            return;
+        }
+        // 将 JSON 数据转换为对象
+        QJsonObject jsonObj = jsonDoc.object();
+        QJsonArray parentArray = jsonObj["children"].toArray();
+        QJsonObject tarobj=parentArray[parentIndex.row()].toObject();
+        QJsonArray tararr=tarobj["children"].toArray();
+
+        QJsonObject to=tararr[topLeft.row()].toObject();
+        to["name"]=filename;
+        tararr[topLeft.row()]=to;
+        tarobj["children"]=tararr;
+        parentArray[parentIndex.row()]=tarobj;
+        jsonObj["children"] = parentArray;
+        qDebug() << "Modified QJsonObject:" << jsonObj;
+        tool::writeJsonFile(modify_pro_config,jsonObj);
+    }
+
+}
+void MainWindow::handleSignal()
+{
+    qDebug()<<"handleSignal";
+    QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2";
+    QStringList qs=tool::getFilesInFolder(folder);//获取项目名称列表
+
+    qDebug()<<qs;
+    initComboBox(qs);
+}

+ 7 - 4
mainwindow.h

@@ -30,7 +30,7 @@ public:
 
     ~MainWindow();
 protected:
-    void mousePressEvent(QMouseEvent* event) override;
+
 
 private:
     Ui::MainWindow *ui;
@@ -43,6 +43,7 @@ private:
     CustomButton* customB3;
     CustomButton* customB4;
 
+    Dialog _sender;
 
     MyListWidget* my1;
     MyListWidget* my2;
@@ -56,9 +57,8 @@ private:
     void add_slide();
     void readFile(const QString& filePath);
     QJsonObject readJsonFile(const QString& filePath);
-    void init();
-    void inittreeview(QJsonValueRef qvr);
-    void init2();
+
+
     void initTreeView(QStandardItem* parentItem,const QJsonArray& jsonArray);
     void initComboBox(QStringList jsonPath);
     void initRight(QString parentNode,QString childNode);
@@ -88,9 +88,12 @@ private slots:
     void on_comboBox_currentTextChanged(const QString &arg1);
     void handleOperationCompleted(QString fromfilePath);
 
+    void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
 
     void on_pushButton_5_clicked();
     void on_comboBox_customContextMenuRequested(const QPoint &pos);
     void on_pushButton_9_clicked();
+
+    void handleSignal();
 };
 #endif // MAINWINDOW_H