-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalignmentview.cpp
271 lines (225 loc) · 8.62 KB
/
alignmentview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* alignmentview.cpp
*
* (Coincident) Alignment Input class
* Copyright (C) 2002 lignum Computing, Inc. <[email protected]>
* $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <qaction.h>
#include <QMenu>
#include <qevent.h>
#include <qcursor.h>
#include <qwhatsthis.h>
#include "command.h"
#include "lignumcadmainwindow.h"
#include "cursorfactory.h"
#include "openglview.h"
#include "pageview.h"
#include "figureview.h"
#include "line.h"
#include "alignmentview.h"
namespace Space2D {
/*
* Simple constructor.
*/
AlignmentCreateInput::AlignmentCreateInput ( PageView* parent )
: parent_( parent ), from_( 0 ), to_( 0 ), xml_rep_( 0 )
{}
/*
*Destructor.
*/
AlignmentCreateInput::~AlignmentCreateInput ( void )
{
// Clean up the undo/redo command representation if necessary.
if ( xml_rep_ != 0 ) delete xml_rep_;
}
/*
* Prep for operation.
*/
void AlignmentCreateInput::startDisplay ( QMenu* /*context_menu*/ )
{
from_ = to_ = 0;
xml_rep_ = new QDomDocument;
QAction* cancel_action = parent_->lCMW()->getUi()->cancelAlignmentAction;
separator_id_ = parent_->view()->contextMenu()->addSeparator();
parent_->view()->contextMenu()->addAction( cancel_action );
connect( cancel_action, SIGNAL( activated() ), SLOT( cancelOperation() ) );
parent_->view()->
setCursor( CursorFactory::instance().cursor( CursorFactory::ALIGNMENT ) );
}
/*
* Actions for each geometry pick.
*/
void AlignmentCreateInput::mousePress ( QMouseEvent* /*me*/,
const SelectedNames& selected )
{
// This is in pick mode, so we only consider the first geometry
// selected (rather than all of them like we had...)
// If the user just clicks randomly, ignore it, but stay in pick mode.
if ( selected.empty() )
return;
SelectedNames::const_iterator f = selected.begin();
// This shouldn't happen, but we can ignore it and stay in pick mode.
if ( (*f).second.size() < 2 )
return;
FigureView* fv =
dynamic_cast< FigureView* >( parent_->figureSelectionNames()[ (*f).second[0] ].get());
GLuint g = (*f).second[1]; // (*(*f).second->begin()).first;
ConstrainedLine* line = dynamic_cast< ConstrainedLine* >( fv->geometry( g ) );
if ( line != 0 ) {
if ( from_ == 0 ) {
from_ = line;
parent_->view()->
setCursor( CursorFactory::instance().cursor( CursorFactory::ALIGNMENTPLUS ) );
}
else {
// Make a few basic checks to see if this comensurate with from_
if ( line == from_ ) {
QWhatsThis::showText( QCursor::pos(),
"<table cellpadding=10><tr>"
"<td><img source=\"not_allowed.png\"></td>"
"<td width=\"80%\">Cannot align to self.</td>"
"</tr></table>" );
return;
}
if ( fabs( fabs( line->e() * from_->e() ) - 1. ) > lC::EPSILON ) {
QWhatsThis::showText( QCursor::pos(),
"<table cellpadding=10><tr>"
"<td><img source=\"not_allowed.png\"></td>"
"<td width=\"80%\">Cannot align non-parallel lines.</td>"
"</tr></table>" );
return;
}
if ( from_->parent() == line->parent() ) {
QWhatsThis::showText( QCursor::pos(),
"<table cellpadding=10><tr>"
"<td><img source=\"not_allowed.png\"></td>"
"<td width=\"80%\">Cannot align two edges in the same figure.</td>"
"</tr></table>" );
return;
}
if ( line->dependsOn( from_ ) ) {
QWhatsThis::showText( QCursor::pos(),
"<table cellpadding=10><tr>"
"<td><img source=\"not_allowed.png\"></td>"
"<td width=\"80%\">Alignment would create circular reference.</td>"
"</tr></table>" );
return;
}
to_ = line;
}
}
}
/*
* If the user is done, perform the reconstaint.
*/
bool AlignmentCreateInput::mouseRelease ( QMouseEvent* /*me*/,
const SelectedNames& /*selected*/ )
{
if ( to_ == 0 )
return false;
QDomElement reconstraints = xml_rep_->createElement( lC::STR::RECONSTRAINTS );
xml_rep_->appendChild( reconstraints );
QDomElement old_constraints = xml_rep_->createElement(lC::STR::OLD_CONSTRAINTS);
reconstraints.appendChild( old_constraints );
QDomElement old_xml = xml_rep_->createElement( lC::STR::CONSTRAINED_LINE );
old_xml.setAttribute( lC::STR::URL, from_->dbURL().toString() );
from_->constraint()->write( old_xml );
old_constraints.appendChild( old_xml );
// Making this contraint can, in some cases, cause the target
// line's former reference to be reconstrained. This happens at
// least in Rectangle in order to keep a width- or height-like
// dimension. In undoing this operation, that cascade change must
// be done first in order to avoid a circular reference. How do
// we know it's going to happen? I guess we have to look at it
// before and after...
ConstrainedLine* from_reference =
dynamic_cast<ConstrainedLine*>( from_->reference() );
if ( from_reference != 0 ) {
from_ref_xml_ = xml_rep_->createElement( lC::STR::CONSTRAINED_LINE );
from_ref_xml_.setAttribute( lC::STR::URL, from_reference->dbURL().toString() );
from_reference->constraint()->write( from_ref_xml_ );
// If from_reference_'s modifiedConstraint signal is emitted,
// then the old XML representation of its constraint is appended
// to this reconstrain command (done in the SLOT).
connect( from_reference, SIGNAL( modifiedConstraint() ),
SLOT( cascadeConstraints() ) );
}
// It is now possible to just create this contraint and the affected
// figures will react to signals from the modified line.
from_->setConstraint( new Coincident( to_ ) );
if ( from_reference != 0 )
disconnect( from_reference, SIGNAL( modifiedConstraint() ),
this, SLOT( cascadeConstraints() ) );
QDomElement new_constraints = xml_rep_->createElement( lC::STR::NEW_CONSTRAINTS );
reconstraints.appendChild( new_constraints );
QDomElement new_xml = xml_rep_->createElement( lC::STR::CONSTRAINED_LINE );
new_xml.setAttribute( lC::STR::URL, from_->dbURL().toString() );
from_->constraint()->write( new_xml );
new_constraints.appendChild( new_xml );
CommandHistory::instance().
addCommand( new ReconstrainCommand( "reconstrain line",
parent_->model(),
xml_rep_ ) );
xml_rep_ = 0;
QAction* cancel_action = parent_->lCMW()->getUi()->cancelAlignmentAction;
cancel_action->disconnect();
parent_->view()->contextMenu()->removeAction( cancel_action );
parent_->view()->contextMenu()->removeAction( separator_id_ );
parent_->view()->unsetCursor();
parent_->figureComplete();
return true;
}
/*
* Cancel the operation if the user presses the Escape key.
*/
void AlignmentCreateInput::keyPress ( QKeyEvent* ke )
{
if ( ke->key() == Qt::Key_Escape )
cancelOperation();
else
ke->ignore();
}
/*
* Cancel the coincident constraint creation.
*/
void AlignmentCreateInput::cancelOperation ( void )
{
// Normally, the Command would take responsibility for the XML
// representation, but if the aligment creation is cancelled, it's never
// passed along.
delete xml_rep_;
xml_rep_ = 0;
QAction* cancel_action = parent_->lCMW()->getUi()->cancelAlignmentAction;
cancel_action->disconnect();
parent_->view()->contextMenu()->removeAction( cancel_action );
parent_->view()->contextMenu()->removeAction( separator_id_ );
parent_->cancelOperation();
}
/*
* Pick up any additional constraints which are changed as result of this
* operation.
*/
void AlignmentCreateInput::cascadeConstraints ( void )
{
// The primary reconstraint target is buried deep in this representation.
// This change has to go before it so it can be undone properly.
xml_rep_->firstChild().firstChild().insertBefore( from_ref_xml_,
xml_rep_->firstChild().firstChild().firstChild() );
}
} // End of Space2D namespace