QvisOpacitySlider.C 38.2 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
3
* Copyright (c) 2000 - 2014, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
brugger's avatar
 
brugger committed
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
brugger's avatar
 
brugger committed
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
brugger's avatar
 
brugger committed
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22 23 24 25 26
*    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
brugger's avatar
 
brugger committed
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30 31 32 33 34 35 36 37 38
* 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.
*
*****************************************************************************/

hkrishna's avatar
hkrishna committed
39
#include <qdrawutil.h>
hrchilds's avatar
hrchilds committed
40
#include <QvisOpacitySlider.h>
whitlocb's avatar
whitlocb committed
41 42 43 44 45 46 47 48 49 50
#include <QBitmap>
#include <QColor>
#include <QImage>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QPalette>
#include <QStyle>
#include <QStyleOption>
#include <QTimer>
hrchilds's avatar
hrchilds committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

// some static constants.
static const int thresholdTime = 500;
static const int repeatTime    = 100;
static int sliderStartVal = 0;

// ****************************************************************************
// Method: QvisOpacitySlider::QvisOpacitySlider
//
// Purpose: 
//   Constructor for the QvisOpacitySlider class.
//
// Arguments:
//   parent : The widget's parent.
//   name   : The widget's name.
//   data   : User data that is emitted with the valueChanged signal.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:14:41 PDT 2000
//
// Modifications:
whitlocb's avatar
whitlocb committed
72 73 74
//   Brad Whitlock, Thu Jun  5 11:20:03 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
75 76
// ****************************************************************************

whitlocb's avatar
whitlocb committed
77 78
QvisOpacitySlider::QvisOpacitySlider(QWidget *parent, const void *data) : 
    QAbstractSlider(parent)
hrchilds's avatar
hrchilds committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
    init();
    userData = data;
}

// ****************************************************************************
// Method: QvisOpacitySlider::QvisOpacitySlider
//
// Purpose: 
//   Constructor for the QvisOpacitySlider class.
//
// Arguments:
//   minValue : The minimum slider value.
//   maxValue : The maximum slider value.
//   step     : The page step value.
//   value    : The initial value.
//   parent   : The widget's parent.
//   name     : The widget's name.
//   data     : User data that is emitted with the valueChanged signal.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:14:41 PDT 2000
//
// Modifications:
whitlocb's avatar
whitlocb committed
103 104 105
//   Brad Whitlock, Thu Jun  5 11:19:54 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
106 107
// ****************************************************************************

whitlocb's avatar
whitlocb committed
108 109 110
QvisOpacitySlider::QvisOpacitySlider(int minValue, int maxValue, int step,
    int val, QWidget *parent, const void *data) :
    QAbstractSlider(parent)
hrchilds's avatar
hrchilds committed
111 112
{
    init();
whitlocb's avatar
whitlocb committed
113 114 115 116 117
    setMinimum(minValue);
    setMaximum(maxValue);
    setPageStep(step);
    setValue(val);

hrchilds's avatar
hrchilds committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    userData = data;
}

// ****************************************************************************
// Method: QvisOpacitySlider::~QvisOpacitySlider
//
// Purpose: 
//   Destructor for the QvisOpacitySlider class.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:18:17 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

