1 /***************************************************************************
2 *   Copyright (C) 2005 by Joshua Keel <joshuakeel@gmail.com>              *
3 *                                                                         *
4 *   This program is free software; you can redistribute it and/or modify  *
5 *   it under the terms of the GNU General Public License as published by  *
6 *   the Free Software Foundation; either version 2 of the License, or     *
7 *   (at your option) any later version.                                   *
8 *                                                                         *
9 *   This program is distributed in the hope that it will be useful,       *
10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 *   GNU General Public License for more details.                          *
13 *                                                                         *
14 *   You should have received a copy of the GNU General Public License     *
15 *   along with this program; if not, write to the                         *
16 *   Free Software Foundation, Inc.,                                       *
17 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
18 ***************************************************************************/
19 
20 #include "vocabedit.h"
21 
22 #include <KLocalizedString>
23 #include <KMessageBox>
24 
25 #include <KEduVocDocument>
26 #include <KEduVocExpression>
27 
28 #include <QDir>
29 #include <QStandardPaths>
30 
31 #include "kanagramsettings.h"
32 #include "vocabsettings.h"
33 #include "kanagramgame.h"
34 
VocabEdit(QWidget * parent,const QString & fileName)35 VocabEdit::VocabEdit(QWidget *parent, const QString  &fileName) : QDialog(parent), m_fileName(QLatin1String(""))
36 {
37     setupUi(this);
38 
39     if (!fileName.isEmpty())
40     {
41         m_fileName = fileName;
42         KEduVocDocument	*doc = new KEduVocDocument(this);
43         doc->open(QUrl::fromLocalFile(m_fileName), KEduVocDocument::FileIgnoreLock);
44         for (int i = 0; i < doc->lesson()->entryCount(KEduVocLesson::Recursive); i++)
45         {
46             KEduVocExpression expr = *doc->lesson()->entries(KEduVocLesson::Recursive).value(i);
47             m_vocabList.append(expr);
48             lboxWords->addItem(doc->lesson()->entries(KEduVocLesson::Recursive).value(i)->translation(0)->text());
49         }
50         txtVocabName->setText(doc->title());
51         txtDescription->setText(doc->documentComment());
52         delete doc;
53     }
54 
55     connect(btnSave, &QPushButton::clicked, this, &VocabEdit::slotSave);
56     connect(btnNewWord, &QPushButton::clicked, this, &VocabEdit::slotNewWord);
57     connect(btnRemoveWord, &QPushButton::clicked, this, &VocabEdit::slotRemoveWord);
58     connect(btnClose, &QPushButton::clicked, this, &VocabEdit::slotClose);
59 
60     connect(txtWord, &QLineEdit::textChanged, this, &VocabEdit::slotWordTextChanged);
61     connect(txtHint, &QLineEdit::textChanged, this, &VocabEdit::slotHintTextChanged);
62 
63     //Connect the name and description boxes to a general textChanged slot, so that we can keep track of
64     //whether they've been changed or not
65     connect(txtVocabName, &QLineEdit::textChanged, this, &VocabEdit::slotTextChanged);
66     connect(txtDescription, &QLineEdit::textChanged, this, &VocabEdit::slotTextChanged);
67 
68     connect(lboxWords, &QListWidget::itemSelectionChanged, this, &VocabEdit::slotSelectionChanged);
69 
70     //Has anything in the dialog changed?
71     m_textChanged = false;
72 }
73 
~VocabEdit()74 VocabEdit::~VocabEdit()
75 {
76 }
77 
slotSave()78 void VocabEdit::slotSave()
79 {
80     KEduVocDocument *doc = new KEduVocDocument(this);
81     doc->setTitle(txtVocabName->text());
82     doc->setDocumentComment(txtDescription->text());
83     KEduVocIdentifier id;
84     doc->appendIdentifier(id);
85     for (int i = 0; i < m_vocabList.size(); i++)
86     {
87         doc->lesson()->appendEntry(&m_vocabList[i]);
88     }
89 
90     const QString fileName = txtVocabName->text().toLower().remove(' ') + ".kvtml";
91     const QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) +
92                    "/apps/kvtml/" + KanagramSettings::dataLanguage();
93     QDir dir;
94     dir.mkpath(path);
95     const QUrl url = QUrl::fromLocalFile(path +
96                             QLatin1Char('/') + fileName);
97     qCDebug(KANAGRAM) << "Saving file as " << url;
98     doc->saveAs(url, KEduVocDocument::Automatic);
99 
100     m_textChanged = false;
101 }
102 
slotClose()103 void VocabEdit::slotClose()
104 {
105     //Has anything in the dialog changed?
106     if (m_textChanged && lboxWords->count() > 0)
107     {
108         int code = KMessageBox::warningYesNo(this, i18n("Would you like to save your changes?"), i18n("Save Changes Dialog"));
109         if (code == KMessageBox::Yes)
110         {
111             slotSave();
112             close();
113         }
114         else
115         {
116             close();
117         }
118     }
119     else
120     {
121         close();
122     }
123 }
124 
slotNewWord()125 void VocabEdit::slotNewWord()
126 {
127     lboxWords->addItem(i18n("New Item"));
128     KEduVocExpression expr = KEduVocExpression();
129     m_vocabList.append(expr);
130     m_textChanged = true;
131 }
132 
slotSelectionChanged()133 void VocabEdit::slotSelectionChanged()
134 {
135     //A little hack to make things work right
136     disconnect(txtWord, &QLineEdit::textChanged, this, &VocabEdit::slotWordTextChanged);
137     disconnect(txtHint, &QLineEdit::textChanged, this, &VocabEdit::slotHintTextChanged);
138     if (lboxWords->currentRow() >= 0)
139     {
140         txtWord->setText(m_vocabList[lboxWords->currentRow()].translation(0)->text());
141         txtHint->setText(m_vocabList[lboxWords->currentRow()].translation(0)->comment());
142     }
143     connect(txtWord, &QLineEdit::textChanged, this, &VocabEdit::slotWordTextChanged);
144     connect(txtHint, &QLineEdit::textChanged, this, &VocabEdit::slotHintTextChanged);
145 }
146 
slotWordTextChanged(const QString & changes)147 void VocabEdit::slotWordTextChanged(const QString &changes)
148 {
149     //Make sure there actually is a currentRow()
150     if (lboxWords->currentRow() != -1)
151     {
152         m_vocabList[lboxWords->currentRow()].setTranslation(0, changes);
153         lboxWords->currentItem()->setText(changes);
154     }
155     m_textChanged = true;
156 }
157 
slotHintTextChanged(const QString & changes)158 void VocabEdit::slotHintTextChanged(const QString &changes)
159 {
160     //Make sure there actually is a currentItem()
161     if (lboxWords->currentRow() != -1)
162         m_vocabList[lboxWords->currentRow()].translation(0)->setComment(changes);
163     m_textChanged = true;
164 }
165 
slotTextChanged(const QString & changes)166 void VocabEdit::slotTextChanged(const QString &changes)
167 {
168     Q_UNUSED(changes);
169 
170     //Make sure we know when text has been modified and not saved, so we
171     //can notify the user
172     m_textChanged = true;
173 }
174 
slotRemoveWord()175 void VocabEdit::slotRemoveWord()
176 {
177     if (lboxWords->count() && lboxWords->currentRow() >= 0) {
178         m_vocabList.erase(m_vocabList.begin() + lboxWords->currentRow());
179         delete lboxWords->takeItem(lboxWords->currentRow());
180     }
181 
182     m_textChanged = true;
183 }
184