watchwindow.cpp 8.41 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, 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.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
#include "watchwindow.h"

32
33
#include "debuggeractions.h"

34
35
#include <utils/qtcassert.h>

con's avatar
con committed
36
37
38
39
40
41
#include <QtCore/QDebug>
#include <QtCore/QTimer>

#include <QtGui/QAction>
#include <QtGui/QContextMenuEvent>
#include <QtGui/QHeaderView>
42
#include <QtGui/QItemDelegate>
con's avatar
con committed
43
44
45
46
47
48
49
#include <QtGui/QLineEdit>
#include <QtGui/QMenu>
#include <QtGui/QResizeEvent>
#include <QtGui/QSplitter>

using namespace Debugger::Internal;

50
51
52
53
54
55
56

/////////////////////////////////////////////////////////////////////
//
// WatchDelegate
//
/////////////////////////////////////////////////////////////////////

57
enum { INameRole = Qt::UserRole, ExpressionRole, ExpandedRole };
con's avatar
con committed
58

59
60
61
62
63
64
65
66
class WatchDelegate : public QItemDelegate
{
public:
    WatchDelegate(QObject *parent) : QItemDelegate(parent) {}

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
        const QModelIndex &) const
    {
67
        qDebug() << "CREATE EDITOR";
68
69
70
71
72
73
74
        return new QLineEdit(parent);
    }

    void setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
        QTC_ASSERT(lineEdit, return);
75
76
77
78
        if (index.column() == 1) 
            lineEdit->setText(index.model()->data(index, Qt::DisplayRole).toString());
        else
            lineEdit->setText(index.model()->data(index, ExpressionRole).toString());
79
80
    }

81
    void setModelData(QWidget *editor, QAbstractItemModel *model,
82
83
        const QModelIndex &index) const
    {
84
        //qDebug() << "SET MODEL DATA";
85
86
87
        QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
        QTC_ASSERT(lineEdit, return);
        QString value = lineEdit->text();
88
89
        QString exp = model->data(index, ExpressionRole).toString();
        model->setData(index, value, Qt::EditRole);
90
91
        if (index.column() == 1) {
            // the value column
hjk's avatar
hjk committed
92
            theDebuggerAction(AssignValue)->trigger(exp + '=' + value);
hjk's avatar
hjk committed
93
94
95
        } else if (index.column() == 2) {
            // the type column
            theDebuggerAction(AssignType)->trigger(exp + '=' + value);
96
97
        } else if (index.column() == 0) {
            // the watcher name column
hjk's avatar
hjk committed
98
            theDebuggerAction(RemoveWatchExpression)->trigger(exp);
hjk's avatar
hjk committed
99
            theDebuggerAction(WatchExpression)->trigger(value);
100
101
102
103
104
105
106
107
108
109
110
        }
    }

    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
        const QModelIndex &) const
    {
        editor->setGeometry(option.rect);
    }
};


con's avatar
con committed
111
112
113
114
115
116
117
/////////////////////////////////////////////////////////////////////
//
// WatchWindow
//
/////////////////////////////////////////////////////////////////////

WatchWindow::WatchWindow(Type type, QWidget *parent)
118
    : QTreeView(parent), m_alwaysResizeColumnsToContents(true), m_type(type)
con's avatar
con committed
119
120
121
122
123
{
    setWindowTitle(tr("Locals and Watchers"));
    setAlternatingRowColors(true);
    setIndentation(indentation() * 9/10);
    setUniformRowHeights(true);
124
    setItemDelegate(new WatchDelegate(this));
con's avatar
con committed
125
126
127
128
129
130
131
132
133

    connect(this, SIGNAL(expanded(QModelIndex)),
        this, SLOT(expandNode(QModelIndex)));
    connect(this, SIGNAL(collapsed(QModelIndex)),
        this, SLOT(collapseNode(QModelIndex)));
}

void WatchWindow::expandNode(const QModelIndex &idx)
{
134
135
136
    QModelIndex mi0 = idx.sibling(idx.row(), 0);
    QVariant iname = model()->data(mi0, INameRole);
    theDebuggerAction(ExpandItem)->trigger(iname);
con's avatar
con committed
137
138
139
140
}

void WatchWindow::collapseNode(const QModelIndex &idx)
{
141
142
143
    QModelIndex mi0 = idx.sibling(idx.row(), 0);
    QVariant iname = model()->data(mi0, INameRole);
    theDebuggerAction(CollapseItem)->trigger(iname);
con's avatar
con committed
144
145
}