QvisOpacitySlider::~QvisOpacitySlider()
{
whitlocb's avatar
whitlocb committed
136
    deleteGradientImage();
hrchilds's avatar
hrchilds committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

// ****************************************************************************
// Method: QvisOpacitySlider::init
//
// Purpose: 
//   Initializes certain object attributes.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:18:36 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::init()
{
whitlocb's avatar
whitlocb committed
155
    gradientImage = 0;
hrchilds's avatar
hrchilds committed
156 157 158 159 160 161
    timer = 0;
    sliderPos = 0;
    sliderVal = 0;
    clickOffset = 0;
    state = Idle;
    tickInt = 0;
whitlocb's avatar
whitlocb committed
162 163
    gradientColor = palette().color(QPalette::Window);
    setFocusPolicy(Qt::StrongFocus);
hrchilds's avatar
hrchilds committed
164
    initTicks();
whitlocb's avatar
whitlocb committed
165 166 167

    connect(this, SIGNAL(valueChanged(int)),
            this, SLOT(handle_valueChanged(int)));
hrchilds's avatar
hrchilds committed
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
}

// ****************************************************************************
// Method: QvisOpacitySlider::initTicks
//
// Purpose: 
//   Does what's needed when someone changes the tickmark status.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:19:28 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::initTicks()
{
    tickOffset = height() - thickness();
}

// ****************************************************************************
// Method: QvisOpacitySlider::positionFromValue
//
// Purpose: 
//   Calculates slider position corresponding to a value.
//
// Arguments:
//   value : The value for which to calculate a position.
//
// Returns:    Slider position.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:20:07 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

int
QvisOpacitySlider::positionFromValue(int value) const
{
whitlocb's avatar
whitlocb committed
210 211
    float t = float(value - minimum()) / float(maximum() - minimum());
    return (int)(t * available());
hrchilds's avatar
hrchilds committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225
}

// ****************************************************************************
// Method: QvisOpacitySlider::available
//
// Purpose: 
//   Returns the available space in which the slider can move.
//
// Returns:    The available space in which the slider can move
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:21:34 PDT 2000
//
// Modifications:
hrchilds's avatar
hrchilds committed
226
//   Brad Whitlock, Thu Nov 13 09:49:31 PDT 2003
whitlocb's avatar
whitlocb committed
227
//   I made it use imageWidth.
hrchilds's avatar
hrchilds committed
228
//
hrchilds's avatar
hrchilds committed
229 230 231 232 233
// ****************************************************************************

int
QvisOpacitySlider::available() const
{
whitlocb's avatar
whitlocb committed
234
    return imageWidth() - sliderLength();
hrchilds's avatar
hrchilds committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
}

// ****************************************************************************
// Method: QvisOpacitySlider::valueFromPosition
//
// Purpose: 
//   Calculates value corresponding to slider position.
//
// Returns:    Value corresponding to slider position.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:22:34 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

int
QvisOpacitySlider::valueFromPosition(int position) const
{
whitlocb's avatar
whitlocb committed
255 256 257
    float t = float(position) / float(available());
    float val = ((1.f - t) * float(minimum())) + (t * float(maximum()));
    return int(val);
hrchilds's avatar
hrchilds committed
258 259 260 261 262 263
}

// ****************************************************************************
// Method: QvisOpacitySlider::rangeChange
//
// Purpose: 
whitlocb's avatar
whitlocb committed
264
//   Implements the virtual QAbstractSlider function.
hrchilds's avatar
hrchilds committed
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
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:23:15 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::rangeChange()
{
    int newPos = positionFromValue(value());
    if(newPos != sliderPos)
        reallyMoveSlider( newPos );
}

// ****************************************************************************
// Method: QvisOpacitySlider::paletteChange
//
// Purpose: 
//   This method is called when the palette changes. The slider uses this
//   opportunity to redraw with its new gradient.
//
// Programmer: Brad Whitlock
// Creation:   Thu Sep 6 14:55:16 PST 2001
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::paletteChange(const QPalette &)
{
whitlocb's avatar
whitlocb committed
298
    deleteGradientImage();
hrchilds's avatar
hrchilds committed
299 300 301 302 303 304 305 306 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
    update();
}

// ****************************************************************************
// Method: QvisOpacitySlider::sliderRect
//
// Purpose: 
//   Returns the slider handle rectangle.
//
// Returns:    The slider handle rectangle.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:26:38 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

QRect
QvisOpacitySlider::sliderRect() const
{
    return QRect(sliderPos, tickOffset, sliderLength(), thickness());
}

// ****************************************************************************
// Method: QvisOpacitySlider::reallyMoveSlider
//
// Purpose: 
//   Performs the actual moving of the slider.
//
// Arguments:
//   newPos : The slider's new position.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:28:25 PDT 2000
//
// Modifications:
hrchilds's avatar
hrchilds committed
336 337 338
//   Brad Whitlock, Thu Nov 13 10:32:26 PDT 2003
//   I made it repaint the text value.
//
whitlocb's avatar
whitlocb committed
339 340 341
//   Brad Whitlock, Thu Jun  5 14:30:50 PDT 2008
//   Use regions and update().
//
hrchilds's avatar
hrchilds committed
342 343 344 345 346 347 348 349 350 351 352 353
// ****************************************************************************

void 
QvisOpacitySlider::reallyMoveSlider(int newPos)
{
    QRect oldR = sliderRect();
    sliderPos = newPos;
    QRect newR = sliderRect();

    // Since sliderRect isn't virtual, I know that oldR and newR
    // are the same size.
    if(oldR.left() < newR.left())
whitlocb's avatar
whitlocb committed
354
        oldR.setRight(qMin(oldR.right(), newR.left()));
hrchilds's avatar
hrchilds committed
355
    else
whitlocb's avatar
whitlocb committed
356
        oldR.setLeft(qMax(oldR.left(), newR.right()));
hrchilds's avatar
hrchilds committed
357

hrchilds's avatar
hrchilds committed
358
    // If we're moving the slider, we have to update the text.
whitlocb's avatar
whitlocb committed
359 360
    int pmw = imageWidth();
    QRegion newTextR(pmw, 0, width() - pmw, height());
hrchilds's avatar
hrchilds committed
361

whitlocb's avatar
whitlocb committed
362
    update(QRegion(oldR) + QRegion(newR) + newTextR);
hrchilds's avatar
hrchilds committed
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
}

// ****************************************************************************
// Method: QvisOpacitySlider::sliderLength
//
// Purpose: 
//   Returns the width of the slider handle.
//
// Returns:    The width of the slider handle.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:38:54 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

int
QvisOpacitySlider::sliderLength() const
{
   return 11;
}

// ****************************************************************************
// Method: QvisOpacitySlider::maximumSliderDragDistance
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:39:24 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

int
QvisOpacitySlider::maximumSliderDragDistance() const
{
    return 20;
}

// ****************************************************************************
// Method: QvisOpacitySlider::paintSlider
//
// Purpose: 
//   Paints the slider button.
//
// Arguments:
//   p : The painter used to paint the slider.
//   r : The slider rectangle.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:27:21 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
whitlocb's avatar
whitlocb committed
420
QvisOpacitySlider::paintSlider( QPainter *p, const QPalette &,
hrchilds's avatar
hrchilds committed
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
    const QRect &r )
{
    QPoint bo = p->brushOrigin();
    p->setBrushOrigin(r.topLeft());

    drawSlider(p, r.x(), r.y(), r.width(), r.height());
    p->setBrushOrigin(bo);
}

// ****************************************************************************
// Method: QvisOpacitySlider::drawSliderGroove
//
// Purpose: 
//   Draws the groove on which the slider handle slides.
//
// Arguments:
//   p      : The painter with which to draw.
//   x      : The groove's x location.
//   x      : The groove's x location.
//   width  : The groove's width.
//   height : The groove's height.
//   c      : A coordinate.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:37:18 PDT 2000
//
// Modifications:
whitlocb's avatar
whitlocb committed
448 449 450
//   Brad Whitlock, Thu Jun  5 11:30:44 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
451 452 453 454
// ****************************************************************************

void
QvisOpacitySlider::drawSliderGroove(QPainter *p, int x, int y, int w, int,
whitlocb's avatar
whitlocb committed
455
    int c)
hrchilds's avatar
hrchilds committed
456
{
hkrishna's avatar
hkrishna committed
457
    qDrawWinPanel(p, x, y + c - 2,  w, 4, palette(), true);
whitlocb's avatar
whitlocb committed
458
    p->setPen(palette().color(QPalette::Shadow));
hrchilds's avatar
hrchilds committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    p->drawLine(x+1, y + c - 1, x + w - 3, y + c - 1);
}

// ****************************************************************************
// Method: QvisOpacitySlider::drawSlider
//
// Purpose: 
//   Draws the slider handle.
//
// Arguments:
//   p      : The painter used to do the drawing.
//   x      : The x position of the slider handle.
//   y      : The y position of the slider handle.
//   width  : The width of the slider handle.
//   height : The height of the slider handle.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:32:38 PDT 2000
//
// Modifications:
whitlocb's avatar
whitlocb committed
479 480 481
//   Brad Whitlock, Thu Jun  5 11:29:44 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
482 483 484 485 486 487
// ****************************************************************************

void
QvisOpacitySlider::drawSlider(QPainter *p, int x, int y, int w, int h)
{
    // Get some colors from the widget's colorgroup.
whitlocb's avatar
whitlocb committed
488 489 490 491
    const QColor c0 = palette().color(QPalette::Shadow);
    const QColor c1 = palette().color(QPalette::Dark);
    const QColor c3 = palette().color(QPalette::Mid);
    const QColor c4 = palette().color(QPalette::Light);
hrchilds's avatar
hrchilds committed
492 493 494 495 496 497 498 499 500

    int x1 = x;
    int x2 = x+w-1;
    int y1 = y;
    int y2 = y+h-1;
    int d = 0;
    y1 = y1 + w/2;
    d =  (w + 1) / 2 - 1;

whitlocb's avatar
whitlocb committed
501
    QPolygon a;
hrchilds's avatar
hrchilds committed
502 503 504
    a.setPoints(5, x1,y1, x1,y2, x2,y2, x2,y1, x1+d,y1-d);

    QBrush oldBrush = p->brush();
whitlocb's avatar
whitlocb committed
505 506
    p->setBrush(palette().button());
    p->setPen(Qt::NoPen);
hrchilds's avatar
hrchilds committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    p->drawRect(x1, y1, x2-x1+1, y2-y1+1);
    p->drawPolygon(a);
    p->setBrush(oldBrush);

    p->setPen(c3);
    p->drawLine(x1+1, y1+1, x1+1, y2);
    p->setPen(c4);
    p->drawLine(x1, y1, x1, y2);
    p->setPen(c0);
    p->drawLine( x2, y1, x2, y2);
    p->setPen(c1);
    p->drawLine( x2-1, y1+1, x2-1, y2-1);
    p->setPen(c0);
    p->drawLine(x1, y2, x2, y2);
    p->setPen(c1);
    p->drawLine(x1+1, y2-1, x2-1, y2-1);
    p->setPen(c4);
    p->drawLine(x1, y1, x1+d, y1-d);
    p->setPen(c0);
    d = w - d - 1;
    p->drawLine(x2, y1, x2-d, y1-d);
    --d;
    p->setPen(c3);
    p->drawLine(x1+1, y1, x1+1+d, y1-d);
    p->setPen(c1);
    p->drawLine(x2-1, y1, x2-1-d, y1-d);
}

// ****************************************************************************
// Method: QvisOpacitySlider::drawTicks
//
// Purpose: 
//   Draws the slider's tick marks.
//
// Arguments:
//   p    : The painter used to draw.
//   dist : The distance from the edge of the widget.
//   w    : The number of pixels between the ticks.
//   i    : The tick interval
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 13:13:53 PST 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::drawTicks(QPainter *p, int dist, int w, int i) const
{
whitlocb's avatar
whitlocb committed
557
    drawTicks(p, palette(), dist, w, i);
hrchilds's avatar
hrchilds committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
}

// ****************************************************************************
// Method: QvisOpacitySlider::drawTicks
//
// Purpose: 
//   Draws the slider's tick marks.
//
// Arguments:
//   p    : The painter used to draw.
//   g    : The color group containing common GUI colors.
//   dist : The distance from the edge of the widget.
//   w    : The number of pixels between the ticks.
//   i    : The tick interval
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 13:13:53 PST 2000
//
// Modifications:
whitlocb's avatar
whitlocb committed
577 578 579
//   Brad Whitlock, Thu Jun  5 11:44:18 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
580 581 582
// ****************************************************************************

void
whitlocb's avatar
whitlocb committed
583
QvisOpacitySlider::drawTicks( QPainter *p, const QPalette& g, int dist,
hrchilds's avatar
hrchilds committed
584 585
    int w, int i) const
{
whitlocb's avatar
whitlocb committed
586 587
    p->setPen(g.color(QPalette::Text));
    int v = minimum();
hrchilds's avatar
hrchilds committed
588
    int fudge = sliderLength() / 2 + 1;
whitlocb's avatar
whitlocb committed
589
    while(v <= maximum() + 1)
hrchilds's avatar
hrchilds committed
590 591 592 593 594 595 596
    {
        int pos = positionFromValue(v) + fudge;
        p->drawLine( pos, dist, pos, dist + w );
        v += i;
    }
}

hrchilds's avatar
hrchilds committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
// ****************************************************************************
// Method: QvisOpacitySlider::textPadding
//
// Purpose: 
//   Returns the distance from the slider to the text.
//
// Returns:    The distance from the slider to the text.
//
// Programmer: Brad Whitlock
// Creation:   Thu Nov 13 10:23:56 PDT 2003
//
// Modifications:
//   
// ****************************************************************************

int
QvisOpacitySlider::textPadding() const
{
    return fontMetrics().width("0") / 2;
}

// ****************************************************************************
whitlocb's avatar
whitlocb committed
619
// Method: QvisOpacitySlider::imageWidth
hrchilds's avatar
hrchilds committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
//
// Purpose: 
//   Returns the width of the pixmap area, which is the width of the whole
//   widget minus the width if the text that we want to display.
//
// Returns:    The width of the pixmap area.
//
// Programmer: Brad Whitlock
// Creation:   Thu Nov 13 09:45:41 PDT 2003
//
// Modifications:
//   
// ****************************************************************************

int
whitlocb's avatar
whitlocb committed
635
QvisOpacitySlider::imageWidth() const
hrchilds's avatar
hrchilds committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
{
    return width() - fontMetrics().width("100%") - textPadding();
}

// ****************************************************************************
// Method: QvisOpacitySlider::paintValueText
//
// Purpose: 
//   Draws the value text at the specified location.
//
// Arguments:
//   p : The painter to use to draw the text.
//   x : The x location at which to draw the text.
//   h : The vertical height that we have to work with.
//
// Programmer: Brad Whitlock
// Creation:   Thu Nov 13 10:19:22 PDT 2003
//
// Modifications:
whitlocb's avatar
whitlocb committed
655 656 657
//   Brad Whitlock, Thu Jun  5 14:29:03 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
658 659 660
// ****************************************************************************

void
whitlocb's avatar
whitlocb committed
661
QvisOpacitySlider::paintValueText(QPainter *p, const QPalette &cg, int x,
hrchilds's avatar
hrchilds committed
662 663 664 665
    int h)
{
    // Create the text that we have to display.
    int v = (state == Dragging) ? (valueFromPosition(sliderPos)) : value();
whitlocb's avatar
whitlocb committed
666
    float t = float(v - minimum()) / float(maximum() - minimum());
hrchilds's avatar
hrchilds committed
667 668 669 670 671 672 673
    QString txt; txt.sprintf("%d%%", int(t * 100.f));

    // Figure out the y offset.
    int dy = h - fontMetrics().height();
    int y = (h - dy / 2);

    // Set the brush and draw the text.
whitlocb's avatar
whitlocb committed
674
    p->setPen(cg.color(QPalette::Text));
hrchilds's avatar
hrchilds committed
675 676 677
    p->drawText(x + textPadding(), y, txt);
}

hrchilds's avatar
hrchilds committed
678
// ****************************************************************************
whitlocb's avatar
whitlocb committed
679
// Method: QvisOpacitySlider::createGradientImage
hrchilds's avatar
hrchilds committed
680 681 682 683 684 685 686 687 688 689 690 691 692
//
// Purpose: 
//   Creates the opacity gradient pixmap that is used as the slider's 
//   background pixmap.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:30:35 PDT 2000
//
// Modifications:
//   Brad Whitlock, Wed Jan 30 14:16:41 PST 2002
//   Added code to draw checkerboard over the gradient if the widget
//   is disabled.
//
hrchilds's avatar
hrchilds committed
693 694 695 696
//   Brad Whitlock, Thu Aug 21 17:31:30 PST 2003
//   Added code to do alpha blending with a pixmap background so it looks
//   like it's supposed to look on MacOS X.
//
hrchilds's avatar
hrchilds committed
697 698 699
//   Brad Whitlock, Thu Nov 13 09:47:15 PDT 2003
//   I made it use a smaller width so we can display the percent.
//
whitlocb's avatar
whitlocb committed
700 701 702
//   Brad Whitlock, Thu Jun  5 14:17:51 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
703 704 705
// ****************************************************************************

void
whitlocb's avatar
whitlocb committed
706
QvisOpacitySlider::createGradientImage()
hrchilds's avatar
hrchilds committed
707 708
{
    // Create the pixmap.
whitlocb's avatar
whitlocb committed
709
    int w = imageWidth();
hrchilds's avatar
hrchilds committed
710
    int h = height() - tickOffset;
whitlocb's avatar
whitlocb committed
711
    gradientImage = new QImage(w, h, QImage::Format_RGB32);
hrchilds's avatar
hrchilds committed
712

whitlocb's avatar
whitlocb committed
713 714
    QBrush brush(palette().window());
    QPainter paint(gradientImage);
hrchilds's avatar
hrchilds committed
715

whitlocb's avatar
whitlocb committed
716
    if(!brush.textureImage().isNull())
hrchilds's avatar
hrchilds committed
717
    {
hrchilds's avatar
hrchilds committed
718
        // Paint the background into the pixmap.
whitlocb's avatar
whitlocb committed
719 720
        paint.fillRect(0, 0, w, h, brush);
        for(int i = 0; i < w; ++i)
hrchilds's avatar
hrchilds committed
721
        {
whitlocb's avatar
whitlocb committed
722 723 724 725
            float t = float(i) / float(w - 1);
            float omt = 1.f - t;
            int   rc, gc, bc;
            for(int j = 0; j < h; ++j)
hrchilds's avatar
hrchilds committed
726
            {
whitlocb's avatar
whitlocb committed
727 728 729 730 731 732 733 734 735
                // Alpha blend with the pixel that's there already.
                QRgb p = gradientImage->pixel(i, j);
                rc = int(omt * float(qRed(p)) + t * float(gradientColor.red()));
                rc = (rc > 255) ? 255 : rc;
                gc = int(omt * float(qGreen(p)) + t * float(gradientColor.green()));
                gc = (gc > 255) ? 255 : gc;
                bc = int(omt * float(qBlue(p)) + t * float(gradientColor.blue()));
                bc = (bc > 255) ? 255 : bc;
                gradientImage->setPixel(i, j, qRgb(rc, gc, bc));
hrchilds's avatar
hrchilds committed
736 737 738 739 740
            }
        }
    }
    else
    {
whitlocb's avatar
whitlocb committed
741 742 743 744 745 746
        QLinearGradient grad(QPointF(0.,0.), QPointF(1.,0.));
        grad.setCoordinateMode(QGradient::ObjectBoundingMode);
        grad.setColorAt(0., palette().color(QPalette::Background));
        grad.setColorAt(1., gradientColor);
        QBrush gradBrush(grad);
        paint.fillRect(0, 0, w, h, gradBrush);
hrchilds's avatar
hrchilds committed
747 748 749 750 751
    }

    // If the widget is disabled then draw a checkerboard over it.
    if(!isEnabled())
    {
whitlocb's avatar
whitlocb committed
752 753 754
        QBrush brush2(palette().window());
        brush2.setStyle(Qt::Dense6Pattern);
        paint.fillRect(rect(), brush2);
hrchilds's avatar
hrchilds committed
755 756 757 758
    }
}

// ****************************************************************************
whitlocb's avatar
whitlocb committed
759
// Method: QvisOpacitySlider::deleteGradientImage
hrchilds's avatar
hrchilds committed
760 761 762 763 764 765 766 767 768 769 770 771
//
// Purpose: 
//   Delete the gradient pixmap.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:31:26 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
whitlocb's avatar
whitlocb committed
772
QvisOpacitySlider::deleteGradientImage()
hrchilds's avatar
hrchilds committed
773
{
whitlocb's avatar
whitlocb committed
774
    if(gradientImage != 0)
hrchilds's avatar
hrchilds committed
775
    {
whitlocb's avatar
whitlocb committed
776 777
        delete gradientImage;
        gradientImage = 0;
hrchilds's avatar
hrchilds committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
    }
}

// ****************************************************************************
// Method: QvisOpacitySlider::setGradientColor
//
// Purpose: 
//   Sets the color used to generate the gradient pixmap.
//
// Arguments:
//   color : The new gradient color.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:31:44 PDT 2000
//
// Modifications:
//   Brad Whitlock, Mon Mar 5 14:55:23 PST 2001
//   Added code to prevent updates if the new color is the same as the old
//   color.
//
// ****************************************************************************

void
QvisOpacitySlider::setGradientColor(const QColor &color)
{
    if(gradientColor != color)
    {
        gradientColor = color;
whitlocb's avatar
whitlocb committed
806
        deleteGradientImage();
hrchilds's avatar
hrchilds committed
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

        if(isVisible())
            update();
    }
}

// ****************************************************************************
// Method: QvisOpacitySlider::resizeEvent
//
// Purpose: 
//   Called when the widget needs to be resized.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:24:59 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::resizeEvent(QResizeEvent *)
{
    rangeChange();
    initTicks();

    // Delete the gradient pixmap so it will be regenerated before the next
    // paint event.
whitlocb's avatar
whitlocb committed
834
    deleteGradientImage();
hrchilds's avatar
hrchilds committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
}

// ****************************************************************************
// Method: QvisOpacitySlider::paintEvent
//
// Purpose: 
//   This method is called when the widget gets a paint event. It redraws the
//   widget.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:41:02 PDT 2000
//
// Modifications:
//   Brad Whitlock, Mon Mar 11 12:42:43 PDT 2002
//   Upgraded to Qt 3.0.
//
hrchilds's avatar
hrchilds committed
851 852 853
//   Brad Whitlock, Thu Aug 21 17:33:54 PST 2003
//   I fixed it so it looks better on MacOS X.
//
hrchilds's avatar
hrchilds committed
854 855 856
//   Brad Whitlock, Thu Nov 13 09:48:31 PDT 2003
//   I made the width smaller so we could display the percent.
//
hrchilds's avatar
hrchilds committed
857 858 859 860 861 862 863 864 865
// ****************************************************************************

void
QvisOpacitySlider::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    int mid = thickness()/2 + sliderLength() / 8;

    // Draw the gradient pixmap.
whitlocb's avatar
whitlocb committed
866 867 868
    if(gradientImage == 0)
        createGradientImage();
    p.drawImage(0, tickOffset, *gradientImage);
hrchilds's avatar
hrchilds committed
869 870

    // Draw the groove on which the slider slides.    
whitlocb's avatar
whitlocb committed
871
    drawSliderGroove(&p, 0, tickOffset, imageWidth(), thickness(), mid);
hrchilds's avatar
hrchilds committed
872 873 874 875 876

    // Figure out the interval between the tick marks.
    int interval = tickInt;
    if(interval <= 0)
    {
whitlocb's avatar
whitlocb committed
877
        interval = singleStep();
hrchilds's avatar
hrchilds committed
878 879 880 881 882
        if(positionFromValue(interval) - positionFromValue(0) < 3)
            interval = pageStep();
    }

    // Draw the tick marks.
whitlocb's avatar
whitlocb committed
883 884 885 886 887
    p.fillRect(0, 0, imageWidth(), tickOffset,
               palette().brush(QPalette::Background));
    p.fillRect(0, tickOffset + thickness(), imageWidth(), height(),
               palette().brush(QPalette::Background));
    drawTicks(&p, palette(), 0, tickOffset - 2, interval);
hrchilds's avatar
hrchilds committed
888 889

    // Draw the slider
whitlocb's avatar
whitlocb committed
890
    paintSlider(&p, palette(), sliderRect());
hrchilds's avatar
hrchilds committed
891 892

    // Draw the value text.
whitlocb's avatar
whitlocb committed
893
    paintValueText(&p, palette(), imageWidth(), height());
hrchilds's avatar
hrchilds committed
894

whitlocb's avatar
whitlocb committed
895 896
    // If this widget has focus, draw the focus rectangle.
    if(hasFocus())
hrchilds's avatar
hrchilds committed
897
    {
whitlocb's avatar
whitlocb committed
898 899 900 901 902
        QStyleOptionFocusRect so;
        so.initFrom(this);
        style()->drawPrimitive(QStyle::PE_FrameFocusRect, 
                               &so,
                               &p);
hrchilds's avatar
hrchilds committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
    }
}

