RulerPlugin.cc 11.7 KB
Newer Older
Matthias Möller's avatar
Matthias Möller committed
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
Matthias Möller's avatar
Matthias Möller committed
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
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper 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 Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
Matthias Möller's avatar
Matthias Möller committed
37 38 39
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
Matthias Möller's avatar
Matthias Möller committed
40 41 42 43 44 45 46
*                                                                            *
\*===========================================================================*/


#include "RulerPlugin.hh"

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
Matthias Möller's avatar
Matthias Möller committed
47
#include <OpenFlipper/common/GlobalOptions.hh>
Matthias Möller's avatar
Matthias Möller committed
48 49 50 51 52 53

//------------------------------------------------------------------------------
RulerPlugin::RulerPlugin()
:
buttonAction_(0),
pickModeName_("MeasureDistance"),
54
lineDrag_(-1),
Matthias Möller's avatar
Matthias Möller committed
55 56 57
dblClickCheck_(false),
optionsWidget_(0),
textSizeSettingName_(name()+QString("/TextSize"))
Matthias Möller's avatar
Matthias Möller committed
58 59 60 61 62 63 64
{

}

//------------------------------------------------------------------------------
RulerPlugin::~RulerPlugin()
{
65
  reset();
Matthias Möller's avatar
Matthias Möller committed
66 67 68 69
}
//------------------------------------------------------------------------------
void RulerPlugin::initializePlugin()
{
70
  QToolBar *button = new QToolBar("Ruler");
Matthias Möller's avatar
Matthias Möller committed
71 72 73 74 75 76

  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
77 78
  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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

  buttonAction_->setChecked(false);

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

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

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

98 99
//////create or change ruler/////////
//set one of the points, depending on the hit state (first, second or modifying)
100 101 102 103
  if (_event->type() == QEvent::MouseButtonRelease )
  {
    unsigned int node_idx, target_idx;
    OpenMesh::Vec3d hitPoint;
Matthias Möller's avatar
Matthias Möller committed
104

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

122
      }
123
      else
124
      {
125 126
        //Ruler was created -> change position of a point
        if (!dragModeActive())
Matthias Möller's avatar
Matthias Möller committed
127
        {
128 129 130 131 132
          //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
133
          else
134
            currentRuler_->setEndPoint(hitPoint);
Matthias Möller's avatar
Matthias Möller committed
135
        }
136 137
        else
        {
138 139 140 141 142
          //second: drag mode is enabled so we can easily update the position
          if (lineDrag_ == 0)
            currentRuler_->setStartPoint(hitPoint);
          else
            currentRuler_->setEndPoint(hitPoint);
143 144
          disableDragMode();
        }
Matthias Möller's avatar
Matthias Möller committed
145 146
      }
    }
147 148 149 150 151
    else // if nothing was picked
    {
      reset();
    }
  }
152 153

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

157
    if (currentRuler_)
158 159 160
    {
      //decides which point is the nearest one, so
      //it can be dragged
Matthias Möller's avatar
Matthias Möller committed
161 162 163 164
      unsigned int node_idx, target_idx;
      OpenMesh::Vec3d hitPoint;

      // Get picked object's identifier by picking in scenegraph
165
      if ( PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING ,_event->pos(), node_idx, target_idx, &hitPoint) )
Matthias Möller's avatar
Matthias Möller committed
166
      {
167 168 169
        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
170 171
      }
    }
172
  }
173 174
////////modify ruler of drag mode was enabled/////////
  else if (_event->type() == QEvent::MouseMove && dragModeActive())
175 176 177 178
  {//mouse moved and drag mode is enabled

    unsigned int node_idx, target_idx;
    OpenMesh::Vec3d hitPoint;
179 180
    ACG::Vec3d hitPoints[2];
    std::copy(currentRuler_->points(),currentRuler_->points()+2,hitPoints);
Matthias Möller's avatar
Matthias Möller committed
181

182 183 184 185 186 187 188
    // 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);
189
      hitPoints[lineDrag_] = ACG::Vec3d(hitPoint.data()[0], hitPoint.data()[1], hitPoints[lineDrag_].data()[2] );
190 191
    }
    else
192 193 194
      hitPoints[lineDrag_] = hitPoint;

    currentRuler_->setPoints(hitPoints[0],hitPoints[1]);
195
  }
196

197
///////////////////////Reset/////////////////////////////////
198 199 200 201
  else if (_event->type() == QEvent::MouseButtonDblClick)
  {//reset
    reset();
    dblClickCheck_ = true;
Matthias Möller's avatar
Matthias Möller committed
202
  }
203 204
  if (dblClickCheck_ && _event->type() == QEvent::MouseButtonRelease)
    dblClickCheck_ = false;
Matthias Möller's avatar
Matthias Möller committed
205 206 207 208 209
}

//------------------------------------------------------------------------------
void RulerPlugin::reset()
{
210
  currentRuler_.reset();
211
  lineDrag_ = -1;
Matthias Möller's avatar
Matthias Möller committed
212
}
213 214 215 216 217 218 219 220 221 222 223 224 225
//------------------------------------------------------------------------------
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
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

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

}

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

241 242 243
//------------------------------------------------------------------------------
void RulerPlugin::slotAllCleared()
{
244 245 246 247 248 249 250
  disableDragMode();
}

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

252 253 254 255 256 257
  disableDragMode();
  if (_id == currentRuler_->getBaseObj()->id())
  {
    disconnect(currentRuler_.get(),SIGNAL(updateView()),this,SIGNAL(updateView()));
    currentRuler_.reset();
  }
258
}
Matthias Möller's avatar
Matthias Möller committed
259 260 261 262 263 264 265 266
//------------------------------------------------------------------------------
bool RulerPlugin::initializeOptionsWidget(QWidget*& _widget)
{
  if (!optionsWidget_)
  {
    optionsWidget_ = new RulerOptions();
  }

267
  unsigned textSize = OpenFlipperSettings().value(textSizeSettingName_,16).toUInt();
Matthias Möller's avatar
Matthias Möller committed
268 269 270 271 272 273 274 275 276 277 278 279
  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);
}
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
//------------------------------------------------------------------------------
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);


}
313

Matthias Möller's avatar
Matthias Möller committed
314 315 316 317
#if QT_VERSION < 0x050000
  Q_EXPORT_PLUGIN2( rulerPlugin , RulerPlugin );
#endif