QvisExpressionsWindow.C 18.7 KB
Newer Older
hrchilds's avatar
hrchilds committed
1
#include <QvisExpressionsWindow.h>
hrchilds's avatar
hrchilds committed
2

hrchilds's avatar
hrchilds committed
3
4
#include <Expression.h>
#include <ExpressionList.h>
hrchilds's avatar
hrchilds committed
5
6
#include <ViewerProxy.h>

hrchilds's avatar
hrchilds committed
7
8
9
10
11
12
13
14
#include <qapplication.h>
#include <qcheckbox.h>
#include <qcombobox.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qlistbox.h>
hrchilds's avatar
hrchilds committed
15
16
#include <qmultilineedit.h>
#include <qpopupmenu.h>
hrchilds's avatar
hrchilds committed
17
#include <qpushbutton.h>
hrchilds's avatar
hrchilds committed
18
#include <qsplitter.h>
hrchilds's avatar
hrchilds committed
19
20
21
22

#define STDMIN(A,B) (((A)<(B)) ? (A) : (B))
#define STDMAX(A,B) (((A)<(B)) ? (B) : (A))

hrchilds's avatar
hrchilds committed
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
52
53
54
55
56
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
const char *known_functions[] = {
        "abs",
        "acos",
        "and",
        "area",
        "asin",
        "aspect",
        "aspect_gamma",
        "atan",
        "condition",
        "coord",
        "coords",
        "cos",
        "cross",
        "deg2rad",
        "degree",
        "det",
        "determinant",
        "diagonal",
        "dimension",
        "dot",
        "effective_tensor",
        "eigenvalue",
        "eigenvector",
        "eq",
        "equal",
        "equals",
        "ge",
        "gradient",
        "gt",
        "gte",
        "if",
        "inverse",
        "jacobian",
        "largest_angle",
        "le",
        "ln",
        "log",
        "log10",
        "lt",
        "lte",
        "magnitude",
        "matvf",
        "ne",
        "neighbor",
        "neq",
        "nmats",
        "node_degree",
        "not",
        "notequal",
        "notequals",
        "oddy",
        "or",
        "polar",
        "principal_deviatoric_tensor",
        "principal_tensor",
        "procid",
        "rad2deg",
        "rand",
        "random",
        "recenter",
        "relative_size",
        "revolved_surface_area",
        "revolved_volume",
        "scaled_jacobian",
        "shape",
        "shape_and_size",
        "shear",
        "sin",
        "skew",
        "smallest_angle",
        "specmf",
        "sq",
        "sqr",
        "sqrt",
        "stretch",
        "tan",
        "taper",
        "tensor_maximum_shear",
        "trace",
        "volume",
        "warpage",
        NULL
};

hrchilds's avatar
hrchilds committed
108
109
110
111
112
113
114
115
116
117
// ****************************************************************************
// Method: QvisExpressionsWindow::QvisExpressionsWindow
//
// Purpose: 
//   This is the constructor for the QvisExpressionsWindow class.
//
// Arguments:
//   exprList_  The ExpressionList subject to observe
//   
//
hrchilds's avatar
hrchilds committed
118
119
// Programmer: Jeremy Meredith
// Creation:   October 10, 2004
hrchilds's avatar
hrchilds committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// ****************************************************************************
QvisExpressionsWindow::QvisExpressionsWindow(
    ExpressionList *exprList_, const char *caption,
    const char *shortName, QvisNotepadArea *notepad) :
    QvisPostableWindowObserver(exprList_, caption, shortName, notepad,
                               QvisPostableWindowObserver::ApplyButton,
                               false)
{
    exprList = exprList_;
}

// ****************************************************************************
// Method: QvisExpressionsWindow::~QvisExpressionsWindow
//
// Purpose: 
//   Destructor for the QvisExpressionsWindow class.
//
hrchilds's avatar
hrchilds committed
138
139
// Programmer: Jeremy Meredith
// Creation:   October 10, 2004
hrchilds's avatar
hrchilds committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Modifications:
//   
// ****************************************************************************
QvisExpressionsWindow::~QvisExpressionsWindow()
{
    exprList = NULL;
}

