00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "vocabedit.h"
00023
00024 #include <qpushbutton.h>
00025 #include <qlistwidget.h>
00026 #include <qlineedit.h>
00027 #include <qfile.h>
00028 #include <qstring.h>
00029 #include <qvector.h>
00030
00031 #include <kstandarddirs.h>
00032 #include <kglobal.h>
00033 #include <kurl.h>
00034 #include <kdebug.h>
00035 #include <kmessagebox.h>
00036 #include <klocale.h>
00037
00038 #include "keduvocdocument.h"
00039 #include "keduvocexpression.h"
00040 #include "vocabsettings.h"
00041 #include "kanagramsettings.h"
00042
00043
00044 VocabEdit::VocabEdit(QWidget *parent, const QString &fileName) : QDialog(parent), m_fileName("")
00045 {
00046 setupUi(this);
00047
00048 if(!fileName.isEmpty())
00049 {
00050 m_fileName = fileName;
00051 KEduVocDocument *doc = new KEduVocDocument(this);
00052 doc->open(KUrl::fromPath(m_fileName));
00053 for(int i = 0; i < doc->lesson()->entryCount(KEduVocLesson::Recursive); i++)
00054 {
00055 KEduVocExpression expr = *doc->lesson()->entries(KEduVocLesson::Recursive).value(i);
00056 m_vocabList.append(expr);
00057 lboxWords->addItem(doc->lesson()->entries(KEduVocLesson::Recursive).value(i)->translation(0)->text());
00058 }
00059 txtVocabName->setText(doc->title());
00060 txtDescription->setText(doc->documentComment());
00061 }
00062
00063 connect(btnSave, SIGNAL(clicked()), this, SLOT(slotSave()));
00064 connect(btnNewWord, SIGNAL(clicked()), this, SLOT(slotNewWord()));
00065 connect(btnRemoveWord, SIGNAL(clicked()), this, SLOT(slotRemoveWord()));
00066 connect(btnClose, SIGNAL(clicked()), this, SLOT(slotClose()));
00067
00068 connect(txtWord, SIGNAL(textChanged(const QString &)), this, SLOT(slotWordTextChanged(const QString &)));
00069 connect(txtHint, SIGNAL(textChanged(const QString &)), this, SLOT(slotHintTextChanged(const QString &)));
00070
00071
00072
00073 connect(txtVocabName, SIGNAL(textChanged(const QString &)), this, SLOT(slotTextChanged(const QString &)));
00074 connect(txtDescription, SIGNAL(textChanged(const QString &)), this, SLOT(slotTextChanged(const QString &)));
00075
00076 connect(lboxWords, SIGNAL(itemSelectionChanged()), this, SLOT(slotSelectionChanged()));
00077
00078
00079 m_textChanged = false;
00080 }
00081
00082 VocabEdit::~VocabEdit()
00083 {
00084 }
00085
00086 void VocabEdit::slotSave()
00087 {
00088 KEduVocDocument *doc = new KEduVocDocument(this);
00089 doc->setTitle(txtVocabName->text());
00090 doc->setDocumentComment(txtDescription->text());
00091 for(int i = 0; i < m_vocabList.size(); i++)
00092 {
00093 doc->lesson()->appendEntry(&m_vocabList[i]);
00094 }
00095
00096 QString fileName = txtVocabName->text().toLower().remove(' ') + ".kvtml";
00097 doc->saveAs(KUrl::fromPath(KGlobal::dirs()->saveLocation("data", "kvtml/" + KanagramSettings::dataLanguage()) + fileName), KEduVocDocument::Automatic, "kanagram");
00098
00099 VocabSettings *settings = (VocabSettings*)this->parentWidget();
00100 settings->refreshView();
00101
00102 if(m_textChanged)
00103 m_textChanged = false;
00104 }
00105
00106 void VocabEdit::slotClose()
00107 {
00108
00109 if(m_textChanged && lboxWords->count() > 0)
00110 {
00111 int code = KMessageBox::warningYesNo(this, i18n("Would you like to save your changes?"), i18n("Save Changes Dialog"));
00112 if(code == KMessageBox::Yes)
00113 {
00114 slotSave();
00115 close();
00116 }
00117 else
00118 close();
00119 }
00120 else
00121 close();
00122 }
00123
00124 void VocabEdit::slotNewWord()
00125 {
00126 lboxWords->addItem("New Item");
00127 KEduVocExpression expr = KEduVocExpression();
00128 m_vocabList.append(expr);
00129
00130 if(m_textChanged == false)
00131 m_textChanged = true;
00132 }
00133
00134 void VocabEdit::slotSelectionChanged()
00135 {
00136
00137 disconnect(txtWord, SIGNAL(textChanged(const QString &)), this, SLOT(slotWordTextChanged(const QString &)));
00138 disconnect(txtHint, SIGNAL(textChanged(const QString &)), this, SLOT(slotHintTextChanged(const QString &)));
00139 if(lboxWords->currentRow() >= 0)
00140 {
00141 txtWord->setText(m_vocabList[lboxWords->currentRow()].translation(0)->text());
00142 txtHint->setText(m_vocabList[lboxWords->currentRow()].translation(0)->comment());
00143 }
00144 connect(txtWord, SIGNAL(textChanged(const QString &)), this, SLOT(slotWordTextChanged(const QString &)));
00145 connect(txtHint, SIGNAL(textChanged(const QString &)), this, SLOT(slotHintTextChanged(const QString &)));
00146 }
00147
00148 void VocabEdit::slotWordTextChanged(const QString &changes)
00149 {
00150
00151 if(lboxWords->currentRow() != -1)
00152 {
00153 m_vocabList[lboxWords->currentRow()].setTranslation(0, changes);
00154 lboxWords->currentItem()->setText(changes);
00155 }
00156
00157 if(m_textChanged == false)
00158 m_textChanged = true;
00159 }
00160
00161 void VocabEdit::slotHintTextChanged(const QString &changes)
00162 {
00163
00164 if(lboxWords->currentRow() != -1)
00165 m_vocabList[lboxWords->currentRow()].translation(0)->setComment(changes);
00166
00167 if(m_textChanged == false)
00168 m_textChanged = true;
00169 }
00170
00171 void VocabEdit::slotTextChanged(const QString &changes)
00172 {
00173
00174
00175 if(m_textChanged == false)
00176 m_textChanged = true;
00177
00178
00179 (void)changes;
00180 }
00181
00182 void VocabEdit::slotRemoveWord()
00183 {
00184 if (lboxWords->count()) {
00185 m_vocabList.erase(m_vocabList.begin() + lboxWords->currentRow());
00186 delete lboxWords->takeItem(lboxWords->currentRow());
00187 }
00188
00189 if(m_textChanged == false)
00190 m_textChanged = true;
00191 }
00192
00193 #include "vocabedit.moc"