File indexing completed on 2025-01-12 03:27:55
0001 /*************************************************************************** 0002 * Copyright (C) 2005 by Joshua Keel <joshuakeel@gmail.com> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0012 * GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License * 0015 * along with this program; if not, write to the * 0016 * Free Software Foundation, Inc., * 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 0018 ***************************************************************************/ 0019 0020 #include "vocabedit.h" 0021 0022 #include <kwidgetsaddons_version.h> 0023 #include <KLocalizedString> 0024 #include <KMessageBox> 0025 0026 #include <KEduVocDocument> 0027 #include <KEduVocExpression> 0028 0029 #include <QDir> 0030 #include <QStandardPaths> 0031 0032 #include "kanagramsettings.h" 0033 #include "vocabsettings.h" 0034 #include "kanagramgame.h" 0035 0036 VocabEdit::VocabEdit(QWidget *parent, const QString &fileName) : QDialog(parent), m_fileName(QLatin1String("")) 0037 { 0038 setupUi(this); 0039 0040 if (!fileName.isEmpty()) 0041 { 0042 m_fileName = fileName; 0043 KEduVocDocument *doc = new KEduVocDocument(this); 0044 doc->open(QUrl::fromLocalFile(m_fileName), KEduVocDocument::FileIgnoreLock); 0045 for (int i = 0; i < doc->lesson()->entryCount(KEduVocLesson::Recursive); i++) 0046 { 0047 KEduVocExpression expr = *doc->lesson()->entries(KEduVocLesson::Recursive).value(i); 0048 m_vocabList.append(expr); 0049 lboxWords->addItem(doc->lesson()->entries(KEduVocLesson::Recursive).value(i)->translation(0)->text()); 0050 } 0051 txtVocabName->setText(doc->title()); 0052 txtDescription->setText(doc->documentComment()); 0053 delete doc; 0054 } 0055 0056 connect(btnSave, &QPushButton::clicked, this, &VocabEdit::slotSave); 0057 connect(btnNewWord, &QPushButton::clicked, this, &VocabEdit::slotNewWord); 0058 connect(btnRemoveWord, &QPushButton::clicked, this, &VocabEdit::slotRemoveWord); 0059 connect(btnClose, &QPushButton::clicked, this, &VocabEdit::slotClose); 0060 0061 connect(txtWord, &QLineEdit::textChanged, this, &VocabEdit::slotWordTextChanged); 0062 connect(txtHint, &QLineEdit::textChanged, this, &VocabEdit::slotHintTextChanged); 0063 0064 //Connect the name and description boxes to a general textChanged slot, so that we can keep track of 0065 //whether they've been changed or not 0066 connect(txtVocabName, &QLineEdit::textChanged, this, &VocabEdit::slotTextChanged); 0067 connect(txtDescription, &QLineEdit::textChanged, this, &VocabEdit::slotTextChanged); 0068 0069 connect(lboxWords, &QListWidget::itemSelectionChanged, this, &VocabEdit::slotSelectionChanged); 0070 0071 //Has anything in the dialog changed? 0072 m_textChanged = false; 0073 } 0074 0075 VocabEdit::~VocabEdit() 0076 { 0077 } 0078 0079 void VocabEdit::slotSave() 0080 { 0081 KEduVocDocument *doc = new KEduVocDocument(this); 0082 doc->setTitle(txtVocabName->text()); 0083 doc->setDocumentComment(txtDescription->text()); 0084 KEduVocIdentifier id; 0085 doc->appendIdentifier(id); 0086 for (int i = 0; i < m_vocabList.size(); i++) 0087 { 0088 doc->lesson()->appendEntry(&m_vocabList[i]); 0089 } 0090 0091 const QString fileName = txtVocabName->text().toLower().remove(' ') + ".kvtml"; 0092 const QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + 0093 "/apps/kvtml/" + KanagramSettings::dataLanguage(); 0094 QDir dir; 0095 dir.mkpath(path); 0096 const QUrl url = QUrl::fromLocalFile(path + 0097 QLatin1Char('/') + fileName); 0098 qCDebug(KANAGRAM) << "Saving file as " << url; 0099 doc->saveAs(url, KEduVocDocument::Automatic); 0100 0101 m_textChanged = false; 0102 } 0103 0104 void VocabEdit::slotClose() 0105 { 0106 //Has anything in the dialog changed? 0107 if (m_textChanged && lboxWords->count() > 0) 0108 { 0109 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0) 0110 int code = KMessageBox::warningTwoActions(this, 0111 #else 0112 int code = KMessageBox::warningYesNo(this, 0113 #endif 0114 i18n("Would you like to save your changes?"), 0115 i18n("Save Changes Dialog"), 0116 KStandardGuiItem::save(), 0117 KStandardGuiItem::discard()); 0118 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0) 0119 if (code == KMessageBox::PrimaryAction) 0120 #else 0121 if (code == KMessageBox::Yes) 0122 #endif 0123 { 0124 slotSave(); 0125 close(); 0126 } 0127 else 0128 { 0129 close(); 0130 } 0131 } 0132 else 0133 { 0134 close(); 0135 } 0136 } 0137 0138 void VocabEdit::slotNewWord() 0139 { 0140 lboxWords->addItem(i18n("New Item")); 0141 KEduVocExpression expr = KEduVocExpression(); 0142 m_vocabList.append(expr); 0143 m_textChanged = true; 0144 } 0145 0146 void VocabEdit::slotSelectionChanged() 0147 { 0148 //A little hack to make things work right 0149 disconnect(txtWord, &QLineEdit::textChanged, this, &VocabEdit::slotWordTextChanged); 0150 disconnect(txtHint, &QLineEdit::textChanged, this, &VocabEdit::slotHintTextChanged); 0151 if (lboxWords->currentRow() >= 0) 0152 { 0153 txtWord->setText(m_vocabList[lboxWords->currentRow()].translation(0)->text()); 0154 txtHint->setText(m_vocabList[lboxWords->currentRow()].translation(0)->comment()); 0155 } 0156 connect(txtWord, &QLineEdit::textChanged, this, &VocabEdit::slotWordTextChanged); 0157 connect(txtHint, &QLineEdit::textChanged, this, &VocabEdit::slotHintTextChanged); 0158 } 0159 0160 void VocabEdit::slotWordTextChanged(const QString &changes) 0161 { 0162 //Make sure there actually is a currentRow() 0163 if (lboxWords->currentRow() != -1) 0164 { 0165 m_vocabList[lboxWords->currentRow()].setTranslation(0, changes); 0166 lboxWords->currentItem()->setText(changes); 0167 } 0168 m_textChanged = true; 0169 } 0170 0171 void VocabEdit::slotHintTextChanged(const QString &changes) 0172 { 0173 //Make sure there actually is a currentItem() 0174 if (lboxWords->currentRow() != -1) 0175 m_vocabList[lboxWords->currentRow()].translation(0)->setComment(changes); 0176 m_textChanged = true; 0177 } 0178 0179 void VocabEdit::slotTextChanged(const QString &changes) 0180 { 0181 Q_UNUSED(changes); 0182 0183 //Make sure we know when text has been modified and not saved, so we 0184 //can notify the user 0185 m_textChanged = true; 0186 } 0187 0188 void VocabEdit::slotRemoveWord() 0189 { 0190 if (lboxWords->count() && lboxWords->currentRow() >= 0) { 0191 m_vocabList.erase(m_vocabList.begin() + lboxWords->currentRow()); 0192 delete lboxWords->takeItem(lboxWords->currentRow()); 0193 } 0194 0195 m_textChanged = true; 0196 } 0197 0198 #include "moc_vocabedit.cpp"