OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / stackhandler.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 "stackhandler.h"
35
36 #include "debuggeractions.h"
37 #include "debuggercore.h"
38 #include "debuggerengine.h"
39
40 #include <utils/qtcassert.h>
41 #include <utils/savedaction.h>
42
43 #include <QtCore/QDebug>
44 #include <QtCore/QFileInfo>
45
46 namespace Debugger {
47 namespace Internal {
48
49 ////////////////////////////////////////////////////////////////////////
50 //
51 // StackHandler
52 //
53 ////////////////////////////////////////////////////////////////////////
54
55 StackHandler::StackHandler()
56   : m_positionIcon(QIcon(QLatin1String(":/debugger/images/location_16.png"))),
57     m_emptyIcon(QIcon(QLatin1String(":/debugger/images/debugger_empty_14.png")))
58 {
59     m_resetLocationScheduled = false;
60     m_contentsValid = false;
61     m_currentIndex = 0;
62     m_canExpand = false;
63     connect(debuggerCore()->action(OperateByInstruction), SIGNAL(triggered()),
64         this, SLOT(resetModel()));
65 }
66
67 StackHandler::~StackHandler()
68 {
69 }
70
71 int StackHandler::rowCount(const QModelIndex &parent) const
72 {
73     // Since the stack is not a tree, row count is 0 for any valid parent
74     return parent.isValid() ? 0 : (m_stackFrames.size() + m_canExpand);
75 }
76
77 int StackHandler::columnCount(const QModelIndex &parent) const
78 {
79     return parent.isValid() ? 0 : 5;
80 }
81
82 QVariant StackHandler::data(const QModelIndex &index, int role) const
83 {
84     if (!index.isValid() || index.row() >= m_stackFrames.size() + m_canExpand)
85         return QVariant();
86
87     if (index.row() == m_stackFrames.size()) {
88         if (role == Qt::DisplayRole && index.column() == 0)
89             return tr("...");
90         if (role == Qt::DisplayRole && index.column() == 1)
91             return tr("<More>");
92         if (role == Qt::DecorationRole && index.column() == 0)
93             return m_emptyIcon;
94         return QVariant();
95     }
96
97     const StackFrame &frame = m_stackFrames.at(index.row());
98
99     if (role == Qt::DisplayRole) {
100         switch (index.column()) {
101         case 0: // Stack frame level
102             return QString::number(frame.level);
103         case 1: // Function name
104             return frame.function;
105         case 2: // File name
106             return frame.file.isEmpty() ? frame.from : QFileInfo(frame.file).fileName();
107         case 3: // Line number
108             return frame.line >= 0 ? QVariant(frame.line) : QVariant();
109         case 4: // Address
110             if (frame.address)
111                 return QString::fromAscii("0x%1").arg(frame.address, 0, 16);
112             return QString();
113         }
114         return QVariant();
115     }
116
117     if (role == Qt::DecorationRole && index.column() == 0) {
118         // Return icon that indicates whether this is the active stack frame
119         return (m_contentsValid && index.row() == m_currentIndex)
120             ? m_positionIcon : m_emptyIcon;
121     }
122
123     if (role == Qt::ToolTipRole)
124         return frame.toToolTip();
125
126     return QVariant();
127 }
128
129
130 QVariant StackHandler::headerData(int section, Qt::Orientation orient, int role) const
131 {
132     if (orient == Qt::Horizontal && role == Qt::DisplayRole) {
133         switch (section) {
134             case 0: return tr("Level");
135             case 1: return tr("Function");
136             case 2: return tr("File");
137             case 3: return tr("Line");
138             case 4: return tr("Address");
139         };
140     }
141     return QVariant();
142 }
143
144 Qt::ItemFlags StackHandler::flags(const QModelIndex &index) const
145 {
146     if (index.row() >= m_stackFrames.size() + m_canExpand)
147         return 0;
148     if (index.row() == m_stackFrames.size())
149         return QAbstractTableModel::flags(index);
150     const StackFrame &frame = m_stackFrames.at(index.row());
151     const bool isValid = (frame.isUsable() && !frame.function.isEmpty())
152         || debuggerCore()->boolSetting(OperateByInstruction);
153     return isValid && m_contentsValid
154         ? QAbstractTableModel::flags(index) : Qt::ItemFlags(0);
155 }
156
157 StackFrame StackHandler::currentFrame() const
158 {
159     QTC_ASSERT(m_currentIndex >= 0, return StackFrame());
160     QTC_ASSERT(m_currentIndex < m_stackFrames.size(), return StackFrame());
161     return m_stackFrames.at(m_currentIndex);
162 }
163
164 void StackHandler::setCurrentIndex(int level)
165 {
166     if (level == m_currentIndex)
167         return;
168
169     // Emit changed for previous frame
170     QModelIndex i = index(m_currentIndex, 0);
171     emit dataChanged(i, i);
172
173     m_currentIndex = level;
174
175     // Emit changed for new frame
176     i = index(m_currentIndex, 0);
177     emit dataChanged(i, i);
178 }
179
180 void StackHandler::removeAll()
181 {
182     m_stackFrames.clear();
183     m_currentIndex = 0;
184     reset();
185 }
186
187 void StackHandler::setFrames(const StackFrames &frames, bool canExpand)
188 {
189     m_resetLocationScheduled = false;
190     m_contentsValid = true;
191     m_canExpand = canExpand;
192     m_stackFrames = frames;
193     if (m_currentIndex >= m_stackFrames.size())
194         m_currentIndex = m_stackFrames.size() - 1;
195     reset();
196 }
197
198 const StackFrames &StackHandler::frames() const
199 {
200     return m_stackFrames;
201 }
202
203 void StackHandler::scheduleResetLocation()
204 {
205     m_resetLocationScheduled = true;
206     m_contentsValid = false;
207 }
208
209 void StackHandler::resetLocation()
210 {
211     if (m_resetLocationScheduled) {
212         m_resetLocationScheduled = false;
213         reset();
214     }
215 }
216
217 } // namespace Internal
218 } // namespace Debugger