1/****************************************************************************
2**
3** Copyright (C) 2018 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtSCriptTools module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** 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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qscriptdebuggerscriptswidget_p.h"
41#include "qscriptdebuggerscriptswidgetinterface_p_p.h"
42#include "qscriptdebuggerscriptsmodel_p.h"
43
44#include <QtCore/qdebug.h>
45#include <QtWidgets/qheaderview.h>
46#include <QtWidgets/qtreeview.h>
47#include <QtWidgets/qboxlayout.h>
48
49QT_BEGIN_NAMESPACE
50
51class QScriptDebuggerScriptsWidgetPrivate
52 : public QScriptDebuggerScriptsWidgetInterfacePrivate
53{
54 Q_DECLARE_PUBLIC(QScriptDebuggerScriptsWidget)
55public:
56 QScriptDebuggerScriptsWidgetPrivate();
57 ~QScriptDebuggerScriptsWidgetPrivate();
58
59 // private slots
60 void _q_onCurrentChanged(const QModelIndex &index);
61
62 QTreeView *view;
63 qint64 currentScriptId;
64};
65
66QScriptDebuggerScriptsWidgetPrivate::QScriptDebuggerScriptsWidgetPrivate()
67{
68 currentScriptId = -1;
69}
70
71QScriptDebuggerScriptsWidgetPrivate::~QScriptDebuggerScriptsWidgetPrivate()
72{
73}
74
75void QScriptDebuggerScriptsWidgetPrivate::_q_onCurrentChanged(const QModelIndex &index)
76{
77 Q_Q(QScriptDebuggerScriptsWidget);
78 if (!index.isValid())
79 return;
80 qint64 sid = q->scriptsModel()->scriptIdFromIndex(index);
81 if (sid != -1) {
82 if (currentScriptId != sid) {
83 currentScriptId = sid;
84 emit q->currentScriptChanged(scriptId: sid);
85 }
86 } else {
87 qint64 sid = q->scriptsModel()->scriptIdFromIndex(index: index.parent());
88 Q_ASSERT(sid != -1);
89 currentScriptId = sid;
90 emit q->currentScriptChanged(scriptId: sid);
91 QPair<QString, int> info = q->scriptsModel()->scriptFunctionInfoFromIndex(index);
92 emit q->scriptLocationSelected(lineNumber: info.second);
93 }
94}
95
96QScriptDebuggerScriptsWidget::QScriptDebuggerScriptsWidget(QWidget *parent)
97 : QScriptDebuggerScriptsWidgetInterface(*new QScriptDebuggerScriptsWidgetPrivate, parent, {})
98{
99 Q_D(QScriptDebuggerScriptsWidget);
100 d->view = new QTreeView();
101 d->view->setEditTriggers(QAbstractItemView::NoEditTriggers);
102// d->view->setAlternatingRowColors(true);
103// d->view->setRootIsDecorated(false);
104 d->view->setSelectionBehavior(QAbstractItemView::SelectRows);
105 d->view->header()->hide();
106// d->view->header()->setDefaultAlignment(Qt::AlignLeft);
107// d->view->header()->setResizeMode(QHeaderView::ResizeToContents);
108
109 QVBoxLayout *vbox = new QVBoxLayout(this);
110 vbox->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
111 vbox->addWidget(d->view);
112}
113
114QScriptDebuggerScriptsWidget::~QScriptDebuggerScriptsWidget()
115{
116}
117
118/*!
119 \reimp
120*/
121QScriptDebuggerScriptsModel *QScriptDebuggerScriptsWidget::scriptsModel() const
122{
123 Q_D(const QScriptDebuggerScriptsWidget);
124 return qobject_cast<QScriptDebuggerScriptsModel*>(object: d->view->model());
125}
126
127/*!
128 \reimp
129*/
130void QScriptDebuggerScriptsWidget::setScriptsModel(QScriptDebuggerScriptsModel *model)
131{
132 Q_D(QScriptDebuggerScriptsWidget);
133 d->view->setModel(model);
134 QObject::connect(sender: d->view->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
135 receiver: this, SLOT(_q_onCurrentChanged(QModelIndex)));
136}
137
138qint64 QScriptDebuggerScriptsWidget::currentScriptId() const
139{
140 Q_D(const QScriptDebuggerScriptsWidget);
141 return scriptsModel()->scriptIdFromIndex(index: d->view->currentIndex());
142}
143
144void QScriptDebuggerScriptsWidget::setCurrentScript(qint64 id)
145{
146 Q_D(QScriptDebuggerScriptsWidget);
147 d->view->setCurrentIndex(scriptsModel()->indexFromScriptId(id));
148}
149
150QT_END_NAMESPACE
151
152#include "moc_qscriptdebuggerscriptswidget_p.cpp"
153

source code of qtscript/src/scripttools/debugging/qscriptdebuggerscriptswidget.cpp