123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218 |
- #include "mainwindow.h"
- #include "custommodel.h"
- #include "ui_mainwindow.h"
- #include "listview_ad.h"
- #include "listview_delgate.h"
- #include <QStandardItemModel>
- #include"../sample_button_slide/CustomButton.h"
- #include"../test_drag/mylistwidget.h"
- #include <QDir>
- #include <QFile>
- #include <QTextStream>
- #include <iostream>
- #include <QMimeData>
- #include <QToolTip>
- using namespace std;
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- //add_slide;
- QWidget *slide_2= ui->slide_2; // 获取ceshi_2_slide控件指针
- customB2 = new CustomButton(this); // 创建自定义控件实例
- connect(customB2, &CustomButton::click, this, &MainWindow::onWidgetClicked);
- // 将自定义控件添加到ceshi_2_slide控件中
- QHBoxLayout *layout2 = new QHBoxLayout(slide_2);
- layout2->setContentsMargins(0, 0, 0, 0);
- layout2->addWidget(customB2);
- layout2->setSpacing(0);
- //add_slide;
- QWidget *slide_3= ui->slide_3; // 获取ceshi_2_slide控件指针
- customB3 = new CustomButton(this); // 创建自定义控件实例
- connect(customB3, &CustomButton::click, this, &MainWindow::onWidgetClicked);
- // 将自定义控件添加到ceshi_2_slide控件中
- QHBoxLayout *layout3 = new QHBoxLayout(slide_3);
- layout3->setContentsMargins(0, 0, 0, 0);
- layout3->addWidget(customB3);
- layout3->setSpacing(0);
- //add_slide;
- QWidget *slide_4= ui->slide_4; // 获取ceshi_2_slide控件指针
- customB4 = new CustomButton(this); // 创建自定义控件实例
- connect(customB4, &CustomButton::click, this, &MainWindow::onWidgetClicked);
- // 将自定义控件添加到ceshi_2_slide控件中
- QHBoxLayout *layout4 = new QHBoxLayout(slide_4);
- layout4->setContentsMargins(0, 0, 0, 0);
- layout4->addWidget(customB4);
- layout4->setSpacing(0);
- //初始化右侧u
- // MyListWidget* my1=new MyListWidget();
- // QVBoxLayout *layout5 = new QVBoxLayout();
- // layout5->addWidget(my1);
- // ui->test_frame->setLayout(layout5);
- // connect(my1, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted);
- my1=new MyListWidget();
- my1->setObjectName("listWidget");
- QVBoxLayout *layout5 = new QVBoxLayout();
- layout5->addWidget(my1);
- ui->frame->setLayout(layout5);
- qDebug()<<"widget_group"<<ui->widget_group1;
- connect(my1, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted);
- connect(my1, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_customContextMenuRequested(QPoint)));
- my2=new MyListWidget();
- my2->setObjectName("listWidget_2");
- QVBoxLayout *layout6 = new QVBoxLayout();
- layout6->addWidget(my2);
- ui->frame_4->setLayout(layout6);
- connect(my2, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted);
- connect(my2, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_2_customContextMenuRequested(QPoint)));
- my3=new MyListWidget();
- my3->setObjectName("listWidget_3");
- QVBoxLayout *layout7 = new QVBoxLayout();
- layout7->addWidget(my3);
- ui->frame_5->setLayout(layout7);
- connect(my3, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted);
- connect(my3, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_3_customContextMenuRequested(QPoint)));
- my4=new MyListWidget();
- my4->setObjectName("listWidget_4");
- QVBoxLayout *layout8 = new QVBoxLayout();
- layout8->addWidget(my4);
- ui->frame_6->setLayout(layout8);
- connect(my4, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted);
- connect(my4, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_4_customContextMenuRequested(QPoint)));
- QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2";
- root_pro_path=folder;
- qDebug()<<folder;
- QStringList qs=tool::getFilesInFolder(folder);//获取项目名称列表
- qDebug()<<qs;
- //初始化comboBox
- initComboBox(qs);
- QString currentPro = ui->comboBox->currentText();
- //初始化TreeView
- QString jsonPath = folder+"/"+currentPro+"/"+"Hotupdate.json";
- qDebug()<<jsonPath;
- QJO=tool::readJsonFile(jsonPath);
- // 从JSON对象中提取数据
- QJA = QJO.value("children").toArray();
- ////////////////////////////////////////////////
- ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
- connect(ui->treeView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotTreeMenu(QPoint)));
- //初始化treeview 和 combobox
- QTreeView *treeView=ui->treeView;
- QStandardItemModel* model=new QStandardItemModel(treeView);
- model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("名称"));
- treeView->setModel(model);
- initTreeView(model->invisibleRootItem(),QJA);
- 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);
- }
- void QTreeView::mousePressEvent(QMouseEvent *event)
- {
- QModelIndex index = indexAt(event->pos());
- if (index.isValid() && index.parent().isValid())
- {
- // 这是一个二级列表项,你可以在这里处理点击事件
- // 例如,获取二级列表项的数据并进行相应的操作
- QVariant data = index.data(Qt::DisplayRole);
- qDebug() << "Clicked on subitem:" << data.toString();
- }
- else
- {
- qDebug() << "Clicked on subitem false";
- QTreeView::mousePressEvent(event);
- }
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::readFile(const QString& filePath)//设置打开该窗体时检查路径并加载
- {
- // 获取文件所在路径
- QString dirPath = QFileInfo(filePath).absolutePath();
- qDebug() << dirPath;
- // 创建路径
- QDir dir;
- if (!dir.mkpath(dirPath))
- {
- qDebug() << "无法创建路径:" << dirPath;
- return;
- }
- // 创建文件
- QFile file(filePath);
- if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
- {
- qDebug() << "无法创建文件:" << file.errorString();
- return;
- }
- // 使用QTextStream读取文件内容
- QTextStream in(&file);
- while (!in.atEnd())
- {
- QString line = in.readLine();
- // 处理每一行的内容
- // TODO: 添加您的逻辑
- qDebug() << line;
- }
- // 关闭文件
- file.close();
- }
- void MainWindow::on_pushButton_clicked()
- {
- dialog=new Dialog(this);
- dialog->setModal(false);
- dialog->show();
- }
- void MainWindow::on_pushButton_2_clicked()
- {
- dialog=new Dialog(this);
- dialog->setModal(false);
- dialog->show();
- }
- QJsonObject MainWindow::readJsonFile(const QString& filePath)
- {
- // 打开JSON文件
- QFile file(filePath);
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
- {
- qDebug() << "无法打开文件:" << file.errorString();
- return QJsonObject();
- }
- // 读取文件内容
- QByteArray jsonData = file.readAll();
- file.close();
- // 解析JSON数据
- QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
- if (!jsonDoc.isObject())
- {
- qDebug() << "无效的JSON文件";
- return QJsonObject();;
- }
- // 获取JSON对象
- QJsonObject jsonObj = jsonDoc.object();
- return jsonObj;
- // 处理其他字段...
- }
- //初始化treeview 和combobox控件
- void MainWindow::initTreeView(QStandardItem* parentItem,const QJsonArray& jsonArray) {
- QStringList pro_list;
- for (QJsonArray::const_iterator it = jsonArray.constBegin(); it != jsonArray.constEnd(); ++it) {
- QJsonValue jsonValue = *it;
- qDebug()<<jsonValue;
- // 处理每个元素,可以根据需要进行类型检查和值提取
- if (jsonValue.isString()) {
- QString fileName = jsonValue.toString();
- QStandardItem* item = new QStandardItem(fileName);
- // if(!fileName.endsWith("json"))
- // parentItem->appendRow(item);
- // 处理字符串值 s
- } else if (jsonValue.isDouble()) {
- double doubleValue = jsonValue.toDouble();
- // 处理双精度浮点数值
- } else if (jsonValue.isObject()) {
- QJsonObject objectValue = jsonValue.toObject();
- QString name = objectValue["name"].toString();
- QStandardItem* item = new QStandardItem(name);
- parentItem->appendRow(item);
- QJsonArray childrenArray = objectValue["children"].toArray();
- pro_list.append(name);
- // 处理对象值
- initTreeView(item, childrenArray);
- }
- }
- ui->treeView->clearSelection();
- ui->treeView->setCurrentIndex(QModelIndex());
- }
- void MainWindow::slotTreeMenu(const QPoint &pos)
- {
- // QModelIndex curItem = ui->treeView->indexAt( pos );
- // QModelIndex index = curItem.sibling(curItem.row(),0);
- // if( !index.isValid() )
- // return;
- // QMenu *popMenu = new QMenu( this );
- // QAction *deleteSeed = new QAction(tr("Delete"), this);
- // QString qss = "QMenu{color:#E8E8E8;background:#4D4D4D;margin:2px;}\
- // QMenu::item{padding:3px 20px 3px 20px;}\
- // QMenu::indicator{width:13px;height:13px;}\
- // QMenu::item:selected{color:#E8E8E8;border:0px solid #575757;background:#1E90FF;}\
- // QMenu::separator{height:1px;background:#757575;}";
- // popMenu->setStyleSheet(qss);
- // popMenu->addAction( deleteSeed );
- // connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( slotTreeMenuDelete()) );
- // popMenu->exec( QCursor::pos() );
- // delete popMenu;
- }
- //删除设定为假删除,在窗体退出的时候才更新json文件,并执行删除操作
- /////////到这里
- /// ///
- /// ////
- /// ///
- /// //
- /// //
- /// /
- ///
- /// //
- void MainWindow::slotTreeMenuDelete()
- {
- // QModelIndex curIndex = ui->treeView->currentIndex();
- // QModelIndex index = curIndex.sibling(curIndex.row(),0); //同一行第一列元素的index
- // qDebug()<<curIndex;
- // qDebug()<<index;
- // QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
- // model->removeRow(index.row(), index.parent());
- }
- void MainWindow::initComboBox(QStringList qs){
- ui->comboBox->clear();
- ui->comboBox->addItems(qs);
- }
- void MainWindow::initRight(QString parentNode,QString childNode){
- //
- QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2";
- QString currentPro = ui->comboBox->currentText();
- config_json_path=folder+"/"+currentPro+"/"+parentNode+"/"+childNode+"/config.json";
- treeview_path=folder+"/"+currentPro+"/"+parentNode+"/"+childNode;
- qDebug()<<config_json_path;
- qDebug()<<treeview_path;
- RQJA=tool::getJsanArra(config_json_path);
- for (int i = 0; i < RQJA.size(); ++i) {
- QJsonValueRef value = RQJA[i];
- QJsonObject jsonObject = value.toObject();
- QJsonValue jsonArrayValue = jsonObject.value("children");
- QJsonValue Used=jsonObject.value("selected");
- QString used=Used.toString();
- QJsonArray jsonArray = jsonArrayValue.toArray();
- qDebug()<<jsonArray.size();
- CustomButton* temb=nullptr;
- QListWidget* rightLw;
- if(i==0)
- {
- rightLw=my1;
- }
- else if(i==1)
- {
- temb=customB2;
- rightLw=my2;
- }
- else if(i==2)
- {
- temb=customB3;
- rightLw=my3;
- }
- else{
- temb=customB4;
- rightLw=my4;
- }
- if(used=="1"&&temb)
- {
- if(temb->dir==Qt::AlignLeft){
- temb->pos_right_button();
- }
- }
- if(used=="0"&&temb)
- {
- if(temb->dir==Qt::AlignRight){
- temb->pos_left_button();
- }
- }
- initRightListwidget(rightLw,jsonArray);
- }
- }
- void MainWindow::on_treeView_clicked(const QModelIndex &index)
- {
- // 获取第一级节点的信息
- if(index.parent().isValid())
- {
- QModelIndex parentIndex = index.parent();
- QString parentData = parentIndex.data().toString();
- qDebug() << "First-level Node: " << parentData;
- // 获取第二级节点的信息
- QString childData = index.data().toString();
- qDebug() << "Second-level Node: " << childData;
- initRight(parentData,childData);
- }
- }
- void MainWindow::initRightListwidget(QListWidget* qw,const QJsonArray& qja)
- {
- QStringList dataList;
- for (const QJsonValue& value : qja) {
- if (value.isString()) {
- QString stringValue = value.toString();
- dataList.append(stringValue);
- }
- }
- qDebug()<<"initRightListwidget";
- qDebug()<<"dataList"<<dataList;
- qDebug()<<"qw"<<qw;
- qw->clear();
- qw->addItems(dataList);
- }
- /////////////////////////////////////////////////////////////////////////////////////////////右侧右击事件
- void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
- {
- qDebug()<<"1111111";
- QListWidgetItem* curItem = my1->itemAt( pos );
- if( curItem == NULL )
- return;
- QMenu *popMenu = new QMenu( this );
- QAction *deleteSeed = new QAction(tr("Delete"), this);
- QString qss = "QMenu{color:#E8E8E8;background:#4D4D4D;margin:2px;}\
- QMenu::item{padding:3px 20px 3px 20px;}\
- QMenu::indicator{width:13px;height:13px;}\
- QMenu::item:selected{color:#E8E8E8;border:0px solid #575757;background:#1E90FF;}\
- QMenu::separator{height:1px;background:#757575;}";
- popMenu->setStyleSheet(qss);
- popMenu->addAction( deleteSeed );
- this->qlw=my1;
- connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) );
- popMenu->exec( QCursor::pos() );
- delete popMenu;
- delete deleteSeed;
- }
- void MainWindow::on_listWidget_2_customContextMenuRequested(const QPoint &pos)
- {
- QListWidgetItem* curItem = my2->itemAt( pos );
- if( curItem == NULL )
- return;
- QMenu *popMenu = new QMenu( this );
- QAction *deleteSeed = new QAction(tr("Delete"), this);
- popMenu->addAction( deleteSeed );
- this->qlw=my2;
- connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()));
- popMenu->exec( QCursor::pos() );
- delete popMenu;
- delete deleteSeed;
- }
- /////daozheer 可以删除项 还没有修改json文件
- void MainWindow::deleteSeedSlot()
- {
- qDebug()<<"deleteSeedSlot";
- QListWidgetItem * item = this->qlw->currentItem();
- qDebug()<<this->qlw;
- if( item == NULL )
- return;
- int curIndex = this->qlw->row(item);
- qDebug()<<curIndex;
- auto curItem=this->qlw->takeItem(curIndex);
- qDebug()<<curItem->text();
- // updateConfig(ui->listWidget);//更新对应的config.json文件
- QString deal_file_path;
- QString group_name;
- int group_index;
- if(qlw->objectName()=="listWidget")
- {
- group_name="group1";
- group_index=0;
- }
- else if(qlw->objectName()=="listWidget_2")
- {
- group_name="group2";
- group_index=1;
- }
- else if(qlw->objectName()=="listWidget_3")
- {
- group_name="group3";
- group_index=2;
- }
- else
- {
- group_name="group4";
- group_index=3;
- }
- deal_file_path=treeview_path+"/"+group_name+"/"+curItem->text();
- QFile file(deal_file_path);
- file.remove();
- QJsonObject groupObject = RQJA[group_index].toObject();
- QJsonArray childrenArray = groupObject["children"].toArray();
- childrenArray.removeAt(curIndex);
- groupObject["children"] = childrenArray;
- RQJA[group_index] = groupObject;
- writeBackToConfigJson();
- delete item;
- }
- void MainWindow::on_listWidget_3_customContextMenuRequested(const QPoint &pos)
- {
- QListWidgetItem* curItem = my3->itemAt( pos );
- if( curItem == NULL )
- return;
- QMenu *popMenu = new QMenu( this );
- QAction *deleteSeed = new QAction(tr("Delete"), this);
- popMenu->addAction( deleteSeed );
- this->qlw=my3;
- connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) );
- popMenu->exec( QCursor::pos() );
- delete popMenu;
- delete deleteSeed;
- }
- void MainWindow::on_listWidget_4_customContextMenuRequested(const QPoint &pos)
- {
- QListWidgetItem* curItem = my4->itemAt( pos );
- if( curItem == NULL )
- return;
- QMenu *popMenu = new QMenu( this );
- QAction *deleteSeed = new QAction(tr("Delete"), this);
- popMenu->addAction( deleteSeed );
- this->qlw=my4;
- connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) );
- popMenu->exec( QCursor::pos() );
- delete popMenu;
- delete deleteSeed;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////右侧右击事件
- //滑块点击slot函数
- void MainWindow::onWidgetClicked()
- {
- QObject *senderObj = sender(); // 获取发出信号的对象指针
- if(RQJA.empty())return;
- if (senderObj)
- {
- QWidget *clickedWidget = qobject_cast<QWidget*>(senderObj); // 将指针转换为QWidget指针
- if (clickedWidget)
- {
- QString widgetName = clickedWidget->parentWidget()->objectName(); // 获取控件的对象名称
- if(widgetName=="slide_2")
- {
- if(customB2->dir==Qt::AlignLeft)
- {
- //变为1
- QJsonValueRef selectedValueRef = RQJA[1].toObject()["selected"];
- selectedValueRef = "1";
- qDebug()<<RQJA;
- }
- else{
- //变为0
- QJsonValueRef selectedValueRef = RQJA[1].toObject()["selected"];
- selectedValueRef = "0";
- qDebug()<<RQJA;
- }
- }
- else if(widgetName=="slide_3")
- {
- if(customB3->dir==Qt::AlignLeft)
- {
- //变为1
- QJsonValueRef selectedValueRef = RQJA[2].toObject()["selected"];
- selectedValueRef = "1";
- qDebug()<<RQJA;
- }
- else{
- //变为0
- QJsonValueRef selectedValueRef = RQJA[2].toObject()["selected"];
- selectedValueRef = "0";
- qDebug()<<RQJA;
- }
- }
- else
- {
- if(customB4->dir==Qt::AlignLeft)
- {
- //变为1
- QJsonValueRef selectedValueRef = RQJA[3].toObject()["selected"];
- selectedValueRef = "1";
- qDebug()<<RQJA;
- }
- else{
- //变为0
- QJsonValueRef selectedValueRef = RQJA[3].toObject()["selected"];
- selectedValueRef = "0";
- qDebug()<<RQJA;
- }
- }
- writeBackToConfigJson();
- }
- }
- }
- //修改config文件
- void MainWindow::writeBackToConfigJson(){
- if(!config_json_path.endsWith("json"))return;
- qDebug()<<config_json_path;
- QFile jsonFile(config_json_path);
- if (jsonFile.open(QIODevice::WriteOnly)) {
- QJsonDocument jsonDoc(RQJA);
- QByteArray jsonData = jsonDoc.toJson();
- qint64 bytesWritten = jsonFile.write(jsonData);
- if (bytesWritten == -1) {
- qDebug() << "Failed to write data to file.";
- } else {
- qDebug() << "Data written to file successfully.";
- }
- jsonFile.close();
- } else {
- qDebug() << "Failed to open file for writing.";
- }
- }
- //重新选择项目进行初始化
- void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
- {
- clearRight();
- QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2";
- QString currentPro = ui->comboBox->currentText();
- //初始化TreeView
- QString jsonPath = folder+"/"+currentPro+"/"+"Hotupdate.json";
- QJO=tool::readJsonFile(jsonPath);
- // 从JSON对象中提取数据
- QJA = QJO.value("children").toArray();
- 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);
- initTreeView(model->invisibleRootItem(),QJA);
- qDebug()<<"QJA"<<QJA;
- QStandardItemModel *tree_model = qobject_cast<QStandardItemModel*>(ui->treeView->model());
- connect(tree_model, &QStandardItemModel::dataChanged, this, &MainWindow::onDataChanged);
- }
- void MainWindow::clearRight()
- {
- my1->clear();
- my2->clear();
- my3->clear();
- my4->clear();
- this->customB2->pos_left_button();
- this->customB3->pos_left_button();
- this->customB4->pos_left_button();
- }
- void MainWindow::handleOperationCompleted(QString fromfilePath)
- {
- QStringList parts = fromfilePath.split("/");
- QObject *senderObj = sender();
- QListWidget *LW = qobject_cast<QListWidget*>(senderObj);
- //QString tofilePath1 = "/Users/lal/QT_pro/build-test_tool1-Qt_6_3_1_for_macOS-Debug/test_tool1.app/Contents/MacOS/~/Documents/RedInterstitialData/HotUpdate2/pro2/tes1/version1/group3";
- QString group_name;
- int group_index;
- if(LW->objectName()=="listWidget")
- {
- group_name="group1";
- group_index=0;
- }
- else if(LW->objectName()=="listWidget_2")
- {
- group_name="group2";
- group_index=1;
- }
- else if(LW->objectName()=="listWidget_3")
- {
- group_name="group3";
- group_index=2;
- }
- else
- {
- group_name="group4";
- group_index=3;
- }
- QString tofilePath1 = treeview_path+"/"+group_name;
- QFile sourceFile(fromfilePath);
- QFile targetFile(tofilePath1 + "/" +parts[parts.size()-1]);
- if (sourceFile.exists() && QDir(tofilePath1).exists()) {
- if (sourceFile.copy(targetFile.fileName())) {
- qDebug() << "File copied successfully.";
- LW->addItem(parts[parts.size()-1]);
- qDebug()<<this->parent();
- QJsonObject groupObject = RQJA[group_index].toObject();
- QJsonArray childrenArray = groupObject["children"].toArray();
- childrenArray.append(parts[parts.size()-1]);
- groupObject["children"] = childrenArray;
- RQJA[group_index] = groupObject;
- writeBackToConfigJson();
- } else {
- qDebug() << "Failed to copy file.";
- QMessageBox::information(this, "失败", "文件名重复");
- }
- } else {
- qDebug() << "Source file or target folder does not exist.";
- }
- }
- void MainWindow::on_pushButton_4_clicked()
- {
- QList<QStandardItem*> items1;
- QStandardItemModel *model1 = static_cast<QStandardItemModel*>(ui->treeView->model());
- // QStandardItem* item2 = new QStandardItem(QStringLiteral("新建测试"));
- QDateTime currentDateTime = QDateTime::currentDateTime();
- QString currentDateTimeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
- QStandardItem* item2 = new QStandardItem(currentDateTimeString);
- QString add_Path=root_pro_path+"/"+ui->comboBox->currentText();
- QDir dir;
- if (!dir.exists(add_Path+"/"+currentDateTimeString)) {
- if (dir.mkpath(add_Path+"/"+currentDateTimeString)) {
- qDebug() << "Folder created successfully.";
- QString filePath = add_Path+"/"+"Hotupdate.json";
- QFile file(filePath);
- if (!file.open(QIODevice::ReadOnly))
- {
- QString errorString = file.errorString();
- qDebug() << "Failed to open file: " << errorString;
- // 其他错误处理逻辑
- }
- QByteArray jsonData = file.readAll();
- file.close();
- QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
- if (jsonDoc.isNull()) {
- qDebug() << "Failed to parse JSON data.";
- return;
- }
- // 将 JSON 数据转换为对象
- QJsonObject jsonObj = jsonDoc.object();
- // 使用 jsonObj 进行进一步操作,例如读取特定的键值对
- QJsonObject newQ;
- QJsonArray newA;
- newQ.insert("children",newA);
- newQ.insert("name",currentDateTimeString);
- QJsonArray qaa=jsonObj["children"].toArray();
- qaa.append(newQ);
- jsonObj["children"]=qaa;
- QJA=qaa;
- tool::writeJsonFile(filePath,jsonObj);
- items1.append(item2);
- model1->appendRow(items1);
- } else {
- qDebug() << "Failed to create folder.";
- }
- } else {
- qDebug() << "Folder already exists.";
- }
- }
- void MainWindow::on_pushButton_5_clicked()
- {
- // 获取选择模型
- QItemSelectionModel* selectionModel = ui->treeView->selectionModel();
- if (selectionModel)
- {
- // 获取当前选中的节点的索引
- QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
- QModelIndex parentIndex = selectedIndexes.first().parent();
- if(parentIndex.isValid())
- {
- QMessageBox::information(this, "提示", "请选中一级节点");
- return;
- }
- // 遍历选中的索引列表
- foreach (const QModelIndex& index, selectedIndexes)
- {
- // 打印选中节点的数据
- QVariant data = index.data(Qt::DisplayRole);
- qDebug() << "Selected Node Data: " << data.toString();
- QString add_Path=root_pro_path+"/"+ui->comboBox->currentText()+"/"+data.toString();
- QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json";
- //直接对新增的版本添加config.json group1 group2 group3 group4
- qDebug()<<add_Path;
- QDir folder(add_Path);
- QStringList subFolders = folder.entryList(QDir::Dirs | QDir::NoDotAndDotDot); // 获取文件夹列表
- QString folderCount = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); // 文件夹数目
- qDebug() << "Number of sub-folders: " << folderCount;
- //新建版本
- QDir dir;
- QString version_path=add_Path+"/"+"version"+folderCount;
- if (dir.exists(version_path))
- {
- return;
- }
- if(!dir.mkpath(version_path))
- {
- return;
- }
- //新建版本下的子文件夹等
- dir.mkpath(version_path+"/"+"group1");
- dir.mkpath(version_path+"/"+"group2");
- dir.mkpath(version_path+"/"+"group3");
- dir.mkpath(version_path+"/"+"group4");
- QString filePath = version_path+"/"+"config.json"; // 指定新文件的路径和名称
- QJsonArray jsonArray;
- for(int i=0;i<4;i++)
- {
- // add obj
- QJsonObject object1;
- QJsonArray children1;
- object1["children"] = children1;
- object1["selected"] = "0";
- jsonArray.append(object1);
- }
- QFile file(filePath);
- if (file.open(QIODevice::WriteOnly | QIODevice::Text))
- {
- QJsonDocument jsonDocument(jsonArray);
- QString jsonString = jsonDocument.toJson();
- QTextStream stream(&file);
- stream << jsonString; // 将JSON文本写入文件
- file.close();
- qDebug() << "File written successfully!";
- }
- else
- {
- qDebug() << "Failed to write the file!";
- }
- //修改项目根json文件 modify_pro_config
- //folderCount
- qDebug()<<index.row();
- 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 childObject = parentArray[index.row()].toObject();
- QJsonArray nestedArray = childObject["children"].toArray();
- // {
- // "name": "version1"
- // }
- QJsonObject temo1;
- temo1["name"]="version" + folderCount;
- nestedArray.append(temo1);
- childObject["children"] = nestedArray;
- parentArray[index.row()] = childObject;
- jsonObj["children"] = parentArray;
- qDebug() << "Modified QJsonObject:" << jsonObj;
- QJA=parentArray;
- tool::writeJsonFile(modify_pro_config,jsonObj);
- //
- QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
- if (model) {
- QStandardItem* parentItem = model->itemFromIndex(index);
- if (parentItem) {
- QStandardItem* childItem = new QStandardItem("version"+folderCount);
- parentItem->appendRow(childItem);
- }
- }
- }
- }
- }
- void MainWindow::on_comboBox_customContextMenuRequested(const QPoint &pos)
- {
- }
- void MainWindow::on_pushButton_9_clicked()
- {
- // qDebug()<<ui->treeView->currentIndex();
- // 获取被选中的节点
- QModelIndex selectedIndex = ui->treeView->currentIndex();
- if(!selectedIndex.isValid())
- {
- // 如果没有选中任何节点,则弹出提示框
- QMessageBox::information(this, "提示", "没有选中任何节点");
- return;
- }
- // 检查选中的节点是一级节点还是二级节点
- if(selectedIndex.parent().isValid())
- {
- // 如果选中的节点有父节点,那么它是二级节点
- // 在这里执行二级节点对应的代码...
- QMessageBox::StandardButton reply;
- reply = QMessageBox::question(this, "Test", "确定要删除"+selectedIndex.data().toString(),
- QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
- if (reply == QMessageBox::Yes) {
- qDebug() << "Yes was clicked";
- qDebug() << "选中的是二级节点";
- QModelIndex parentIndex = selectedIndex.parent();
- QString del_Path=root_pro_path+"/"+ui->comboBox->currentText()+"/"+parentIndex.data().toString()+"/"+selectedIndex.data().toString();
- QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json";
- //删除文件夹
- QDir dir(del_Path);
- dir.removeRecursively();
- //修改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 childObject = parentArray[parentIndex.row()].toObject();
- QJsonArray nestedArray = childObject["children"].toArray();
- nestedArray.removeAt(selectedIndex.row());
- childObject["children"] = nestedArray;
- parentArray[parentIndex.row()] = childObject;
- jsonObj["children"] = parentArray;
- qDebug() << "Modified QJsonObject:" << jsonObj;
- tool::writeJsonFile(modify_pro_config,jsonObj);
- QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
- if (model) {
- QStandardItem* parentItem = model->itemFromIndex(parentIndex);
- if (parentItem) {
- parentItem->removeRow(selectedIndex.row());
- }
- }
- } else if (reply == QMessageBox::No) {
- qDebug() << "No was clicked";
- } else {
- qDebug() << "Cancel was clicked";
- }
- }
- else
- {
- // 如果选中的节点没有父节点,那么它是一级节点
- // 在这里执行一级节点对应的代码...
- QMessageBox::StandardButton reply;
- reply = QMessageBox::question(this, "Test", "确定要删除"+selectedIndex.data().toString(),
- QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
- if (reply == QMessageBox::Yes) {
- qDebug() << "Yes was clicked";
- qDebug() << "选中的是一级节点";
- QString del_Path=root_pro_path+"/"+ui->comboBox->currentText()+"/"+selectedIndex.data().toString();
- QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json";
- //删除文件夹
- QDir dir(del_Path);
- dir.removeRecursively();
- //修改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();
- parentArray.removeAt(selectedIndex.row());
- jsonObj["children"] = parentArray;
- qDebug() << "Modified QJsonObject:" << jsonObj;
- tool::writeJsonFile(modify_pro_config,jsonObj);
- QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
- model->removeRow(selectedIndex.row());
- } else if (reply == QMessageBox::No) {
- qDebug() << "No was clicked";
- } else {
- qDebug() << "Cancel was 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);
- }
|