~ubuntu-branches/ubuntu/oneiric/soqt/oneiric

« back to all changes in this revision

Viewing changes to src/Inventor/Qt/widgets/SoQtThumbWheel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2004-05-29 02:58:50 UTC
  • Revision ID: james.westby@ubuntu.com-20040529025850-phd20eva5uyhhdrf
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************\
 
2
 *
 
3
 *  This file is part of the Coin 3D visualization library.
 
4
 *  Copyright (C) 1998-2003 by Systems in Motion.  All rights reserved.
 
5
 *
 
6
 *  This library is free software; you can redistribute it and/or
 
7
 *  modify it under the terms of the GNU General Public License
 
8
 *  ("GPL") version 2 as published by the Free Software Foundation.
 
9
 *  See the file LICENSE.GPL at the root directory of this source
 
10
 *  distribution for additional information about the GNU GPL.
 
11
 *
 
12
 *  For using Coin with software that can not be combined with the GNU
 
13
 *  GPL, and for taking advantage of the additional benefits of our
 
14
 *  support services, please contact Systems in Motion about acquiring
 
15
 *  a Coin Professional Edition License.
 
16
 *
 
17
 *  See <URL:http://www.coin3d.org> for more information.
 
18
 *
 
19
 *  Systems in Motion, Teknobyen, Abels Gate 5, 7030 Trondheim, NORWAY.
 
20
 *  <URL:http://www.sim.no>.
 
21
 *
 
22
\**************************************************************************/
 
23
 
 
24
 
 
25
// FIXME: should strongly consider making the thumbwheel a public
 
26
// class, as we've had several requests for it. 20020226 mortene.
 
27
 
 
28
#include <math.h>
 
29
#include <assert.h>
 
30
#include <stdio.h>
 
31
 
 
32
#include <qpainter.h>
 
33
#include <qdrawutil.h>
 
34
#include <qimage.h>
 
35
#include <qpixmap.h>
 
36
#include <qmetaobject.h>
 
37
#include <Inventor/Qt/widgets/moc_SoQtThumbWheel.icc>
 
38
 
 
39
#include <Inventor/SbBasic.h>
 
40
#include <Inventor/errors/SoDebugError.h>
 
41
 
 
42
#include <soqtdefs.h>
 
43
#include <Inventor/Qt/widgets/SoAnyThumbWheel.h>
 
44
#include <Inventor/Qt/widgets/SoQtThumbWheel.h>
 
45
 
 
46
// *************************************************************************
 
47
 
 
48
static const int SHADEBORDERWIDTH = 0;
 
49
 
 
50
SoQtThumbWheel::SoQtThumbWheel(QWidget * parent,
 
51
                               const char * name)
 
52
  : QWidget(parent, name)
 
53
{
 
54
  this->constructor(SoQtThumbWheel::Vertical);
 
55
}
 
56
 
 
57
SoQtThumbWheel::SoQtThumbWheel(Orientation orientation,
 
58
                               QWidget * parent,
 
59
                               const char * name)
 
60
  : QWidget(parent, name)
 
61
{
 
62
  this->constructor(orientation);
 
63
}
 
64
 
 
65
void
 
66
SoQtThumbWheel::constructor(Orientation orientation)
 
67
{
 
68
  this->orient = orientation;
 
69
  this->state = SoQtThumbWheel::Idle;
 
70
  this->wheelValue = this->tempWheelValue = 0.0f;
 
71
  this->wheel = new SoAnyThumbWheel;
 
72
  this->wheel->setMovement(SoAnyThumbWheel::UNIFORM);
 
73
  this->wheel->setGraphicsByteOrder(SoAnyThumbWheel::ARGB);
 
74
  this->pixmaps = NULL;
 
75
  this->numPixmaps = 0;
 
76
  this->currentPixmap = -1;
 
77
}
 
78
 
 
79
SoQtThumbWheel::~SoQtThumbWheel()
 
80
{
 
81
  delete this->wheel;
 
82
  if (this->pixmaps) {
 
83
    for (int i = 0; i < this->numPixmaps; i++)
 
84
      delete this->pixmaps[i];
 
85
    delete [] this->pixmaps;
 
86
  }
 
87
}
 
