QvisScribbleOpacityBar.C 15.1 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
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
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
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
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.
*
*****************************************************************************/

hrchilds's avatar
hrchilds committed
39 40
#include "QvisScribbleOpacityBar.h"

whitlocb's avatar
whitlocb committed
41 42 43 44 45
#include <QMouseEvent>
#include <QPainter>
#include <QPolygon>
#include <QPixmap>
#include <QImage>
hrchilds's avatar
hrchilds committed
46

hrchilds's avatar
hrchilds committed
47
#include <visitstream.h>
hrchilds's avatar
hrchilds committed
48 49 50
#include <math.h>
#include <stdlib.h>

hrchilds's avatar
hrchilds committed
51 52
#include <ColorControlPointList.h>

hrchilds's avatar
hrchilds committed
53 54 55 56 57 58 59 60 61 62 63 64 65
// ****************************************************************************
//  Method:  QvisScribbleOpacityBar::QvisScribbleOpacityBar
//
//  Purpose:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 31, 2001
//
//  Modifications:
//    Brad Whitlock, Thu Feb 14 12:57:49 PDT 2002
//    Added deletion of values array and the internal pixmap.
//
whitlocb's avatar
whitlocb committed
66 67 68
//    Brad Whitlock, Wed Jun  4 09:20:11 PDT 2008
//    Qt 4.
//
hrchilds's avatar
hrchilds committed
69 70
// ****************************************************************************

whitlocb's avatar
whitlocb committed
71 72
QvisScribbleOpacityBar::QvisScribbleOpacityBar(QWidget *parent)
    : QvisAbstractOpacityBar(parent)
hrchilds's avatar
hrchilds committed
73 74 75 76 77 78
{
    setFrameStyle( QFrame::Panel | QFrame::Sunken );
    setLineWidth( 2 );
    setMinimumHeight(50);
    setMinimumWidth(128);

whitlocb's avatar
whitlocb committed
79
    nvalues = 256;
hrchilds's avatar
hrchilds committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    values = new float[nvalues];
    for (int i=0; i<nvalues; ++i)
    {
        values[i] = float(i)/float(nvalues-1);
    }
    mousedown = false;
}

// ****************************************************************************
//  Method:  QvisScribbleOpacityBar::~QvisScribbleOpacityBar
//
//  Purpose:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 31, 2001
//
//  Modifications:
//    Brad Whitlock, Thu Feb 14 12:57:49 PDT 2002
//    Added deletion of values array.
//
// ****************************************************************************

QvisScribbleOpacityBar::~QvisScribbleOpacityBar()
{
    delete [] values;
}


// ****************************************************************************
//  Method:  QvisScribbleOpacityBar::paintToPixmap
//
//  Purpose:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 31, 2001
//
hrchilds's avatar
hrchilds committed
118
//  Modifications:
whitlocb's avatar
whitlocb committed
119 120 121
//    Gunther H. Weber, April 6, 2007
//    Added possibility of having a "color table" background instead of solid
//    black background.
hrchilds's avatar
hrchilds committed
122
//
whitlocb's avatar
whitlocb committed
123 124
//    Brad Whitlock, Wed Jun  4 09:55:53 PDT 2008
//    Qt 4.
hrchilds's avatar
hrchilds committed
125
//
126 127 128
//    Brad Whitlock, Thu Dec 18 11:45:20 PST 2008
//    I rewrote the method so it uses drawFilledCurve.
//
hrchilds's avatar
hrchilds committed
129
// ****************************************************************************
whitlocb's avatar
whitlocb committed
130

hrchilds's avatar
hrchilds committed
131
void
132
QvisScribbleOpacityBar::drawOpacities()
hrchilds's avatar
hrchilds committed
133 134
{
    QColor white(255, 255, 255 );
135
    if(histTexture != 0)
136
        drawFilledCurve(values, NULL, nvalues, white, 0.7f);
137
    else
138
        drawFilledCurve(values, NULL, nvalues, white, 1.f);
hrchilds's avatar
hrchilds committed
139 140 141 142 143 144 145 146 147 148 149 150
}


