mainwindow.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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_2_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_3_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_4_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. ////////////////////////////////////////////////
  97. ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
  98. connect(ui->treeView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotTreeMenu(QPoint)));
  99. //初始化treeview 和 combobox
  100. QTreeView *treeView=ui->treeView;
  101. QStandardItemModel* model=new QStandardItemModel(treeView);
  102. model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("名称"));
  103. treeView->setModel(model);
  104. initTreeView(model->invisibleRootItem(),QJA);
  105. qDebug()<<"QJA"<<QJA;
  106. //初始化右边
  107. // initRight(QString parentNode,QString childNode);
  108. }
  109. void QTreeView::mousePressEvent(QMouseEvent *event)
  110. {
  111. QModelIndex index = indexAt(event->pos());
  112. if (index.isValid() && index.parent().isValid())
  113. {
  114. // 这是一个二级列表项,你可以在这里处理点击事件
  115. // 例如,获取二级列表项的数据并进行相应的操作
  116. QVariant data = index.data(Qt::DisplayRole);
  117. qDebug() << "Clicked on subitem:" << data.toString();
  118. }
  119. else
  120. {
  121. qDebug() << "Clicked on subitem false";
  122. QTreeView::mousePressEvent(event);
  123. }
  124. }
  125. MainWindow::~MainWindow()
  126. {
  127. delete ui;
  128. }
  129. void MainWindow::readFile(const QString& filePath)//设置打开该窗体时检查路径并加载
  130. {
  131. // 获取文件所在路径
  132. QString dirPath = QFileInfo(filePath).absolutePath();
  133. qDebug() << dirPath;
  134. // 创建路径
  135. QDir dir;
  136. if (!dir.mkpath(dirPath))
  137. {
  138. qDebug() << "无法创建路径:" << dirPath;
  139. return;
  140. }
  141. // 创建文件
  142. QFile file(filePath);
  143. if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
  144. {
  145. qDebug() << "无法创建文件:" << file.errorString();
  146. return;
  147. }
  148. // 使用QTextStream读取文件内容
  149. QTextStream in(&file);
  150. while (!in.atEnd())
  151. {
  152. QString line = in.readLine();
  153. // 处理每一行的内容
  154. // TODO: 添加您的逻辑
  155. qDebug() << line;
  156. }
  157. // 关闭文件
  158. file.close();
  159. }
  160. void MainWindow::on_pushButton_clicked()
  161. {
  162. dialog=new Dialog(this);
  163. dialog->setModal(false);
  164. dialog->show();
  165. }
  166. void MainWindow::on_pushButton_2_clicked()
  167. {
  168. dialog=new Dialog(this);
  169. dialog->setModal(false);
  170. dialog->show();
  171. }
  172. QJsonObject MainWindow::readJsonFile(const QString& filePath)
  173. {
  174. // 打开JSON文件
  175. QFile file(filePath);
  176. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  177. {
  178. qDebug() << "无法打开文件:" << file.errorString();
  179. return QJsonObject();
  180. }
  181. // 读取文件内容
  182. QByteArray jsonData = file.readAll();
  183. file.close();
  184. // 解析JSON数据
  185. QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
  186. if (!jsonDoc.isObject())
  187. {
  188. qDebug() << "无效的JSON文件";
  189. return QJsonObject();;
  190. }
  191. // 获取JSON对象
  192. QJsonObject jsonObj = jsonDoc.object();
  193. return jsonObj;
  194. // 处理其他字段...
  195. }
  196. void MainWindow::init()
  197. {
  198. QString filePath = "~/Documents/RedInterstitialData/Hotupdate.tool/Hotupdate1.json";
  199. QJA=tool::getJsanArra(filePath);
  200. // 从JSON对象中提取数据
  201. int num=QJA.count();
  202. qDebug() << num;
  203. QStringList pro_list;
  204. for(int i=1;i<=num;i++)
  205. {
  206. pro_list.append(QString::number(i));
  207. }
  208. ui->comboBox->clear();
  209. ui->comboBox->addItems(pro_list);
  210. ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
  211. connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu);
  212. //初始化当前窗口
  213. int index=ui->comboBox->currentData().toString().toInt();
  214. inittreeview(QJA[index]);
  215. }
  216. void MainWindow::init2()
  217. {
  218. // QString filePath = "~/Documents/RedInterstitialData/Hotupdate2/Hotupdate2.json";
  219. // QString folder = "~/Documents/RedInterstitialData/Hotupdate2";
  220. // tool::generateFolderJsonFile(folder,filePath);
  221. // QString jsonPath = "~/Documents/RedInterstitialData/Hotupdate2/Hotupdate2.json";
  222. // QJO=tool::readJsonFile(jsonPath);
  223. // // 从JSON对象中提取数据
  224. // QJA = QJO.value("children").toArray();
  225. // ui->comboBox->clear();
  226. // ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
  227. // connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu);
  228. // //初始化treeview 和 combobox
  229. // QTreeView *treeView=ui->treeView;
  230. // QStandardItemModel* model=new QStandardItemModel(treeView);
  231. // model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("名称"));
  232. // treeView->setModel(model);
  233. // QStringList pro_list;
  234. // ui->comboBox->clear();
  235. // pro_list=initializeTreeView_conboBox(model->invisibleRootItem(),QJA);
  236. // ui->comboBox->addItems(pro_list);
  237. }
  238. //初始化treeview 和combobox控件
  239. void MainWindow::initTreeView(QStandardItem* parentItem,const QJsonArray& jsonArray) {
  240. QStringList pro_list;
  241. for (QJsonArray::const_iterator it = jsonArray.constBegin(); it != jsonArray.constEnd(); ++it) {
  242. QJsonValue jsonValue = *it;
  243. qDebug()<<jsonValue;
  244. // 处理每个元素,可以根据需要进行类型检查和值提取
  245. if (jsonValue.isString()) {
  246. QString fileName = jsonValue.toString();
  247. QStandardItem* item = new QStandardItem(fileName);
  248. // if(!fileName.endsWith("json"))
  249. // parentItem->appendRow(item);
  250. // 处理字符串值 s
  251. } else if (jsonValue.isDouble()) {
  252. double doubleValue = jsonValue.toDouble();
  253. // 处理双精度浮点数值
  254. } else if (jsonValue.isObject()) {
  255. QJsonObject objectValue = jsonValue.toObject();
  256. QString name = objectValue["name"].toString();
  257. QStandardItem* item = new QStandardItem(name);
  258. parentItem->appendRow(item);
  259. QJsonArray childrenArray = objectValue["children"].toArray();
  260. pro_list.append(name);
  261. // 处理对象值
  262. initTreeView(item, childrenArray);
  263. }
  264. }
  265. ui->treeView->clearSelection();
  266. ui->treeView->setCurrentIndex(QModelIndex());
  267. }
  268. void MainWindow::inittreeview(QJsonValueRef ref)
  269. {
  270. //1,构造Model,这里示例具有3层关系的model构造过程
  271. QStandardItemModel* model = new QStandardItemModel(ui->treeView);
  272. model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("序号") << QStringLiteral("名称")); //设置列头
  273. for(int i=0;i<5;i++)
  274. {
  275. //一级节点,加入mModel
  276. QList<QStandardItem*> items1;
  277. QStandardItem* item1 = new QStandardItem(QString::number(i));
  278. QStandardItem* item2 = new QStandardItem(QStringLiteral("一级节点"));
  279. items1.append(item1);
  280. items1.append(item2);
  281. model->appendRow(items1);
  282. for(int j=0;j<5;j++)
  283. {
  284. //二级节点,加入第1个一级节点
  285. QList<QStandardItem*> items2;
  286. QStandardItem* item3 = new QStandardItem(QString::number(j));
  287. QStandardItem* item4 = new QStandardItem(QStringLiteral("二级节点"));
  288. items2.append(item3);
  289. items2.append(item4);
  290. item1->appendRow(items2);
  291. }
  292. }
  293. //2,给QTreeView应用model
  294. ui->treeView->setModel(model);
  295. }
  296. void MainWindow::slotTreeMenu(const QPoint &pos)
  297. {
  298. // QModelIndex curItem = ui->treeView->indexAt( pos );
  299. // QModelIndex index = curItem.sibling(curItem.row(),0);
  300. // if( !index.isValid() )
  301. // return;
  302. // QMenu *popMenu = new QMenu( this );
  303. // QAction *deleteSeed = new QAction(tr("Delete"), this);
  304. // QString qss = "QMenu{color:#E8E8E8;background:#4D4D4D;margin:2px;}\
  305. // QMenu::item{padding:3px 20px 3px 20px;}\
  306. // QMenu::indicator{width:13px;height:13px;}\
  307. // QMenu::item:selected{color:#E8E8E8;border:0px solid #575757;background:#1E90FF;}\
  308. // QMenu::separator{height:1px;background:#757575;}";
  309. // popMenu->setStyleSheet(qss);
  310. // popMenu->addAction( deleteSeed );
  311. // connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( slotTreeMenuDelete()) );
  312. // popMenu->exec( QCursor::pos() );
  313. // delete popMenu;
  314. }
  315. //删除设定为假删除,在窗体退出的时候才更新json文件,并执行删除操作
  316. /////////到这里
  317. /// ///
  318. /// ////
  319. /// ///
  320. /// //
  321. /// //
  322. /// /
  323. ///
  324. /// //
  325. void MainWindow::slotTreeMenuDelete()
  326. {
  327. // QModelIndex curIndex = ui->treeView->currentIndex();
  328. // QModelIndex index = curIndex.sibling(curIndex.row(),0); //同一行第一列元素的index
  329. // qDebug()<<curIndex;
  330. // qDebug()<<index;
  331. // QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
  332. // model->removeRow(index.row(), index.parent());
  333. }
  334. void MainWindow::initComboBox(QStringList qs){
  335. ui->comboBox->clear();
  336. ui->comboBox->addItems(qs);
  337. }
  338. void MainWindow::initRight(QString parentNode,QString childNode){
  339. //
  340. QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2";
  341. QString currentPro = ui->comboBox->currentText();
  342. config_json_path=folder+"/"+currentPro+"/"+parentNode+"/"+childNode+"/config.json";
  343. treeview_path=folder+"/"+currentPro+"/"+parentNode+"/"+childNode;
  344. qDebug()<<config_json_path;
  345. qDebug()<<treeview_path;
  346. RQJA=tool::getJsanArra(config_json_path);
  347. for (int i = 0; i < RQJA.size(); ++i) {
  348. QJsonValueRef value = RQJA[i];
  349. QJsonObject jsonObject = value.toObject();
  350. QJsonValue jsonArrayValue = jsonObject.value("children");
  351. QJsonValue Used=jsonObject.value("selected");
  352. QString used=Used.toString();
  353. QJsonArray jsonArray = jsonArrayValue.toArray();
  354. qDebug()<<jsonArray.size();
  355. CustomButton* temb=nullptr;
  356. QListWidget* rightLw;
  357. if(i==0)
  358. {
  359. rightLw=my1;
  360. }
  361. else if(i==1)
  362. {
  363. temb=customB2;
  364. rightLw=my2;
  365. }
  366. else if(i==2)
  367. {
  368. temb=customB3;
  369. rightLw=my3;
  370. }
  371. else{
  372. temb=customB4;
  373. rightLw=my4;
  374. }
  375. if(used=="1"&&temb)
  376. {
  377. if(temb->dir==Qt::AlignLeft){
  378. temb->pos_right_button();
  379. }
  380. }
  381. if(used=="0"&&temb)
  382. {
  383. if(temb->dir==Qt::AlignRight){
  384. temb->pos_left_button();
  385. }
  386. }
  387. initRightListwidget(rightLw,jsonArray);
  388. }
  389. }
  390. void MainWindow::on_treeView_clicked(const QModelIndex &index)
  391. {
  392. // 获取第一级节点的信息
  393. if(index.parent().isValid())
  394. {
  395. QModelIndex parentIndex = index.parent();
  396. QString parentData = parentIndex.data().toString();
  397. qDebug() << "First-level Node: " << parentData;
  398. // 获取第二级节点的信息
  399. QString childData = index.data().toString();
  400. qDebug() << "Second-level Node: " << childData;
  401. initRight(parentData,childData);
  402. }
  403. }
  404. void MainWindow::mousePressEvent(QMouseEvent* event)
  405. {
  406. // // 调用基类的函数处理其他的鼠标点击事件
  407. // QMainWindow::mousePressEvent(event);
  408. // // 检查点击的位置是否在 treeView 的边界内
  409. // if(ui->treeView->rect().contains(event->pos()))
  410. // {
  411. // // 转换全局坐标为 treeView 的本地坐标
  412. // QPoint treeViewPos = ui->treeView->mapFromGlobal(event->globalPos());
  413. // // 检查是否有节点在点击的位置
  414. // if(!ui->treeView->indexAt(treeViewPos).isValid())
  415. // {
  416. // // 在这里执行你的操作...
  417. // qDebug() << "点击了 treeView 的空白部分";
  418. // ui->treeView->clearSelection();
  419. // ui->treeView->setCurrentIndex(QModelIndex());
  420. // }
  421. // }
  422. // else{
  423. // ui->treeView->clearSelection();
  424. // ui->treeView->setCurrentIndex(QModelIndex());
  425. // }
  426. }
  427. void MainWindow::initRightListwidget(QListWidget* qw,const QJsonArray& qja)
  428. {
  429. QStringList dataList;
  430. for (const QJsonValue& value : qja) {
  431. if (value.isString()) {
  432. QString stringValue = value.toString();
  433. dataList.append(stringValue);
  434. }
  435. }
  436. qDebug()<<"initRightListwidget";
  437. qDebug()<<"dataList"<<dataList;
  438. qDebug()<<"qw"<<qw;
  439. qw->clear();
  440. qw->addItems(dataList);
  441. }
  442. /////////////////////////////////////////////////////////////////////////////////////////////右侧右击事件
  443. void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
  444. {
  445. qDebug()<<"1111111";
  446. QListWidgetItem* curItem = my1->itemAt( pos );
  447. if( curItem == NULL )
  448. return;
  449. QMenu *popMenu = new QMenu( this );
  450. QAction *deleteSeed = new QAction(tr("Delete"), this);
  451. QString qss = "QMenu{color:#E8E8E8;background:#4D4D4D;margin:2px;}\
  452. QMenu::item{padding:3px 20px 3px 20px;}\
  453. QMenu::indicator{width:13px;height:13px;}\
  454. QMenu::item:selected{color:#E8E8E8;border:0px solid #575757;background:#1E90FF;}\
  455. QMenu::separator{height:1px;background:#757575;}";
  456. popMenu->setStyleSheet(qss);
  457. popMenu->addAction( deleteSeed );
  458. this->qlw=my1;
  459. connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) );
  460. popMenu->exec( QCursor::pos() );
  461. delete popMenu;
  462. delete deleteSeed;
  463. }
  464. void MainWindow::on_listWidget_2_customContextMenuRequested(const QPoint &pos)
  465. {
  466. QListWidgetItem* curItem = my2->itemAt( pos );
  467. if( curItem == NULL )
  468. return;
  469. QMenu *popMenu = new QMenu( this );
  470. QAction *deleteSeed = new QAction(tr("Delete"), this);
  471. popMenu->addAction( deleteSeed );
  472. this->qlw=my2;
  473. connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()));
  474. popMenu->exec( QCursor::pos() );
  475. delete popMenu;
  476. delete deleteSeed;
  477. }
  478. /////daozheer 可以删除项 还没有修改json文件
  479. void MainWindow::deleteSeedSlot()
  480. {
  481. qDebug()<<"deleteSeedSlot";
  482. QListWidgetItem * item = this->qlw->currentItem();
  483. qDebug()<<this->qlw;
  484. if( item == NULL )
  485. return;
  486. int curIndex = this->qlw->row(item);
  487. qDebug()<<curIndex;
  488. auto curItem=this->qlw->takeItem(curIndex);
  489. qDebug()<<curItem->text();
  490. // updateConfig(ui->listWidget);//更新对应的config.json文件
  491. QString deal_file_path;
  492. QString group_name;
  493. int group_index;
  494. if(qlw->objectName()=="listWidget")
  495. {
  496. group_name="group1";
  497. group_index=0;
  498. }
  499. else if(qlw->objectName()=="listWidget_2")
  500. {
  501. group_name="group2";
  502. group_index=1;
  503. }
  504. else if(qlw->objectName()=="listWidget_3")
  505. {
  506. group_name="group3";
  507. group_index=2;
  508. }
  509. else
  510. {
  511. group_name="group4";
  512. group_index=3;
  513. }
  514. deal_file_path=treeview_path+"/"+group_name+"/"+curItem->text();
  515. QFile file(deal_file_path);
  516. file.remove();
  517. QJsonObject groupObject = RQJA[group_index].toObject();
  518. QJsonArray childrenArray = groupObject["children"].toArray();
  519. childrenArray.removeAt(curIndex);
  520. groupObject["children"] = childrenArray;
  521. RQJA[group_index] = groupObject;
  522. writeBackToConfigJson();
  523. delete item;
  524. }
  525. void MainWindow::on_listWidget_3_customContextMenuRequested(const QPoint &pos)
  526. {
  527. QListWidgetItem* curItem = my3->itemAt( pos );
  528. if( curItem == NULL )
  529. return;
  530. QMenu *popMenu = new QMenu( this );
  531. QAction *deleteSeed = new QAction(tr("Delete"), this);
  532. popMenu->addAction( deleteSeed );
  533. this->qlw=my3;
  534. connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) );
  535. popMenu->exec( QCursor::pos() );
  536. delete popMenu;
  537. delete deleteSeed;
  538. }
  539. void MainWindow::on_listWidget_4_customContextMenuRequested(const QPoint &pos)
  540. {
  541. QListWidgetItem* curItem = my4->itemAt( pos );
  542. if( curItem == NULL )
  543. return;
  544. QMenu *popMenu = new QMenu( this );
  545. QAction *deleteSeed = new QAction(tr("Delete"), this);
  546. popMenu->addAction( deleteSeed );
  547. this->qlw=my4;
  548. connect( deleteSeed, SIGNAL(triggered() ), this, SLOT( deleteSeedSlot()) );
  549. popMenu->exec( QCursor::pos() );
  550. delete popMenu;
  551. delete deleteSeed;
  552. }
  553. /////////////////////////////////////////////////////////////////////////////////////////////右侧右击事件
  554. //滑块点击slot函数
  555. void MainWindow::onWidgetClicked()
  556. {
  557. QObject *senderObj = sender(); // 获取发出信号的对象指针
  558. if(RQJA.empty())return;
  559. if (senderObj)
  560. {
  561. QWidget *clickedWidget = qobject_cast<QWidget*>(senderObj); // 将指针转换为QWidget指针
  562. if (clickedWidget)
  563. {
  564. QString widgetName = clickedWidget->parentWidget()->objectName(); // 获取控件的对象名称
  565. if(widgetName=="slide_2")
  566. {
  567. if(customB2->dir==Qt::AlignLeft)
  568. {
  569. //变为1
  570. QJsonValueRef selectedValueRef = RQJA[1].toObject()["selected"];
  571. selectedValueRef = "1";
  572. qDebug()<<RQJA;
  573. }
  574. else{
  575. //变为0
  576. QJsonValueRef selectedValueRef = RQJA[1].toObject()["selected"];
  577. selectedValueRef = "0";
  578. qDebug()<<RQJA;
  579. }
  580. }
  581. else if(widgetName=="slide_3")
  582. {
  583. if(customB3->dir==Qt::AlignLeft)
  584. {
  585. //变为1
  586. QJsonValueRef selectedValueRef = RQJA[2].toObject()["selected"];
  587. selectedValueRef = "1";
  588. qDebug()<<RQJA;
  589. }
  590. else{
  591. //变为0
  592. QJsonValueRef selectedValueRef = RQJA[2].toObject()["selected"];
  593. selectedValueRef = "0";
  594. qDebug()<<RQJA;
  595. }
  596. }
  597. else
  598. {
  599. if(customB4->dir==Qt::AlignLeft)
  600. {
  601. //变为1
  602. QJsonValueRef selectedValueRef = RQJA[3].toObject()["selected"];
  603. selectedValueRef = "1";
  604. qDebug()<<RQJA;
  605. }
  606. else{
  607. //变为0
  608. QJsonValueRef selectedValueRef = RQJA[3].toObject()["selected"];
  609. selectedValueRef = "0";
  610. qDebug()<<RQJA;
  611. }
  612. }
  613. writeBackToConfigJson();
  614. }
  615. }
  616. }
  617. //修改config文件
  618. void MainWindow::writeBackToConfigJson(){
  619. if(!config_json_path.endsWith("json"))return;
  620. qDebug()<<config_json_path;
  621. QFile jsonFile(config_json_path);
  622. if (jsonFile.open(QIODevice::WriteOnly)) {
  623. QJsonDocument jsonDoc(RQJA);
  624. QByteArray jsonData = jsonDoc.toJson();
  625. qint64 bytesWritten = jsonFile.write(jsonData);
  626. if (bytesWritten == -1) {
  627. qDebug() << "Failed to write data to file.";
  628. } else {
  629. qDebug() << "Data written to file successfully.";
  630. }
  631. jsonFile.close();
  632. } else {
  633. qDebug() << "Failed to open file for writing.";
  634. }
  635. }
  636. //重新选择项目进行初始化
  637. void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
  638. {
  639. clearRight();
  640. QString folder = "/users/lal/documents/RedInterstitialData/HotUpdate2";
  641. QString currentPro = ui->comboBox->currentText();
  642. //初始化TreeView
  643. QString jsonPath = folder+"/"+currentPro+"/"+"Hotupdate.json";
  644. QJO=tool::readJsonFile(jsonPath);
  645. // 从JSON对象中提取数据
  646. QJA = QJO.value("children").toArray();
  647. ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
  648. connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::slotTreeMenu);
  649. //初始化treeview 和 combobox
  650. QTreeView *treeView=ui->treeView;
  651. QStandardItemModel* model=new QStandardItemModel(treeView);
  652. model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("名称"));
  653. treeView->setModel(model);
  654. initTreeView(model->invisibleRootItem(),QJA);
  655. qDebug()<<"QJA"<<QJA;
  656. }
  657. void MainWindow::clearRight()
  658. {
  659. my1->clear();
  660. my2->clear();
  661. my3->clear();
  662. my4->clear();
  663. this->customB2->pos_left_button();
  664. this->customB3->pos_left_button();
  665. this->customB4->pos_left_button();
  666. }
  667. void MainWindow::handleOperationCompleted(QString fromfilePath)
  668. {
  669. QStringList parts = fromfilePath.split("/");
  670. QObject *senderObj = sender();
  671. QListWidget *LW = qobject_cast<QListWidget*>(senderObj);
  672. //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";
  673. QString group_name;
  674. int group_index;
  675. if(LW->objectName()=="listWidget")
  676. {
  677. group_name="group1";
  678. group_index=0;
  679. }
  680. else if(LW->objectName()=="listWidget_2")
  681. {
  682. group_name="group2";
  683. group_index=1;
  684. }
  685. else if(LW->objectName()=="listWidget_3")
  686. {
  687. group_name="group3";
  688. group_index=2;
  689. }
  690. else
  691. {
  692. group_name="group4";
  693. group_index=3;
  694. }
  695. QString tofilePath1 = treeview_path+"/"+group_name;
  696. QFile sourceFile(fromfilePath);
  697. QFile targetFile(tofilePath1 + "/" +parts[parts.size()-1]);
  698. if (sourceFile.exists() && QDir(tofilePath1).exists()) {
  699. if (sourceFile.copy(targetFile.fileName())) {
  700. qDebug() << "File copied successfully.";
  701. LW->addItem(parts[parts.size()-1]);
  702. qDebug()<<this->parent();
  703. QJsonObject groupObject = RQJA[group_index].toObject();
  704. QJsonArray childrenArray = groupObject["children"].toArray();
  705. childrenArray.append(parts[parts.size()-1]);
  706. groupObject["children"] = childrenArray;
  707. RQJA[group_index] = groupObject;
  708. writeBackToConfigJson();
  709. } else {
  710. qDebug() << "Failed to copy file.";
  711. QMessageBox::information(this, "失败", "文件名重复");
  712. }
  713. } else {
  714. qDebug() << "Source file or target folder does not exist.";
  715. }
  716. }
  717. void MainWindow::on_pushButton_4_clicked()
  718. {
  719. QList<QStandardItem*> items1;
  720. QStandardItemModel *model1 = static_cast<QStandardItemModel*>(ui->treeView->model());
  721. // QStandardItem* item2 = new QStandardItem(QStringLiteral("新建测试"));
  722. QDateTime currentDateTime = QDateTime::currentDateTime();
  723. QString currentDateTimeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
  724. QStandardItem* item2 = new QStandardItem(currentDateTimeString);
  725. QString add_Path=root_pro_path+"/"+ui->comboBox->currentText();
  726. QDir dir;
  727. if (!dir.exists(add_Path+"/"+currentDateTimeString)) {
  728. if (dir.mkpath(add_Path+"/"+currentDateTimeString)) {
  729. qDebug() << "Folder created successfully.";
  730. QString filePath = add_Path+"/"+"Hotupdate.json";
  731. QFile file(filePath);
  732. if (!file.open(QIODevice::ReadOnly))
  733. {
  734. QString errorString = file.errorString();
  735. qDebug() << "Failed to open file: " << errorString;
  736. // 其他错误处理逻辑
  737. }
  738. QByteArray jsonData = file.readAll();
  739. file.close();
  740. QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
  741. if (jsonDoc.isNull()) {
  742. qDebug() << "Failed to parse JSON data.";
  743. return;
  744. }
  745. // 将 JSON 数据转换为对象
  746. QJsonObject jsonObj = jsonDoc.object();
  747. // 使用 jsonObj 进行进一步操作,例如读取特定的键值对
  748. QJsonObject newQ;
  749. QJsonArray newA;
  750. newQ.insert("children",newA);
  751. newQ.insert("name",currentDateTimeString);
  752. QJsonArray qaa=jsonObj["children"].toArray();
  753. qaa.append(newQ);
  754. jsonObj["children"]=qaa;
  755. tool::writeJsonFile(filePath,jsonObj);
  756. items1.append(item2);
  757. model1->appendRow(items1);
  758. } else {
  759. qDebug() << "Failed to create folder.";
  760. }
  761. } else {
  762. qDebug() << "Folder already exists.";
  763. }
  764. }
  765. void MainWindow::on_pushButton_5_clicked()
  766. {
  767. // 获取选择模型
  768. QItemSelectionModel* selectionModel = ui->treeView->selectionModel();
  769. if (selectionModel)
  770. {
  771. // 获取当前选中的节点的索引
  772. QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
  773. QModelIndex parentIndex = selectedIndexes.first().parent();
  774. if(parentIndex.isValid())
  775. {
  776. QMessageBox::information(this, "提示", "请选中一级节点");
  777. return;
  778. }
  779. // 遍历选中的索引列表
  780. foreach (const QModelIndex& index, selectedIndexes)
  781. {
  782. // 打印选中节点的数据
  783. QVariant data = index.data(Qt::DisplayRole);
  784. qDebug() << "Selected Node Data: " << data.toString();
  785. QString add_Path=root_pro_path+"/"+ui->comboBox->currentText()+"/"+data.toString();
  786. QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json";
  787. //直接对新增的版本添加config.json group1 group2 group3 group4
  788. qDebug()<<add_Path;
  789. QDir folder(add_Path);
  790. QStringList subFolders = folder.entryList(QDir::Dirs | QDir::NoDotAndDotDot); // 获取文件夹列表
  791. QString folderCount = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); // 文件夹数目
  792. qDebug() << "Number of sub-folders: " << folderCount;
  793. //新建版本
  794. QDir dir;
  795. QString version_path=add_Path+"/"+"version"+folderCount;
  796. dir.mkpath(version_path);
  797. //新建版本下的子文件夹等
  798. dir.mkpath(version_path+"/"+"group1");
  799. dir.mkpath(version_path+"/"+"group2");
  800. dir.mkpath(version_path+"/"+"group3");
  801. dir.mkpath(version_path+"/"+"group4");
  802. QString filePath = version_path+"/"+"config.json"; // 指定新文件的路径和名称
  803. QJsonArray jsonArray;
  804. for(int i=0;i<4;i++)
  805. {
  806. // add obj
  807. QJsonObject object1;
  808. QJsonArray children1;
  809. object1["children"] = children1;
  810. object1["selected"] = "0";
  811. jsonArray.append(object1);
  812. }
  813. QFile file(filePath);
  814. if (file.open(QIODevice::WriteOnly | QIODevice::Text))
  815. {
  816. QJsonDocument jsonDocument(jsonArray);
  817. QString jsonString = jsonDocument.toJson();
  818. QTextStream stream(&file);
  819. stream << jsonString; // 将JSON文本写入文件
  820. file.close();
  821. qDebug() << "File written successfully!";
  822. }
  823. else
  824. {
  825. qDebug() << "Failed to write the file!";
  826. }
  827. //修改项目根json文件 modify_pro_config
  828. //folderCount
  829. qDebug()<<index.row();
  830. QFile file1(modify_pro_config);
  831. if (!file1.open(QIODevice::ReadOnly))
  832. {
  833. QString errorString = file1.errorString();
  834. qDebug() << "Failed to open file: " << errorString;
  835. // 其他错误处理逻辑
  836. }
  837. QByteArray jsonData = file1.readAll();
  838. QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
  839. if (jsonDoc.isNull()) {
  840. qDebug() << "Failed to parse JSON data.";
  841. return;
  842. }
  843. // 将 JSON 数据转换为对象
  844. QJsonObject jsonObj = jsonDoc.object();
  845. QJsonArray parentArray = jsonObj["children"].toArray();
  846. QJsonObject childObject = parentArray[index.row()].toObject();
  847. QJsonArray nestedArray = childObject["children"].toArray();
  848. // {
  849. // "name": "version1"
  850. // }
  851. QJsonObject temo1;
  852. temo1["name"]="version" + folderCount;
  853. nestedArray.append(temo1);
  854. childObject["children"] = nestedArray;
  855. parentArray[index.row()] = childObject;
  856. jsonObj["children"] = parentArray;
  857. qDebug() << "Modified QJsonObject:" << jsonObj;
  858. tool::writeJsonFile(modify_pro_config,jsonObj);
  859. //
  860. QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
  861. if (model) {
  862. QStandardItem* parentItem = model->itemFromIndex(index);
  863. if (parentItem) {
  864. QStandardItem* childItem = new QStandardItem("version"+folderCount);
  865. parentItem->appendRow(childItem);
  866. }
  867. }
  868. }
  869. }
  870. }
  871. void MainWindow::on_comboBox_customContextMenuRequested(const QPoint &pos)
  872. {
  873. }
  874. void MainWindow::on_pushButton_9_clicked()
  875. {
  876. // qDebug()<<ui->treeView->currentIndex();
  877. // 获取被选中的节点
  878. QModelIndex selectedIndex = ui->treeView->currentIndex();
  879. if(!selectedIndex.isValid())
  880. {
  881. // 如果没有选中任何节点,则弹出提示框
  882. QMessageBox::information(this, "提示", "没有选中任何节点");
  883. return;
  884. }
  885. // 检查选中的节点是一级节点还是二级节点
  886. if(selectedIndex.parent().isValid())
  887. {
  888. // 如果选中的节点有父节点,那么它是二级节点
  889. // 在这里执行二级节点对应的代码...
  890. QMessageBox::StandardButton reply;
  891. reply = QMessageBox::question(this, "Test", "确定要删除"+selectedIndex.data().toString(),
  892. QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
  893. if (reply == QMessageBox::Yes) {
  894. qDebug() << "Yes was clicked";
  895. qDebug() << "选中的是二级节点";
  896. QModelIndex parentIndex = selectedIndex.parent();
  897. QString del_Path=root_pro_path+"/"+ui->comboBox->currentText()+"/"+parentIndex.data().toString()+"/"+selectedIndex.data().toString();
  898. QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json";
  899. //删除文件夹
  900. QDir dir(del_Path);
  901. dir.removeRecursively();
  902. //修改json文件
  903. QFile file1(modify_pro_config);
  904. if (!file1.open(QIODevice::ReadOnly))
  905. {
  906. QString errorString = file1.errorString();
  907. qDebug() << "Failed to open file: " << errorString;
  908. // 其他错误处理逻辑
  909. }
  910. QByteArray jsonData = file1.readAll();
  911. QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
  912. if (jsonDoc.isNull()) {
  913. qDebug() << "Failed to parse JSON data.";
  914. return;
  915. }
  916. // 将 JSON 数据转换为对象
  917. QJsonObject jsonObj = jsonDoc.object();
  918. QJsonArray parentArray = jsonObj["children"].toArray();
  919. QJsonObject childObject = parentArray[parentIndex.row()].toObject();
  920. QJsonArray nestedArray = childObject["children"].toArray();
  921. nestedArray.removeAt(selectedIndex.row());
  922. childObject["children"] = nestedArray;
  923. parentArray[parentIndex.row()] = childObject;
  924. jsonObj["children"] = parentArray;
  925. qDebug() << "Modified QJsonObject:" << jsonObj;
  926. tool::writeJsonFile(modify_pro_config,jsonObj);
  927. QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
  928. if (model) {
  929. QStandardItem* parentItem = model->itemFromIndex(parentIndex);
  930. if (parentItem) {
  931. parentItem->removeRow(selectedIndex.row());
  932. }
  933. }
  934. } else if (reply == QMessageBox::No) {
  935. qDebug() << "No was clicked";
  936. } else {
  937. qDebug() << "Cancel was clicked";
  938. }
  939. }
  940. else
  941. {
  942. // 如果选中的节点没有父节点,那么它是一级节点
  943. // 在这里执行一级节点对应的代码...
  944. QMessageBox::StandardButton reply;
  945. reply = QMessageBox::question(this, "Test", "确定要删除"+selectedIndex.data().toString(),
  946. QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
  947. if (reply == QMessageBox::Yes) {
  948. qDebug() << "Yes was clicked";
  949. qDebug() << "选中的是一级节点";
  950. QString del_Path=root_pro_path+"/"+ui->comboBox->currentText()+"/"+selectedIndex.data().toString();
  951. QString modify_pro_config = root_pro_path+"/"+ui->comboBox->currentText()+"/"+"Hotupdate.json";
  952. //删除文件夹
  953. QDir dir(del_Path);
  954. dir.removeRecursively();
  955. //修改json文件
  956. QFile file1(modify_pro_config);
  957. if (!file1.open(QIODevice::ReadOnly))
  958. {
  959. QString errorString = file1.errorString();
  960. qDebug() << "Failed to open file: " << errorString;
  961. // 其他错误处理逻辑
  962. }
  963. QByteArray jsonData = file1.readAll();
  964. QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
  965. if (jsonDoc.isNull()) {
  966. qDebug() << "Failed to parse JSON data.";
  967. return;
  968. }
  969. // 将 JSON 数据转换为对象
  970. QJsonObject jsonObj = jsonDoc.object();
  971. QJsonArray parentArray = jsonObj["children"].toArray();
  972. parentArray.removeAt(selectedIndex.row());
  973. jsonObj["children"] = parentArray;
  974. qDebug() << "Modified QJsonObject:" << jsonObj;
  975. tool::writeJsonFile(modify_pro_config,jsonObj);
  976. QStandardItemModel* model = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
  977. model->removeRow(selectedIndex.row());
  978. } else if (reply == QMessageBox::No) {
  979. qDebug() << "No was clicked";
  980. } else {
  981. qDebug() << "Cancel was clicked";
  982. }
  983. }
  984. }