// ****************************************************************************
// Method: QvisOpacitySlider::mousePressEvent
//
// Purpose: 
//   This method is called when the mouse is pressed in this widget.
//
// Arguments:
//   e : The mouse event.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:43:54 PDT 2000
//
// Modifications:
whitlocb's avatar
whitlocb committed
919 920 921
//   Brad Whitlock, Thu Jun  5 14:21:18 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
922 923 924 925 926 927 928 929 930
// ****************************************************************************

void
QvisOpacitySlider::mousePressEvent(QMouseEvent *e)
{
    resetState();
    sliderStartVal = sliderVal;
    QRect r = sliderRect();

whitlocb's avatar
whitlocb committed
931
    if(e->button() == Qt::RightButton)
hrchilds's avatar
hrchilds committed
932 933 934 935 936 937
    {
        return;
    }
    else if(r.contains(e->pos()))
    {
        state = Dragging;
whitlocb's avatar
whitlocb committed
938
        clickOffset = (int)(e->pos().x() - sliderPos);
hrchilds's avatar
hrchilds committed
939 940
//    emit sliderPressed();
    }
whitlocb's avatar
whitlocb committed
941
    else if(e->button() == Qt::MidButton)
hrchilds's avatar
hrchilds committed
942
    {
whitlocb's avatar
whitlocb committed
943
        int pos = e->pos().x();
hrchilds's avatar
hrchilds committed
944 945 946 947 948 949 950 951 952 953 954
        moveSlider(pos - sliderLength() / 2);
        state = Dragging;
        clickOffset = sliderLength() / 2;
    }
    else if(e->pos().x() < r.left())
    {
        state = TimingDown;
        subtractPage();
        if(!timer)
            timer = new QTimer(this);
        connect( timer, SIGNAL(timeout()), SLOT(repeatTimeout()));
whitlocb's avatar
whitlocb committed
955 956
        timer->setSingleShot(true);
        timer->start(thresholdTime);
hrchilds's avatar
hrchilds committed
957 958 959 960 961 962 963 964
    }
    else if(e->pos().x() > r.right())
    {
        state = TimingUp;
        addPage();
        if(!timer)
            timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), SLOT(repeatTimeout()));
