Warning, file /education/ktouch/src/ktouchcontext.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * SPDX-FileCopyrightText: 2016 Sebastian Gottfried <sebastian.gottfried@posteo.de> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "ktouchcontext.h" 0008 0009 #include <QMenu> 0010 #include <QPointer> 0011 #include <QQuickView> 0012 0013 #include "application.h" 0014 #include "colorsconfigwidget.h" 0015 #include "editor/resourceeditor.h" 0016 #include "customlessoneditordialog.h" 0017 #include "preferences.h" 0018 #include "trainingconfigwidget.h" 0019 #include "core/lesson.h" 0020 0021 #include <kxmlgui_version.h> 0022 #include <KActionCollection> 0023 #include <KStandardAction> 0024 #include <KHelpMenu> 0025 #include <KToggleFullScreenAction> 0026 #include <KShortcutsDialog> 0027 #include <KConfigDialog> 0028 #include <KCMultiDialog> 0029 #include <KLocalizedString> 0030 0031 #ifdef KTOUCH_BUILD_WITH_X11 0032 #include "x11_helper.h" 0033 #endif 0034 0035 0036 0037 const QString keyboardKCMName = QStringLiteral("plasma/kcms/systemsettings/kcm_keyboard"); 0038 0039 KTouchContext::KTouchContext(KMainWindow* mainWindow, QQuickView* view, QObject *parent) : 0040 QObject(parent), 0041 m_actionCollection(new KActionCollection(this)), 0042 m_menu(new QMenu(mainWindow)), 0043 m_mainWindow(mainWindow), 0044 m_view(view) 0045 { 0046 #ifdef KTOUCH_BUILD_WITH_X11 0047 m_XEventNotifier = new XEventNotifier(); 0048 m_XEventNotifier->start(); 0049 connect(m_XEventNotifier, &XEventNotifier::layoutChanged, this, &KTouchContext::keyboardLayoutNameChanged); 0050 #endif 0051 init(); 0052 } 0053 0054 KTouchContext::~KTouchContext() 0055 { 0056 #ifdef KTOUCH_BUILD_WITH_X11 0057 m_XEventNotifier->stop(); 0058 delete m_XEventNotifier; 0059 #endif 0060 } 0061 0062 QString KTouchContext::keyboardLayoutName() const 0063 { 0064 #ifdef KTOUCH_BUILD_WITH_X11 0065 return X11Helper::getCurrentLayout().toString(); 0066 #else 0067 return "unknown"; 0068 #endif 0069 } 0070 0071 DataIndex* KTouchContext::dataIndex() 0072 { 0073 return Application::dataIndex(); 0074 } 0075 0076 bool KTouchContext::keyboardKCMAvailable() 0077 { 0078 return testKCMAvailibility(keyboardKCMName); 0079 } 0080 0081 void KTouchContext::showMenu(int xPos, int yPos) 0082 { 0083 m_menu->popup(m_view->mapToGlobal(QPoint(xPos, yPos))); 0084 } 0085 0086 Lesson* KTouchContext::createLesson() 0087 { 0088 return new Lesson(); 0089 } 0090 0091 void KTouchContext::showResourceEditor() 0092 { 0093 QPointer<ResourceEditor>& resourceEditorRef = Application::resourceEditorRef(); 0094 if (resourceEditorRef.isNull()) 0095 { 0096 resourceEditorRef = QPointer<ResourceEditor>(new ResourceEditor()); 0097 } 0098 0099 ResourceEditor* resourceEditor = resourceEditorRef.data(); 0100 0101 resourceEditor->show(); 0102 resourceEditor->activateWindow(); 0103 } 0104 0105 bool KTouchContext::showCustomLessonDialog(Lesson* lesson, KeyboardLayout* keyboardLayout) 0106 { 0107 CustomLessonEditorDialog* dialog = new CustomLessonEditorDialog(m_mainWindow); 0108 0109 dialog->setLesson(lesson); 0110 dialog->setKeyboardLayout(keyboardLayout); 0111 0112 bool result = dialog->exec() == QDialog::Accepted; 0113 0114 delete dialog; 0115 0116 return result; 0117 } 0118 0119 void KTouchContext::showConfigDialog() 0120 { 0121 if (KConfigDialog::showDialog(QStringLiteral("preferences"))) 0122 { 0123 return; 0124 } 0125 0126 KConfigDialog* dialog = new KConfigDialog(m_mainWindow, QStringLiteral("preferences"), Preferences::self()); 0127 dialog->setFaceType(KPageDialog::List); 0128 dialog->setModal(true); 0129 dialog->addPage(new TrainingConfigWidget(), i18n("Training"), QStringLiteral("chronometer"), i18n("Training Settings")); 0130 dialog->addPage(new ColorsConfigWidget(), i18n("Colors"), QStringLiteral("preferences-desktop-color"), i18n("Color Settings")); 0131 dialog->show(); 0132 } 0133 0134 void KTouchContext::configureShortcuts() 0135 { 0136 KShortcutsDialog::showDialog(m_actionCollection, KShortcutsEditor::LetterShortcutsDisallowed, m_mainWindow); 0137 } 0138 0139 void KTouchContext::showKeyboardKCM() 0140 { 0141 QPointer<KCMultiDialog> kcm = new KCMultiDialog(m_mainWindow); 0142 0143 kcm->setWindowTitle(i18nc("@title:window", "Configure Keyboard")); 0144 kcm->addModule(KPluginMetaData(keyboardKCMName)); 0145 kcm->exec(); 0146 0147 delete kcm; 0148 } 0149 0150 void KTouchContext::setFullscreen(bool fullScreen) 0151 { 0152 KToggleFullScreenAction::setFullScreen(m_mainWindow, fullScreen); 0153 } 0154 0155 void KTouchContext::init() 0156 { 0157 m_actionCollection->addAssociatedWidget(m_mainWindow); 0158 m_menu->addAction(KStandardAction::fullScreen(this, SLOT(setFullscreen(bool)), m_mainWindow, m_actionCollection)); 0159 m_menu->addSeparator(); 0160 QAction* editorAction = new QAction(i18n("Course and Keyboard Layout Editor..."), this); 0161 connect(editorAction, &QAction::triggered, this, &KTouchContext::showResourceEditor); 0162 m_actionCollection->addAction(QStringLiteral("editor"), editorAction); 0163 m_menu->addAction(editorAction); 0164 m_menu->addSeparator(); 0165 m_menu->addAction(KStandardAction::preferences(this, SLOT(showConfigDialog()), m_actionCollection)); 0166 m_menu->addAction(KStandardAction::keyBindings(this, SLOT(configureShortcuts()), m_actionCollection)); 0167 0168 if (testKCMAvailibility(keyboardKCMName)) 0169 { 0170 QAction* configureKeyboardAction = new QAction(i18n("Configure Keyboard..."), this); 0171 m_menu->addAction(configureKeyboardAction); 0172 connect(configureKeyboardAction, &QAction::triggered, this, &KTouchContext::showKeyboardKCM); 0173 } 0174 0175 m_menu->addSeparator(); 0176 KHelpMenu* helpMenu = new KHelpMenu(m_mainWindow); 0177 m_menu->addMenu(helpMenu->menu()); 0178 } 0179 0180 bool KTouchContext::testKCMAvailibility(const QString& name) 0181 { 0182 return KPluginMetaData(name).isValid(); 0183 }