mainwindow.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. #include "mainwindow.h"
  2. #include "custommodel.h"
  3. #include "ui_mainwindow.h"
  4. #include "listview_ad.h"
  5. #include "listview_delgate.h"
  6. #include <QStandardItemModel>
  7. #include"../sample_button_slide/CustomButton.h"
  8. #include"../test_drag/mylistwidget.h"
  9. #include <QDir>
  10. #include <QFile>
  11. #include <QTextStream>
  12. #include <iostream>
  13. #include <QMimeData>
  14. using namespace std;
  15. MainWindow::MainWindow(QWidget *parent)
  16. : QMainWindow(parent)
  17. , ui(new Ui::MainWindow)
  18. {
  19. ui->setupUi(this);
  20. //add_slide;
  21. QWidget *slide_2= ui->slide_2; // 获取ceshi_2_slide控件指针
  22. customB2 = new CustomButton(this); // 创建自定义控件实例
  23. connect(customB2, &CustomButton::click, this, &MainWindow::onWidgetClicked);
  24. // 将自定义控件添加到ceshi_2_slide控件中
  25. QHBoxLayout *layout2 = new QHBoxLayout(slide_2);
  26. layout2->setContentsMargins(0, 0, 0, 0);
  27. layout2->addWidget(customB2);
  28. layout2->setSpacing(0);
  29. //add_slide;
  30. QWidget *slide_3= ui->slide_3; // 获取ceshi_2_slide控件指针
  31. customB3 = new CustomButton(this); // 创建自定义控件实例
  32. connect(customB3, &CustomButton::click, this, &MainWindow::onWidgetClicked);
  33. // 将自定义控件添加到ceshi_2_slide控件中
  34. QHBoxLayout *layout3 = new QHBoxLayout(slide_3);
  35. layout3->setContentsMargins(0, 0, 0, 0);
  36. layout3->addWidget(customB3);
  37. layout3->setSpacing(0);
  38. //add_slide;
  39. QWidget *slide_4= ui->slide_4; // 获取ceshi_2_slide控件指针
  40. customB4 = new CustomButton(this); // 创建自定义控件实例
  41. connect(customB4, &CustomButton::click, this, &MainWindow::onWidgetClicked);
  42. // 将自定义控件添加到ceshi_2_slide控件中
  43. QHBoxLayout *layout4 = new QHBoxLayout(slide_4);
  44. layout4->setContentsMargins(0, 0, 0, 0);
  45. layout4->addWidget(customB4);
  46. layout4->setSpacing(0);
  47. //初始化右侧u
  48. // MyListWidget* my1=new MyListWidget();
  49. // QVBoxLayout *layout5 = new QVBoxLayout();
  50. // layout5->addWidget(my1);
  51. // ui->test_frame->setLayout(layout5);
  52. // connect(my1, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted);
  53. my1=new MyListWidget();
  54. my1->setObjectName("listWidget");
  55. QVBoxLayout *layout5 = new QVBoxLayout();
  56. layout5->addWidget(my1);
  57. ui->frame->setLayout(layout5);
  58. qDebug()<<"widget_group"<<ui->widget_group1;
  59. connect(my1, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted);
  60. connect(my1, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_customContextMenuRequested(QPoint)));
  61. my2=new MyListWidget();
  62. my2->setObjectName("listWidget_2");
  63. QVBoxLayout *layout6 = new QVBoxLayout();
  64. layout6->addWidget(my2);
  65. ui->frame_4->setLayout(layout6);
  66. connect(my2, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted);
  67. connect(my2, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_customContextMenuRequested(QPoint)));
  68. my3=new MyListWidget();
  69. my3->setObjectName("listWidget_3");
  70. QVBoxLayout *layout7 = new QVBoxLayout();
  71. layout7->addWidget(my3);
  72. ui->frame_5->setLayout(layout7);
  73. connect(my3, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted);
  74. connect(my3, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_customContextMenuRequested(QPoint)));
  75. my4=new MyListWidget();
  76. my4->setObjectName("listWidget_4");
  77. QVBoxLayout *layout8 = new QVBoxLayout();
  78. layout8->addWidget(my4);
  79. ui->frame_6->setLayout(layout8);
  80. connect(my4, &MyListWidget::operationCompleted, this, &MainWindow::handleOperationCompleted);
  81. connect(my4, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_listWidget_customContextMenuRequested(QPoint)));
  82. QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2";
  83. root_pro_path=folder;
  84. qDebug()<<folder;
  85. QStringList qs=tool::getFilesInFolder(folder);//获取项目名称列表
  86. qDebug()<<qs;
  87. //初始化comboBox
  88. initComboBox(qs);
  89. QString currentPro = ui->comboBox->currentText();
  90. //初始化TreeView
  91. QString jsonPath = folder+"/"+currentPro+"/"+"Hotupdate.json";
  92. qDebug()<<jsonPath;
  93. QJO=tool::readJsonFile(jsonPath);
  94. // 从JSON对象中提取数据
  95. QJA = QJO.value("children").toArray();
  96. ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
  97. connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu);
  98. //初始化treeview 和 combobox
  99. QTreeView *treeView=ui->treeView;
  100. QStandardItemModel* model=new QStandardItemModel(treeView);
  101. model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("名称"));
  102. treeView->setModel(model);
  103. initTreeView(model->invisibleRootItem(),QJA);
  104. qDebug()<<"QJA"<<QJA;
  105. //初始化右边
  106. // initRight(QString parentNode,QString childNode);
  107. }
  108. void QTreeView::mousePressEvent(QMouseEvent *event)
  109. {
  110. QModelIndex index = indexAt(event->pos());
  111. if (index.isValid() && index.parent().isValid())
  112. {
  113. // 这是一个二级列表项,你可以在这里处理点击事件
  114. // 例如,获取二级列表项的数据并进行相应的操作
  115. QVariant data = index.data(Qt::DisplayRole);
  116. qDebug() << "Clicked on subitem:" << data.toString();
  117. }
  118. else
  119. {
  120. qDebug() << "Clicked on subitem false";
  121. QTreeView::mousePressEvent(event);
  122. }
  123. }
  124. MainWindow::~MainWindow()
  125. {
  126. delete ui;
  127. }
  128. void MainWindow::readFile(const QString& filePath)//设置打开该窗体时检查路径并加载
  129. {
  130. // 获取文件所在路径
  131. QString dirPath = QFileInfo(filePath).absolutePath();
  132. qDebug() << dirPath;
  133. // 创建路径
  134. QDir dir;
  135. if (!dir.mkpath(dirPath))
  136. {
  137. qDebug() << "无法创建路径:" << dirPath;
  138. return;
  139. }
  140. // 创建文件
  141. QFile file(filePath);
  142. if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
  143. {
  144. qDebug() << "无法创建文件:" << file.errorString();
  145. return;
  146. }
  147. // 使用QTextStream读取文件内容
  148. QTextStream in(&file);
  149. while (!in.atEnd())
  150. {
  151. QString line = in.readLine();
  152. // 处理每一行的内容
  153. // TODO: 添加您的逻辑
  154. qDebug() << line;
  155. }
  156. // 关闭文件
  157. file.close();
  158. }
  159. void MainWindow::on_pushButton_clicked()
  160. {
  161. dialog=new Dialog(this);
  162. dialog->setModal(false);
  163. dialog->show();
  164. }
  165. void MainWindow::on_pushButton_2_clicked()
  166. {
  167. dialog=new Dialog(this);
  168. dialog->setModal(false);
  169. dialog->show();
  170. }
  171. QJsonObject MainWindow::readJsonFile(const QString& filePath)
  172. {
  173. // 打开JSON文件
  174. QFile file(filePath);
  175. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  176. {
  177. qDebug() << "无法打开文件:" << file.errorString();
  178. return QJsonObject();
  179. }
  180. // 读取文件内容
  181. QByteArray jsonData = file.readAll();
  182. file.close();
  183. // 解析JSON数据
  184. QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
  185. if (!jsonDoc.isObject())
  186. {
  187. qDebug() << "无效的JSON文件";
  188. return QJsonObject();;
  189. }
  190. // 获取JSON对象
  191. QJsonObject jsonObj = jsonDoc.object();
  192. return jsonObj;
  193. // 处理其他字段...
  194. }
  195. void MainWindow::init()
  196. {
  197. QString filePath = "~/Documents/RedInterstitialData/Hotupdate.tool/Hotupdate1.json";
  198. QJA=tool::getJsanArra(filePath);
  199. // 从JSON对象中提取数据
  200. int num=QJA.count();
  201. qDebug() << num;
  202. QStringList pro_list;
  203. for(int i=1;i<=num;i++)
  204. {
  205. pro_list.append(QString::number(i));
  206. }
  207. ui->comboBox->clear();
  208. ui->comboBox->addItems(pro_list);
  209. ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
  210. connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu);
  211. //初始化当前窗口
  212. int index=ui->comboBox->currentData().toString().toInt();
  213. inittreeview(QJA[index]);
  214. }
  215. void MainWindow::init2()
  216. {
  217. // QString filePath = "~/Documents/RedInterstitialData/Hotupdate2/Hotupdate2.json";
  218. // QString folder = "~/Documents/RedInterstitialData/Hotupdate2";
  219. // tool::generateFolderJsonFile(folder,filePath);
  220. // QString jsonPath = "~/Documents/RedInterstitialData/Hotupdate2/Hotupdate2.json";
  221. // QJO=tool::readJsonFile(jsonPath);
  222. // // 从JSON对象中提取数据
  223. // QJA = QJO.value("children").toArray();
  224. // ui->comboBox->clear();
  225. // ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
  226. // connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu);
  227. // //初始化treeview 和 combobox
  228. // QTreeView *treeView=ui->treeView;
  229. // QStandardItemModel* model=new QStandardItemModel(treeView);
  230. // model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("名称"));
  231. // treeView->setModel(model);
  232. // QStringList pro_list;
  233. // ui->comboBox->clear();
  234. // pro_list=initializeTreeView_conboBox(model->invisibleRootItem(),QJA);
  235. // ui->comboBox->addItems(pro_list);
  236. }
  237. //初始化treeview 和combobox控件
  238. void MainWindow::initTreeView(QStandardItem* parentItem,const QJsonArray& jsonArray) {
  239. QStringList pro_list;
  240. for (QJsonArray::const_iterator it = jsonArray.constBegin(); it != jsonArray.constEnd(); ++it) {
  241. QJsonValue jsonValue = *it;
  242. qDebug()<<jsonValue;
  243. // 处理每个元素,可以根据需要进行类型检查和值提取
  244. if (jsonValue.isString()) {
  245. QString fileName = jsonValue.toString();
  246. QStandardItem* item = new QStandardItem(fileName);
  247. // if(!fileName.endsWith("json"))
  248. // parentItem->appendRow(item);
  249. // 处理字符串值 s
  250. } else if (jsonValue.isDouble()) {
  251. double doubleValue = jsonValue.toDouble();
  252. // 处理双精度浮点数值
  253. } else if (jsonValue.isObject()) {
  254. QJsonObject objectValue = jsonValue.toObject();
  255. QString name = objectValue["name"].toString();
  256. QStandardItem* item = new QStandardItem(name);
  257. parentItem->appendRow(item);
  258. QJsonArray childrenArray = objectValue["children"].toArray();
  259. pro_list.append(name);
  260. // 处理对象值
  261. initTreeView(item, childrenArray);
  262. }
  263. }
  264. }
  265. void MainWindow::inittreeview(QJsonValueRef ref)
  266. {
  267. //1,构造Model,这里示例具有3层关系的model构造过程
  268. QStandardItemModel* model = new QStandardItemModel(ui->treeView);
  269. model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("序号") << QStringLiteral("名称")); //设置列头
  270. for(int i=0;i<5;i++)
  271. {
  272. //一级节点,加入mModel
  273. QList<QStandardItem*> items1;
  274. QStandardItem* item1 = new QStandardItem(QString::number(i));
  275. QStandardItem* item2 = new QStandardItem(QStringLiteral("一级节点"));
  276. items1.append(item1);
  277. items1.append(item2);
  278. model->appendRow(items1);
  279. for(int j=0;j<5;j++)
  280. {
  281. //二级节点,加入第1个一级节点
  282. QList<QStandardItem*> items2;
  283. QStandardItem* item3 = new QStandardItem(QString::number(j));
  284. QStandardItem* item4 = new QStandardItem(QStringLiteral("二级节点"));
  285. items2.append(item3);
  286. items2.append(item4);
  287. item1->appendRow(items2);
  288. }
  289. }
  290. //2,给QTreeView应用model
  291. ui->treeView->setModel(model);
  292. }
  293. void MainWindow::on_pushButton_4_clicked()
  294. {
  295. QList<QStandardItem*> items1;
  296. QStandardItemModel *model1 = static_cast<QStandardItemModel*>(ui->treeView->model());
  297. QStandardItem* item2 = new QStandardItem(QStringLiteral("新建测试"));
  298. items1.append(item2);
  299. model1->appendRow(items1);
  300. }
  301. void MainWindow::slotTreeMenu(const QPoint &pos)
  302. {
  303. QString qss = "QMenu{color:#E8E8E8;background:#4D4D4D;margin:2px;}\
  304. QMenu::item{padding:3px 20px 3px 20px;}\
  305. QMenu::indicator{width:13px;height:13px;}\
  306. QMenu::item:selected{color:#E8E8E8;border:0px solid #575757;background:#1E90FF;}\
  307. QMenu::separator{height:1px;background:#757575;}";
  308. QMenu menu;
  309. menu.setStyleSheet(qss); //可以给菜单设置样式
  310. QModelIndex curIndex = ui->treeView->indexAt(pos); //当前点击的元素的index
  311. QModelIndex index = curIndex.sibling(curIndex.row(),0); //该行的第1列元素的index
  312. if (index.isValid())
  313. {
  314. //可获取元素的文本、data,进行其他判断处理
  315. //QStandardItem* item = mModel->itemFromIndex(index);
  316. //QString text = item->text();
  317. //QVariant data = item->data(Qt::UserRole + 1);
  318. //...
  319. //添加一行菜单,进行展开
  320. menu.addAction(QStringLiteral("删除"), this, SLOT(slotTreeMenuDelete(bool)));
  321. }
  322. menu.exec(QCursor::pos()); //显示菜单
  323. }
  324. void MainWindow::slotTreeMenuExpand(bool checked)
  325. {
  326. QModelIndex curIndex = ui->treeView->currentIndex();
  327. QModelIndex index = curIndex.sibling(curIndex.row(),0); //同一行第一列元素的index
  328. if(index.isValid())
  329. {
  330. ui->treeView->expand(index);
  331. }
  332. }
  333. void MainWindow::slotTreeMenuCollapse(bool checked)
  334. {
  335. QModelIndex curIndex = ui->treeView->currentIndex();
  336. QModelIndex index = curIndex.sibling(curIndex.row(),0); //同一行第一列元素的index
  337. if(index.isValid())
  338. {
  339. ui->treeView->collapse(index);
  340. }
  341. }
  342. //删除设定为假删除,在窗体退出的时候才更新json文件,并执行删除操作
  343. /////////到这里
  344. /// ///
  345. /// ////
  346. /// ///
  347. /// //
  348. /// //
  349. /// /
  350. ///
  351. /// //
  352. void MainWindow::slotTreeMenuDelete(bool checked)
  353. {
  354. QModelIndex curIndex = ui->treeView->currentIndex();
  355. QModelIndex index = curIndex.sibling(curIndex.row(),0); //同一行第一列元素的index
  356. qDebug()<<curIndex;
  357. qDebug()<<index;
  358. }
  359. void MainWindow::initComboBox(QStringList qs){
  360. ui->comboBox->clear();
  361. ui->comboBox->addItems(qs);
  362. }
  363. void MainWindow::initRight(QString parentNode,QString childNode){
  364. //
  365. QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2";
  366. QString currentPro = ui->comboBox->currentText();
  367. config_json_path=folder+"/"+currentPro+"/"+parentNode+"/"+childNode+"/config.json";
  368. treeview_path=folder+"/"+currentPro+"/"+parentNode+"/"+childNode;
  369. qDebug()<<config_json_path;
  370. qDebug()<<treeview_path;
  371. RQJA=tool::getJsanArra(config_json_path);
  372. for (int i = 0; i < RQJA.size(); ++i) {
  373. QJsonValueRef value = RQJA[i];
  374. QJsonObject jsonObject = value.toObject();
  375. QJsonValue jsonArrayValue = jsonObject.value("children");
  376. QJsonValue Used=jsonObject.value("selected");
  377. QString used=Used.toString();
  378. QJsonArray jsonArray = jsonArrayValue.toArray();
  379. qDebug()<<jsonArray.size();
  380. CustomButton* temb=nullptr;
  381. QListWidget* rightLw;
  382. if(i==0)
  383. {
  384. rightLw=my1;
  385. }
  386. else if(i==1)
  387. {
  388. temb=customB2;
  389. rightLw=my2;
  390. }
  391. else if(i==2)
  392. {
  393. temb=customB3;
  394. rightLw=my3;
  395. }
  396. else{
  397. temb=customB4;
  398. rightLw=my4;
  399. }
  400. if(used=="1"&&temb)
  401. {
  402. if(temb->dir==Qt::AlignLeft){
  403. temb->pos_right_button();
  404. }
  405. }
  406. if(used=="0"&&temb)
  407. {
  408. if(temb->dir==Qt::AlignRight){
  409. temb->pos_left_button();
  410. }
  411. }
  412. initRightListwidget(rightLw,jsonArray);
  413. }
  414. }
  415. void MainWindow::on_treeView_clicked(const QModelIndex &index)
  416. {
  417. // 获取第一级节点的信息
  418. if(index.parent().isValid())
  419. {
  420. QModelIndex parentIndex = index.parent();
  421. QString parentData = parentIndex.data().toString();
  422. qDebug() << "First-level Node: " << parentData;
  423. // 获取第二级节点的信息
  424. QString childData = index.data().toString();
  425. qDebug() << "Second-level Node: " << childData;
  426. initRight(parentData,childData);
  427. }
  428. }
  429. void MainWindow::initRightListwidget(QListWidget* qw,const QJsonArray& qja)
  430. {
  431. QStringList dataList;
  432. for (const QJsonValue& value : qja) {
  433. if (value.isString()) {
  434. QString stringValue = value.toString();
  435. dataList.append(stringValue);
  436. }
  437. }
  438. qDebug()<<"initRightListwidget";
  439. qDebug()<<"dataList"<<dataList;
  440. qDebug()<<"qw"<<qw;
  441. qw->clear();
  442. qw->addItems(dataList);
  443. }
  444. /////////////////////////////////////////////////////////////////////////////////////////////右侧右击事件
  445. void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
  446. {
  447. qDebug()<<"1111111";
  448. QListWidgetItem* curItem = my1->itemAt( pos );
  449. if( curItem == NULL )
  450. return;
  451. QMenu *popMenu = new QMenu( this );
  452. QAction *deleteSeed = new QAction(tr("Delete"), this);
  453. QString qss = "QMenu{color:#E8E8E8;background:#4D4D4D;margin:2px;}\
  454. QMenu::item{padding:3px 20px 3px 20px;}\
  455. QMenu::indicator{width:13px;height:13px;}\
  456. QMenu::item:selected{color:#E8E8E8;border:0px solid #575757;background:#1E90FF;}\
  457. QMenu::separator{height:1px;background:#757575;}";
  458. popMenu->setStyleSheet(qss);
  459. popMenu->addAction( deleteSeed );
  460. this->qlw=my1;
  461. connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) );
  462. popMenu->exec( QCursor::pos() );
  463. delete popMenu;
  464. delete deleteSeed;
  465. }
  466. void MainWindow::on_listWidget_2_customContextMenuRequested(const QPoint &pos)
  467. {
  468. QListWidgetItem* curItem = my2->itemAt( pos );
  469. if( curItem == NULL )
  470. return;
  471. QMenu *popMenu = new QMenu( this );
  472. QAction *deleteSeed = new QAction(tr("Delete"), this);
  473. popMenu->addAction( deleteSeed );
  474. this->qlw=my2;
  475. connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()));
  476. popMenu->exec( QCursor::pos() );
  477. delete popMenu;
  478. delete deleteSeed;
  479. }
  480. /////daozheer 可以删除项 还没有修改json文件
  481. void MainWindow::deleteSeedSlot()
  482. {
  483. qDebug()<<"deleteSeedSlot";
  484. QListWidgetItem * item = this->qlw->currentItem();
  485. qDebug()<<this->qlw;
  486. if( item == NULL )
  487. return;
  488. int curIndex = this->qlw->row(item);
  489. qDebug()<<curIndex;
  490. auto curItem=this->qlw->takeItem(curIndex);
  491. qDebug()<<curItem->text();
  492. // updateConfig(ui->listWidget);//更新对应的config.json文件
  493. QString deal_file_path;
  494. QString group_name;
  495. int group_index;
  496. if(qlw->objectName()=="listWidget")
  497. {
  498. group_name="group1";
  499. group_index=0;
  500. }
  501. else if(qlw->objectName()=="listWidget_2")
  502. {
  503. group_name="group2";
  504. group_index=1;
  505. }
  506. else if(qlw->objectName()=="listWidget_3")
  507. {
  508. group_name="group3";
  509. group_index=2;
  510. }
  511. else
  512. {
  513. group_name="group4";
  514. group_index=3;
  515. }
  516. deal_file_path=treeview_path+"/"+group_name+"/"+curItem->text();
  517. QFile file(deal_file_path);
  518. file.remove();
  519. QJsonObject groupObject = RQJA[group_index].toObject();
  520. QJsonArray childrenArray = groupObject["children"].toArray();
  521. childrenArray.removeAt(curIndex);
  522. groupObject["children"] = childrenArray;
  523. RQJA[group_index] = groupObject;
  524. writeBackToConfigJson();
  525. delete item;
  526. }
  527. void MainWindow::on_listWidget_3_customContextMenuRequested(const QPoint &pos)
  528. {
  529. QListWidgetItem* curItem = my3->itemAt( pos );
  530. if( curItem == NULL )
  531. return;
  532. QMenu *popMenu = new QMenu( this );
  533. QAction *deleteSeed = new QAction(tr("Delete"), this);
  534. popMenu->addAction( deleteSeed );
  535. this->qlw=my3;
  536. connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) );
  537. popMenu->exec( QCursor::pos() );
  538. delete popMenu;
  539. delete deleteSeed;
  540. }
  541. void MainWindow::on_listWidget_4_customContextMenuRequested(const QPoint &pos)
  542. {
  543. QListWidgetItem* curItem = my4->itemAt( pos );
  544. if( curItem == NULL )
  545. return;
  546. QMenu *popMenu = new QMenu( this );
  547. QAction *deleteSeed = new QAction(tr("Delete"), this);
  548. popMenu->addAction( deleteSeed );
  549. this->qlw=my4;
  550. connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) );
  551. popMenu->exec( QCursor::pos() );
  552. delete popMenu;
  553. delete deleteSeed;
  554. }
  555. /////////////////////////////////////////////////////////////////////////////////////////////右侧右击事件
  556. //滑块点击slot函数
  557. void MainWindow::onWidgetClicked()
  558. {
  559. QObject *senderObj = sender(); // 获取发出信号的对象指针
  560. if(RQJA.empty())return;
  561. if (senderObj)
  562. {
  563. QWidget *clickedWidget = qobject_cast<QWidget*>(senderObj); // 将指针转换为QWidget指针
  564. if (clickedWidget)
  565. {
  566. QString widgetName = clickedWidget->parentWidget()->objectName(); // 获取控件的对象名称
  567. if(widgetName=="slide_2")
  568. {
  569. if(customB2->dir==Qt::AlignLeft)
  570. {
  571. //变为1
  572. QJsonValueRef selectedValueRef = RQJA[1].toObject()["selected"];
  573. selectedValueRef = "1";
  574. qDebug()<<RQJA;
  575. }
  576. else{
  577. //变为0
  578. QJsonValueRef selectedValueRef = RQJA[1].toObject()["selected"];
  579. selectedValueRef = "0";
  580. qDebug()<<RQJA;
  581. }
  582. }
  583. else if(widgetName=="slide_3")
  584. {
  585. if(customB3->dir==Qt::AlignLeft)
  586. {
  587. //变为1
  588. QJsonValueRef selectedValueRef = RQJA[2].toObject()["selected"];
  589. selectedValueRef = "1";
  590. qDebug()<<RQJA;
  591. }
  592. else{
  593. //变为0
  594. QJsonValueRef selectedValueRef = RQJA[2].toObject()["selected"];
  595. selectedValueRef = "0";
  596. qDebug()<<RQJA;
  597. }
  598. }
  599. else
  600. {
  601. if(customB4->dir==Qt::AlignLeft)
  602. {
  603. //变为1
  604. QJsonValueRef selectedValueRef = RQJA[3].toObject()["selected"];
  605. selectedValueRef = "1";
  606. qDebug()<<RQJA;
  607. }
  608. else{
  609. //变为0
  610. QJsonValueRef selectedValueRef = RQJA[3].toObject()["selected"];
  611. selectedValueRef = "0";
  612. qDebug()<<RQJA;
  613. }
  614. }
  615. writeBackToConfigJson();
  616. }
  617. }
  618. }
  619. //修改config文件
  620. void MainWindow::writeBackToConfigJson(){
  621. if(!config_json_path.endsWith("json"))return;
  622. qDebug()<<config_json_path;
  623. QFile jsonFile(config_json_path);
  624. if (jsonFile.open(QIODevice::WriteOnly)) {
  625. QJsonDocument jsonDoc(RQJA);
  626. QByteArray jsonData = jsonDoc.toJson();
  627. qint64 bytesWritten = jsonFile.write(jsonData);
  628. if (bytesWritten == -1) {
  629. qDebug() << "Failed to write data to file.";
  630. } else {
  631. qDebug() << "Data written to file successfully.";
  632. }
  633. jsonFile.close();
  634. } else {
  635. qDebug() << "Failed to open file for writing.";
  636. }
  637. }
  638. //重新选择项目进行初始化
  639. void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
  640. {
  641. clearRight();
  642. QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2";
  643. QString currentPro = ui->comboBox->currentText();
  644. //初始化TreeView
  645. QString jsonPath = folder+"/"+currentPro+"/"+"Hotupdate.json";
  646. QJO=tool::readJsonFile(jsonPath);
  647. // 从JSON对象中提取数据
  648. QJA = QJO.value("children").toArray();
  649. ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
  650. connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu);
  651. //初始化treeview 和 combobox
  652. QTreeView *treeView=ui->treeView;
  653. QStandardItemModel* model=new QStandardItemModel(treeView);
  654. model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("名称"));
  655. treeView->setModel(model);
  656. initTreeView(model->invisibleRootItem(),QJA);
  657. qDebug()<<"QJA"<<QJA;
  658. }
  659. void MainWindow::clearRight()
  660. {
  661. my1->clear();
  662. my2->clear();
  663. my3->clear();
  664. my4->clear();
  665. this->customB2->pos_left_button();
  666. this->customB3->pos_left_button();
  667. this->customB4->pos_left_button();
  668. }
  669. void MainWindow::handleOperationCompleted(QString fromfilePath)
  670. {
  671. QStringList parts = fromfilePath.split("/");
  672. QObject *senderObj = sender();
  673. QListWidget *LW = qobject_cast<QListWidget*>(senderObj);
  674. //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";
  675. QString group_name;
  676. int group_index;
  677. if(LW->objectName()=="listWidget")
  678. {
  679. group_name="group1";
  680. group_index=0;
  681. }
  682. else if(LW->objectName()=="listWidget_2")
  683. {
  684. group_name="group2";
  685. group_index=1;
  686. }
  687. else if(LW->objectName()=="listWidget_3")
  688. {
  689. group_name="group3";
  690. group_index=2;
  691. }
  692. else
  693. {
  694. group_name="group4";
  695. group_index=3;
  696. }
  697. QString tofilePath1 = treeview_path+"/"+group_name;
  698. QFile sourceFile(fromfilePath);
  699. QFile targetFile(tofilePath1 + "/" +parts[parts.size()-1]);
  700. if (sourceFile.exists() && QDir(tofilePath1).exists()) {
  701. if (sourceFile.copy(targetFile.fileName())) {
  702. qDebug() << "File copied successfully.";
  703. LW->addItem(parts[parts.size()-1]);
  704. qDebug()<<this->parent();
  705. QJsonObject groupObject = RQJA[group_index].toObject();
  706. QJsonArray childrenArray = groupObject["children"].toArray();
  707. childrenArray.append(parts[parts.size()-1]);
  708. groupObject["children"] = childrenArray;
  709. RQJA[group_index] = groupObject;
  710. writeBackToConfigJson();
  711. } else {
  712. qDebug() << "Failed to copy file.";
  713. QMessageBox::information(this, "失败", "文件名重复");
  714. }
  715. } else {
  716. qDebug() << "Source file or target folder does not exist.";
  717. }
  718. }