From 71ac13e9bfb676a461656fd0333a9da83b850ddb Mon Sep 17 00:00:00 2001 From: Louis de Carufel Date: Thu, 6 Jun 2024 08:40:54 -0400 Subject: [PATCH] Bookmarks are now listed in menu. --- qrenderdoc/Windows/ShaderViewer.cpp | 65 ++++++++++++++++++++++++++--- qrenderdoc/Windows/ShaderViewer.h | 4 ++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/qrenderdoc/Windows/ShaderViewer.cpp b/qrenderdoc/Windows/ShaderViewer.cpp index 00d9129ec..87f83849a 100644 --- a/qrenderdoc/Windows/ShaderViewer.cpp +++ b/qrenderdoc/Windows/ShaderViewer.cpp @@ -1217,16 +1217,69 @@ void ShaderViewer::ConfigureBookmarkMenu() QObject::connect(clearAction, &QAction::triggered, [this]() { ClearAllBookmarks(); }); bookmarkMenu->addAction(clearAction); - QObject::connect(bookmarkMenu, &QMenu::aboutToShow, [this, nextAction, prevAction, clearAction]() { - const bool hasBookmarks = HasBookmarks(); - nextAction->setEnabled(hasBookmarks); - prevAction->setEnabled(hasBookmarks); - clearAction->setEnabled(hasBookmarks); - }); + QObject::connect(bookmarkMenu, &QMenu::aboutToShow, + [this, bookmarkMenu, nextAction, prevAction, clearAction]() { + UpdateBookmarkMenu(bookmarkMenu, nextAction, prevAction, clearAction); + }); + bookmarkMenu->addSeparator(); ui->bookmark->setMenu(bookmarkMenu); } +void ShaderViewer::UpdateBookmarkMenu(QMenu *menu, QAction *nextAction, QAction *prevAction, + QAction *clearAction) +{ + // setup permanent actions + const bool hasBookmarks = HasBookmarks(); + nextAction->setEnabled(hasBookmarks); + prevAction->setEnabled(hasBookmarks); + clearAction->setEnabled(hasBookmarks); + + // remove current bookmark list + QList actions = menu->actions(); + for(auto itAction = actions.rbegin(); itAction != actions.rend() && !(*itAction)->isSeparator(); + ++itAction) + menu->removeAction(*itAction); + + // populate bookmark list + ScintillaEdit *cur = currentScintilla(); + if(!cur) + return; + + auto itBookmarks = m_Bookmarks.find(cur); + if(itBookmarks == m_Bookmarks.end()) + return; + + QString filename; + if(cur == m_DisassemblyView) + filename = m_DisassemblyFrame->windowTitle(); + else + filename = cur->windowTitle(); + + int numAddedBookmarks = 0; + for(sptr_t lineNumber : *itBookmarks) + { + QString textLine = QString::fromUtf8(cur->getLine(lineNumber)).simplified(); + if(textLine.size() > BOOKMARK_MAX_MENU_ENTRY_LENGTH) + { + textLine.chop(textLine.size() - BOOKMARK_MAX_MENU_ENTRY_LENGTH); + textLine.append(lit("...")); + } + else if(textLine.isEmpty()) + { + textLine = lit("(empty)"); + } + + QString name = QFormatStr("%1:%2 - %3").arg(filename).arg(lineNumber + 1).arg(textLine); + QAction *action = new QAction(name, menu); + QObject::connect(action, &QAction::triggered, [cur, lineNumber]() { cur->gotoLine(lineNumber); }); + menu->addAction(action); + + if(++numAddedBookmarks >= BOOKMARK_MAX_MENU_ENTRY_COUNT) + break; + } +} + QAction *ShaderViewer::MakeExecuteAction(QString name, const QIcon &icon, QString tooltip, QKeySequence shortcut) { diff --git a/qrenderdoc/Windows/ShaderViewer.h b/qrenderdoc/Windows/ShaderViewer.h index 71cde2a70..78792ac06 100644 --- a/qrenderdoc/Windows/ShaderViewer.h +++ b/qrenderdoc/Windows/ShaderViewer.h @@ -219,6 +219,8 @@ private: void MarkModification(); void ConfigureBookmarkMenu(); + void UpdateBookmarkMenu(QMenu *menu, QAction *nextAction, QAction *prevAction, + QAction *clearAction); void PopulateCompileTools(); void PopulateCompileToolParameters(); @@ -336,6 +338,8 @@ private: QList> m_FindAllResults; + static const int BOOKMARK_MAX_MENU_ENTRY_LENGTH = 40; // max length of bookmark names in menu + static const int BOOKMARK_MAX_MENU_ENTRY_COUNT = 30; // max number of bookmarks listed in menu QMap> m_Bookmarks; static const int CURRENT_MARKER = 0;