QvisScreenPositionEdit.C 10.4 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
3
* Copyright (c) 2000 - 2013, 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 41 42 43
#include <stdio.h>
#include <QvisScreenPositionEdit.h>
#include <QvisScreenPositioner.h>
#include <QvisTurnDownButton.h>

whitlocb's avatar
whitlocb committed
44 45 46 47 48 49 50 51
#include <QApplication>
#include <QDesktopWidget>
#include <QLayout>
#include <QLineEdit>
#include <QPainter>
#include <QPushButton>
#include <QStyle>
#include <QTimer>
hrchilds's avatar
hrchilds committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

// ****************************************************************************
// Method: QvisScreenPositionEdit::QvisScreenPositionEdit
//
// Purpose: 
//   Constructor for the QvisScreenPositionEdit class.
//
// Arguments:
//   parent : The widget's parent.
//   name   : The widget's name
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 2 13:49:54 PST 2003
//
// Modifications:
whitlocb's avatar
whitlocb committed
67 68 69
//   Brad Whitlock, Tue Jun  3 16:12:06 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
70 71
// ****************************************************************************

whitlocb's avatar
whitlocb committed
72 73
QvisScreenPositionEdit::QvisScreenPositionEdit(QWidget *parent) : 
    QWidget(parent)
hrchilds's avatar
hrchilds committed
74 75
{
    QHBoxLayout *hLayout = new QHBoxLayout(this);
whitlocb's avatar
whitlocb committed
76 77 78
    hLayout->setMargin(0);
    hLayout->setSpacing(0);
    lineEdit = new QLineEdit(this);
hrchilds's avatar
hrchilds committed
79 80 81 82
    connect(lineEdit, SIGNAL(returnPressed()),
            this, SLOT(returnPressed()));
    hLayout->addWidget(lineEdit);

whitlocb's avatar
whitlocb committed
83
    turnDown = new QvisTurnDownButton(this);
hrchilds's avatar
hrchilds committed
84 85 86 87
    connect(turnDown, SIGNAL(pressed()),
            this, SLOT(popup()));
    hLayout->addWidget(turnDown);

whitlocb's avatar
whitlocb committed
88 89 90
    lineEdit->setMinimumHeight(turnDown->height());

    screenPositionPopup = new QvisScreenPositioner(this, Qt::Popup);
hrchilds's avatar
hrchilds committed
91 92
    screenPositionPopup->setFixedSize(QSize(150,150));
    connect(screenPositionPopup,
hrchilds's avatar
hrchilds committed
93 94
            SIGNAL(intermediateScreenPositionChanged(double,double)),
            this, SLOT(updateText(double, double)));
hrchilds's avatar
hrchilds committed
95
    connect(screenPositionPopup,
hrchilds's avatar
hrchilds committed
96 97
            SIGNAL(screenPositionChanged(double,double)),
            this, SLOT(newScreenPosition(double, double)));
hrchilds's avatar
hrchilds committed
98

whitlocb's avatar
whitlocb committed
99
    popupTimer = new QTimer(this);
hrchilds's avatar
hrchilds committed
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
    connect(popupTimer, SIGNAL(timeout()),
            this, SLOT(closePopup()));

    setPosition(0.5f, 0.5f);
}

// ****************************************************************************
// Method: QvisScreenPositionEdit::~QvisScreenPositionEdit
//
// Purpose: 
//   Destructor for the QvisScreenPositionEdit class.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 2 13:50:35 PST 2003
//
// Modifications:
//   
// ****************************************************************************

QvisScreenPositionEdit::~QvisScreenPositionEdit()
{
}

// ****************************************************************************
// Method: QvisScreenPositionEdit::setPosition
//
// Purpose: 
//   Sets the screen position.
//
// Arguments:
//   x,y : The new screen position.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 2 13:50:53 PST 2003
//
// Modifications:
//   
// ****************************************************************************

void
hrchilds's avatar
hrchilds committed
140
QvisScreenPositionEdit::setPosition(double x, double y)
hrchilds's avatar
hrchilds committed
141
{
hrchilds's avatar
hrchilds committed
142 143
    screenX = (x < 0.) ? 0. : ((x > 1.) ? 1. : x);
    screenY = (y < 0.) ? 0. : ((y > 1.) ? 1. : y);
hrchilds's avatar
hrchilds committed
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
    updateText(screenX, screenY);
    screenPositionPopup->setScreenPosition(screenX, screenY);
}

// ****************************************************************************
// Method: QvisScreenPositionEdit::getPosition
//
// Purpose: 
//   Gets the screen position.
//
// Arguments:
//   x,y : The return coordinates.
//
// Returns:    True if the coordinates are valid, false otherwise.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 2 13:51:39 PST 2003
//
// Modifications:
//   
// ****************************************************************************

bool
hrchilds's avatar
hrchilds committed
169
QvisScreenPositionEdit::getPosition(double &x, double &y)
hrchilds's avatar
hrchilds committed
170 171
{
    bool okay = false;
hrchilds's avatar
hrchilds committed
172
    double newX, newY;
hrchilds's avatar
hrchilds committed
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

    if((okay = getCurrentValues(&newX, &newY)) == true)
    {
        screenX = newX;
        screenY = newY;
    }

    x = screenX;
    y = screenY;
    return okay;
}

