1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 #include<QCoreApplication>
27 #include<QMouseEvent>
28 #include<QAction>
29 #include<tuple>
30 #include<utility>
31 #include "qg_cadtoolbarmain.h"
32 #include "qg_cadtoolbar.h"
33 #include "qg_actionhandler.h"
34 
35 /*
36  *  Constructs a QG_CadToolBarMain as a child of 'parent', with the
37  *  name 'name' and widget flags set to 'f'.
38  */
QG_CadToolBarMain(QG_CadToolBar * parent,Qt::WindowFlags fl)39 QG_CadToolBarMain::QG_CadToolBarMain(QG_CadToolBar* parent, Qt::WindowFlags fl)
40 	:LC_CadToolBarInterface(parent, fl)
41 {
42 	initToolBars();
43 }
44 
addSubActions(const std::vector<QAction * > & actions,bool)45 void QG_CadToolBarMain::addSubActions(const std::vector<QAction*>& actions, bool /*addGroup*/)
46 {
47 	const std::initializer_list<std::pair<QAction**, RS2::ActionType>> actionTypes=
48 	{
49 		std::make_pair(&bMenuText, RS2::ActionDrawMText),
50 		std::make_pair(&bMenuImage, RS2::ActionDrawImage),
51 		std::make_pair(&bMenuPoint, RS2::ActionDrawPoint),
52 		std::make_pair(&bMenuBlock, RS2::ActionBlocksCreate),
53 		std::make_pair(&bMenuHatch, RS2::ActionDrawHatch)
54 	};
55 	for(auto a: actions){
56 		auto it0=std::find_if(actionTypes.begin(), actionTypes.end(),
57 							  [&a](const std::pair<QAction**, RS2::ActionType>& a0)->bool{
58 			return a->data() == a0.second;
59 		});
60 		if(it0==actionTypes.end()) return;
61 		* it0->first = a;
62 	}
63 	if(std::any_of(actionTypes.begin(), actionTypes.end(),
64 				   [](const std::pair<QAction**, RS2::ActionType>& a)->bool{
65 				   return !*(a.first);
66 })) return;
67 	const std::initializer_list<std::tuple<QAction**, QString, const char*>> buttons={
68 		std::make_tuple(&bMenuLine, "menuline", R"(Show toolbar "Lines")"),
69 		std::make_tuple(&bMenuArc, "menuarc", R"(Show toolbar "Arcs")"),
70 		std::make_tuple(&bMenuCircle, "menucircle", R"(Show toolbar "Circles")"),
71 		std::make_tuple(&bMenuEllipse, "menuellipse", R"(Show toolbar "Ellipses")"),
72 		std::make_tuple(&bMenuPolyline, "menupolyline", R"(Show toolbar "Polylines")"),
73 		std::make_tuple(&bMenuSpline, "menuspline", R"(Show toolbar "Splines")"),
74 		std::make_tuple(&bMenuDim, "dimhor", R"(Show toolbar "Dimensions")"),
75 //		std::make_tuple(&bMenuHatch, "menuhatch", R"(Create Hatch)"),
76 		std::make_tuple(&bMenuModify, "menuedit", R"(Show toolbar "Modify")"),
77 		std::make_tuple(&bMenuInfo, "menumeasure", R"(Show toolbar "Info")"),
78 		std::make_tuple(&bMenuSelect, "menuselect", R"(Show toolbar "Select")")
79 	};
80 	std::vector<QAction*> listAction;
81 	for(const auto& a: buttons){
82 		QAction* p=new QAction(QIcon(":/extui/"+std::get<1>(a)+".png"),
83 					  QCoreApplication::translate("main", std::get<2>(a)), this);
84 		*std::get<0>(a)=p;
85 		listAction.push_back(p);
86 	}
87 	auto it = std::find(listAction.begin(), listAction.end(),bMenuDim);
88 
89 	//add draw actions
90 	listAction.insert(it, bMenuText);
91 
92 	QAction* nullAction=new QAction(this);
93 	nullAction->setEnabled(false);
94 	listAction.insert(it, nullAction);
95 
96 	listAction.insert(it, bMenuPoint);
97 
98 	it = std::find(listAction.begin(), listAction.end(),bMenuModify);
99 	listAction.insert(it, bMenuImage);
100 	it = std::find(listAction.begin(), listAction.end(),bMenuSelect);
101 	listAction.insert(it, bMenuBlock);
102 	it = std::find(listAction.begin(), listAction.end(),bMenuImage);
103 	listAction.insert(it, bMenuHatch);
104 	LC_CadToolBarInterface::addSubActions(listAction, false);
105 	for(auto a: actionTypes){
106 		(*a.first)->setCheckable(true);
107 		m_pActionGroup->addAction(*a.first);
108 	}
109 	if(actionHandler)
110 		setActionHandler(actionHandler);
111 }
112 
setActionHandler(QG_ActionHandler * ah)113 void QG_CadToolBarMain::setActionHandler(QG_ActionHandler* ah)
114 {
115 	actionHandler=ah;
116 	if(!bMenuLine) return;
117 	connect(bMenuLine, SIGNAL(triggered()),
118 			cadToolBar, SLOT(showToolBarLines()));
119 	connect(bMenuArc, SIGNAL(triggered()),
120 			cadToolBar, SLOT(showToolBarArcs()));
121 	connect(bMenuCircle, SIGNAL(triggered()),
122 			cadToolBar, SLOT(showToolBarCircles()));
123 	connect(bMenuEllipse, SIGNAL(triggered()),
124 			cadToolBar, SLOT(showToolBarEllipses()));
125 	connect(bMenuSpline, SIGNAL(triggered()),
126 			cadToolBar, SLOT(showToolBarSplines()));
127 	connect(bMenuPolyline, SIGNAL(triggered()),
128 			cadToolBar, SLOT(showToolBarPolylines()));
129 
130 	connect(bMenuDim, SIGNAL(triggered()),
131 			cadToolBar, SLOT(showToolBarDim()));
132 
133 	connect(bMenuModify, SIGNAL(triggered()),
134 			cadToolBar, SLOT(showToolBarModify()));
135 	connect(bMenuInfo, SIGNAL(triggered()),
136 			cadToolBar, SLOT(showToolBarInfo()));
137 
138 	connect(bMenuBlock, SIGNAL(triggered()),
139 			actionHandler, SLOT(slotBlocksCreate()));
140 	connect(bMenuSelect, SIGNAL(triggered()),
141 			cadToolBar, SLOT(showToolBarSelect()));
142 
143 }
144 
145 //clear current action
finishCurrentAction(bool resetToolBar)146 void QG_CadToolBarMain::finishCurrentAction(bool resetToolBar)
147 {
148 	if(!actionHandler) return;
149     RS_ActionInterface* currentAction =actionHandler->getCurrentAction();
150 	if(currentAction ) {
151         currentAction->finish(resetToolBar); //finish the action, but do not update toolBar
152     }
153 }
154 
slotDrawMText()155 void QG_CadToolBarMain::slotDrawMText()
156 {
157     finishCurrentAction();
158     actionHandler->slotDrawMText();
159 }
160 
slotDrawImage()161 void QG_CadToolBarMain::slotDrawImage()
162 {
163     finishCurrentAction();
164     actionHandler->slotDrawImage();
165 }
166 
167 //restore action from checked button
restoreAction()168 void QG_CadToolBarMain::restoreAction()
169 {
170 	if(!(actionHandler&&bMenuPoint)) return;
171 	if ( bMenuPoint ->isChecked() ) {
172         actionHandler->slotDrawPoint();
173         return;
174     }
175 	m_pHidden->setChecked(true);
176     finishCurrentAction();
177 }
178 
resetToolBar()179 void QG_CadToolBarMain::resetToolBar()
180 {
181 	killAllActions();
182 	m_pHidden->setChecked(true);
183 }
184 
mousePressEvent(QMouseEvent * e)185 void QG_CadToolBarMain::mousePressEvent(QMouseEvent* e)
186 {
187 	if (e->button()==Qt::RightButton && cadToolBar) {
188 		resetToolBar();
189 	}
190 }
191 
192 
showCadToolBar(RS2::ActionType actionType)193 void QG_CadToolBarMain::showCadToolBar(RS2::ActionType actionType) {
194 	if(!bMenuImage) return;
195     switch(actionType){
196     case RS2::ActionDrawImage:
197         bMenuImage->setChecked(true);
198         break;
199     case RS2::ActionDrawPoint:
200         bMenuPoint->setChecked(true);
201         break;
202     case RS2::ActionDrawMText:
203         bMenuText->setChecked(true);
204         break;
205     default:
206 		m_pHidden->setChecked(true);
207         break;
208     }
209 }
210