File indexing completed on 2024-04-28 15:30:32

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