QvisExpressionsWindow.C 8.99 KB
Newer Older
hrchilds's avatar
hrchilds committed
1
2
3
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
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
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
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
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
#include <QvisExpressionsWindow.h>
#include <Expression.h>
#include <ExpressionList.h>
#include <qapplication.h>
#include <qcheckbox.h>
#include <qcombobox.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qlistbox.h>
#include <qlistview.h>
#include <qpushbutton.h>
#include <ExprDeleteButton.h>
#include <ExprTypeItem.h>
#include <ExpressionTable.h>

#include <ViewerProxy.h>

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

// ****************************************************************************
// Method: QvisExpressionsWindow::QvisExpressionsWindow
//
// Purpose: 
//   This is the constructor for the QvisExpressionsWindow class.
//
// Arguments:
//   exprList_  The ExpressionList subject to observe
//   
//
// Programmer: Sean Ahern
// Creation:   Fri Sep 14 13:34:45 PDT 2001
//
// ****************************************************************************
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.
//
// Programmer: Sean Ahern
// Creation:   Fri Sep 14 13:34:32 PDT 2001
//
// Modifications:
//   
// ****************************************************************************
QvisExpressionsWindow::~QvisExpressionsWindow()
{
    exprList = NULL;
}

// ****************************************************************************
// Method: QvisExpressionsWindow::CreateWindowContents
//
// Purpose: 
//   Creates the widgets for the window.
//
// Programmer: Sean Ahern
// Creation:   Fri Sep 14 13:34:33 PDT 2001
//
// Modifications:
//   
// ****************************************************************************
void
QvisExpressionsWindow::CreateWindowContents()
{
    // Create the table that contains all of the expressions.
    table = new ExpressionTable(0, 5, central, "table");
    table->setShowGrid(true);
    table->setSelectionMode(QTable::NoSelection);
    QHeader *header = table->horizontalHeader();
    header->setLabel(0, tr("Name"));
    header->setLabel(1, tr("Definition"));
    header->setLabel(2, tr("Type"));
    header->setLabel(3, tr("Hidden"));
    header->setLabel(4, tr(""));
    topLayout->addWidget(table);

    // Create the list management buttons.
    QHBoxLayout *buttonArea = new QHBoxLayout(topLayout);

    newButton = new QPushButton("New", central, "newButton");
    buttonArea->addWidget(newButton);
    buttonArea->addStretch(1);
    connect(newButton, SIGNAL(clicked()), this, SLOT(addExpression()));
}

// ****************************************************************************
// 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
//           of whether or not they are selected.
//
// Returns:    
//
// Note:       
//
// Programmer: Sean Ahern
// Creation:   Fri Sep 14 13:34:34 PDT 2001
//
// Modifications:
//
// ****************************************************************************
void
QvisExpressionsWindow::UpdateWindow(bool doAll)
{
    // Update the table
    table->blockSignals(true);

    // Clear the old table
    for (int col = 0; col < 5; col++)
        for (int row = 0; row < table->numRows(); row++)
            table->clearCellWidget(row, col);

    // Check to make sure the table is big enough.
    int     nexp = exprList->GetNumExpressions();
    table->setNumRows(nexp);

    // Put in the expressions
    int     widths[] = {        // Minimum widths for the columns
        fontMetrics().width("Name"),
        fontMetrics().width("Definition"),
        fontMetrics().width("Type")
    };
    for (int row = 0; row < nexp; row++)
    {
        // The name of the expression
        const char *name = (*exprList)[row].GetName().c_str();
        table->setText(row, 0, QString(name));
        widths[0] = STDMAX(widths[0], fontMetrics().width(name));

        // Its definition
        const char *def = (*exprList)[row].GetDefinition().c_str();
        table->setText(row, 1, QString(def));
        widths[1] = STDMAX(widths[1], fontMetrics().width(def));

        // The type
        ExprTypeItem *ti = new ExprTypeItem(table, QTableItem::WhenCurrent,
                                            (*exprList)[row].GetType());
        table->setItem(row, 2, ti);
        widths[2] =
            STDMAX(widths[2],
                     fontMetrics().
                     width(Expression::
                           GetTypeString((*exprList)[row].GetType())));

        // The "hidden" flag
        QCheckBox *hid = new QCheckBox(NULL, table, "hidden");
        hid->setBackgroundColor(white); // XXX Hack.  I'm not positive
                                        // that the rest of the table cells
                                        // are drawn in white, though they
                                        // seem to be.  I can't query any
                                        // of the cells because none exist
                                        // at this point.
        hid->setChecked((*exprList)[row].GetHidden());
        table->setCellWidget(row, 3, hid);

        // The delete button
        ExprDeleteButton *d =
            new ExprDeleteButton(row, "Delete", table, "deleteButton");
        connect(d, SIGNAL(deleted(int)), this, SLOT(deleteExpression(int)));
        table->setCellWidget(row, 4, d);
    }

    // Fix up the column widths.
    int     pads[] = { 16, 16, 30 };
    if (nexp != 0)
    {
        for (int i = 0; i < 3; i++)
            table->setColumnWidth(i, widths[i] + pads[i]);
        table->adjustColumn(3);        // Hidden flag is automatic
    }

    table->blockSignals(false);
}

