File: katehighlightmenu.cpp

package info (click to toggle)
kf6-ktexteditor 6.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,676 kB
  • sloc: cpp: 93,343; javascript: 20,925; xml: 237; sh: 20; ansic: 16; makefile: 8
file content (100 lines) | stat: -rw-r--r-- 3,059 bytes parent folder | download | duplicates (3)
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"