// ****************************************************************************
//  Method:  QvisScribbleOpacityBar::mousePressEvent
//
//  Purpose:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 31, 2001
//
whitlocb's avatar
whitlocb committed
151 152 153 154
//  Modifications:
//    Brad Whitlock, Wed Jun  4 09:58:43 PDT 2008
//    Use update().
//
hrchilds's avatar
hrchilds committed
155 156 157 158 159 160 161 162 163 164 165
// ****************************************************************************
void
QvisScribbleOpacityBar::mousePressEvent(QMouseEvent *e)
{
    int x = e->x();
    int y = e->y();
    setValue(x2val(x), y2val(y));
    lastx = x;
    lasty = y;
    mousedown = true;

166
    imageDirty();
whitlocb's avatar
whitlocb committed
167
    update();
hrchilds's avatar
hrchilds committed
168 169 170 171 172 173 174 175 176 177 178 179
}


// ****************************************************************************
//  Method:  QvisScribbleOpacityBar::mouseMoveEvent
//
//  Purpose:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 31, 2001
//
whitlocb's avatar
whitlocb committed
180 181 182 183
//  Modifications:
//    Brad Whitlock, Wed Jun  4 09:58:43 PDT 2008
//    Use update().
//
hrchilds's avatar
hrchilds committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197
// ****************************************************************************
void
QvisScribbleOpacityBar::mouseMoveEvent(QMouseEvent *e)
{
    if (!mousedown)
        return;

    int x = e->x();
    int y = e->y();
    setValues(lastx, lasty,
              x,     y);
    lastx = x;
    lasty = y;

198
    imageDirty();
whitlocb's avatar
whitlocb committed
199
    update();
hrchilds's avatar
hrchilds committed
200 201 202 203 204 205 206 207 208 209 210 211
}


// ****************************************************************************
//  Method:  QvisScribbleOpacityBar::mouseReleaseEvent
//
//  Purpose:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 31, 2001
//
whitlocb's avatar
whitlocb committed
212 213 214 215
//  Modifications:
//    Brad Whitlock, Wed Jun  4 09:58:43 PDT 2008
//    Use update().
//
hrchilds's avatar
hrchilds committed
216 217 218 219 220 221 222 223 224 225
// ****************************************************************************
void
QvisScribbleOpacityBar::mouseReleaseEvent(QMouseEvent *e)
{
    int x = e->x();
    int y = e->y();
    setValues(lastx, lasty,
              x,     y);
    mousedown = false;

226
    imageDirty();
whitlocb's avatar
whitlocb committed
227
    update();
hrchilds's avatar
hrchilds committed
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

    emit mouseReleased();
}


// ****************************************************************************
//  Method:  QvisScribbleOpacityBar::setValues
//
//  Purpose:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 31, 2001
//
// ****************************************************************************
void
QvisScribbleOpacityBar::setValues(int x1, int y1, int x2, int y2)
{
    if (x1==x2)
    {
        setValue(x2val(x2), y2val(y2));
        return;
    }

    int   xdiff = abs(x2 - x1) + 1;
    int   step  = (x1 < x2) ? 1 : -1;
    float slope = float(y2 - y1) / float (x2 - x1);
    for (int i=0; i<xdiff; i++)
        setValue(x2val(x1 + i*step),
                 y2val(y1 + int(float(i)*slope*step)));

}


// ****************************************************************************
//  Method:  QvisScribbleOpacityBar::setValue
//
//  Purpose:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 31, 2001
//
hrchilds's avatar
hrchilds committed
271
//  Modifications:
whitlocb's avatar
whitlocb committed
272 273 274
//    Gunther H. Weber, April 6, 2007
//    Added possibility of having a "color table" background instead of solid
//    black background.
hrchilds's avatar
hrchilds committed
275
//
whitlocb's avatar
whitlocb committed
276 277
//    Brad Whitlock, Wed Jun  4 10:24:39 PDT 2008
//    poke directly into the QImage.
hrchilds's avatar
hrchilds committed
278
//
279 280 281
//    Brad Whitlock, Thu Dec 18 11:46:45 PST 2008
//    I removed the code to poke into the image.
//
hrchilds's avatar
hrchilds committed
282
// ****************************************************************************
whitlocb's avatar
whitlocb committed
283

hrchilds's avatar
hrchilds committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 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
void
QvisScribbleOpacityBar::setValue(float xval, float yval)
{
    int x = int(xval * float(nvalues-1));
    values[x] = yval;
}


