1/*
2 SPDX-FileCopyrightText: 2001-2003 Christoph Cullmann <cullmann@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7// BEGIN Includes
8#include "katehighlightmenu.h"
9
10#include "katedocument.h"
11#include "katesyntaxmanager.h"
12
13#include <KLocalizedString>
14
15#include <QActionGroup>
16#include <QMenu>
17// END Includes
18
19void KateHighlightingMenu::init()
20{
21 m_doc = nullptr;
22
23 connect(sender: menu(), signal: &QMenu::aboutToShow, context: this, slot: &KateHighlightingMenu::slotAboutToShow);
24 m_actionGroup = new QActionGroup(menu());
25}
26
27void KateHighlightingMenu::updateMenu(KTextEditor::DocumentPrivate *doc)
28{
29 m_doc = doc;
30}
31
32void KateHighlightingMenu::slotAboutToShow()
33{
34 const auto modeList = KateHlManager::self()->modeList();
35 for (const auto &hl : modeList) {
36 QString hlName = hl.translatedName();
37 QString hlSection = hl.translatedSection();
38 if (hlName == QLatin1String("None")) {
39 hlName = i18n("None");
40 }
41
42 if (!hl.isHidden() && !hlName.isEmpty()) {
43 const bool namesHaveHlName = std::find(first: names.begin(), last: names.end(), val: hlName) != names.end();
44
45 if (!hlSection.isEmpty() && !namesHaveHlName) {
46 auto it = std::find(first: subMenusName.begin(), last: subMenusName.end(), val: hlSection);
47 if (it == subMenusName.end()) {
48 subMenusName.push_back(x: hlSection);
49 // pass proper parent for cleanup + Wayland correctness
50 subMenus.emplace_back(args: new QMenu(QLatin1Char('&') + hlSection, menu()));
51 menu()->addMenu(menu: subMenus.back());
52
53 // last element is the one we just inserted
54 it = --subMenusName.end();
55 }
56
57 const auto m = std::distance(first: subMenusName.begin(), last: it);
58 names.push_back(x: hlName);
59 QAction *a = subMenus.at(n: m)->addAction(text: QLatin1Char('&') + hlName, receiver: this, SLOT(setHl()));
60 m_actionGroup->addAction(a);
61 a->setData(hl.name());
62 a->setCheckable(true);
63 subActions.push_back(x: a);
64 } else if (!namesHaveHlName) {
65 names.push_back(x: hlName);
66 QAction *a = menu()->addAction(text: QLatin1Char('&') + hlName, receiver: this, SLOT(setHl()));
67 m_actionGroup->addAction(a);
68 a->setData(hl.name());
69 a->setCheckable(true);
70 subActions.push_back(x: a);
71 }
72 }
73 }
74
75 if (!m_doc) {
76 return;
77 }
78 const QString mode = m_doc->highlightingMode();
79 for (auto subAction : subActions) {
80 subAction->setChecked(subAction->data().toString() == mode);
81 }
82}
83
84void KateHighlightingMenu::setHl()
85{
86 if (!m_doc || !sender()) {
87 return;
88 }
89 QAction *action = qobject_cast<QAction *>(object: sender());
90 if (!action) {
91 return;
92 }
93 QString mode = action->data().toString();
94 m_doc->setHighlightingMode(mode);
95
96 // use change, honor this
97 m_doc->setDontChangeHlOnSave();
98}
99
100#include "moc_katehighlightmenu.cpp"
101

source code of ktexteditor/src/syntax/katehighlightmenu.cpp