-
Notifications
You must be signed in to change notification settings - Fork 3
/
RibbonCustomizeWidget.cpp
1381 lines (1141 loc) · 49.5 KB
/
RibbonCustomizeWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by weiwu on 21-5-2.
//
#include "RibbonCustomizeWidget.h"
#include "RibbonCustomizeData.h"
bool sa_customize_datas_to_xml(QXmlStreamWriter *xml, const QList<RibbonCustomizeData> &cds) {
if (cds.size() <= 0) {
return (false);
}
xml->writeStartElement("sa-ribbon-customize");
for (const RibbonCustomizeData &d : cds) {
xml->writeStartElement("customize-data");
xml->writeAttribute("type", QString::number(d.actionType()));
xml->writeAttribute("index", QString::number(d.indexValue));
xml->writeAttribute("key", d.keyValue);
xml->writeAttribute("category", d.categoryObjNameValue);
xml->writeAttribute("pannel", d.pannelObjNameValue);
xml->writeAttribute("row-prop", QString::number(d.actionRowProportionValue));
xml->writeEndElement();
}
xml->writeEndElement();
if (xml->hasError()) {
qWarning() << "write has error";
}
return (true);
}
QList<RibbonCustomizeData> sa_customize_datas_from_xml(QXmlStreamReader *xml, RibbonActionsManager *mgr) {
//先找到"sa-ribbon-customize"
while (!xml->atEnd()) {
qDebug() << "name:" << xml->name() << " qualifiedName:" << xml->qualifiedName();
if (xml->isStartElement() && (xml->name() == "sa-ribbon-customize")) {
break;
}
xml->readNext();
}
QList<RibbonCustomizeData> res;
//开始遍历"customize-data"
while (!xml->atEnd()) {
if (xml->isStartElement() && (xml->name() == "customize-data")) {
//首先读取属性type
RibbonCustomizeData d;
QXmlStreamAttributes attrs = xml->attributes();
if (!attrs.hasAttribute("type")) {
//说明异常,跳过这个
xml->readNextStartElement();
continue;
}
bool isOk = false;
int v = xml->attributes().value("type").toInt(&isOk);
if (!isOk) {
//说明异常,跳过这个
xml->readNextStartElement();
continue;
}
d.setActionType(static_cast<RibbonCustomizeData::ActionType>(v));
//开始读取子对象
if (attrs.hasAttribute("index")) {
v = xml->attributes().value("index").toInt(&isOk);
if (isOk) {
d.indexValue = v;
}
}
if (attrs.hasAttribute("key")) {
d.keyValue = attrs.value("key").toString();
}
if (attrs.hasAttribute("category")) {
d.categoryObjNameValue = attrs.value("category").toString();
}
if (attrs.hasAttribute("pannel")) {
d.pannelObjNameValue = attrs.value("pannel").toString();
}
if (attrs.hasAttribute("row-prop")) {
v = xml->attributes().value("row-prop").toInt(&isOk);
if (isOk) {
d.actionRowProportionValue = static_cast<RibbonPannelItem::RowProportion>(v);
}
}
d.setActionsManager(mgr);
res.append(d);
}
xml->readNext();
}
if (xml->hasError()) {
qWarning() << xml->errorString();
}
return (res);
}
int sa_customize_datas_apply(const QList<RibbonCustomizeData> &cds, RibbonMainWindow *w) {
int c = 0;
for (const RibbonCustomizeData &d : cds) {
if (d.apply(w)) {
++c;
}
}
return (c);
}
bool sa_apply_customize_from_xml_file(const QString &filePath, RibbonMainWindow *w, RibbonActionsManager *mgr) {
QFile f(filePath);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
return (false);
}
f.seek(0);
QXmlStreamReader xml(&f);
return (RibbonCustomizeWidget::fromXml(&xml, w, mgr));
}
/**
* @brief 构建RibbonCustomizeWidget的Ui
*/
class RibbonCustomizeWidgetUi {
public:
QHBoxLayout *horizontalLayoutMain;
QVBoxLayout *verticalLayoutSelect;
QLabel *labelSelectAction;
QHBoxLayout *horizontalLayoutSearch;
QComboBox *comboBoxActionIndex;
QLineEdit *lineEditSearchAction;
QListView *listViewSelect;
QVBoxLayout *verticalLayoutMidButtons;
QSpacerItem *verticalSpacerUp;
QPushButton *pushButtonAdd;
QPushButton *pushButtonDelete;
QSpacerItem *verticalSpacerDown;
QLabel *labelProportion;
QComboBox *comboBoxActionProportion;
QVBoxLayout *verticalLayoutResult;
QLabel *labelCustomize;
QHBoxLayout *horizontalLayoutCategorySelect;
QRadioButton *radioButtonMainCategory;
QRadioButton *radioButtonAllCategory;
QButtonGroup *radioButtonGroup;
QTreeView *treeViewResult;
QHBoxLayout *horizontalLayoutActionOptBtns;
QPushButton *pushButtonNewCategory;
QPushButton *pushButtonNewPannel;
QPushButton *pushButtonRename;
QVBoxLayout *verticalLayoutRightButtons;
QSpacerItem *verticalSpacerUp2;
QToolButton *toolButtonUp;
QToolButton *toolButtonDown;
QSpacerItem *verticalSpacerDown2;
void setupUi(QWidget *customizeWidget) {
if (customizeWidget->objectName().isEmpty()) {
customizeWidget->setObjectName(QStringLiteral("RibbonCustomizeWidget"));
}
customizeWidget->resize(800, 600);
horizontalLayoutMain = new QHBoxLayout(customizeWidget);
horizontalLayoutMain->setObjectName(QStringLiteral("horizontalLayoutMain"));
verticalLayoutSelect = new QVBoxLayout();
verticalLayoutSelect->setObjectName(QStringLiteral("verticalLayoutSelect"));
labelSelectAction = new QLabel(customizeWidget);
labelSelectAction->setObjectName(QStringLiteral("labelSelectAction"));
verticalLayoutSelect->addWidget(labelSelectAction);
horizontalLayoutSearch = new QHBoxLayout();
horizontalLayoutSearch->setObjectName(QStringLiteral("horizontalLayoutSearch"));
comboBoxActionIndex = new QComboBox(customizeWidget);
comboBoxActionIndex->setObjectName(QStringLiteral("comboBoxActionIndex"));
comboBoxActionIndex->setEditable(false);
horizontalLayoutSearch->addWidget(comboBoxActionIndex);
lineEditSearchAction = new QLineEdit(customizeWidget);
lineEditSearchAction->setObjectName(QStringLiteral("lineEditSearchAction"));
horizontalLayoutSearch->addWidget(lineEditSearchAction);
verticalLayoutSelect->addLayout(horizontalLayoutSearch);
listViewSelect = new QListView(customizeWidget);
listViewSelect->setObjectName(QStringLiteral("listViewSelect"));
verticalLayoutSelect->addWidget(listViewSelect);
horizontalLayoutMain->addLayout(verticalLayoutSelect);
verticalLayoutMidButtons = new QVBoxLayout();
verticalLayoutMidButtons->setObjectName(QStringLiteral("verticalLayoutMidButtons"));
verticalSpacerUp = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
verticalLayoutMidButtons->addItem(verticalSpacerUp);
pushButtonAdd = new QPushButton(customizeWidget);
pushButtonAdd->setObjectName(QStringLiteral("pushButtonAdd"));
pushButtonAdd->setEnabled(false);
verticalLayoutMidButtons->addWidget(pushButtonAdd);
pushButtonDelete = new QPushButton(customizeWidget);
pushButtonDelete->setObjectName(QStringLiteral("pushButtonDelete"));
pushButtonDelete->setEnabled(false);
verticalLayoutMidButtons->addWidget(pushButtonDelete);
verticalSpacerDown = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
verticalLayoutMidButtons->addItem(verticalSpacerDown);
labelProportion = new QLabel(customizeWidget);
labelProportion->setObjectName(QStringLiteral("labelProportion"));
verticalLayoutMidButtons->addWidget(labelProportion);
comboBoxActionProportion = new QComboBox(customizeWidget);
comboBoxActionProportion->setObjectName(QStringLiteral("comboBoxActionProportion"));
comboBoxActionProportion->setEditable(false);
verticalLayoutMidButtons->addWidget(comboBoxActionProportion);
horizontalLayoutMain->addLayout(verticalLayoutMidButtons);
verticalLayoutResult = new QVBoxLayout();
verticalLayoutResult->setObjectName(QStringLiteral("verticalLayoutResult"));
labelCustomize = new QLabel(customizeWidget);
labelCustomize->setObjectName(QStringLiteral("labelCustomize"));
verticalLayoutResult->addWidget(labelCustomize);
horizontalLayoutCategorySelect = new QHBoxLayout();
horizontalLayoutCategorySelect->setObjectName(QStringLiteral("horizontalLayoutCategorySelect"));
radioButtonMainCategory = new QRadioButton(customizeWidget);
radioButtonMainCategory->setObjectName(QStringLiteral("radioButtonMainCategory"));
radioButtonMainCategory->setChecked(false);
horizontalLayoutCategorySelect->addWidget(radioButtonMainCategory);
radioButtonAllCategory = new QRadioButton(customizeWidget);
radioButtonAllCategory->setObjectName(QStringLiteral("radioButtonAllCategory"));
radioButtonAllCategory->setChecked(true);
horizontalLayoutCategorySelect->addWidget(radioButtonAllCategory);
radioButtonGroup = new QButtonGroup(customizeWidget);
radioButtonGroup->addButton(radioButtonMainCategory);
radioButtonGroup->addButton(radioButtonAllCategory);
verticalLayoutResult->addLayout(horizontalLayoutCategorySelect);
treeViewResult = new QTreeView(customizeWidget);
treeViewResult->setObjectName(QStringLiteral("treeViewResult"));
treeViewResult->setHeaderHidden(true);
treeViewResult->setSelectionMode(QAbstractItemView::SingleSelection);
treeViewResult->setAnimated(true); //支持动画
treeViewResult->setEditTriggers(QAbstractItemView::NoEditTriggers); //不允许直接在item上重命名
verticalLayoutResult->addWidget(treeViewResult);
horizontalLayoutActionOptBtns = new QHBoxLayout();
horizontalLayoutActionOptBtns->setObjectName(QStringLiteral("horizontalLayoutActionOptBtns"));
pushButtonNewCategory = new QPushButton(customizeWidget);
pushButtonNewCategory->setObjectName(QStringLiteral("pushButtonNewCategory"));
horizontalLayoutActionOptBtns->addWidget(pushButtonNewCategory);
pushButtonNewPannel = new QPushButton(customizeWidget);
pushButtonNewPannel->setObjectName(QStringLiteral("pushButtonNewPannel"));
horizontalLayoutActionOptBtns->addWidget(pushButtonNewPannel);
pushButtonRename = new QPushButton(customizeWidget);
pushButtonRename->setObjectName(QStringLiteral("pushButtonRename"));
horizontalLayoutActionOptBtns->addWidget(pushButtonRename);
verticalLayoutResult->addLayout(horizontalLayoutActionOptBtns);
horizontalLayoutMain->addLayout(verticalLayoutResult);
verticalLayoutRightButtons = new QVBoxLayout();
verticalLayoutRightButtons->setObjectName(QStringLiteral("verticalLayoutRightButtons"));
verticalSpacerUp2 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
verticalLayoutRightButtons->addItem(verticalSpacerUp2);
toolButtonUp = new QToolButton(customizeWidget);
toolButtonUp->setObjectName(QStringLiteral("pushButtonUp"));
toolButtonUp->setArrowType(Qt::UpArrow);
toolButtonUp->setAutoRaise(true);
verticalLayoutRightButtons->addWidget(toolButtonUp);
toolButtonDown = new QToolButton(customizeWidget);
toolButtonDown->setObjectName(QStringLiteral("pushButtonDown"));
toolButtonDown->setArrowType(Qt::DownArrow);
toolButtonDown->setAutoRaise(true);
verticalLayoutRightButtons->addWidget(toolButtonDown);
verticalSpacerDown2 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
verticalLayoutRightButtons->addItem(verticalSpacerDown2);
horizontalLayoutMain->addLayout(verticalLayoutRightButtons);
retranslateUi(customizeWidget);
} // setupUi
void retranslateUi(QWidget *customizeWidget) {
customizeWidget->setWindowTitle(
QApplication::translate("RibbonCustomizeWidget", "Customize Widget", Q_NULLPTR));
labelSelectAction->setText(QApplication::translate("RibbonCustomizeWidget",
"\344\273\216\344\270\213\345\210\227\351\200\211\351\241\271\345\215\241\351\200\211\346\213\251\345\221\275\344\273\244\357\274\232",
Q_NULLPTR));
lineEditSearchAction->setInputMask(QString());
lineEditSearchAction->setText(QString());
lineEditSearchAction->setPlaceholderText(
QApplication::translate("RibbonCustomizeWidget", "\346\237\245\346\211\276\345\221\275\344\273\244",
Q_NULLPTR));
pushButtonAdd->setText(
QApplication::translate("RibbonCustomizeWidget", "\346\267\273\345\212\240 >>", Q_NULLPTR));
pushButtonDelete->setText(
QApplication::translate("RibbonCustomizeWidget", "<< \345\210\240\351\231\244", Q_NULLPTR));
labelCustomize->setText(QApplication::translate("RibbonCustomizeWidget",
"\350\207\252\345\256\232\344\271\211\345\212\237\350\203\275\345\214\272\357\274\232",
Q_NULLPTR));
radioButtonMainCategory->setText(
QApplication::translate("RibbonCustomizeWidget", "\344\270\273\351\200\211\351\241\271\345\215\241",
Q_NULLPTR));
radioButtonAllCategory->setText(QApplication::translate("RibbonCustomizeWidget",
"\346\211\200\346\234\211\351\200\211\351\241\271\345\215\241",
Q_NULLPTR));
pushButtonNewCategory->setText(QApplication::translate("RibbonCustomizeWidget",
"\346\226\260\345\273\272\351\200\211\351\241\271\345\215\241",
Q_NULLPTR));
pushButtonNewPannel->setText(
QApplication::translate("RibbonCustomizeWidget", "\346\226\260\345\273\272\347\273\204", Q_NULLPTR));
pushButtonRename->setText(
QApplication::translate("RibbonCustomizeWidget", "\351\207\215\345\221\275\345\220\215", Q_NULLPTR));
labelProportion->setText(QApplication::translate("RibbonCustomizeWidget", "proportion:", Q_NULLPTR));
} // retranslateUi
};
/**
* @brief 管理RibbonCustomizeWidget的业务逻辑
*/
class RibbonCustomizeWidgetPrivate {
public:
RibbonCustomizeWidget *mParent;
RibbonCustomizeWidget::RibbonTreeShowType mShowType; ///< 显示类型
RibbonMainWindow *mRibbonWindow; ///< 保存RibbonMainWindow的指针
RibbonActionsManager *mActionMgr; ///< action管理器
RibbonActionsManagerModel *mAcionModel; ///< action管理器对应的model
QStandardItemModel *mRibbonModel; ///< 用于很成ribbon的树
int mCustomizeCategoryCount; ///< 记录自定义Category的个数
int mCustomizePannelCount; ///< 记录自定义Pannel的个数
RibbonCustomizeWidgetPrivate(RibbonCustomizeWidget *p);
void updateModel();
QList<RibbonCustomizeData> mCustomizeDatas; ///< 记录所有的自定义动作
QList<RibbonCustomizeData> mOldCustomizeDatas; ///< 记录旧的自定义动作
//创建一个随机id,形如:pre_QDateTime::currentMSecsSinceEpoch_suf
static QString makeRandomObjName(const QString &pre);
int itemLevel(QStandardItem *item) const;
//
bool isCustomizeItem(QStandardItem *item) const;
//把item转换为category
RibbonCategory *itemToCategory(QStandardItem *item) const;
//把item转换为RibbonPannel
RibbonPannel *itemToPannel(QStandardItem *item) const;
//获取item对应的object name
QString itemObjectName(QStandardItem *item) const;
//判断是否可以自定义
bool isItemCanCustomize(QStandardItem *item) const;
//从item转为action
QAction *itemToAction(QStandardItem *item) const;
};
RibbonCustomizeWidgetPrivate::RibbonCustomizeWidgetPrivate(RibbonCustomizeWidget *p)
: mParent(p), mShowType(RibbonCustomizeWidget::ShowAllCategory), mRibbonWindow(nullptr), mActionMgr(nullptr),
mAcionModel(new RibbonActionsManagerModel(p)), mRibbonModel(new QStandardItemModel(p)),
mCustomizeCategoryCount(0), mCustomizePannelCount(0) {
}
void RibbonCustomizeWidgetPrivate::updateModel() {
if (mRibbonWindow == nullptr) {
return;
}
mRibbonModel->clear();
RibbonBar *ribbonbar = mRibbonWindow->ribbonBar();
QList<RibbonCategory *> categorys = ribbonbar->categoryPages();
for (RibbonCategory *c : categorys) {
if ((mShowType == RibbonCustomizeWidget::ShowMainCategory) && c->isContextCategory()) {
//如果是只显示主内容,如果是上下文标签就忽略
continue;
}
QStandardItem *ci = new QStandardItem();
if (c->isContextCategory()) {
ci->setText(QString("[%1]").arg(c->windowTitle()));
} else {
ci->setText(c->windowTitle());
}
if (c->isCanCustomize() && !c->isContextCategory()) {
//上下文标签不做显示隐藏处理
ci->setCheckable(true);
ci->setCheckState(ribbonbar->isCategoryVisible(c) ? Qt::Checked : Qt::Unchecked);
ci->setData(true, RibbonCustomizeWidget::CanCustomizeRole);//标记这个是可以自定义的
}
ci->setData(0, RibbonCustomizeWidget::LevelRole);
ci->setData(QVariant::fromValue<qintptr>(qintptr(c)), RibbonCustomizeWidget::PointerRole);
QList<RibbonPannel *> pannels = c->pannelList();
for (RibbonPannel *p : pannels) {
QStandardItem *pi = new QStandardItem(p->windowTitle());
pi->setData(1, RibbonCustomizeWidget::LevelRole);
pi->setData(QVariant::fromValue<qintptr>(qintptr(p)), RibbonCustomizeWidget::PointerRole);
if (p->isCanCustomize()) {
pi->setData(true, RibbonCustomizeWidget::CanCustomizeRole);//标记这个是可以自定义的
}
ci->appendRow(pi);
const QList<RibbonPannelItem *> &items = p->ribbonPannelItem();
for (RibbonPannelItem *i : items) {
if (i->action->isSeparator()) {
continue;
}
QStandardItem *ii = new QStandardItem();
if (i->customWidget) {
//如果是自定义窗口
if (i->widget()->windowTitle().isEmpty() && i->widget()->windowIcon().isNull()) {
delete ii;
continue;//如果窗口啥也没有,就跳过
}
ii->setText(i->widget()->windowTitle());
ii->setIcon(i->widget()->windowIcon());
if (RibbonCustomizeData::isCanCustomize(i->widget())) {
ii->setData(true, RibbonCustomizeWidget::CanCustomizeRole);//标记这个是可以自定义的
}
} else {
ii->setText(i->action->text());
ii->setIcon(i->action->icon());
if (RibbonCustomizeData::isCanCustomize(i->action)) {
ii->setData(true, RibbonCustomizeWidget::CanCustomizeRole);//标记这个是可以自定义的
}
}
ii->setData(2, RibbonCustomizeWidget::LevelRole);
ii->setData(QVariant::fromValue<qintptr>(qintptr(i)), RibbonCustomizeWidget::PointerRole);
pi->appendRow(ii);
}
}
mRibbonModel->appendRow(ci);
}
}
/**
* @brief 创建一个随机id,形如:pre_QDateTime::currentMSecsSinceEpoch
* @param pre 前缀
* @return
*/
QString RibbonCustomizeWidgetPrivate::makeRandomObjName(const QString &pre) {
return (QString("%1_%2").arg(pre).arg(QDateTime::currentMSecsSinceEpoch()));
}
/**
* @brief 获取item的level
* @param item
* @return
*/
int RibbonCustomizeWidgetPrivate::itemLevel(QStandardItem *item) const {
return (item->data(RibbonCustomizeWidget::LevelRole).toInt());
}
/**
* @brief 判断itemn为自定义的item,自定义的item都带有CustomizeRole角色
* @param item
* @return
*/
bool RibbonCustomizeWidgetPrivate::isCustomizeItem(QStandardItem *item) const {
if (nullptr == item) {
return (false);
}
return (item->data(RibbonCustomizeWidget::CustomizeRole).isValid());
}
/**
* @brief 把item转换为category
* @param item
* @return无法转换返回nullptr
*/
RibbonCategory *RibbonCustomizeWidgetPrivate::itemToCategory(QStandardItem *item) const {
int level = item->data(RibbonCustomizeWidget::LevelRole).toInt();
if (level != 0) {
return (nullptr);
}
qintptr p = item->data(RibbonCustomizeWidget::PointerRole).value<qintptr>();
return (reinterpret_cast<RibbonCategory *>(p));
}
/**
* @brief 把item转换为pannel
* @param item
* @return 无法转换返回nullptr
*/
RibbonPannel *RibbonCustomizeWidgetPrivate::itemToPannel(QStandardItem *item) const {
int level = item->data(RibbonCustomizeWidget::LevelRole).toInt();
if (level != 1) {
return (nullptr);
}
qintptr p = item->data(RibbonCustomizeWidget::PointerRole).value<qintptr>();
return (reinterpret_cast<RibbonPannel *>(p));
}
/**
* @brief 获取item对应的objectname
* @param item
* @return 如果无法获取,返回一个空的QString
*/
QString RibbonCustomizeWidgetPrivate::itemObjectName(QStandardItem *item) const {
QString objName;
if (isCustomizeItem(item)) {
//说明是自定义的
objName = item->data(RibbonCustomizeWidget::CustomizeObjNameRole).toString();
} else {
//说明这个是非自定义的
int level = itemLevel(item);
if (0 == level) {
RibbonCategory *category = itemToCategory(item);
if (category) {
objName = category->objectName();
}
} else if (1 == level) {
RibbonPannel *pannel = itemToPannel(item);
if (pannel) {
objName = pannel->objectName();
}
}
}
return (objName);
}
/**
* @brief 判断item是否可自定义
* @param item
* @return
*/
bool RibbonCustomizeWidgetPrivate::isItemCanCustomize(QStandardItem *item) const {
if (nullptr == item) {
return (false);
}
QVariant v = item->data(RibbonCustomizeWidget::CanCustomizeRole);
if (v.isValid()) {
return (v.toBool());
}
return (false);
}
/**
* @brief 从item 转为action
* @param item
* @return
*/
QAction *RibbonCustomizeWidgetPrivate::itemToAction(QStandardItem *item) const {
if (2 != itemLevel(item)) {
return (nullptr);
}
//这里要非常注意,RibbonCustomizeWidget::CustomizeRole为true时,说明这个是自定义的内容,
//这时PointerRole里存放的是action指针,不是RibbonPannelItem
QAction *act = nullptr;
if (item->data(RibbonCustomizeWidget::CustomizeRole).toBool()) {
act = reinterpret_cast<QAction *>(item->data(RibbonCustomizeWidget::PointerRole).value<qintptr>());
} else {
RibbonPannelItem *pi = reinterpret_cast<RibbonPannelItem *>(item->data(
RibbonCustomizeWidget::PointerRole).value<qintptr>());
act = (pi->action);
}
return (act);
}
/**
* @brief RibbonCustomizeWidget::RibbonCustomizeWidget
* @param ribbonWindow 传入需要管理的RibbonMainWindow指针
* @param parent 用于界面生成的parent,可以和RibbonMainWindow一样
* @param f 同QWidget::QWidget的第二个参数
*/
RibbonCustomizeWidget::RibbonCustomizeWidget(RibbonMainWindow *ribbonWindow, QWidget *parent, Qt::WindowFlags f)
: QWidget(parent, f), ui(new RibbonCustomizeWidgetUi), m_d(new RibbonCustomizeWidgetPrivate(this)) {
m_d->mRibbonWindow = ribbonWindow;
ui->setupUi(this);
ui->listViewSelect->setModel(m_d->mAcionModel);
ui->treeViewResult->setModel(m_d->mRibbonModel);
initConnection();
updateModel();
}
RibbonCustomizeWidget::~RibbonCustomizeWidget() {
delete ui;
delete m_d;
}
void RibbonCustomizeWidget::initConnection() {
// 这个需要qt5.8以上支持
// connect(ui->comboBoxActionIndex, QOverload<int>::of(&QComboBox::currentIndexChanged)
// , this, &RibbonCustomizeWidget::onComboBoxActionIndexCurrentIndexChanged);
connect(ui->comboBoxActionIndex, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
&RibbonCustomizeWidget::onComboBoxActionIndexCurrentIndexChanged);
// 这个需要qt5.8以上支持
// connect(ui->radioButtonGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked)
// , this, &RibbonCustomizeWidget::onRadioButtonGroupButtonClicked);
connect(ui->radioButtonGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *)>(&QButtonGroup::buttonClicked),
this, &RibbonCustomizeWidget::onRadioButtonGroupButtonClicked);
connect(ui->pushButtonNewCategory, &QPushButton::clicked, this,
&RibbonCustomizeWidget::onPushButtonNewCategoryClicked);
connect(ui->pushButtonNewPannel, &QPushButton::clicked, this, &RibbonCustomizeWidget::onPushButtonNewPannelClicked);
connect(ui->pushButtonRename, &QPushButton::clicked, this, &RibbonCustomizeWidget::onPushButtonRenameClicked);
connect(ui->pushButtonAdd, &QPushButton::clicked, this, &RibbonCustomizeWidget::onPushButtonAddClicked);
connect(ui->pushButtonDelete, &QPushButton::clicked, this, &RibbonCustomizeWidget::onPushButtonDeleteClicked);
connect(ui->listViewSelect, &QAbstractItemView::clicked, this, &RibbonCustomizeWidget::onListViewSelectClicked);
connect(ui->treeViewResult, &QAbstractItemView::clicked, this, &RibbonCustomizeWidget::onTreeViewResultClicked);
connect(ui->toolButtonUp, &QToolButton::clicked, this, &RibbonCustomizeWidget::onToolButtonUpClicked);
connect(ui->toolButtonDown, &QToolButton::clicked, this, &RibbonCustomizeWidget::onToolButtonDownClicked);
connect(m_d->mRibbonModel, &QStandardItemModel::itemChanged, this, &RibbonCustomizeWidget::onItemChanged);
connect(ui->lineEditSearchAction, &QLineEdit::textEdited, this,
&RibbonCustomizeWidget::onLineEditSearchActionTextEdited);
}
/**
* @brief 设置action管理器
* @param mgr
*/
void RibbonCustomizeWidget::setupActionsManager(RibbonActionsManager *mgr) {
m_d->mActionMgr = mgr;
if (m_d->mActionMgr) {
m_d->mAcionModel->uninstallActionsManager();
}
m_d->mAcionModel->setupActionsManager(mgr);
//更新左边复选框
QList<int> tags = mgr->actionTags();
ui->comboBoxActionIndex->clear();
for (int tag : tags) {
ui->comboBoxActionIndex->addItem(mgr->tagName(tag), tag);
}
}
bool RibbonCustomizeWidget::isChanged() const {
return (m_d->mCustomizeDatas.size() > 0);
}
/**
* @brief 获取model
* @return
*/
const QStandardItemModel *RibbonCustomizeWidget::model() const {
return (m_d->mRibbonModel);
}
/**
* @brief 根据当前的radiobutton选项来更新model
*/
void RibbonCustomizeWidget::updateModel() {
updateModel(ui->radioButtonAllCategory->isChecked() ? ShowAllCategory : ShowMainCategory);
if (m_d->mRibbonWindow) {
RibbonBar *bar = m_d->mRibbonWindow->ribbonBar();
if (bar) {
ui->comboBoxActionProportion->clear();
if (bar->isTwoRowStyle()) {
ui->comboBoxActionProportion->addItem(tr("large"), RibbonPannelItem::Large);
ui->comboBoxActionProportion->addItem(tr("small"), RibbonPannelItem::Small);
} else {
ui->comboBoxActionProportion->addItem(tr("large"), RibbonPannelItem::Large);
ui->comboBoxActionProportion->addItem(tr("medium"), RibbonPannelItem::Medium);
ui->comboBoxActionProportion->addItem(tr("small"), RibbonPannelItem::Small);
}
}
}
}
/**
* @brief 更新model
*/
void RibbonCustomizeWidget::updateModel(RibbonTreeShowType type) {
m_d->mShowType = type;
m_d->updateModel();
}
/**
* @brief 应用所有设定
* @return 应用成功返回true
* @note 所有设定有一个应用成功都会返回true
*/
bool RibbonCustomizeWidget::applys() {
simplify();
return (sa_customize_datas_apply(m_d->mCustomizeDatas, m_d->mRibbonWindow) > 0);
}
/**
* @brief 转换为xml
*
* 此函数仅会写element,不会写document相关内容,因此如果需要写document,
* 需要在此函数前调用QXmlStreamWriter::writeStartDocument(),在此函数后调用QXmlStreamWriter::writeEndDocument()
*
* @note 注意,在传入QXmlStreamWriter之前,需要设置编码为utf-8:xml->setCodec("utf-8");
* @note 由于QXmlStreamWriter在QString作为io时,是不支持编码的,而此又无法保证自定义过程不出现中文字符,
* 因此,QXmlStreamWriter不应该通过QString进行构造,如果需要用到string,也需要通过QByteArray构造,如:
* @code
* RibbonCustomizeDialog dlg(this);//this为RibbonMainWindow的窗口
* dlg.setupActionsManager(m_actMgr);
* if (RibbonCustomizeDialog::Accepted == dlg.exec()) {
* dlg.applys();
* QByteArray str;
* QXmlStreamWriter xml(&str);//QXmlStreamWriter不建议通过QString构造,遇到中文会异常
* xml.setAutoFormatting(true);
* xml.setAutoFormattingIndent(2);
* xml.setCodec("utf-8");//在writeStartDocument之前指定编码
* xml.writeStartDocument();
* bool isok = dlg.toXml(&xml);
* xml.writeEndDocument();
* if (isok) {
* QFile f("customize.xml");
* if (f.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Truncate)) {
* QTextStream s(&f);
* s.setCodec("utf-8");//指定编码输出
* s << str;
* s.flush();
* }
* m_edit->append("write xml:");//m_edit的定义为:QTextEdit *m_edit;
* m_edit->append(str);
* }
* }
* @endcode
* @return 如果出现异常,返回false,如果没有自定义数据也会返回false
* @see sa_customize_datas_to_xml
*/
bool RibbonCustomizeWidget::toXml(QXmlStreamWriter *xml) const {
QList<RibbonCustomizeData> res;
res = m_d->mOldCustomizeDatas + m_d->mCustomizeDatas;
res = RibbonCustomizeData::simplify(res);
return (sa_customize_datas_to_xml(xml, res));
}
/**
* @brief 把配置写入文件中
* @param xmlpath
* @return
*/
bool RibbonCustomizeWidget::toXml(const QString &xmlpath) const {
QFile f(xmlpath);
if (!f.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
return (false);
}
QXmlStreamWriter xml(&f);
xml.setAutoFormatting(true);
xml.setAutoFormattingIndent(2);
xml.setCodec("utf-8");//在writeStartDocument之前指定编码
xml.writeStartDocument();
bool isOK = toXml(&xml);
xml.writeEndDocument();
f.close();
return (isOK);
}
/**
* @brief 从xml中加载QList<RibbonCustomizeData>
*
* 对于基于配置文件的设置,对话框显示前建议调用此函数,保证叠加设置的正确记录
* @param xml
* @return
* @note 此函数要在@ref setupActionsManager 函数之后调用
*/
void RibbonCustomizeWidget::fromXml(QXmlStreamReader *xml) {
QList<RibbonCustomizeData> cds = sa_customize_datas_from_xml(xml, m_d->mActionMgr);
m_d->mOldCustomizeDatas = cds;
}
/**
* @brief 从xml中加载QList<RibbonCustomizeData>
*
* 对于基于配置文件的设置,对话框显示前建议调用此函数,保证叠加设置的正确记录
* @param xmlpath
* @note 此函数要在@ref setupActionsManager 函数之后调用
* @note 如果程序启动后加载了自定义配置,再调用此窗口时需要调用此函数,把原来的配置加载进来,
* 在生成新动作时会把旧动作保存,但在调用applys时不会调用此加载的动作
*/
void RibbonCustomizeWidget::fromXml(const QString &xmlpath) {
QFile f(xmlpath);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
return;
}
f.seek(0);
QXmlStreamReader xml(&f);
fromXml(&xml);
}
/**
* @brief 应用xml配置
*
* @note 重复加载一个配置文件会发生异常,为了避免此类事件发生,一般通过一个变量保证只加载一次,如:
* @code
* //只能调用一次
* static bool has_call = false;
* if (!has_call) {
* QFile f("customize.xml");
* if (!f.open(QIODevice::ReadWrite|QIODevice::Text)) {
* return;
* }
* f.seek(0);
* QXmlStreamReader xml(&f);
* has_call = RibbonCustomizeWidget::fromXml(&xml, this, m_actMgr);
* }
* @endcode
* @param xml
* @param w
* @return 所有设定有一个应用成功都会返回true
* @see sa_customize_datas_from_xml sa_customize_datas_apply sa_apply_customize_from_xml_file
*/
bool RibbonCustomizeWidget::fromXml(QXmlStreamReader *xml, RibbonMainWindow *w, RibbonActionsManager *mgr) {
//先找到sa-ribbon-customize标签
QList<RibbonCustomizeData> cds = sa_customize_datas_from_xml(xml, mgr);
return (sa_customize_datas_apply(cds, w) > 0);
}
/**
* @brief 清除所有动作
*
* 在执行applys函数后,如果要继续调用,应该clear,否则会导致异常
*/
void RibbonCustomizeWidget::clear() {
m_d->mCustomizeDatas.clear();
}
/**
* @brief 精简
*/
void RibbonCustomizeWidget::simplify() {
m_d->mCustomizeDatas = RibbonCustomizeData::simplify(m_d->mCustomizeDatas);
}
/**
* @brief 获取当前界面选中的行属性
* @return
*/
RibbonPannelItem::RowProportion RibbonCustomizeWidget::selectedRowProportion() const {
return (static_cast<RibbonPannelItem::RowProportion>(ui->comboBoxActionProportion->currentData().toInt()));
}
/**
* @brief 获取listview中选中的action
* @return 如果没有选中action,返回nullptr
* @note 如果要获取treeview选中的action,使用@ref itemToAction 函数
*/
QAction *RibbonCustomizeWidget::selectedAction() const {
QItemSelectionModel *m = ui->listViewSelect->selectionModel();
if ((nullptr == m) || !m->hasSelection()) {
return (nullptr);
}
QModelIndex i = m->currentIndex();
return (m_d->mAcionModel->indexToAction(i));
}
/**
* @brief 把item转换为action
* @param item
* @return 如果没有action可转换,返回nullptr
*/
QAction *RibbonCustomizeWidget::itemToAction(QStandardItem *item) const {
return (m_d->itemToAction(item));
}
/**
* @brief 获取ribbon tree选中的item
* @return
*/
QStandardItem *RibbonCustomizeWidget::selectedItem() const {
QItemSelectionModel *m = ui->treeViewResult->selectionModel();
if ((nullptr == m) || !m->hasSelection()) {
return (nullptr);
}
QModelIndex i = m->currentIndex();
return (m_d->mRibbonModel->itemFromIndex(i));
}
/**
* @brief 获取选中的ribbon tree 的level
* @return -1为选中异常,0代表选中了category 1代表选中了pannel 2代表选中了action
*/
int RibbonCustomizeWidget::selectedRibbonLevel() const {
QStandardItem *item = selectedItem();
if (item) {
return (itemLevel(item));
}
return (-1);
}
/**
* @brief 获取StandardItem 的level
* @param item
* @return
*/
int RibbonCustomizeWidget::itemLevel(QStandardItem *item) const {
return (m_d->itemLevel(item));
}
/**
* @brief 设置某个item被选中
* @param item
*/
void RibbonCustomizeWidget::setSelectItem(QStandardItem *item, bool ensureVisible) {
QItemSelectionModel *m = ui->treeViewResult->selectionModel();
if (nullptr == m) {
return;
}
if (m) {
m->clearSelection();
m->select(item->index(), QItemSelectionModel::Select);
}
if (ensureVisible) {
ui->treeViewResult->scrollTo(item->index());
}
}
/**
* @brief 判断itemn能否改动,可以改动返回true
* @param item
* @return
*/
bool RibbonCustomizeWidget::isItemCanCustomize(QStandardItem *item) const {
return (m_d->isItemCanCustomize(item));