RulerPlugin.cc 12.2 KB
Newer Older
Matthias Möller's avatar
Matthias Möller committed
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Matthias Möller's avatar
Matthias Möller committed
39 40 41 42 43
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
Matthias Möller's avatar
Matthias Möller committed
44 45 46
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
Matthias Möller's avatar
Matthias Möller committed
47 48 49 50 51 52 53
*                                                                            *
\*===========================================================================*/


#include "RulerPlugin.hh"

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
Matthias Möller's avatar
Matthias Möller committed
54
#include <OpenFlipper/common/GlobalOptions.hh>
Matthias Möller's avatar
Matthias Möller committed
55 56 57 58 59 60

//------------------------------------------------------------------------------
RulerPlugin::RulerPlugin()
:
buttonAction_(0),
pickModeName_("MeasureDistance"),
61
lineDrag_(-1),
Matthias Möller's avatar
Matthias Möller committed
62 63 64
dblClickCheck_(false),
optionsWidget_(0),
textSizeSettingName_(name()+QString("/TextSize"))
Matthias Möller's avatar
Matthias Möller committed
65 66 67 68 69 70 71
{

}

//------------------------------------------------------------------------------
RulerPlugin::~RulerPlugin()
{
72
  reset();
Matthias Möller's avatar
Matthias Möller committed
73 74 75 76
}
//------------------------------------------------------------------------------
void RulerPlugin::initializePlugin()
{
77
  QToolBar *button = new QToolBar("Ruler");
Matthias Möller's avatar
Matthias Möller committed
78 79 80 81 82 83

  buttonAction_ = new QAction(tr("<B>Ruler</B><br> Display the distance between two points."),this);

  button->addAction(buttonAction_);
  buttonAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"ruler.png"));
  buttonAction_->setCheckable(true);
Matthias Möller's avatar
Matthias Möller committed
84 85
  WhatsThisGenerator whatsThisGen("Ruler");
  whatsThisGen.setWhatsThis(buttonAction_,tr("Measures the distance between two points.<br><b>LeftClick</b>: define/change the position.<br><b>DoubleClick</b>: reset the ruler.<br>"));
Matthias Möller's avatar
Matthias Möller committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

  buttonAction_->setChecked(false);

  connect(buttonAction_,SIGNAL(triggered()), this , SLOT(slotChangePickMode()) );
  emit addToolbar(button);
}

//------------------------------------------------------------------------------
void RulerPlugin::pluginsInitialized()
{
  emit addPickMode(pickModeName_);
}

//------------------------------------------------------------------------------
void RulerPlugin::slotMouseEvent(QMouseEvent* _event)
{
102 103
  if ( PluginFunctions::pickMode() != pickModeName_)
    return;
Matthias Möller's avatar
doc  
Matthias Möller committed
104

105 106
//////create or change ruler/////////
//set one of the points, depending on the hit state (first, second or modifying)
107 108
  if (_event->type() == QEvent::MouseButtonRelease )
  {
109
    size_t node_idx, target_idx;
110
    OpenMesh::Vec3d hitPoint;
Matthias Möller's avatar
Matthias Möller committed
111

112 113 114
    // Get picked object's identifier by picking in scenegraph
    if ( PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING ,_event->pos(), node_idx, target_idx, &hitPoint) && !dblClickCheck_)
    {
115
      if (!currentRuler_)
116
      {
117
        //create a new Ruler
118 119
        BaseObjectData* object;
        if ( PluginFunctions::getPickedObject(node_idx, object) )
Matthias Möller's avatar
Matthias Möller committed
120
        {
121 122
          currentRuler_.reset(new Ruler(object,name(),0));
          connect(currentRuler_.get(),SIGNAL(updateView()),this,SIGNAL(updateView()));
123
          unsigned textSize = OpenFlipperSettings().value(textSizeSettingName_,16).toUInt();
Matthias Möller's avatar
Matthias Möller committed
124
          currentRuler_->setTextSize(textSize);
125 126 127
          currentRuler_->setPoints(hitPoint,hitPoint);
          enableDragMode(1);
        }
Matthias Möller's avatar
Matthias Möller committed
128

129
      }
130
      else
131
      {
132 133
        //Ruler was created -> change position of a point
        if (!dragModeActive())
Matthias Möller's avatar
Matthias Möller committed
134
        {
135 136 137 138 139
          //dragmode is disabled ->  update position of nearest point
          float distToStart = (currentRuler_->points()[0] - hitPoint).length();
          float distToEnd = (currentRuler_->points()[1] - hitPoint).length();
          if (distToStart < distToEnd)
            currentRuler_->setStartPoint(hitPoint);
Matthias Möller's avatar
Matthias Möller committed
140
          else
141
            currentRuler_->setEndPoint(hitPoint);
Matthias Möller's avatar
Matthias Möller committed
142
        }
143 144
        else
        {
145 146 147 148 149
          //second: drag mode is enabled so we can easily update the position
          if (lineDrag_ == 0)
            currentRuler_->setStartPoint(hitPoint);
          else
            currentRuler_->setEndPoint(hitPoint);
150 151
          disableDragMode();
        }
Matthias Möller's avatar
Matthias Möller committed
152 153
      }
    }
154 155 156 157 158
    else // if nothing was picked
    {
      reset();
    }
  }