whitlocb's avatar
whitlocb committed
965 966
        timer->setSingleShot(true);
        timer->start(thresholdTime);
hrchilds's avatar
hrchilds committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    }
}

// ****************************************************************************
// Method: QvisOpacitySlider::mouseMoveEvent
//
// Purpose: 
//   This method is called when the mouse is moved in this widget.
//
// Arguments:
//   e : The mouse move event.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:46:37 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::mouseMoveEvent(QMouseEvent *e)
{
    if(state != Dragging)
        return;

    QRect r = rect();
    int m = maximumSliderDragDistance();
    if(m >= 0)
    {
        r.setRect(r.x() - m, r.y() - 2*m/3,
                  r.width() + 2*m, r.height() + 3*m);
        if(!r.contains(e->pos()))
        {
            moveSlider( positionFromValue( sliderStartVal) );
            return;
        }
    }

whitlocb's avatar
whitlocb committed
1005
    moveSlider(e->pos().x() - clickOffset );
hrchilds's avatar
hrchilds committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
}

// ****************************************************************************
// Method: QvisOpacitySlider::wheelEvent
//
// Purpose: 
//   This method is called when the widget gets a wheel event.
//
// Arguments:
//   e : The wheel event.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:48:10 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::wheelEvent(QWheelEvent * e)
{
    static float offset = 0;
    static QvisOpacitySlider* offset_owner = 0;
    if(offset_owner != this)
    {
        offset_owner = this;
        offset = 0;
    }
whitlocb's avatar
whitlocb committed
1034 1035
    offset += -e->delta()*qMax(pageStep(),singleStep())/120;
    if(qAbs(offset)<1)
hrchilds's avatar
hrchilds committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
        return;
    setValue( value() + int(offset) );
    offset -= int(offset);
}

