1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "stackhandler.h"
36 #include "debuggeractions.h"
37 #include "debuggercore.h"
38 #include "debuggerengine.h"
40 #include <utils/qtcassert.h>
41 #include <utils/savedaction.h>
43 #include <QtCore/QDebug>
44 #include <QtCore/QFileInfo>
49 ////////////////////////////////////////////////////////////////////////
53 ////////////////////////////////////////////////////////////////////////
55 StackHandler::StackHandler()
56 : m_positionIcon(QIcon(QLatin1String(":/debugger/images/location_16.png"))),
57 m_emptyIcon(QIcon(QLatin1String(":/debugger/images/debugger_empty_14.png")))
59 m_resetLocationScheduled = false;
60 m_contentsValid = false;
63 connect(debuggerCore()->action(OperateByInstruction), SIGNAL(triggered()),
64 this, SLOT(resetModel()));
67 StackHandler::~StackHandler()
71 int StackHandler::rowCount(const QModelIndex &parent) const
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);
77 int StackHandler::columnCount(const QModelIndex &parent) const
79 return parent.isValid() ? 0 : 5;
82 QVariant StackHandler::data(const QModelIndex &index, int role) const
84 if (!index.isValid() || index.row() >= m_stackFrames.size() + m_canExpand)
87 if (index.row() == m_stackFrames.size()) {
88 if (role == Qt::DisplayRole && index.column() == 0)
90 if (role == Qt::DisplayRole && index.column() == 1)
92 if (role == Qt::DecorationRole && index.column() == 0)
97 const StackFrame &frame = m_stackFrames.at(index.row());
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;
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();
111 return QString::fromAscii("0x%1").arg(frame.address, 0, 16);
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;
123 if (role == Qt::ToolTipRole)
124 return frame.toToolTip();
130 QVariant StackHandler::headerData(int section, Qt::Orientation orient, int role) const
132 if (orient == Qt::Horizontal && role == Qt::DisplayRole) {
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");
144 Qt::ItemFlags StackHandler::flags(const QModelIndex &index) const
146 if (index.row() >= m_stackFrames.size() + m_canExpand)
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);
157 StackFrame StackHandler::currentFrame() const
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);
164 void StackHandler::setCurrentIndex(int level)
166 if (level == m_currentIndex)
169 // Emit changed for previous frame
170 QModelIndex i = index(m_currentIndex, 0);
171 emit dataChanged(i, i);
173 m_currentIndex = level;
175 // Emit changed for new frame
176 i = index(m_currentIndex, 0);
177 emit dataChanged(i, i);
180 void StackHandler::removeAll()
182 m_stackFrames.clear();
187 void StackHandler::setFrames(const StackFrames &frames, bool canExpand)
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;
198 const StackFrames &StackHandler::frames() const
200 return m_stackFrames;
203 void StackHandler::scheduleResetLocation()
205 m_resetLocationScheduled = true;
206 m_contentsValid = false;
209 void StackHandler::resetLocation()
211 if (m_resetLocationScheduled) {
212 m_resetLocationScheduled = false;
217 } // namespace Internal
218 } // namespace Debugger