source: branches/4.5.1/src/scripttools/debugging/qscriptdebuggerconsolecommandmanager.cpp@ 559

Last change on this file since 559 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtSCriptTools module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qscriptdebuggerconsolecommandmanager_p.h"
43#include "qscriptdebuggerconsolecommand_p.h"
44#include "qscriptdebuggerconsolecommandgroupdata_p.h"
45
46#include <QtCore/qlist.h>
47#include <QtCore/qstringlist.h>
48
49QT_BEGIN_NAMESPACE
50
51/*!
52 \since 4.5
53 \class QScriptDebuggerConsoleCommandManager
54 \internal
55
56 \brief The QScriptDebuggerConsoleCommandManager manages a collection of console commands.
57
58*/
59
60class QScriptDebuggerConsoleCommandManagerPrivate
61{
62 Q_DECLARE_PUBLIC(QScriptDebuggerConsoleCommandManager)
63public:
64 QScriptDebuggerConsoleCommandManagerPrivate();
65 ~QScriptDebuggerConsoleCommandManagerPrivate();
66
67 QList<QScriptDebuggerConsoleCommand*> commands;
68 QMap<QString, QScriptDebuggerConsoleCommandGroupData> groups;
69
70 QScriptDebuggerConsoleCommandManager *q_ptr;
71};
72
73QScriptDebuggerConsoleCommandManagerPrivate::QScriptDebuggerConsoleCommandManagerPrivate()
74{
75 groups[QLatin1String("breakpoints")] =
76 QScriptDebuggerConsoleCommandGroupData(QLatin1String("Making program stop at certain points"),
77 QLatin1String(""));
78 groups[QLatin1String("files")] =
79 QScriptDebuggerConsoleCommandGroupData(QLatin1String("Examining files"),
80 QLatin1String(""));
81 groups[QLatin1String("stack")] =
82 QScriptDebuggerConsoleCommandGroupData(QLatin1String("Examining the stack"),
83 QLatin1String(""));
84 groups[QLatin1String("running")] =
85 QScriptDebuggerConsoleCommandGroupData(QLatin1String("Running the program"),
86 QLatin1String(""));
87 groups[QLatin1String("status")] =
88 QScriptDebuggerConsoleCommandGroupData(QLatin1String("Status inquiries"),
89 QLatin1String(""));
90 groups[QLatin1String("void")] =
91 QScriptDebuggerConsoleCommandGroupData(QLatin1String("No such group"),
92 QLatin1String("It's a secret to everyone"));
93}
94
95QScriptDebuggerConsoleCommandManagerPrivate::~QScriptDebuggerConsoleCommandManagerPrivate()
96{
97 qDeleteAll(commands);
98}
99
100QScriptDebuggerConsoleCommandManager::QScriptDebuggerConsoleCommandManager()
101 : d_ptr(new QScriptDebuggerConsoleCommandManagerPrivate)
102{
103 d_ptr->q_ptr = this;
104}
105
106QScriptDebuggerConsoleCommandManager::~QScriptDebuggerConsoleCommandManager()
107{
108 delete d_ptr;
109}
110
111/*!
112 Adds the given \a command.
113 The manager takes ownership of the command.
114*/
115void QScriptDebuggerConsoleCommandManager::addCommand(QScriptDebuggerConsoleCommand *command)
116{
117 Q_D(QScriptDebuggerConsoleCommandManager);
118 Q_ASSERT(command != 0);
119 if (command->name().isEmpty()) {
120 qWarning("addCommand(): nameless command ignored");
121 return;
122 }
123 if (command->group().isEmpty()) {
124 qWarning("addCommand(): groupless command '%s' ignored",
125 qPrintable(command->name()));
126 return;
127 }
128 if (findCommand(command->name()) != 0) {
129 qWarning("addCommand(): duplicate command '%s' (group '%s') ignored",
130 qPrintable(command->name()), qPrintable(command->group()));
131 return;
132 }
133 if (!d->groups.contains(command->group())) {
134 qWarning("addCommand(): group '%s' for command '%s' is unknown!",
135 qPrintable(command->group()), qPrintable(command->name()));
136 }
137 d->commands.append(command);
138}
139
140/*!
141 Registers a command group with the given \a name and \a data.
142*/
143void QScriptDebuggerConsoleCommandManager::addCommandGroup(
144 const QString &name, const QScriptDebuggerConsoleCommandGroupData &data)
145{
146 Q_D(QScriptDebuggerConsoleCommandManager);
147 if (name.isEmpty()) {
148 qWarning("addCommandGroup(): nameless group ignored");
149 return;
150 }
151 if (d->groups.contains(name)) {
152 qWarning("addCommandGroup(): group '%s' already defined",
153 qPrintable(name));
154 return;
155 }
156 d->groups[name] = data;
157}
158
159/*!
160 Returns the command with the given \a name if one exists, otherwise
161 returns 0.
162*/
163QScriptDebuggerConsoleCommand *QScriptDebuggerConsoleCommandManager::findCommand(const QString &name) const
164{
165 Q_D(const QScriptDebuggerConsoleCommandManager);
166 for (int i = 0; i < d->commands.size(); ++i) {
167 QScriptDebuggerConsoleCommand *cmd = d->commands.at(i);
168 if (cmd->name() == name)
169 return cmd;
170 else if (cmd->aliases().contains(name))
171 return cmd;
172 }
173 return 0;
174}
175
176/*!
177 Returns the commands organized into groups.
178*/
179QMap<QString, QList<QScriptDebuggerConsoleCommand*> > QScriptDebuggerConsoleCommandManager::commands() const
180{
181 Q_D(const QScriptDebuggerConsoleCommandManager);
182 QMap<QString, QList<QScriptDebuggerConsoleCommand*> > result;
183 for (int i = 0; i < d->commands.size(); ++i) {
184 QScriptDebuggerConsoleCommand *cmd = d->commands.at(i);
185 result[cmd->group()].append(cmd);
186 }
187 return result;
188}
189
190/*!
191 Returns commands in the group of the given \a name.
192*/
193QScriptDebuggerConsoleCommandList QScriptDebuggerConsoleCommandManager::commandsInGroup(const QString &name) const
194{
195 Q_D(const QScriptDebuggerConsoleCommandManager);
196 QScriptDebuggerConsoleCommandList result;
197 for (int i = 0; i < d->commands.size(); ++i) {
198 QScriptDebuggerConsoleCommand *cmd = d->commands.at(i);
199 if (cmd->group() == name)
200 result.append(cmd);
201 }
202 return result;
203}
204
205/*!
206 Returns data associated with the group of the given \a name.
207*/
208QScriptDebuggerConsoleCommandGroupData QScriptDebuggerConsoleCommandManager::commandGroupData(const QString &name) const
209{
210 Q_D(const QScriptDebuggerConsoleCommandManager);
211 return d->groups.value(name);
212}
213
214/*!
215 Returns all command groups.
216*/
217QMap<QString, QScriptDebuggerConsoleCommandGroupData> QScriptDebuggerConsoleCommandManager::commandGroups() const
218{
219 Q_D(const QScriptDebuggerConsoleCommandManager);
220 return d->groups;
221}
222
223/*!
224 Returns the possible completions for the given \a prefix.
225*/
226QStringList QScriptDebuggerConsoleCommandManager::completions(const QString &prefix) const
227{
228 Q_D(const QScriptDebuggerConsoleCommandManager);
229 QStringList result;
230 for (int i = 0; i < d->commands.size(); ++i) {
231 QScriptDebuggerConsoleCommand *cmd = d->commands.at(i);
232 QStringList names;
233 names.append(cmd->name());
234// names += cmd->aliases();
235 for (int j = 0; j < names.size(); ++j) {
236 const QString &name = names.at(j);
237 if ((name.length() > prefix.length()) && name.startsWith(prefix))
238 result.append(name);
239 }
240 }
241 qStableSort(result);
242 return result;
243}
244
245QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.