// ****************************************************************************
// Method: QvisOpacitySlider::mouseReleaseEvent
//
// Purpose: 
//   This method is called when the mouse button is released in the widget.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:48:56 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::mouseReleaseEvent(QMouseEvent *)
{
    resetState();
}

// ****************************************************************************
// Method: QvisOpacitySlider::moveSlider
//
// Purpose: 
//   Moves the left (or top) edge of the slider to position pos. Performs
//   snapping.
//
// Arguments:
//   pos : The slider's new position.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:51:00 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::moveSlider(int pos)
{
whitlocb's avatar
whitlocb committed
1080
    int newPos = qMin(available(), qMax( 0, pos));
hrchilds's avatar
hrchilds committed
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
    int newVal = valueFromPosition(newPos);
    newPos = positionFromValue( newVal );

    if(sliderPos != newPos)
        reallyMoveSlider(newPos);
}

// ****************************************************************************
// Method: QvisOpacitySlider::resetState
//
// Purpose: 
//   Resets all state information and stops the timer
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:52:13 PDT 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::resetState()
{
    if(timer )
    {
        timer->stop();
        timer->disconnect();
    }

    switch(state)
    {
    case TimingUp:
    case TimingDown:
        break;
    case Dragging:
        setValue(valueFromPosition(sliderPos));
//        emit sliderReleased();
        break;
    case Idle:
        break;
    }

    state = Idle;
}