// ****************************************************************************
// Method: QvisExpressionsWindow::CreateWindowContents
//
// Purpose: 
//   Creates the widgets for the window.
//
hrchilds's avatar
hrchilds committed
155
156
157
158
159
// Programmer: Jeremy Meredith
// Creation:   October 10, 2004
//
// Note: as of writing, unsure if I want to use the splitter or not.
//       We should pick one of the two methods eventually.
hrchilds's avatar
hrchilds committed
160
161
162
163
164
165
166
//
// Modifications:
//   
// ****************************************************************************
void
QvisExpressionsWindow::CreateWindowContents()
{
hrchilds's avatar
hrchilds committed
167
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
210
211
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
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
//#define USE_SPLITTER
#ifndef USE_SPLITTER
    QHBoxLayout *mainLayout = new QHBoxLayout(topLayout, -1, "mainLayout");
#else
    QSplitter *mainSplitter = new QSplitter(central);
    topLayout->addWidget(mainSplitter);
    topLayout->setStretchFactor(mainSplitter, 100);
#endif

#ifndef USE_SPLITTER
    QGridLayout *listLayout = new QGridLayout(mainLayout, 3,2, 5, "listLayout");
    mainLayout->setStretchFactor(listLayout, 30);
#define f1 central
#else
    QFrame *f1 = new QFrame(mainSplitter);
    QGridLayout *listLayout = new QGridLayout(f1, 3,2, 0, 5, "listLayout");
#endif

    exprListBox = new QListBox(f1);
    listLayout->addMultiCellWidget(exprListBox, 0,0, 0,1);

    newButton = new QPushButton("New", f1, "newButton");
    listLayout->addWidget(newButton, 1,0);

    delButton = new QPushButton("Del", f1, "delButton");
    listLayout->addWidget(delButton, 1,1);

    displayAllVars = new QCheckBox("Display all", f1, "displayAllVars");
    listLayout->addMultiCellWidget(displayAllVars, 2,2, 0,1);

#ifndef USE_SPLITTER
    mainLayout->addSpacing(10);
    QGridLayout *editLayout = new QGridLayout(mainLayout, 6,2, 5, "editLayout");
    mainLayout->setStretchFactor(editLayout, 70);
#define f2 central
#else
    QFrame *f2 = new QFrame(mainSplitter);
    QGridLayout *editLayout = new QGridLayout(f2, 7,2, 0, 5, "editLayout");
#endif
    editLayout->addColSpacing(1,20);
    int row = 0;

    nameEditLabel = new QLabel("Name", f2, "nameEditLabel");
    nameEdit = new QLineEdit(f2, "nameEdit");
    editLayout->addWidget(nameEditLabel, row, 0);
    editLayout->addWidget(nameEdit, row, 1);
    row++;

    typeLabel = new QLabel("Type", f2, "typeLabel");
    typeList = new QComboBox(f2, "typeList");
    int numtypes = Expression::GetNumTypes();
    numtypes = 5;  // HACK!!!  Variable types after 5 currently fail.  FIXME!!!
    for (int i=1; i < numtypes ; i++)
        typeList->insertItem(Expression::GetTypeString((Expression::ExprType)i));
    editLayout->addWidget(typeLabel, row, 0);
    editLayout->addWidget(typeList, row, 1);
    row++;

    hidden = new QCheckBox("Hidden", f2, "hidden");
    editLayout->addMultiCellWidget(hidden, row,row, 0,1);
    row++;

    definitionEditLabel = new QLabel("Definition", f2, "definitionEditLabel");
    editLayout->addWidget(definitionEditLabel, row, 0);
    row++;

    definitionEdit = new QMultiLineEdit(f2, "definitionEdit");
    definitionEdit->setWordWrap(QMultiLineEdit::WidgetWidth);
    definitionEdit->setWrapPolicy(QMultiLineEdit::AtWhiteSpace);
    editLayout->addMultiCellWidget(definitionEdit, row,row, 0,1);
    row++;

    editLayout->addRowSpacing(row, 20);
    row++;

    insertFunctionButton = new QPushButton("Insert Function", f2);
    insertFunctionMenu = new QPopupMenu(f2, "insertFunctionMenu");
    for (int i=0; known_functions[i]; i++)
        insertFunctionMenu->insertItem(known_functions[i]);
    insertFunctionButton->setPopup(insertFunctionMenu);
    editLayout->addWidget(insertFunctionButton, row, 0);

    // connect signals

    connect(exprListBox, SIGNAL(selectionChanged()),
            this, SLOT(UpdateWindowSingleItem()));
    connect(nameEdit, SIGNAL(textChanged(const QString&)),
            this, SLOT(nameTextChanged(const QString&)));
    connect(definitionEdit, SIGNAL(textChanged()),
            this, SLOT(definitionTextChanged()));
    connect(newButton, SIGNAL(pressed()),
            this, SLOT(addExpression()));
    connect(delButton, SIGNAL(pressed()),
            this, SLOT(delExpression()));
    connect(typeList, SIGNAL(activated(int)),
            this, SLOT(typeChanged(int)));
    connect(hidden, SIGNAL(clicked()),
            this, SLOT(hiddenChanged()));
    connect(displayAllVars, SIGNAL(clicked()),
            this, SLOT(displayAllVarsChanged()));
    connect(insertFunctionMenu, SIGNAL(activated(int)),
            this, SLOT(insertFunction(int)));
hrchilds's avatar
hrchilds committed
269
270
271
272
273
274
275
276
277
278
279
280
}

