12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160 |
- #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>
- 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;
- //初始化右边
- // 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;
- // 处理其他字段...
- }
- 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控件
- 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::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);
- }
- 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::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)
- {
- 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;
- }
- 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;
- 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;
- dir.mkpath(version_path);
- //新建版本下的子文件夹等
- 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();
- 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;
- 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();
- 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();
- 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";
- }
- }
- }
|