KHtml

scriptsdock.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2006 Matt Broadstone ([email protected])
4  * Copyright (C) 2008 Maks Orlovich ([email protected])
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "scriptsdock.h"
22 
23 #include <QVBoxLayout>
24 #include <QTreeWidget>
25 #include <QTreeWidgetItem>
26 #include <QStandardItemModel>
27 #include <QHeaderView>
28 #include <QUrl>
29 
30 #include "debugwindow.h"
31 #include "debugdocument.h"
32 
33 using namespace KJS;
34 using namespace KJSDebugger;
35 
36 ScriptsDock::ScriptsDock(QWidget *parent)
37  : QDockWidget(i18n("Loaded Scripts"), parent)
38 {
39  setFeatures(DockWidgetMovable | DockWidgetFloatable);
40  m_widget = new QTreeWidget;
41  m_widget->header()->hide();
42 
43  connect(m_widget, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
44  this, SLOT(scriptSelected(QTreeWidgetItem*,int)));
45 
46  setWidget(m_widget);
47 }
48 
49 ScriptsDock::~ScriptsDock()
50 {
51 }
52 
53 void ScriptsDock::documentDestroyed(DebugDocument *document)
54 {
55  // We may be asked to remove a document held for reload repeatedly;
56  // ignore later attempts
57  if (!m_documents.contains(document)) {
58  return;
59  }
60 
61  QTreeWidgetItem *fragment = m_documents[document];
62  m_documents.remove(document);
63 
64  QTreeWidgetItem *file = fragment->parent();
65  QTreeWidgetItem *domain = file->parent();
66 
67  delete file->takeChild(file->indexOfChild(fragment));
68 
69  if (file->childCount()) {
70  return;
71  }
72 
73  // Need to clean up the file as well.
74  delete domain->takeChild(domain->indexOfChild(file));
75 
76  if (domain->childCount()) {
77  return;
78  }
79 
80  // ... and domain
81  m_headers.remove(domain->text(0));
82  delete m_widget->takeTopLevelItem(m_widget->indexOfTopLevelItem(domain));
83 }
84 
85 void ScriptsDock::addDocument(DebugDocument *document)
86 {
87  assert(!m_documents.contains(document));
88 
89  QString name = document->name();
90  QString domain;
91  QString favicon;
92 
93  if (document->url().isEmpty()) {
94  domain = "????"; // TODO: proper i18n'able string
95  }
96 
97  else {
98  QUrl kurl(document->url());
99  if (!kurl.host().isEmpty()) {
100  domain = kurl.host();
101  } else {
102  domain = "localhost";
103  }
104 
105  favicon = KIO::favIconForUrl(kurl);
106  }
107 
108  QTreeWidgetItem *parent = 0;
109  if (m_headers.contains(domain)) {
110  parent = m_headers[domain];
111  } else {
112  parent = new QTreeWidgetItem(QStringList() << domain);
113  if (!favicon.isEmpty()) {
114  QIcon icon = QIcon::fromTheme(favicon);
115  parent->setIcon(0, icon);
116  }
117 
118  m_headers[domain] = parent;
119  m_widget->invisibleRootItem()->addChild(parent);
120  }
121 
122  // Now try to find a kid for the name
123  QTreeWidgetItem *file = 0;
124  for (int c = 0; c < parent->childCount(); ++c) {
125  QTreeWidgetItem *cand = parent->child(c);
126  if (cand->text(0) == name) {
127  file = cand;
128  }
129  }
130 
131  if (!file) {
132  file = new QTreeWidgetItem(parent, QStringList() << name);
133  }
134 
135  // Now add the fragment
136  QString lines = QString::number(1 + document->baseLine()) + "-" +
137  QString::number(1 + document->baseLine() + document->length() - 1);
138 
139  QTreeWidgetItem *child = new QTreeWidgetItem(file, QStringList() << lines);
140  m_documents[document] = child;
141 }
142 
143 void ScriptsDock::scriptSelected(QTreeWidgetItem *item, int /*column*/)
144 {
145  DebugDocument *doc = m_documents.key(item);
146  if (doc) {
147  emit displayScript(doc);
148  }
149 }
150 
QString name(const QVariant &location)
QTreeWidgetItem * child(int index) const const
void setIcon(int column, const QIcon &icon)
QString number(int n, int base)
bool isEmpty() const const
KIOCORE_EXPORT QString favIconForUrl(const QUrl &url)
void hide()
void addChild(QTreeWidgetItem *child)
QTreeWidgetItem * parent() const const
int indexOfChild(QTreeWidgetItem *child) const const
QString i18n(const char *text, const TYPE &arg...)
QIcon fromTheme(const QString &name)
QHeaderView * header() const const
int childCount() const const
QString text(int column) const const
QTreeWidgetItem * takeChild(int index)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.