146
147
void WatchWindow::keyPressEvent(QKeyEvent *ev)
{
hjk's avatar
hjk committed
148
    if (ev->key() == Qt::Key_Delete && m_type == WatchersType) {
149
150
151
        QModelIndex idx = currentIndex();
        QModelIndex idx1 = idx.sibling(idx.row(), 0);
        QString exp = model()->data(idx1).toString();
hjk's avatar
hjk committed
152
        theDebuggerAction(RemoveWatchExpression)->trigger(exp);
153
154
155
    } else if (ev->key() == Qt::Key_Return
            && ev->modifiers() == Qt::ControlModifier
            && m_type == LocalsType) {
hjk's avatar
hjk committed
156
157
158
159
        QModelIndex idx = currentIndex();
        QModelIndex idx1 = idx.sibling(idx.row(), 0);
        QString exp = model()->data(idx1).toString();
        theDebuggerAction(WatchExpression)->trigger(exp);
160
161
162
163
    }
    QTreeView::keyPressEvent(ev);
}

con's avatar
con committed
164
165
166
void WatchWindow::contextMenuEvent(QContextMenuEvent *ev)
{
    QMenu menu;
167
168
    QAction *act1 = new QAction(tr("Adjust column widths to contents"), &menu);
    QAction *act2 = new QAction(tr("Always adjust column widths to contents"), &menu);
con's avatar
con committed
169
170
171
172
173
174
175
176
177
    act2->setCheckable(true);
    act2->setChecked(m_alwaysResizeColumnsToContents);

    menu.addAction(act1);
    menu.addAction(act2);

    QModelIndex idx = indexAt(ev->pos());
    QModelIndex mi0 = idx.sibling(idx.row(), 0);
    QString exp = model()->data(mi0).toString();
178
179
180

    menu.addSeparator();
    int type = (m_type == LocalsType) ? WatchExpression : RemoveWatchExpression;
hjk's avatar
hjk committed
181
    menu.addAction(theDebuggerAction(type)->updatedAction(exp));
182

183
    //QAction *act4 = theDebuggerAction(WatchExpressionInWindow);
184
185
    //menu.addAction(act4);

186
187
    QAction *act3 = new QAction(tr("Insert new watch item"), &menu); 
    menu.addAction(act3);
188

189
    menu.addSeparator();
190
191
    menu.addAction(theDebuggerAction(RecheckDebuggingHelpers));
    menu.addAction(theDebuggerAction(UseDebuggingHelpers));
192
    menu.addSeparator();
193
    menu.addAction(theDebuggerAction(SettingsDialog));
con's avatar
con committed
194
195
196

    QAction *act = menu.exec(ev->globalPos());

197
    if (act == act1)
con's avatar
con committed
198
199
200
        resizeColumnsToContents();
    else if (act == act2)
        setAlwaysResizeColumnsToContents(!m_alwaysResizeColumnsToContents);
201
    else if (act == act3)
202
        theDebuggerAction(WatchExpression)->trigger(tr("<Edit>"));
con's avatar
con committed
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
}

void WatchWindow::resizeColumnsToContents()
{
    resizeColumnToContents(0);
    resizeColumnToContents(1);
}

void WatchWindow::setAlwaysResizeColumnsToContents(bool on)
{
    if (!header())
        return;
    m_alwaysResizeColumnsToContents = on;
    QHeaderView::ResizeMode mode = on 
        ? QHeaderView::ResizeToContents : QHeaderView::Interactive;
    header()->setResizeMode(0, mode);
    header()->setResizeMode(1, mode);
}

void WatchWindow::editItem(const QModelIndex &idx)
{
    Q_UNUSED(idx); // FIXME
}

void WatchWindow::reset()
{
229
    QTreeView::reset(); 
con's avatar
con committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    int row = 0;
    if (m_type == TooltipType)
        row = 1;
    else if (m_type == WatchersType)
        row = 2;
    setRootIndex(model()->index(row, 0, model()->index(0, 0)));
}

void WatchWindow::setModel(QAbstractItemModel *model)
{
    QTreeView::setModel(model);

    setRootIsDecorated(true);
    header()->setDefaultAlignment(Qt::AlignLeft);
    header()->setResizeMode(QHeaderView::ResizeToContents);
    if (m_type != LocalsType)
        header()->hide();
247
248
249
250
251
252
253

    connect(model, SIGNAL(layoutChanged()), this, SLOT(resetHelper()));
}

void WatchWindow::resetHelper()
{
    resetHelper(model()->index(0, 0));
con's avatar
con committed
254
255
}

256
void WatchWindow::resetHelper(const QModelIndex &idx)
con's avatar
con committed
257
{
258
    if (model()->data(idx, ExpandedRole).toBool()) {
259
        //qDebug() << "EXPANDING " << model()->data(idx, INameRole);
con's avatar
con committed
260
261
262
        expand(idx);
        for (int i = 0, n = model()->rowCount(idx); i != n; ++i) {
            QModelIndex idx1 = model()->index(i, 0, idx);
263
            resetHelper(idx1);
con's avatar
con committed
264
        }
265
266
267
    } else {
        //qDebug() << "COLLAPSING " << model()->data(idx, INameRole);
        collapse(idx);
con's avatar
con committed
268
269
    }
}