OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / stackwindow.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "stackwindow.h"
35 #include "stackhandler.h"
36
37 #include "debuggeractions.h"
38 #include "debuggerconstants.h"
39 #include "debuggercore.h"
40 #include "debuggerengine.h"
41
42 #include <utils/qtcassert.h>
43 #include <utils/savedaction.h>
44
45 #include <QtCore/QDebug>
46
47 #include <QtGui/QApplication>
48 #include <QtGui/QClipboard>
49 #include <QtGui/QHeaderView>
50 #include <QtGui/QMenu>
51 #include <QtGui/QResizeEvent>
52
53 namespace Debugger {
54 namespace Internal {
55
56 static DebuggerEngine *currentEngine()
57 {
58     return debuggerCore()->currentEngine();
59 }
60
61 StackWindow::StackWindow(QWidget *parent)
62     : QTreeView(parent), m_alwaysResizeColumnsToContents(false)
63 {
64     setAttribute(Qt::WA_MacShowFocusRect, false);
65     setFrameStyle(QFrame::NoFrame);
66
67     QAction *act = debuggerCore()->action(UseAlternatingRowColors);
68     setWindowTitle(tr("Stack"));
69
70     setAlternatingRowColors(act->isChecked());
71     setRootIsDecorated(false);
72     setIconSize(QSize(10, 10));
73
74     header()->setDefaultAlignment(Qt::AlignLeft);
75
76     connect(this, SIGNAL(activated(QModelIndex)),
77         SLOT(rowActivated(QModelIndex)));
78     connect(act, SIGNAL(toggled(bool)),
79         SLOT(setAlternatingRowColorsHelper(bool)));
80     connect(debuggerCore()->action(UseAddressInStackView), SIGNAL(toggled(bool)),
81         SLOT(showAddressColumn(bool)));
82     connect(debuggerCore()->action(ExpandStack), SIGNAL(triggered()),
83         SLOT(reloadFullStack()));
84     connect(debuggerCore()->action(MaximalStackDepth), SIGNAL(triggered()),
85         SLOT(reloadFullStack()));
86     showAddressColumn(false);
87 }
88
89 void StackWindow::showAddressColumn(bool on)
90 {
91     setColumnHidden(4, !on);
92 }
93
94 void StackWindow::rowActivated(const QModelIndex &index)
95 {
96     currentEngine()->activateFrame(index.row());
97 }
98
99 void StackWindow::setModel(QAbstractItemModel *model)
100 {
101     QTreeView::setModel(model);
102     //resizeColumnsToContents();
103     resizeColumnToContents(0);
104     resizeColumnToContents(3);
105 }
106
107 void StackWindow::contextMenuEvent(QContextMenuEvent *ev)
108 {
109     DebuggerEngine *engine = currentEngine();
110     StackHandler *handler = engine->stackHandler();
111     const QModelIndex index = indexAt(ev->pos());
112     const int row = index.row();
113     const unsigned engineCapabilities = engine->debuggerCapabilities();
114     StackFrame frame;
115     if (row >= 0 && row < handler->stackSize())
116         frame = handler->frameAt(row);
117     const quint64 address = frame.address;
118
119     QMenu menu;
120     menu.addAction(debuggerCore()->action(ExpandStack));
121
122     QAction *actCopyContents = menu.addAction(tr("Copy Contents to Clipboard"));
123     actCopyContents->setEnabled(model() != 0);
124
125     if (engineCapabilities & CreateFullBacktraceCapability)
126         menu.addAction(debuggerCore()->action(CreateFullBacktrace));
127
128     QAction *actShowMemory = menu.addAction(QString());
129     if (address == 0) {
130         actShowMemory->setText(tr("Open Memory Editor"));
131         actShowMemory->setEnabled(false);
132     } else {
133         actShowMemory->setText(tr("Open Memory Editor at 0x%1").arg(address, 0, 16));
134         actShowMemory->setEnabled(engineCapabilities & ShowMemoryCapability);
135     }
136
137     QAction *actShowDisassembler = menu.addAction(QString());
138     if (address == 0) {
139         actShowDisassembler->setText(tr("Open Disassembler"));
140         actShowDisassembler->setEnabled(false);
141     } else {
142         actShowDisassembler->setText(tr("Open Disassembler at 0x%1").arg(address, 0, 16));
143         actShowDisassembler->setEnabled(engineCapabilities & DisassemblerCapability);
144     }
145
146     QAction *actLoadSymbols = 0;
147     if (engineCapabilities & ShowModuleSymbolsCapability)
148         actLoadSymbols = menu.addAction(tr("Try to Load Unknown Symbols"));
149
150     menu.addSeparator();
151 #if 0 // @TODO: not implemented
152     menu.addAction(debuggerCore()->action(UseToolTipsInStackView));
153 #endif
154     menu.addAction(debuggerCore()->action(UseAddressInStackView));
155
156     QAction *actAdjust = menu.addAction(tr("Adjust Column Widths to Contents"));
157
158     QAction *actAlwaysAdjust =
159         menu.addAction(tr("Always Adjust Column Widths to Contents"));
160     actAlwaysAdjust->setCheckable(true);
161     actAlwaysAdjust->setChecked(m_alwaysResizeColumnsToContents);
162
163     menu.addSeparator();
164
165     menu.addAction(debuggerCore()->action(SettingsDialog));
166
167     QAction *act = menu.exec(ev->globalPos());
168
169     if (!act)
170         ;
171     else if (act == actCopyContents)
172         copyContentsToClipboard();
173     else if (act == actAdjust)
174         resizeColumnsToContents();
175     else if (act == actAlwaysAdjust)
176         setAlwaysResizeColumnsToContents(!m_alwaysResizeColumnsToContents);
177     else if (act == actShowMemory)
178         engine->openMemoryView(address);
179     else if (act == actShowDisassembler)
180         engine->openDisassemblerView(frame);
181     else if (act == actLoadSymbols)
182         engine->loadSymbolsForStack();
183 }
184
185 void StackWindow::copyContentsToClipboard()
186 {
187     QString str;
188     int n = model()->rowCount();
189     int m = model()->columnCount();
190     for (int i = 0; i != n; ++i) {
191         for (int j = 0; j != m; ++j) {
192             QModelIndex index = model()->index(i, j);
193             str += model()->data(index).toString();
194             str += '\t';
195         }
196         str += '\n';
197     }
198     QClipboard *clipboard = QApplication::clipboard();
199 #    ifdef Q_WS_X11
200     clipboard->setText(str, QClipboard::Selection);
201 #    endif
202     clipboard->setText(str, QClipboard::Clipboard);
203 }
204
205 void StackWindow::reloadFullStack()
206 {
207     currentEngine()->reloadFullStack();
208 }
209
210 void StackWindow::resizeColumnsToContents()
211 {
212     for (int i = model()->columnCount(); --i >= 0; )
213         resizeColumnToContents(i);
214 }
215
216 void StackWindow::setAlwaysResizeColumnsToContents(bool on)
217 {
218     m_alwaysResizeColumnsToContents = on;
219     QHeaderView::ResizeMode mode =
220         on ? QHeaderView::ResizeToContents : QHeaderView::Interactive;
221     for (int i = model()->columnCount(); --i >= 0; )
222         header()->setResizeMode(i, mode);
223 }
224
225 } // namespace Internal
226 } // namespace Debugger