File indexing completed on 2024-04-21 03:57:31

0001 /*
0002     SPDX-FileCopyrightText: 2001-2010 Christoph Cullmann <cullmann@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 // BEGIN Includes
0008 #include "katemodemenu.h"
0009 
0010 #include "kateconfig.h"
0011 #include "katedocument.h"
0012 #include "kateglobal.h"
0013 #include "katepartdebug.h"
0014 
0015 #include <QActionGroup>
0016 #include <QMenu>
0017 // END Includes
0018 
0019 void KateModeMenu::init()
0020 {
0021     m_doc = nullptr;
0022 
0023     connect(menu(), &QMenu::triggered, this, &KateModeMenu::setType);
0024 
0025     connect(menu(), &QMenu::aboutToShow, this, &KateModeMenu::slotAboutToShow);
0026 
0027     m_actionGroup = new QActionGroup(menu());
0028 }
0029 
0030 void KateModeMenu::updateMenu(KTextEditor::Document *doc)
0031 {
0032     m_doc = static_cast<KTextEditor::DocumentPrivate *>(doc);
0033 }
0034 
0035 void KateModeMenu::slotAboutToShow()
0036 {
0037     KTextEditor::DocumentPrivate *doc = m_doc;
0038     int count = KTextEditor::EditorPrivate::self()->modeManager()->list().count();
0039 
0040     for (int z = 0; z < count; z++) {
0041         QString nameRaw = KTextEditor::EditorPrivate::self()->modeManager()->list().at(z)->name;
0042         QString hlName = KTextEditor::EditorPrivate::self()->modeManager()->list().at(z)->nameTranslated();
0043         QString hlSection = KTextEditor::EditorPrivate::self()->modeManager()->list().at(z)->sectionTranslated();
0044 
0045         if (hlName.isEmpty()) {
0046             continue;
0047         }
0048         if (!hlSection.isEmpty() && !names.contains(hlName)) {
0049             if (!subMenusName.contains(hlSection)) {
0050                 subMenusName << hlSection;
0051                 // pass proper parent for cleanup + Wayland correctness
0052                 QMenu *qmenu = new QMenu(hlSection, menu());
0053                 connect(qmenu, &QMenu::triggered, this, &KateModeMenu::setType);
0054                 subMenus.append(qmenu);
0055                 menu()->addMenu(qmenu);
0056             }
0057 
0058             int m = subMenusName.indexOf(hlSection);
0059             names << hlName;
0060             QAction *action = subMenus.at(m)->addAction(hlName);
0061             m_actionGroup->addAction(action);
0062             action->setCheckable(true);
0063             action->setData(nameRaw);
0064         } else if (!names.contains(hlName)) {
0065             names << hlName;
0066 
0067             disconnect(menu(), &QMenu::triggered, this, &KateModeMenu::setType);
0068             connect(menu(), &QMenu::triggered, this, &KateModeMenu::setType);
0069 
0070             QAction *action = menu()->addAction(hlName);
0071             m_actionGroup->addAction(action);
0072             action->setCheckable(true);
0073             action->setData(nameRaw);
0074         }
0075     }
0076 
0077     if (!doc) {
0078         return;
0079     }
0080 
0081     for (int i = 0; i < subMenus.count(); i++) {
0082         QList<QAction *> actions = subMenus.at(i)->actions();
0083         for (int j = 0; j < actions.count(); ++j) {
0084             actions[j]->setChecked(false);
0085         }
0086     }
0087 
0088     QList<QAction *> actions = menu()->actions();
0089     for (int i = 0; i < actions.count(); ++i) {
0090         actions[i]->setChecked(false);
0091     }
0092 
0093     if (doc->fileType().isEmpty() || doc->fileType() == QLatin1String("Normal")) {
0094         for (int i = 0; i < actions.count(); ++i) {
0095             if (actions[i]->data().toString() == QLatin1String("Normal")) {
0096                 actions[i]->setChecked(true);
0097             }
0098         }
0099     } else {
0100         if (!doc->fileType().isEmpty()) {
0101             const KateFileType &t = KTextEditor::EditorPrivate::self()->modeManager()->fileType(doc->fileType());
0102             int i = subMenusName.indexOf(t.sectionTranslated());
0103             if (i >= 0 && subMenus.at(i)) {
0104                 QList<QAction *> actions = subMenus.at(i)->actions();
0105                 for (int j = 0; j < actions.count(); ++j) {
0106                     if (actions[j]->data().toString() == doc->fileType()) {
0107                         actions[j]->setChecked(true);
0108                     }
0109                 }
0110             } else {
0111                 QList<QAction *> actions = menu()->actions();
0112                 for (int j = 0; j < actions.count(); ++j) {
0113                     if (actions[j]->data().toString().isEmpty()) {
0114                         actions[j]->setChecked(true);
0115                     }
0116                 }
0117             }
0118         }
0119     }
0120 }
0121 
0122 void KateModeMenu::setType(QAction *action)
0123 {
0124     KTextEditor::DocumentPrivate *doc = m_doc;
0125 
0126     if (doc) {
0127         doc->updateFileType(action->data().toString(), true);
0128     }
0129 }