• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

kanagram

vocabedit.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Joshua Keel                                     *
00003  *   joshuakeel@gmail.com                                                  *
00004  *                                                                         *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  *   This program is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU General Public License for more details.                          *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU General Public License     *
00017  *   along with this program; if not, write to the                         *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
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     //Connect the name and description boxes to a general textChanged slot, so that we can keep track of
00072     //whether they've been changed or not
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     //Has anything in the dialog changed?
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     //Has anything in the dialog changed?
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     //A little hack to make things work right
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     //Make sure there actually is a currentRow()
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     //Make sure there actually is a currentItem()
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     //Make sure we know when text has been modified and not saved, so we
00174     //can notify the user
00175     if(m_textChanged == false)
00176         m_textChanged = true;
00177 
00178     //Make gcc happy
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"

kanagram

Skip menu "kanagram"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal