KTextEditor

katemodemenu.cpp
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(menu(), &QMenu::triggered, this, &KateModeMenu::setType);
24
25 connect(menu(), &QMenu::aboutToShow, this, &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(z)->name;
42 QString hlName = KTextEditor::EditorPrivate::self()->modeManager()->list().at(z)->nameTranslated();
43 QString hlSection = KTextEditor::EditorPrivate::self()->modeManager()->list().at(z)->sectionTranslated();
44
45 if (hlName.isEmpty()) {
46 continue;
47 }
48 if (!hlSection.isEmpty() && !names.contains(hlName)) {
49 if (!subMenusName.contains(hlSection)) {
50 subMenusName << hlSection;
51 // pass proper parent for cleanup + Wayland correctness
52 QMenu *qmenu = new QMenu(hlSection, menu());
53 connect(qmenu, &QMenu::triggered, this, &KateModeMenu::setType);
54 subMenus.append(qmenu);
55 menu()->addMenu(qmenu);
56 }
57
58 int m = subMenusName.indexOf(hlSection);
59 names << hlName;
60 QAction *action = subMenus.at(m)->addAction(hlName);
61 m_actionGroup->addAction(action);
62 action->setCheckable(true);
63 action->setData(nameRaw);
64 } else if (!names.contains(hlName)) {
65 names << hlName;
66
67 disconnect(menu(), &QMenu::triggered, this, &KateModeMenu::setType);
68 connect(menu(), &QMenu::triggered, this, &KateModeMenu::setType);
69
70 QAction *action = menu()->addAction(hlName);
71 m_actionGroup->addAction(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(doc->fileType());
102 int i = subMenusName.indexOf(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(action->data().toString(), true);
128 }
129}
A KParts derived class representing a text document.
Definition document.h:284
KateModeManager * modeManager()
global mode manager used to manage the modes centrally
Definition kateglobal.h:229
static KTextEditor::EditorPrivate * self()
Kate Part Internal stuff ;)
char * toString(const EngineQuery &query)
void setCheckable(bool)
QVariant data() const const
QMenu * menu() const const
void setData(const QVariant &data)
QAction * addAction(QAction *action)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
qsizetype count() const const
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
void aboutToShow()
QAction * addMenu(QMenu *menu)
void triggered(QAction *action)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
qsizetype indexOf(const QRegularExpression &re, qsizetype from) const const
QString toString() const const
QList< QAction * > actions() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:57:26 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.