159 160

//////enable drag mode//////
161 162
  else if (_event->type() == QEvent::MouseButtonPress)
  {//enable drag mode
Matthias Möller's avatar
doc  
Matthias Möller committed
163

164
    if (currentRuler_)
165 166 167
    {
      //decides which point is the nearest one, so
      //it can be dragged
168
      size_t node_idx, target_idx;
Matthias Möller's avatar
Matthias Möller committed
169 170 171
      OpenMesh::Vec3d hitPoint;

      // Get picked object's identifier by picking in scenegraph
172
      if ( PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING ,_event->pos(), node_idx, target_idx, &hitPoint) )
Matthias Möller's avatar
Matthias Möller committed
173
      {
174 175 176
        float distToStart = (currentRuler_->points()[0] - hitPoint).length();
        float distToEnd = (currentRuler_->points()[1] - hitPoint).length();
        enableDragMode( (distToStart < distToEnd)? 0 : 1);
Matthias Möller's avatar
Matthias Möller committed
177 178
      }
    }
179
  }
180 181
////////modify ruler of drag mode was enabled/////////
  else if (_event->type() == QEvent::MouseMove && dragModeActive())
182 183
  {//mouse moved and drag mode is enabled

184
    size_t node_idx, target_idx;
185
    OpenMesh::Vec3d hitPoint;
186 187
    ACG::Vec3d hitPoints[2];
    std::copy(currentRuler_->points(),currentRuler_->points()+2,hitPoints);
Matthias Möller's avatar
Matthias Möller committed
188

189 190 191 192 193 194 195
    // Get picked object's identifier by picking in scenegraph
    if ( !PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING ,_event->pos(), node_idx, target_idx, &hitPoint) )
    {
      //if mouse is not over an object, get the unprojected coordinates and the last z-value
      QPoint position = _event->pos();
      ACG::Vec3d viewCoords = ACG::Vec3d(position.x(), PluginFunctions::viewerProperties().glState().context_height() - position.y(), 0.5);
      hitPoint = PluginFunctions::viewerProperties().glState().unproject(viewCoords);
196
      hitPoints[lineDrag_] = ACG::Vec3d(hitPoint.data()[0], hitPoint.data()[1], hitPoints[lineDrag_].data()[2] );
197 198
    }
    else
199 200 201
      hitPoints[lineDrag_] = hitPoint;

    currentRuler_->setPoints(hitPoints[0],hitPoints[1]);
202
  }
203

204
///////////////////////Reset/////////////////////////////////
205 206 207 208
  else if (_event->type() == QEvent::MouseButtonDblClick)
  {//reset
    reset();
    dblClickCheck_ = true;
Matthias Möller's avatar
Matthias Möller committed
209
  }
