ctkVTKRenderViewEventPlayer.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QDebug>
  17. #include <QMessageBox>
  18. #include <QKeyEvent>
  19. #include <QMouseEvent>
  20. #include <QRegExp>
  21. #include <QTimer>
  22. #include <QWidget>
  23. // CTK includes
  24. #include "ctkVTKRenderView.h"
  25. #include "ctkVTKRenderViewEventPlayer.h"
  26. ctkVTKRenderViewEventPlayer::ctkVTKRenderViewEventPlayer(const QByteArray& classname, QObject * p)
  27. : pqWidgetEventPlayer(p), mClassType(classname)
  28. {
  29. }
  30. bool ctkVTKRenderViewEventPlayer::playEvent(QObject* Object,
  31. const QString& Command,
  32. const QString& Arguments,
  33. bool& Error)
  34. {
  35. Q_UNUSED(Error);
  36. QWidget* widget = qobject_cast<QWidget*>(Object);
  37. if(widget && Object->inherits(mClassType.data()))
  38. {
  39. if (Command == "3DViewSize")
  40. {
  41. QRegExp mouseRegExp("\\(([^,]*),([^,]*),([^,]),([^,]),([^,]*)\\)");
  42. if (mouseRegExp.indexIn(Arguments)!= -1)
  43. {
  44. QVariant v = mouseRegExp.cap(1);
  45. int w = v.toInt();
  46. v = mouseRegExp.cap(2);
  47. int h = v.toInt();
  48. v = mouseRegExp.cap(3);
  49. // int app_w = v.toInt();
  50. v = mouseRegExp.cap(4);
  51. // int app_h = v.toInt();
  52. v = mouseRegExp.cap(5);
  53. bool rescale = v.toBool();
  54. if ( (w != widget->width() || h != widget->height()) && rescale == true)
  55. {
  56. QMessageBox::StandardButton answer = QMessageBox::warning(0, tr("Size issue ..."),
  57. tr("The Render view size is: width : %1 \t height : %2 \n"
  58. "But the size was width : %3 \t height : %4 \n"
  59. "during the record \n"
  60. "Do you want to replace it ?")
  61. .arg(widget->size().width())
  62. .arg(widget->size().height())
  63. .arg(w)
  64. .arg(h),
  65. QMessageBox::Yes | QMessageBox::No ,
  66. QMessageBox::Yes);
  67. if (answer == QMessageBox::Yes )
  68. {
  69. qDebug() << "Foo ****";
  70. // widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  71. widget->setMinimumSize(QSize(w,h));
  72. widget->setMaximumSize(QSize(w,h));
  73. widget->updateGeometry();
  74. widget->topLevelWidget()->topLevelWidget()->updateGeometry();
  75. }
  76. }
  77. }
  78. return true;
  79. }
  80. if (Command == "mousePress" || Command == "mouseRelease" ||
  81. Command == "mouseMove" || Command == "mouseWheel")
  82. {
  83. QRegExp mouseRegExp("\\(([^,]*),([^,]*),([^,]),([^,]),([^,]*)\\)");
  84. if (mouseRegExp.indexIn(Arguments)!= -1)
  85. {
  86. double x_center = widget->size().width() / 2.0;
  87. double y_center = widget->size().height() / 2.0;
  88. QVariant v = mouseRegExp.cap(1);
  89. double x = x_center - (v.toDouble() * x_center);
  90. x = static_cast<int>(x + 0.5);
  91. v = mouseRegExp.cap(2);
  92. double y = y_center - (v.toDouble() * y_center);
  93. y = static_cast<int>(y + 0.5);
  94. v = mouseRegExp.cap(4);
  95. Qt::MouseButtons buttons = static_cast<Qt::MouseButton>(v.toInt());
  96. v = mouseRegExp.cap(5);
  97. Qt::KeyboardModifiers keym = static_cast<Qt::KeyboardModifier>(v.toInt());
  98. v = mouseRegExp.cap(3);
  99. if (Command == "mouseWheel")
  100. {
  101. // QEvent::Type type = QEvent::Wheel;
  102. int delta = ( v.toInt() == 0 ) ? -1 : 1;
  103. QWheelEvent we(QPoint(x,y), delta, buttons, keym);
  104. QCoreApplication::sendEvent(Object, &we);
  105. return true;
  106. }
  107. Qt::MouseButton button = static_cast<Qt::MouseButton>(v.toInt());
  108. QEvent::Type type = (Command == "mousePress")? QEvent::MouseButtonPress :
  109. ((Command=="mouseMove")? QEvent::MouseMove : QEvent::MouseButtonRelease);
  110. QMouseEvent e(type, QPoint(x,y), button, buttons, keym);
  111. QCoreApplication::sendEvent(Object, &e);
  112. }
  113. return true;
  114. }
  115. }
  116. return false;
  117. }