88
 
 
89
void
 
90
SoQtThumbWheel::setOrientation(Orientation orientation)
 
91
{
 
92
  this->orient = orientation;
 
93
  this->repaint(FALSE);
 
94
}
 
95
 
 
96
void
 
97
SoQtThumbWheel::paintEvent(QPaintEvent * event)
 
98
{
 
99
  QPainter p(this);
 
100
  QRect paintRect = event->rect();
 
101
  p.setClipRect(paintRect);
 
102
  QColorGroup g = this->colorGroup();
 
103
 
 
104
  int w, d;
 
105
  if (this->orient == SoQtThumbWheel::Vertical) {
 
106
    w = this->width() - 12;
 
107
    d = this->height() - 6;
 
108
  } else {
 
109
    w = this->height() - 12;
 
110
    d = this->width() - 6;
 
111
  }
 
112
 
 
113
  // Handle resizing to too small dimensions gracefully.
 
114
  if ((d <= 0) || (w <= 0)) return;
 
115
 
 
116
  this->initWheel(d, w);
 
117
 
 
118
  int pixmap = this->wheel->getBitmapForValue(this->tempWheelValue,
 
119
                                              (this->state == SoQtThumbWheel::Disabled) ?
 
120
                                              SoAnyThumbWheel::DISABLED : SoAnyThumbWheel::ENABLED);
 
121
 
 
122
  QRect widgetrect(0, 0, this->width(), this->height());
 
123
  QRect wheelrect(widgetrect);
 
124
 
 
125
  if (this->orient == Vertical) {
 
126
    wheelrect.setTop(   wheelrect.top() + 2);
 
127
    wheelrect.setBottom(wheelrect.bottom() - 2);
 
128
    wheelrect.setLeft(  wheelrect.left() + 5);
 
129
    wheelrect.setRight( wheelrect.right() - 5);
 
130
  } else {
 
131
    wheelrect.setTop(   wheelrect.top() + 5);
 
132
    wheelrect.setBottom(wheelrect.bottom() - 5);
 
133
    wheelrect.setLeft(  wheelrect.left() + 2);
 
134
    wheelrect.setRight( wheelrect.right() - 2);
 
135
  }
 
136
  
 
137
  qDrawPlainRect(&p, wheelrect.left(), wheelrect.top(), wheelrect.width(),
 
138
                 wheelrect.height(), QColor(0, 0, 0), 1);
 
139
  
 
140
  wheelrect.setTop(   wheelrect.top() + 1);
 
141
  wheelrect.setBottom(wheelrect.bottom() - 1);
 
142
  wheelrect.setLeft(  wheelrect.left() + 1);
 
143
  wheelrect.setRight( wheelrect.right() - 1);
 
144
  // wheelrect is now wheel-only
 
145
 
 
146
  if (this->orient == Vertical)
 
147
    bitBlt(this, wheelrect.left(), wheelrect.top(), this->pixmaps[pixmap],
 
148
           0, 0, w, d, CopyROP);
 
149
  else
 
150
    bitBlt(this, wheelrect.left(), wheelrect.top(), this->pixmaps[pixmap],
 
151
           0, 0, d, w, CopyROP);
 
152
  this->currentPixmap = pixmap;
 
153
}
 
154
 
 
155
/*!
 
156
  \internal
 
157
*/
 
158
 
 
159
void
 
160
SoQtThumbWheel::mousePressEvent(QMouseEvent * event)
 
