snapshothandler.cpp 8.77 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 "sessionengine.h"
37
#include "debuggeractions.h"
38
#include "debuggerconstants.h"
39
#include "debuggerengine.h"
40 41
#include "debuggerrunner.h"
#include "debuggerplugin.h"
42 43

#include <utils/qtcassert.h>
44
#include <utils/savedaction.h>
45 46 47 48 49 50 51 52 53

#include <QtCore/QAbstractTableModel>
#include <QtCore/QDebug>
#include <QtCore/QFileInfo>
#include <QtCore/QDir>

namespace Debugger {
namespace Internal {

54
#if 0
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
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);
77
/*    str << SnapshotHandler::tr("Function:") << ' ' << function() << ' '
78
        << SnapshotHandler::tr("File:") << ' ' << m_location << ' '
79
        << SnapshotHandler::tr("Date:") << ' ' << m_date.toString(); */
80 81 82 83 84 85 86 87
    return res;
}

QString SnapshotData::toToolTip() const
{
    QString res;
    QTextStream str(&res);
    str << "<html><body><table>"
88
/*
89 90 91 92
        << "<tr><td>" << SnapshotHandler::tr("Function:")
            << "</td><td>" << function() << "</td></tr>"
        << "<tr><td>" << SnapshotHandler::tr("File:")
            << "</td><td>" << QDir::toNativeSeparators(m_location) << "</td></tr>"
93
        << "</table></body></html>"; */
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    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;
}
116
#endif
117 118 119 120 121 122 123

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

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

SnapshotHandler::~SnapshotHandler()
{
133
    for (int i = m_snapshots.size(); --i >= 0; ) {
134
        if (DebuggerEngine *engine = engineAt(i)) {
135 136 137
            const DebuggerStartParameters & sp = engine->startParameters();
            if (sp.isSnapshot && !sp.coreFile.isEmpty())
                QFile::remove(sp.coreFile);
138
        }
139 140 141 142 143
    }
}

DebuggerEngine *SnapshotHandler::engineAt(int i) const
{
144 145 146
    DebuggerEngine *engine = m_snapshots.at(i)->engine();
    QTC_ASSERT(engine, qDebug() << "ENGINE AT " << i << "DELETED");
    return engine;
147 148 149 150 151 152 153 154 155 156
}

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
{
157
    return parent.isValid() ? 0 : 2;
158 159 160 161 162 163 164
}

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

165
    const DebuggerEngine *engine = engineAt(index.row());
166 167 168 169 170 171 172

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

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

173
    const DebuggerStartParameters &sp = engine->startParameters();
174

175 176
    switch (role) {
    case Qt::DisplayRole:
177
        switch (index.column()) {
178 179 180 181
        case 0:
            return sp.displayName;
        case 1:
            return sp.coreFile.isEmpty() ? sp.executable : sp.coreFile;
182 183 184
        }
        return QVariant();

185 186
    case Qt::ToolTipRole:
        return QVariant();
187

188 189 190 191
    case Qt::DecorationRole: // Return icon that indicates whether this is the active stack frame
        if (index.column() == 0)
            return (index.row() == m_currentIndex) ? m_positionIcon : m_emptyIcon;
        break;
192

193 194 195
    default:
        break;
    }
196 197 198 199 200 201 202
    return QVariant();
}

QVariant SnapshotHandler::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
203 204
            case 0: return tr("Name");
            case 1: return tr("File");
205 206 207 208 209 210 211 212 213 214 215 216 217 218
        };
    }
    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);
}

219 220 221
bool SnapshotHandler::setData
    (const QModelIndex &index, const QVariant &value, int role)
{
222
    Q_UNUSED(value);
223
    if (index.isValid() && role == RequestCreateSnapshotRole) {
224 225
        DebuggerEngine *engine = engineAt(index.row());
        QTC_ASSERT(engine, return false);
226
        engine->createSnapshot();
227 228
        return true;
    }
229 230
    if (index.isValid() && role == RequestActivateSnapshotRole) {
        m_currentIndex = index.row();
231
        //qDebug() << "ACTIVATING INDEX: " << m_currentIndex << " OF " << size();
232 233
        DebuggerPlugin::displayDebugger(m_snapshots.at(m_currentIndex));
        reset();
234 235
        return true;
    }
236 237 238 239 240 241 242
    if (index.isValid() && role == RequestRemoveSnapshotRole) {
        DebuggerEngine *engine = engineAt(index.row());
        //qDebug() << "REMOVING " << engine;
        QTC_ASSERT(engine, return false);
        engine->quitDebugger();
        return true;
    }
243
    return false;
244 245
}

246 247 248 249 250 251
#if 0
    // See http://sourceware.org/bugzilla/show_bug.cgi?id=11241.
    setState(EngineSetupRequested);
    postCommand("set stack-cache off");
#endif

252 253 254
void SnapshotHandler::removeAll()
{
    m_snapshots.clear();
255
    m_currentIndex = -1;
256 257 258
    reset();
}

259
void SnapshotHandler::appendSnapshot(DebuggerRunControl *rc)
260
{
261 262
    m_snapshots.append(rc);
    m_currentIndex = size() - 1;
263 264 265
    reset();
}

266 267
void SnapshotHandler::removeSnapshot(DebuggerRunControl *rc)
{
268
    // Could be that the run controls died before it was appended.
269
    int index = m_snapshots.indexOf(rc);
270 271
    if (index != -1)
        removeSnapshot(index);
272 273
}

274 275
void SnapshotHandler::removeSnapshot(int index)
{
276 277 278
    const DebuggerEngine *engine = engineAt(index);
    QTC_ASSERT(engine, return);
    QString fileName = engine->startParameters().coreFile;
279 280
    //if (!fileName.isEmpty())
    //    QFile::remove(fileName);
281 282 283 284 285 286 287 288
    m_snapshots.removeAt(index);
    if (index == m_currentIndex)
        m_currentIndex = -1;
    else if (index < m_currentIndex)
        --m_currentIndex;
    reset();
}

289
void SnapshotHandler::setCurrentIndex(int index)
290 291 292 293 294
{
    m_currentIndex = index;
    reset();
}

295
DebuggerRunControl *SnapshotHandler::at(int i) const
296 297 298 299
{
    return m_snapshots.at(i).data();
}

300 301 302 303 304 305 306 307 308 309 310 311
QList<DebuggerRunControl*> SnapshotHandler::runControls() const
{
    // Return unique list of run controls
    QList<DebuggerRunControl*> rc;
    rc.reserve(m_snapshots.size());
    foreach(const QPointer<DebuggerRunControl> &runControlPtr, m_snapshots)
        if (DebuggerRunControl *runControl = runControlPtr)
            if (!rc.contains(runControl))
                rc.push_back(runControl);
    return rc;
}

312 313
} // namespace Internal
} // namespace Debugger