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 }
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"
Backend of KTextEditor::Document related public KTextEditor interfaces.
QString i18n(const char *text, const TYPE &arg...)
void setCheckable(bool)
QVariant data() const const
QMenu * menu() const const
void setData(const QVariant &data)
QAction * addAction(QAction *action)
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
void aboutToShow()
QAction * addMenu(QMenu *menu)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
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 Fri Dec 20 2024 11:56:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.