| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Thomas Anderson <blobfish[at]gmx.com> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <cassert> |
| 24 |
|
| 25 |
#include <Inventor/SbRotation.h> |
| 26 |
#include <Inventor/nodes/SoBaseColor.h> |
| 27 |
#include <Inventor/nodes/SoSeparator.h> |
| 28 |
#include <Inventor/nodes/SoTranslation.h> |
| 29 |
#include <Inventor/draggers/SoDragger.h> |
| 30 |
#include <Inventor/nodekits/SoSubKit.h> |
| 31 |
|
| 32 |
#include "SoLinearDragger.h" |
| 33 |
|
| 34 |
#include <Base/Quantity.h> |
| 35 |
#include <Gui/Inventor/SoToggleSwitch.h> |
| 36 |
#include <Gui/MainWindow.h> |
| 37 |
#include <Gui/SoTextLabel.h> |
| 38 |
#include <Gui/Utilities.h> |
| 39 |
|
| 40 |
#include "SoLinearDraggerGeometry.h" |
| 41 |
|
| 42 |
using namespace Gui; |
| 43 |
|
| 44 |
SO_KIT_SOURCE(SoLinearDragger) |
| 45 |
|
| 46 |
void SoLinearDragger::initClass() |
| 47 |
{ |
| 48 |
SO_KIT_INIT_CLASS(SoLinearDragger, SoDragger, "Dragger"); |
| 49 |
} |
| 50 |
|
| 51 |
SoLinearDragger::SoLinearDragger() |
| 52 |
{ |
| 53 |
SO_KIT_CONSTRUCTOR(SoLinearDragger); |
| 54 |
|
| 55 |
#if defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) |
| 56 |
this->ref(); |
| 57 |
#endif |
| 58 |
|
| 59 |
SO_KIT_ADD_CATALOG_ENTRY(baseGeomSwitch, SoToggleSwitch, true, topSeparator, motionMatrix, true); |
| 60 |
SO_KIT_ADD_CATALOG_ABSTRACT_ENTRY( |
| 61 |
baseGeom, |
| 62 |
SoLinearGeometryBaseKit, |
| 63 |
SoArrowBase, |
| 64 |
true, |
| 65 |
baseGeomSwitch, |
| 66 |
"", |
| 67 |
true |
| 68 |
); |
| 69 |
|
| 70 |
FC_ADD_CATALOG_ENTRY(baseColor, SoBaseColor, geomSeparator); |
| 71 |
FC_ADD_CATALOG_ENTRY(activeSwitch, SoToggleSwitch, geomSeparator); |
| 72 |
FC_ADD_CATALOG_ENTRY(secondaryColor, SoBaseColor, activeSwitch); |
| 73 |
FC_ADD_CATALOG_ENTRY(labelSwitch, SoToggleSwitch, geomSeparator); |
| 74 |
FC_ADD_CATALOG_ENTRY(labelSeparator, SoSeparator, labelSwitch); |
| 75 |
FC_ADD_CATALOG_ENTRY(scale, SoScale, geomSeparator); |
| 76 |
SO_KIT_ADD_CATALOG_ABSTRACT_ENTRY( |
| 77 |
arrow, |
| 78 |
SoLinearGeometryKit, |
| 79 |
SoArrowGeometry, |
| 80 |
true, |
| 81 |
geomSeparator, |
| 82 |
"", |
| 83 |
true |
| 84 |
); |
| 85 |
|
| 86 |
SO_KIT_ADD_FIELD(translation, (0.0, 0.0, 0.0)); |
| 87 |
SO_KIT_ADD_FIELD(translationIncrement, (1.0)); |
| 88 |
SO_KIT_ADD_FIELD(translationIncrementCount, (0)); |
| 89 |
SO_KIT_ADD_FIELD(autoScaleResult, (1.0)); |
| 90 |
SO_KIT_ADD_FIELD(color, (1, 0, 0)); |
| 91 |
SO_KIT_ADD_FIELD(activeColor, (1, 1, 0)); |
| 92 |
SO_KIT_ADD_FIELD(labelVisible, (1)); |
| 93 |
SO_KIT_ADD_FIELD(geometryScale, (1, 1, 1)); |
| 94 |
SO_KIT_ADD_FIELD(active, (false)); |
| 95 |
SO_KIT_ADD_FIELD(baseGeomVisible, (false)); |
| 96 |
|
| 97 |
SO_KIT_INIT_INSTANCE(); |
| 98 |
|
| 99 |
setPart("baseColor", buildColor()); |
| 100 |
setPart("labelSeparator", buildLabelGeometry()); |
| 101 |
setPart("secondaryColor", buildActiveColor()); |
| 102 |
|
| 103 |
auto sw = SO_GET_ANY_PART(this, "labelSwitch", SoToggleSwitch); |
| 104 |
sw->on.connectFrom(&labelVisible); |
| 105 |
|
| 106 |
auto scale = SO_GET_ANY_PART(this, "scale", SoScale); |
| 107 |
scale->scaleFactor.connectFrom(&geometryScale); |
| 108 |
|
| 109 |
this->addStartCallback(&SoLinearDragger::startCB); |
| 110 |
this->addMotionCallback(&SoLinearDragger::motionCB); |
| 111 |
this->addFinishCallback(&SoLinearDragger::finishCB); |
| 112 |
|
| 113 |
addValueChangedCallback(&SoLinearDragger::valueChangedCB); |
| 114 |
|
| 115 |
fieldSensor.setFunction(&SoLinearDragger::fieldSensorCB); |
| 116 |
fieldSensor.setData(this); |
| 117 |
fieldSensor.setPriority(0); |
| 118 |
|
| 119 |
this->setUpConnections(TRUE, TRUE); |
| 120 |
|
| 121 |
sw = SO_GET_ANY_PART(this, "activeSwitch", SoToggleSwitch); |
| 122 |
sw->on.connectFrom(&active); |
| 123 |
|
| 124 |
sw = SO_GET_ANY_PART(this, "baseGeomSwitch", SoToggleSwitch); |
| 125 |
sw->on.connectFrom(&baseGeomVisible); |
| 126 |
} |
| 127 |
|
| 128 |
SoLinearDragger::~SoLinearDragger() |
| 129 |
{ |
| 130 |
fieldSensor.setData(nullptr); |
| 131 |
fieldSensor.detach(); |
| 132 |
|
| 133 |
this->removeStartCallback(&SoLinearDragger::startCB); |
| 134 |
this->removeMotionCallback(&SoLinearDragger::motionCB); |
| 135 |
this->removeFinishCallback(&SoLinearDragger::finishCB); |
| 136 |
removeValueChangedCallback(&SoLinearDragger::valueChangedCB); |
| 137 |
} |
| 138 |
|
| 139 |
SoSeparator* SoLinearDragger::buildLabelGeometry() |
| 140 |
{ |
| 141 |
auto labelSeparator = new SoSeparator; |
| 142 |
|
| 143 |
auto labelTranslation = new SoTranslation(); |
| 144 |
labelSeparator->addChild(labelTranslation); |
| 145 |
|
| 146 |
auto arrow = SO_GET_PART(this, "arrow", SoLinearGeometryKit); |
| 147 |
labelTranslation->translation.connectFrom(&arrow->tipPosition); |
| 148 |
|
| 149 |
auto label = new SoFrameLabel(); |
| 150 |
label->string.connectFrom(&this->label); |
| 151 |
label->textColor.setValue(1.0, 1.0, 1.0); |
| 152 |
label->horAlignment = SoImage::CENTER; |
| 153 |
label->vertAlignment = SoImage::HALF; |
| 154 |
label->border = false; |
| 155 |
label->backgroundUseBaseColor = true; |
| 156 |
labelSeparator->addChild(label); |
| 157 |
|
| 158 |
return labelSeparator; |
| 159 |
} |
| 160 |
|
| 161 |
SoBaseColor* SoLinearDragger::buildActiveColor() |
| 162 |
{ |
| 163 |
auto color = new SoBaseColor; |
| 164 |
color->rgb.connectFrom(&activeColor); |
| 165 |
|
| 166 |
return color; |
| 167 |
} |
| 168 |
|
| 169 |
SoBaseColor* SoLinearDragger::buildColor() |
| 170 |
{ |
| 171 |
auto color = new SoBaseColor; |
| 172 |
color->rgb.connectFrom(&this->color); |
| 173 |
|
| 174 |
return color; |
| 175 |
} |
| 176 |
|
| 177 |
void SoLinearDragger::startCB(void*, SoDragger* d) |
| 178 |
{ |
| 179 |
auto sudoThis = static_cast<SoLinearDragger*>(d); |
| 180 |
assert(sudoThis); |
| 181 |
sudoThis->dragStart(); |
| 182 |
} |
| 183 |
|
| 184 |
void SoLinearDragger::motionCB(void*, SoDragger* d) |
| 185 |
{ |
| 186 |
auto sudoThis = static_cast<SoLinearDragger*>(d); |
| 187 |
assert(sudoThis); |
| 188 |
sudoThis->drag(); |
| 189 |
} |
| 190 |
|
| 191 |
void SoLinearDragger::finishCB(void*, SoDragger* d) |
| 192 |
{ |
| 193 |
auto sudoThis = static_cast<SoLinearDragger*>(d); |
| 194 |
assert(sudoThis); |
| 195 |
sudoThis->dragFinish(); |
| 196 |
} |
| 197 |
|
| 198 |
void SoLinearDragger::fieldSensorCB(void* f, SoSensor*) |
| 199 |
{ |
| 200 |
auto sudoThis = static_cast<SoLinearDragger*>(f); |
| 201 |
|
| 202 |
if (!f) { |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
SbMatrix matrix = sudoThis->getMotionMatrix(); // clazy:exclude=rule-of-two-soft |
| 207 |
sudoThis->workFieldsIntoTransform(matrix); |
| 208 |
sudoThis->setMotionMatrix(matrix); |
| 209 |
} |
| 210 |
|
| 211 |
void SoLinearDragger::valueChangedCB(void*, SoDragger* d) |
| 212 |
{ |
| 213 |
auto sudoThis = dynamic_cast<SoLinearDragger*>(d); |
| 214 |
assert(sudoThis); |
| 215 |
SbMatrix matrix = sudoThis->getMotionMatrix(); // clazy:exclude=rule-of-two-soft |
| 216 |
SbVec3f trans = getMatrixTransform(matrix).translation; |
| 217 |
|
| 218 |
sudoThis->fieldSensor.detach(); |
| 219 |
if (sudoThis->translation.getValue() != trans) { |
| 220 |
sudoThis->translation = trans; |
| 221 |
} |
| 222 |
sudoThis->fieldSensor.attach(&sudoThis->translation); |
| 223 |
} |
| 224 |
|
| 225 |
void SoLinearDragger::dragStart() |
| 226 |
{ |
| 227 |
active = true; |
| 228 |
|
| 229 |
// do an initial projection to eliminate discrepancies |
| 230 |
// in arrow head pick. we define the arrow in the y+ direction |
| 231 |
// and we know local space will be relative to this. so y vector |
| 232 |
// line projection will work. |
| 233 |
projector.setViewVolume(this->getViewVolume()); |
| 234 |
projector.setWorkingSpace(this->getLocalToWorldMatrix()); |
| 235 |
projector.setLine(SbLine(SbVec3f(0.0, 0.0, 0.0), SbVec3f(0.0, 1.0, 0.0))); |
| 236 |
SbVec3f hitPoint = projector.project(getNormalizedLocaterPosition()); |
| 237 |
|
| 238 |
projector.setLine(SbLine(SbVec3f(0.0, 0.0, 0.0), hitPoint)); |
| 239 |
|
| 240 |
SbMatrix localToWorld = getLocalToWorldMatrix(); |
| 241 |
localToWorld.multVecMatrix(hitPoint, hitPoint); |
| 242 |
setStartingPoint((hitPoint)); |
| 243 |
|
| 244 |
translationIncrementCount.setValue(0); |
| 245 |
} |
| 246 |
|
| 247 |
void SoLinearDragger::drag() |
| 248 |
{ |
| 249 |
projector.setViewVolume(this->getViewVolume()); |
| 250 |
projector.setWorkingSpace(this->getLocalToWorldMatrix()); |
| 251 |
|
| 252 |
SbVec3f hitPoint = projector.project(getNormalizedLocaterPosition()); |
| 253 |
SbVec3f startingPoint = getLocalStartingPoint(); |
| 254 |
SbVec3f localMovement = hitPoint - startingPoint; |
| 255 |
|
| 256 |
// scale the increment to match local space. |
| 257 |
float scaledIncrement = static_cast<float>(translationIncrement.getValue()) |
| 258 |
/ autoScaleResult.getValue(); |
| 259 |
|
| 260 |
localMovement = roundTranslation(localMovement, scaledIncrement); |
| 261 |
// when the movement vector is null either the appendTranslation or |
| 262 |
// the setMotionMatrix doesn't work. either way it stops translating |
| 263 |
// back to its initial starting point. |
| 264 |
if (localMovement.equals(SbVec3f(0.0, 0.0, 0.0), 0.00001f)) { |
| 265 |
setMotionMatrix(getStartMotionMatrix()); |
| 266 |
// don't know why I need the following but if I don't have it |
| 267 |
// it won't return to original position. |
| 268 |
this->valueChanged(); |
| 269 |
} |
| 270 |
else { |
| 271 |
setMotionMatrix(appendTranslation(getStartMotionMatrix(), localMovement)); |
| 272 |
} |
| 273 |
|
| 274 |
Base::Quantity quantity( |
| 275 |
static_cast<double>(translationIncrementCount.getValue()) * translationIncrement.getValue(), |
| 276 |
Base::Unit::Length |
| 277 |
); |
| 278 |
|
| 279 |
QString message = QStringLiteral("%1 %2").arg( |
| 280 |
QObject::tr("Translation:"), |
| 281 |
QString::fromStdString(quantity.getUserString()) |
| 282 |
); |
| 283 |
getMainWindow()->showMessage(message, 3000); |
| 284 |
} |
| 285 |
|
| 286 |
void SoLinearDragger::dragFinish() |
| 287 |
{ |
| 288 |
active = false; |
| 289 |
} |
| 290 |
|
| 291 |
SbBool SoLinearDragger::setUpConnections(SbBool onoff, SbBool doitalways) |
| 292 |
{ |
| 293 |
if (!doitalways && this->connectionsSetUp == onoff) { |
| 294 |
return onoff; |
| 295 |
} |
| 296 |
|
| 297 |
SbBool oldval = this->connectionsSetUp; |
| 298 |
|
| 299 |
if (onoff) { |
| 300 |
inherited::setUpConnections(onoff, doitalways); |
| 301 |
SoLinearDragger::fieldSensorCB(this, nullptr); |
| 302 |
if (this->fieldSensor.getAttachedField() != &this->translation) { |
| 303 |
this->fieldSensor.attach(&this->translation); |
| 304 |
} |
| 305 |
} |
| 306 |
else { |
| 307 |
if (this->fieldSensor.getAttachedField()) { |
| 308 |
this->fieldSensor.detach(); |
| 309 |
} |
| 310 |
inherited::setUpConnections(onoff, doitalways); |
| 311 |
} |
| 312 |
this->connectionsSetUp = onoff; |
| 313 |
return oldval; |
| 314 |
} |
| 315 |
|
| 316 |
SbVec3f SoLinearDragger::roundTranslation(const SbVec3f& vecIn, float incrementIn) |
| 317 |
{ |
| 318 |
// everything is transformed into local space. That means we only have |
| 319 |
// worry about the y-value. |
| 320 |
|
| 321 |
int yCount = 0; |
| 322 |
float yValue = vecIn[1]; |
| 323 |
|
| 324 |
if (fabs(yValue) > (incrementIn / 2.0)) { |
| 325 |
yCount = static_cast<int>(yValue / incrementIn); |
| 326 |
float remainder = fmod(yValue, incrementIn); |
| 327 |
if (remainder >= (incrementIn / 2.0)) { |
| 328 |
yCount++; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
translationIncrementCount.setValue(yCount); |
| 333 |
|
| 334 |
SbVec3f out; |
| 335 |
out[0] = 0; |
| 336 |
out[1] = static_cast<float>(yCount) * incrementIn; |
| 337 |
out[2] = 0.0; |
| 338 |
|
| 339 |
return out; |
| 340 |
} |
| 341 |
|
| 342 |
void SoLinearDragger::instantiateBaseGeometry() |
| 343 |
{ |
| 344 |
baseGeomVisible = true; |
| 345 |
|
| 346 |
auto baseGeom = SO_GET_ANY_PART(this, "baseGeom", SoLinearGeometryBaseKit); |
| 347 |
baseGeom->geometryScale.connectFrom(&geometryScale); |
| 348 |
baseGeom->translation.connectFrom(&translation); |
| 349 |
baseGeom->active.connectFrom(&active); |
| 350 |
} |
| 351 |
|
| 352 |
SO_KIT_SOURCE(SoLinearDraggerContainer) |
| 353 |
|
| 354 |
void SoLinearDraggerContainer::initClass() |
| 355 |
{ |
| 356 |
SoLinearDragger::initClass(); |
| 357 |
SO_KIT_INIT_CLASS(SoLinearDraggerContainer, SoInteractionKit, "InteractionKit"); |
| 358 |
} |
| 359 |
|
| 360 |
SoLinearDraggerContainer::SoLinearDraggerContainer() |
| 361 |
{ |
| 362 |
SO_KIT_CONSTRUCTOR(SoLinearDraggerContainer); |
| 363 |
|
| 364 |
#if defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) |
| 365 |
this->ref(); |
| 366 |
#endif |
| 367 |
|
| 368 |
FC_ADD_CATALOG_ENTRY(draggerSwitch, SoToggleSwitch, geomSeparator); |
| 369 |
FC_ADD_CATALOG_ENTRY(transform, SoTransform, draggerSwitch); |
| 370 |
FC_ADD_CATALOG_ENTRY(dragger, SoLinearDragger, draggerSwitch); |
| 371 |
|
| 372 |
SO_KIT_ADD_FIELD(rotation, (0, 0, 0, 0)); |
| 373 |
SO_KIT_ADD_FIELD(color, (0, 0, 0)); |
| 374 |
SO_KIT_ADD_FIELD(translation, (0, 0, 0)); |
| 375 |
SO_KIT_ADD_FIELD(visible, (1)); |
| 376 |
|
| 377 |
SO_KIT_INIT_INSTANCE(); |
| 378 |
|
| 379 |
setPart("transform", buildTransform()); |
| 380 |
|
| 381 |
auto sw = SO_GET_ANY_PART(this, "draggerSwitch", SoToggleSwitch); |
| 382 |
sw->on.connectFrom(&visible); |
| 383 |
|
| 384 |
getDragger()->color.connectFrom(&color); |
| 385 |
} |
| 386 |
|
| 387 |
SoTransform* SoLinearDraggerContainer::buildTransform() |
| 388 |
{ |
| 389 |
auto transform = new SoTransform; |
| 390 |
transform->translation.connectFrom(&this->translation); |
| 391 |
transform->rotation.connectFrom(&this->rotation); |
| 392 |
|
| 393 |
return transform; |
| 394 |
} |
| 395 |
|
| 396 |
SoLinearDragger* SoLinearDraggerContainer::getDragger() |
| 397 |
{ |
| 398 |
return SO_GET_PART(this, "dragger", SoLinearDragger); |
| 399 |
} |
| 400 |
|
| 401 |
SbVec3f SoLinearDraggerContainer::getPointerDirection() |
| 402 |
{ |
| 403 |
// This is the direction along which the SoLinearDragger points in it local space |
| 404 |
SbVec3f draggerDir = SO_GET_ANY_PART(this, "arrow", SoLinearGeometryKit)->tipPosition.getValue(); |
| 405 |
rotation.getValue().multVec(draggerDir, draggerDir); |
| 406 |
|
| 407 |
return draggerDir; |
| 408 |
} |
| 409 |
|
| 410 |
void SoLinearDraggerContainer::setPointerDirection(const SbVec3f& dir) |
| 411 |
{ |
| 412 |
// This is the direction from the origin to the tip of the dragger |
| 413 |
SbVec3f draggerDir = SO_GET_ANY_PART(this, "arrow", SoLinearGeometryKit)->tipPosition.getValue(); |
| 414 |
|
| 415 |
SbRotation rot {draggerDir, dir}; |
| 416 |
rotation.setValue(rot); |
| 417 |
} |