QvisHistogramLimits.C 17.2 KB
Newer Older
1 2
/*****************************************************************************
*
3
* Copyright (c) 2000 - 2014, Lawrence Livermore National Security, LLC
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 42 43 44 45 46 47 48 49 50 51
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-442911
* All rights reserved.
*
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
* 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
*    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
*    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 LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  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.
*
*****************************************************************************/

#include <QvisHistogramLimits.h>

#include <QGridLayout>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QMouseEvent>
#include <QPainter>
#include <QStyle>
#include <QTimer>

#include <QvisHistogram.h>

whitlocb's avatar
whitlocb committed
52 53 54 55 56
#include <float.h>
#define MIN_VALUE -FLT_MAX
#define MAX_VALUE  FLT_MAX
#define EPSILON (100 * FLT_MIN)

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 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
// ****************************************************************************
// Method: QvisHistogramLimits::QvisHistogramLimits
//
// Purpose: 
//   Constructor
//
// Arguments:
//   parent : The widget's parent.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

QvisHistogramLimits::QvisHistogramLimits(QWidget *parent) :
    QGroupBox(parent), hitRect()
{
    closeEnabled = false;
    setTitle(" ");

    QGridLayout *gLayout = new QGridLayout(this);
    gLayout->setMargin(5);
    gLayout->setSpacing(5);

    QLabel *minLabel = new QLabel(tr("Minimum"), this);
    gLayout->addWidget(minLabel, 0, 0);
    selectedRange[0] = new QLineEdit(this);
    connect(selectedRange[0], SIGNAL(editingFinished()),
            this, SLOT(minChanged()));
    gLayout->addWidget(selectedRange[0], 0, 1);

    QLabel *maxLabel = new QLabel(tr("Maximum"), this);
    gLayout->addWidget(maxLabel, 0, 2);
    selectedRange[1] = new QLineEdit(this);
    connect(selectedRange[1], SIGNAL(editingFinished()),
            this, SLOT(maxChanged()));
    gLayout->addWidget(selectedRange[1], 0, 3);

    histogram = new QvisHistogram(this);
    histogram->setEnabled(false);
    connect(histogram, SIGNAL(selectedRangeChanged(float,float)),
            this, SLOT(rangeChanged(float,float)));
    gLayout->addWidget(histogram, 1, 0, 1, 4);

    totalRange[0] = new QLabel(this);
    gLayout->addWidget(totalRange[0], 2, 0);

    totalRange[1] = new QLabel(this);
    gLayout->addWidget(totalRange[1], 2, 3, Qt::AlignRight);

    gLayout->setRowStretch(1, 10);
}

// ****************************************************************************
// Method: QvisHistogramLimits::~QvisHistogramLimits
//
// Purpose: 
//   Destructor
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

QvisHistogramLimits::~QvisHistogramLimits()
{
}

// ****************************************************************************
// Method: QvisHistogramLimits::setHistogram
//
// Purpose: 
//   Set the histogram that we'll display.
//
// Arguments:
//   hist : The histogram texture data.
//   nhist : The number of elements in the histogram array.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

void
QvisHistogramLimits::setHistogram(const float *hist, int nhist)
{
    histogram->setEnabled(true);
    histogram->setHistogramTexture(hist, nhist);
}

// ****************************************************************************
// Method: QvisHistogramLimits::getTotalRange
//
// Purpose: 
//   Return the total range used in the histogram.
//
// Arguments:
//   valid : Whether the histogram range is valid.
//   r0    : The min value.
//   r1    : The max value.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

void
QvisHistogramLimits::getTotalRange(bool &valid, float &r0, float &r1) const
{
    histogram->getTotalRange(valid, r0, r1);
}

whitlocb's avatar
whitlocb committed
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
// ****************************************************************************
// Method: QvisHistogramLimits::GetRangeText
//
// Purpose: 
//   Get the range text.
//
// Arguments:
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Wed Oct 26 17:08:25 PDT 2011
//
// Modifications:
//   
// ****************************************************************************

