KHtml

callstackdock.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2006 Matt Broadstone ([email protected])
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include "callstackdock.h"
21 
22 #include <QVBoxLayout>
23 #include <QTableWidget>
24 #include <QHeaderView>
25 
26 #include <klocalizedstring.h>
27 
28 #include "debugdocument.h"
29 #include "interpreter_ctx.h"
30 
31 namespace KJSDebugger
32 {
33 
34 CallStackDock::CallStackDock(QWidget *parent)
35  : QDockWidget(i18n("Call Stack"), parent)
36 {
37  setFeatures(DockWidgetMovable | DockWidgetFloatable);
38  m_view = new QTableWidget(0, 2);
39  m_view->setHorizontalHeaderLabels(QStringList() << i18n("Call") << i18n("Line"));
40  m_view->verticalHeader()->hide();
41  m_view->setShowGrid(false);
42  m_view->setAlternatingRowColors(true);
43  m_view->setSelectionBehavior(QAbstractItemView::SelectRows);
44  m_view->setSelectionMode(QAbstractItemView::SingleSelection);
45 
46  connect(m_view, SIGNAL(itemClicked(QTableWidgetItem*)),
47  this, SLOT(slotViewItem(QTableWidgetItem*)));
48  connect(m_view, SIGNAL(itemDoubleClicked(QTableWidgetItem*)),
49  this, SLOT(slotViewItem(QTableWidgetItem*)));
50 
51  setWidget(m_view);
52 
53  m_activeCtx = 0;
54 }
55 
56 CallStackDock::~CallStackDock()
57 {
58 }
59 
60 void CallStackDock::clearDisplay()
61 {
62  m_activeCtx = 0;
63  m_view->clearContents();
64  m_view->setRowCount(0);
65 }
66 
67 void CallStackDock::displayStack(InterpreterContext *ic)
68 {
69  m_activeCtx = ic;
70 
71  // Try to save our position across updates if nothing changed.
72  // this is needed for console eval.
73  bool dirty = false;
74  int activeRow = m_view->currentRow();
75 
76  if (ic->callStack.count() != m_view->rowCount()) {
77  m_view->setRowCount(ic->callStack.count());
78  dirty = true;
79  }
80 
81  for (int row = 0; row < ic->callStack.size(); ++row) {
82  const CallStackEntry &entry = ic->callStack[row];
83 
84  int displayRow = ic->callStack.count() - row - 1; //Want newest entry on top
85  QString fnLabel = entry.name;
86  QString lnLabel = QString::number(entry.lineNumber + 1);
87 
88  dirty = dirty /* avoids latter stuff if may be null */
89  || m_view->item(displayRow, 0)->text() != fnLabel
90  || m_view->item(displayRow, 1)->text() != lnLabel;
91 
92  QTableWidgetItem *function = new QTableWidgetItem(fnLabel);
93  function->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
94  m_view->setItem(displayRow, 0, function);
95  QTableWidgetItem *lineNumber = new QTableWidgetItem(lnLabel);
97  m_view->setItem(displayRow, 1, lineNumber);
98  }
99 
100  m_view->resizeColumnsToContents();
101  m_view->resizeRowsToContents();
102 
103  if (!dirty && activeRow != -1) {
104  m_view->setCurrentCell(activeRow, 0);
105  slotViewItem(m_view->item(activeRow, 0));
106  }
107 }
108 
109 void CallStackDock::slotViewItem(QTableWidgetItem *item)
110 {
111  if (!m_activeCtx) {
112  return;
113  }
114 
115  CallStackEntry &entry = m_activeCtx->callStack[m_view->rowCount() - m_view->row(item) - 1];
116  emit displayScript(entry.doc.get(), entry.lineNumber);
117 }
118 
119 KJS::ExecState *CallStackDock::selectedFrameContext()
120 {
121  QList<QTableWidgetItem *> selected = m_view->selectedItems();
122  if (selected.isEmpty()) {
123  return 0;
124  }
125 
126  return m_activeCtx->execContexts[m_view->rowCount() - m_view->row(selected[0]) - 1];
127 
128 }
129 
130 }
QString number(int n, int base)
bool isEmpty() const const
QString i18n(const char *text, const TYPE &arg...)
void setFlags(Qt::ItemFlags flags)
ItemIsEnabled
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.