Source

gazebo / gazebo / gui / model_editor / WallItem.cc

Full commit
  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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright 2012 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

#include "gazebo/common/Exception.hh"
#include "gazebo/gui/model_editor/GrabberHandle.hh"
#include "gazebo/gui/model_editor/EditorView.hh"
#include "gazebo/gui/model_editor/EditorItem.hh"
#include "gazebo/gui/model_editor/RectItem.hh"
#include "gazebo/gui/model_editor/BuildingItem.hh"
#include "gazebo/gui/model_editor/LineSegmentItem.hh"
#include "gazebo/gui/model_editor/PolylineItem.hh"
#include "gazebo/gui/model_editor/WallInspectorDialog.hh"
#include "gazebo/gui/model_editor/BuildingMaker.hh"
#include "gazebo/gui/model_editor/WallItem.hh"

using namespace gazebo;
using namespace gui;

/////////////////////////////////////////////////
WallItem::WallItem(const QPointF &_start, const QPointF &_end)
    : PolylineItem(_start, _end), BuildingItem()
{
  this->editorType = "Wall";
  this->scale = BuildingMaker::conversionScale;

  this->level = 0;

  this->wallThickness = 20;
  this->wallHeight = 250;

  this->SetThickness(this->wallThickness);

  this->selectedSegment = NULL;

  this->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
  this->setAcceptHoverEvents(true);

  this->inspector = new WallInspectorDialog();
  this->inspector->setModal(false);
  connect(this->inspector, SIGNAL(Applied()), this, SLOT(OnApply()));

  this->openInspectorAct = new QAction(tr("&Open Wall Inspector"), this);
  this->openInspectorAct->setStatusTip(tr("Open Wall Inspector"));
  connect(this->openInspectorAct, SIGNAL(triggered()),
    this, SLOT(OnOpenInspector()));
  this->deleteItemAct = new QAction(tr("&Delete"), this);
  this->deleteItemAct->setStatusTip(tr("Delete"));
  connect(this->deleteItemAct, SIGNAL(triggered()),
    this, SLOT(OnDeleteItem()));
}

/////////////////////////////////////////////////
WallItem::~WallItem()
{
  delete this->inspector;
}

/////////////////////////////////////////////////
double WallItem::GetHeight() const
{
  return this->wallHeight;
}

/////////////////////////////////////////////////
void WallItem::SetHeight(double _height)
{
  this->wallHeight = _height;
}

/////////////////////////////////////////////////
WallItem *WallItem::Clone() const
{
  WallItem *wallItem = new WallItem(this->scenePos(),
      this->scenePos() + QPointF(1, 0));

  LineSegmentItem *segment = this->segments[0];
  wallItem->SetVertexPosition(0, this->mapToScene(segment->line().p1()));
  wallItem->SetVertexPosition(1, this->mapToScene(segment->line().p2()));


  // TODO: Code below just simiulates the way wall are created through user
  // interactions. There should be a better way of doing this
  for (unsigned int i = 1; i < this->segments.size(); ++i)
  {
    segment = this->segments[i];
    wallItem->AddPoint(this->mapToScene(segment->line().p1()) + QPointF(1, 0));
    wallItem->SetVertexPosition(wallItem->GetVertexCount()-1,
        this->mapToScene(segment->line().p2()));
  }
  wallItem->AddPoint(this->mapToScene(
      this->segments[wallItem->GetSegmentCount()-1]->line().p2())
      + QPointF(1, 0));
  wallItem->PopEndPoint();

  wallItem->SetLevel(this->level);
  wallItem->SetHeight(this->wallHeight);
  wallItem->SetThickness(this->wallThickness);

  return wallItem;
}

