File indexing completed on 2022-11-29 13:00:37

0001 /*
0002     SPDX-FileCopyrightText: 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "wordtypeview.h"
0007 
0008 #include <QContextMenuEvent>
0009 #include <QMenu>
0010 
0011 #include <KLocalizedString>
0012 #include <KMessageBox>
0013 #include <KSelectAction>
0014 #include <QAction>
0015 
0016 #include <KEduVocExpression>
0017 #include <KEduVocWordtype>
0018 
0019 #include "containermodel.h"
0020 #include "editor/editor.h"
0021 
0022 using namespace Editor;
0023 
0024 WordTypeView::WordTypeView(EditorWindow *parent)
0025     : ContainerView(parent)
0026 {
0027     setContextMenuPolicy(Qt::DefaultContextMenu);
0028 
0029     m_actionNewWordType = new QAction(this);
0030     m_actionNewWordType->setText(i18n("New"));
0031     m_actionNewWordType->setIcon(QIcon::fromTheme(QStringLiteral("lesson-add")));
0032     m_actionNewWordType->setWhatsThis(i18n("Add a new word type to your document"));
0033     m_actionNewWordType->setToolTip(m_actionNewWordType->whatsThis());
0034     m_actionNewWordType->setStatusTip(m_actionNewWordType->whatsThis());
0035     m_actionNewWordType->setStatusTip(m_actionNewWordType->whatsThis());
0036 
0037     m_actionRenameWordType = new QAction(this);
0038     m_actionRenameWordType->setText(i18n("Rename"));
0039     m_actionRenameWordType->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
0040     m_actionRenameWordType->setWhatsThis(i18n("Rename the selected word type"));
0041     m_actionRenameWordType->setToolTip(m_actionRenameWordType->whatsThis());
0042     m_actionRenameWordType->setStatusTip(m_actionRenameWordType->whatsThis());
0043     m_actionRenameWordType->setStatusTip(m_actionRenameWordType->whatsThis());
0044 
0045     m_actionDeleteWordType = new QAction(this);
0046     m_actionDeleteWordType->setText(i18n("Delete Word Type"));
0047     m_actionDeleteWordType->setIcon(QIcon::fromTheme(QStringLiteral("lesson-remove")));
0048     m_actionDeleteWordType->setWhatsThis(i18n("Delete the selected word type."));
0049     m_actionDeleteWordType->setToolTip(m_actionDeleteWordType->whatsThis());
0050     m_actionDeleteWordType->setStatusTip(m_actionDeleteWordType->whatsThis());
0051     m_actionDeleteWordType->setStatusTip(m_actionDeleteWordType->whatsThis());
0052 
0053     m_actionSpecialTypeMenu = new KSelectAction(this);
0054     m_actionSpecialTypeMenu->setText(
0055         i18nc("Let the user select what grammatical meaning is connected to a word type (nouns have gender, verbs conjugations etc)", "Grammar"));
0056     // actionSplitWordType->setIcon(QIcon::fromTheme(""));  /// @todo better icon
0057     m_actionSpecialTypeMenu->setWhatsThis(i18n("To let Parley know the grammatical meaning of a word type."));
0058     m_actionSpecialTypeMenu->setToolTip(m_actionSpecialTypeMenu->whatsThis());
0059     m_actionSpecialTypeMenu->setStatusTip(m_actionSpecialTypeMenu->whatsThis());
0060     m_actionSpecialTypeMenu->setStatusTip(m_actionSpecialTypeMenu->whatsThis());
0061 
0062     m_nounAction = new QAction(this);
0063     m_nounAction->setText(i18n("Noun"));
0064     m_nounAction->setCheckable(true);
0065     m_nounAction->setWhatsThis(i18n("This word type folder contains nouns."));
0066     m_nounAction->setToolTip(m_nounAction->whatsThis());
0067     m_nounAction->setStatusTip(m_nounAction->whatsThis());
0068     m_nounAction->setStatusTip(m_nounAction->whatsThis());
0069 
0070     m_nounMaleAction = new QAction(this);
0071     m_nounMaleAction->setText(i18n("Masculine Noun"));
0072     m_nounMaleAction->setCheckable(true);
0073     m_nounMaleAction->setWhatsThis(i18n("This word type folder contains masculine nouns."));
0074     m_nounMaleAction->setToolTip(m_nounMaleAction->whatsThis());
0075     m_nounMaleAction->setStatusTip(m_nounMaleAction->whatsThis());
0076     m_nounMaleAction->setStatusTip(m_nounMaleAction->whatsThis());
0077 
0078     m_nounFemaleAction = new QAction(this);
0079     m_nounFemaleAction->setText(i18n("Feminine Noun"));
0080     m_nounFemaleAction->setCheckable(true);
0081     m_nounFemaleAction->setWhatsThis(i18n("This word type folder contains feminine nouns."));
0082     m_nounFemaleAction->setToolTip(m_nounFemaleAction->whatsThis());
0083     m_nounFemaleAction->setStatusTip(m_nounFemaleAction->whatsThis());
0084     m_nounFemaleAction->setStatusTip(m_nounFemaleAction->whatsThis());
0085 
0086     m_nounNeutralAction = new QAction(this);
0087     m_nounNeutralAction->setText(i18n("Neuter Noun"));
0088     m_nounNeutralAction->setCheckable(true);
0089     m_nounNeutralAction->setWhatsThis(i18n("This word type folder contains neuter nouns."));
0090     m_nounNeutralAction->setToolTip(m_nounNeutralAction->whatsThis());
0091     m_nounNeutralAction->setStatusTip(m_nounNeutralAction->whatsThis());
0092     m_nounNeutralAction->setStatusTip(m_nounNeutralAction->whatsThis());
0093 
0094     m_adjectiveAction = new QAction(this);
0095     m_adjectiveAction->setText(i18n("Adjective"));
0096     m_adjectiveAction->setCheckable(true);
0097     m_adjectiveAction->setWhatsThis(i18n("This word type folder contains adjectives."));
0098     m_adjectiveAction->setToolTip(m_adjectiveAction->whatsThis());
0099     m_adjectiveAction->setStatusTip(m_adjectiveAction->whatsThis());
0100     m_adjectiveAction->setStatusTip(m_adjectiveAction->whatsThis());
0101 
0102     m_adverbAction = new QAction(this);
0103     m_adverbAction->setText(i18n("Adverb"));
0104     m_adverbAction->setCheckable(true);
0105     m_adverbAction->setWhatsThis(i18n("This word type folder contains adverbs."));
0106     m_adverbAction->setToolTip(m_adverbAction->whatsThis());
0107     m_adverbAction->setStatusTip(m_adverbAction->whatsThis());
0108     m_adverbAction->setStatusTip(m_adverbAction->whatsThis());
0109 
0110     m_verbAction = new QAction(this);
0111     m_verbAction->setText(i18n("Verb"));
0112     m_verbAction->setCheckable(true);
0113     m_verbAction->setWhatsThis(i18n("This word type folder contains verbs."));
0114     m_verbAction->setToolTip(m_verbAction->whatsThis());
0115     m_verbAction->setStatusTip(m_verbAction->whatsThis());
0116     m_verbAction->setStatusTip(m_verbAction->whatsThis());
0117 
0118     m_conjunctionAction = new QAction(this);
0119     m_conjunctionAction->setText(i18n("Conjunction"));
0120     m_conjunctionAction->setCheckable(true);
0121     m_conjunctionAction->setWhatsThis(i18n("This word type folder contains conjunctions."));
0122     m_conjunctionAction->setToolTip(m_conjunctionAction->whatsThis());
0123     m_conjunctionAction->setStatusTip(m_conjunctionAction->whatsThis());
0124     m_conjunctionAction->setStatusTip(m_conjunctionAction->whatsThis());
0125 
0126     m_noneAction = new QAction(this);
0127     m_noneAction->setText(i18n("No Special Type"));
0128     m_noneAction->setCheckable(true);
0129     m_noneAction->setChecked(true);
0130     m_noneAction->setWhatsThis(i18n("This word type folder contains no word type with special meaning."));
0131     m_noneAction->setToolTip(m_noneAction->whatsThis());
0132     m_noneAction->setStatusTip(m_noneAction->whatsThis());
0133     m_noneAction->setStatusTip(m_noneAction->whatsThis());
0134 
0135     QAction *separator = new QAction(this);
0136     separator->setSeparator(true);
0137 
0138     m_actionSpecialTypeMenu->addAction(m_noneAction);
0139     m_actionSpecialTypeMenu->addAction(separator);
0140     m_actionSpecialTypeMenu->addAction(m_nounAction);
0141     m_actionSpecialTypeMenu->addAction(m_nounMaleAction);
0142     m_actionSpecialTypeMenu->addAction(m_nounFemaleAction);
0143     m_actionSpecialTypeMenu->addAction(m_nounNeutralAction);
0144     m_actionSpecialTypeMenu->addAction(m_adjectiveAction);
0145     m_actionSpecialTypeMenu->addAction(m_adverbAction);
0146     m_actionSpecialTypeMenu->addAction(m_verbAction);
0147     m_actionSpecialTypeMenu->addAction(m_conjunctionAction);
0148 
0149     connect(m_actionNewWordType, &QAction::triggered, this, &WordTypeView::slotCreateNewWordType);
0150     connect(m_actionRenameWordType, &QAction::triggered, this, &WordTypeView::slotRename);
0151     connect(m_actionDeleteWordType, &QAction::triggered, this, &WordTypeView::slotDeleteWordType);
0152 
0153     // right click menu for the WordType view:
0154     addAction(m_actionNewWordType);
0155     addAction(m_actionRenameWordType);
0156     addAction(m_actionDeleteWordType);
0157     separator = new QAction(this);
0158     separator->setSeparator(true);
0159     addAction(separator);
0160     separator = new QAction(this);
0161     separator->setSeparator(true);
0162     addAction(separator);
0163     addAction(m_actionSpecialTypeMenu);
0164 
0165     connect(m_noneAction, &QAction::triggered, this, &WordTypeView::setWordTypeNone);
0166     connect(m_nounAction, &QAction::triggered, this, &WordTypeView::setWordTypeNoun);
0167     connect(m_nounMaleAction, &QAction::triggered, this, &WordTypeView::setWordTypeNounMale);
0168     connect(m_nounFemaleAction, &QAction::triggered, this, &WordTypeView::setWordTypeNounFemale);
0169     connect(m_nounNeutralAction, &QAction::triggered, this, &WordTypeView::setWordTypeNounNeutral);
0170     connect(m_adjectiveAction, &QAction::triggered, this, &WordTypeView::setWordTypeAdjective);
0171     connect(m_adverbAction, &QAction::triggered, this, &WordTypeView::setWordTypeAdverb);
0172     connect(m_verbAction, &QAction::triggered, this, &WordTypeView::setWordTypeVerb);
0173     connect(m_conjunctionAction, &QAction::triggered, this, &WordTypeView::setWordTypeConjunction);
0174 }
0175 
0176 void WordTypeView::setTranslation(KEduVocExpression *entry, int translation)
0177 {
0178     if (entry == nullptr) {
0179         return;
0180     }
0181 
0182     // attempt to find the container to select
0183     QModelIndex index(m_model->index(entry->translation(translation)->wordType()));
0184     scrollTo(index);
0185     selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
0186 }
0187 
0188 void WordTypeView::slotCreateNewWordType()
0189 {
0190     QModelIndex selectedIndex = selectionModel()->currentIndex();
0191 
0192     if (!selectedIndex.isValid()) {
0193         selectedIndex = m_model->index(0, 0, QModelIndex());
0194     }
0195 
0196     QModelIndex modelIndex = m_model->appendContainer(selectedIndex);
0197 
0198     scrollTo(modelIndex);
0199     selectionModel()->setCurrentIndex(modelIndex, QItemSelectionModel::ClearAndSelect);
0200     edit(modelIndex); // let the user type a new name for the WordType
0201 }
0202 
0203 void WordTypeView::slotDeleteWordType()
0204 {
0205     QModelIndex selectedIndex = selectionModel()->currentIndex();
0206 
0207     if (selectedIndex.parent() == QModelIndex()) {
0208         KMessageBox::information(this, i18n("The root word type cannot be deleted."));
0209         return;
0210     }
0211 
0212     KEduVocWordType *WordType = static_cast<KEduVocWordType *>(selectedIndex.internalPointer());
0213 
0214     int count = WordType->entryCount();
0215 
0216     if (count == 0
0217         || KMessageBox::warningYesNo(this,
0218                                      i18np("There is a word left with this word type. It will lose its type. Continue?",
0219                                            "There are %1 words left with this word type. They will lose their word type. Continue?",
0220                                            count))
0221             == KMessageBox::Yes) {
0222         m_model->deleteContainer(selectedIndex);
0223     }
0224 }
0225 
0226 void WordTypeView::contextMenuEvent(QContextMenuEvent *event)
0227 {
0228     // check for the root element:
0229     QModelIndex selectedIndex = selectionModel()->currentIndex();
0230     m_actionRenameWordType->setEnabled(selectedIndex.parent() != QModelIndex());
0231     m_actionDeleteWordType->setEnabled(selectedIndex.parent() != QModelIndex());
0232     m_actionSpecialTypeMenu->setEnabled(selectedIndex.parent() != QModelIndex());
0233 
0234     if (selectedIndex.isValid()) {
0235         KEduVocWordFlags t = (static_cast<KEduVocWordType *>(selectionModel()->currentIndex().internalPointer())->wordType());
0236 
0237         if (!t)
0238             m_noneAction->setChecked(true);
0239 
0240         else if (t.testFlag(KEduVocWordFlag::Noun) && t.testFlag(KEduVocWordFlag::Masculine))
0241             m_nounMaleAction->setChecked(true);
0242 
0243         else if (t.testFlag(KEduVocWordFlag::Noun) && t.testFlag(KEduVocWordFlag::Feminine))
0244             m_nounFemaleAction->setChecked(true);
0245 
0246         else if (t.testFlag(KEduVocWordFlag::Noun) && t.testFlag(KEduVocWordFlag::Neuter))
0247             m_nounNeutralAction->setChecked(true);
0248 
0249         else if (t.testFlag(KEduVocWordFlag::Noun))
0250             m_nounAction->setChecked(true);
0251 
0252         else if (t.testFlag(KEduVocWordFlag::Adjective))
0253             m_adjectiveAction->setChecked(true);
0254 
0255         else if (t.testFlag(KEduVocWordFlag::Adverb))
0256             m_adverbAction->setChecked(true);
0257 
0258         else if (t.testFlag(KEduVocWordFlag::Verb))
0259             m_verbAction->setChecked(true);
0260 
0261         else if (t.testFlag(KEduVocWordFlag::Conjunction))
0262             m_conjunctionAction->setChecked(true);
0263     }
0264 
0265     QMenu::exec(actions(), event->globalPos());
0266 }
0267 
0268 void WordTypeView::setWordTypeNone()
0269 {
0270     KEduVocWordType *wordType = static_cast<KEduVocWordType *>(selectionModel()->currentIndex().internalPointer());
0271     wordType->setWordType(KEduVocWordFlag::NoInformation);
0272 }
0273 
0274 void WordTypeView::setWordTypeNoun()
0275 {
0276     KEduVocWordType *wordType = static_cast<KEduVocWordType *>(selectionModel()->currentIndex().internalPointer());
0277     wordType->setWordType(KEduVocWordFlag::Noun);
0278 }
0279 
0280 void WordTypeView::setWordTypeNounMale()
0281 {
0282     KEduVocWordType *wordType = static_cast<KEduVocWordType *>(selectionModel()->currentIndex().internalPointer());
0283     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Masculine);
0284 }
0285 
0286 void WordTypeView::setWordTypeNounFemale()
0287 {
0288     KEduVocWordType *wordType = static_cast<KEduVocWordType *>(selectionModel()->currentIndex().internalPointer());
0289     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Feminine);
0290 }
0291 
0292 void WordTypeView::setWordTypeNounNeutral()
0293 {
0294     KEduVocWordType *wordType = static_cast<KEduVocWordType *>(selectionModel()->currentIndex().internalPointer());
0295     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Neuter);
0296 }
0297 
0298 void WordTypeView::setWordTypeAdjective()
0299 {
0300     KEduVocWordType *wordType = static_cast<KEduVocWordType *>(selectionModel()->currentIndex().internalPointer());
0301     wordType->setWordType(KEduVocWordFlag::Adjective);
0302 }
0303 
0304 void WordTypeView::setWordTypeAdverb()
0305 {
0306     KEduVocWordType *wordType = static_cast<KEduVocWordType *>(selectionModel()->currentIndex().internalPointer());
0307     wordType->setWordType(KEduVocWordFlag::Adverb);
0308 }
0309 
0310 void WordTypeView::setWordTypeVerb()
0311 {
0312     KEduVocWordType *wordType = static_cast<KEduVocWordType *>(selectionModel()->currentIndex().internalPointer());
0313     wordType->setWordType(KEduVocWordFlag::Verb);
0314 }
0315 
0316 void WordTypeView::setWordTypeConjunction()
0317 {
0318     KEduVocWordType *wordType = static_cast<KEduVocWordType *>(selectionModel()->currentIndex().internalPointer());
0319     wordType->setWordType(KEduVocWordFlag::Conjunction);
0320 }