210 211
  if (dblClickCheck_ && _event->type() == QEvent::MouseButtonRelease)
    dblClickCheck_ = false;
Matthias Möller's avatar
Matthias Möller committed
212 213 214 215 216
}

//------------------------------------------------------------------------------
void RulerPlugin::reset()
{
217
  currentRuler_.reset();
218
  lineDrag_ = -1;
Matthias Möller's avatar
Matthias Möller committed
219
}
220 221 222 223 224 225 226 227 228 229 230 231 232
//------------------------------------------------------------------------------
void RulerPlugin::enableDragMode(const int _point)
{
  lineDrag_ = _point;
  emit setPickModeMouseTracking(pickModeName_,true);
}

//------------------------------------------------------------------------------
void RulerPlugin::disableDragMode()
{
  lineDrag_ = -1;
  emit setPickModeMouseTracking(pickModeName_,false);
}
Matthias Möller's avatar
Matthias Möller committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

//------------------------------------------------------------------------------
void RulerPlugin::slotChangePickMode()
{
  PluginFunctions::actionMode( Viewer::PickingMode );
  PluginFunctions::pickMode(pickModeName_);

}

//------------------------------------------------------------------------------
void RulerPlugin::slotPickModeChanged(const std::string& _mode)
{
  buttonAction_->setChecked(_mode == pickModeName_);
}

248 249 250
//------------------------------------------------------------------------------
void RulerPlugin::slotAllCleared()
{
251 252 253 254 255 256 257
  disableDragMode();
}

void RulerPlugin::objectDeleted(int _id)
{
  if (!currentRuler_)
    return;
258

259 260 261 262 263 264
  disableDragMode();
  if (_id == currentRuler_->getBaseObj()->id())
  {
    disconnect(currentRuler_.get(),SIGNAL(updateView()),this,SIGNAL(updateView()));
    currentRuler_.reset();
  }
265
}
Matthias Möller's avatar
Matthias Möller committed
266 267 268 269 270 271 272 273
//------------------------------------------------------------------------------
bool RulerPlugin::initializeOptionsWidget(QWidget*& _widget)
{
  if (!optionsWidget_)
  {
    optionsWidget_ = new RulerOptions();
  }

274
  unsigned textSize = OpenFlipperSettings().value(textSizeSettingName_,16).toUInt();
Matthias Möller's avatar
Matthias Möller committed
275 276 277 278 279 280 281 282 283 284 285 286
  optionsWidget_->textSizeSpinBox->setValue(textSize);
  _widget = optionsWidget_;
  return true;
}
//------------------------------------------------------------------------------
void RulerPlugin::applyOptions()
{
  int textSize = optionsWidget_->textSizeSpinBox->value();
  if (currentRuler_)
    currentRuler_->setTextSize(textSize);
  OpenFlipperSettings().setValue(textSizeSettingName_,textSize);
}
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
//------------------------------------------------------------------------------
void RulerPlugin::slotViewChanged()
{

  if (!currentRuler_)
    return;

  //compute line direction
  ACG::Vec3d lineVector = currentRuler_->points()[0] - currentRuler_->points()[1];
  ACG::Vec3d rightVec = (PluginFunctions::viewingDirection() % -PluginFunctions::upVector()).normalize();

  float cosAngleLineRight = lineVector.normalize() | rightVec;
  float cosAngleUpLine = PluginFunctions::upVector().normalized() | lineVector.normalized() ;


  rightVec *= -0.5f*currentRuler_->textScale();
  ACG::Vec3d updownVec = PluginFunctions::upVector()*currentRuler_->textScale();

  //compute up/down offset
  if (cosAngleLineRight > 0.f)
  {
    updownVec *= (cosAngleUpLine < 0.f) ? -2.f : 0.5f;
  }
  else
  {
    updownVec *= (cosAngleUpLine < 0.f) ? 0.5f : -2.0f;
  }

  // small offset to the right and big offset up/down depending on the line
  currentRuler_->setTextOffset(rightVec+updownVec);


}
320

Matthias Möller's avatar
Matthias Möller committed
321