/////////////////////////////////////////////////
bool WallItem::GrabberEventFilter(GrabberHandle *_grabber, QEvent *_event)
{
  QGraphicsSceneMouseEvent *mouseEvent =
    dynamic_cast<QGraphicsSceneMouseEvent*>(_event);

  switch (_event->type())
  {
    case QEvent::GraphicsSceneMousePress:
    {
      _grabber->SetMouseState(QEvent::GraphicsSceneMousePress);
      QPointF scenePosition =  _grabber->mapToScene(mouseEvent->pos());

      _grabber->SetMouseDownX(scenePosition.x());
      _grabber->SetMouseDownY(scenePosition.y());
      break;
    }
    case QEvent::GraphicsSceneMouseRelease:
    {
      _grabber->SetMouseState(QEvent::GraphicsSceneMouseRelease);
      break;
    }
    case QEvent::GraphicsSceneMouseMove:
    {
      _grabber->SetMouseState(QEvent::GraphicsSceneMouseMove);
      break;
    }
    case QEvent::GraphicsSceneHoverEnter:
    case QEvent::GraphicsSceneHoverMove:
    {
      QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
      return true;
    }
    case QEvent::GraphicsSceneHoverLeave:
    {
      QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor));
      return true;
    }
    default:
    {
      break;
    }
  }

  if (!mouseEvent)
    return false;

  if (_grabber->GetMouseState() == QEvent::GraphicsSceneMouseMove)
  {
    QPointF scenePosition = _grabber->mapToScene(mouseEvent->pos());
    int grabberIndex = _grabber->GetIndex();

    // Snap wall rotations to fixed size increments
    QPointF newScenePos = scenePosition;
    if (!(QApplication::keyboardModifiers() & Qt::ShiftModifier))
    {
      LineSegmentItem *segment = this->selectedSegment;
      QPointF lineOrigin = segment->line().p1();

      if (segment->GetIndex() == grabberIndex)
        lineOrigin = segment->line().p2();

      QLineF lineToPoint(lineOrigin, segment->mapFromScene(scenePosition));
      QPointF startScenePoint = segment->mapToScene(lineOrigin);

      double angle = QLineF(startScenePoint, scenePosition).angle();
      double range = 7.5;
      int increment = angle / range;

      if ((angle - range*increment) > range/2)
        increment++;

      angle = -range*increment;
      double lineLength = lineToPoint.length();

      newScenePos.setX(startScenePoint.x() + cos(GZ_DTOR(angle))*lineLength);
      newScenePos.setY(startScenePoint.y() + sin(GZ_DTOR(angle))*lineLength);
    }

    this->SetVertexPosition(grabberIndex, newScenePos);
    this->update();

    // re-align child items when a vertex is moved
    for (int i = grabberIndex; i > grabberIndex - 2; --i)
    {
      if ((i - this->GetSegmentCount()) != 0 && i >= 0)
      {
        this->UpdateSegmentChildren(this->GetSegment(i));
      }
    }
  }
  return true;
}

/////////////////////////////////////////////////
bool WallItem::SegmentEventFilter(LineSegmentItem *_segment, QEvent *_event)
{
  QGraphicsSceneMouseEvent *mouseEvent =
    dynamic_cast<QGraphicsSceneMouseEvent*>(_event);

  QPointF scenePosition;
  if (mouseEvent)
    scenePosition = mouseEvent->scenePos();

  switch (_event->type())
  {
    case QEvent::GraphicsSceneMousePress:
    {
      _segment->SetMouseState(QEvent::GraphicsSceneMousePress);
      _segment->SetMouseDownX(scenePosition.x());
      _segment->SetMouseDownY(scenePosition.y());

      this->setSelected(true);
      this->SetSegmentSelected(_segment->GetIndex(), true);
      this->segmentMouseMove = scenePosition;
      break;
    }
    case QEvent::GraphicsSceneMouseRelease:
    {
      _segment->SetMouseState(QEvent::GraphicsSceneMouseRelease);
      break;
    }
    case QEvent::GraphicsSceneMouseMove:
    {
      _segment->SetMouseState(QEvent::GraphicsSceneMouseMove);
      break;
    }
    case QEvent::GraphicsSceneContextMenu:
    {
      this->SetSegmentSelected(_segment->GetIndex(), true);
      QMenu menu;
      menu.addAction(this->openInspectorAct);
      menu.addAction(this->deleteItemAct);
      menu.exec(dynamic_cast<QGraphicsSceneContextMenuEvent*>(
          _event)->screenPos());
      return true;
    }
    case QEvent::GraphicsSceneMouseDoubleClick:
    {
      this->SetSegmentSelected(_segment->GetIndex(), true);
      this->OnOpenInspector();
      _segment->SetMouseState(QEvent::GraphicsSceneMouseDoubleClick);
      break;
    }
    case QEvent::GraphicsSceneHoverEnter:
    case QEvent::GraphicsSceneHoverMove:
    {
      if (_segment->isSelected())
        QApplication::setOverrideCursor(QCursor(Qt::SizeAllCursor));
      return true;
    }
    case QEvent::GraphicsSceneHoverLeave:
    {
      if (_segment->isSelected())
        QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor));
      return true;
    }
    default:
    {
      break;
    }
  }

  if (!mouseEvent)
    return false;

  if (_segment->GetMouseState() == QEvent::GraphicsSceneMouseMove)
  {
    QPointF trans = scenePosition - segmentMouseMove;

    this->TranslateVertex(_segment->GetIndex(), trans);
    this->TranslateVertex(_segment->GetIndex() + 1, trans);

    this->segmentMouseMove = scenePosition;

    this->update();

    // re-align child items when a segment is moved
    QList<QGraphicsItem *> children = _segment->childItems();
    for ( int i = 0; i < children.size(); ++i)
      children[i]->moveBy(trans.x(), trans.y());
    for (int i = _segment->GetIndex() - 1; i <= _segment->GetIndex() + 1; i+=2)
    {
      if ((i - this->GetSegmentCount()) != 0 && i >= 0)
      {
        this->UpdateSegmentChildren(this->GetSegment(i));
      }
    }
  }
  return true;
}