// ****************************************************************************
// Method: QvisOpacitySlider::keyPressEvent
//
// Purpose: 
//   Called when the widget needs to respond to a key press.
//
// Arguments:
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:53:45 PDT 2000
//
// Modifications:
whitlocb's avatar
whitlocb committed
1142 1143 1144
//   Brad Whitlock, Thu Jun  5 14:23:51 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
1145 1146 1147 1148 1149 1150 1151
// ****************************************************************************

void
QvisOpacitySlider::keyPressEvent(QKeyEvent *e)
{
    switch(e->key())
    {
whitlocb's avatar
whitlocb committed
1152 1153 1154 1155 1156
    case Qt::Key_Left:
        if((e->modifiers() & Qt::ShiftModifier) > 0)
            subtractPage();
        else
            subtractLine();
hrchilds's avatar
hrchilds committed
1157
        break;
whitlocb's avatar
whitlocb committed
1158 1159 1160 1161 1162
    case Qt::Key_Right:
        if((e->modifiers() & Qt::ShiftModifier) > 0)
            addPage();
        else
            addLine();
hrchilds's avatar
hrchilds committed
1163
        break;
whitlocb's avatar
whitlocb committed
1164
    case Qt::Key_PageDown:
hrchilds's avatar
hrchilds committed
1165 1166
        subtractPage();
        break;
whitlocb's avatar
whitlocb committed
1167
    case Qt::Key_PageUp:
hrchilds's avatar
hrchilds committed
1168 1169
        addPage();
        break;
whitlocb's avatar
whitlocb committed
1170 1171
    case Qt::Key_Home:
        setValue( minimum() );
hrchilds's avatar
hrchilds committed
1172
        break;
whitlocb's avatar
whitlocb committed
1173 1174
    case Qt::Key_End:
        setValue( maximum() );
hrchilds's avatar
hrchilds committed
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
        break;
    default:
        e->ignore();
    }
}

