1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
/*
* Debugger Console View
*
* Copyright 2003 John Birch <jbb@kdevelop.org>
* Copyright 2006 Vladimir Prus <ghost@cs.msu.su>
* Copyright 2007 Hamish Rodda <rodda@kde.org>
* Copyright 2016 Aetf <aetf@unlimitedcodeworks.xyz>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "debuggerconsoleview.h"
#include "debuglog.h"
#include "midebuggerplugin.h"
#include "midebugsession.h"
#include <interfaces/icore.h>
#include <interfaces/idebugcontroller.h>
#include <KColorScheme>
#include <KHistoryComboBox>
#include <KLocalizedString>
#include <QAction>
#include <QEvent>
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QMenu>
#include <QScopedPointer>
#include <QScrollBar>
#include <QStyle>
#include <QTextEdit>
#include <QToolBar>
#include <QVBoxLayout>
#include <QPoint>
using namespace KDevMI;
DebuggerConsoleView::DebuggerConsoleView(MIDebuggerPlugin *plugin, QWidget *parent)
: QWidget(parent)
, m_repeatLastCommand(false)
, m_showInternalCommands(false)
, m_cmdEditorHadFocus(false)
, m_maxLines(5000)
{
setWindowIcon(QIcon::fromTheme(QStringLiteral("dialog-scripts")));
setWindowTitle(i18nc("@title:window", "Debugger Console"));
setWhatsThis(i18nc("@info:whatsthis",
"<b>Debugger Console</b><p>"
"Shows all debugger commands being executed. "
"You can also issue any other debugger command while debugging.</p>"));
setupUi();
m_actRepeat = new QAction(QIcon::fromTheme(QStringLiteral("edit-redo")),
QString(),
this);
m_actRepeat->setToolTip(i18nc("@info:tooltip", "Repeat last command when hit Return"));
m_actRepeat->setCheckable(true);
m_actRepeat->setChecked(m_repeatLastCommand);
connect(m_actRepeat, &QAction::toggled, this, &DebuggerConsoleView::toggleRepeat);
m_toolBar->insertAction(m_actCmdEditor, m_actRepeat);
m_actInterrupt = new QAction(QIcon::fromTheme(QStringLiteral("media-playback-pause")),
QString(),
this);
m_actInterrupt->setToolTip(i18nc("@info:tooltip", "Pause execution of the app to enter debugger commands"));
connect(m_actInterrupt, &QAction::triggered, this, &DebuggerConsoleView::interruptDebugger);
m_toolBar->insertAction(m_actCmdEditor, m_actInterrupt);
setShowInterrupt(true);
m_actShowInternal = new QAction(i18nc("@action", "Show Internal Commands"), this);
m_actShowInternal->setCheckable(true);
m_actShowInternal->setChecked(m_showInternalCommands);
m_actShowInternal->setWhatsThis(i18nc("@info:whatsthis",
"Controls if commands issued internally by KDevelop "
"will be shown or not.<br>"
"This option will affect only future commands, it will not "
"add or remove already issued commands from the view."));
connect(m_actShowInternal, &QAction::toggled,
this, &DebuggerConsoleView::toggleShowInternalCommands);
handleDebuggerStateChange(s_none, s_dbgNotStarted);
m_updateTimer.setSingleShot(true);
m_updateTimer.setInterval(100);
connect(&m_updateTimer, &QTimer::timeout, this, &DebuggerConsoleView::flushPending);
connect(plugin->core()->debugController(), &KDevelop::IDebugController::currentSessionChanged,
this, &DebuggerConsoleView::handleSessionChanged);
connect(plugin, &MIDebuggerPlugin::reset, this, &DebuggerConsoleView::clear);
connect(plugin, &MIDebuggerPlugin::raiseDebuggerConsoleViews,
this, &DebuggerConsoleView::requestRaise);
handleSessionChanged(plugin->core()->debugController()->currentSession());
updateColors();
}
void DebuggerConsoleView::changeEvent(QEvent *event)
{
if (event->type() == QEvent::PaletteChange) {
updateColors();
}
}
void DebuggerConsoleView::updateColors()
{
KColorScheme scheme(QPalette::Active);
m_stdColor = scheme.foreground(KColorScheme::LinkText).color();
m_errorColor = scheme.foreground(KColorScheme::NegativeText).color();
}
void DebuggerConsoleView::setupUi()
{
setupToolBar();
m_textView = new QTextEdit;
m_textView->setReadOnly(true);
m_textView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_textView, &QTextEdit::customContextMenuRequested,
this, &DebuggerConsoleView::showContextMenu);
auto vbox = new QVBoxLayout;
vbox->setContentsMargins(0, 0, 0, 0);
vbox->addWidget(m_textView);
vbox->addWidget(m_toolBar);
setLayout(vbox);
m_cmdEditor = new KHistoryComboBox(this);
m_cmdEditor->setDuplicatesEnabled(false);
connect(m_cmdEditor, QOverload<const QString&>::of(&KHistoryComboBox::returnPressed),
this, &DebuggerConsoleView::trySendCommand);
auto label = new QLabel(i18nc("@label:listbox", "&Command:"), this);
label->setBuddy(m_cmdEditor);
auto hbox = new QHBoxLayout;
hbox->addWidget(label);
hbox->addWidget(m_cmdEditor);
hbox->setStretchFactor(m_cmdEditor, 1);
hbox->setContentsMargins(0, 0, 0, 0);
auto cmdEditor = new QWidget(this);
cmdEditor->setLayout(hbox);
m_actCmdEditor = m_toolBar->addWidget(cmdEditor);
}
void DebuggerConsoleView::setupToolBar()
{
m_toolBar = new QToolBar(this);
int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
m_toolBar->setIconSize(QSize(iconSize, iconSize));
m_toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
m_toolBar->setFloatable(false);
m_toolBar->setMovable(false);
m_toolBar->setWindowTitle(i18nc("@title:window", "%1 Command Bar", windowTitle()));
m_toolBar->setContextMenuPolicy(Qt::PreventContextMenu);
// remove margins, to make command editor nicely aligned with the output
m_toolBar->layout()->setContentsMargins(0, 0, 0, 0);
}
void DebuggerConsoleView::focusInEvent(QFocusEvent*)
{
m_textView->verticalScrollBar()->setValue(m_textView->verticalScrollBar()->maximum());
m_cmdEditor->setFocus();
}
DebuggerConsoleView::~DebuggerConsoleView()
{
}
void DebuggerConsoleView::setShowInterrupt(bool enable)
{
m_actInterrupt->setVisible(enable);
}
void DebuggerConsoleView::setReplacePrompt(const QString& prompt)
{
m_alterPrompt = prompt;
}
void DebuggerConsoleView::setShowInternalCommands(bool enable)
{
if (enable != m_showInternalCommands)
{
m_showInternalCommands = enable;
// Set of strings to show changes, text edit still has old
// set. Refresh.
m_textView->clear();
QStringList& newList = m_showInternalCommands ? m_allOutput : m_userOutput;
for (const auto &line : newList) {
// Note that color formatting is already applied to 'line'.
appendLine(line);
}
}
}
void DebuggerConsoleView::showContextMenu(const QPoint &pos)
{
// FIXME: QTextEdit::createStandardContextMenu takes position in document coordinates
// while pos is in QTextEdit::viewport coordinates.
// Seems not a big issue currently as menu content seems position independent, but still better fix
QScopedPointer<QMenu> popup(m_textView->createStandardContextMenu(pos));
popup->addSeparator();
popup->addAction(m_actShowInternal);
popup->exec(m_textView->viewport()->mapToGlobal(pos));
}
void DebuggerConsoleView::toggleRepeat(bool checked)
{
m_repeatLastCommand = checked;
}
void DebuggerConsoleView::toggleShowInternalCommands(bool checked)
{
setShowInternalCommands(checked);
}
void DebuggerConsoleView::appendLine(const QString& line)
{
m_pendingOutput += line;
// To improve performance, we update the view after some delay.
if (!m_updateTimer.isActive())
{
m_updateTimer.start();
}
}
void DebuggerConsoleView::flushPending()
{
m_textView->setUpdatesEnabled(false);
QTextDocument *document = m_textView->document();
QTextCursor cursor(document);
cursor.movePosition(QTextCursor::End);
cursor.insertHtml(m_pendingOutput);
m_pendingOutput.clear();
m_textView->verticalScrollBar()->setValue(m_textView->verticalScrollBar()->maximum());
m_textView->setUpdatesEnabled(true);
m_textView->update();
if (m_cmdEditorHadFocus) {
m_cmdEditor->setFocus();
}
}
void DebuggerConsoleView::clear()
{
if (m_textView)
m_textView->clear();
if (m_cmdEditor)
m_cmdEditor->clear();
m_userOutput.clear();
m_allOutput.clear();
}
void DebuggerConsoleView::handleDebuggerStateChange(DBGStateFlags, DBGStateFlags newStatus)
{
if (newStatus & s_dbgNotStarted) {
m_actInterrupt->setEnabled(false);
m_cmdEditor->setEnabled(false);
return;
} else {
m_actInterrupt->setEnabled(true);
}
if (newStatus & s_dbgBusy) {
if (m_cmdEditor->isEnabled()) {
m_cmdEditorHadFocus = m_cmdEditor->hasFocus();
}
m_cmdEditor->setEnabled(false);
} else {
m_cmdEditor->setEnabled(true);
}
}
QString DebuggerConsoleView::toHtmlEscaped(QString text)
{
text = text.toHtmlEscaped();
text.replace(QLatin1Char('\n'), QLatin1String("<br>"));
return text;
}
QString DebuggerConsoleView::colorify(QString text, const QColor& color)
{
text = QLatin1String("<font color=\"") + color.name() + QLatin1String("\">") + text + QLatin1String("</font>");
return text;
}
void DebuggerConsoleView::receivedInternalCommandStdout(const QString& line)
{
receivedStdout(line, true);
}
void DebuggerConsoleView::receivedUserCommandStdout(const QString& line)
{
receivedStdout(line, false);
}
void DebuggerConsoleView::receivedStdout(const QString& line, bool internal)
{
QString colored = toHtmlEscaped(line);
if (colored.startsWith(QLatin1String("(gdb)"))) {
if (!m_alterPrompt.isEmpty()) {
colored.replace(0, 5, m_alterPrompt);
}
colored = colorify(colored, m_stdColor);
}
m_allOutput.append(colored);
trimList(m_allOutput, m_maxLines);
if (!internal) {
m_userOutput.append(colored);
trimList(m_userOutput, m_maxLines);
}
if (!internal || m_showInternalCommands)
appendLine(colored);
}
void DebuggerConsoleView::receivedStderr(const QString& line)
{
QString colored = toHtmlEscaped(line);
colored = colorify(colored, m_errorColor);
// Errors are shown inside user commands too.
m_allOutput.append(colored);
trimList(m_allOutput, m_maxLines);
m_userOutput.append(colored);
trimList(m_userOutput, m_maxLines);
appendLine(colored);
}
void DebuggerConsoleView::trimList(QStringList& l, int max_size)
{
int length = l.count();
if (length > max_size)
{
for(int to_delete = length - max_size; to_delete; --to_delete)
{
l.erase(l.begin());
}
}
}
void DebuggerConsoleView::trySendCommand(QString cmd)
{
if (m_repeatLastCommand && cmd.isEmpty()) {
cmd = m_cmdEditor->historyItems().last();
}
if (!cmd.isEmpty())
{
m_cmdEditor->addToHistory(cmd);
m_cmdEditor->clearEditText();
emit sendCommand(cmd);
}
}
void DebuggerConsoleView::handleSessionChanged(KDevelop::IDebugSession* s)
{
auto *session = qobject_cast<MIDebugSession*>(s);
if (!session) return;
connect(this, &DebuggerConsoleView::sendCommand,
session, &MIDebugSession::addUserCommand);
connect(this, &DebuggerConsoleView::interruptDebugger,
session, &MIDebugSession::interruptDebugger);
connect(session, &MIDebugSession::debuggerInternalCommandOutput,
this, &DebuggerConsoleView::receivedInternalCommandStdout);
connect(session, &MIDebugSession::debuggerUserCommandOutput,
this, &DebuggerConsoleView::receivedUserCommandStdout);
connect(session, &MIDebugSession::debuggerInternalOutput,
this, &DebuggerConsoleView::receivedStderr);
connect(session, &MIDebugSession::debuggerStateChanged,
this, &DebuggerConsoleView::handleDebuggerStateChange);
handleDebuggerStateChange(s_none, session->debuggerState());
}
|