// ****************************************************************************
//  Method:  QvisScribbleOpacityBar::getRawOpacities
//
//  Purpose:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 31, 2001
//
// ****************************************************************************
float *
QvisScribbleOpacityBar::getRawOpacities(int n)
{
    int nvalues2 = n;
    float *values2 = new float[nvalues2];
    if (nvalues2 > nvalues)
    {
        for (int i=0; i<nvalues2; i++)
            values2[i] = values[(i * nvalues) / nvalues2];
    }
    else
    {
        for (int i=0; i<nvalues; i++)
            values2[(i * nvalues2) / nvalues] = values[i];
    }

    return values2;    
}


// ****************************************************************************
//  Method:  QvisScribbleOpacityBar::setRawOpacities
//
//  Purpose:
//    Sets all of the opacities in the widget.
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 31, 2001
//
//  Modifications:
//    Brad Whitlock, Fri Apr 6 12:27:22 PDT 2001
//    I added code to emit a valueChanged signal.
//
335 336 337
//    Brad Whitlock, Thu Dec 18 14:09:40 PST 2008
//    I changed how the image gets invalidated.
//
hrchilds's avatar
hrchilds committed
338
// ****************************************************************************
339

hrchilds's avatar
hrchilds committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353
void
QvisScribbleOpacityBar::setRawOpacities(int n, float *v)
{
    if (n < nvalues)
    {
        for (int i=0; i<nvalues; i++)
            values[i] = v[(i * n) / nvalues];
    }
    else
    {
        for (int i=0; i<n; i++)
            values[(i * nvalues) / n] = v[i];
    }

354
    imageDirty();
hrchilds's avatar
hrchilds committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    update();

    // Emit a signal indicating that the values changed.
    emit opacitiesChanged();
}

// ****************************************************************************
// Method: QvisScribbleOpacityBar::makeTotallyZero
//
// Purpose: 
//   This is a Qt slot function that sets all of the alpha values to zero
//   and updates the widget.
//
// Programmer: Brad Whitlock
// Creation:   Mon Feb 5 13:41:25 PST 2001
//
// Modifications:
372 373
//    Brad Whitlock, Thu Dec 18 14:09:40 PST 2008
//    I changed how the image gets invalidated.
hrchilds's avatar
hrchilds committed
374 375 376 377 378 379 380 381 382 383
//   
// ****************************************************************************

void
QvisScribbleOpacityBar::makeTotallyZero()
{
    // Set all the alphas to zero.
    for(int i = 0; i < nvalues; ++i)
        values[i] = 0.;

384
    imageDirty();
hrchilds's avatar
hrchilds committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    update();

    // Emit a signal indicating that the values changed.
    emit opacitiesChanged();
}

// ****************************************************************************
// Method: QvisScribbleOpacityBar::makeLinearRamp
//
// Purpose: 
//   This is a Qt slot function that sets the alpha values to be a linear ramp.
//
// Programmer: Brad Whitlock
// Creation:   Mon Feb 5 13:42:01 PST 2001
//
// Modifications:
401 402
//    Brad Whitlock, Thu Dec 18 14:09:40 PST 2008
//    I changed how the image gets invalidated.
hrchilds's avatar
hrchilds committed
403 404 405 406 407 408 409 410 411 412
//   
// ****************************************************************************

void
QvisScribbleOpacityBar::makeLinearRamp()
{
    // Make a ramp.
    for(int i = 0; i < nvalues; ++i)
        values[i] = float(i) * float(1. / nvalues);

413
    imageDirty();
hrchilds's avatar
hrchilds committed
414 415 416 417 418 419
    update();

    // Emit a signal indicating that the values changed.
    emit opacitiesChanged();
}

hrchilds's avatar
hrchilds committed
420 421 422 423 424 425 426 427 428 429 430
// ****************************************************************************
// Method: QvisScribbleOpacityBar::makeInverseLinearRamp
//
// Purpose: 
//   This is a Qt slot function that sets the alpha values to be an inverse
//   linear ramp.
//
// Programmer: Gunther H. Weber (based on makeLinearRamp() by Brad Whitlock)
// Creation:   Thu Apr  5 15:59:05 PDT 2007
//
// Modifications:
431 432
//   Brad Whitlock, Thu Dec 18 14:09:40 PST 2008
//   I changed how the image gets invalidated.
hrchilds's avatar
hrchilds committed
433 434 435 436 437 438 439 440 441 442
//   
// ****************************************************************************

