1 #include <QMouseEvent>
2 #include <QGridLayout>
3 #include <QActionGroup>
4 #include <QIcon>
5 #include <QToolButton>
6 #include <QToolBar>
7 #include "lc_cadtoolbarinterface.h"
8 #include "qg_cadtoolbar.h"
9 #include "qg_actionhandler.h"
10 #include "rs_debug.h"
11
LC_CadToolBarInterface(QG_CadToolBar * _parentTB,Qt::WindowFlags fl)12 LC_CadToolBarInterface::LC_CadToolBarInterface(QG_CadToolBar* _parentTB, Qt::WindowFlags fl):
13 QWidget(_parentTB, fl)
14 ,cadToolBar(_parentTB)
15 ,actionHandler(nullptr)
16 ,m_pHidden(new QAction("ActionHidden", this))
17 ,m_pGrid0(new QToolBar)
18 ,m_pGrid1(new QToolBar)
19 ,m_pActionGroup(new QActionGroup(this))
20 {
21 }
22
initToolBars()23 void LC_CadToolBarInterface::initToolBars()
24 {
25 switch(rtti()){
26 case RS2::ToolBarSelect:
27 m_pButtonForward = new QAction( QIcon(":/extui/forward.png"), "Continue", this);
28 //continue to default, no break by design
29 default:
30 m_pButtonBack = new QAction(QIcon(":/extui/back.png"), "Back", this);
31 case RS2::ToolBarMain:
32 break;
33 }
34 setStyleSheet("QToolBar{ margin: 0px }");
35 setContentsMargins(0,0,0,0);
36 // setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
37 for(auto p: {m_pGrid0, m_pGrid1}){
38 p->setFloatable(false);
39 p->setMovable(false);
40 p->setOrientation(Qt::Vertical);
41 p->setContentsMargins(0,0,0,0);
42 }
43
44 m_pActionGroup->setExclusive(true);
45 m_pHidden->setCheckable(true);
46 m_pHidden->setChecked(true);
47 m_pActionGroup->addAction(m_pHidden);
48
49 QHBoxLayout* hLayout=new QHBoxLayout;
50 hLayout->addWidget(m_pGrid0);
51 hLayout->addWidget(m_pGrid1);
52 hLayout->setSpacing(1);
53 hLayout->setContentsMargins(0,0,0,0);
54
55 QVBoxLayout* vLayout=new QVBoxLayout;
56 vLayout->setSpacing(1);
57 vLayout->setContentsMargins(0,0,0,0);
58 if(m_pButtonBack){
59 QToolButton* button=new QToolButton;
60 button->setDefaultAction(m_pButtonBack);
61 button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
62 vLayout->addWidget(button);
63 connect(m_pButtonBack, SIGNAL(triggered()), cadToolBar, SLOT(back()));
64 }
65 vLayout->addLayout(hLayout);
66 if(rtti()!=RS2::ToolBarSelect)
67 vLayout->addStretch(1);
68
69 setLayout(vLayout);
70 }
71
setActionHandler(QG_ActionHandler * ah)72 void LC_CadToolBarInterface::setActionHandler(QG_ActionHandler* ah)
73 {
74 actionHandler=ah;
75 }
76
finishCurrentAction(bool resetToolBar)77 void LC_CadToolBarInterface::finishCurrentAction(bool resetToolBar)
78 {
79 if(!actionHandler) return;
80 RS_ActionInterface* currentAction =actionHandler->getCurrentAction();
81 if(currentAction) {
82 currentAction->finish(resetToolBar); //finish the action, but do not update toolBar
83 }
84 }
85
86
killSelectActions()87 void LC_CadToolBarInterface::killSelectActions()
88 {
89 if(!actionHandler) return;
90 actionHandler->killSelectActions();
91 }
92
killAllActions()93 void LC_CadToolBarInterface::killAllActions()
94 {
95 if(!actionHandler) return;
96 actionHandler->killAllActions();
97 }
98
sizeHint() const99 QSize LC_CadToolBarInterface::sizeHint() const
100 {
101 return QSize(-1,-1);
102 }
103
104
mousePressEvent(QMouseEvent * e)105 void LC_CadToolBarInterface::mousePressEvent(QMouseEvent* e) {
106 if (e->button()==Qt::RightButton && cadToolBar) {
107 finishCurrentAction(true);
108 cadToolBar->showPreviousToolBar(true);
109 e->accept();
110 }
111 }
112
back()113 void LC_CadToolBarInterface::back()
114 {
115 finishCurrentAction(true);
116 if (cadToolBar) {
117 cadToolBar->showPreviousToolBar(true);
118 }
119 }
120
121
addSubAction(QAction * const action,bool addGroup)122 void LC_CadToolBarInterface::addSubAction(QAction*const action, bool addGroup)
123 {
124 RS_DEBUG->print("LC_CadToolBarInterface::addSubAction(): begin\n");
125 switch(rtti()){
126 case RS2::ToolBarMain:
127 action->setCheckable(false);
128 break;
129 default:
130 action->setCheckable(true);
131 }
132 if(actions0>actions1){
133 m_pGrid1->addAction(action);
134 ++actions1;
135 }else{
136 m_pGrid0->addAction(action);
137 ++actions0;
138 }
139
140 if(addGroup) m_pActionGroup->addAction(action);
141 RS_DEBUG->print("LC_CadToolBarInterface::addSubAction(): end\n");
142
143 }
144
145
addSubActions(const std::vector<QAction * > & actions,bool addGroup)146 void LC_CadToolBarInterface::addSubActions(const std::vector<QAction*>& actions, bool addGroup)
147 {
148 for(auto p: actions){
149 this->addSubAction(p, addGroup);
150 }
151 resize(cadToolBar->size());
152 }
153