// ****************************************************************************
// Method: QvisExpressionsWindow::Apply
//
// Purpose: 
//   This is a Qt slot function that is called when the Apply button is clicked.
//
// Programmer: Sean Ahern
// Creation:   Fri Sep 14 13:34:37 PDT 2001
//
// Modifications:
//   
// ****************************************************************************
void
QvisExpressionsWindow::Apply(bool forceUpdate)
{
    // Tell all the cells to stop editing themselves
    table->StopEditing();

    // Read the values from the table
    int     nrows = table->numRows();
    for (int row = 0; row < nrows; row++)
    {
        QCheckBox *hidButton = (QCheckBox *) table->cellWidget(row, 3);

        (*exprList)[row].SetName(table->text(row, 0).ascii());
        (*exprList)[row].SetDefinition(table->text(row, 1).ascii());
        (*exprList)[row].SetType(Expression::GetTypeId(table->text(row,2).ascii()));
        (*exprList)[row].SetHidden(hidButton->isChecked());
    }

    exprList->Notify();

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

//
// Qt Slot functions...
//

// ****************************************************************************
// Method: QvisExpressionsWindow::apply
//
// Purpose: 
//   This is a Qt slot function that is called when the window's Apply
//   button is clicked.
//
// Programmer: Sean Ahern
// Creation:   Fri Sep 14 13:34:37 PDT 2001
//
// Modifications:
//   
// ****************************************************************************
void
QvisExpressionsWindow::apply()
{
    Apply(true);
}

// ****************************************************************************
// Method: QvisExpressionsWindow::addExpression
//
// Purpose: 
//   This is a Qt slot function that adds a new expression that is empty.
//
// Programmer: Sean Ahern
// Creation:   Fri Sep 14 13:34:42 PDT 2001
//
// Modifications:
//   
// ****************************************************************************
void
QvisExpressionsWindow::addExpression()
{
    // Update the current contents of the window.
    Apply();

    Expression *e = new Expression();
    e->SetName("?");
    e->SetDefinition("?");

    exprList->AddExpression(*e);
    delete(e);

    exprList->Notify();
}

// ****************************************************************************
// Method: QvisExpressionsWindow::deleteExpression
//
// Purpose: 
//   This is a Qt slot function that is called to delete an expression.
//
// Programmer: Sean Ahern
// Creation:   Fri Sep 14 13:34:42 PDT 2001
//
// Modifications:
//   
// ****************************************************************************
void
QvisExpressionsWindow::deleteExpression(int row)
{
    // Update the current contents of the window
    Apply();

    exprList->RemoveExpression(row);
    exprList->Notify();
}