00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "autorotatetool.h"
00026 #include <avogadro/primitive.h>
00027 #include <avogadro/glwidget.h>
00028 #include <avogadro/camera.h>
00029
00030 #include <QtPlugin>
00031 #include <QObject>
00032
00033 #include <QSlider>
00034 #include <QLabel>
00035 #include <QPushButton>
00036 #include <QVBoxLayout>
00037 #include <QHBoxLayout>
00038
00039 using namespace std;
00040 using namespace OpenBabel;
00041 using namespace Eigen;
00042
00043 namespace Avogadro {
00044
00045 AutoRotateTool::AutoRotateTool(QObject *parent) : Tool(parent), m_glwidget(0), m_leftButtonPressed(false),
00046 m_midButtonPressed(false), m_timerId(0), m_xRotation(0), m_yRotation(0), m_zRotation(0), m_maxRotation(40),
00047 m_settingsWidget(0), m_buttonStartStop(0), m_sliderX(0), m_sliderY(0), m_sliderZ(0)
00048
00049 {
00050 QAction *action = activateAction();
00051 action->setIcon(QIcon(QString::fromUtf8(":/autorotate/autorotate.png")));
00052 action->setToolTip(tr("Auto Rotation Tool"));
00053 }
00054
00055 AutoRotateTool::~AutoRotateTool()
00056 {
00057 }
00058
00059 int AutoRotateTool::usefulness() const
00060 {
00061 return 20000;
00062 }
00063
00064 void AutoRotateTool::rotate() const
00065 {
00066 if (!m_glwidget->molecule()) {
00067 return;
00068 }
00069
00070
00071 Vector3d xAxis = m_glwidget->camera()->backTransformedXAxis();
00072 Vector3d yAxis = m_glwidget->camera()->backTransformedYAxis();
00073 Vector3d zAxis = m_glwidget->camera()->backTransformedZAxis();
00074
00075 m_glwidget->camera()->translate( m_glwidget->center() );
00076 m_glwidget->camera()->rotate( m_xRotation * ROTATION_SPEED, yAxis );
00077 m_glwidget->camera()->rotate( m_yRotation * ROTATION_SPEED, xAxis );
00078 m_glwidget->camera()->rotate( m_zRotation * ROTATION_SPEED, zAxis );
00079 m_glwidget->camera()->translate( -m_glwidget->center() );
00080 }
00081
00082 QUndoCommand* AutoRotateTool::mousePress(GLWidget *widget, const QMouseEvent *event)
00083 {
00084
00085 m_glwidget = widget;
00086 m_startDraggingPosition = event->pos();
00087 m_currentDraggingPosition = m_startDraggingPosition;
00088 m_leftButtonPressed = ( event->buttons() & Qt::LeftButton );
00089 m_midButtonPressed = ( event->buttons() & Qt::MidButton );
00090
00091 m_glwidget->update();
00092
00093 return 0;
00094 }
00095
00096 QUndoCommand* AutoRotateTool::mouseRelease(GLWidget *widget, const QMouseEvent *event)
00097 {
00098 m_glwidget = widget;
00099
00100 double xMultiplier = m_maxRotation / static_cast<double>(m_glwidget->width());
00101 double yMultiplier = m_maxRotation / static_cast<double>(m_glwidget->height());
00102 QPoint deltaDragging = event->pos() - m_startDraggingPosition;
00103
00104 if(m_leftButtonPressed)
00105 {
00106
00107 m_xRotation = static_cast<int>(deltaDragging.x() * xMultiplier);
00108 m_sliderX->setValue(m_xRotation);
00109 m_yRotation = static_cast<int>(deltaDragging.y() * yMultiplier);
00110 m_sliderY->setValue(m_yRotation);
00111 m_zRotation = 0;
00112 m_sliderZ->setValue(m_zRotation);
00113
00114 enableTimer();
00115 }
00116 else if (m_midButtonPressed)
00117 {
00118
00119 m_xRotation = 0;
00120 m_sliderX->setValue(m_xRotation);
00121 m_yRotation = 0;
00122 m_sliderY->setValue(m_yRotation);
00123 m_zRotation = static_cast<int>(deltaDragging.x() * xMultiplier);
00124 m_sliderZ->setValue(m_zRotation);
00125
00126 enableTimer();
00127 }
00128
00129 m_leftButtonPressed = false;
00130 m_midButtonPressed = false;
00131
00132 m_glwidget->update();
00133
00134 return 0;
00135 }
00136
00137 QUndoCommand* AutoRotateTool::mouseMove(GLWidget *widget, const QMouseEvent *event)
00138 {
00139 m_glwidget = widget;
00140
00141
00142 m_currentDraggingPosition = event->pos();
00143
00144 m_glwidget->update();
00145
00146 return 0;
00147 }
00148
00149
00150 bool AutoRotateTool::paint(GLWidget *widget)
00151 {
00152 m_glwidget = widget;
00153 if(m_leftButtonPressed || m_midButtonPressed)
00154 {
00155
00156 if (m_leftButtonPressed) {
00157 glColor4f(1., 0., 0., 1.);
00158 } else if (m_midButtonPressed) {
00159 glColor4f(0., 1., 0., 1.);
00160 }
00161 Vector3d start = m_glwidget->camera()->unProject(m_startDraggingPosition);
00162 Vector3d end = m_glwidget->camera()->unProject(m_currentDraggingPosition);
00163 glDisable(GL_LIGHTING);
00164 glBegin(GL_LINES);
00165 glVertex3d(start.x(), start.y(), start.z());
00166 glVertex3d(end.x(), end.y(), end.z());
00167 glEnd();
00168 glEnable(GL_LIGHTING);
00169 }
00170 return true;
00171 }
00172
00173 void AutoRotateTool::timerEvent(QTimerEvent*)
00174 {
00175
00176
00177 if (m_glwidget && (m_xRotation || m_yRotation || m_zRotation))
00178 {
00179 rotate();
00180 m_glwidget->update();
00181 }
00182 }
00183
00184 void AutoRotateTool::setXRotation(int i)
00185 {
00186 m_xRotation = i;
00187 }
00188
00189 void AutoRotateTool::setYRotation(int i)
00190 {
00191 m_yRotation = i;
00192 }
00193
00194 void AutoRotateTool::setZRotation(int i)
00195 {
00196 m_zRotation = i;
00197 }
00198
00199 void AutoRotateTool::toggleTimer()
00200 {
00201
00202 if (m_timerId) {
00203 disableTimer();
00204 } else {
00205 enableTimer();
00206 }
00207 }
00208
00209 void AutoRotateTool::enableTimer()
00210 {
00211 if(!m_timerId)
00212 {
00213 m_timerId = startTimer(40);
00214 m_buttonStartStop->setText(tr("Stop"));
00215 }
00216 }
00217
00218 void AutoRotateTool::disableTimer()
00219 {
00220 if(m_timerId)
00221 {
00222 killTimer(m_timerId);
00223 m_timerId = 0;
00224 m_buttonStartStop->setText(tr("Start"));
00225 }
00226 }
00227
00228 void AutoRotateTool::resetRotations()
00229 {
00230
00231 emit resetRotation(0);
00232 }
00233
00234 QWidget* AutoRotateTool::settingsWidget() {
00235 if(!m_settingsWidget) {
00236 m_settingsWidget = new QWidget;
00237
00238
00239 QLabel* labelX = new QLabel(tr("x rotation:"));
00240 labelX->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
00241 labelX->setMaximumHeight(15);
00242 m_sliderX = new QSlider(m_settingsWidget);
00243 m_sliderX->setOrientation(Qt::Horizontal);
00244 m_sliderX->setTickPosition(QSlider::TicksAbove);
00245 m_sliderX->setToolTip(tr("x rotation"));
00246 m_sliderX->setTickInterval(10);
00247 m_sliderX->setPageStep(5);
00248 m_sliderX->setRange(-m_maxRotation, m_maxRotation);
00249 m_sliderX->setValue(0);
00250 QHBoxLayout* xLayout = new QHBoxLayout;
00251 xLayout->addWidget(labelX);
00252 xLayout->addWidget(m_sliderX);
00253
00254
00255 QLabel* labelY = new QLabel(tr("y rotation:"));
00256 labelY->setMaximumHeight(15);
00257 m_sliderY = new QSlider(m_settingsWidget);
00258 m_sliderY->setOrientation(Qt::Horizontal);
00259 m_sliderY->setTickPosition(QSlider::TicksAbove);
00260 m_sliderY->setToolTip(tr("y rotation"));
00261 m_sliderY->setTickInterval(10);
00262 m_sliderY->setPageStep(5);
00263 m_sliderY->setRange(-m_maxRotation, m_maxRotation);
00264 m_sliderY->setValue(0);
00265 QHBoxLayout* yLayout = new QHBoxLayout;
00266 yLayout->addWidget(labelY);
00267 yLayout->addWidget(m_sliderY);
00268
00269
00270 QLabel* labelZ = new QLabel(tr("z rotation:"));
00271 labelZ->setMaximumHeight(15);
00272 m_sliderZ = new QSlider(m_settingsWidget);
00273 m_sliderZ->setOrientation(Qt::Horizontal);
00274 m_sliderZ->setTickPosition(QSlider::TicksAbove);
00275 m_sliderZ->setToolTip(tr("z rotation"));
00276 m_sliderZ->setTickInterval(10);
00277 m_sliderZ->setPageStep(5);
00278 m_sliderZ->setRange(-m_maxRotation, m_maxRotation);
00279 m_sliderZ->setValue(0);
00280 QHBoxLayout* zLayout = new QHBoxLayout;
00281 zLayout->addWidget(labelZ);
00282 zLayout->addWidget(m_sliderZ);
00283
00284
00285 m_buttonStartStop = new QPushButton(tr("Start"), m_settingsWidget);
00286 QPushButton* buttonReset = new QPushButton(tr("Reset"), m_settingsWidget);
00287 QHBoxLayout* buttonLayout = new QHBoxLayout();
00288 buttonLayout->addWidget(m_buttonStartStop);
00289 buttonLayout->addWidget(buttonReset);
00290
00291 QVBoxLayout* layout = new QVBoxLayout();
00292 layout->addLayout(xLayout);
00293 layout->addLayout(yLayout);
00294 layout->addLayout(zLayout);
00295 layout->addLayout(buttonLayout);
00296 layout->addStretch(1);
00297 m_settingsWidget->setLayout(layout);
00298
00299
00300 connect(m_sliderX, SIGNAL(valueChanged(int)),
00301 this, SLOT(setXRotation(int)));
00302
00303 connect(m_sliderY, SIGNAL(valueChanged(int)),
00304 this, SLOT(setYRotation(int)));
00305
00306 connect(m_sliderZ, SIGNAL(valueChanged(int)),
00307 this, SLOT(setZRotation(int)));
00308
00309
00310 connect(m_buttonStartStop, SIGNAL(clicked()),
00311 this, SLOT(toggleTimer()));
00312
00313
00314 connect(buttonReset, SIGNAL(clicked()),
00315 this, SLOT(resetRotations()));
00316
00317 connect(this, SIGNAL(resetRotation(int)),
00318 m_sliderX, SLOT(setValue(int)));
00319 connect(this, SIGNAL(resetRotation(int)),
00320 m_sliderY, SLOT(setValue(int)));
00321 connect(this, SIGNAL(resetRotation(int)),
00322 m_sliderZ, SLOT(setValue(int)));
00323
00324 connect(m_settingsWidget, SIGNAL(destroyed()),
00325 this, SLOT(settingsWidgetDestroyed()));
00326 }
00327
00328 return m_settingsWidget;
00329 }
00330
00331 void AutoRotateTool::settingsWidgetDestroyed()
00332 {
00333 m_settingsWidget = 0;
00334 m_buttonStartStop = 0;
00335 }
00336 }
00337
00338 #include "autorotatetool.moc"
00339
00340 Q_EXPORT_PLUGIN2(autorotatetool, Avogadro::AutoRotateToolFactory)