// ****************************************************************************
// Method: QvisOpacitySlider::setValue
//
// Purpose: 
whitlocb's avatar
whitlocb committed
1185
//   Makes QAbstractSlider::setValue() available as a slot.
hrchilds's avatar
hrchilds committed
1186 1187 1188 1189 1190 1191 1192 1193
//
// Arguments:
//   value : The slider's new value.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 12:55:39 PDT 2000
//
// Modifications:
whitlocb's avatar
whitlocb committed
1194 1195 1196
//   Brad Whitlock, Thu Jun  5 15:42:05 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
1197 1198 1199 1200 1201
// ****************************************************************************

void
QvisOpacitySlider::setValue(int value)
{
whitlocb's avatar
whitlocb committed
1202 1203 1204 1205 1206 1207 1208
    int v = value;
    if(v < minimum())
        v = minimum();
    if(v > maximum())
        v = maximum();
    QAbstractSlider::setValue(v);
    rangeChange();
hrchilds's avatar
hrchilds committed
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
}

// ****************************************************************************
// Method: QvisOpacitySlider::setEnabled
//
// Purpose: 
//   Sets the widget's enabled state and causes it to repaint if necessary.
//
// Arguments:
//   val : The new enabled state.
//
// Programmer: Brad Whitlock
// Creation:   Thu Jan 31 09:25:55 PDT 2002
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::setEnabled(bool val)
{
    if(isEnabled() != val)
whitlocb's avatar
whitlocb committed
1231
        deleteGradientImage();
hrchilds's avatar
hrchilds committed
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289

    QWidget::setEnabled(val);
}

// ****************************************************************************
// Method: QvisOpacitySlider::addStep
//
// Purpose: 
//   Moves the slider one pageStep() upwards.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 13:20:52 PST 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::addStep()
{
    addPage();
}

// ****************************************************************************
// Method: QvisOpacitySlider::subtractStep
//
// Purpose: 
//   Moves the slider one pageStep() downwards.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 13:21:16 PST 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::subtractStep()
{
    subtractPage();
}

