File indexing completed on 2024-04-21 03:57:43
0001 /* 0002 SPDX-FileCopyrightText: 2001-2003 Christoph Cullmann <cullmann@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 // BEGIN Includes 0008 #include "katehighlightmenu.h" 0009 0010 #include "katedocument.h" 0011 #include "katesyntaxmanager.h" 0012 0013 #include <KLocalizedString> 0014 0015 #include <QActionGroup> 0016 #include <QMenu> 0017 // END Includes 0018 0019 void KateHighlightingMenu::init() 0020 { 0021 m_doc = nullptr; 0022 0023 connect(menu(), &QMenu::aboutToShow, this, &KateHighlightingMenu::slotAboutToShow); 0024 m_actionGroup = new QActionGroup(menu()); 0025 } 0026 0027 void KateHighlightingMenu::updateMenu(KTextEditor::DocumentPrivate *doc) 0028 { 0029 m_doc = doc; 0030 } 0031 0032 void KateHighlightingMenu::slotAboutToShow() 0033 { 0034 const auto modeList = KateHlManager::self()->modeList(); 0035 for (const auto &hl : modeList) { 0036 QString hlName = hl.translatedName(); 0037 QString hlSection = hl.translatedSection(); 0038 if (hlName == QLatin1String("None")) { 0039 hlName = i18n("None"); 0040 } 0041 0042 if (!hl.isHidden() && !hlName.isEmpty()) { 0043 const bool namesHaveHlName = std::find(names.begin(), names.end(), hlName) != names.end(); 0044 0045 if (!hlSection.isEmpty() && !namesHaveHlName) { 0046 auto it = std::find(subMenusName.begin(), subMenusName.end(), hlSection); 0047 if (it == subMenusName.end()) { 0048 subMenusName.push_back(hlSection); 0049 // pass proper parent for cleanup + Wayland correctness 0050 subMenus.emplace_back(new QMenu(QLatin1Char('&') + hlSection, menu())); 0051 menu()->addMenu(subMenus.back()); 0052 0053 // last element is the one we just inserted 0054 it = --subMenusName.end(); 0055 } 0056 0057 const auto m = std::distance(subMenusName.begin(), it); 0058 names.push_back(hlName); 0059 QAction *a = subMenus.at(m)->addAction(QLatin1Char('&') + hlName, this, SLOT(setHl())); 0060 m_actionGroup->addAction(a); 0061 a->setData(hl.name()); 0062 a->setCheckable(true); 0063 subActions.push_back(a); 0064 } else if (!namesHaveHlName) { 0065 names.push_back(hlName); 0066 QAction *a = menu()->addAction(QLatin1Char('&') + hlName, this, SLOT(setHl())); 0067 m_actionGroup->addAction(a); 0068 a->setData(hl.name()); 0069 a->setCheckable(true); 0070 subActions.push_back(a); 0071 } 0072 } 0073 } 0074 0075 if (!m_doc) { 0076 return; 0077 } 0078 const QString mode = m_doc->highlightingMode(); 0079 for (auto subAction : subActions) { 0080 subAction->setChecked(subAction->data().toString() == mode); 0081 } 0082 } 0083 0084 void KateHighlightingMenu::setHl() 0085 { 0086 if (!m_doc || !sender()) { 0087 return; 0088 } 0089 QAction *action = qobject_cast<QAction *>(sender()); 0090 if (!action) { 0091 return; 0092 } 0093 QString mode = action->data().toString(); 0094 m_doc->setHighlightingMode(mode); 0095 0096 // use change, honor this 0097 m_doc->setDontChangeHlOnSave(); 0098 } 0099 0100 #include "moc_katehighlightmenu.cpp"