// ****************************************************************************
// Method: QvisExpressionsWindow::UpdateWindow
//
// Purpose: 
//   This method is called when the window's subject is changed. The
//   subject tells this window what attributes changed and we put the
//   new values into those widgets.
//
// Arguments:
//   doAll : If this flag is true, update all the widgets regardless
hrchilds's avatar
hrchilds committed
281
//           of whether or not they are selected.  Currently ignored.
hrchilds's avatar
hrchilds committed
282
//
hrchilds's avatar
hrchilds committed
283
284
285
286
// Programmer: Jeremy Meredith
// Creation:   October 10, 2004
//
// Modifications:
hrchilds's avatar
hrchilds committed
287
//
hrchilds's avatar
hrchilds committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// ****************************************************************************
void
QvisExpressionsWindow::UpdateWindow(bool)
{
    BlockAllSignals(true);

    exprListBox->clear();
    for (int i=0; i<exprList->GetNumExpressions(); i++)
    {
        if (displayAllVars->isChecked() ||
            !exprList->GetExpression(i).GetFromDB())
        {
            exprListBox->insertItem(exprList->GetExpression(i).GetName());
        }
    }
    BlockAllSignals(false);

    UpdateWindowSingleItem();
}

// ****************************************************************************
//  Method:  QvisExpressionsWindow::UpdateWindowSingleItem
hrchilds's avatar
hrchilds committed
310
//
hrchilds's avatar
hrchilds committed
311
312
//  Purpose:
//    Update the pane of the window where a single expression is being edited.
hrchilds's avatar
hrchilds committed
313
//
hrchilds's avatar
hrchilds committed
314
315
//  Programmer:  Jeremy Meredith
//  Creation:    October 10, 2004
hrchilds's avatar
hrchilds committed
316
317
318
//
// ****************************************************************************
void
hrchilds's avatar
hrchilds committed
319
QvisExpressionsWindow::UpdateWindowSingleItem()
hrchilds's avatar
hrchilds committed
320
{
hrchilds's avatar
hrchilds committed
321
322
323
324
325
326
327
328
329
330
    BlockAllSignals(true);
    int index = exprListBox->currentItem();

    if (index == -1)
    {
        nameEdit->setText("");
        hidden->setChecked(false);
        definitionEdit->setText("");
    }
    else
hrchilds's avatar
hrchilds committed
331
    {
hrchilds's avatar
hrchilds committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
        Expression *e = (*exprList)[exprListBox->currentText()];
        if (!e)
        {
            nameEdit->setText("");
            hidden->setChecked(false);
            definitionEdit->setText("");
        }
        else
        {
            nameEdit->setText(e->GetName());
            hidden->setChecked(e->GetHidden());
            typeList->setCurrentText(Expression::GetTypeString(e->GetType()));
            definitionEdit->setText(e->GetDefinition());
        }
hrchilds's avatar
hrchilds committed
346
    }
hrchilds's avatar
hrchilds committed
347
348
349
350
    
    UpdateWindowSensitivity();
    BlockAllSignals(false);
}
hrchilds's avatar
hrchilds committed
351

hrchilds's avatar
hrchilds committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// ****************************************************************************
//  Method:  QvisExpressionsWindow::UpdateWindowSensitivity
//
//  Purpose:
//    Update the sensitivity of any widget needing it.
//
//  Programmer:  Jeremy Meredith
//  Creation:    October 10, 2004
//
// ****************************************************************************
void
QvisExpressionsWindow::UpdateWindowSensitivity()
{
    bool enable = true;
    if (exprListBox->currentItem() == -1)
hrchilds's avatar
hrchilds committed
367
    {
hrchilds's avatar
hrchilds committed
368
369
370
371
372
373
        enable = false;
    }
    else if ((*exprList)[exprListBox->currentText().latin1()] &&
             (*exprList)[exprListBox->currentText().latin1()]->GetFromDB())
    {
        enable = false;
hrchilds's avatar
hrchilds committed
374
375
    }

hrchilds's avatar
hrchilds committed
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
    nameEdit->setEnabled(enable);
    delButton->setEnabled(enable);
    hidden->setEnabled(enable);
    typeList->setEnabled(enable);
    definitionEdit->setEnabled(enable);
    insertFunctionButton->setEnabled(enable);
}

