| File: | builds/wireshark/wireshark/ui/qt/lua_debugger/lua_debugger_code_editor.cpp |
| Warning: | line 488, column 38 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* lua_debugger_code_editor.cpp | |||
| 2 | * | |||
| 3 | * Wireshark - Network traffic analyzer | |||
| 4 | * By Gerald Combs <[email protected]> | |||
| 5 | * Copyright 1998 Gerald Combs | |||
| 6 | * | |||
| 7 | * SPDX-License-Identifier: GPL-2.0-or-later | |||
| 8 | */ | |||
| 9 | ||||
| 10 | /** | |||
| 11 | * @file | |||
| 12 | * Code editor stack: editor view, gutter, theme palette, font policy | |||
| 13 | * and the script-tabs controller. | |||
| 14 | */ | |||
| 15 | ||||
| 16 | #include <lua_debugger_code_editor.h> | |||
| 17 | #include <ui_lua_debugger_dialog.h> | |||
| 18 | ||||
| 19 | #include <QAction> | |||
| 20 | #include <QApplication> | |||
| 21 | #include <QColor> | |||
| 22 | #include <QContextMenuEvent> | |||
| 23 | #include <QDir> | |||
| 24 | #include <QEvent> | |||
| 25 | #include <QFile> | |||
| 26 | #include <QFileInfo> | |||
| 27 | #include <QFont> | |||
| 28 | #include <QFontDatabase> | |||
| 29 | #include <QGuiApplication> | |||
| 30 | #include <QHeaderView> | |||
| 31 | #include <QKeyEvent> | |||
| 32 | #include <QList> | |||
| 33 | #include <QMessageBox> | |||
| 34 | #include <QMetaObject> | |||
| 35 | #include <QModelIndex> | |||
| 36 | #include <QMouseEvent> | |||
| 37 | #include <QPainter> | |||
| 38 | #include <QPalette> | |||
| 39 | #include <QPlainTextEdit> | |||
| 40 | #include <QStandardItem> | |||
| 41 | #include <QStandardItemModel> | |||
| 42 | #include <QStandardPaths> | |||
| 43 | #include <QStringList> | |||
| 44 | #include <QTabWidget> | |||
| 45 | #include <QTextBlock> | |||
| 46 | #include <QTextDocument> | |||
| 47 | #include <QTextStream> | |||
| 48 | #include <QTreeView> | |||
| 49 | #include <QWidget> | |||
| 50 | #include <QtGlobal> | |||
| 51 | ||||
| 52 | #include <algorithm> | |||
| 53 | #include <climits> | |||
| 54 | ||||
| 55 | #include "epan/wslua/wslua_debugger.h" | |||
| 56 | #include "lua_debugger_breakpoints.h" | |||
| 57 | #include "lua_debugger_dialog.h" | |||
| 58 | #include "lua_debugger_files.h" | |||
| 59 | #include "main_application.h" | |||
| 60 | #include <ui/qt/utils/font_manager.h> | |||
| 61 | #include "utils/color_utils.h" | |||
| 62 | #include <ui/qt/utils/theme_manager.h> | |||
| 63 | #include <ui/qt/utils/themes/contrast_adapt_icon.h> | |||
| 64 | ||||
| 65 | /* ===== code_palette ===== */ | |||
| 66 | ||||
| 67 | LuaDebuggerEditorPalette luaDebuggerEditorPalette() | |||
| 68 | { | |||
| 69 | const ThemeManager *theme = ThemeManager::instance(); | |||
| 70 | LuaDebuggerEditorPalette p; | |||
| 71 | p.editorBackground = theme->color(ThemeManager::CodeBackground); | |||
| 72 | p.editorText = theme->color(ThemeManager::CodeText); | |||
| 73 | p.selection = theme->color(ThemeManager::CodeSelection); | |||
| 74 | p.selectionText = theme->color(ThemeManager::CodeSelectionText); | |||
| 75 | p.gutterBackground = theme->color(ThemeManager::CodeGutter); | |||
| 76 | p.gutterText = theme->color(ThemeManager::CodeGutterText); | |||
| 77 | p.pausedLine = theme->color(ThemeManager::CodePausedLine); | |||
| 78 | return p; | |||
| 79 | } | |||
| 80 | ||||
| 81 | /* ===== code_view ===== */ | |||
| 82 | ||||
| 83 | LuaDebuggerCodeView::LuaDebuggerCodeView(QWidget *parent) | |||
| 84 | : QPlainTextEdit(parent), lineNumberArea(new LineNumberArea(this)), syntaxHighlighter(nullptr) | |||
| 85 | { | |||
| 86 | /* Gutter tooltip surfaces both click affordances; the Shift+click | |||
| 87 | * behavior is otherwise non-discoverable. */ | |||
| 88 | lineNumberArea->setToolTip(tr("Click: add or remove breakpoint\n" | |||
| 89 | "Drag existing breakpoint: move to nearest free visible line\n" | |||
| 90 | "Shift+click: on an empty line, add a disabled breakpoint;\n" | |||
| 91 | "on an existing breakpoint, toggle its active state")); | |||
| 92 | syntaxHighlighter = new LuaSyntaxHighlighter(document()); | |||
| 93 | ||||
| 94 | connect(this, &LuaDebuggerCodeView::blockCountChanged, this, &LuaDebuggerCodeView::updateLineNumberAreaWidth); | |||
| 95 | connect(this, &LuaDebuggerCodeView::updateRequest, this, &LuaDebuggerCodeView::updateLineNumberArea); | |||
| 96 | connect(this, &LuaDebuggerCodeView::cursorPositionChanged, this, &LuaDebuggerCodeView::rebuildLineHighlights); | |||
| 97 | ||||
| 98 | /* Track line insertions/deletions for automatic breakpoint adjustment. | |||
| 99 | * contentsChange() reports the exact edit position, so typing, paste, | |||
| 100 | * cut, drag-and-drop, undo and redo are all handled the same way. */ | |||
| 101 | previousBlockCount_ = static_cast<qint32>(document()->blockCount()); | |||
| 102 | connect(document(), &QTextDocument::contentsChange, this, &LuaDebuggerCodeView::onContentsChange); | |||
| 103 | ||||
| 104 | /* QAbstractScrollArea delivers key events to the viewport; Esc never | |||
| 105 | * reaches QDialog::keyPressEvent. Forward to LuaDebuggerDialog::handleEscapeKey(). */ | |||
| 106 | viewport()->installEventFilter(this); | |||
| 107 | ||||
| 108 | setReadOnly(false); | |||
| 109 | setLineWrapMode(QPlainTextEdit::NoWrap); | |||
| 110 | ||||
| 111 | QFont initialFont; | |||
| 112 | if (mainApp && mainApp->isInitialized()) | |||
| 113 | { | |||
| 114 | initialFont = FontManager::monospaceFont(); | |||
| 115 | } | |||
| 116 | setEditorFont(initialFont); | |||
| 117 | applyEditorPalette(); | |||
| 118 | rebuildLineHighlights(); | |||
| 119 | } | |||
| 120 | ||||
| 121 | QString LuaDebuggerCodeView::luaIdentifierUnderCursor(const QTextCursor &cursor) const | |||
| 122 | { | |||
| 123 | const QString block = cursor.block().text(); | |||
| 124 | const int posInBlock = cursor.positionInBlock(); | |||
| 125 | if (block.isEmpty() || posInBlock < 0 || posInBlock > block.size()) | |||
| 126 | { | |||
| 127 | return {}; | |||
| 128 | } | |||
| 129 | ||||
| 130 | auto isIdentChar = [](QChar c) { return c.isLetterOrNumber() || c == QLatin1Char('_'); }; | |||
| 131 | auto isIdentStart = [](QChar c) { return c.isLetter() || c == QLatin1Char('_'); }; | |||
| 132 | ||||
| 133 | /* Caret may sit one past the end of the identifier; expand left | |||
| 134 | * until we are inside, then sweep both directions. */ | |||
| 135 | int start = posInBlock; | |||
| 136 | int end = posInBlock; | |||
| 137 | if (start > 0 && !isIdentChar(block.at(start - 1)) && (start >= block.size() || !isIdentChar(block.at(start)))) | |||
| 138 | { | |||
| 139 | return {}; | |||
| 140 | } | |||
| 141 | while (start > 0 && isIdentChar(block.at(start - 1))) | |||
| 142 | { | |||
| 143 | --start; | |||
| 144 | } | |||
| 145 | while (end < block.size() && isIdentChar(block.at(end))) | |||
| 146 | { | |||
| 147 | ++end; | |||
| 148 | } | |||
| 149 | if (start >= end) | |||
| 150 | { | |||
| 151 | return {}; | |||
| 152 | } | |||
| 153 | if (!isIdentStart(block.at(start))) | |||
| 154 | { | |||
| 155 | return {}; | |||
| 156 | } | |||
| 157 | return block.mid(start, end - start); | |||
| 158 | } | |||
| 159 | ||||
| 160 | QString LuaDebuggerCodeView::watchExpressionForContextMenu(const QPoint &viewportPos) const | |||
| 161 | { | |||
| 162 | QString s = textCursor().selectedText().trimmed(); | |||
| 163 | if (!s.isEmpty()) | |||
| 164 | { | |||
| 165 | return s; | |||
| 166 | } | |||
| 167 | return luaIdentifierUnderCursor(cursorForPosition(viewportPos)); | |||
| 168 | } | |||
| 169 | ||||
| 170 | qint32 LuaDebuggerCodeView::lineNumberAreaWidth() | |||
| 171 | { | |||
| 172 | qint32 digits = 1; | |||
| 173 | qint32 maxBlockCount = qMax(1, blockCount()); | |||
| 174 | while (maxBlockCount >= 10) | |||
| 175 | { | |||
| 176 | maxBlockCount /= 10; | |||
| 177 | ++digits; | |||
| 178 | } | |||
| 179 | ||||
| 180 | const qint32 space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits; | |||
| 181 | return space + 20; // Extra space for breakpoint icon | |||
| 182 | } | |||
| 183 | ||||
| 184 | void LuaDebuggerCodeView::updateLineNumberAreaWidth(int /* newBlockCount */) | |||
| 185 | { | |||
| 186 | setViewportMargins(static_cast<int>(lineNumberAreaWidth()), 0, 0, 0); | |||
| 187 | } | |||
| 188 | ||||
| 189 | void LuaDebuggerCodeView::updateLineNumberArea(const QRect &rect, int dy) | |||
| 190 | { | |||
| 191 | if (dy) | |||
| 192 | lineNumberArea->scroll(0, dy); | |||
| 193 | else | |||
| 194 | lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); | |||
| 195 | ||||
| 196 | if (rect.contains(viewport()->rect())) | |||
| 197 | updateLineNumberAreaWidth(0); | |||
| 198 | } | |||
| 199 | ||||
| 200 | void LuaDebuggerCodeView::resizeEvent(QResizeEvent *e) | |||
| 201 | { | |||
| 202 | QPlainTextEdit::resizeEvent(e); | |||
| 203 | ||||
| 204 | QRect cr = contentsRect(); | |||
| 205 | lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), static_cast<int>(lineNumberAreaWidth()), cr.height())); | |||
| 206 | } | |||
| 207 | ||||
| 208 | bool LuaDebuggerCodeView::eventFilter(QObject *watched, QEvent *event) | |||
| 209 | { | |||
| 210 | if (watched == viewport() && event->type() == QEvent::KeyPress) | |||
| 211 | { | |||
| 212 | auto *ke = static_cast<QKeyEvent *>(event); | |||
| 213 | if (ke->key() == Qt::Key_Escape && ke->modifiers() == Qt::NoModifier) | |||
| 214 | { | |||
| 215 | if (LuaDebuggerDialog *dlg = LuaDebuggerDialog::instance()) | |||
| 216 | { | |||
| 217 | QMetaObject::invokeMethod(dlg, "handleEscapeKey", Qt::QueuedConnection); | |||
| 218 | return true; | |||
| 219 | } | |||
| 220 | } | |||
| 221 | } | |||
| 222 | return QPlainTextEdit::eventFilter(watched, event); | |||
| 223 | } | |||
| 224 | ||||
| 225 | void LuaDebuggerCodeView::keyPressEvent(QKeyEvent *event) | |||
| 226 | { | |||
| 227 | if (isReadOnly()) | |||
| 228 | { | |||
| 229 | const int key = event->key(); | |||
| 230 | const bool editKey = key == Qt::Key_Backspace || key == Qt::Key_Delete || key == Qt::Key_Return || | |||
| 231 | key == Qt::Key_Enter || key == Qt::Key_Tab || | |||
| 232 | (!event->text().isEmpty() && event->text().at(0).isPrint() && | |||
| 233 | !(event->modifiers() & (Qt::ControlModifier | Qt::MetaModifier))); | |||
| 234 | if (editKey) | |||
| 235 | { | |||
| 236 | emit readOnlyEditAttempted(); | |||
| 237 | } | |||
| 238 | } | |||
| 239 | QPlainTextEdit::keyPressEvent(event); | |||
| 240 | } | |||
| 241 | ||||
| 242 | void LuaDebuggerCodeView::reloadContents(const QString &text) | |||
| 243 | { | |||
| 244 | reloadingContents_ = true; | |||
| 245 | setPlainText(text); | |||
| 246 | reloadingContents_ = false; | |||
| 247 | } | |||
| 248 | ||||
| 249 | void LuaDebuggerCodeView::rebuildLineHighlights() | |||
| 250 | { | |||
| 251 | QList<QTextEdit::ExtraSelection> extraSelections; | |||
| 252 | ||||
| 253 | /* Debugger paused line — amber bar; theme-specific (independent of caret). */ | |||
| 254 | if (pausedExecutionLine_ > 0) | |||
| 255 | { | |||
| 256 | QTextBlock pauseBlock = document()->findBlockByNumber(static_cast<int>(pausedExecutionLine_ - 1)); | |||
| 257 | if (pauseBlock.isValid()) | |||
| 258 | { | |||
| 259 | QTextCursor pauseCursor(pauseBlock); | |||
| 260 | pauseCursor.movePosition(QTextCursor::StartOfBlock); | |||
| 261 | QTextEdit::ExtraSelection pauseSel; | |||
| 262 | const LuaDebuggerEditorPalette palette = luaDebuggerEditorPalette(); | |||
| 263 | pauseSel.format.setBackground(palette.pausedLine); | |||
| 264 | pauseSel.format.setProperty(QTextFormat::FullWidthSelection, true); | |||
| 265 | pauseSel.cursor = pauseCursor; | |||
| 266 | pauseSel.cursor.clearSelection(); | |||
| 267 | extraSelections.append(pauseSel); | |||
| 268 | } | |||
| 269 | } | |||
| 270 | ||||
| 271 | /* Caret line — subtle; skip if same line as debugger (do not replace pause look). */ | |||
| 272 | QTextBlock caretBlock = textCursor().block(); | |||
| 273 | if (caretBlock.isValid()) | |||
| 274 | { | |||
| 275 | const int caretLine = caretBlock.blockNumber() + 1; | |||
| 276 | const bool sameAsPause = (pausedExecutionLine_ > 0) && (caretLine == pausedExecutionLine_); | |||
| 277 | ||||
| 278 | if (!sameAsPause) | |||
| 279 | { | |||
| 280 | /* Match the line-number gutter background (see applyEditorPalette). */ | |||
| 281 | const QColor lineColor = lineNumberArea->palette().color(QPalette::Base); | |||
| 282 | ||||
| 283 | QTextCursor caretLineCursor(caretBlock); | |||
| 284 | caretLineCursor.movePosition(QTextCursor::StartOfBlock); | |||
| 285 | QTextEdit::ExtraSelection caretSel; | |||
| 286 | caretSel.format.setBackground(lineColor); | |||
| 287 | caretSel.format.setProperty(QTextFormat::FullWidthSelection, true); | |||
| 288 | caretSel.cursor = caretLineCursor; | |||
| 289 | caretSel.cursor.clearSelection(); | |||
| 290 | extraSelections.append(caretSel); | |||
| 291 | } | |||
| 292 | } | |||
| 293 | ||||
| 294 | setExtraSelections(extraSelections); | |||
| 295 | } | |||
| 296 | ||||
| 297 | void LuaDebuggerCodeView::onContentsChange(int position) | |||
| 298 | { | |||
| 299 | /* Compare against the block count before this edit. Edits that do not | |||
| 300 | * add or remove a line (the common case: typing) exit here. */ | |||
| 301 | const qint32 newBlockCount = static_cast<qint32>(document()->blockCount()); | |||
| 302 | const qint32 blockDelta = newBlockCount - previousBlockCount_; | |||
| 303 | previousBlockCount_ = newBlockCount; | |||
| 304 | ||||
| 305 | if (blockDelta == 0 || filename.isEmpty() || reloadingContents_) | |||
| 306 | { | |||
| 307 | return; | |||
| 308 | } | |||
| 309 | ||||
| 310 | /* The edit starts at @a position, which is valid in the new document. */ | |||
| 311 | const QTextBlock block = document()->findBlock(position); | |||
| 312 | if (!block.isValid()) | |||
| 313 | { | |||
| 314 | return; | |||
| 315 | } | |||
| 316 | ||||
| 317 | const qint32 changeLineNumber = static_cast<qint32>(block.blockNumber() + 1); | |||
| 318 | const bool atLineStart = (position == block.position()); | |||
| 319 | ||||
| 320 | emit editorLinesChanged(filename, changeLineNumber, blockDelta, atLineStart); | |||
| 321 | } | |||
| 322 | ||||
| 323 | void LuaDebuggerCodeView::setCurrentLine(qint32 line) | |||
| 324 | { | |||
| 325 | /* The gutter repaints on cursor movement via the updateRequest | |||
| 326 | * signal, but the early-return branches below do not move the | |||
| 327 | * cursor, and we need the paused-line arrow (and the | |||
| 328 | * accompanying dimmed breakpoint circle, if any) to appear / | |||
| 329 | * disappear whenever pausedExecutionLine_ changes. Request a | |||
| 330 | * line-number-area repaint explicitly in every branch. */ | |||
| 331 | if (line <= 0) | |||
| 332 | { | |||
| 333 | pausedExecutionLine_ = -1; | |||
| 334 | rebuildLineHighlights(); | |||
| 335 | lineNumberArea->update(); | |||
| 336 | return; | |||
| 337 | } | |||
| 338 | ||||
| 339 | QTextBlock block = document()->findBlockByNumber(static_cast<int>(line - 1)); | |||
| 340 | if (!block.isValid()) | |||
| 341 | { | |||
| 342 | pausedExecutionLine_ = -1; | |||
| 343 | rebuildLineHighlights(); | |||
| 344 | lineNumberArea->update(); | |||
| 345 | return; | |||
| 346 | } | |||
| 347 | ||||
| 348 | pausedExecutionLine_ = line; | |||
| 349 | QTextCursor cursor(block); | |||
| 350 | cursor.movePosition(QTextCursor::StartOfBlock); | |||
| 351 | setTextCursor(cursor); | |||
| 352 | rebuildLineHighlights(); | |||
| 353 | lineNumberArea->update(); | |||
| 354 | } | |||
| 355 | ||||
| 356 | void LuaDebuggerCodeView::moveCaretToLineStart(qint32 line) | |||
| 357 | { | |||
| 358 | if (line <= 0) | |||
| 359 | { | |||
| 360 | return; | |||
| 361 | } | |||
| 362 | ||||
| 363 | QTextBlock block = document()->findBlockByNumber(static_cast<int>(line - 1)); | |||
| 364 | if (!block.isValid()) | |||
| 365 | { | |||
| 366 | return; | |||
| 367 | } | |||
| 368 | ||||
| 369 | QTextCursor cursor(block); | |||
| 370 | cursor.movePosition(QTextCursor::StartOfBlock); | |||
| 371 | setTextCursor(cursor); | |||
| 372 | } | |||
| 373 | ||||
| 374 | void LuaDebuggerCodeView::clearCurrentLineHighlight() | |||
| 375 | { | |||
| 376 | pausedExecutionLine_ = -1; | |||
| 377 | rebuildLineHighlights(); | |||
| 378 | /* Force the gutter to repaint so the paused-line arrow | |||
| 379 | * is cleared and any dimmed breakpoint circle returns to full | |||
| 380 | * brightness on resume, even when no cursor movement triggers | |||
| 381 | * updateRequest. */ | |||
| 382 | lineNumberArea->update(); | |||
| 383 | } | |||
| 384 | ||||
| 385 | void LuaDebuggerCodeView::setEditorFont(const QFont &font) | |||
| 386 | { | |||
| 387 | QFont resolvedFont = font; | |||
| 388 | if (resolvedFont.family().isEmpty()) | |||
| 389 | { | |||
| 390 | resolvedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); | |||
| 391 | } | |||
| 392 | resolvedFont.setStyleHint(QFont::TypeWriter, QFont::PreferDefault); | |||
| 393 | QPlainTextEdit::setFont(resolvedFont); | |||
| 394 | lineNumberArea->setFont(resolvedFont); | |||
| 395 | updateLineNumberAreaWidth(0); | |||
| 396 | lineNumberArea->update(); | |||
| 397 | viewport()->update(); | |||
| 398 | } | |||
| 399 | ||||
| 400 | void LuaDebuggerCodeView::updateBreakpointMarkers() | |||
| 401 | { | |||
| 402 | lineNumberArea->update(); | |||
| 403 | } | |||
| 404 | ||||
| 405 | void LuaDebuggerCodeView::applyTheme() | |||
| 406 | { | |||
| 407 | applyEditorPalette(); | |||
| 408 | if (syntaxHighlighter) | |||
| 409 | { | |||
| 410 | static_cast<LuaSyntaxHighlighter *>(syntaxHighlighter)->updateColors(); | |||
| 411 | } | |||
| 412 | lineNumberArea->update(); | |||
| 413 | viewport()->update(); | |||
| 414 | rebuildLineHighlights(); | |||
| 415 | } | |||
| 416 | ||||
| 417 | void LuaDebuggerCodeView::applyEditorPalette() | |||
| 418 | { | |||
| 419 | const LuaDebuggerEditorPalette colors = luaDebuggerEditorPalette(); | |||
| 420 | QPalette pal = palette(); | |||
| 421 | QPalette gutterPal = lineNumberArea->palette(); | |||
| 422 | ||||
| 423 | pal.setColor(QPalette::Base, colors.editorBackground); | |||
| 424 | pal.setColor(QPalette::Text, colors.editorText); | |||
| 425 | pal.setColor(QPalette::Highlight, colors.selection); | |||
| 426 | pal.setColor(QPalette::HighlightedText, colors.selectionText); | |||
| 427 | /* Match the editor base on Window so the gap between gutter and | |||
| 428 | * viewport doesn't reveal the host palette. */ | |||
| 429 | pal.setColor(QPalette::Window, colors.editorBackground); | |||
| 430 | ||||
| 431 | gutterPal.setColor(QPalette::Window, colors.gutterBackground); | |||
| 432 | gutterPal.setColor(QPalette::Base, colors.gutterBackground); | |||
| 433 | gutterPal.setColor(QPalette::WindowText, colors.gutterText); | |||
| 434 | ||||
| 435 | setPalette(pal); | |||
| 436 | setAutoFillBackground(true); | |||
| 437 | ||||
| 438 | viewport()->setPalette(pal); | |||
| 439 | viewport()->setAutoFillBackground(true); | |||
| 440 | ||||
| 441 | lineNumberArea->setPalette(gutterPal); | |||
| 442 | lineNumberArea->setAutoFillBackground(true); | |||
| 443 | lineNumberArea->update(); | |||
| 444 | } | |||
| 445 | ||||
| 446 | void LuaDebuggerCodeView::lineNumberAreaPaintEvent(QPaintEvent *event) | |||
| 447 | { | |||
| 448 | const LuaDebuggerEditorPalette colors = luaDebuggerEditorPalette(); | |||
| 449 | QPainter painter(lineNumberArea); | |||
| 450 | painter.setRenderHint(QPainter::Antialiasing, true); | |||
| 451 | ||||
| 452 | painter.fillRect(event->rect(), colors.gutterBackground); | |||
| 453 | ||||
| 454 | /* Highlight the drag target line if a breakpoint move is in progress. */ | |||
| 455 | LineNumberArea *gutter = static_cast<LineNumberArea *>(lineNumberArea); | |||
| 456 | if (gutter && gutter->isDraggingBreakpoint() && gutter->dragTargetLine() > 0) | |||
| ||||
| 457 | { | |||
| 458 | const qint32 targetLine = gutter->dragTargetLine(); | |||
| 459 | QTextBlock targetBlock = document()->findBlockByNumber(static_cast<int>(targetLine - 1)); | |||
| 460 | if (targetBlock.isValid()) | |||
| 461 | { | |||
| 462 | qint32 targetTop = static_cast<qint32>(blockBoundingGeometry(targetBlock).translated(contentOffset()).top()); | |||
| 463 | qint32 targetHeight = static_cast<qint32>(blockBoundingRect(targetBlock).height()); | |||
| 464 | QColor dragHighlight = colors.selection; | |||
| 465 | dragHighlight.setAlpha(80); | |||
| 466 | painter.fillRect(0, targetTop, lineNumberArea->width(), targetHeight, dragHighlight); | |||
| 467 | } | |||
| 468 | } | |||
| 469 | ||||
| 470 | /* Canonicalize the filename once for all visible lines. */ | |||
| 471 | char *canonical = nullptr; | |||
| 472 | if (!filename.isEmpty()) | |||
| 473 | { | |||
| 474 | canonical = wslua_debugger_canonical_path(filename.toUtf8().constData()); | |||
| 475 | } | |||
| 476 | ||||
| 477 | QTextBlock block = firstVisibleBlock(); | |||
| 478 | qint32 blockNumber = block.blockNumber(); | |||
| 479 | qint32 top = static_cast<qint32>(blockBoundingGeometry(block).translated(contentOffset()).top()); | |||
| 480 | qint32 bottom = top + static_cast<qint32>(blockBoundingRect(block).height()); | |||
| 481 | ||||
| 482 | while (block.isValid() && top <= event->rect().bottom()) | |||
| 483 | { | |||
| 484 | if (block.isVisible() && bottom >= event->rect().top()) | |||
| 485 | { | |||
| 486 | QString number = QString::number(blockNumber + 1); | |||
| 487 | painter.setPen(colors.gutterText); | |||
| 488 | painter.drawText(0, top, lineNumberArea->width() - 20, fontMetrics().height(), Qt::AlignRight, number); | |||
| ||||
| 489 | ||||
| 490 | const qint32 lineNo = static_cast<qint32>(blockNumber + 1); | |||
| 491 | const bool pausedHere = (pausedExecutionLine_ == lineNo); | |||
| 492 | ||||
| 493 | /* Breakpoint circle. On the paused line the continue icon is | |||
| 494 | * drawn on top of it. | |||
| 495 | * During a breakpoint drag, skip drawing the source line's | |||
| 496 | * breakpoint so the preview at the target is visually distinct. */ | |||
| 497 | const bool isDragSource = (gutter && gutter->isDraggingBreakpoint() && lineNo == gutter->dragSourceLine()); | |||
| 498 | if (canonical && !isDragSource) | |||
| 499 | { | |||
| 500 | bool hasExtras = false; | |||
| 501 | const int32_t state = wslua_debugger_get_breakpoint_state_canonical(canonical, lineNo, &hasExtras); | |||
| 502 | if (state != -1) | |||
| 503 | { | |||
| 504 | const qint32 radius = fontMetrics().height() / 2 - 1; | |||
| 505 | const qint32 cx = lineNumberArea->width() - 16; | |||
| 506 | const qint32 cy = top + 2; | |||
| 507 | luaDbgDrawBreakpointDot(painter, cx, cy, radius, (state == 1), colors.gutterText, hasExtras); | |||
| 508 | } | |||
| 509 | } | |||
| 510 | ||||
| 511 | /* The continue icon in the theme's warning colour, outlined in | |||
| 512 | * the gutter text colour, marks the line the debugger is paused | |||
| 513 | * at. Drawn after (and therefore on top of) the breakpoint | |||
| 514 | * circle. */ | |||
| 515 | if (pausedHere) | |||
| 516 | { | |||
| 517 | const qint32 radius = fontMetrics().height() / 2 - 2; | |||
| 518 | const qint32 diameter = radius * 2; | |||
| 519 | const qint32 x = lineNumberArea->width() - 15; | |||
| 520 | const qint32 y = top + 2; | |||
| 521 | /* The play shape covers about 18/24 of the icon box. Draw | |||
| 522 | * the box at a whole-pixel size, unscaled, centred on the | |||
| 523 | * diameter-sized square the marker occupies. */ | |||
| 524 | const qint32 box = qRound(diameter * 24.0 / 18.0); | |||
| 525 | const QPoint boxTopLeft(x + (diameter - box) / 2, y + (diameter - box) / 2); | |||
| 526 | const qreal dpr = painter.device() ? painter.device()->devicePixelRatioF() : 1.0; | |||
| 527 | static ContrastAdaptIcon icon("lua-debug-paused-line"); | |||
| 528 | icon.setTokenColors({ | |||
| 529 | {ThemeManager::AccentWarning, ThemeManager::instance()->color(ThemeManager::AccentWarning)}, | |||
| 530 | {ThemeManager::PaletteText, colors.gutterText}, | |||
| 531 | }); | |||
| 532 | painter.drawPixmap(boxTopLeft, icon.pixmap(QSize(box, box), dpr)); | |||
| 533 | } | |||
| 534 | } | |||
| 535 | ||||
| 536 | block = block.next(); | |||
| 537 | top = bottom; | |||
| 538 | bottom = top + static_cast<qint32>(blockBoundingRect(block).height()); | |||
| 539 | ++blockNumber; | |||
| 540 | } | |||
| 541 | ||||
| 542 | /* Draw semi-transparent preview breakpoint at the drag target line. | |||
| 543 | * Uses the same icon (concentric for extras) and colour as the | |||
| 544 | * source breakpoint, with reduced alpha for visual distinction. */ | |||
| 545 | if (gutter && gutter->isDraggingBreakpoint() && gutter->dragTargetLine() > 0) | |||
| 546 | { | |||
| 547 | const qint32 targetLine = gutter->dragTargetLine(); | |||
| 548 | const qint32 sourceLine = gutter->dragSourceLine(); | |||
| 549 | QTextBlock targetBlock = document()->findBlockByNumber(static_cast<int>(targetLine - 1)); | |||
| 550 | if (targetBlock.isValid() && canonical && sourceLine > 0) | |||
| 551 | { | |||
| 552 | /* Query the source breakpoint to get its state and extras info. */ | |||
| 553 | bool hasExtras = false; | |||
| 554 | const int32_t state = wslua_debugger_get_breakpoint_state_canonical(canonical, sourceLine, &hasExtras); | |||
| 555 | if (state != -1) | |||
| 556 | { | |||
| 557 | qint32 targetTop = static_cast<qint32>(blockBoundingGeometry(targetBlock).translated(contentOffset()).top()); | |||
| 558 | qint32 targetBottom = targetTop + static_cast<qint32>(blockBoundingRect(targetBlock).height()); | |||
| 559 | ||||
| 560 | if (targetTop <= event->rect().bottom() && targetBottom >= event->rect().top()) | |||
| 561 | { | |||
| 562 | const qint32 radius = fontMetrics().height() / 2 - 1; | |||
| 563 | const qint32 cx = lineNumberArea->width() - 16; | |||
| 564 | const qint32 cy = targetTop + 2; | |||
| 565 | luaDbgDrawBreakpointDot(painter, cx, cy, radius, (state == 1), colors.gutterText, hasExtras, | |||
| 566 | 120); | |||
| 567 | } | |||
| 568 | } | |||
| 569 | } | |||
| 570 | } | |||
| 571 | ||||
| 572 | g_free(canonical)(__builtin_object_size ((canonical), 0) != ((size_t) - 1)) ? g_free_sized (canonical, __builtin_object_size ((canonical), 0)) : (g_free ) (canonical); | |||
| 573 | } | |||
| 574 | ||||
| 575 | qint32 LineNumberArea::lineAtY(qint32 yPx) const | |||
| 576 | { | |||
| 577 | QTextBlock block = codeEditor->firstVisibleBlock(); | |||
| 578 | qint32 top = | |||
| 579 | static_cast<qint32>(codeEditor->blockBoundingGeometry(block).translated(codeEditor->contentOffset()).top()); | |||
| 580 | qint32 bottom = top + static_cast<qint32>(codeEditor->blockBoundingRect(block).height()); | |||
| 581 | qint32 blockNumber = block.blockNumber(); | |||
| 582 | while (block.isValid()) | |||
| 583 | { | |||
| 584 | if (yPx >= top && yPx <= bottom) | |||
| 585 | { | |||
| 586 | return blockNumber + 1; | |||
| 587 | } | |||
| 588 | block = block.next(); | |||
| 589 | top = bottom; | |||
| 590 | bottom = top + static_cast<qint32>(codeEditor->blockBoundingRect(block).height()); | |||
| 591 | ++blockNumber; | |||
| 592 | } | |||
| 593 | return -1; | |||
| 594 | } | |||
| 595 | ||||
| 596 | bool LineNumberArea::hasBreakpointAtLine(qint32 line) const | |||
| 597 | { | |||
| 598 | if (!codeEditor || codeEditor->filename.isEmpty() || line < 1) | |||
| 599 | { | |||
| 600 | return false; | |||
| 601 | } | |||
| 602 | ||||
| 603 | char *canonical = wslua_debugger_canonical_path(codeEditor->filename.toUtf8().constData()); | |||
| 604 | if (!canonical) | |||
| 605 | { | |||
| 606 | return false; | |||
| 607 | } | |||
| 608 | const int32_t state = wslua_debugger_get_breakpoint_state_canonical(canonical, line, /*has_extras=*/nullptr); | |||
| 609 | g_free(canonical)(__builtin_object_size ((canonical), 0) != ((size_t) - 1)) ? g_free_sized (canonical, __builtin_object_size ((canonical), 0)) : (g_free ) (canonical); | |||
| 610 | return state != -1; | |||
| 611 | } | |||
| 612 | ||||
| 613 | qint32 LineNumberArea::nearestVisibleDropLine(qint32 yPx, qint32 sourceLine) const | |||
| 614 | { | |||
| 615 | struct Candidate | |||
| 616 | { | |||
| 617 | qint32 line; | |||
| 618 | qint32 centerY; | |||
| 619 | }; | |||
| 620 | ||||
| 621 | QVector<Candidate> candidates; | |||
| 622 | QTextBlock block = codeEditor->firstVisibleBlock(); | |||
| 623 | qint32 top = | |||
| 624 | static_cast<qint32>(codeEditor->blockBoundingGeometry(block).translated(codeEditor->contentOffset()).top()); | |||
| 625 | qint32 bottom = top + static_cast<qint32>(codeEditor->blockBoundingRect(block).height()); | |||
| 626 | qint32 blockNumber = block.blockNumber(); | |||
| 627 | const qint32 viewportBottom = codeEditor->viewport()->height(); | |||
| 628 | ||||
| 629 | while (block.isValid() && top <= viewportBottom) | |||
| 630 | { | |||
| 631 | if (block.isVisible() && bottom >= 0) | |||
| 632 | { | |||
| 633 | const qint32 line = blockNumber + 1; | |||
| 634 | const bool occupied = hasBreakpointAtLine(line); | |||
| 635 | if (!occupied || line == sourceLine) | |||
| 636 | { | |||
| 637 | candidates.append({line, top + (bottom - top) / 2}); | |||
| 638 | } | |||
| 639 | } | |||
| 640 | ||||
| 641 | block = block.next(); | |||
| 642 | top = bottom; | |||
| 643 | bottom = top + static_cast<qint32>(codeEditor->blockBoundingRect(block).height()); | |||
| 644 | ++blockNumber; | |||
| 645 | } | |||
| 646 | ||||
| 647 | if (candidates.isEmpty()) | |||
| 648 | { | |||
| 649 | return -1; | |||
| 650 | } | |||
| 651 | ||||
| 652 | qint32 bestLine = -1; | |||
| 653 | qint32 bestDist = INT_MAX2147483647; | |||
| 654 | qint32 bestLineDelta = INT_MAX2147483647; | |||
| 655 | for (const Candidate &candidate : candidates) | |||
| 656 | { | |||
| 657 | const qint32 yDist = qAbs(candidate.centerY - yPx); | |||
| 658 | const qint32 lineDelta = qAbs(candidate.line - sourceLine); | |||
| 659 | if (bestLine == -1 || yDist < bestDist || (yDist == bestDist && lineDelta < bestLineDelta)) | |||
| 660 | { | |||
| 661 | bestLine = candidate.line; | |||
| 662 | bestDist = yDist; | |||
| 663 | bestLineDelta = lineDelta; | |||
| 664 | } | |||
| 665 | } | |||
| 666 | return bestLine; | |||
| 667 | } | |||
| 668 | ||||
| 669 | void LineNumberArea::mousePressEvent(QMouseEvent *event) | |||
| 670 | { | |||
| 671 | leftPressArmed_ = false; | |||
| 672 | draggingBreakpoint_ = false; | |||
| 673 | pressedLine_ = -1; | |||
| 674 | dragTargetLine_ = -1; | |||
| 675 | pressShiftToggle_ = false; | |||
| 676 | pressHadBreakpoint_ = false; | |||
| 677 | ||||
| 678 | /* Only the primary button drives add / remove / toggle here. The | |||
| 679 | * secondary button (right-click on Win/Linux, Ctrl-click or | |||
| 680 | * two-finger trackpad tap on macOS) is handled by | |||
| 681 | * @ref contextMenuEvent, which always pops the | |||
| 682 | * Edit / Disable / Remove menu so that gesture is never confused | |||
| 683 | * with the toggle path. */ | |||
| 684 | if (event->button() != Qt::LeftButton) | |||
| 685 | { | |||
| 686 | QWidget::mousePressEvent(event); | |||
| 687 | return; | |||
| 688 | } | |||
| 689 | ||||
| 690 | const QPoint click_pos = event->pos(); | |||
| 691 | if (click_pos.x() <= width() - 20) | |||
| 692 | { | |||
| 693 | QWidget::mousePressEvent(event); | |||
| 694 | return; | |||
| 695 | } | |||
| 696 | ||||
| 697 | const qint32 lineNo = lineAtY(click_pos.y()); | |||
| 698 | if (lineNo < 1) | |||
| 699 | { | |||
| 700 | QWidget::mousePressEvent(event); | |||
| 701 | return; | |||
| 702 | } | |||
| 703 | ||||
| 704 | leftPressArmed_ = true; | |||
| 705 | pressPos_ = click_pos; | |||
| 706 | pressedLine_ = lineNo; | |||
| 707 | dragTargetLine_ = lineNo; | |||
| 708 | pressShiftToggle_ = (event->modifiers() & Qt::ShiftModifier) != 0; | |||
| 709 | pressHadBreakpoint_ = hasBreakpointAtLine(lineNo); | |||
| 710 | event->accept(); | |||
| 711 | } | |||
| 712 | ||||
| 713 | void LineNumberArea::mouseMoveEvent(QMouseEvent *event) | |||
| 714 | { | |||
| 715 | if (!leftPressArmed_ || !pressHadBreakpoint_) | |||
| 716 | { | |||
| 717 | QWidget::mouseMoveEvent(event); | |||
| 718 | return; | |||
| 719 | } | |||
| 720 | ||||
| 721 | if (!draggingBreakpoint_) | |||
| 722 | { | |||
| 723 | const qint32 dragDistance = (event->pos() - pressPos_).manhattanLength(); | |||
| 724 | if (dragDistance < QApplication::startDragDistance()) | |||
| 725 | { | |||
| 726 | return; | |||
| 727 | } | |||
| 728 | draggingBreakpoint_ = true; | |||
| 729 | setCursor(Qt::ClosedHandCursor); | |||
| 730 | } | |||
| 731 | ||||
| 732 | const qint32 prevTarget = dragTargetLine_; | |||
| 733 | dragTargetLine_ = nearestVisibleDropLine(event->pos().y(), pressedLine_); | |||
| 734 | ||||
| 735 | /* Repaint gutter to show updated drag target highlight. */ | |||
| 736 | if (dragTargetLine_ != prevTarget) | |||
| 737 | { | |||
| 738 | update(); | |||
| 739 | } | |||
| 740 | event->accept(); | |||
| 741 | } | |||
| 742 | ||||
| 743 | void LineNumberArea::mouseReleaseEvent(QMouseEvent *event) | |||
| 744 | { | |||
| 745 | if (event->button() != Qt::LeftButton) | |||
| 746 | { | |||
| 747 | QWidget::mouseReleaseEvent(event); | |||
| 748 | return; | |||
| 749 | } | |||
| 750 | ||||
| 751 | const bool armed = leftPressArmed_; | |||
| 752 | const bool wasDragging = draggingBreakpoint_; | |||
| 753 | const bool sourceHasBreakpoint = pressHadBreakpoint_; | |||
| 754 | const bool toggleActive = pressShiftToggle_; | |||
| 755 | const qint32 lineNo = pressedLine_; | |||
| 756 | const qint32 dropLine = (dragTargetLine_ > 0) ? dragTargetLine_ : nearestVisibleDropLine(event->pos().y(), lineNo); | |||
| 757 | ||||
| 758 | leftPressArmed_ = false; | |||
| 759 | draggingBreakpoint_ = false; | |||
| 760 | pressedLine_ = -1; | |||
| 761 | dragTargetLine_ = -1; | |||
| 762 | pressShiftToggle_ = false; | |||
| 763 | pressHadBreakpoint_ = false; | |||
| 764 | unsetCursor(); | |||
| 765 | ||||
| 766 | if (!armed || lineNo < 1) | |||
| 767 | { | |||
| 768 | QWidget::mouseReleaseEvent(event); | |||
| 769 | return; | |||
| 770 | } | |||
| 771 | ||||
| 772 | if (wasDragging && sourceHasBreakpoint) | |||
| 773 | { | |||
| 774 | if (dropLine > 0 && dropLine != lineNo) | |||
| 775 | { | |||
| 776 | emit codeEditor->breakpointMoveRequested(codeEditor->filename, lineNo, dropLine); | |||
| 777 | codeEditor->viewport()->update(); | |||
| 778 | update(); | |||
| 779 | } | |||
| 780 | event->accept(); | |||
| 781 | return; | |||
| 782 | } | |||
| 783 | ||||
| 784 | /* Plain left-click on a "rich" breakpoint (one carrying a | |||
| 785 | * condition, a hit-count target, or a log message) opens the | |||
| 786 | * Edit / Disable / Remove popup instead of removing it: those | |||
| 787 | * extras are easy to lose to a misclick, so the destructive | |||
| 788 | * action requires an explicit Remove choice from the menu. Plain | |||
| 789 | * breakpoints and clicks on bare lines keep the original | |||
| 790 | * add-or-remove-on-click flow. Shift+click keeps the existing | |||
| 791 | * modifier semantics (toggle active / pre-arm disabled). */ | |||
| 792 | bool richBp = false; | |||
| 793 | if (!toggleActive && !codeEditor->filename.isEmpty()) | |||
| 794 | { | |||
| 795 | char *canonical = wslua_debugger_canonical_path(codeEditor->filename.toUtf8().constData()); | |||
| 796 | if (canonical) | |||
| 797 | { | |||
| 798 | bool hasExtras = false; | |||
| 799 | const int32_t state = wslua_debugger_get_breakpoint_state_canonical(canonical, lineNo, &hasExtras); | |||
| 800 | g_free(canonical)(__builtin_object_size ((canonical), 0) != ((size_t) - 1)) ? g_free_sized (canonical, __builtin_object_size ((canonical), 0)) : (g_free ) (canonical); | |||
| 801 | if (state != -1 && hasExtras) | |||
| 802 | { | |||
| 803 | richBp = true; | |||
| 804 | } | |||
| 805 | } | |||
| 806 | } | |||
| 807 | ||||
| 808 | if (richBp) | |||
| 809 | { | |||
| 810 | QPoint globalPos = event->globalPosition().toPoint(); | |||
| 811 | emit codeEditor->breakpointGutterMenuRequested(codeEditor->filename, lineNo, globalPos); | |||
| 812 | } | |||
| 813 | else | |||
| 814 | { | |||
| 815 | emit codeEditor->breakpointToggled(codeEditor->filename, lineNo, toggleActive); | |||
| 816 | codeEditor->viewport()->update(); | |||
| 817 | update(); | |||
| 818 | } | |||
| 819 | ||||
| 820 | event->accept(); | |||
| 821 | } | |||
| 822 | ||||
| 823 | void LineNumberArea::contextMenuEvent(QContextMenuEvent *event) | |||
| 824 | { | |||
| 825 | /* Secondary-click contract on the gutter: always pop the | |||
| 826 | * Edit / Disable / Remove menu when the gesture lands on an | |||
| 827 | * existing breakpoint, regardless of whether it carries extras. | |||
| 828 | * This is the canonical "second click" affordance — works on | |||
| 829 | * Windows / Linux right-click, macOS Ctrl-click and the macOS | |||
| 830 | * two-finger trackpad tap, all of which Qt funnels through | |||
| 831 | * QContextMenuEvent. Bare lines have nothing to act on; we let | |||
| 832 | * the event through so the parent editor (or its viewport) can | |||
| 833 | * decide what to do (currently nothing in that area). */ | |||
| 834 | const QPoint pos = event->pos(); | |||
| 835 | if (pos.x() <= width() - 20 || codeEditor->filename.isEmpty()) | |||
| 836 | { | |||
| 837 | QWidget::contextMenuEvent(event); | |||
| 838 | return; | |||
| 839 | } | |||
| 840 | ||||
| 841 | const qint32 lineNo = lineAtY(pos.y()); | |||
| 842 | if (lineNo < 1) | |||
| 843 | { | |||
| 844 | QWidget::contextMenuEvent(event); | |||
| 845 | return; | |||
| 846 | } | |||
| 847 | ||||
| 848 | char *canonical = wslua_debugger_canonical_path(codeEditor->filename.toUtf8().constData()); | |||
| 849 | if (!canonical) | |||
| 850 | { | |||
| 851 | QWidget::contextMenuEvent(event); | |||
| 852 | return; | |||
| 853 | } | |||
| 854 | const int32_t state = wslua_debugger_get_breakpoint_state_canonical(canonical, lineNo, /*has_extras=*/nullptr); | |||
| 855 | g_free(canonical)(__builtin_object_size ((canonical), 0) != ((size_t) - 1)) ? g_free_sized (canonical, __builtin_object_size ((canonical), 0)) : (g_free ) (canonical); | |||
| 856 | if (state == -1) | |||
| 857 | { | |||
| 858 | QWidget::contextMenuEvent(event); | |||
| 859 | return; | |||
| 860 | } | |||
| 861 | ||||
| 862 | emit codeEditor->breakpointGutterMenuRequested(codeEditor->filename, lineNo, event->globalPos()); | |||
| 863 | event->accept(); | |||
| 864 | } | |||
| 865 | ||||
| 866 | /* ===== font_policy ===== */ | |||
| 867 | ||||
| 868 | void LuaDebuggerFontPolicy::attach(QTabWidget *codeTabs, QTreeView *variablesTree, QTreeView *watchTree, | |||
| 869 | QStandardItemModel *watchModel, QTreeView *stackTree, QTreeView *fileTree, | |||
| 870 | QTreeView *breakpointsTree, QPlainTextEdit *evalInputEdit, | |||
| 871 | QPlainTextEdit *evalOutputEdit) | |||
| 872 | { | |||
| 873 | codeTabs_ = codeTabs; | |||
| 874 | variablesTree_ = variablesTree; | |||
| 875 | watchTree_ = watchTree; | |||
| 876 | watchModel_ = watchModel; | |||
| 877 | stackTree_ = stackTree; | |||
| 878 | fileTree_ = fileTree; | |||
| 879 | breakpointsTree_ = breakpointsTree; | |||
| 880 | evalInputEdit_ = evalInputEdit; | |||
| 881 | evalOutputEdit_ = evalOutputEdit; | |||
| 882 | } | |||
| 883 | ||||
| 884 | void LuaDebuggerFontPolicy::applyAll() | |||
| 885 | { | |||
| 886 | applyToCodeEditors(monospaceFont(true)); | |||
| 887 | applyToPanels(); | |||
| 888 | } | |||
| 889 | ||||
| 890 | void LuaDebuggerFontPolicy::applyToCodeEditors(const QFont &font) | |||
| 891 | { | |||
| 892 | QFont effective = font; | |||
| 893 | if (effective.family().isEmpty()) | |||
| 894 | { | |||
| 895 | effective = monospaceFont(true); | |||
| 896 | } | |||
| 897 | ||||
| 898 | if (!codeTabs_) | |||
| 899 | { | |||
| 900 | return; | |||
| 901 | } | |||
| 902 | const qint32 tabCount = static_cast<qint32>(codeTabs_->count()); | |||
| 903 | for (qint32 tabIndex = 0; tabIndex < tabCount; ++tabIndex) | |||
| 904 | { | |||
| 905 | LuaDebuggerCodeView *view = qobject_cast<LuaDebuggerCodeView *>(codeTabs_->widget(static_cast<int>(tabIndex))); | |||
| 906 | if (view) | |||
| 907 | { | |||
| 908 | view->setEditorFont(effective); | |||
| 909 | } | |||
| 910 | } | |||
| 911 | } | |||
| 912 | ||||
| 913 | void LuaDebuggerFontPolicy::applyToPanels() | |||
| 914 | { | |||
| 915 | const QFont panelMono = monospaceFont(false); | |||
| 916 | const QFont headerFont = regularFont(); | |||
| 917 | ||||
| 918 | const QList<QWidget *> widgets = {variablesTree_, watchTree_, stackTree_, | |||
| 919 | breakpointsTree_, evalInputEdit_, evalOutputEdit_}; | |||
| 920 | for (QWidget *widget : widgets) | |||
| 921 | { | |||
| 922 | if (widget) | |||
| 923 | { | |||
| 924 | widget->setFont(panelMono); | |||
| 925 | } | |||
| 926 | } | |||
| 927 | ||||
| 928 | const QList<QTreeView *> treesWithStandardHeaders = {variablesTree_, watchTree_, stackTree_, fileTree_, | |||
| 929 | breakpointsTree_}; | |||
| 930 | for (QTreeView *tree : treesWithStandardHeaders) | |||
| 931 | { | |||
| 932 | if (tree && tree->header()) | |||
| 933 | { | |||
| 934 | tree->header()->setFont(headerFont); | |||
| 935 | } | |||
| 936 | } | |||
| 937 | reapplyToWatchItemModel(); | |||
| 938 | } | |||
| 939 | ||||
| 940 | namespace | |||
| 941 | { | |||
| 942 | ||||
| 943 | /* Walk the watch QStandardItemModel tree: set each item's QFont to the | |||
| 944 | * panel monospace while preserving the existing bold bit (change- | |||
| 945 | * highlight) so it wins over the tree widget font after row moves. */ | |||
| 946 | // NOLINTNEXTLINE(misc-no-recursion) | |||
| 947 | void reapplyMonospaceToWatchItemModelRecursive(const QFont &base, QStandardItemModel *m, const QModelIndex &parent) | |||
| 948 | { | |||
| 949 | if (!m) | |||
| 950 | { | |||
| 951 | return; | |||
| 952 | } | |||
| 953 | const int rows = m->rowCount(parent); | |||
| 954 | const int cols = m->columnCount(parent); | |||
| 955 | for (int r = 0; r < rows; ++r) | |||
| 956 | { | |||
| 957 | for (int c = 0; c < cols; ++c) | |||
| 958 | { | |||
| 959 | const QModelIndex idx = m->index(r, c, parent); | |||
| 960 | if (QStandardItem *it = m->itemFromIndex(idx)) | |||
| 961 | { | |||
| 962 | QFont f = base; | |||
| 963 | f.setBold(it->font().bold()); | |||
| 964 | it->setFont(f); | |||
| 965 | } | |||
| 966 | } | |||
| 967 | const QModelIndex specIndex = m->index(r, WatchColumn::Spec, parent); | |||
| 968 | if (specIndex.isValid() && m->rowCount(specIndex) > 0) | |||
| 969 | { | |||
| 970 | reapplyMonospaceToWatchItemModelRecursive(base, m, specIndex); | |||
| 971 | } | |||
| 972 | } | |||
| 973 | } | |||
| 974 | ||||
| 975 | } // namespace | |||
| 976 | ||||
| 977 | void LuaDebuggerFontPolicy::reapplyToWatchItemModel() | |||
| 978 | { | |||
| 979 | if (!watchModel_) | |||
| 980 | { | |||
| 981 | return; | |||
| 982 | } | |||
| 983 | reapplyMonospaceToWatchItemModelRecursive(monospaceFont(false), watchModel_, QModelIndex()); | |||
| 984 | if (watchTree_) | |||
| 985 | { | |||
| 986 | watchTree_->update(); | |||
| 987 | } | |||
| 988 | } | |||
| 989 | ||||
| 990 | // TODO: LuaDebuggerFontPolicy duplicates FontManager (font resolution + the | |||
| 991 | // app-ready fallback). Drop it and have callers use the FontManager directly. | |||
| 992 | QFont LuaDebuggerFontPolicy::monospaceFont(bool zoomed) const | |||
| 993 | { | |||
| 994 | /* Monospace font for panels and the script editor. */ | |||
| 995 | if (mainApp && mainApp->isInitialized()) | |||
| 996 | { | |||
| 997 | return (zoomed ? FontManager::zoomedMonospaceFont() : FontManager::monospaceFont()); | |||
| 998 | } | |||
| 999 | ||||
| 1000 | /* Fall back to system fixed font */ | |||
| 1001 | return QFontDatabase::systemFont(QFontDatabase::FixedFont); | |||
| 1002 | } | |||
| 1003 | ||||
| 1004 | QFont LuaDebuggerFontPolicy::regularFont() const | |||
| 1005 | { | |||
| 1006 | if (mainApp && mainApp->isInitialized()) | |||
| 1007 | { | |||
| 1008 | return mainApp->font(); | |||
| 1009 | } | |||
| 1010 | return QGuiApplication::font(); | |||
| 1011 | } | |||
| 1012 | ||||
| 1013 | /* ===== code_tabs_controller ===== */ | |||
| 1014 | ||||
| 1015 | LuaDebuggerCodeTabsController::LuaDebuggerCodeTabsController(LuaDebuggerDialog *host) : QObject(host), host_(host) {} | |||
| 1016 | ||||
| 1017 | void LuaDebuggerCodeTabsController::attach(QTabWidget *tabs) | |||
| 1018 | { | |||
| 1019 | tabs_ = tabs; | |||
| 1020 | } | |||
| 1021 | ||||
| 1022 | void LuaDebuggerDialog::wireCodeTabs() | |||
| 1023 | { | |||
| 1024 | codeTabsController_.attach(ui->codeTabWidget); | |||
| 1025 | ||||
| 1026 | connect(ui->actionSaveFile, &QAction::triggered, &codeTabsController_, &LuaDebuggerCodeTabsController::onSaveFile); | |||
| 1027 | ||||
| 1028 | connect(ui->codeTabWidget, &QTabWidget::tabCloseRequested, &codeTabsController_, | |||
| 1029 | &LuaDebuggerCodeTabsController::onTabCloseRequested); | |||
| 1030 | connect(ui->codeTabWidget, &QTabWidget::currentChanged, &codeTabsController_, | |||
| 1031 | &LuaDebuggerCodeTabsController::onCurrentTabChanged); | |||
| 1032 | } | |||
| 1033 | ||||
| 1034 | QString LuaDebuggerCodeTabsController::lastOpenDirectory() | |||
| 1035 | { | |||
| 1036 | if (lastOpenDirectory_.isEmpty()) | |||
| 1037 | { | |||
| 1038 | lastOpenDirectory_ = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); | |||
| 1039 | if (lastOpenDirectory_.isEmpty()) | |||
| 1040 | { | |||
| 1041 | lastOpenDirectory_ = QDir::homePath(); | |||
| 1042 | } | |||
| 1043 | } | |||
| 1044 | return lastOpenDirectory_; | |||
| 1045 | } | |||
| 1046 | ||||
| 1047 | void LuaDebuggerCodeTabsController::setLastOpenDirectory(const QString &dir) | |||
| 1048 | { | |||
| 1049 | lastOpenDirectory_ = dir; | |||
| 1050 | } | |||
| 1051 | ||||
| 1052 | LuaDebuggerCodeView *LuaDebuggerCodeTabsController::loadFile(const QString &file_path) | |||
| 1053 | { | |||
| 1054 | if (!host_ || !tabs_) | |||
| 1055 | { | |||
| 1056 | return nullptr; | |||
| 1057 | } | |||
| 1058 | ||||
| 1059 | QString normalizedPath = host_->normalizedFilePath(file_path); | |||
| 1060 | if (normalizedPath.isEmpty()) | |||
| 1061 | { | |||
| 1062 | normalizedPath = file_path; | |||
| 1063 | } | |||
| 1064 | ||||
| 1065 | /* Check if file exists before creating a tab. */ | |||
| 1066 | QFileInfo fileInfo(normalizedPath); | |||
| 1067 | if (!fileInfo.exists() || !fileInfo.isFile()) | |||
| 1068 | { | |||
| 1069 | return nullptr; | |||
| 1070 | } | |||
| 1071 | ||||
| 1072 | const qint32 existingTabCount = static_cast<qint32>(tabs_->count()); | |||
| 1073 | for (qint32 tabIndex = 0; tabIndex < existingTabCount; ++tabIndex) | |||
| 1074 | { | |||
| 1075 | LuaDebuggerCodeView *view = qobject_cast<LuaDebuggerCodeView *>(tabs_->widget(static_cast<int>(tabIndex))); | |||
| 1076 | if (view && view->getFilename() == normalizedPath) | |||
| 1077 | { | |||
| 1078 | tabs_->setCurrentIndex(static_cast<int>(tabIndex)); | |||
| 1079 | return view; | |||
| 1080 | } | |||
| 1081 | } | |||
| 1082 | ||||
| 1083 | LuaDebuggerCodeView *codeView = new LuaDebuggerCodeView(tabs_); | |||
| 1084 | codeView->setEditorFont(host_->fontPolicy().monospaceFont(true)); | |||
| 1085 | codeView->setFilename(normalizedPath); | |||
| 1086 | /* A tab opened while paused (e.g. by selecting a stack frame) must not | |||
| 1087 | * be editable either. */ | |||
| 1088 | codeView->setReadOnly(host_->isInPausedStack(normalizedPath)); | |||
| 1089 | ||||
| 1090 | QFile file(normalizedPath); | |||
| 1091 | if (file.open(QIODevice::ReadOnly | QIODevice::Text)) | |||
| 1092 | { | |||
| 1093 | codeView->setPlainText(file.readAll()); | |||
| 1094 | } | |||
| 1095 | else | |||
| 1096 | { | |||
| 1097 | /* This should not happen since we checked exists() above, | |||
| 1098 | * but handle it gracefully just in case. */ | |||
| 1099 | delete codeView; | |||
| 1100 | return nullptr; | |||
| 1101 | } | |||
| 1102 | ||||
| 1103 | host_->filesController().ensureEntry(normalizedPath); | |||
| 1104 | ||||
| 1105 | codeView->setContextMenuPolicy(Qt::CustomContextMenu); | |||
| 1106 | connect(codeView, &QWidget::customContextMenuRequested, host_, &LuaDebuggerDialog::onCodeViewContextMenu); | |||
| 1107 | ||||
| 1108 | /* Queued connection: the gutter emits this from inside its | |||
| 1109 | * mousePressEvent, so dispatching the popup via a queued slot | |||
| 1110 | * lets the press event fully unwind before QMenu::exec() spins | |||
| 1111 | * its own nested loop. Avoids the "press grab still attached" | |||
| 1112 | * artefacts that bite when a modal popup is opened directly out | |||
| 1113 | * of a press handler. */ | |||
| 1114 | connect(codeView, &LuaDebuggerCodeView::breakpointGutterMenuRequested, &host_->breakpointsController(), | |||
| 1115 | &LuaDebuggerBreakpointsController::showGutterMenu, Qt::QueuedConnection); | |||
| 1116 | ||||
| 1117 | connect(codeView, &LuaDebuggerCodeView::breakpointToggled, this, | |||
| 1118 | &LuaDebuggerCodeTabsController::onCodeViewBreakpointToggled); | |||
| 1119 | ||||
| 1120 | connect(codeView, &LuaDebuggerCodeView::breakpointMoveRequested, &host_->breakpointsController(), | |||
| 1121 | &LuaDebuggerBreakpointsController::moveAtLine, Qt::QueuedConnection); | |||
| 1122 | ||||
| 1123 | connect(codeView, &LuaDebuggerCodeView::readOnlyEditAttempted, host_, &LuaDebuggerDialog::showReadOnlyWarning); | |||
| 1124 | connect(codeView, &LuaDebuggerCodeView::editorLinesChanged, &host_->breakpointsController(), | |||
| 1125 | &LuaDebuggerBreakpointsController::onEditorLinesChanged); | |||
| 1126 | ||||
| 1127 | connect(codeView->document(), &QTextDocument::modificationChanged, this, | |||
| 1128 | [this, codeView]() | |||
| 1129 | { | |||
| 1130 | updateTabTextForCodeView(codeView); | |||
| 1131 | updateWindowModifiedState(); | |||
| 1132 | if (tabs_ && tabs_->currentWidget() == codeView) | |||
| 1133 | { | |||
| 1134 | updateSaveActionState(); | |||
| 1135 | host_->updateEditorWarning(); | |||
| 1136 | } | |||
| 1137 | }); | |||
| 1138 | connect(codeView, &QPlainTextEdit::cursorPositionChanged, &host_->breakpointsController(), | |||
| 1139 | &LuaDebuggerBreakpointsController::updateHeaderButtonState); | |||
| 1140 | ||||
| 1141 | tabs_->addTab(codeView, QFileInfo(normalizedPath).fileName()); | |||
| 1142 | updateTabTextForCodeView(codeView); | |||
| 1143 | tabs_->setCurrentWidget(codeView); | |||
| 1144 | tabs_->show(); | |||
| 1145 | updateSaveActionState(); | |||
| 1146 | updateWindowModifiedState(); | |||
| 1147 | host_->updateLuaEditorAuxFrames(); | |||
| 1148 | return codeView; | |||
| 1149 | } | |||
| 1150 | ||||
| 1151 | LuaDebuggerCodeView *LuaDebuggerCodeTabsController::currentCodeView() const | |||
| 1152 | { | |||
| 1153 | if (!tabs_) | |||
| 1154 | { | |||
| 1155 | return nullptr; | |||
| 1156 | } | |||
| 1157 | return qobject_cast<LuaDebuggerCodeView *>(tabs_->currentWidget()); | |||
| 1158 | } | |||
| 1159 | ||||
| 1160 | qint32 LuaDebuggerCodeTabsController::unsavedOpenScriptTabCount() const | |||
| 1161 | { | |||
| 1162 | if (!tabs_) | |||
| 1163 | { | |||
| 1164 | return 0; | |||
| 1165 | } | |||
| 1166 | qint32 count = 0; | |||
| 1167 | const qint32 tabCount = static_cast<qint32>(tabs_->count()); | |||
| 1168 | for (qint32 tabIndex = 0; tabIndex < tabCount; ++tabIndex) | |||
| 1169 | { | |||
| 1170 | LuaDebuggerCodeView *view = qobject_cast<LuaDebuggerCodeView *>(tabs_->widget(static_cast<int>(tabIndex))); | |||
| 1171 | if (view && view->document()->isModified()) | |||
| 1172 | { | |||
| 1173 | ++count; | |||
| 1174 | } | |||
| 1175 | } | |||
| 1176 | return count; | |||
| 1177 | } | |||
| 1178 | ||||
| 1179 | bool LuaDebuggerCodeTabsController::hasUnsavedChanges() const | |||
| 1180 | { | |||
| 1181 | return unsavedOpenScriptTabCount() > 0; | |||
| 1182 | } | |||
| 1183 | ||||
| 1184 | bool LuaDebuggerCodeTabsController::ensureUnsavedChangesHandled(const QString &title) | |||
| 1185 | { | |||
| 1186 | if (!hasUnsavedChanges()) | |||
| 1187 | { | |||
| 1188 | return true; | |||
| 1189 | } | |||
| 1190 | ||||
| 1191 | const qint32 unsavedCount = unsavedOpenScriptTabCount(); | |||
| 1192 | const QMessageBox::StandardButton reply = | |||
| 1193 | QMessageBox::question(host_, title, tr("There are unsaved changes in %Ln open file(s).", "", unsavedCount), | |||
| 1194 | QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save); | |||
| 1195 | ||||
| 1196 | if (reply == QMessageBox::Cancel) | |||
| 1197 | { | |||
| 1198 | return false; | |||
| 1199 | } | |||
| 1200 | if (reply == QMessageBox::Save) | |||
| 1201 | { | |||
| 1202 | return saveAllModified(); | |||
| 1203 | } | |||
| 1204 | clearAllDocumentModified(); | |||
| 1205 | return true; | |||
| 1206 | } | |||
| 1207 | ||||
| 1208 | void LuaDebuggerCodeTabsController::clearAllDocumentModified() | |||
| 1209 | { | |||
| 1210 | if (!tabs_) | |||
| 1211 | { | |||
| 1212 | return; | |||
| 1213 | } | |||
| 1214 | const qint32 tabCount = static_cast<qint32>(tabs_->count()); | |||
| 1215 | for (qint32 tabIndex = 0; tabIndex < tabCount; ++tabIndex) | |||
| 1216 | { | |||
| 1217 | LuaDebuggerCodeView *view = qobject_cast<LuaDebuggerCodeView *>(tabs_->widget(static_cast<int>(tabIndex))); | |||
| 1218 | if (view) | |||
| 1219 | { | |||
| 1220 | view->document()->setModified(false); | |||
| 1221 | } | |||
| 1222 | } | |||
| 1223 | } | |||
| 1224 | ||||
| 1225 | bool LuaDebuggerCodeTabsController::saveCodeView(LuaDebuggerCodeView *view) | |||
| 1226 | { | |||
| 1227 | if (!view) | |||
| 1228 | { | |||
| 1229 | return false; | |||
| 1230 | } | |||
| 1231 | const QString path = view->getFilename(); | |||
| 1232 | if (path.isEmpty()) | |||
| 1233 | { | |||
| 1234 | return false; | |||
| 1235 | } | |||
| 1236 | ||||
| 1237 | QFile file(path); | |||
| 1238 | if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) | |||
| 1239 | { | |||
| 1240 | QMessageBox::warning(host_, tr("Save Lua Script"), | |||
| 1241 | tr("Could not write to %1:\n%2").arg(path, file.errorString())); | |||
| 1242 | return false; | |||
| 1243 | } | |||
| 1244 | QTextStream out(&file); | |||
| 1245 | out << view->toPlainText(); | |||
| 1246 | file.close(); | |||
| 1247 | view->document()->setModified(false); | |||
| 1248 | /* The file now differs from the script Lua is running. */ | |||
| 1249 | view->setScriptStale(true); | |||
| 1250 | host_->updateEditorWarning(); | |||
| 1251 | return true; | |||
| 1252 | } | |||
| 1253 | ||||
| 1254 | void LuaDebuggerCodeTabsController::updateReadOnly() | |||
| 1255 | { | |||
| 1256 | if (!tabs_) | |||
| 1257 | { | |||
| 1258 | return; | |||
| 1259 | } | |||
| 1260 | for (int i = 0; i < tabs_->count(); ++i) | |||
| 1261 | { | |||
| 1262 | if (auto *view = qobject_cast<LuaDebuggerCodeView *>(tabs_->widget(i))) | |||
| 1263 | { | |||
| 1264 | view->setReadOnly(host_->isInPausedStack(view->getFilename())); | |||
| 1265 | } | |||
| 1266 | } | |||
| 1267 | } | |||
| 1268 | ||||
| 1269 | void LuaDebuggerCodeTabsController::clearAllScriptStale() | |||
| 1270 | { | |||
| 1271 | if (!tabs_) | |||
| 1272 | { | |||
| 1273 | return; | |||
| 1274 | } | |||
| 1275 | for (int i = 0; i < tabs_->count(); ++i) | |||
| 1276 | { | |||
| 1277 | if (auto *view = qobject_cast<LuaDebuggerCodeView *>(tabs_->widget(i))) | |||
| 1278 | { | |||
| 1279 | view->setScriptStale(false); | |||
| 1280 | } | |||
| 1281 | } | |||
| 1282 | host_->updateEditorWarning(); | |||
| 1283 | } | |||
| 1284 | ||||
| 1285 | bool LuaDebuggerCodeTabsController::saveAllModified() | |||
| 1286 | { | |||
| 1287 | if (!tabs_) | |||
| 1288 | { | |||
| 1289 | return true; | |||
| 1290 | } | |||
| 1291 | const qint32 tabCount = static_cast<qint32>(tabs_->count()); | |||
| 1292 | for (qint32 tabIndex = 0; tabIndex < tabCount; ++tabIndex) | |||
| 1293 | { | |||
| 1294 | LuaDebuggerCodeView *view = qobject_cast<LuaDebuggerCodeView *>(tabs_->widget(static_cast<int>(tabIndex))); | |||
| 1295 | if (view && view->document()->isModified()) | |||
| 1296 | { | |||
| 1297 | if (!saveCodeView(view)) | |||
| 1298 | { | |||
| 1299 | return false; | |||
| 1300 | } | |||
| 1301 | } | |||
| 1302 | } | |||
| 1303 | return true; | |||
| 1304 | } | |||
| 1305 | ||||
| 1306 | void LuaDebuggerCodeTabsController::updateTabTextForCodeView(LuaDebuggerCodeView *view) | |||
| 1307 | { | |||
| 1308 | if (!view || !tabs_) | |||
| 1309 | { | |||
| 1310 | return; | |||
| 1311 | } | |||
| 1312 | const int tabIndex = tabs_->indexOf(view); | |||
| 1313 | if (tabIndex < 0) | |||
| 1314 | { | |||
| 1315 | return; | |||
| 1316 | } | |||
| 1317 | QString label = QFileInfo(view->getFilename()).fileName(); | |||
| 1318 | if (view->document()->isModified()) | |||
| 1319 | { | |||
| 1320 | label += QStringLiteral(" *")(QString(QtPrivate::qMakeStringPrivate(u"" " *"))); | |||
| 1321 | } | |||
| 1322 | tabs_->setTabText(tabIndex, label); | |||
| 1323 | } | |||
| 1324 | ||||
| 1325 | void LuaDebuggerCodeTabsController::updateSaveActionState() | |||
| 1326 | { | |||
| 1327 | if (!host_) | |||
| 1328 | { | |||
| 1329 | return; | |||
| 1330 | } | |||
| 1331 | LuaDebuggerCodeView *view = currentCodeView(); | |||
| 1332 | host_->setSaveActionEnabled(view && view->document()->isModified()); | |||
| 1333 | } | |||
| 1334 | ||||
| 1335 | void LuaDebuggerCodeTabsController::updateWindowModifiedState() | |||
| 1336 | { | |||
| 1337 | if (!host_) | |||
| 1338 | { | |||
| 1339 | return; | |||
| 1340 | } | |||
| 1341 | host_->setWindowModified(hasUnsavedChanges()); | |||
| 1342 | } | |||
| 1343 | ||||
| 1344 | void LuaDebuggerCodeTabsController::openInitialBreakpointFiles(const QVector<QString> &files) | |||
| 1345 | { | |||
| 1346 | for (const QString &path : files) | |||
| 1347 | { | |||
| 1348 | loadFile(path); | |||
| 1349 | } | |||
| 1350 | } | |||
| 1351 | ||||
| 1352 | void LuaDebuggerCodeTabsController::clearAllCodeHighlights() | |||
| 1353 | { | |||
| 1354 | if (!tabs_) | |||
| 1355 | { | |||
| 1356 | return; | |||
| 1357 | } | |||
| 1358 | const qint32 tabCount = static_cast<qint32>(tabs_->count()); | |||
| 1359 | for (qint32 tabIndex = 0; tabIndex < tabCount; ++tabIndex) | |||
| 1360 | { | |||
| 1361 | LuaDebuggerCodeView *view = qobject_cast<LuaDebuggerCodeView *>(tabs_->widget(static_cast<int>(tabIndex))); | |||
| 1362 | if (view) | |||
| 1363 | { | |||
| 1364 | view->clearCurrentLineHighlight(); | |||
| 1365 | } | |||
| 1366 | } | |||
| 1367 | } | |||
| 1368 | ||||
| 1369 | void LuaDebuggerCodeTabsController::applyThemeToAllTabs() | |||
| 1370 | { | |||
| 1371 | if (!tabs_) | |||
| 1372 | { | |||
| 1373 | return; | |||
| 1374 | } | |||
| 1375 | const qint32 tabCount = static_cast<qint32>(tabs_->count()); | |||
| 1376 | for (qint32 tabIndex = 0; tabIndex < tabCount; ++tabIndex) | |||
| 1377 | { | |||
| 1378 | LuaDebuggerCodeView *view = qobject_cast<LuaDebuggerCodeView *>(tabs_->widget(static_cast<int>(tabIndex))); | |||
| 1379 | if (view) | |||
| 1380 | { | |||
| 1381 | view->applyTheme(); | |||
| 1382 | } | |||
| 1383 | } | |||
| 1384 | } | |||
| 1385 | ||||
| 1386 | void LuaDebuggerCodeTabsController::onSaveFile() | |||
| 1387 | { | |||
| 1388 | LuaDebuggerCodeView *view = currentCodeView(); | |||
| 1389 | if (!view || !view->document()->isModified()) | |||
| 1390 | { | |||
| 1391 | return; | |||
| 1392 | } | |||
| 1393 | saveCodeView(view); | |||
| 1394 | updateSaveActionState(); | |||
| 1395 | } | |||
| 1396 | ||||
| 1397 | void LuaDebuggerCodeTabsController::onCurrentTabChanged(int index) | |||
| 1398 | { | |||
| 1399 | Q_UNUSED(index)(void)index;; | |||
| 1400 | if (!host_) | |||
| 1401 | { | |||
| 1402 | return; | |||
| 1403 | } | |||
| 1404 | updateSaveActionState(); | |||
| 1405 | host_->updateLuaEditorAuxFrames(); | |||
| 1406 | host_->updateEditorWarning(); | |||
| 1407 | host_->breakpointsController().updateHeaderButtonState(); | |||
| 1408 | host_->updateContinueActionState(); | |||
| 1409 | } | |||
| 1410 | ||||
| 1411 | void LuaDebuggerCodeTabsController::onTabCloseRequested(int index) | |||
| 1412 | { | |||
| 1413 | if (!host_ || !tabs_) | |||
| 1414 | { | |||
| 1415 | return; | |||
| 1416 | } | |||
| 1417 | QWidget *widget = tabs_->widget(index); | |||
| 1418 | auto *view = qobject_cast<LuaDebuggerCodeView *>(widget); | |||
| 1419 | if (view && view->document()->isModified()) | |||
| 1420 | { | |||
| 1421 | const QMessageBox::StandardButton reply = QMessageBox::question( | |||
| 1422 | host_, tr("Lua Debugger"), | |||
| 1423 | tr("Save changes to %1 before closing?").arg(QFileInfo(view->getFilename()).fileName()), | |||
| 1424 | QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save); | |||
| 1425 | if (reply == QMessageBox::Cancel) | |||
| 1426 | { | |||
| 1427 | return; | |||
| 1428 | } | |||
| 1429 | if (reply == QMessageBox::Save) | |||
| 1430 | { | |||
| 1431 | if (!saveCodeView(view)) | |||
| 1432 | { | |||
| 1433 | return; | |||
| 1434 | } | |||
| 1435 | } | |||
| 1436 | else | |||
| 1437 | { | |||
| 1438 | view->document()->setModified(false); | |||
| 1439 | } | |||
| 1440 | } | |||
| 1441 | ||||
| 1442 | tabs_->removeTab(index); | |||
| 1443 | delete widget; | |||
| 1444 | updateSaveActionState(); | |||
| 1445 | updateWindowModifiedState(); | |||
| 1446 | } | |||
| 1447 | ||||
| 1448 | void LuaDebuggerCodeTabsController::onCodeViewBreakpointToggled(const QString &file_path, qint32 line, bool toggleActive) | |||
| 1449 | { | |||
| 1450 | if (!host_) | |||
| 1451 | { | |||
| 1452 | return; | |||
| 1453 | } | |||
| 1454 | /* Two distinct gestures share this code-view signal: | |||
| 1455 | * | |||
| 1456 | * - Plain click (toggleActive=false) — F9 / gutter add+remove. The | |||
| 1457 | * breakpoint is created active and the debugger is auto-enabled | |||
| 1458 | * so the new row is immediately effective. | |||
| 1459 | * - Shift+click (toggleActive=true) — pre-arm / disarm without | |||
| 1460 | * destroying the row, so the user can park breakpoints on lines | |||
| 1461 | * they intend to revisit later. Never auto-enables the debugger; | |||
| 1462 | * that would defeat the "no surprise" intent. | |||
| 1463 | * | |||
| 1464 | * Both gestures dispatch through @ref LuaDebuggerBreakpointsController | |||
| 1465 | * so the chrome refresh (table rebuild, gutter markers, header dot) | |||
| 1466 | * is identical regardless of the entry point. */ | |||
| 1467 | if (toggleActive) | |||
| 1468 | { | |||
| 1469 | host_->breakpointsController().shiftToggleAtLine(file_path, line); | |||
| 1470 | } | |||
| 1471 | else | |||
| 1472 | { | |||
| 1473 | host_->breakpointsController().toggleAtLine(file_path, line); | |||
| 1474 | } | |||
| 1475 | } |