QtCanpool 3.3.0
载入中...
搜索中...
未找到
ribboncustomizedata.cpp
浏览该文件的文档.
1
6#include "ribbonbar.h"
7#include "ribbonwindow.h"
8
9#include <QDebug>
10#include <QObject>
11
13
19#ifndef QX_RIBBON_PROP_CAN_CUSTOMIZE
20#define QX_RIBBON_PROP_CAN_CUSTOMIZE "_qx_isCanCustomize"
21#endif
22
23QList<RibbonCustomizeData> remove_indexs(const QList<RibbonCustomizeData> &csd, const QList<int> &willRemoveIndexs)
24{
25 QList<RibbonCustomizeData> res;
26
27 for (int i = 0; i < csd.size(); ++i) {
28 if (!willRemoveIndexs.contains(i)) {
29 res << csd[i];
30 }
31 }
32 return res;
33}
34
38
39RibbonCustomizeData::RibbonCustomizeData()
40 : indexValue(-1)
41 , actionRowProportionValue(RibbonGroup::Large)
42 , m_type(UnknowActionType)
43 , m_actionsManager(Q_NULLPTR)
44 , customized(false)
45{
46}
47
49 : indexValue(-1)
50 , actionRowProportionValue(RibbonGroup::Large)
51 , m_type(type)
52 , m_actionsManager(mgr)
53 , customized(false)
54{
55}
56
65
74
82{
83 return (actionType() != UnknowActionType);
84}
85
92{
93 if (customized) {
94 return true;
95 }
96 if (Q_NULLPTR == bar) {
97 return false;
98 }
99 switch (actionType()) {
100 case UnknowActionType:
101 return false;
102
103 case AddPageActionType: {
104 // 添加标签
106 if (Q_NULLPTR == page) {
107 return false;
108 }
109 page->setObjectName(pageObjNameValue);
111 break;
112 }
113
114 case AddGroupActionType: {
115 // 添加group
117 if (Q_NULLPTR == page) {
118 return false;
119 }
121 group->setObjectName(groupObjNameValue);
123 break;
124 }
125
126 case AddActionActionType: {
127 if (Q_NULLPTR == m_actionsManager) {
128 return false;
129 }
131 if (Q_NULLPTR == page) {
132 return false;
133 }
135 if (Q_NULLPTR == group) {
136 return false;
137 }
138 QAction *act = m_actionsManager->action(keyValue);
139 if (Q_NULLPTR == act) {
140 return false;
141 }
144 break;
145 }
146
149 if (Q_NULLPTR == page) {
150 return false;
151 }
152 bar->removePage(page);
153 break;
154 }
155
158 if (Q_NULLPTR == page) {
159 return false;
160 }
162 if (Q_NULLPTR == group) {
163 return false;
164 }
165 page->removeGroup(group);
166 break;
167 }
168
171 if (Q_NULLPTR == page) {
172 return false;
173 }
175 if (Q_NULLPTR == group) {
176 return false;
177 }
178 QAction *act = m_actionsManager->action(keyValue);
179 if (Q_NULLPTR == act) {
180 return false;
181 }
182 group->removeAction(act);
183 break;
184 }
185
188 if (Q_NULLPTR == page) {
189 return false;
190 }
191 int currentindex = bar->pageIndex(page);
192 if (-1 == currentindex) {
193 return false;
194 }
195 int toindex = currentindex + indexValue;
196 bar->movePage(currentindex, toindex);
197 break;
198 }
199
202 if (Q_NULLPTR == page) {
203 return false;
204 }
206 if (Q_NULLPTR == group) {
207 return false;
208 }
209 int groupIndex = page->groupIndex(group);
210 if (-1 == groupIndex) {
211 return false;
212 }
213 page->moveGroup(groupIndex, groupIndex + indexValue);
214 break;
215 }
216
219 if (Q_NULLPTR == page) {
220 return false;
221 }
223 if (Q_NULLPTR == group) {
224 return false;
225 }
226 QAction *act = m_actionsManager->action(keyValue);
227 if (Q_NULLPTR == act) {
228 return false;
229 }
230 int actindex = group->actionIndex(act);
231 if (actindex <= -1) {
232 return false;
233 }
234 group->moveAction(actindex, actindex + indexValue);
235 break;
236 }
237
240 if (Q_NULLPTR == page) {
241 return false;
242 }
243 page->setWindowTitle(keyValue);
244 break;
245 }
246
249 if (Q_NULLPTR == page) {
250 return false;
251 }
253 if (Q_NULLPTR == group) {
254 return false;
255 }
256 group->setWindowTitle(keyValue);
257 break;
258 }
259
262 if (Q_NULLPTR == page) {
263 return false;
264 }
265 if (1 == indexValue) {
266 bar->showPage(page);
267 } else {
268 bar->hidePage(page);
269 }
270 break;
271 }
272
273 default:
274 return false;
275 }
276 customized = true;
277 return true;
278}
279
281{
282 return m_actionsManager;
283}
284
286{
287 m_actionsManager = mgr;
288}
289
291 const QString &objName)
292{
294
295 data.indexValue = index;
296 data.keyValue = title;
297 data.pageObjNameValue = objName;
298#if QX_RIBBON_DEBUG
299 qDebug() << QString("add page %1").arg(objName);
300#endif
301 return data;
302}
303
313 const QString &pageObjName, const QString &objName)
314{
316
317 data.indexValue = index;
318 data.keyValue = title;
319 data.groupObjNameValue = objName;
320 data.pageObjNameValue = pageObjName;
321#ifdef QX_RIBBON_DEBUG
322 qDebug() << QString("add group %1 to page %2").arg(objName, pageObjName);
323#endif
324 return data;
325}
326
338 const QString &pageObjName,
339 const QString &groupObjName)
340{
342
343 data.keyValue = key;
344 data.pageObjNameValue = pageObjName;
345 data.groupObjNameValue = groupObjName;
346 data.actionRowProportionValue = rp;
347#if QX_RIBBON_DEBUG
348 qDebug() << QString("add action %1 to %2/%3").arg(key, pageObjName, groupObjName);
349#endif
350 return data;
351}
352
353RibbonCustomizeData RibbonCustomizeData::makeRenamePageCustomizeData(const QString &newName, const QString &pageObjName)
354{
356
357 if (pageObjName.isEmpty()) {
358 qDebug() << QObject::tr("Ribbon Warning !!! customize rename page,"
359 "but get an empty page object name,"
360 "if you want to customize Ribbon,"
361 "please make sure every element has been set object name.");
362 }
363 data.keyValue = newName;
364 data.pageObjNameValue = pageObjName;
365
366 return data;
367}
368
370 const QString &pageObjName,
371 const QString &groupObjName)
372{
374
375 if (groupObjName.isEmpty() || pageObjName.isEmpty()) {
376 qDebug() << QObject::tr("Ribbon Warning !!! customize rename group,"
377 "but get an empty page/group object name,"
378 "if you want to customize Ribbon,"
379 "please make sure every element has been set object name.");
380 }
381 data.keyValue = newName;
382 data.groupObjNameValue = groupObjName;
383 data.pageObjNameValue = pageObjName;
384 return data;
385}
386
394{
396
397 if (pageObjName.isEmpty()) {
398 qDebug() << QObject::tr("Ribbon Warning !!! customize change page order,"
399 "but get an empty page object name,"
400 "if you want to customize Ribbon,"
401 "please make sure every element has been set object name.");
402 }
403#if QX_RIBBON_DEBUG
404 qDebug() << QString("change page %1 order from %2 to %3").arg(pageObjName).arg(data.indexValue).arg(moveIndex);
405#endif
406 data.pageObjNameValue = pageObjName;
407 data.indexValue = moveIndex;
408 return data;
409}
410
419 const QString &groupObjName, int moveIndex)
420{
422
423 if (pageObjName.isEmpty() || groupObjName.isEmpty()) {
424 qDebug() << QObject::tr("Ribbon Warning !!! customize change group order,"
425 "but get an empty page/group object name,"
426 "if you want to customize Ribbon,"
427 "please make sure every element has been set object name.");
428 }
429#if QX_RIBBON_DEBUG
430 qDebug() << QString("change group %1 order from %2 to %3").arg(groupObjName).arg(data.indexValue).arg(moveIndex);
431#endif
432 data.pageObjNameValue = pageObjName;
433 data.groupObjNameValue = groupObjName;
434 data.indexValue = moveIndex;
435 return data;
436}
437
448 const QString &groupObjName,
449 const QString &key,
450 RibbonActionsManager *mgr, int moveIndex)
451{
453
454 if (pageObjName.isEmpty() || groupObjName.isEmpty() || key.isEmpty()) {
455 qDebug() << QObject::tr("Ribbon Warning !!! customize change action order,"
456 "but get an empty page/group/action object name,"
457 "if you want to customize Ribbon,"
458 "please make sure every element has been set object name.");
459 }
460#if QX_RIBBON_DEBUG
461 qDebug() << QString("change action %1 order from %2 to %3").arg(key).arg(data.indexValue).arg(moveIndex);
462#endif
463 data.pageObjNameValue = pageObjName;
464 data.groupObjNameValue = groupObjName;
465 data.keyValue = key;
466 data.indexValue = moveIndex;
467 return data;
468}
469
471{
473
474 if (pageObjName.isEmpty()) {
475 qDebug() << QObject::tr("Ribbon Warning !!! customize remove page,"
476 "but get an empty page object name,"
477 "if you want to customize Ribbon,"
478 "please make sure every element has been set object name.");
479 }
480#if QX_RIBBON_DEBUG
481 qDebug() << QString("remove page %1").arg(pageObjName);
482#endif
483 data.pageObjNameValue = pageObjName;
484 return data;
485}
486
488 const QString &groupObjName)
489{
491
492 if (pageObjName.isEmpty() || groupObjName.isEmpty()) {
493 qDebug() << QObject::tr("Ribbon Warning !!! customize remove group,"
494 "but get an empty page/group object name,"
495 "if you want to customize Ribbon,"
496 "please make sure every element has been set object name.");
497 }
498#if QX_RIBBON_DEBUG
499 qDebug() << QString("remove group %1 from page %2").arg(groupObjName, pageObjName);
500#endif
501 data.pageObjNameValue = pageObjName;
502 data.groupObjNameValue = groupObjName;
503 return data;
504}
505
515 const QString &groupObjName, const QString &key,
517{
519
520 if (pageObjName.isEmpty() || groupObjName.isEmpty() || key.isEmpty()) {
521 qDebug() << QObject::tr("Ribbon Warning !!! customize remove action,"
522 "but get an empty page/group/action object name,"
523 "if you want to customize Ribbon,"
524 "please make sure every element has been set object name.");
525 }
526#if QX_RIBBON_DEBUG
527 qDebug() << QString("remove action %1 from %2/%3").arg(key, pageObjName, groupObjName);
528#endif
529 data.pageObjNameValue = pageObjName;
530 data.groupObjNameValue = groupObjName;
531 data.keyValue = key;
532 return data;
533}
534
542{
544
545 if (pageObjName.isEmpty()) {
546 qDebug() << QObject::tr("Ribbon Warning !!! customize visible page,"
547 "but get an empty page object name,"
548 "if you want to customize Ribbon,"
549 "please make sure every element has been set object name.");
550 }
551 data.pageObjNameValue = pageObjName;
552 data.indexValue = isShow ? 1 : 0;
553 return data;
554}
555
557{
558 QVariant v = obj->property(QX_RIBBON_PROP_CAN_CUSTOMIZE);
559
560 if (v.isValid()) {
561 return v.toBool();
562 }
563 return false;
564}
565
566void RibbonCustomizeData::setCanCustomize(QObject *obj, bool canbe)
567{
568 obj->setProperty(QX_RIBBON_PROP_CAN_CUSTOMIZE, canbe);
569}
570
584QList<RibbonCustomizeData> RibbonCustomizeData::simplify(const QList<RibbonCustomizeData> &csd)
585{
586 int size = csd.size();
587
588 if (size <= 1) {
589 return csd;
590 }
591 QList<RibbonCustomizeData> res;
592 QList<int> willRemoveIndexs; // 记录要删除的index
593
595 // 说明:同一个元素不可能先删再加,只能先加再删,而且只能删一次,所以二级循环遍历,找到就跳出内循环
596 for (int i = 0; i < size; ++i) {
597 if (csd[i].actionType() == AddPageActionType) {
598 for (int j = i + 1; j < size; ++j) {
599 if (csd[j].actionType() != RemovePageActionType) {
600 continue;
601 }
602 if (csd[i].pageObjNameValue == csd[j].pageObjNameValue) {
603 willRemoveIndexs << i << j;
604 break;
605 }
606 }
607 } else if (csd[i].actionType() == AddGroupActionType) {
608 for (int j = i + 1; j < size; ++j) {
609 if (csd[j].actionType() != RemoveGroupActionType) {
610 continue;
611 }
612 if ((csd[i].groupObjNameValue == csd[j].groupObjNameValue) &&
613 (csd[i].pageObjNameValue == csd[j].pageObjNameValue)) {
614 willRemoveIndexs << i << j;
615 break;
616 }
617 }
618 } else if (csd[i].actionType() == AddActionActionType) {
619 for (int j = i + 1; j < size; ++j) {
620 if (csd[j].actionType() != RemoveActionActionType) {
621 continue;
622 }
623 if ((csd[i].keyValue == csd[j].keyValue) && (csd[i].groupObjNameValue == csd[j].groupObjNameValue) &&
624 (csd[i].pageObjNameValue == csd[j].pageObjNameValue)) {
625 willRemoveIndexs << i << j;
626 break;
627 }
628 }
629 }
630 }
631 res = remove_indexs(csd, willRemoveIndexs);
632 willRemoveIndexs.clear();
633
635 // 说明:同一个元素可能会被反复隐藏和显示,所以二级循环遍历,找到就删除前一个、记录后一个,直到内循环结束
636 // 下一次外循环,如果已被删除的或已经是最后的,就直接跳过
637 QList<int> willNotCheckIndexs; // 记录免检的index
638 size = res.size();
639 for (int i = 0; i < size; ++i) {
640 if (res[i].actionType() == VisiblePageActionType) {
641 // 前置判断中使用的contains也是for循环,加了这个判断一级循环增加了处理能有多少性能收益?
642 if (willRemoveIndexs.contains(i) || willNotCheckIndexs.contains(i)) {
643 continue;
644 }
645 int last = i;
646 for (int j = i + 1; j < size; ++j) {
647 if (res[j].actionType() == VisiblePageActionType) {
648 if (res[i].pageObjNameValue == res[j].pageObjNameValue) {
649 willRemoveIndexs << last; // 删除前一个只保留最后一个
650 last = j;
651 }
652 }
653 }
654 if (last != i) { // 说明多次出现,last已经是最后了,下次可以免检了
655 willNotCheckIndexs << last;
656 }
657 }
658 }
659 willNotCheckIndexs.clear();
660 res = remove_indexs(res, willRemoveIndexs);
661 willRemoveIndexs.clear();
662
664 size = res.size();
665 QList<int> willRenameIndexs;
666 for (int i = 0; i < size; ++i) {
667 if (res[i].actionType() == RenamePageActionType) {
668 // 向后查询,如果查询到有同一个Page改名,把这个索引加入删除队列
669 int last = i;
670 for (int j = i + 1; j < size; ++j) {
671 if ((res[j].actionType() == RenamePageActionType) &&
672 (res[i].pageObjNameValue == res[j].pageObjNameValue)) {
673 willRemoveIndexs << last;
674 last = j;
675 }
676 }
677 willRenameIndexs << last;
678 } else if (res[i].actionType() == RenameGroupActionType) {
679 // 向后查询,如果查询到有同一个group改名,把这个索引加入删除队列
680 int last = i;
681 for (int j = i + 1; j < size; ++j) {
682 if ((res[j].actionType() == RenameGroupActionType) &&
683 (res[i].groupObjNameValue == res[j].groupObjNameValue) &&
684 (res[i].pageObjNameValue == res[j].pageObjNameValue)) {
685 willRemoveIndexs << last;
686 last = j;
687 }
688 }
689 willRenameIndexs << last;
690 }
691 }
692 res = remove_indexs(res, willRemoveIndexs);
693 willRemoveIndexs.clear();
694
696 foreach (int i, willRenameIndexs) {
697 if (res[i].actionType() == RenamePageActionType) {
698 for (int j = 0; j < i; ++j) {
699 if (res[j].pageObjNameValue == res[i].pageObjNameValue) {
700 res[j].keyValue = res[i].keyValue;
701 break;
702 }
703 }
704 } else if (res[i].actionType() == RenameGroupActionType) {
705 for (int j = 0; j < i; ++j) {
706 if ((res[j].groupObjNameValue == res[i].groupObjNameValue) &&
707 (res[j].pageObjNameValue == res[i].pageObjNameValue)) {
708 res[j].keyValue = res[i].keyValue;
709 break;
710 }
711 }
712 }
713 }
714 res = remove_indexs(res, willRenameIndexs);
715 willRenameIndexs.clear();
716
718 size = res.size();
719 for (int i = 1; i < size; ++i) {
720 if ((res[i - 1].actionType() == ChangePageOrderActionType) &&
722 (res[i - 1].pageObjNameValue == res[i].pageObjNameValue)) {
723 // 说明连续两个顺序调整,把前一个indexvalue和后一个indexvalue相加,前一个删除
724 res[i].indexValue += res[i - 1].indexValue;
725 willRemoveIndexs << i - 1;
726 } else if ((res[i - 1].actionType() == ChangeGroupOrderActionType) &&
728 (res[i - 1].groupObjNameValue == res[i].groupObjNameValue) &&
729 (res[i - 1].pageObjNameValue == res[i].pageObjNameValue)) {
730 // 说明连续两个顺序调整,把前一个indexvalue和后一个indexvalue相加,前一个删除
731 res[i].indexValue += res[i - 1].indexValue;
732 willRemoveIndexs << i - 1;
733 } else if ((res[i - 1].actionType() == ChangeActionOrderActionType) &&
734 (res[i].actionType() == ChangeActionOrderActionType) && (res[i - 1].keyValue == res[i].keyValue) &&
735 (res[i - 1].groupObjNameValue == res[i].groupObjNameValue) &&
736 (res[i - 1].pageObjNameValue == res[i].pageObjNameValue)) {
737 // 说明连续两个顺序调整,把前一个indexvalue和后一个indexvalue相加,前一个删除
738 res[i].indexValue += res[i - 1].indexValue;
739 willRemoveIndexs << i - 1;
740 }
741 }
742 res = remove_indexs(res, willRemoveIndexs);
743 willRemoveIndexs.clear();
744
746 size = res.size();
747 for (int i = 0; i < size; ++i) {
750 if (0 == res[i].indexValue) {
751 willRemoveIndexs << i;
752 }
753 }
754 }
755 res = remove_indexs(res, willRemoveIndexs);
756 willRemoveIndexs.clear();
757 return res;
758}
759
用于管理 Ribbon 的所有 Action
QAction * action(const QString &key) const
通过key获取action
RibbonBar 继承 QMenuBar, 在 RibbonWindow 中直接替换了原来的 QMenuBar
Definition ribbonbar.h:99
RibbonPage * insertPage(int index, const QString &title)
void movePage(int from, int to)
int pageIndex(RibbonPage *page) const
void showPage(RibbonPage *page)
显示被隐藏的page
void removePage(RibbonPage *page)
移除RibbonPage
RibbonPage * pageByObjectName(const QString &objname) const
通过ObjectName查找Page
void hidePage(RibbonPage *page)
隐藏page,并不会删除或者取走,只是隐藏
记录所有自定义操作的数据类
static RibbonCustomizeData makeChangeGroupOrderCustomizeData(const QString &pageObjName, const QString &groupObjName, int moveIndex)
对应ChangeGroupOrderActionType
void setActionsManager(RibbonActionsManager *mgr)
static void setCanCustomize(QObject *obj, bool canbe=true)
int indexValue
记录顺序的参数
QString pageObjNameValue
记录pageObjName,用于定位Page
static QList< RibbonCustomizeData > simplify(const QList< RibbonCustomizeData > &csd)
对QList<RibbonCustomizeData>进行简化操作
ActionType actionType() const
获取CustomizeData的action type
static RibbonCustomizeData makeRemoveGroupCustomizeData(const QString &pageObjName, const QString &groupObjName)
static RibbonCustomizeData makeAddPageCustomizeData(const QString &title, int index, const QString &objName)
static RibbonCustomizeData makeAddGroupCustomizeData(const QString &title, int index, const QString &pageObjName, const QString &objName)
创建一个AddGroupActionType的RibbonCustomizeData
bool isValid() const
判断是否是一个正常的CustomizeData
@ RemoveGroupActionType
删除group操作
@ VisiblePageActionType
对page执行隐藏/显示操作
@ ChangePageOrderActionType
改变page顺序的操作
@ AddActionActionType
添加action操作
@ RenamePageActionType
对page更名操作
@ RenameGroupActionType
对Group更名操作
@ ChangeActionOrderActionType
改变action顺序的操作
@ ChangeGroupOrderActionType
改变group顺序的操作
@ RemoveActionActionType
删除action操作
static RibbonCustomizeData makeVisiblePageCustomizeData(const QString &pageObjName, bool isShow)
RibbonCustomizeData::makeVisiblePageCustomizeData
static RibbonCustomizeData makeRemovePageCustomizeData(const QString &pageObjName)
QString keyValue
记录标题、索引等参数
static RibbonCustomizeData makeRenamePageCustomizeData(const QString &newName, const QString &pageObjName)
static RibbonCustomizeData makeRenameGroupCustomizeData(const QString &newName, const QString &pageObjName, const QString &groupObjName)
static RibbonCustomizeData makeRemoveActionCustomizeData(const QString &pageObjName, const QString &groupObjName, const QString &key, RibbonActionsManager *mgr)
对应RemoveActionActionType
static RibbonCustomizeData makeChangeActionOrderCustomizeData(const QString &pageObjName, const QString &groupObjName, const QString &key, RibbonActionsManager *mgr, int moveIndex)
对应ChangeActionOrderActionType
QString groupObjNameValue
记录groupObjName,ribbon的Customize索引大部分基于objname
static RibbonCustomizeData makeChangePageOrderCustomizeData(const QString &pageObjName, int moveIndex)
对应ChangePageOrderActionType
static bool isCanCustomize(QObject *obj)
static RibbonCustomizeData makeAddActionCustomizeData(const QString &key, RibbonActionsManager *mgr, RibbonGroup::RowProportion rp, const QString &pageObjName, const QString &groupObjName)
添加action
RibbonActionsManager * actionsManager()
bool apply(RibbonBar *bar)
应用RibbonCustomizeData到RibbonBar
RibbonGroup::RowProportion actionRowProportionValue
RibbonCustomizeData()
RibbonCustomizeData
void setActionType(ActionType a)
设置CustomizeData的action type
group 用于对 ribbon 页窗口中的控件进行分组
Definition ribbongroup.h:29
RibbonButton * addAction(QAction *action, RibbonGroup::RowProportion rp)
添加action
void moveAction(int from, int to)
移动action
int actionIndex(QAction *act) const
action对应的布局index,此操作一般用于移动,其他意义不大
一项 ribbon tab 页
Definition ribbonpage.h:30
bool removeGroup(RibbonGroup *group)
移除Group,Page会直接回收RibbonGroup内存
void moveGroup(int from, int to)
移动一个Group从from index到to index
RibbonGroup * insertGroup(int index, const QString &title)
新建一个group,并插入到index位置
int groupIndex(RibbonGroup *group) const
查找group对应的索引
RibbonGroup * groupByObjectName(const QString &objname) const
通过ObjectName查找group
QList< RibbonCustomizeData > remove_indexs(const QList< RibbonCustomizeData > &csd, const QList< int > &willRemoveIndexs)
#define Q_NULLPTR
Copyleft (C) 2025 maminjie canpool@163.com SPDX-License-Identifier: MulanPSL-2.0
#define QX_RIBBON_END_NAMESPACE
#define QX_RIBBON_BEGIN_NAMESPACE
#define QX_RIBBON_PROP_CAN_CUSTOMIZE
属性,用于标记是否可以进行自定义,用于动态设置到 RibbonPage 和 RibbonGroup, 值为bool,在为true时,可以通过 RibbonCustomizeWidget 改变这个Ribb...