QtWheel.cc 12.1 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
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
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *           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.              *
 *                                                                           *
\*===========================================================================*/

Jan Möbius's avatar
Jan Möbius committed
42

Jan Möbius's avatar
Jan Möbius committed
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



//=============================================================================
//
//  CLASS QtWheel - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================

#include "QtWheel.hh"

#include <cmath>
#include <iostream>

//#include <QDrawutil>

#include <QPainter>
#include <QMenu>
#include <QTimer>
#include <qdrawutil.h>


//== NAMESPACES ===============================================================


namespace ACG {
namespace QtWidgets {


//== IMPLEMENTATION ==========================================================


QtWheel::QtWheel(QWidget* _parent,
	       const char* /* _name */ ,
	       Orientation _orientation)
Jan Möbius's avatar
Jan Möbius committed
80
81
82
83
84
85
86
87
88
89
  : QFrame(_parent),
    angle_(0.0),
    lastAngle_(0.0),
    gear_(1.0),
    gearShift_(0),
    orientation_(_orientation),
    ticks_(36),
    marker_(false),
    dragging_(false),
    tracking_(false)
Jan Möbius's avatar
Jan Möbius committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{

  palette_     = palette();
  palette_.setColor( QPalette::Dark,  QColor(0,0,0));

  setFrameStyle( QtWheel::Panel | QtWheel::Raised );
  setLineWidth(2);

  setContextMenuPolicy ( Qt::CustomContextMenu );

  connect (this, SIGNAL (customContextMenuRequested ( const QPoint & ) ),
           this, SLOT( slotCustomContextMenuRequested ( const QPoint & ) ));

}

Jan Möbius's avatar
Jan Möbius committed
105

Jan Möbius's avatar
Jan Möbius committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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
QtWheel::~QtWheel() {
}

//----------------------------------------------------------------------------

void QtWheel::mousePressEvent(QMouseEvent* _e)
{
  if (_e->button()==Qt::LeftButton) {
    pos_=_e->pos();
    dragging_=true;
    lastAngle_=angle_;
  }
}

void QtWheel::mouseReleaseEvent(QMouseEvent* _e)
{
  if (_e->button()==Qt::LeftButton)
  {
    dragging_=false;

//     turn(_e->pos());
//     emit angleChangedTo(angle_);
//     emit angleChangedBy(angle_-lastAngle_);
  }
}


void QtWheel::mouseMoveEvent(QMouseEvent* _e)
{
  if (_e->buttons()&Qt::LeftButton)
  {
    float dAngle=turn(_e->pos());

    if (tracking_ && dAngle!=0.0) { // 0.0 is explicitly returned
      lastAngle_=angle_-dAngle;

      emit angleChangedTo(angle_);
      emit angleChangedBy(dAngle);
    }
  }
}


double QtWheel::turn(const QPoint& _pos)
{
  QPoint dPos=(_pos-pos_);
  pos_=_pos;

  int d = orientation_== Horizontal ? dPos.x() : dPos.y();

  double dAngle=0.0;

  if (d!=0) {
    // full width/height = 180 deg for gear()==1
    dAngle=double(d)/double(size_)*M_PI*gear_;
    angle_+=dAngle;

    redrawPixmap();
    repaint();
  }
  return dAngle;
}

//----------------------------------------------------------------------------

void QtWheel::mouseDoubleClickEvent(QMouseEvent* _e) {
  if (_e->button()==Qt::LeftButton) {
    int sz,x;
    if (orientation_== Horizontal) {
      sz=width();  x=_e->x();
    }
    else {
      sz=height(); x=_e->y();
    }

    if (x<sz/2) {
      if (gearShift_<8) {
        ++gearShift_;
        gear_*=2.0;

        redrawPixmap();
        repaint();

        emit gearUp();
      }
    }
    else {
      if (gearShift_>-8) {
        --gearShift_;
        gear_/=2.0;

        redrawPixmap();
        repaint();

        emit gearDown();
      }
    }
  }
}

//----------------------------------------------------------------------------

void QtWheel::keyPressEvent(QKeyEvent* _e) {

  //
  // This does not work! Do we really need/want keyboard input???
  //

  if (dragging_)
    return;

  double dAngle=0.0;

  if (!_e->isAutoRepeat())
    lastAngle_=angle_;

  switch (_e->key()) {
    case Qt::Key_Left:
    case Qt::Key_Up: {
      dAngle= -M_PI/double(ticks_)*gear_;
    }
    break;

    case Qt::Key_Right:
    case Qt::Key_Down: {
      dAngle= +M_PI/double(ticks_)*gear_;
    }
    break;

    default: return;
  }

  if (tracking_)
    lastAngle_=angle_;

  angle_+=dAngle;

  redrawPixmap();
  repaint();

  if (tracking_) {
    emit angleChangedTo(angle_);
    emit angleChangedBy(angle_-lastAngle_);
  }
}

void QtWheel::keyReleaseEvent(QKeyEvent* _e) {
  switch (_e->key()) {
    case Qt::Key_Left:
    case Qt::Key_Up:
    case Qt::Key_Right:
    case Qt::Key_Down: {
      if (!tracking_) {
        emit angleChangedTo(angle_);
        emit angleChangedBy(angle_-lastAngle_);
      }
    };
    break;

    default: return;
  }
}

//----------------------------------------------------------------------------

void QtWheel::resizeEvent(QResizeEvent* _e) {
  QFrame::resizeEvent(_e);
  redrawPixmap();
}

void QtWheel::paintEvent(QPaintEvent* _e) {
  if (isVisible()) {
    QFrame::paintEvent(_e);


    QPainter painter(this);
    QRect r=contentsRect();

    /// @todo: bitBlt(this,r.left(),r.top(),pixmap_);
    painter.drawPixmap( r.left(), r.top(), pixmap_ );

  }
}

//----------------------------------------------------------------------------

void QtWheel::redrawPixmap() {
 QRect r=contentsRect();

 if (r.width()<=0 || r.height()<=0) {
   pixmap_ = QPixmap( 0, 0 );
   return;
 }

 if (pixmap_.size()!=r.size())
   pixmap_ = QPixmap(r.size());

  QPainter paint;

  paint.begin( &pixmap_);
306
  pixmap_.fill( palette().window().color() );
Jan Möbius's avatar
Jan Möbius committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459

  // coords of wheel frame
  QRect contents = contentsRect();
  contents.moveTopLeft(QPoint(0,0)); // transform to pixmap coord sys

  QPen pen(Qt::black, 1);
  paint.setPen(pen);

  if (orientation_ == Horizontal) {

    shrinkRect(contents, 3, 2);

    // draw a black frame
    paint.drawRect(contents);
    shrinkRect(contents, 1, 0);
    paint.drawRect(contents);
    shrinkRect(contents, 3, 2);

    int x0 = contents.left();
    int y0 = contents.top();
    int w0 = contents.width();
    int h0 = contents.height();

    size_=w0;

    if (gearShift_>0) {
      QBrush b; b.setColor(QColor(Qt::red)); b.setStyle(Qt::SolidPattern);
      int w=std::min(4*gearShift_,w0-8);
      paint.fillRect(x0+8,y0-1,w,h0+2,b);
    }
    else if (gearShift_<0) {
      QBrush b; b.setColor(QColor(Qt::blue));
      int w=std::min(-4*gearShift_,w0-8); b.setStyle(Qt::SolidPattern);
      paint.fillRect(x0+w0-w-8,y0-1,w,h0+2,b);
    }

    // draw the wheel
    double step = 2 * M_PI / (double) ticks_;
    for (int i = 0; i < ticks_; i++) {
      double x = sin(angle_ + i * step);
      double y = cos(angle_ + i * step);
      if (y>0) {
        qDrawShadeLine( &paint,
                        (int) (x0+(w0+x*w0)/2.0f), y0,
                        (int) (x0+(w0+x*w0)/2.0f), y0+h0,
                        palette_, false, 1, 1);
      }
    }
  }

  else if (orientation_ == Vertical) {

    shrinkRect(contents, 2, 3);

    // draw a black frame
    paint.drawRect(contents);
    shrinkRect(contents, 0, 1);
    paint.drawRect(contents);
    shrinkRect(contents, 2, 3);


    int x0 = contents.left();
    int y0 = contents.top();
    int w0 = contents.width();
    int h0 = contents.height();

    size_=h0;

    if (gearShift_>0) {
      QBrush b; b.setColor(QColor(Qt::red)); b.setStyle(Qt::SolidPattern);
      int h=-std::min(-4*gearShift_,h0-8);
      paint.fillRect(x0-1,y0+8,w0+2,h,b);
    }
    else if (gearShift_<0) {
      QBrush b; b.setColor(QColor(Qt::blue)); b.setStyle(Qt::SolidPattern);
      int h=-std::min(4*gearShift_,h0-8);
      paint.fillRect(x0-1,y0+h0-h-8,w0+2,h,b);
    }

    // draw the wheel
    double step = 2 * M_PI / (double) ticks_;
    for (int i = 0; i < ticks_; i++) {
      double x = sin(angle_ + i * step);
      double y = cos(angle_ + i * step);
      if (y>0) {
	     qDrawShadeLine( &paint,
                        x0,    (int) (y0+(h0+x*h0)/2.0f),
                        x0+w0, (int) (y0+(h0+x*h0)/2.0f),
                        palette_, false, 1, 1);
      } // if y
    } // for ticks_
  } // if Vertical
  paint.end();
}

//-----------------------------------------------------------------------------

void QtWheel::shrinkRect(QRect& _rect, int _dx, int _dy) {
  _rect.setLeft(_rect.left()+_dx);
  _rect.setRight(_rect.right()-_dx);
  _rect.setTop(_rect.top()+_dy); // dy -> -dy
  _rect.setBottom(_rect.bottom()-_dy);
}

//-----------------------------------------------------------------------------

QSizePolicy QtWheel::sizePolicy() const
{
  if (orientation_== Horizontal)
    return QSizePolicy(QSizePolicy::Preferred,
		       QSizePolicy::Minimum);
  else
    return QSizePolicy(QSizePolicy::Minimum,
		       QSizePolicy::Preferred);
}

//-----------------------------------------------------------------------------

QSize QtWheel::sizeHint() const
{
  if (orientation_==Horizontal)
    return QSize(120,20);
  else
    return QSize(20,120);
}

//-----------------------------------------------------------------------------


double QtWheel::clip(double _angle) {
  return fmod(_angle,2*M_PI);
}

double QtWheel::deg(double _angle) {
  return _angle*180.0/M_PI;
}

//-----------------------------------------------------------------------------

void QtWheel::slotCustomContextMenuRequested ( const QPoint & pos ) {

  QMenu* menu = new QMenu(this);
  QAction *hide = menu->addAction("Hide wheel");
  connect( hide, SIGNAL(triggered()) , this, SIGNAL(hideWheel()) );
  menu->popup( mapToGlobal(pos) );

}


//=============================================================================
} // namespace QtWidgets
} // namespace ACG
//=============================================================================