1/*
2 SPDX-FileCopyrightText: 2001-2010 Christoph Cullmann <cullmann@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7// BEGIN Includes
8#include "katemodemenu.h"
9
10#include "kateconfig.h"
11#include "katedocument.h"
12#include "kateglobal.h"
13#include "katepartdebug.h"
14
15#include <QActionGroup>
16#include <QMenu>
17// END Includes
18
19void KateModeMenu::init()
20{
21 m_doc = nullptr;
22
23 connect(sender: menu(), signal: &QMenu::triggered, context: this, slot: &KateModeMenu::setType);
24
25 connect(sender: menu(), signal: &QMenu::aboutToShow, context: this, slot: &KateModeMenu::slotAboutToShow);
26
27 m_actionGroup = new QActionGroup(menu());
28}
29
30void KateModeMenu::updateMenu(KTextEditor::Document *doc)
31{
32 m_doc = static_cast<KTextEditor::DocumentPrivate *>(doc);
33}
34
35void KateModeMenu::slotAboutToShow()
36{
37 KTextEditor::DocumentPrivate *doc = m_doc;
38 int count = KTextEditor::EditorPrivate::self()->modeManager()->list().count();
39
40 for (int z = 0; z < count; z++) {
41 QString nameRaw = KTextEditor::EditorPrivate::self()->modeManager()->list().at(i: z)->name;
42 QString hlName = KTextEditor::EditorPrivate::self()->modeManager()->list().at(i: z)->nameTranslated();
43 QString hlSection = KTextEditor::EditorPrivate::self()->modeManager()->list().at(i: z)->sectionTranslated();
44
45 if (hlName.isEmpty()) {
46 continue;
47 }
48 if (!hlSection.isEmpty() && !names.contains(str: hlName)) {
49 if (!subMenusName.contains(str: hlSection)) {
50 subMenusName << hlSection;
51 // pass proper parent for cleanup + Wayland correctness
52 QMenu *qmenu = new QMenu(hlSection, menu());
53 connect(sender: qmenu, signal: &QMenu::triggered, context: this, slot: &KateModeMenu::setType);
54 subMenus.append(t: qmenu);
55 menu()->addMenu(menu: qmenu);
56 }
57
58 int m = subMenusName.indexOf(str: hlSection);
59 names << hlName;
60 QAction *action = subMenus.at(i: m)->addAction(text: hlName);
61 m_actionGroup->addAction(a: action);
62 action->setCheckable(true);
63 action->setData(nameRaw);
64 } else if (!names.contains(str: hlName)) {
65 names << hlName;
66
67 disconnect(sender: menu(), signal: &QMenu::triggered, receiver: this, slot: &KateModeMenu::setType);
68 connect(sender: menu(), signal: &QMenu::triggered, context: this, slot: &KateModeMenu::setType);
69
70 QAction *action = menu()->addAction(text: hlName);
71 m_actionGroup->addAction(a: action);
72 action->setCheckable(true);
73 action->setData(nameRaw);
74 }
75 }
76
77 if (!doc) {
78 return;
79 }
80
81 for (int i = 0; i < subMenus.count(); i++) {
82 QList<QAction *> actions = subMenus.at(i)->actions();
83 for (int j = 0; j < actions.count(); ++j) {
84 actions[j]->setChecked(false);
85 }
86 }
87
88 QList<QAction *> actions = menu()->actions();
89 for (int i = 0; i < actions.count(); ++i) {
90 actions[i]->setChecked(false);
91 }
92
93 if (doc->fileType().isEmpty() || doc->fileType() == QLatin1String("Normal")) {
94 for (int i = 0; i < actions.count(); ++i) {
95 if (actions[i]->data().toString() == QLatin1String("Normal")) {
96 actions[i]->setChecked(true);
97 }
98 }
99 } else {
100 if (!doc->fileType().isEmpty()) {
101 const KateFileType &t = KTextEditor::EditorPrivate::self()->modeManager()->fileType(name: doc->fileType());
102 int i = subMenusName.indexOf(str: t.sectionTranslated());
103 if (i >= 0 && subMenus.at(i)) {
104 QList<QAction *> actions = subMenus.at(i)->actions();
105 for (int j = 0; j < actions.count(); ++j) {
106 if (actions[j]->data().toString() == doc->fileType()) {
107 actions[j]->setChecked(true);
108 }
109 }
110 } else {
111 QList<QAction *> actions = menu()->actions();
112 for (int j = 0; j < actions.count(); ++j) {
113 if (actions[j]->data().toString().isEmpty()) {
114 actions[j]->setChecked(true);
115 }
116 }
117 }
118 }
119 }
120}
121
122void KateModeMenu::setType(QAction *action)
123{
124 KTextEditor::DocumentPrivate *doc = m_doc;
125
126 if (doc) {
127 doc->updateFileType(newType: action->data().toString(), user: true);
128 }
129}
130

source code of ktexteditor/src/mode/katemodemenu.cpp