void
QvisScribbleOpacityBar::makeInverseLinearRamp()
{
    // Make a ramp.
    for(int i = 0; i < nvalues; ++i)
        values[i] = float(nvalues - i - 1) * float(1. / nvalues);

443
    imageDirty();
hrchilds's avatar
hrchilds committed
444 445 446 447 448 449 450
    update();

    // Emit a signal indicating that the values changed.
    emit opacitiesChanged();
}


hrchilds's avatar
hrchilds committed
451 452 453 454 455 456 457 458 459 460 461
// ****************************************************************************
// Method: QvisScribbleOpacityBar::makeTotallyOne
//
// Purpose: 
//   This is a Qt slot function that sets all of the alpha values to
//   maximum strength.
//
// Programmer: Brad Whitlock
// Creation:   Mon Feb 5 13:42:35 PST 2001
//
// Modifications:
462 463
//   Brad Whitlock, Thu Dec 18 14:09:40 PST 2008
//   I changed how the image gets invalidated.
hrchilds's avatar
hrchilds committed
464 465 466 467 468 469 470 471 472 473
//   
// ****************************************************************************

void
QvisScribbleOpacityBar::makeTotallyOne()
{
    // Set all the alphas to 255.
    for(int i = 0; i < nvalues; ++i)
        values[i] = 1.;

474
    imageDirty();
hrchilds's avatar
hrchilds committed
475 476 477 478 479 480
    update();

    // Emit a signal indicating that the values changed.
    emit opacitiesChanged();
}

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 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

// ****************************************************************************
// Method: QvisScribbleOpacityBar::makeTent
//
// Purpose: 
//   This is a Qt slot function that sets a series of tents
//
// Programmer: Pascal Grosset
// Creation:   Mon Feb 12 15:33:35 MST 2001
//
// Modifications:
//   Brad Whitlock, Thu Dec 18 14:09:40 PST 2008
//   I changed how the image gets invalidated.
//   
// ****************************************************************************

void
QvisScribbleOpacityBar::makeTent()
{
    int numSplits = 12;
    float sizeOfOne = nvalues/(numSplits*2);
    float opacityValue = 0.85;
    float up = opacityValue/sizeOfOne;

    for(int i = 0; i < nvalues; ++i)
        values[i] = 0.;

    for(int i = 1; i < nvalues; i++){
        float sumUp = 0;
        for (int j=0; j<sizeOfOne-1; j++){
            if (i >= nvalues)
                break;
            sumUp = sumUp + up;
            values[i] = sumUp;
            i++;
        }
        for (int j=0; j<sizeOfOne-1; j++){
            if (i >= nvalues)
                break;
            sumUp = sumUp - up;
            values[i] = (sumUp>=0)?sumUp:0.0;
            i++;
        }
    }

    imageDirty();
    update();

    // Emit a signal indicating that the values changed.
    emit opacitiesChanged();
}

hrchilds's avatar
hrchilds committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546
// ****************************************************************************
// Method: QvisScribbleOpacityBar::smoothCurve
//
// Purpose: 
//   This is a Qt slot function that applies a simple filter to the alphas
//   in order to smooth them out.
//
// Programmer: Brad Whitlock
// Creation:   Mon Feb 5 13:43:15 PST 2001
//
// Modifications:
//   Brad Whitlock, Thu Feb 14 13:07:26 PST 2002
//   Fixed an ABR.
//
547 548 549
//   Brad Whitlock, Thu Dec 18 14:09:40 PST 2008
//   I changed how the image gets invalidated.
//
hrchilds's avatar
hrchilds committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
// ****************************************************************************

void
QvisScribbleOpacityBar::smoothCurve()
{
    // Smooth the curve
    for(int i = 1; i < nvalues - 1; ++i)
    {
        // 1 3 1 filter.
        float smooth = (0.2 * values[i - 1]) +
                       (0.6 * values[i]) + 
                       (0.2 * values[i + 1]);
        values[i] = (smooth > 1.) ? 1. : smooth;
    }

565
    imageDirty();
hrchilds's avatar
hrchilds committed
566 567 568 569 570
    update();

    // Emit a signal indicating that the values changed.
    emit opacitiesChanged();
}