/////////////////////////////////////////////////
void WallItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *_event)
{
  _event->ignore();
}

/////////////////////////////////////////////////
void WallItem::OnOpenInspector()
{
  if (!this->selectedSegment)
    return;
  QLineF line = this->selectedSegment->line();
  double segmentLength = line.length() + this->wallThickness;
  QPointF segmentStartPoint = this->mapToScene(line.p1());
  QPointF segmentEndPoint = this->mapToScene(line.p2());

  this->inspector->SetName(this->GetName());
  this->inspector->SetThickness(this->wallThickness * this->scale);
  this->inspector->SetHeight(this->wallHeight * this->scale);
  this->inspector->SetLength(segmentLength * this->scale);
  QPointF startPos = segmentStartPoint * this->scale;
  startPos.setY(-startPos.y());
  this->inspector->SetStartPosition(startPos);
  QPointF endPos = segmentEndPoint * this->scale;
  endPos.setY(-endPos.y());
  this->inspector->SetEndPosition(endPos);
  this->inspector->show();
}

/////////////////////////////////////////////////
void WallItem::OnDeleteItem()
{
  if (!this->selectedSegment)
    return;

  dynamic_cast<EditorView *>(this->scene()->views()[0])->DeleteItem(
      this->selectedSegment);
}

/////////////////////////////////////////////////
void WallItem::OnApply()
{
  WallInspectorDialog *dialog =
     qobject_cast<WallInspectorDialog *>(QObject::sender());

  QLineF line = this->selectedSegment->line();
  double segmentLength = line.length() + this->wallThickness;
  this->wallThickness = dialog->GetThickness() / this->scale;
  this->SetThickness(this->wallThickness);
  this->wallHeight = dialog->GetHeight() / this->scale;
  this->WallChanged();

  double newLength = dialog->GetLength() / this->scale;

  // The if statement below limits the change to either the length of
  // the wall segment or its start/end pos.
  // Comparison between doubles up to 1 decimal place
  if (fabs(newLength - segmentLength) > 0.1)
  {
    newLength = std::max(newLength - this->wallThickness, 1.0);
    line.setLength(newLength);
    this->SetVertexPosition(this->selectedSegment->GetIndex()+1,
        this->mapToScene(line.p2()));
  }
  else
  {
    QPointF newStartPoint = dialog->GetStartPosition() / this->scale;
    newStartPoint.setY(-newStartPoint.y());
    QPointF newEndPoint = dialog->GetEndPosition() / this->scale;
    newEndPoint.setY(-newEndPoint.y());

    this->SetVertexPosition(this->selectedSegment->GetIndex(),
      newStartPoint);
    this->SetVertexPosition(this->selectedSegment->GetIndex() + 1,
      newEndPoint);
  }
}

/////////////////////////////////////////////////
void WallItem::SetSegmentSelected(unsigned int _index, bool _selected)
{
  if (_index >= this->segments.size())
    gzthrow("Index too large")

  if (_selected && this->selectedSegment)
  {
    unsigned int oldIndex = this->selectedSegment->GetIndex();
    this->selectedSegment->setSelected(false);
    this->grabbers[oldIndex]->setVisible(false);
    this->grabbers[oldIndex+1]->setVisible(false);
  }
  this->selectedSegment = this->segments[_index];
  this->grabbers[_index]->setVisible(_selected);
  this->grabbers[_index+1]->setVisible(_selected);
  this->selectedSegment->setSelected(_selected);
}

/////////////////////////////////////////////////
void WallItem::UpdateSegmentChildren(LineSegmentItem *_segment)
{
  QList<QGraphicsItem *> children = _segment->childItems();
  for (int j = 0; j < children.size(); ++j)
  {
    // TODO find a more generic way than casting child as rect item,
    // and need to keep wall-children pos ratio fixed
    RectItem *rectItem = dynamic_cast<RectItem *>(children[j]);
    if (rectItem)
    {
      rectItem->SetRotation(-_segment->line().angle());
      QPointF delta = rectItem->pos() - _segment->line().p1();
      QPointF deltaLine = _segment->line().p2() - _segment->line().p1();
      double deltaRatio = sqrt(delta.x()*delta.x() + delta.y()*delta.y())
          / _segment->line().length();
      rectItem->setPos(_segment->line().p1() + deltaRatio*deltaLine);
    }
  }
}

/////////////////////////////////////////////////
void WallItem::WallChanged()
{
  emit DepthChanged(this->wallThickness);
  emit HeightChanged(this->wallHeight);
}

/////////////////////////////////////////////////
void WallItem::Update()
{
  this->WallChanged();
  PolylineItem::Update();
}