File indexing completed on 2022-09-20 12:22:40

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