File: variablewidget.cpp

package info (click to toggle)
kdevelop 4%3A3.3.5-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 48,900 kB
  • ctags: 30,911
  • sloc: cpp: 289,305; sh: 18,675; makefile: 3,890; perl: 3,261; ruby: 2,081; ansic: 1,779; python: 1,636; xml: 577; yacc: 421; java: 359; lex: 252; php: 20; ada: 5; fortran: 4; pascal: 4; haskell: 2; sql: 1
file content (1267 lines) | stat: -rw-r--r-- 39,347 bytes parent folder | download
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
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
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
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
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
// **************************************************************************
//    begin                : Sun Aug 8 1999
//    copyright            : (C) 1999 by John Birch
//    email                : jbb@kdevelop.org
// **************************************************************************

// **************************************************************************
// *                                                                        *
// *   This program is free software; you can redistribute it and/or modify *
// *   it under the terms of the GNU General Public License as published by *
// *   the Free Software Foundation; either version 2 of the License, or    *
// *   (at your option) any later version.                                  *
// *                                                                        *
// **************************************************************************

#include "variablewidget.h"
#include "gdbparser.h"
#include "gdbcommand.h"

#include <kdebug.h>
#include <kpopupmenu.h>
#include <klineedit.h>
#include <kdeversion.h>
#include <kiconloader.h>

#include <qheader.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qhbox.h>
#include <qpainter.h>
#include <qpushbutton.h>
#include <qregexp.h>
#include <qcursor.h>
#include <qwhatsthis.h>
#include <klocale.h>

#include <qpoint.h>
#include <qclipboard.h>
#include <kapplication.h>


/** The variables widget is passive, and is invoked by the rest of the
    code via two main slots:
    - slotDbgStatus
    - slotCurrentFrame

    The first is received the program status changes and the second is
    recieved after current frame in the debugger can possibly changes.

    The widget has a list item for each frame/thread combination, with
    variables as children. However, at each moment only one item is shown.
    When handling the slotCurrentFrame, we check if variables for the
    current frame are available. If yes, we simply show the corresponding item.
    Otherwise, we fetch the new data from debugger.

    Fetching the data is done by emitting the produceVariablesInfo signal.
    In response, we get slotParametersReady and slotLocalsReady signal,
    in that order.

    The data is parsed and changed variables are highlighted. After that,
    we 'trim' variable items that were not reported by gdb -- that is, gone
    out of scope.
*/

// **************************************************************************
// **************************************************************************
// **************************************************************************