161
{
 
162
  if (this->state != SoQtThumbWheel::Idle)
 
163
    return;
 
164
 
 
165
  if (event->button() != LeftButton)
 
166
    return;
 
167
 
 
168
  QRect wheel;
 
169
  if (this->orient == Vertical) {
 
170
    wheel.setLeft(SHADEBORDERWIDTH + 3);
 
171
    wheel.setTop(SHADEBORDERWIDTH + 6);
 
172
    wheel.setRight(this->width() - SHADEBORDERWIDTH - 3);
 
173
    wheel.setBottom(this->height() - SHADEBORDERWIDTH - 6);
 
174
  } else {
 
175
    wheel.setLeft(SHADEBORDERWIDTH + 6);
 
176
    wheel.setTop(SHADEBORDERWIDTH + 3);
 
177
    wheel.setRight(this->width() - SHADEBORDERWIDTH - 6);
 
178
    wheel.setBottom(this->height() - SHADEBORDERWIDTH - 3);
 
179
  }
 
180
 
 
181
  if (! wheel.contains(event->pos()))
 
182
    return;
 
183
 
 
184
  this->state = SoQtThumbWheel::Dragging;
 
185
 
 
186
  if (this->orient == SoQtThumbWheel::Vertical)
 
187
    this->mouseDownPos = event->pos().y() - SHADEBORDERWIDTH - 6;
 
188
  else
 
189
    this->mouseDownPos = event->pos().x() - SHADEBORDERWIDTH - 6;
 
190
 
 
191
  this->mouseLastPos = this->mouseDownPos;
 
192
 
 
193
  emit wheelPressed();
 
194
}
 
195
 
 
196
/*!
 
197
  \internal
 
198
*/
 
199
 
 
200
void
 
201
SoQtThumbWheel::mouseMoveEvent(QMouseEvent * event)
 
202
{
 
203
  if (this->state != SoQtThumbWheel::Dragging)
 
204
    return;
 
205
 
 
206
  if (this->orient == SoQtThumbWheel::Vertical)
 
207
    this->mouseLastPos = event->pos().y() - SHADEBORDERWIDTH - 6;
 
208
  else
 
209
    this->mouseLastPos = event->pos().x() - SHADEBORDERWIDTH - 6;
 
210
 
 
211
 
 
212
  this->tempWheelValue = this->wheel->calculateValue(this->wheelValue,
 
213
      this->mouseDownPos, this->mouseLastPos - this->mouseDownPos);
 
214
 
 
215
  emit wheelMoved(this->tempWheelValue);
 
216
 
 
217
  this->repaint(FALSE);
 
218
}
 
219
 
 
220
/*!
 
221
  \internal
 
222
*/
 
223
 
 
224
void
 
225
SoQtThumbWheel::mouseReleaseEvent(QMouseEvent * event)
 
226
{
 
227
  if (this->state != SoQtThumbWheel::Dragging)
 
228
    return;
 
229
 
 
230
  if (event->button() != LeftButton)
 
231
    return;
 
232
 
 
233
  this->wheelValue = this->tempWheelValue;
 
234
  this->mouseLastPos = this->mouseDownPos;
 
235
  this->state = SoQtThumbWheel::Idle;
 
236
  emit wheelReleased();
 
237
}
 
238
 
 
239
/*
 
240
float
 
241
SoQtThumbWheel::getNormalizedValue(int pos) const
 
242
{
 
243
  int relativepos = pos - this->mouseDownPos;
 
244
  return (float) relativepos / (float)this->getWheelLength() * 2.0f;
 
245
}
 
246
*/
 
247
 
 
248
/*
 
249
int
 
250
SoQtThumbWheel::getWheelLength(void) const
 
251
{
 
252
  return this->orient == SoQtThumbWheel::Vertical ?
 
253
    this->height() : this->width();
 
254
}
 
255
*/
 
256
 
 
257
/*
 
258
int
 
259
SoQtThumbWheel::orientedCoord(const QPoint &p) const
 
260
{
 
261
  return (this->orient == SoQtThumbWheel::Horizontal) ?  p.x() : p.y();
 
262
}
 
263
*/
 
264
 
 
265
QSize
 
266
SoQtThumbWheel::sizeHint(void) const
 
267
{
 
268
  const int length = 122;
 
269
  int thick = 24;
 
270
 
 
271
  if (this->orient == SoQtThumbWheel::Horizontal)
 
272
    return QSize(length, thick);
 
273
  else
 
274
    return QSize(thick, length);
 
275
}
 
276
 
 
277
SoQtThumbWheel::Orientation
 
278
SoQtThumbWheel::orientation(void) const
 
279
{
 
280
  return this->orient;
 
281
}
 
282
 
 
283
float
 
284
SoQtThumbWheel::value(void) const
 
285
{
 
286
  return this->wheelValue;
 
287
}
 
288
 
 
289
// *************************************************************************
 
290
 
 
291
void
 
