pqAnimationTimeWidget.cxx 9.68 KB
Newer Older
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
/*=========================================================================

   Program: ParaView
   Module:  pqAnimationTimeWidget.cxx

   Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
   under the terms of the ParaView license version 1.2.

   See License_v1.2.txt for the full ParaView license.
   A copy of this license can be obtained by contacting
   Kitware Inc.
   28 Corporate Drive
   Clifton Park, NY 12065
   USA

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 AUTHORS 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.

========================================================================*/
#include "pqAnimationTimeWidget.h"
#include "ui_pqAnimationTimeWidget.h"

#include "pqPropertyLinksConnection.h"
#include "pqPropertyLinks.h"
#include "vtkSMPropertyHelper.h"
#include "vtkSMTimeKeeperProxy.h"
#include "vtkWeakPointer.h"

#include <QDoubleValidator>

class pqAnimationTimeWidget::pqInternals
{
public:
  Ui::AnimationTimeWidget Ui;
  vtkWeakPointer<vtkSMProxy> AnimationScene;
  void* AnimationSceneVoidPtr;
  pqPropertyLinks Links;
  int CachedTimestepCount;

  pqInternals(pqAnimationTimeWidget* self) :
    AnimationSceneVoidPtr(NULL),
    CachedTimestepCount(-1)
    {
    this->Ui.setupUi(self);
    this->Ui.timeValue->setValidator(new QDoubleValidator(self));
    }
};

namespace
{
  /// Used to link the number of elements in a sm-property to the qt widget.
  class pqAnimationTimeWidgetLinks : public pqPropertyLinksConnection
  {
  typedef pqPropertyLinksConnection Superclass;
public:
  pqAnimationTimeWidgetLinks(
    QObject* qobject, const char* qproperty, const char* qsignal,
    vtkSMProxy* smproxy, vtkSMProperty* smproperty, int smindex,
    bool use_unchecked_modified_event,
    QObject* parentObject=0)
    : Superclass(qobject, qproperty, qsignal,
      smproxy, smproperty, smindex,
      use_unchecked_modified_event, parentObject)
    {
    }
  virtual ~pqAnimationTimeWidgetLinks()
    {
    }

protected:
  virtual QVariant currentServerManagerValue(bool use_unchecked) const
    {
    Q_ASSERT(use_unchecked == false);
86
    Q_UNUSED(use_unchecked);
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
    unsigned int count = vtkSMPropertyHelper(this->propertySM()).GetNumberOfElements();
    return QVariant(static_cast<int>(count));
    }

private:
  Q_DISABLE_COPY(pqAnimationTimeWidgetLinks);
  };
}


//-----------------------------------------------------------------------------
pqAnimationTimeWidget::pqAnimationTimeWidget(QWidget* parentObject)
  : Superclass(parentObject),
  Internals(new pqAnimationTimeWidget::pqInternals(this))
{
  this->setEnabled(false);

  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  this->connect(ui.timeValue, SIGNAL(textChangedAndEditingFinished()), SIGNAL(timeValueChanged()));
  this->connect(ui.radioButtonValue, SIGNAL(toggled(bool)), SIGNAL(playModeChanged()));
  this->connect(ui.radioButtonValue, SIGNAL(toggled(bool)), SLOT(updateTimestepCountLabelVisibility()));
  this->connect(ui.timestepValue, SIGNAL(editingFinished()),
    SLOT(timestepValueChanged()));
}

//-----------------------------------------------------------------------------
pqAnimationTimeWidget::~pqAnimationTimeWidget()
{
}

//-----------------------------------------------------------------------------
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
118
void pqAnimationTimeWidget::setAnimationScene(vtkSMProxy* ascene)
119 120
{
  pqInternals& internals = *this->Internals;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
121
  if (internals.AnimationSceneVoidPtr == ascene)
122 123 124 125 126
    {
    return;
    }

  internals.Links.clear();
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
127 128 129 130
  internals.AnimationScene = ascene;
  internals.AnimationSceneVoidPtr = ascene;
  this->setEnabled(ascene != NULL);
  if (!ascene)
131 132 133 134 135 136
    {
    return;
    }

  internals.Links.addPropertyLink(
    this, "timeValue", SIGNAL(timeValueChanged()),
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
137
    ascene, ascene->GetProperty("AnimationTime"));
138
  internals.Links.addPropertyLink(this, "playMode", SIGNAL(playModeChanged()),
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
139
    ascene, ascene->GetProperty("PlayMode"));
140 141 142

  // In a ParaView application, it's safe to assume that the timekeeper an
  // animation scene is using doesn't change in the life span of the scene.
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
143 144
  vtkSMProxy* atimekeeper = vtkSMPropertyHelper(ascene, "TimeKeeper").GetAsProxy();
  Q_ASSERT(atimekeeper != NULL);
145 146 147

  internals.Links.addPropertyLink<pqAnimationTimeWidgetLinks>(
    this, "timeStepCount", SIGNAL(dummySignal()),
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
148
    atimekeeper, atimekeeper->GetProperty("TimestepValues"));
149 150
  internals.Links.addPropertyLink(
    this, "timeLabel", SIGNAL(dummySignal()),
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
151
    atimekeeper, atimekeeper->GetProperty("TimeLabel"));
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
}

