1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2015 Dongxu Li (dongxuli2011 at gmail.com)
6 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
7 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
8 **
9 **
10 ** This file may be distributed and/or modified under the terms of the
11 ** GNU General Public License version 2 as published by the Free Software
12 ** Foundation and appearing in the file gpl-2.0.txt included in the
13 ** packaging of this file.
14 **
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ** GNU General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program; if not, write to the Free Software
22 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 **
24 ** This copyright notice MUST APPEAR in all copies of the script!
25 **
26 **********************************************************************/
27 #include<QAction>
28 #include<QToolButton>
29 #include<QLayout>
30 #include<QMouseEvent>
31 #include "qg_cadtoolbarselect.h"
32 #include "qg_cadtoolbar.h"
33 #include "rs_actionselect.h"
34 #include "qg_actionhandler.h"
35 #include "rs_debug.h"
36 
37 /*
38  *  Constructs a QG_CadToolBarSelect as a child of 'parent', with the
39  *  name 'name' and widget flags set to 'f'.
40  */
QG_CadToolBarSelect(QG_CadToolBar * parent,Qt::WindowFlags fl)41 QG_CadToolBarSelect::QG_CadToolBarSelect(QG_CadToolBar* parent, Qt::WindowFlags fl)
42 	:LC_CadToolBarInterface(parent, fl)
43 	,nextAction(-1)
44 	,selectAction(nullptr)
45 {
46 	initToolBars();
47 	if(layout()){
48 		QToolButton* button=new QToolButton;
49 		button->setDefaultAction(m_pButtonForward);
50         button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
51 		layout()->addWidget(button);
52         static_cast<QVBoxLayout*>(layout())->addStretch(1);
53 	}
54 	connect(m_pButtonForward, SIGNAL(triggered()), this, SLOT(runNextAction()));
55 }
56 
setSelectAction(RS_ActionInterface * selectAction)57 void QG_CadToolBarSelect::setSelectAction(RS_ActionInterface* selectAction) {
58     this->selectAction = selectAction;
59 }
60 
setNextAction(int nextAction)61 void QG_CadToolBarSelect::setNextAction(int nextAction) {
62     this->nextAction = nextAction;
63     if (nextAction==-1) {
64 //		DEBUG_HEADER
65 		m_pButtonForward->setVisible(false);
66     } else {
67 //		DEBUG_HEADER
68 		m_pButtonForward->setVisible(true);
69     }
70 }
71 
runNextAction()72 void QG_CadToolBarSelect::runNextAction() {
73 	if (selectAction) {
74         if(selectAction->rtti() == RS2::ActionSelect){
75             //refuse to run next action if no entity is selected, to avoid segfault by action upon empty selection
76             //issue#235
77             if( static_cast<RS_ActionSelect*>(selectAction)->countSelected()==0) return;
78         }
79         selectAction->finish();
80 		selectAction = nullptr;
81     }
82     if (nextAction!=-1) {
83         actionHandler->killSelectActions();
84         actionHandler->setCurrentAction((RS2::ActionType)nextAction);
85     }
86 }
87 
mousePressEvent(QMouseEvent * e)88 void QG_CadToolBarSelect::mousePressEvent(QMouseEvent* e) {
89 	if (e->button()==Qt::RightButton && cadToolBar) {
90 		on_bBack_clicked();
91 		e->accept();
92 	}
93 }
94 
on_bBack_clicked()95 void QG_CadToolBarSelect::on_bBack_clicked()
96 {
97 	killSelectActions();
98 	if(cadToolBar){
99 		cadToolBar->showPreviousToolBar(true);
100 		cadToolBar->resetToolBar();
101 	}
102 }
103