// ****************************************************************************
//  Method:  QvisExpressionsWindow::BlockAllSignals
//
//  Purpose:
//    Block signals for all important widgets.  Or unblock, of course.
//
//  Arguments:
//    block      true to block, false to unblock
//
//  Programmer:  Jeremy Meredith
//  Creation:    October 10, 2004
//
// ****************************************************************************
void
QvisExpressionsWindow::BlockAllSignals(bool block)
{
    exprListBox->blockSignals(block);
    nameEdit->blockSignals(block);
    definitionEdit->blockSignals(block);
hrchilds's avatar
hrchilds committed
403
404
405
406
407
408
409
410
}

// ****************************************************************************
// Method: QvisExpressionsWindow::Apply
//
// Purpose: 
//   This is a Qt slot function that is called when the Apply button is clicked.
//
hrchilds's avatar
hrchilds committed
411
412
// Programmer: Jeremy Meredith
// Creation:   October 10, 2004
hrchilds's avatar
hrchilds committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//
// Modifications:
//   
// ****************************************************************************
void
QvisExpressionsWindow::Apply(bool forceUpdate)
{
    exprList->Notify();

    if (AutoUpdate() || forceUpdate)
        viewer->ProcessExpressions();
}

// ****************************************************************************
// Method: QvisExpressionsWindow::apply
//
// Purpose: 
//   This is a Qt slot function that is called when the window's Apply
//   button is clicked.
//
hrchilds's avatar
hrchilds committed
433
434
// Programmer: Jeremy Meredith
// Creation:   October 10, 2004
hrchilds's avatar
hrchilds committed
435
436
437
438
439
440
441
//
// Modifications:
//   
// ****************************************************************************
void
QvisExpressionsWindow::apply()
{
hrchilds's avatar
hrchilds committed
442
443
444
445
446
    // Apply will recreate the window and unselect the current expression.
    // If we have an expression selected, reselect it afterwards.
    int reselect = (exprListBox->currentItem() != -1);
    QString item = exprListBox->currentText();

hrchilds's avatar
hrchilds committed
447
    Apply(true);
hrchilds's avatar
hrchilds committed
448
449
450
451
452
453
454
455
456

    if (reselect)
    {
        for (int i=0; i<exprListBox->count(); i++)
        {
            if (exprListBox->text(i) == item)
                exprListBox->setSelected(i, true);
        }
    }
hrchilds's avatar
hrchilds committed
457
458
459
460
461
462
463
464
}

// ****************************************************************************
// Method: QvisExpressionsWindow::addExpression
//
// Purpose: 
//   This is a Qt slot function that adds a new expression that is empty.
//
hrchilds's avatar
hrchilds committed
465
466
// Programmer: Jeremy Meredith
// Creation:   October 10, 2004
hrchilds's avatar
hrchilds committed
467
468
469
470
471
472
473
//
// Modifications:
//   
// ****************************************************************************
void
QvisExpressionsWindow::addExpression()
{
hrchilds's avatar
hrchilds committed
474
475
476
477
478
479
480
481
482
483
484
485
    // Find an unused expression name
    int newid = 1;
    bool okay = false;
    QString newName;
    while (!okay)
    {
        newName.sprintf("unnamed%d", newid);
        if ((*exprList)[newName.latin1()])
            newid++;
        else
            okay = true;
    }
hrchilds's avatar
hrchilds committed
486

hrchilds's avatar
hrchilds committed
487
488
489
490
    Expression e;
    e.SetName(newName.latin1());
    e.SetDefinition("");
    exprList->AddExpression(e);
hrchilds's avatar
hrchilds committed
491
492

    exprList->Notify();
hrchilds's avatar
hrchilds committed
493
494
495
496
497
498

    for (int i=0; i<exprListBox->count(); i++)
    {
        if (exprListBox->text(i) == newName)
            exprListBox->setSelected(i, true);
    }
hrchilds's avatar
hrchilds committed
499
500
501
}

