1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2012 Rallaz (rallazz@gmail.com)
6 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
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 
27 #include "rs_actionorder.h"
28 
29 #include <QAction>
30 #include <QMouseEvent>
31 #include "rs_dialogfactory.h"
32 #include "rs_graphicview.h"
33 #include "rs_polyline.h"
34 #include "rs_debug.h"
35 
36 
37 
RS_ActionOrder(RS_EntityContainer & container,RS_GraphicView & graphicView,RS2::ActionType type)38 RS_ActionOrder::RS_ActionOrder(RS_EntityContainer& container,
39         RS_GraphicView& graphicView, RS2::ActionType type)
40         :RS_PreviewActionInterface("Sort Entities",
41 						   container, graphicView)
42 		,targetEntity(nullptr)
43 		,orderType(type)
44 {
45 	actionType=RS2::ActionOrderBottom;
46 }
47 
init(int status)48 void RS_ActionOrder::init(int status) {
49     RS_ActionInterface::init(status);
50 	targetEntity = nullptr;
51     if (orderType == RS2::ActionOrderBottom ||
52             orderType == RS2::ActionOrderTop) {
53         trigger();
54     } else
55         snapMode.restriction = RS2::RestrictNothing;
56 }
57 
trigger()58 void RS_ActionOrder::trigger() {
59     RS_DEBUG->print("RS_ActionOrder::trigger()");
60 
61     QList<RS_Entity *> entList;
62 	for(auto e: *container){
63         if (e->isSelected())
64             entList.append(e);
65     }
66 
67     if (targetEntity) {
68 		int index = -1;
69 		targetEntity->setHighlighted(false);
70         graphicView->drawEntity(targetEntity);
71 
72         switch (orderType) {
73         case RS2::ActionOrderLower:
74             index = document->findEntity(targetEntity);
75             document->moveEntity(index, entList);
76             break;
77         case RS2::ActionOrderRaise:
78             index = document->findEntity(targetEntity)+1;
79             document->moveEntity(index, entList);
80             break;
81         default:
82             break;
83         }
84 		targetEntity = nullptr;
85     } else {
86         switch (orderType) {
87         case RS2::ActionOrderBottom:
88             document->moveEntity(-1, entList);
89             break;
90         case RS2::ActionOrderTop:
91             document->moveEntity(document->count()+1, entList);
92             break;
93         default:
94             break;
95         }
96     }
97     setStatus(getStatus()-1);
98 }
99 
100 
101 
mouseMoveEvent(QMouseEvent * e)102 void RS_ActionOrder::mouseMoveEvent(QMouseEvent* e) {
103     RS_DEBUG->print("RS_ActionOrder::mouseMoveEvent begin");
104 
105     switch (getStatus()) {
106     case ChooseEntity:
107         snapFree(e);
108         break;
109     default:
110         break;
111     }
112 
113     RS_DEBUG->print("RS_ActionOrder::mouseMoveEvent end");
114 }
115 
116 
117 
mouseReleaseEvent(QMouseEvent * e)118 void RS_ActionOrder::mouseReleaseEvent(QMouseEvent* e) {
119     if (e->button()==Qt::LeftButton) {
120         switch (getStatus()) {
121         case ChooseEntity:
122             targetEntity = catchEntity(e);
123 			if (!targetEntity) {
124                 RS_DIALOGFACTORY->commandMessage(tr("No Entity found."));
125             } else {
126                 targetEntity->setHighlighted(true);
127                 graphicView->drawEntity(targetEntity);
128                 graphicView->redraw();
129                 trigger();
130             }
131             break;
132         default:
133             break;
134         }
135     } else if (e->button()==Qt::RightButton) {
136         deleteSnapper();
137         if (targetEntity) {
138             targetEntity->setHighlighted(false);
139             graphicView->drawEntity(targetEntity);
140                 graphicView->redraw();
141         }
142         init(getStatus()-1);
143     }
144     deleteSnapper();
145 }
146 
updateMouseButtonHints()147 void RS_ActionOrder::updateMouseButtonHints() {
148     switch (getStatus()) {
149     case ChooseEntity:
150         RS_DIALOGFACTORY->updateMouseWidget(tr("Choose entity for order"),
151                                             tr("Cancel"));
152         break;
153     default:
154 		RS_DIALOGFACTORY->updateMouseWidget();
155         break;
156     }
157 }
158 
159 
160 
updateMouseCursor()161 void RS_ActionOrder::updateMouseCursor() {
162     graphicView->setMouseCursor(RS2::CadCursor);
163 }
164 
165 
166 // EOF
167