//-----------------------------------------------------------------------------
vtkSMProxy* pqAnimationTimeWidget::animationScene() const
{
  pqInternals& internals = *this->Internals;
  return internals.AnimationScene;
}

//-----------------------------------------------------------------------------
vtkSMProxy* pqAnimationTimeWidget::timeKeeper() const
{
  pqInternals& internals = *this->Internals;
  return internals.AnimationScene?
    vtkSMPropertyHelper(internals.AnimationScene, "TimeKeeper").GetAsProxy() : NULL;
}

//-----------------------------------------------------------------------------
void pqAnimationTimeWidget::setTimeValue(double time)
{
  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  ui.timeValue->setTextAndResetCursor(QString::number(time));
  bool prev = ui.timestepValue->blockSignals(true);
  ui.timestepValue->setValue(
    vtkSMTimeKeeperProxy::GetLowerBoundTimeStepIndex(this->timeKeeper(), time));
  ui.timestepValue->blockSignals(prev);
}

//-----------------------------------------------------------------------------
double pqAnimationTimeWidget::timeValue() const
{
  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  return ui.timeValue->text().toDouble();
}

//-----------------------------------------------------------------------------
void pqAnimationTimeWidget::setTimeStepCount(int value)
{
  pqInternals& internals = *this->Internals;
  internals.CachedTimestepCount = value;

  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  ui.timestepValue->setMaximum(value > 0? value -1 : 0);
  ui.timestepCountLabel->setText(QString("of %1").arg(value));
  ui.timestepCountLabel->setVisible(
    (value > 0) && (this->playMode() == "Snap To TimeSteps"));

  bool prev = ui.timestepValue->blockSignals(true);
  ui.timestepValue->setValue(
    vtkSMTimeKeeperProxy::GetLowerBoundTimeStepIndex(this->timeKeeper(), this->timeValue()));
  ui.timestepValue->blockSignals(prev);

  this->updateTimestepCountLabelVisibility();
}

//-----------------------------------------------------------------------------
int pqAnimationTimeWidget::timeStepCount() const
{
  pqInternals& internals = *this->Internals;
  return internals.CachedTimestepCount;
}

//-----------------------------------------------------------------------------
void pqAnimationTimeWidget::setPlayMode(const QString& value)
{
  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  if (value == "Sequence" || value == "Real Time")
    {
    ui.radioButtonValue->setChecked(true);
    }
  else if (value == "Snap To TimeSteps")
    {
    ui.radioButtonStep->setChecked(true);
    }
  this->updateTimestepCountLabelVisibility();
}

//-----------------------------------------------------------------------------
QString pqAnimationTimeWidget::playMode() const
{
  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  return ui.radioButtonValue->isChecked()? "Sequence" : "Snap To TimeSteps";
}

//-----------------------------------------------------------------------------
void pqAnimationTimeWidget::updateTimestepCountLabelVisibility()
{
  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  ui.timestepCountLabel->setVisible(
    (this->timeStepCount() > 0) && (this->playMode() == "Snap To TimeSteps"));
}

//-----------------------------------------------------------------------------
void pqAnimationTimeWidget::timestepValueChanged()
{
  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  int index = ui.timestepValue->value();
  vtkSMPropertyHelper helper(this->timeKeeper(), "TimestepValues");
  if (index >=0 && static_cast<unsigned int>(index) < helper.GetNumberOfElements())
    {
    this->setTimeValue(helper.GetAsDouble(index));
    emit this->timeValueChanged();
    }
}
256 257 258 259 260 261 262 263 264 265 266 267 268 269
//-----------------------------------------------------------------------------
void pqAnimationTimeWidget::setTimeLabel(const QString& val)
{
  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  ui.timeLabel->setText(val + ":");
}

//-----------------------------------------------------------------------------
QString pqAnimationTimeWidget::timeLabel() const
{
  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  QString txt = ui.timeLabel->text();
  return txt.left(txt.length()-1);
}
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

//-----------------------------------------------------------------------------
void pqAnimationTimeWidget::setPlayModeReadOnly(bool val)
{
  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  ui.radioButtonValue->setVisible(!val);
  ui.radioButtonStep->setVisible(!val);
}

//-----------------------------------------------------------------------------
bool pqAnimationTimeWidget::playModeReadOnly() const
{
  Ui::AnimationTimeWidget &ui = this->Internals->Ui;
  return !ui.radioButtonStep->isVisible();
}