// ****************************************************************************
// Method: QvisOpacitySlider::repeatTimeout
//
// Purpose: 
//   Waits for autorepeat.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 13:21:35 PST 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::repeatTimeout()
{
whitlocb's avatar
whitlocb committed
1290 1291
    if(timer==0)
        return;
hrchilds's avatar
hrchilds committed
1292 1293 1294 1295 1296
    timer->disconnect();
    if(state == TimingDown)
        connect(timer, SIGNAL(timeout()), SLOT(subtractStep()));
    else if ( state == TimingUp )
        connect(timer, SIGNAL(timeout()), SLOT(addStep()));
whitlocb's avatar
whitlocb committed
1297 1298
    timer->setSingleShot(false);
    timer->start(repeatTime);
hrchilds's avatar
hrchilds committed
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
}

// ****************************************************************************
// Method: QvisOpacitySlider::sizeHint
//
// Purpose: 
//   Returns the widget's preferred size.
//
// Returns:    The widget's preferred size.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 13:20:15 PST 2000
//
// Modifications:
//   
// ****************************************************************************

QSize
QvisOpacitySlider::sizeHint() const
{
    const int length = 150;
    int thick = 16;
    const int tickSpace = 5;

    thick += tickSpace;
    thick += sliderLength() / 4;

    return QSize(length, thick);
}

// ****************************************************************************
// Method: QvisOpacitySlider::minimumSizeHint
//
// Purpose: 
//   Returns the widget's minimum size.
//
// Returns:    The widget's minimum size.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 13:19:43 PST 2000
//
// Modifications:
//   
// ****************************************************************************

QSize
QvisOpacitySlider::minimumSizeHint() const
{
    QSize s = sizeHint();
    s.setWidth(sliderLength());

    return s;
}

// ****************************************************************************
// Method: QvisOpacitySlider::sizePolicy
//
// Purpose: 
//   Returns the widget's size policy.
//
// Returns:    The widget's size policy.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 13:17:36 PST 2000
//
// Modifications:
//   
// ****************************************************************************

QSizePolicy
QvisOpacitySlider::sizePolicy() const
{
    return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
}

// ****************************************************************************
// Method: QvisOpacitySlider::thickness
//
// Purpose: 
//   Returns the number of pixels to use for the business part of the
//    slider (i.e. the non-tickmark portion). The remaining space is shared
//    equally between the tickmark regions. This function and  sizeHint()
//    are closely related; if you change one, you almost certainly
//    have to change the other.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 13:18:07 PST 2000
//
// Modifications:
//   
// ****************************************************************************

int
QvisOpacitySlider::thickness() const
{
    int space = height();
    int thick = 6 + sliderLength() / 4;
    space -= thick;

    if(space > 0)
    thick += (space * 2) / 3;

    return thick;
}

// ****************************************************************************
// Method: QvisOpacitySlider::setTickInterval
//
// Purpose: 
//   Sets a new tick interval.
//
// Arguments:
//   i : The new tick interval.
//
// Programmer: Brad Whitlock
// Creation:   Thu Dec 7 13:18:58 PST 2000
//
// Modifications:
//   
// ****************************************************************************

void
QvisOpacitySlider::setTickInterval(int i)
{
whitlocb's avatar
whitlocb committed
1423
    tickInt = qMax(0, i);
hrchilds's avatar
hrchilds committed
1424 1425 1426 1427
    update();
}

void
whitlocb's avatar
whitlocb committed
1428
QvisOpacitySlider::addPage()
hrchilds's avatar
hrchilds committed
1429
{
whitlocb's avatar
whitlocb committed
1430
    setValue(value() + pageStep());
hrchilds's avatar
hrchilds committed
1431 1432
}

whitlocb's avatar
whitlocb committed
1433 1434
void
QvisOpacitySlider::subtractPage()
hrchilds's avatar
hrchilds committed
1435
{
whitlocb's avatar
whitlocb committed
1436
    setValue(value() - pageStep());
hrchilds's avatar
hrchilds committed
1437 1438 1439
}

void
whitlocb's avatar
whitlocb committed
1440
QvisOpacitySlider::addLine()
hrchilds's avatar
hrchilds committed
1441
{
whitlocb's avatar
whitlocb committed
1442
    setValue(value() + singleStep());
hrchilds's avatar
hrchilds committed
1443 1444 1445
}

void
whitlocb's avatar
whitlocb committed
1446
QvisOpacitySlider::subtractLine()
hrchilds's avatar
hrchilds committed
1447
{
whitlocb's avatar
whitlocb committed
1448
    setValue(value() - singleStep());
hrchilds's avatar
hrchilds committed
1449 1450 1451
}

// ****************************************************************************
whitlocb's avatar
whitlocb committed
1452
// Method: QvisOpacitySlider::valueChanged
hrchilds's avatar
hrchilds committed
1453 1454
//
// Purpose: 
whitlocb's avatar
whitlocb committed
1455
//   Implements the virtual QAbstractSlider function.
hrchilds's avatar
hrchilds committed
1456 1457
//
// Programmer: Brad Whitlock
whitlocb's avatar
whitlocb committed
1458
// Creation:   Thu Dec 7 12:23:57 PDT 2000
hrchilds's avatar
hrchilds committed
1459 1460 1461 1462 1463
//
// Modifications:
//   
// ****************************************************************************

whitlocb's avatar
whitlocb committed
1464 1465
void
QvisOpacitySlider::handle_valueChanged(int val)
hrchilds's avatar
hrchilds committed
1466
{
whitlocb's avatar
whitlocb committed
1467 1468
    // emit the valueChanged with user data signal.
    emit valueChanged(val, userData);
hrchilds's avatar
hrchilds committed
1469
}