// ****************************************************************************
// Method: QvisScreenPositionEdit::getCurrentValues
//
// Purpose: 
//   Gets the current values from the line edit.
//
// Arguments:
//   newX, newY : The return coordinates.
//
// Returns:    True if the coordinates are good; false otherwise.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 2 13:52:38 PST 2003
//
// Modifications:
whitlocb's avatar
whitlocb committed
200 201 202
//   Brad Whitlock, Tue Jun  3 16:17:48 PDT 2008
//   Qt 4.
//
hrchilds's avatar
hrchilds committed
203 204 205
// ****************************************************************************

bool
hrchilds's avatar
hrchilds committed
206
QvisScreenPositionEdit::getCurrentValues(double *newX, double *newY)
hrchilds's avatar
hrchilds committed
207
{
whitlocb's avatar
whitlocb committed
208
    QString temp(lineEdit->displayText().simplified());
hrchilds's avatar
hrchilds committed
209 210
    bool okay = !temp.isEmpty();
    if(okay)
whitlocb's avatar
whitlocb committed
211
        okay = (sscanf(temp.toStdString().c_str(), "%lg %lg", newX, newY) == 2);
hrchilds's avatar
hrchilds committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

    return okay;
}

//
// Qt slots
//

// ****************************************************************************
// Method: QvisScreenPositionEdit::updateText
//
// Purpose: 
//   This is a Qt slot function that updates the text in the line edit.
//
// Arguments:
//   x,y : The new coordinate to display.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 2 13:53:39 PST 2003
//
// Modifications:
//   
// ****************************************************************************

void
hrchilds's avatar
hrchilds committed
237
QvisScreenPositionEdit::updateText(double x, double y)
hrchilds's avatar
hrchilds committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
{
    QString tmp;
    tmp.sprintf("%g %g", x, y);
    lineEdit->setText(tmp);
}

// ****************************************************************************
// Method: QvisScreenPositionEdit::newScreenPosition
//
// Purpose: 
//   This is a Qt slot function that emits a screenPositionChanged signal
//   when the screen coordinate changes for whatever reason.
//
// Arguments:
//   x,y : The new screen coordinate.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 2 13:54:19 PST 2003
//
// Modifications:
//   
// ****************************************************************************

void
hrchilds's avatar
hrchilds committed
262
QvisScreenPositionEdit::newScreenPosition(double x, double y)
hrchilds's avatar
hrchilds committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
{
    screenX = x;
    screenY = y;
    updateText(screenX, screenY);
    closePopup();

    emit screenPositionChanged(screenX, screenY);
}

// ****************************************************************************
// Method: QvisScreenPositionEdit::popup
//
// Purpose: 
//   This is a Qt slot function that is called to popup the screen positioner
//   widget.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 2 13:55:18 PST 2003
//
// Modifications:
//   
// ****************************************************************************

void
QvisScreenPositionEdit::popup()
{
    popupTimer->start(15000);

    // Figure out a good place to popup the menu.
    int menuW = screenPositionPopup->width();
    int menuH = screenPositionPopup->height();
    QPoint p(mapToGlobal(QPoint(0,0)));
    int menuX = p.x() + width() - menuW;
    int menuY = p.y() + height();

    // Fix the X dimension.
    if(menuX < 0)
        menuX = 0;
    else if(menuX + menuW > QApplication::desktop()->width())
    {
        int extrapixels = QApplication::desktop()->width() - menuX - menuW;
        menuX -= (extrapixels + 5);
    }

    // Fix the Y dimension.
    if(menuY - menuH < 0)
        menuY = menuY - height() - menuH;

    // Show the popup menu.         
    screenPositionPopup->move(menuX, menuY);
    screenPositionPopup->popupShow();
}

// ****************************************************************************
// Method: QvisScreenPositionEdit::closePopup
//
// Purpose: 
//   This is a Qt slot function that is called by a timer when the screen
//   positioner widget has been popped up for too long.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 2 13:55:47 PST 2003
//
// Modifications:
//   
// ****************************************************************************

void
QvisScreenPositionEdit::closePopup()
{
    turnDown->setDown(false);
    popupTimer->stop();
    screenPositionPopup->hide();
}

// ****************************************************************************
// Method: QvisScreenPositionEdit::returnPressed
//
// Purpose: 
//   This is a Qt slot function that is called when return is pressed in the
//   line edit.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 2 13:56:24 PST 2003
//
// Modifications:
//   
// ****************************************************************************

void
QvisScreenPositionEdit::returnPressed()
{
hrchilds's avatar
hrchilds committed
355
    double newX, newY;
hrchilds's avatar
hrchilds committed
356 357 358 359 360 361 362
    if(getCurrentValues(&newX, &newY))
    {
        newScreenPosition(newX, newY);
        screenPositionPopup->setScreenPosition(screenX, screenY);
    }
}