// ****************************************************************************
hrchilds's avatar
hrchilds committed
502
// Method: QvisExpressionsWindow::delExpression
hrchilds's avatar
hrchilds committed
503
504
505
506
//
// Purpose: 
//   This is a Qt slot function that is called to delete an expression.
//
hrchilds's avatar
hrchilds committed
507
508
// Programmer: Jeremy Meredith
// Creation:   October 10, 2004
hrchilds's avatar
hrchilds committed
509
510
511
512
513
//
// Modifications:
//   
// ****************************************************************************
void
hrchilds's avatar
hrchilds committed
514
QvisExpressionsWindow::delExpression()
hrchilds's avatar
hrchilds committed
515
{
hrchilds's avatar
hrchilds committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
    int index = exprListBox->currentItem();

    if (index == -1)
        return;

    for (int i=0; i<exprList->GetNumExpressions(); i++)
    {
        if (exprList->GetExpression(i).GetName() == 
            exprListBox->currentText().latin1())
        {
            exprList->RemoveExpression(i);
            break;
        }
    }
hrchilds's avatar
hrchilds committed
530
531
532

    exprList->Notify();
}
hrchilds's avatar
hrchilds committed
533
534
535
536
537
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691

// ****************************************************************************
//  Method:  QvisExpressionsWindow::definitionTextChanged
//
//  Purpose:
//    Slot function when definition text is changed.  This is called for
//    any change to the definition text.
//
//  Programmer:  Jeremy Meredith
//  Creation:    October 10, 2004
//
// ****************************************************************************
void
QvisExpressionsWindow::definitionTextChanged()
{
    int index = exprListBox->currentItem();
    if (index == -1)
        return;

    Expression *e = (*exprList)[exprListBox->currentText()];
    if (!e)
        return;

    e->SetDefinition(definitionEdit->text());
}

// ****************************************************************************
//  Method:  QvisExpressionsWindow::nameTextChanged
//
//  Purpose:
//    Slot function when any change happens to the expression names.
//
//  Arguments:
//    text       the new text.
//
//  Programmer:  Jeremy Meredith
//  Creation:    October 10, 2004
//
// ****************************************************************************
void
QvisExpressionsWindow::nameTextChanged(const QString &text)
{
    int index = exprListBox->currentItem();
    if (index == -1)
        return;

    Expression *e = (*exprList)[exprListBox->currentText()];
    if (!e)
        return;

    QString newname = text.stripWhiteSpace();
    e->SetName(newname);
    BlockAllSignals(true);
    exprListBox->changeItem(newname, index);
    BlockAllSignals(false);
}

// ****************************************************************************
//  Method:  QvisExpressionsWindow::typeChanged
//
//  Purpose:
//    Slot function for the "type" combo box.  Sets the expression type.
//
//  Arguments:
//    value      the new type
//
//  Programmer:  Jeremy Meredith
//  Creation:    October 10, 2004
//
// ****************************************************************************
void
QvisExpressionsWindow::typeChanged(int value)
{
    int index = exprListBox->currentItem();
    if (index == -1)
        return;

    Expression *e = (*exprList)[exprListBox->currentText()];
    if (!e)
        return;

    // Add one because we skipped index 0 (unknown) when creating the
    // list box originally
    e->SetType(Expression::ExprType(value+1));
}

// ****************************************************************************
//  Method:  QvisExpressionsWindow::hiddenChanged
//
//  Purpose:
//    Slot function for the hidden toggle.
//    Sets the "hidden" flag of an expression.
//
//  Programmer:  Jeremy Meredith
//  Creation:    October 10, 2004
//
// ****************************************************************************
void
QvisExpressionsWindow::hiddenChanged()
{
    int index = exprListBox->currentItem();
    if (index == -1)
        return;

    Expression *e = (*exprList)[exprListBox->currentText()];
    if (!e)
        return;

    e->SetHidden(hidden->isChecked());
}

// ****************************************************************************
//  Method:  QvisExpressionsWindow::displayAllVarsChanged
//
//  Purpose:
//    Slot function, called when "display all variables" is toggled.
//    Just update the window because it will be taken care of.
//
//  Programmer:  Jeremy Meredith
//  Creation:    October 10, 2004
//
// ****************************************************************************
void
QvisExpressionsWindow::displayAllVarsChanged()
{
    UpdateWindow(true);
}

// ****************************************************************************
//  Method:  QvisExpressionsWindow::insertFunction
//
//  Purpose:
//    Slot function when a function was selected for insertion by 
//    the popup menu.
//
//  Arguments:
//    id         the menu item id of the function
//
//  Programmer:  Jeremy Meredith
//  Creation:    October 10, 2004
//
// ****************************************************************************
void
QvisExpressionsWindow::insertFunction(int id)
{
    if (!definitionEdit->isEnabled())
        return;

    QString orig = definitionEdit->text();
    definitionEdit->insert(insertFunctionMenu->text(id));
    definitionEdit->insert("()");

    int col,line;
    definitionEdit->getCursorPosition(&line, &col);
    col--;
    if (col < 0)
        col = 0;
    definitionEdit->setCursorPosition(line, col);
}