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