namespace GDBDebugger
{

VariableWidget::VariableWidget(QWidget *parent, const char *name)
    : QWidget(parent, name), firstShow_(true)
{
    setIcon(SmallIcon("math_brace"));
    setCaption(i18n("Variable Tree"));

    varTree_ = new VariableTree(this);
    
    watchVarEditor_ = new KHistoryCombo( this, 
                                         "var-to-watch editor");

    QHBoxLayout* buttons = new QHBoxLayout();

    buttons->addStretch();

    QPushButton *evalButton = new QPushButton(i18n("&Evaluate"), this );
    buttons->addWidget(evalButton);   

    QPushButton *addButton = new QPushButton(i18n("&Watch"), this );
    buttons->addWidget(addButton);

    QVBoxLayout *topLayout = new QVBoxLayout(this, 2);
    topLayout->addWidget(varTree_, 10);
    topLayout->addWidget(watchVarEditor_);
    topLayout->addItem(buttons);
    

    connect( addButton, SIGNAL(clicked()), SLOT(slotAddWatchVariable()) );
    connect( evalButton, SIGNAL(clicked()), SLOT(slotEvaluateExpression()) );

    connect( watchVarEditor_, SIGNAL(returnPressed()), 
             SLOT(slotEvaluateExpression()) );

    // Setup help items.

    QWhatsThis::add(this, i18n(
        "<b>Variable tree</b><p>"
        "The variable tree allows you to see the values of local "
        "variables and arbitrary expressions."              
        "<p>Local variables are displayed automatically and are updated "
        "as you step through your program. "
        "For each expression you enter, you can either evaluate it once, "
        "or \"watch\" it (make it auto-updated). Expressions that are not "
        "auto-updated can be updated manually from the context menu. "
        "Expressions can be renamed to more descriptive names by clicking "
        "on the name column."
        "<p>To change the value of a variable or an expression, "
        "click on the value."));

    QWhatsThis::add(watchVarEditor_, 
                    i18n("<b>Expression entry</b>"
                         "<p>Type in expression to evaluate."));

    QWhatsThis::add(evalButton, 
                    i18n("Evaluate the expression."));

    QWhatsThis::add(addButton, 
                    i18n("Evaluate the expression and "
                         "auto-update the value when stepping."));
}

// **************************************************************************

void VariableWidget::clear()
{
  // Use 'trim' so that logic about which top-level items are 
  // always retained is contained only in that function.
  varTree_->setActiveFlag();
  varTree_->trim();
}

// **************************************************************************

// When the variables view is shown the first time, 
// set the width of 'variable name' column to half the total
// width.
// Ideally, KMDI should emit 'initial size set' signal, but
// it does not, so we rely on the fact that size is already
// set when the widget is first shown.
void VariableWidget::showEvent(QShowEvent *)
{
    if (firstShow_)
    {
        firstShow_ = false;
        varTree_->setColumnWidth(0, width()/2);
    }
}

// **************************************************************************

void VariableWidget::slotAddWatchVariable()
{
//    QString watchVar(watchVarEntry_->text());
    QString watchVar(watchVarEditor_->currentText());
    if (!watchVar.isEmpty())
    {
        slotAddWatchVariable(watchVar);
    }
}

// **************************************************************************

void VariableWidget::slotAddWatchVariable(const QString &ident)
{
    if (!ident.isEmpty())
    {
        watchVarEditor_->addToHistory(ident);
        varTree_->slotAddWatchVariable(ident);
        watchVarEditor_->clearEdit();
    }
}

void VariableWidget::slotEvaluateExpression()
{
    QString exp(watchVarEditor_->currentText());
    if (!exp.isEmpty())
    {
        slotEvaluateExpression(exp);
    }
}

void VariableWidget::slotEvaluateExpression(const QString &ident)
{
    if (!ident.isEmpty())
    {
        watchVarEditor_->addToHistory(ident);
        varTree_->slotEvaluateExpression(ident);
        watchVarEditor_->clearEdit();
    }    
}

// **************************************************************************

void VariableWidget::focusInEvent(QFocusEvent */*e*/)
{
    varTree_->setFocus();
}




// **************************************************************************
// **************************************************************************
// **************************************************************************

VariableTree::VariableTree(VariableWidget *parent, const char *name)
    : KListView(parent, name),
      QToolTip( viewport() ),
      activeFlag_(0),
      currentThread_(-1),
      justPaused_(false),
      recentExpressions_(0)
{
    setRootIsDecorated(true);
    setAllColumnsShowFocus(true);
    setColumnWidthMode(0, Manual);
    setSorting(-1);
    QListView::setSelectionMode(QListView::Single);

    addColumn(i18n("Variable"), 100 );
    addColumn(i18n("Value"), 100 );
    addColumn(i18n("Type"), 100 );

    connect( this, SIGNAL(contextMenu(KListView*, QListViewItem*, const QPoint&)),
             SLOT(slotContextMenu(KListView*, QListViewItem*)) );
    connect( this, SIGNAL(toggleRadix(QListViewItem*)), SLOT(slotToggleRadix(QListViewItem*)) );
    connect( this, SIGNAL(itemRenamed( QListViewItem*, int, const QString&)),
             this, SLOT(slotItemRenamed( QListViewItem*, int, const QString&)));
}

// **************************************************************************

VariableTree::~VariableTree()
{
}

// **************************************************************************

void VariableTree::slotContextMenu(KListView *, QListViewItem *item)
{
    if (!item)
        return;

    setSelected(item, true);    // Need to select this item.

    if (item->parent())
    {
        KPopupMenu popup(this);
        popup.insertTitle(item->text(VarNameCol));
        int idRemember = -2;
        int idRemove = -2;
        int idReevaluate = -2;        
        int idWatch = -2;

        QListViewItem* root = findRoot(item);

        if (root != recentExpressions_)
        {
            idRemember = popup.insertItem(
                SmallIcon("pencil"), i18n("Remember Value"));
        }

        if (dynamic_cast<WatchRoot*>(root)) {
            idRemove = popup.insertItem( 
                SmallIcon("editdelete"), i18n("Remove Watch Variable") );
        } else if (root != recentExpressions_) {
            idWatch = popup.insertItem(
                i18n("Watch Variable"));
        }
        if (root == recentExpressions_) {
            idReevaluate = popup.insertItem( 
                SmallIcon("reload"), i18n("Reevaluate Expression") );
            idRemove = popup.insertItem( 
                SmallIcon("editdelete"), i18n("Remove Expression") );
        }

        int idToggleWatch = popup.insertItem( i18n("Toggle Watchpoint") );
        int idToggleRadix = popup.insertItem( i18n("Toggle Hex/Decimal") );
        int	idCopyToClipboard = popup.insertItem( 
            SmallIcon("editcopy"), i18n("Copy to Clipboard") );
        int res = popup.exec(QCursor::pos());

        if (res == idRemember)
        {
            if (VarItem *item = dynamic_cast<VarItem*>(currentItem()))
            {
                ((VariableWidget*)parent())->
                    slotEvaluateExpression(item->gdbExpression());
            }
        } 
        else if (res == idWatch)
        {
            if (VarItem *item = dynamic_cast<VarItem*>(currentItem()))
            {
                ((VariableWidget*)parent())->
                    slotAddWatchVariable(item->gdbExpression());
            }
        } 
        else if (res == idRemove)
            delete item;
        else if (res == idToggleRadix)
            emit toggleRadix(item);
        else if (res == idCopyToClipboard)
        {
            QClipboard *qb = KApplication::clipboard();
            QString text = "{ \"" + item->text( 0 ) + "\", " + // name
                            "\"" + item->text( 2 ) + "\", " + // type
                            "\"" + item->text( 1 ) + "\" }";  // value

#if KDE_VERSION > 305
            qb->setText( text, QClipboard::Clipboard );
#else
            qb->setText( text );
#endif
        }
        else if (res == idToggleWatch)
        {
            if (VarItem *item = dynamic_cast<VarItem*>(currentItem()))
                emit toggleWatchpoint(item->gdbExpression());
        }
        else if (res == idReevaluate)
        {
            if (VarItem* item = dynamic_cast<VarItem*>(currentItem()))
            {
                emit expandItem(item);
            }
        }
    }
    else if (item == recentExpressions_)
    {
        KPopupMenu popup(this);
        popup.insertTitle(i18n("Recent Expressions"));
        int idRemove = popup.insertItem(
            SmallIcon("editdelete"), i18n("Remove All"));
        int idReevaluate = popup.insertItem(
            SmallIcon("reload"), i18n("Reevaluate All"));
        int res = popup.exec(QCursor::pos());
        
        if (res == idRemove)
        {
            delete recentExpressions_;
            recentExpressions_ = 0;
        }
        else if (res == idReevaluate)
        {
            for(QListViewItemIterator it(item); *it; ++it)
            {
                VarItem* var = dynamic_cast<VarItem*>(*it);                    
                Q_ASSERT(var && 
                         "only VarItem allowed under 'Recent expressions'");
                emit expandItem(var);
            }
        }
    }
}

// **************************************************************************

void VariableTree::slotAddWatchVariable(const QString &watchVar)
{
    kdDebug(9012) << "Add watch variable: " << watchVar << endl;
    VarItem *varItem = new VarItem(findWatch(), watchVar, typeUnknown);
    emit expandItem(varItem);
}

void VariableTree::slotEvaluateExpression(const QString &expression)
{
    if (recentExpressions_ == 0)
    {
        recentExpressions_ = new TrimmableItem(this);
        recentExpressions_->setText(0, "Recent");
        recentExpressions_->setOpen(true);
    }

    VarItem *varItem = new VarItem(recentExpressions_, expression, typeUnknown);
    varItem->setRenameEnabled(0, 1);
    emit expandItem(varItem);
}

// **************************************************************************

QListViewItem *VariableTree::findRoot(QListViewItem *item) const
{
    while (item->parent())
        item = item->parent();

    return item;
}

// **************************************************************************

VarFrameRoot *VariableTree::findFrame(int frameNo, int threadNo) const
{
    QListViewItem *sibling = firstChild();

    // frames only exist on th top level so we only need to
    // check the siblings
    while (sibling) {
        VarFrameRoot *frame = dynamic_cast<VarFrameRoot*> (sibling);
        if (frame && frame->matchDetails(frameNo, threadNo))
            return frame;

        sibling = sibling->nextSibling();
    }

    return 0;
}

// **************************************************************************

WatchRoot *VariableTree::findWatch()
{
    QListViewItem *sibling = firstChild();

    while (sibling) {
        if (WatchRoot *watch = dynamic_cast<WatchRoot*> (sibling))
            return watch;

        sibling = sibling->nextSibling();
    }

    return new WatchRoot(this);
}

// **************************************************************************

void VariableTree::trim()
{
    QListViewItem *child = firstChild();

    while (child) {
        QListViewItem *nextChild = child->nextSibling();

        // don't trim the watch root, or 'recent expressions' root.
        if (!(dynamic_cast<WatchRoot*> (child)) 
            && child != recentExpressions_) {
            if (TrimmableItem *item = dynamic_cast<TrimmableItem*> (child)) {
                if (item->isActive())
                    item->trim();
                else
                    delete item;
            }
        }
        child = nextChild;
    }
}

// **************************************************************************

void VariableTree::trimExcessFrames()
{
    viewport()->setUpdatesEnabled(false);
    QListViewItem *child = firstChild();

    while (child) {
        QListViewItem *nextChild = child->nextSibling();
        if (VarFrameRoot *frame = dynamic_cast<VarFrameRoot*> (child)) {
            if (!frame->matchDetails(0, currentThread_))
                delete frame;
        }
        child = nextChild;
    }
    viewport()->setUpdatesEnabled(true);
    repaint();
}

// **************************************************************************

QListViewItem *VariableTree::lastChild() const
{
    QListViewItem *child = firstChild();
    if (child)
        while (QListViewItem *nextChild = child->nextSibling())
            child = nextChild;

    return child;
}

// **************************************************************************

void VariableTree::maybeTip(const QPoint &p)
{
    kdDebug(9012) << "ToolTip::maybeTip()" << endl;

    VarItem * item = dynamic_cast<VarItem*>( itemAt( p ) );
    if ( item )
    {
        QRect r = itemRect( item );
        if ( r.isValid() )
            tip( r, item->tipText() );
    }
}

/* rgruber:
 * this it the slot which is connected to the toggleRadix() signal
 * it removes the given watch variable an replaces it by another
 * watch that includes a format modifier
 */
void VariableTree::slotToggleRadix(QListViewItem * item)
{
  if (item==NULL)  //no item->nothing to do
    return;

  VarItem *pOldItem = dynamic_cast<VarItem*>(item);
  VarItem *pNewItem = NULL;

  QString strName = pOldItem->text(VarNameCol);

  QString strTmp = strName.left(3).lower();
  if (iOutRadix == 10) {
      if (strTmp == "/d ")   //is there a wrong format modifier...
          strName = "/x "+strName.right(strName.length()-3);  //...replace the modifier
      else if (strTmp == "/x ")
          strName = strName.right(strName.length()-3);  //stripe the modifier
      else
          strName = QString("/x ")+strName;  //add the hex-formater
  } else
  if (iOutRadix == 16) {
      if (strTmp == "/x ")   //is there a wrong format modifier...
          strName = "/d "+strName.right(strName.length()-3);  //...replace the modifier
      else if (strTmp == "/d ")   //is there a format modifier?
          strName = strName.right(strName.length()-3);  //stripe the modifier
      else
          strName = QString("/d ")+strName;  //add the dec-formater
  }

  pNewItem = new VarItem((TrimmableItem *) item->parent(), strName, typeUnknown);
  emit expandItem(pNewItem);

  pNewItem->moveItem(pOldItem);  //move the new item up right under the old one

  delete item;  //remove the old one so that is seam as if it was replaced by the new item
  pOldItem=NULL;
}

void VariableTree::slotDbgStatus(const QString&, int statusFlag)
{
    if (statusFlag & s_appNotStarted)
    {
        // The application no longer exists. Remove all locals.
        setActiveFlag();

        // Now wipe the tree out
        viewport()->setUpdatesEnabled(false);
        trim();
        setUpdatesEnabled(true);
        repaint();
    }
    else
    {
        // Application still exists.
        if (!(statusFlag & s_appBusy))
        {
            // But is not busy. This means application has just stopped for
            // some reason. Need to refresh locals when
            // slotChangedFrame is called. Cannot do it here, because
            // we don't know which thread we're in.
            justPaused_ = true;
        }
    }
}

VarFrameRoot* VariableTree::demand_frame_root(int frameNo, int threadNo)
{
    VarFrameRoot *frame = findFrame(frameNo, threadNo); 
    if (!frame)
    {
        frame = new VarFrameRoot(this, frameNo, threadNo);
        frame->setFrameName("Locals");
        // Make sure "Locals" item is always the top item, before
        // "watch" and "recent experessions" items.
       this->takeItem(frame);
       this->insertItem(frame);
    }
    return frame;
}

void VariableTree::slotParametersReady(const char* data)
{
    // The locals are always attached to the currentFrame
    VarFrameRoot *frame = demand_frame_root(currentFrame_, currentThread_);
    frame->setParams(data);
}

void VariableTree::slotLocalsReady(const char* data)
{
    setUpdatesEnabled(false);

    VarFrameRoot *frame = demand_frame_root(currentFrame_, currentThread_);
    frame->setLocals(data);
    frame->setOpen(true);
    
    // If we're regetting locals for the frame 0, it surely means
    // the application was just paused. Otherwise, 
    // (say after selecting frame 1 and then frame 0) we'd have locals
    // for frame 0 already. If app was just paused, then other frames
    // are out-of-date, and we trim them. If user later selects frame 1,
    // we get locals for that frame.
    // TODO: should we reset data for other threads?
    if (currentFrame_ == 0 || currentThread_ == -1)
        trim();
    else 
       frame->trim();

    setUpdatesEnabled(true);
    triggerUpdate();
}

void VariableTree::slotCurrentFrame(int frameNo, int threadNo)
{
    // It's quite likely that frameNo == currentFrame_ and
    // threadNo == currentThread_. For example, this happens
    // when the 'step' command is executed.
    if (frameNo != currentFrame_ || threadNo != currentThread_)
    {
        // Hide the current frame vars root.
        demand_frame_root(currentFrame_, currentThread_)->setVisible(false);
        
        currentFrame_ = frameNo;
        currentThread_ = threadNo;
    }

    // Show the current frame.
    VarFrameRoot* frame = demand_frame_root(currentFrame_, currentThread_);
    frame->setVisible(true);
        
    // If no locals for frame were obtained, reget the local.
    // Also reget the locals if the program was just paused. In that
    // case we're always on frame 0, and the setLocals function
    // we eventually remove frames 1, N, if they are present. They
    // will be repopulated if needed.
    if (frame->needLocals() || justPaused_) 
    {
        setActiveFlag();
        // This will eventually call back to slotParametersReady and 
        // slotLocalsReady
        emit produceVariablesInfo();

        if (justPaused_)
        {
            findWatch()->requestWatchVars();
        }
        justPaused_ = false;
    }
}

void 
VariableTree::slotItemRenamed(QListViewItem* item, int col, const QString& text)
{
    if (col == ValueCol)
    {
        VarItem* v = dynamic_cast<VarItem*>(item);
        Q_ASSERT(v);
        if (v)
        {
            // Set the value
            emit setValue(v->gdbExpression(), text);
            // And immediately reload it from gdb, 
            // so that it's display format is the one gdb uses,
            // not the one user has typed. Otherwise, on the next
            // step, the visible value might change and be highlighted
            // as changed, which is bogus.
            emit expandItem(v);
        }
    }
}

// **************************************************************************
// **************************************************************************
// **************************************************************************

TrimmableItem::TrimmableItem(VariableTree *parent)
    : KListViewItem (parent, parent->lastChild()),
      activeFlag_(0)
{
    setActive();
}

// **************************************************************************

TrimmableItem::TrimmableItem(TrimmableItem *parent)
    : KListViewItem (parent, parent->lastChild()),
      activeFlag_(0),
      waitingForData_(false)
{
    setActive();
}

// **************************************************************************

TrimmableItem::~TrimmableItem()
{
}

// **************************************************************************

void TrimmableItem::paintCell(QPainter *p, const QColorGroup &cg,
                              int column, int width, int align)
{
    if ( !p )
        return;
    // make toplevel item (watch and frame items) names bold
    if (column == 0 && !parent())
    {
        QFont f = p->font();
        f.setBold(true);
        p->setFont(f);
    }
    QListViewItem::paintCell( p, cg, column, width, align );
}

// **************************************************************************

int TrimmableItem::rootActiveFlag() const
{
    return ((VariableTree*)listView())->activeFlag();
}

// **************************************************************************

bool TrimmableItem::isTrimmable() const
{
    return !waitingForData_;
}

// **************************************************************************

QListViewItem *TrimmableItem::lastChild() const
{
    QListViewItem *child = firstChild();
    if (child)
        while (QListViewItem *nextChild = child->nextSibling())
            child = nextChild;

    return child;
}

// **************************************************************************

TrimmableItem *TrimmableItem::findMatch(const QString &match, DataType type) const
{
    QListViewItem *child = firstChild();
    bool bRenew=false;  //this indicates if the current item needs to be replaced by a new one.
   			//the problem is, that the debugger always replaces already
			//format-modified local item with non-mofified ones. So with every
			//run we need to newly modify the outcome of the debugger

    int iOutRad = ((VariableTree*)listView())->iOutRadix; //local copy of the output radix

    // Check the siblings on this branch
    while (child) {
        QString strMatch = child->text(VarNameCol);
        bRenew=false;
	if (strMatch.left(3) == "/x " || strMatch.left(3) == "/d ") {  //is the current item format modified?
	    strMatch = strMatch.right(strMatch.length()-3);
	    bRenew=true;
	}
	if (strMatch == match) {
            if (TrimmableItem *item = dynamic_cast<TrimmableItem*> (child))
                if ( item->getDataType() == type ||
		     ( iOutRad==16 && item->getDataType() == typeValue ) ||
		     ( iOutRad==10 && item->getDataType() == typePointer ) ) {
		    if (bRenew && dynamic_cast<VarItem*>(item)) { //do we need to replace?
			VarItem* pNewItem = new VarItem((TrimmableItem *) item->parent(),
				 child->text(VarNameCol), typeUnknown);
			emit ((VariableTree*)pNewItem->listView())->expandItem(pNewItem);
			pNewItem->moveItem(item);
			delete item;
			item=NULL;
			item=pNewItem;
		    }
		    return item;
		}
        }

        child = child->nextSibling();
    }

    return 0;
}

// **************************************************************************

void TrimmableItem::trim()
{
    QListViewItem *child = firstChild();

    while (child) {
        QListViewItem *nextChild = child->nextSibling();
        if (TrimmableItem *item = dynamic_cast<TrimmableItem*>(child)) {
            // Never trim a branch if we are waiting on data to arrive.
            if (isTrimmable()) {
                if (item->isActive())
                    item->trim();      // recurse
                else
                    delete item;
            }
        }
        child = nextChild;
    }
}

// **************************************************************************

DataType TrimmableItem::getDataType() const
{
    return typeUnknown;
}

// **************************************************************************

void TrimmableItem::setCache(const QCString&)
{
    Q_ASSERT(false);
}

// **************************************************************************

QCString TrimmableItem::getCache()
{
    Q_ASSERT(false);
    return QCString();
}

// **************************************************************************

void TrimmableItem::updateValue(char* /* buf */)
{
    waitingForData_ = false;
}

// **************************************************************************

QString TrimmableItem::key (int, bool) const
{
    return QString::null;
}

// **************************************************************************
// **************************************************************************
// **************************************************************************

VarItem::VarItem(TrimmableItem *parent, const QString &varName, DataType dataType)
    : TrimmableItem (parent),
      name_(varName),
      cache_(QCString()),
      dataType_(dataType),
      highlight_(false)
{
    setText(VarNameCol, varName);
    // Allow to change variable name by editing.
    setRenameEnabled(ValueCol, true);

    kdDebug(9012) << " ### VarItem::VarItem *CONSTR*" << endl;
    emit ((VariableTree*)listView())->varItemConstructed(this);
}

// **************************************************************************

VarItem::~VarItem()
{
}

// **************************************************************************

QString VarItem::gdbExpression() const
{
    QString vPath("");
    for(const VarItem* item = this; 
        item; 
        item = dynamic_cast<const VarItem*>(item->parent()))
    {
        // Children of array item have array names in them,
        // e.g. "p[0]", so when visiting parent we don't need to
        // add parent name. However, when 'gdbExpression' is called
        // on array itself, we do need the name.
        if (item->getDataType() == typeArray && item != this)
            continue;

        // VP, 2005/07/19: I don't know the reason for this
        // check. But retaining to avoid breaking anything.
        if ((item->text(VarNameCol))[0] != '<') {
            QString itemName = item->name_;
            if (vPath.isEmpty())
                vPath = itemName.replace(QRegExp("^static "), "");
            else
                vPath = itemName.replace(QRegExp("^static "), "") 
                    + "." + vPath;
        }
    }

    if (isOpen() && dataType_ == typePointer)
        // We're currently showing pointed-to value        
        return "*" + vPath;
    else
        return vPath;
}

// **************************************************************************

void VarItem::setText(int column, const QString &data)
{
    QString strData=data;

    if (!isActive() && isOpen() && dataType_ == typePointer) {
        waitingForData();
        ((VariableTree*)listView())->expandItem(this);
    }

    setActive();
    if (column == ValueCol) {
        QString oldValue(text(column));
        if (!oldValue.isEmpty())                   // Don't highlight new items
            highlight_ = (oldValue != QString(data));
    }

    QListViewItem::setText(column, strData);
    repaint();
}

// **************************************************************************

void VarItem::updateValue(char *buf)
{
    TrimmableItem::updateValue(buf);

    // Hack due to my bad QString implementation - this just tidies up the display
    if ((strncmp(buf, "There is no member named len.", 29) == 0) ||
        (strncmp(buf, "There is no member or method named len.", 39) == 0))
        return;

    if (strncmp(buf, "Cannot access memory at address", 31) == 0 &&
        dataType_ == typePointer &&  //only if it is a pointer...
        ((VariableTree*)listView())->iOutRadix == 16) { //...and only do if outputradix is set to hex
	dataType_ = typeValue;
	((VariableTree*)listView())->expandItem(this);
	return;
    }

    if (*buf == '$') {
        if (char *end = strchr(buf, '='))
            buf = end+2;
    }

    if (dataType_ == typeUnknown) {
        dataType_ = GDBParser::getGDBParser()->determineType(buf);

        // Try fixing a format string here by overriding the dataType calculated
        // from this data
        QString varName = getName();
        if (dataType_ == typePointer && varName[0] == '/')
            dataType_ = typeValue;
    }

    // A hack to nicely display QStrings. The content of QString is unicode
    // for for ASCII only strings we get ascii character mixed with \000.
    // Remove those \000 now.
    
    // This is not very nice, becuse we're doing this unconditionally
    // and this method can be called twice: first with data that gdb sends
    // for a variable, and second after we request the string data. In theory
    // the data sent by gdb might contain \000 that should not be translated.
    //
    // What's even worse, ideally we should convert the string data from
    // gdb into a QString again, handling all other escapes and composing
    // one QChar from two characters from gdb. But to do that, we *should*
    // now if the data if generic gdb value, and result of request for string
    // data. Fixing is is for later.
    QCString r(buf);
    r.replace( QRegExp("\\\\000|\\\\0"), "" );

    GDBParser::getGDBParser()->parseValue(this, r.data());
    setActive();
}

// **************************************************************************

void VarItem::updateType(char *buf)
{
    kdDebug(9012) << " ### VarItem::updateType " << buf << endl;

    QString str(buf);
    int eq = str.find('=');
    if (eq < 0)
        return;
    str.replace(QRegExp("[\n\r]"),"");
    str = str.mid(eq + 1, 0xffff).stripWhiteSpace();

    originalValueType_ = str.latin1();

    setText(VarTypeCol, str);
    handleSpecialTypes();    
}

// **************************************************************************

void VarItem::setCache(const QCString &value)
{
    cache_ = value;
    setExpandable(true);
    handleSpecialTypes();
    if (isOpen())
        setOpen(true);
    repaint();
    setActive();
}

// **************************************************************************

void VarItem::setOpen(bool open)
{
    QListViewItem::setOpen(open);

    if (open) {
        // Opening an item can be potentially expensive, so cache value
        // received from gdb. If we have value already, just parse it,
        // don't issue another request.
        if (cache_) {
            QCString value = cache_;
            cache_ = QCString();
            GDBParser::getGDBParser()->parseCompositeValue(this, value.data());
            handleSpecialTypes();
            trim();
        } else {
            if (dataType_ == typePointer || dataType_ == typeReference) {
                waitingForData();
                emit ((VariableTree*)listView())->expandItem(this);
            }
        }
    } else {
        // Closing item. For pointer/references, it means we switch from
        // display the pointer-to value to displaying the pointer itself.
        if (dataType_ == typePointer || dataType_ == typeReference) {
            waitingForData();
            emit ((VariableTree*)listView())->expandItem(this);
        }
    }
}

// **************************************************************************

QCString VarItem::getCache()
{
    return cache_;
}

// **************************************************************************

/* This function is called in two cases -- when the type of variable first
   becomes known.in two cases. First is when the type of variable
   first becomes known. Second is when value has changed.

   The method looks at the type, and if necessary, issues an additional gdb
   requests. 
*/
void VarItem::handleSpecialTypes()
{
    if (originalValueType_.isEmpty())
        return;

    QString type = originalValueType_;
    if (dataType_ == typeReference)
    {
        // Disable the special processing for non-opened
        // references. Not sure it's the best thing, but
        // previous code worked like this.
        if (!isOpen())
            return;

        static QRegExp strip_reference("(.*)[ ]*&");
        if (strip_reference.exactMatch(type))
        {
            type = strip_reference.cap(1);
        }
    }
    if (dataType_ == typePointer)
    {
        if (!isOpen())
            return;

        static QRegExp strip_pointer("(.*)[ ]*\\*");
        if (strip_pointer.exactMatch(type))
        {
            type = strip_pointer.cap(1);
        }
    }

    static QRegExp qstring("^(const)?[ ]*QString[ ]*$");

    if (qstring.exactMatch(type)) {
        waitingForData();
        emit ((VariableTree*)listView())->expandUserItem(
            this,
            QCString().sprintf("(($len=($data=%s.d).len)?*((char*)&$data.unicode[0])@($len>100?200:$len*2):\"\")",
                               gdbExpression().latin1()));
    }
}

// **************************************************************************

DataType VarItem::getDataType() const
{
    return dataType_;
}

// **************************************************************************

// Overridden to highlight the changed items
void VarItem::paintCell(QPainter *p, const QColorGroup &cg,
                        int column, int width, int align)
{
    if ( !p )
        return;

    if (column == ValueCol && highlight_) {
        QColorGroup hl_cg( cg.foreground(), cg.background(), cg.light(),
                           cg.dark(), cg.mid(), red, cg.base());
        QListViewItem::paintCell( p, hl_cg, column, width, align );
    } else
        QListViewItem::paintCell( p, cg, column, width, align );
}

// **************************************************************************

QString VarItem::tipText() const
{
    const unsigned int maxTooltipSize = 70;
    QString tip = text( ValueCol );

    if (tip.length() < maxTooltipSize )
	    return tip;
    else
	    return tip.mid( 0, maxTooltipSize - 1 ) + " [...]";
}

// **************************************************************************
// **************************************************************************
// **************************************************************************

VarFrameRoot::VarFrameRoot(VariableTree *parent, int frameNo, int threadNo)
    : TrimmableItem (parent),
      needLocals_(true),
      frameNo_(frameNo),
      threadNo_(threadNo),
      params_(QCString()),
      locals_(QCString())
{
    setExpandable(true);
}

// **************************************************************************

VarFrameRoot::~VarFrameRoot()
{
}

// **************************************************************************

void VarFrameRoot::setParams(const char *params)
{
    setActive();
    if (strncmp(params, "No arguments", strlen("No arguments")) == 0)
        params_ = "";
    else
        params_ = params;
}

// **************************************************************************

void VarFrameRoot::setLocals(const char *locals)
{
    setActive();

    // "No symbol table info available" or "No locals."
    bool noLocals = (locals &&  (strncmp(locals, "No ", 3) == 0));
    setExpandable(!params_.isEmpty() || !noLocals);

    if (noLocals) {
        locals_ = "";
        if (locals)
            if (char *end = strchr(locals, '\n'))
                *end = 0;      // clobber the new line
    } else
        locals_ = locals;

    if (!isExpandable() && noLocals)
        setText( ValueCol, locals );

    needLocals_ = false;
    if (isOpen())
        setOpen(true);
}

// **************************************************************************

// Override setOpen so that we can decide what to do when we do change
// state. This
void VarFrameRoot::setOpen(bool open)
{
    QListViewItem::setOpen(open);

    if (!open)
        return;

    if (!params_.isNull())
        GDBParser::getGDBParser()->parseCompositeValue(this, params_.data());
    if (!locals_.isNull())
        GDBParser::getGDBParser()->parseCompositeValue(this, locals_.data());

    locals_ = QCString();
    params_ = QCString();
}

// **************************************************************************

bool VarFrameRoot::matchDetails(int frameNo, int threadNo)
{
    return frameNo == frameNo_ && threadNo == threadNo_;
}

// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************

WatchRoot::WatchRoot(VariableTree *parent)
    : TrimmableItem(parent)
{
    setText(0, i18n("Watch"));
    setOpen(true);
}

// **************************************************************************

WatchRoot::~WatchRoot()
{
}

// **************************************************************************

void WatchRoot::requestWatchVars()
{
    for (QListViewItem *child = firstChild(); child; child = child->nextSibling())
        if (VarItem *varItem = dynamic_cast<VarItem*>(child))
            emit ((VariableTree*)listView())->expandItem(varItem);
}

// **************************************************************************
// **************************************************************************
// **************************************************************************

}


#include "variablewidget.moc"