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

0001 /*
0002  * SPDX-FileCopyrightText: 2013 Sebastian Gottfried <sebastiangottfried@web.de>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "customlessoneditordialog.h"
0008 
0009 #include <QPushButton>
0010 
0011 #include "core/keyboardlayout.h"
0012 #include "core/key.h"
0013 #include "core/keychar.h"
0014 #include "core/lesson.h"
0015 #include "editor/lessontexthighlighter.h"
0016 
0017 CustomLessonEditorDialog::CustomLessonEditorDialog(QWidget* parent) :
0018     QDialog(parent),
0019     Ui::CustomLessonEditorDialog(),
0020     m_lesson(0),
0021     m_keyboardLayout(0)
0022 {
0023     this->setupUi(this);
0024 
0025     connect(m_titleLineEdit, &QLineEdit::textChanged, this, &CustomLessonEditorDialog::onTitleChanged);
0026     connect(m_lessonTextEditor->textEdit(), &QTextEdit::textChanged, this, &CustomLessonEditorDialog::onTextChanged);
0027     connect(this, &CustomLessonEditorDialog::validChanged, m_buttonBox->button(QDialogButtonBox::Ok), &QPushButton::setEnabled);
0028 }
0029 
0030 Lesson* CustomLessonEditorDialog::lesson() const
0031 {
0032     return m_lesson;
0033 }
0034 
0035 void CustomLessonEditorDialog::setLesson(Lesson* lesson)
0036 {
0037     if (lesson != m_lesson)
0038     {
0039         if (m_lesson)
0040         {
0041             m_lesson->disconnect(this);
0042         }
0043 
0044         m_lesson = lesson;
0045 
0046         if (lesson)
0047         {
0048             updateTitle();
0049             updateText();
0050 
0051             connect(lesson, &Lesson::titleChanged, this, &CustomLessonEditorDialog::updateTitle);
0052             connect(lesson, &Lesson::textChanged, this, &CustomLessonEditorDialog::updateText);
0053         }
0054     }
0055 }
0056 
0057 KeyboardLayout* CustomLessonEditorDialog::keyboardLayout() const
0058 {
0059     return m_keyboardLayout;
0060 }
0061 
0062 void CustomLessonEditorDialog::setKeyboardLayout(KeyboardLayout* keyboardLayout)
0063 {
0064     if (keyboardLayout != m_keyboardLayout)
0065     {
0066         if (m_keyboardLayout)
0067         {
0068             m_keyboardLayout->disconnect(this);
0069         }
0070 
0071         m_keyboardLayout = keyboardLayout;
0072         updateAllowedCharacters();
0073 
0074         if (m_keyboardLayout)
0075         {
0076             connect(m_keyboardLayout, &Resource::isValidChanged, this, &CustomLessonEditorDialog::updateAllowedCharacters);
0077         }
0078     }
0079 }
0080 
0081 void CustomLessonEditorDialog::updateTitle()
0082 {
0083     const QString title = m_lesson->title();
0084 
0085     if (title != m_titleLineEdit->text())
0086     {
0087         m_titleLineEdit->setText(title);
0088     }
0089 }
0090 
0091 void CustomLessonEditorDialog::updateText()
0092 {
0093     const QString text = m_lesson->text();
0094 
0095     if (text != m_lessonTextEditor->textEdit()->toPlainText())
0096     {
0097         m_lessonTextEditor->textEdit()->setPlainText(text);
0098     }
0099 }
0100 
0101 void CustomLessonEditorDialog::updateAllowedCharacters()
0102 {
0103     if (!m_keyboardLayout || !m_keyboardLayout->isValid())
0104     {
0105         m_lessonTextEditor->highlighter()->setAllowedCharacters(QString());
0106         return;
0107     }
0108 
0109     QString chars;
0110 
0111     for (int i = 0; i < m_keyboardLayout->keyCount(); i++)
0112     {
0113         Key* const key = qobject_cast<Key*>(m_keyboardLayout->key(i));
0114 
0115         if (!key)
0116             continue;
0117 
0118         for (int j = 0; j < key->keyCharCount(); j++)
0119         {
0120             KeyChar* const keyChar = key->keyChar(j);
0121 
0122             chars += keyChar->value();
0123         }
0124     }
0125 
0126     m_lessonTextEditor->highlighter()->setAllowedCharacters(chars);
0127 }
0128 
0129 void CustomLessonEditorDialog::onTitleChanged()
0130 {
0131     if (!m_lesson)
0132         return;
0133 
0134     m_lesson->setTitle(m_titleLineEdit->text());
0135 
0136     emitValidChanged();
0137 }
0138 
0139 void CustomLessonEditorDialog::onTextChanged()
0140 {
0141     if (!m_lesson)
0142         return;
0143 
0144     m_lesson->setText(m_lessonTextEditor->textEdit()->toPlainText());
0145 
0146     emitValidChanged();
0147 }
0148 
0149 void CustomLessonEditorDialog::emitValidChanged()
0150 {
0151     const QString title = m_lesson->title();
0152     const QString text = m_lesson->text();
0153     const bool valid = !title.isEmpty() && !text.isEmpty();
0154 
0155     emit validChanged(valid);
0156 }