292
SoQtThumbWheel::initWheel(int diameter, int width)
 
293
{
 
294
  int d, w;
 
295
  this->wheel->getSize(d, w);
 
296
  if (d == diameter && w == width) return;
 
297
 
 
298
  this->wheel->setSize(diameter, width);
 
299
 
 
300
  int pwidth = width;
 
301
  int pheight = diameter;
 
302
  if (this->orient == Horizontal) {
 
303
    pwidth = diameter;
 
304
    pheight = width;
 
305
  }
 
306
 
 
307
  if (this->pixmaps != NULL) {
 
308
    for (int i = 0; i < this->numPixmaps; i++)
 
309
      delete this->pixmaps[i];
 
310
    delete [] this->pixmaps;
 
311
  }
 
312
 
 
313
  this->numPixmaps = this->wheel->getNumBitmaps();
 
314
  this->pixmaps = new QPixmap * [this->numPixmaps];
 
315
  QImage image(pwidth, pheight, 32);
 
316
  for (int i = 0; i < this->numPixmaps; i++) {
 
317
    this->wheel->drawBitmap(i, image.bits(), (this->orient == Vertical) ?
 
318
                            SoAnyThumbWheel::VERTICAL : SoAnyThumbWheel::HORIZONTAL);
 
319
    this->pixmaps[i] = new QPixmap(QSize(pwidth, pheight));
 
320
    bool s = this->pixmaps[i]->convertFromImage(image);
 
321
    if (!s && SOQT_DEBUG) {
 
322
      SoDebugError::post("SoQtThumbWheel::initWheel",
 
323
                         "Could not convert QImage to QPixmap, "
 
324
                         "for unknown reason.");
 
325
    }
 
326
  }
 
327
}
 
328
 
 
329
// *************************************************************************
 
330
 
 
331
void
 
332
SoQtThumbWheel::setEnabled(bool enable)
 
333
{
 
334
  if (enable)
 
335
    this->state = SoQtThumbWheel::Idle;
 
336
  else
 
337
    this->state = SoQtThumbWheel::Disabled;
 
338
  this->repaint(FALSE);
 
339
}
 
340
 
 
341
bool
 
342
SoQtThumbWheel::isEnabled(void) const
 
343
{
 
344
  return (this->state != SoQtThumbWheel::Disabled);
 
345
}
 
346
 
 
347
void
 
348
SoQtThumbWheel::setValue(float value)
 
349
{
 
350
  this->wheelValue = this->tempWheelValue = value;
 
351
  this->mouseDownPos = this->mouseLastPos;
 
352
  this->repaint(FALSE);
 
353
}
 
354
 
 
355
// *************************************************************************
 
356
 
 
357
void
 
358
SoQtThumbWheel::setRangeBoundaryHandling(boundaryHandling handling)
 
359
{
 
360
  switch (handling) {
 
361
  case CLAMP:
 
362
    this->wheel->setBoundaryHandling(SoAnyThumbWheel::CLAMP);
 
363
    break;
 
364
  case MODULATE:
 
365
    this->wheel->setBoundaryHandling(SoAnyThumbWheel::MODULATE);
 
366
    break;
 
367
  case ACCUMULATE:
 
368
    this->wheel->setBoundaryHandling(SoAnyThumbWheel::ACCUMULATE);
 
369
    break;
 
370
  default:
 
371
    assert(0 && "impossible");
 
372
  }
 
373
}
 
374
 
 
375
// *************************************************************************
 
376
 
 
377
SoQtThumbWheel::boundaryHandling
 
378
SoQtThumbWheel::getRangeBoundaryHandling(void) const
 
379
{
 
380
  switch (this->wheel->getBoundaryHandling()) {
 
381
  case SoAnyThumbWheel::CLAMP:
 
382
    return CLAMP;
 
383
  case SoAnyThumbWheel::MODULATE:
 
384
    return MODULATE;
 
385
  case SoAnyThumbWheel::ACCUMULATE:
 
386
    return ACCUMULATE;
 
387
  default:
 
388
    assert(0 && "impossible");
 
389
  }
 
390
  return CLAMP; // never reached
 
391
}
 
392
 
 
393
// *************************************************************************