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
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "watchwindow.h"
#include "breakhandler.h"
#include "debuggeractions.h"
#include "debuggerconstants.h"
#include "debuggercore.h"
#include "debuggerdialogs.h"
#include "debuggerengine.h"
#include "debuggerstartparameters.h"
#include "watchdelegatewidgets.h"
#include "watchhandler.h"
#include "debuggertooltipmanager.h"
#include "memoryviewwidget.h"
#include <utils/qtcassert.h>
#include <utils/savedaction.h>
#include <QtCore/QDebug>
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
#include <QtCore/QVariant>
#include <QtGui/QApplication>
#include <QtGui/QPalette>
#include <QtGui/QClipboard>
#include <QtGui/QContextMenuEvent>
#include <QtGui/QHeaderView>
#include <QtGui/QItemDelegate>
#include <QtGui/QMenu>
#include <QtGui/QPainter>
#include <QtGui/QResizeEvent>
#include <QtGui/QInputDialog>
/////////////////////////////////////////////////////////////////////
//
// WatchDelegate
//
/////////////////////////////////////////////////////////////////////
namespace Debugger {
namespace Internal {
static DebuggerEngine *currentEngine()
{
return debuggerCore()->currentEngine();
}
class WatchDelegate : public QItemDelegate
{
public:
explicit WatchDelegate(WatchWindow *parent)
: QItemDelegate(parent), m_watchWindow(parent)
{}
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
const QModelIndex &index) const
{
// Value column: Custom editor. Apply integer-specific settings.
if (index.column() == 1) {
const QVariant::Type type =
static_cast<QVariant::Type>(index.data(LocalsEditTypeRole).toInt());
switch (type) {
case QVariant::Bool:
return new BooleanComboBox(parent);
default:
break;
}
WatchLineEdit *edit = WatchLineEdit::create(type, parent);
edit->setFrame(false);
IntegerWatchLineEdit *intEdit
= qobject_cast<IntegerWatchLineEdit *>(edit);
if (intEdit)
intEdit->setBase(index.data(LocalsIntegerBaseRole).toInt());
return edit;
}
// Standard line edits for the rest.
QLineEdit *lineEdit = new QLineEdit(parent);
lineEdit->setFrame(false);
return lineEdit;
}
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
// Standard handling for anything but the watcher name column (change
// expression), which removes/recreates a row, which cannot be done
// in model->setData().
if (index.column() != 0) {
QItemDelegate::setModelData(editor, model, index);
return;
}
const QMetaProperty userProperty = editor->metaObject()->userProperty();
QTC_ASSERT(userProperty.isValid(), return);
const QString value = editor->property(userProperty.name()).toString();
const QString exp = index.data(LocalsExpressionRole).toString();
if (exp == value)
return;
m_watchWindow->removeWatchExpression(exp);
m_watchWindow->watchExpression(value);
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &) const
{
editor->setGeometry(option.rect);
}
private:
WatchWindow *m_watchWindow;
};
// Watch model query helpers.
static inline quint64 addressOf(const QModelIndex &m)
{ return m.data(LocalsAddressRole).toULongLong(); }
static inline quint64 pointerValueOf(const QModelIndex &m)
{ return m.data(LocalsPointerValueRole).toULongLong(); }
static inline QString nameOf(const QModelIndex &m)
{ return m.data().toString(); }
static inline uint sizeOf(const QModelIndex &m)
{ return m.data(LocalsSizeRole).toUInt(); }
// Helper functionality to indicate the area of a member variable in
// a vector representing the memory area by a unique color
// number and tooltip. Parts of it will be overwritten when recursing
// over the children.
typedef QPair<int, QString> ColorNumberToolTipPair;
typedef QVector<ColorNumberToolTipPair> ColorNumberToolTipVector;
static int memberVariableRecursion(const QModelIndex &m,
const QString &name,
quint64 start, quint64 end,
int *colorNumberIn,
ColorNumberToolTipVector *cnmv)
{
int childCount = 0;
const int rowCount = m.model()->rowCount(m);
if (!rowCount)
return childCount;
const QString nameRoot = name + QLatin1Char('.');
for (int r = 0; r < rowCount; r++) {
const QModelIndex childIndex = m.child(r, 0);
const quint64 childAddress = addressOf(childIndex);
const uint childSize = sizeOf(childIndex);
if (childAddress && childAddress >= start
&& (childAddress + childSize) <= end) { // Non-static, within area?
const QString childName = nameRoot + nameOf(childIndex);
const quint64 childOffset = childAddress - start;
const QString toolTip = WatchWindow::tr("%1 at #%2").arg(childName).arg(childOffset);
const ColorNumberToolTipPair colorNumberNamePair((*colorNumberIn)++, toolTip);
const ColorNumberToolTipVector::iterator begin = cnmv->begin() + childOffset;
qFill(begin, begin + childSize, colorNumberNamePair);
childCount++;
childCount += memberVariableRecursion(childIndex, childName, start, end, colorNumberIn, cnmv);
}
}
return childCount;
}
/*!
\fn variableMemoryMarkup()
\brief Creates markup for a variable in the memory view.
Marks the visible children with alternating colors in the parent, that is, for
\code
struct Foo {
char c1
char c2
int x2;
QPair<int, int> pair
}
\endcode
create something like:
\code
0 memberColor1
1 memberColor2
2 base color (padding area of parent)
3 base color
4 member color1
...
8 memberColor2 (pair.first)
...
12 memberColor1 (pair.second)
\endcode
Fixme: When dereferencing a pointer, the size of the pointee is not
known, currently. So, we take an area of 1024 and fill the background
with the default color so that just the members are shown
(sizeIsEstimate=true). This could be fixed by passing the pointee size
as well from the debugger, but would require expensive type manipulation.
\sa Debugger::Internal::MemoryViewWidget
*/
typedef QList<MemoryViewWidget::Markup> MemoryViewWidgetMarkup;
static inline MemoryViewWidgetMarkup
variableMemoryMarkup(const QModelIndex &m, quint64 address, quint64 size,
bool sizeIsEstimate,
const QTextCharFormat &defaultFormat,
const QColor &defaultBackground)
{
enum { debug = 0 };
// Starting out from base, create an array representing the area filled with base
// color. Fill children with some unique color numbers,
// leaving the padding areas of the parent colored with the base color.
MemoryViewWidgetMarkup result;
const QString name = nameOf(m);
int colorNumber = 0;
ColorNumberToolTipVector ranges(size, ColorNumberToolTipPair(colorNumber, name));
const int childCount = memberVariableRecursion(m, name, address, address + size, &colorNumber, &ranges);
if (sizeIsEstimate && !childCount)
return result; // Fixme: Exact size not known, no point in filling if no children.
if (debug) {
QDebug dbg = qDebug().nospace();
dbg << name << ' ' << address << ' ' << size << '\n';
QString name;
for (unsigned i = 0; i < size; i++)
if (name != ranges.at(i).second) {
dbg << ",[" << i << ' ' << ranges.at(i).first << ' ' << ranges.at(i).second << ']';
name = ranges.at(i).second;
}
}
// Condense ranges of identical color into markup ranges. Assign member colors
// interchangeably.
const QColor baseColor = sizeIsEstimate ? defaultBackground : Qt::lightGray;
QColor memberColor1 = QColor(Qt::yellow).lighter();
QColor memberColor2 = QColor(Qt::cyan).lighter();
int lastColorNumber = 0;
int childNumber = 0;
for (unsigned i = 0; i < size; i++) {
const ColorNumberToolTipPair &range = ranges.at(i);
if (result.isEmpty() || lastColorNumber != range.first) {
lastColorNumber = range.first;
QTextCharFormat format = defaultFormat;
if (range.first == 0) { // Base color: Parent
format.setBackground(QBrush(baseColor));
} else {
if (childNumber++ & 1) { // Alternating member colors.
format.setBackground(QBrush(memberColor1));
memberColor1 = memberColor1.darker(120);
} else {
format.setBackground(QBrush(memberColor2));
memberColor2 = memberColor2.darker(120);
}
} // color switch
result.push_back(MemoryViewWidget::Markup(address + i, 1, format, range.second));
} else {
result.back().size++;
}
}
if (debug) {
QDebug dbg = qDebug().nospace();
dbg << name << ' ' << address << ' ' << size << '\n';
QString name;
for (unsigned i = 0; i < size; i++)
if (name != ranges.at(i).second) {
dbg << ',' << i << ' ' << ranges.at(i).first << ' ' << ranges.at(i).second;
name = ranges.at(i).second;
}
dbg << '\n';
foreach (const MemoryViewWidget::Markup &m, result)
dbg << m.address << ' ' << m.size << ' ' << m.toolTip << '\n';
}
return result;
}
// Convenience to create a memory view of a variable.
static void addVariableMemoryView(DebuggerEngine *engine,
const QModelIndex &m, bool deferencePointer,
const QPoint &p, QWidget *parent)
{
const QColor background = parent->palette().color(QPalette::Normal, QPalette::Base);
LocalsMemoryViewWidget *w = new LocalsMemoryViewWidget(parent);
const quint64 address = deferencePointer ? pointerValueOf(m) : addressOf(m);
// Fixme: Get the size of pointee (see variableMemoryMarkup())?
// Also, gdb does not report the size yet as of 8.4.2011
const quint64 typeSize = sizeOf(m);
const bool sizeIsEstimate = deferencePointer || !typeSize;
const quint64 size = sizeIsEstimate ? 1024 : typeSize;
if (!address)
return;
const MemoryViewWidgetMarkup markup
= variableMemoryMarkup(m, address, size, sizeIsEstimate,
w->textCharFormat(), background);
w->init(address, qMax(size, LocalsMemoryViewWidget::defaultLength), nameOf(m));
w->setMarkup(markup);
w->move(p);
engine->addMemoryView(w);
}
/////////////////////////////////////////////////////////////////////
//
// WatchWindow
//
/////////////////////////////////////////////////////////////////////
WatchWindow::WatchWindow(Type type, QWidget *parent)
: QTreeView(parent),
m_type(type)
{
setObjectName(QLatin1String("WatchWindow"));
m_grabbing = false;
setFrameStyle(QFrame::NoFrame);
setAttribute(Qt::WA_MacShowFocusRect, false);
setWindowTitle(tr("Locals and Watchers"));
setIndentation(indentation() * 9/10);
setUniformRowHeights(true);
setItemDelegate(new WatchDelegate(this));
setDragEnabled(true);
setAcceptDrops(true);
setDropIndicatorShown(true);
QAction *useColors = debuggerCore()->action(UseAlternatingRowColors);
setAlternatingRowColors(useColors->isChecked());
QAction *adjustColumns = debuggerCore()->action(AlwaysAdjustLocalsColumnWidths);
connect(useColors, SIGNAL(toggled(bool)),
SLOT(setAlternatingRowColorsHelper(bool)));
connect(adjustColumns, SIGNAL(triggered(bool)),
SLOT(setAlwaysResizeColumnsToContents(bool)));
connect(this, SIGNAL(expanded(QModelIndex)),
SLOT(expandNode(QModelIndex)));
connect(this, SIGNAL(collapsed(QModelIndex)),
SLOT(collapseNode(QModelIndex)));
}
void WatchWindow::expandNode(const QModelIndex &idx)
{
setModelData(LocalsExpandedRole, true, idx);
}
void WatchWindow::collapseNode(const QModelIndex &idx)
{
setModelData(LocalsExpandedRole, false, idx);
}
void WatchWindow::keyPressEvent(QKeyEvent *ev)
{
if (ev->key() == Qt::Key_Delete && m_type == WatchersType) {
QModelIndex idx = currentIndex();
QModelIndex idx1 = idx.sibling(idx.row(), 0);
QString exp = idx1.data(LocalsRawExpressionRole).toString();
removeWatchExpression(exp);
} else if (ev->key() == Qt::Key_Return
&& ev->modifiers() == Qt::ControlModifier
&& m_type == LocalsType) {
QModelIndex idx = currentIndex();
QModelIndex idx1 = idx.sibling(idx.row(), 0);
QString exp = model()->data(idx1).toString();
watchExpression(exp);
}
QTreeView::keyPressEvent(ev);
}
void WatchWindow::dragEnterEvent(QDragEnterEvent *ev)
{
//QTreeView::dragEnterEvent(ev);
if (ev->mimeData()->hasFormat("text/plain")) {
ev->setDropAction(Qt::CopyAction);
ev->accept();
}
}
void WatchWindow::dragMoveEvent(QDragMoveEvent *ev)
{
//QTreeView::dragMoveEvent(ev);
if (ev->mimeData()->hasFormat("text/plain")) {
ev->setDropAction(Qt::CopyAction);
ev->accept();
}
}
void WatchWindow::dropEvent(QDropEvent *ev)
{
if (ev->mimeData()->hasFormat("text/plain")) {
watchExpression(ev->mimeData()->text());
//ev->acceptProposedAction();
ev->setDropAction(Qt::CopyAction);
ev->accept();
}
//QTreeView::dropEvent(ev);
}
void WatchWindow::mouseDoubleClickEvent(QMouseEvent *ev)
{
const QModelIndex idx = indexAt(ev->pos());
if (!idx.isValid()) {
// The "<Edit>" case.
watchExpression(QString());
return;
}
QTreeView::mouseDoubleClickEvent(ev);
}
// Text for add watch action with truncated expression
static inline QString addWatchActionText(QString exp)
{
if (exp.isEmpty())
return WatchWindow::tr("Watch Expression");
if (exp.size() > 30) {
exp.truncate(30);
exp.append(QLatin1String("..."));
}
return WatchWindow::tr("Watch Expression \"%1\"").arg(exp);
}
// Text for add watch action with truncated expression
static inline QString removeWatchActionText(QString exp)
{
if (exp.isEmpty())
return WatchWindow::tr("Remove Watch Expression");
if (exp.size() > 30) {
exp.truncate(30);
exp.append(QLatin1String("..."));
}
return WatchWindow::tr("Remove Watch Expression \"%1\"").arg(exp);
}
static inline void copyToClipboard(const QString &clipboardText)
{
QClipboard *clipboard = QApplication::clipboard();
#ifdef Q_WS_X11
clipboard->setText(clipboardText, QClipboard::Selection);
#endif
clipboard->setText(clipboardText, QClipboard::Clipboard);
}
void WatchWindow::contextMenuEvent(QContextMenuEvent *ev)
{
DebuggerEngine *engine = currentEngine();
WatchHandler *handler = engine->watchHandler();
const QModelIndex idx = indexAt(ev->pos());
const QModelIndex mi0 = idx.sibling(idx.row(), 0);
const QModelIndex mi1 = idx.sibling(idx.row(), 1);
const QModelIndex mi2 = idx.sibling(idx.row(), 2);
const quint64 address = addressOf(mi0);
const uint size = sizeOf(mi0);
const quint64 pointerValue = pointerValueOf(mi0);
const QString exp = mi0.data(LocalsExpressionRole).toString();
const QString type = mi2.data().toString();
const QStringList alternativeFormats =
mi0.data(LocalsTypeFormatListRole).toStringList();
const int typeFormat =
mi0.data(LocalsTypeFormatRole).toInt();
const int individualFormat =
mi0.data(LocalsIndividualFormatRole).toInt();
const int effectiveIndividualFormat =
individualFormat == -1 ? typeFormat : individualFormat;
const int unprintableBase = handler->unprintableBase();
QMenu formatMenu;
QList<QAction *> typeFormatActions;
QList<QAction *> individualFormatActions;
QAction *clearTypeFormatAction = 0;
QAction *clearIndividualFormatAction = 0;
QAction *showUnprintableUnicode = 0;
QAction *showUnprintableOctal = 0;
QAction *showUnprintableHexadecimal = 0;
formatMenu.setTitle(tr("Change Display Format..."));
showUnprintableUnicode =
formatMenu.addAction(tr("Treat All Characters as Printable"));
showUnprintableUnicode->setCheckable(true);
showUnprintableUnicode->setChecked(unprintableBase == 0);
showUnprintableOctal =
formatMenu.addAction(tr("Show Unprintable Characters as Octal"));
showUnprintableOctal->setCheckable(true);
showUnprintableOctal->setChecked(unprintableBase == 8);
showUnprintableHexadecimal =
formatMenu.addAction(tr("Show Unprintable Characters as Hexadecimal"));
showUnprintableHexadecimal->setCheckable(true);
showUnprintableHexadecimal->setChecked(unprintableBase == 16);
if (idx.isValid() /*&& !alternativeFormats.isEmpty() */) {
const QString spacer = QLatin1String(" ");
formatMenu.addSeparator();
QAction *dummy = formatMenu.addAction(
tr("Change Display for Object Named \"%1\":").arg(mi0.data().toString()));
dummy->setEnabled(false);
clearIndividualFormatAction
= formatMenu.addAction(spacer + tr("Use Display Format Based on Type"));
//clearIndividualFormatAction->setEnabled(individualFormat != -1);
clearIndividualFormatAction->setCheckable(true);
clearIndividualFormatAction->setChecked(effectiveIndividualFormat == -1);
for (int i = 0; i != alternativeFormats.size(); ++i) {
const QString format = spacer + alternativeFormats.at(i);
QAction *act = new QAction(format, &formatMenu);
act->setCheckable(true);
if (i == effectiveIndividualFormat)
act->setChecked(true);
formatMenu.addAction(act);
individualFormatActions.append(act);
}
formatMenu.addSeparator();
dummy = formatMenu.addAction(
tr("Change Display for Type \"%1\":").arg(type));
dummy->setEnabled(false);
clearTypeFormatAction = formatMenu.addAction(spacer + tr("Automatic"));
//clearTypeFormatAction->setEnabled(typeFormat != -1);
//clearTypeFormatAction->setEnabled(individualFormat != -1);
clearTypeFormatAction->setCheckable(true);
clearTypeFormatAction->setChecked(typeFormat == -1);
for (int i = 0; i != alternativeFormats.size(); ++i) {
const QString format = spacer + alternativeFormats.at(i);
QAction *act = new QAction(format, &formatMenu);
act->setCheckable(true);
//act->setEnabled(individualFormat != -1);
if (i == typeFormat)
act->setChecked(true);
formatMenu.addAction(act);
typeFormatActions.append(act);
}
} else {
QAction *dummy = formatMenu.addAction(
tr("Change Display for Type or Item..."));
dummy->setEnabled(false);
}
const bool actionsEnabled = engine->debuggerActionsEnabled();
const unsigned engineCapabilities = engine->debuggerCapabilities();
const bool canHandleWatches = engineCapabilities & AddWatcherCapability;
const DebuggerState state = engine->state();
const bool canInsertWatches = (state==InferiorStopOk) || ((state==InferiorRunOk) && engine->acceptsWatchesWhileRunning());
QMenu menu;
QAction *actInsertNewWatchItem = menu.addAction(tr("Insert New Watch Item"));
actInsertNewWatchItem->setEnabled(canHandleWatches && canInsertWatches);
QAction *actSelectWidgetToWatch = menu.addAction(tr("Select Widget to Watch"));
actSelectWidgetToWatch->setEnabled(canHandleWatches && (engine->canWatchWidgets()));
// Offer to open address pointed to or variable address.
const bool createPointerActions = pointerValue && pointerValue != address;
menu.addSeparator();
QAction *actSetWatchpointAtVariableAddress = 0;
QAction *actSetWatchpointAtPointerValue = 0;
const bool canSetWatchpoint = engineCapabilities & WatchpointCapability;
if (canSetWatchpoint && address) {
actSetWatchpointAtVariableAddress =
new QAction(tr("Add Watchpoint at Object's Address (0x%1)")
.arg(address, 0, 16), &menu);
actSetWatchpointAtVariableAddress->
setChecked(mi0.data(LocalsIsWatchpointAtAddressRole).toBool());
if (createPointerActions) {
actSetWatchpointAtPointerValue =
new QAction(tr("Add Watchpoint at Referenced Address (0x%1)")
.arg(pointerValue, 0, 16), &menu);
actSetWatchpointAtPointerValue->setCheckable(true);
actSetWatchpointAtPointerValue->
setChecked(mi0.data(LocalsIsWatchpointAtPointerValueRole).toBool());
}
} else {
actSetWatchpointAtVariableAddress =
new QAction(tr("Add Watchpoint"), &menu);
actSetWatchpointAtVariableAddress->setEnabled(false);
}
actSetWatchpointAtVariableAddress->setToolTip(
tr("Setting a watchpoint on an address will cause the program "
"to stop when the data at the address it modified."));
QAction *actWatchExpression = new QAction(addWatchActionText(exp), &menu);
actWatchExpression->setEnabled(canHandleWatches && !exp.isEmpty());
// Can remove watch if engine can handle it or session engine.
QAction *actRemoveWatchExpression = new QAction(removeWatchActionText(exp), &menu);
actRemoveWatchExpression->setEnabled(
(canHandleWatches || state == DebuggerNotReady) && !exp.isEmpty());
QAction *actRemoveWatches = new QAction(tr("Remove All Watch Items"), &menu);
actRemoveWatches->setEnabled(!WatchHandler::watcherNames().isEmpty());
if (m_type == LocalsType)
menu.addAction(actWatchExpression);
else {
menu.addAction(actRemoveWatchExpression);
menu.addAction(actRemoveWatches);
}
QMenu memoryMenu;
memoryMenu.setTitle(tr("Open Memory Editor..."));
QAction *actOpenMemoryEditAtVariableAddress = new QAction(&memoryMenu);
QAction *actOpenMemoryEditAtPointerValue = new QAction(&memoryMenu);
QAction *actOpenMemoryEditor = new QAction(&memoryMenu);
QAction *actOpenMemoryViewAtVariableAddress = new QAction(&memoryMenu);
QAction *actOpenMemoryViewAtPointerValue = new QAction(&memoryMenu);
if (engineCapabilities & ShowMemoryCapability) {
actOpenMemoryEditor->setText(tr("Open Memory Editor..."));
if (address) {
actOpenMemoryEditAtVariableAddress->setText(
tr("Open Memory Editor at Object's Address (0x%1)")
.arg(address, 0, 16));
actOpenMemoryViewAtVariableAddress->setText(
tr("Open Memory View at Object's Address (0x%1)")
.arg(address, 0, 16));
} else {
actOpenMemoryEditAtVariableAddress->setText(
tr("Open Memory Editor at Object's Address"));
actOpenMemoryEditAtVariableAddress->setEnabled(false);
actOpenMemoryViewAtVariableAddress->setText(
tr("Open Memory View at Object's Address"));
actOpenMemoryViewAtVariableAddress->setEnabled(false);
}
if (createPointerActions) {
actOpenMemoryEditAtPointerValue->setText(
tr("Open Memory Editor at Referenced Address (0x%1)")
.arg(pointerValue, 0, 16));
actOpenMemoryViewAtPointerValue->setText(
tr("Open Memory View at Referenced Address (0x%1)")
.arg(pointerValue, 0, 16));
} else {
actOpenMemoryEditAtPointerValue->setText(
tr("Open Memory Editor at Referenced Address"));
actOpenMemoryEditAtPointerValue->setEnabled(false);
actOpenMemoryViewAtPointerValue->setText(
tr("Open Memory View at Referenced Address"));
actOpenMemoryViewAtPointerValue->setEnabled(false);
}
memoryMenu.addAction(actOpenMemoryViewAtVariableAddress);
memoryMenu.addAction(actOpenMemoryViewAtPointerValue);
memoryMenu.addAction(actOpenMemoryEditAtVariableAddress);
memoryMenu.addAction(actOpenMemoryEditAtPointerValue);
memoryMenu.addAction(actOpenMemoryEditor);
} else {
memoryMenu.setEnabled(false);
}
QAction *actCopy = new QAction(tr("Copy Contents to Clipboard"), &menu);
QAction *actCopyValue = new QAction(tr("Copy Value to Clipboard"), &menu);
actCopyValue->setEnabled(idx.isValid());
menu.addAction(actInsertNewWatchItem);
menu.addAction(actSelectWidgetToWatch);
menu.addMenu(&formatMenu);
menu.addMenu(&memoryMenu);
menu.addAction(actSetWatchpointAtVariableAddress);
if (actSetWatchpointAtPointerValue)
menu.addAction(actSetWatchpointAtPointerValue);
menu.addAction(actCopy);
menu.addAction(actCopyValue);
menu.addSeparator();
menu.addAction(debuggerCore()->action(UseDebuggingHelpers));
menu.addAction(debuggerCore()->action(UseToolTipsInLocalsView));
menu.addAction(debuggerCore()->action(AutoDerefPointers));
menu.addAction(debuggerCore()->action(ShowStdNamespace));
menu.addAction(debuggerCore()->action(ShowQtNamespace));
menu.addAction(debuggerCore()->action(SortStructMembers));
QAction *actAdjustColumnWidths =
menu.addAction(tr("Adjust Column Widths to Contents"));
menu.addAction(debuggerCore()->action(AlwaysAdjustLocalsColumnWidths));
menu.addSeparator();
QAction *actClearCodeModelSnapshot
= new QAction(tr("Refresh Code Model Snapshot"), &menu);
actClearCodeModelSnapshot->setEnabled(actionsEnabled
&& debuggerCore()->action(UseCodeModel)->isChecked());
menu.addAction(actClearCodeModelSnapshot);
QAction *actShowInEditor
= new QAction(tr("Show View Contents in Editor"), &menu);
actShowInEditor->setEnabled(actionsEnabled);
menu.addAction(actShowInEditor);
menu.addAction(debuggerCore()->action(SettingsDialog));
QAction *actCloseEditorToolTips = new QAction(tr("Close Editor Tooltips"), &menu);
actCloseEditorToolTips->setEnabled(DebuggerToolTipManager::instance()->hasToolTips());
menu.addAction(actCloseEditorToolTips);
QAction *act = menu.exec(ev->globalPos());
if (act == 0)
return;
if (act == actAdjustColumnWidths) {
resizeColumnsToContents();
} else if (act == actInsertNewWatchItem) {
bool ok;
QString newExp = QInputDialog::getText(this, tr("Enter watch expression"),
tr("Expression:"), QLineEdit::Normal,
QString(), &ok);
if (ok && !newExp.isEmpty()) {
watchExpression(newExp);
}
} else if (act == actOpenMemoryEditAtVariableAddress) {
currentEngine()->openMemoryView(address);
} else if (act == actOpenMemoryEditAtPointerValue) {
currentEngine()->openMemoryView(pointerValue);
} else if (act == actOpenMemoryEditor) {
AddressDialog dialog;
if (dialog.exec() == QDialog::Accepted)
currentEngine()->openMemoryView(dialog.address());
} else if (act == actOpenMemoryViewAtVariableAddress) {
addVariableMemoryView(currentEngine(), mi0, false, ev->globalPos(), this);
} else if (act == actOpenMemoryViewAtPointerValue) {
addVariableMemoryView(currentEngine(), mi0, true, ev->globalPos(), this);
} else if (act == actSetWatchpointAtVariableAddress) {
setWatchpoint(address, size);
} else if (act == actSetWatchpointAtPointerValue) {
setWatchpoint(pointerValue, 1);
} else if (act == actSelectWidgetToWatch) {
grabMouse(Qt::CrossCursor);
m_grabbing = true;
} else if (act == actWatchExpression) {
watchExpression(exp);
} else if (act == actRemoveWatchExpression) {
removeWatchExpression(exp);
} else if (act == actCopy) {
copyToClipboard(DebuggerTreeViewToolTipWidget::treeModelClipboardContents(model()));
} else if (act == actCopyValue) {
copyToClipboard(mi1.data().toString());
} else if (act == actRemoveWatches) {
currentEngine()->watchHandler()->clearWatches();
} else if (act == actClearCodeModelSnapshot) {
debuggerCore()->clearCppCodeModelSnapshot();
} else if (act == clearTypeFormatAction) {
setModelData(LocalsTypeFormatRole, -1, mi1);
} else if (act == clearIndividualFormatAction) {
setModelData(LocalsIndividualFormatRole, -1, mi1);
} else if (act == actShowInEditor) {
QString contents = handler->editorContents();
debuggerCore()->openTextEditor(tr("Locals & Watchers"), contents);
} else if (act == showUnprintableUnicode) {
handler->setUnprintableBase(0);
} else if (act == showUnprintableOctal) {
handler->setUnprintableBase(8);
} else if (act == showUnprintableHexadecimal) {
handler->setUnprintableBase(16);
} else if (act == actCloseEditorToolTips) {
DebuggerToolTipManager::instance()->closeAllToolTips();
} else {
for (int i = 0; i != typeFormatActions.size(); ++i) {
if (act == typeFormatActions.at(i))
setModelData(LocalsTypeFormatRole, i, mi1);
}
for (int i = 0; i != individualFormatActions.size(); ++i) {
if (act == individualFormatActions.at(i))
setModelData(LocalsIndividualFormatRole, i, mi1);
}
}
}
void WatchWindow::resizeColumnsToContents()
{
resizeColumnToContents(0);
resizeColumnToContents(1);
}
void WatchWindow::setAlwaysResizeColumnsToContents(bool on)
{
if (!header())
return;
QHeaderView::ResizeMode mode = on
? QHeaderView::ResizeToContents : QHeaderView::Interactive;
header()->setResizeMode(0, mode);
header()->setResizeMode(1, mode);
}
bool WatchWindow::event(QEvent *ev)
{
if (m_grabbing && ev->type() == QEvent::MouseButtonPress) {
QMouseEvent *mev = static_cast<QMouseEvent *>(ev);
m_grabbing = false;
releaseMouse();
currentEngine()->watchPoint(mapToGlobal(mev->pos()));
}
return QTreeView::event(ev);
}
void WatchWindow::editItem(const QModelIndex &idx)
{
Q_UNUSED(idx) // FIXME
}
void WatchWindow::setModel(QAbstractItemModel *model)
{
QTreeView::setModel(model);
setRootIsDecorated(true);
if (header()) {
setAlwaysResizeColumnsToContents(
debuggerCore()->boolSetting(AlwaysAdjustLocalsColumnWidths));
header()->setDefaultAlignment(Qt::AlignLeft);
if (m_type != LocalsType)
header()->hide();
}
connect(model, SIGNAL(layoutChanged()), SLOT(resetHelper()));
connect(model, SIGNAL(enableUpdates(bool)), SLOT(setUpdatesEnabled(bool)));
// Potentially left in disabled state in case engine crashes when expanding.
setUpdatesEnabled(true);
}
void WatchWindow::setUpdatesEnabled(bool enable)
{
//qDebug() << "ENABLING UPDATES: " << enable;
QTreeView::setUpdatesEnabled(enable);
}
void WatchWindow::resetHelper()
{
bool old = updatesEnabled();
setUpdatesEnabled(false);
resetHelper(model()->index(0, 0));
setUpdatesEnabled(old);
}
void WatchWindow::resetHelper(const QModelIndex &idx)
{
if (idx.data(LocalsExpandedRole).toBool()) {
//qDebug() << "EXPANDING " << model()->data(idx, INameRole);
if (!isExpanded(idx)) {
expand(idx);
for (int i = 0, n = model()->rowCount(idx); i != n; ++i) {
QModelIndex idx1 = model()->index(i, 0, idx);
resetHelper(idx1);
}
}
} else {
//qDebug() << "COLLAPSING " << model()->data(idx, INameRole);
if (isExpanded(idx))
collapse(idx);
}
}
void WatchWindow::watchExpression(const QString &exp)
{
currentEngine()->watchHandler()->watchExpression(exp);
}
void WatchWindow::removeWatchExpression(const QString &exp)
{
currentEngine()->watchHandler()->removeWatchExpression(exp);
}
void WatchWindow::setModelData
(int role, const QVariant &value, const QModelIndex &index)
{
QTC_ASSERT(model(), return);
model()->setData(index, value, role);
}
void WatchWindow::setWatchpoint(quint64 address, unsigned size)
{
BreakpointParameters data(Watchpoint);
data.address = address;
data.size = size;
BreakpointId id = breakHandler()->findWatchpoint(data);
if (id) {
qDebug() << "WATCHPOINT EXISTS";
// removeBreakpoint(index);
return;
}
breakHandler()->appendBreakpoint(data);
}
} // namespace Internal
} // namespace Debugger
|