void
QvisHistogramLimits::GetRangeText(float r0, float r1, QString &r0Text, QString &r1Text) const
{
    if(r0 <= (MIN_VALUE + EPSILON))
        r0Text = tr("min").toLower();
    else
        r0Text = QString().setNum(r0);
    if(r1 >= (MAX_VALUE - EPSILON))
        r1Text = tr("max").toLower();
    else 
        r1Text = QString().setNum(r1);
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222
// ****************************************************************************
// Method: QvisHistogramLimits::setTotalRange
//
// Purpose: 
//   Set the total range used in the histogram.
//
// Arguments:
//   r0    : The min value.
//   r1    : The max value.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
whitlocb's avatar
whitlocb committed
223 224 225
//   Brad Whitlock, Wed Oct 26 16:43:01 PDT 2011
//   Handle min and max.
//
226 227 228 229 230
// ****************************************************************************

void
QvisHistogramLimits::setTotalRange(float r0, float r1)
{
whitlocb's avatar
whitlocb committed
231 232 233 234
    QString r0Text, r1Text;
    GetRangeText(r0, r1, r0Text, r1Text);
    totalRange[0]->setText(r0Text);
    totalRange[1]->setText(r1Text);
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

    histogram->setTotalRange(r0, r1);
}

// ****************************************************************************
// Method: QvisHistogramLimits::invalidateTotalRange
//
// Purpose: 
//   Invalidate the total range used in the histogram.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

void
QvisHistogramLimits::invalidateTotalRange()
{
    histogram->invalidateTotalRange();
    totalRange[0]->setText("");
    totalRange[1]->setText("");
}

// ****************************************************************************
// Method: QvisHistogramLimits::getSelectedRange
//
// Purpose: 
//   Return the selected range used in the histogram.
//
// Arguments:
//   r0    : The min value.
//   r1    : The max value.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
whitlocb's avatar
whitlocb committed
274 275 276
//   Brad Whitlock, Wed Oct 26 15:31:32 PDT 2011
//   Force the min and max to get locked in before we ask for them.
//
277 278 279
// ****************************************************************************

void
whitlocb's avatar
whitlocb committed
280
QvisHistogramLimits::getSelectedRange(float &r0, float &r1) 
281
{
whitlocb's avatar
whitlocb committed
282 283 284 285 286 287
    // Make sure the values are up to date with the text fields.
    blockSignals(true);
    minChanged();
    maxChanged();
    blockSignals(false);

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    histogram->getSelectedRange(r0, r1);
}

// ****************************************************************************
// Method: QvisHistogramLimits::setSelectedRange
//
// Purpose: 
//   Set the selected range used in the histogram.
//
// Arguments:
//   r0    : The min value.
//   r1    : The max value.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
whitlocb's avatar
whitlocb committed
305 306 307
//   Brad Whitlock, Wed Oct 26 16:48:13 PDT 2011
//   Handle min/max.
//
308 309 310 311 312
// ****************************************************************************

void
QvisHistogramLimits::setSelectedRange(float r0, float r1)
{
whitlocb's avatar
whitlocb committed
313 314 315 316 317
    QString r0Text, r1Text;
    GetRangeText(r0, r1, r0Text, r1Text);

    selectedRange[0]->setText(r0Text);
    selectedRange[1]->setText(r1Text);
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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

    histogram->setSelectedRange(r0, r1);
}

// ****************************************************************************
// Method: QvisHistogramLimits::setVariable
//
// Purpose: 
//   Set the name of the variable used in the histogram.
//
// Arguments:
//   var : The variable name.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

void
QvisHistogramLimits::setVariable(const QString &var)
{
    setTitle(var);
}

// ****************************************************************************
// Method: QvisHistogramLimits::getVariable
//
// Purpose: 
//   Return the variable name.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

QString
QvisHistogramLimits::getVariable() const
{
    return title();
}

// ****************************************************************************
// Method: QvisHistogramLimits::setCloseEnabled
//
// Purpose: 
//   Set whether the close button is enabled.
//
// Arguments:
//   val : Whether the close button is enabled.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

void
QvisHistogramLimits::setCloseEnabled(bool val)
{
    closeEnabled = val;
    update();
}

// ****************************************************************************
// Method: QvisHistogramLimits::getCloseEnabled
//
// Purpose: 
//   Return whether the close button is enabled.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

bool
QvisHistogramLimits::getCloseEnabled() const
{
    return closeEnabled;
}

// ****************************************************************************
// Method: QvisHistogramLimits::paintEvent
//
// Purpose: 
//   Draw a "close" button over the frame so we can click on it to issue a 
//   closeClicked signal.
//
// Arguments:
//   pe : The paint event.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

void
QvisHistogramLimits::paintEvent(QPaintEvent *pe)
{
    QGroupBox::paintEvent(pe);

    // Draw a close button.
    if(closeEnabled)
    {
        QIcon del(style()->standardIcon(QStyle::SP_TitleBarCloseButton));
#if QT_VERSION < 0x040500
        QSize size(del.actualSize(QSize(20,20)));
#else
        QList<QSize> sizes(del.availableSizes());
        QSize size(sizes[0]);
#endif
        QPainter p(this);

        hitRect = QRect(width() - size.width() - 5, 0,
                        size.width(), size.height());

        p.fillRect(hitRect, palette().brush(QPalette::Background));
        del.paint(&p, hitRect, Qt::AlignCenter,
                  QIcon::Active, isEnabled() ? QIcon::On : QIcon::Off);
    }
}

// ****************************************************************************
// Method: QvisHistogramLimits::mousePressEvent
//
// Purpose: 
//   We override this method so we can see if we clicked on the close button
//   that this class provides.
//
// Arguments:
//   e : The mouse event.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
//   
// ****************************************************************************

void
QvisHistogramLimits::mousePressEvent(QMouseEvent *e)
{
    if(closeEnabled && hitRect.contains(e->pos()))
        emit closeClicked();
    else
        QGroupBox::mousePressEvent(e);
}

//
// Qt slots
//

// ****************************************************************************
// Method: QvisHistogramLimits::updateSelectedText
//
// Purpose: 
//   This Qt slot function updates the selected range line edits.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
whitlocb's avatar
whitlocb committed
488 489 490
//   Brad Whitlock, Wed Oct 26 16:56:47 PDT 2011
//   Use GetRangeText.
//
491 492 493 494 495 496 497 498
// ****************************************************************************

void
QvisHistogramLimits::updateSelectedText()
{
    float r0, r1;
    histogram->getSelectedRange(r0, r1);

whitlocb's avatar
whitlocb committed
499 500 501 502
    QString r0Text, r1Text;
    GetRangeText(r0, r1, r0Text, r1Text);
    selectedRange[0]->setText(r0Text);
    selectedRange[1]->setText(r1Text);
503 504 505 506 507 508 509 510 511 512 513 514 515
}

// ****************************************************************************
// Method: QvisHistogramLimits::minChanged
//
// Purpose: 
//   This Qt slot function gets called when the min gets changed via the 
//   min line edit.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
whitlocb's avatar
whitlocb committed
516 517 518
//   Brad Whitlock, Wed Oct 26 16:51:29 PDT 2011
//   Handle min/max.
//
519 520 521 522 523 524 525
// ****************************************************************************

void
QvisHistogramLimits::minChanged()
{
    // Convert the text to a float and store the value into the histogram.
    bool okay = false;
whitlocb's avatar
whitlocb committed
526 527 528 529 530 531 532 533 534 535 536 537
    QString minText, maxText;
    GetRangeText(MIN_VALUE, MAX_VALUE, minText, maxText);
    QString txt(selectedRange[0]->text().toLower());
    float value;
    if(txt == minText)
    {
        value = MIN_VALUE;
        okay = true;
    }
    else
        value = txt.toFloat(&okay);

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    if(okay)
    {
        float r0, r1;
        histogram->getSelectedRange(r0, r1);
        if(r0 != value)
        {
            if(histogram->setSelectedRange(value, r1))
                QTimer::singleShot(10, this, SLOT(updateSelectedText()));

            histogram->getSelectedRange(r0, r1);
            emit selectedRangeChanged(r0, r1);
            emit selectedRangeChanged(getVariable(), r0, r1);
        }
    }
}

// ****************************************************************************
// Method: QvisHistogramLimits::maxChanged
//
// Purpose: 
//   This Qt slot function gets called when the max gets changed via the 
//   max line edit.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
whitlocb's avatar
whitlocb committed
565 566 567
//   Brad Whitlock, Wed Oct 26 16:52:10 PDT 2011
//   Handle min/max.
//
568 569 570 571 572 573 574
// ****************************************************************************

void
QvisHistogramLimits::maxChanged()
{
    // Convert the text to a float and store the value into the histogram.
    bool okay = false;
whitlocb's avatar
whitlocb committed
575 576 577 578 579 580 581 582 583 584 585 586
    QString minText, maxText;
    GetRangeText(MIN_VALUE, MAX_VALUE, minText, maxText);
    QString txt(selectedRange[1]->text().toLower());
    float value;
    if(txt == maxText)
    {
        value = MAX_VALUE;
        okay = true;
    }
    else
        value = txt.toFloat(&okay);

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    if(okay)
    {
        float r0, r1;
        histogram->getSelectedRange(r0, r1);
        if(r1 != value)
        {
            if(histogram->setSelectedRange(r0, value))
                QTimer::singleShot(10, this, SLOT(updateSelectedText()));

            histogram->getSelectedRange(r0, r1);
            emit selectedRangeChanged(r0, r1);
            emit selectedRangeChanged(getVariable(), r0, r1);
        }
    }
}

// ****************************************************************************
// Method: QvisHistogramLimits::rangeChanged
//
// Purpose: 
//   This Qt slot gets called when we adjust the ranges using the histogram
//   control.
//
// Arguments:
//   r0 : The new minimum range.
//   r1 : The new maximum range.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 28 16:00:40 PST 2010
//
// Modifications:
whitlocb's avatar
whitlocb committed
618 619 620
//   Brad Whitlock, Wed Oct 26 16:52:55 PDT 2011
//   Handle min/max.
//
621 622 623 624 625
// ****************************************************************************

void
QvisHistogramLimits::rangeChanged(float r0, float r1)
{
whitlocb's avatar
whitlocb committed
626 627 628 629
    QString r0Text, r1Text;
    GetRangeText(r0, r1, r0Text, r1Text);
    selectedRange[0]->setText(r0Text);
    selectedRange[1]->setText(r1Text);
630 631 632 633

    emit selectedRangeChanged(r0, r1);
    emit selectedRangeChanged(getVariable(), r0, r1);
}