snapshothandler.cpp 7.59 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 7 8
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15 16 17 18 19 20 21 22 23 24
**
** GNU Lesser General Public License Usage
**
** 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.
**
con's avatar
con committed
25 26 27 28 29 30
** 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
31 32 33 34 35
**
**************************************************************************/

#include "snapshothandler.h"

36
#include "debuggerconstants.h"
37
#include "debuggercore.h"
38
#include "debuggerengine.h"
39
#include "debuggerrunner.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
40
#include "debuggerstartparameters.h"
41 42 43 44

#include <utils/qtcassert.h>

#include <QtCore/QDebug>
45 46
#include <QtCore/QFile>
#include <QtGui/QIcon>
47 48 49 50

namespace Debugger {
namespace Internal {

51
#if 0
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
SnapshotData::SnapshotData()
{}

void SnapshotData::clear()
{
    m_frames.clear();
    m_location.clear();
    m_date = QDateTime();
}

QString SnapshotData::function() const
{
    if (m_frames.isEmpty())
        return QString();
    const StackFrame &frame = m_frames.at(0);
    return frame.function + ":" + QString::number(frame.line);
}

QString SnapshotData::toString() const
{
    QString res;
    QTextStream str(&res);
74
/*    str << SnapshotHandler::tr("Function:") << ' ' << function() << ' '
75
        << SnapshotHandler::tr("File:") << ' ' << m_location << ' '
76
        << SnapshotHandler::tr("Date:") << ' ' << m_date.toString(); */
77 78 79 80 81 82 83 84
    return res;
}

QString SnapshotData::toToolTip() const
{
    QString res;
    QTextStream str(&res);
    str << "<html><body><table>"
85
/*
86 87 88 89
        << "<tr><td>" << SnapshotHandler::tr("Function:")
            << "</td><td>" << function() << "</td></tr>"
        << "<tr><td>" << SnapshotHandler::tr("File:")
            << "</td><td>" << QDir::toNativeSeparators(m_location) << "</td></tr>"
90
        << "</table></body></html>"; */
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    return res;
}

QDebug operator<<(QDebug d, const  SnapshotData &f)
{
    QString res;
    QTextStream str(&res);
    str << f.location();
/*
    str << "level=" << f.level << " address=" << f.address;
    if (!f.function.isEmpty())
        str << ' ' << f.function;
    if (!f.location.isEmpty())
        str << ' ' << f.location << ':' << f.line;
    if (!f.from.isEmpty())
        str << " from=" << f.from;
    if (!f.to.isEmpty())
        str << " to=" << f.to;
*/
    d.nospace() << res;
    return d;
}
113
#endif
114 115 116 117 118 119 120

////////////////////////////////////////////////////////////////////////
//
// SnapshotHandler
//
////////////////////////////////////////////////////////////////////////

121
SnapshotHandler::SnapshotHandler()
122 123
  : m_positionIcon(QIcon(QLatin1String(":/debugger/images/location_16.png"))),
    m_emptyIcon(QIcon(QLatin1String(":/debugger/images/debugger_empty_14.png")))
124
{
125
    m_currentIndex = -1;
126 127 128 129
}

SnapshotHandler::~SnapshotHandler()
{
130
    for (int i = m_snapshots.size(); --i >= 0; ) {
131 132
        if (DebuggerEngine *engine = at(i)) {
            const DebuggerStartParameters &sp = engine->startParameters();
133 134
            if (sp.isSnapshot && !sp.coreFile.isEmpty())
                QFile::remove(sp.coreFile);
135
        }
136 137 138
    }
}

139 140 141 142 143 144 145 146
int SnapshotHandler::rowCount(const QModelIndex &parent) const
{
    // Since the stack is not a tree, row count is 0 for any valid parent
    return parent.isValid() ? 0 : m_snapshots.size();
}

int SnapshotHandler::columnCount(const QModelIndex &parent) const
{
147
    return parent.isValid() ? 0 : 2;
148 149 150 151 152 153 154
}

QVariant SnapshotHandler::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= m_snapshots.size())
        return QVariant();

155
    const DebuggerEngine *engine = at(index.row());
156 157 158 159 160 161 162

