KTextEditor

katehighlightmenu.cpp
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(menu(), &QMenu::aboutToShow, this, &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(names.begin(), names.end(), hlName) != names.end();
44
45 if (!hlSection.isEmpty() && !namesHaveHlName) {
46 auto it = std::find(subMenusName.begin(), subMenusName.end(), hlSection);
47 if (it == subMenusName.end()) {
48 subMenusName.push_back(hlSection);
49 // pass proper parent for cleanup + Wayland correctness
50 subMenus.emplace_back(new QMenu(QLatin1Char('&') + hlSection, menu()));
51 menu()->addMenu(subMenus.back());
52
53 // last element is the one we just inserted
54 it = --subMenusName.end();
55 }
56
57 const auto m = std::distance(subMenusName.begin(), it);
58 names.push_back(hlName);
59 QAction *a = subMenus.at(m)->addAction(QLatin1Char('&') + hlName, this, SLOT(setHl()));
60 m_actionGroup->addAction(a);
61 a->setData(hl.name());
62 a->setCheckable(true);
63 subActions.push_back(a);
64 } else if (!namesHaveHlName) {
65 names.push_back(hlName);
66 QAction *a = menu()->addAction(QLatin1Char('&') + hlName, this, SLOT(setHl()));
67 m_actionGroup->addAction(a);
68 a->setData(hl.name());
69 a->setCheckable(true);
70 subActions.push_back(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 *>(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"
QString i18n(const char *text, const TYPE &arg...)
void setCheckable(bool)
QVariant data() const const
QMenu * menu() const const
void setData(const QVariant &userData)
QAction * addAction(QAction *action)
void aboutToShow()
QAction * addAction(const QString &text)
QAction * addMenu(QMenu *menu)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * sender() const const
bool isEmpty() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Feb 24 2024 20:00:58 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.