    if (role == SnapshotCapabilityRole)
        return engine && (engine->debuggerCapabilities() & SnapshotCapability);

    if (!engine)
        return QLatin1String("<finished>");

163
    const DebuggerStartParameters &sp = engine->startParameters();
164

165 166
    switch (role) {
    case Qt::DisplayRole:
167
        switch (index.column()) {
168 169 170 171
        case 0:
            return sp.displayName;
        case 1:
            return sp.coreFile.isEmpty() ? sp.executable : sp.coreFile;
172 173 174
        }
        return QVariant();

175 176
    case Qt::ToolTipRole:
        return QVariant();
177

178 179
    case Qt::DecorationRole:
        // Return icon that indicates whether this is the active stack frame.
180 181 182
        if (index.column() == 0)
            return (index.row() == m_currentIndex) ? m_positionIcon : m_emptyIcon;
        break;
183

184 185 186
    default:
        break;
    }
187 188 189 190 191 192 193
    return QVariant();
}

QVariant SnapshotHandler::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
194 195
            case 0: return tr("Name");
            case 1: return tr("File");
196 197 198 199 200 201 202 203 204 205 206 207 208 209
        };
    }
    return QVariant();
}

Qt::ItemFlags SnapshotHandler::flags(const QModelIndex &index) const
{
    if (index.row() >= m_snapshots.size())
        return 0;
    if (index.row() == m_snapshots.size())
        return QAbstractTableModel::flags(index);
    return true ? QAbstractTableModel::flags(index) : Qt::ItemFlags(0);
}

210
void SnapshotHandler::activateSnapshot(int index)
211
{
212 213
    m_currentIndex = index;
    //qDebug() << "ACTIVATING INDEX: " << m_currentIndex << " OF " << size();
214
    debuggerCore()->displayDebugger(at(index), true);
215
    reset();
216 217
}

218 219
void SnapshotHandler::createSnapshot(int index)
{
220
    DebuggerEngine *engine = at(index);
221 222 223 224 225 226
    QTC_ASSERT(engine, return);
    engine->createSnapshot();
}

void SnapshotHandler::removeSnapshot(int index)
{
227
    DebuggerEngine *engine = at(index);
228 229
    //qDebug() << "REMOVING " << engine;
    QTC_ASSERT(engine, return);
230 231 232 233 234
#if 0
    // See http://sourceware.org/bugzilla/show_bug.cgi?id=11241.
    setState(EngineSetupRequested);
    postCommand("set stack-cache off");
#endif
235
    //QString fileName = engine->startParameters().coreFile;
236 237 238 239 240 241 242
    //if (!fileName.isEmpty())
    //    QFile::remove(fileName);
    m_snapshots.removeAt(index);
    if (index == m_currentIndex)
        m_currentIndex = -1;
    else if (index < m_currentIndex)
        --m_currentIndex;
243
    //engine->quitDebugger();
244 245 246
    reset();
}

247

248 249 250
void SnapshotHandler::removeAll()
{
    m_snapshots.clear();
251
    m_currentIndex = -1;
252 253 254
    reset();
}

255
void SnapshotHandler::appendSnapshot(DebuggerEngine *engine)
256
{
257
    m_snapshots.append(engine);
258
    m_currentIndex = size() - 1;
259 260 261
    reset();
}

262
void SnapshotHandler::removeSnapshot(DebuggerEngine *engine)
263
{
264
    // Could be that the run controls died before it was appended.
265
    int index = m_snapshots.indexOf(engine);
266 267
    if (index != -1)
        removeSnapshot(index);
268 269
}

270
void SnapshotHandler::setCurrentIndex(int index)
271 272 273 274 275
{
    m_currentIndex = index;
    reset();
}

276
DebuggerEngine *SnapshotHandler::at(int i) const
277 278 279 280
{
    return m_snapshots.at(i).data();
}

281 282
} // namespace Internal
} // namespace Debugger