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

keduca

kcontroladdedit.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           kcontroladdedit.cpp  -  description
00003                              -------------------
00004     begin                : Sun May 27 2001
00005     copyright            : (C) 2001 by Javier Campos
00006     email                : javi@asyris.org
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "kcontroladdedit.h"
00019 #include "kcontroladdedit.moc"
00020 
00021 // KDE includes
00022 #include <klocale.h>
00023 #include <kconfig.h>
00024 #include <kurlrequester.h>
00025 #include <kcombobox.h>
00026 #include <knuminput.h>
00027 #include <klistview.h>
00028 //#include <.h>
00029 
00030 // Qt includes
00031 #include <qlabel.h>
00032 #include <qtextedit.h>
00033 
00034 KControlAddEdit::KControlAddEdit(QWidget *parent, const char *name, bool modal, WFlags f)
00035     : KControlAddEditBase(parent, name, modal, f),
00036       _currentAnswerItem(0)
00037 {
00038     configRead();
00039 }
00040 
00041 KControlAddEdit::~KControlAddEdit()
00042 {
00043 }
00044 
00046 void KControlAddEdit::init(FileRead *keducaFile, bool edit)
00047 {
00048     setCurrentAnswerItem(0);
00049     _keducaFile = keducaFile;
00050     _editMode = edit;
00051 
00052     _listAnswers->setSorting(-1);
00053     
00054     if( _editMode )
00055     {
00056         setCaption(i18n("Modify Question"));
00057         fillPage();
00058     }else{
00059         setCaption(i18n("Add Questions"));
00060         slotQuestionTypeChanged( 0 );
00061     }
00062 
00063     // settings for Question page
00064     setHelpEnabled ( _pageQuestion, false );
00065     setNextEnabled( _pageQuestion, false );
00066 
00067     // settings for Answer page
00068     setHelpEnabled ( _pageAnswer, false );
00069     setFinishEnabled( _pageAnswer, true );
00070 }
00071 
00073 void KControlAddEdit::fillPage()
00074 {
00075     _questionText->setText(         _keducaFile->getQuestion( FileRead::QF_TEXT )       );
00076     if( !_keducaFile->getQuestion( FileRead::QF_PICTURE ).isEmpty() )
00077     {
00078         _questionPreview->setPixmap(_keducaFile->getQuestion(FileRead::QF_PICTURE));
00079         _answerPreview->setPixmap(_keducaFile->getQuestion(FileRead::QF_PICTURE));
00080         _questionPicture->setURL(   _keducaFile->getQuestion( FileRead::QF_PICTURE ) );
00081     }
00082     _questionType->setCurrentItem( _keducaFile->getQuestionInt( FileRead::QF_TYPE )-1 );
00083     slotQuestionTypeChanged( _questionType->currentItem() );
00084     _questionPoint->setValue(       _keducaFile->getQuestionInt( FileRead::QF_POINTS )  );
00085     _questionTip->setText(          _keducaFile->getQuestion( FileRead::QF_TIP )        );
00086     _questionExplain->setText(  _keducaFile->getQuestion( FileRead::QF_EXPLAIN ) );
00087     _questionTime->setValue(        _keducaFile->getQuestionInt( FileRead::QF_TIME )    );
00088 
00089     _listAnswers->clear();
00090     _keducaFile->recordAnswerFirst();
00091     while( !_keducaFile->recordAnswerEOF() )
00092     {
00093         QListViewItem *newItem = new QListViewItem(_listAnswers);
00094         newItem->setText(0,_keducaFile->getAnswer( FileRead::AF_TEXT ));
00095         newItem->setText(1,_keducaFile->getAnswerValue()?i18n("True"):i18n("False"));
00096         newItem->setText(2,QString::number(_keducaFile->getAnswerPoints()));
00097 
00098         _keducaFile->recordAnswerNext();
00099     };
00100 }
00101 
00102 void KControlAddEdit::setCurrentAnswerItem(QListViewItem *item)
00103 {
00104     _currentAnswerItem = item;
00105     if (item)
00106         _buttonInsert->setText(i18n("&Apply"));
00107     else
00108         _buttonInsert->setText(i18n("&Add"));
00109 }
00110 
00112 void KControlAddEdit::slotAddAnswer()
00113 {
00114     if( !_answerText->text().isEmpty() )
00115     {
00116         // add new listview item
00117         QListViewItem *newItem = _currentAnswerItem
00118                                  ? _currentAnswerItem
00119                                  : new QListViewItem(_listAnswers,_listAnswers->lastItem());
00120         newItem->setText(0,_answerText->text());
00121         newItem->setText(1,_answerValue->currentItem() == 1 ?i18n("True"):i18n("False"));
00122         newItem->setText(2,QString::number(_answerPoints->value()));
00123 
00124         // reset values
00125         newItem->setSelected(false);
00126         resetAnswerValues();
00127     }
00128 }
00129 
00130 void KControlAddEdit::resetAnswerValues()
00131 {
00132     _answerText->setText("");
00133     _answerValue->setCurrentItem( 0 );
00134     _answerPoints->setValue(0);
00135     _answerText->setFocus();
00136     setCurrentAnswerItem(0);
00137     _buttonUp->setEnabled(false);
00138     _buttonDown->setEnabled(false);
00139     _buttonRemove->setEnabled(false);
00140     _buttonInsert->setEnabled(false);
00141 }
00142 
00144 void KControlAddEdit::slotRemoveAnswer()
00145 {
00146     delete _listAnswers->currentItem();
00147     resetAnswerValues();
00148 }
00149 
00151 void KControlAddEdit::slotMoveUpAnswer()
00152 {
00153     QListViewItem *item = _listAnswers->currentItem();
00154 
00155     if (item && item->itemAbove())
00156         item->itemAbove()->moveItem(item);
00157 
00158     _buttonUp->setEnabled(item->itemAbove()!=0);
00159     _buttonDown->setEnabled(item->itemBelow()!=0);
00160 }
00161 
00163 void KControlAddEdit::slotMoveDownAnswer()
00164 {
00165     QListViewItem *item = _listAnswers->currentItem();
00166 
00167     if (item)
00168         item->moveItem(item->itemBelow());
00169 
00170     _buttonUp->setEnabled(item->itemAbove()!=0);
00171     _buttonDown->setEnabled(item->itemBelow()!=0);
00172 }
00173 
00174 void KControlAddEdit::slotAnswerSelected(QListViewItem *item)
00175 {
00176     if (!item)
00177         return;
00178 
00179     setCurrentAnswerItem(item);
00180 
00181     // set the answer fields to the value of the selected answer
00182     _answerText->setText(item->text(0));
00183     _answerValue->setCurrentItem(item->text(1) == i18n("True")?1:0);
00184     _answerPoints->setValue(item->text(2).toInt());
00185 
00186     // enable/disable the buttons as appropriate
00187     _buttonRemove->setEnabled(true);
00188     _buttonInsert->setEnabled(true);
00189     _buttonDown->setEnabled(item->itemBelow()!=0);
00190     _buttonUp->setEnabled(item->itemAbove()!=0);
00191 }
00192 
00194 void KControlAddEdit::accept()
00195 {
00196     if( !_editMode )
00197         _keducaFile->insertQuestion();
00198 
00199     addQuestion();
00200     configWrite();
00201     done( QDialog::Accepted );
00202 }
00203 
00205 void KControlAddEdit::addQuestion()
00206 {
00207     _keducaFile->setQuestion( FileRead::QF_TEXT, _questionText->text() );
00208     _keducaFile->setQuestion( FileRead::QF_TYPE, _questionType->currentItem()+1 );
00209     _keducaFile->setQuestion( FileRead::QF_PICTURE, _questionPicture->url() );
00210     _keducaFile->setQuestion( FileRead::QF_POINTS, _questionPoint->value() );
00211     _keducaFile->setQuestion( FileRead::QF_TIME, _questionTime->value() );
00212     _keducaFile->setQuestion( FileRead::QF_TIP, _questionTip->text() );
00213     _keducaFile->setQuestion( FileRead::QF_EXPLAIN, _questionExplain->text() );
00214 
00215     _keducaFile->clearAnswers();
00216     QListViewItem *item = _listAnswers->firstChild();
00217     while (item) {
00218         _keducaFile->setAnswer(
00219             item->text(0), // The Answer text
00220             item->text(1)==i18n("True"), // the validity of the answer
00221             item->text(2).toInt()); // the points
00222         item = item->nextSibling();
00223     }
00224 }
00226 void KControlAddEdit::configRead()
00227 {
00228     KConfig *config = KGlobal::config();
00229     config->setGroup("AddModify Window");
00230     QSize defaultSize(500,400);
00231     resize( config->readSizeEntry("Geometry", &defaultSize ) );
00232 }
00233 
00235 void KControlAddEdit::configWrite()
00236 {
00237     KConfig *config = KGlobal::config();
00238     config->setGroup("AddModify Window");
00239     config->writeEntry("Geometry", size() );
00240     config->sync();
00241 }
00242 
00244 void KControlAddEdit::slotDataChanged()
00245 {
00246     if( _pageQuestion == currentPage() )
00247     {
00248         nextButton()->setEnabled( !_questionText->text().isEmpty() );
00249     }
00250     if( _pageAnswer == currentPage() )
00251     {
00252         _buttonInsert->setEnabled( !_answerText->text().isEmpty() );
00253     }
00254 }
00255 
00256 
00258 void KControlAddEdit::slotPreviewImage( const QString &text)
00259 {
00260     if( text.isEmpty() ) { _questionPreview->setText(""); _answerPreview->setText(""); return; }
00261 
00262     _questionPreview->setPixmap( QPixmap( text ) );
00263     _answerPreview->setPixmap( QPixmap( text ) );
00264 }
00265 
00267 void KControlAddEdit::showPage(QWidget *page)
00268 {
00269     QWizard::showPage(page);
00270     slotDataChanged();
00271 
00272     if ( page == _pageQuestion ) {
00273         _questionText->setFocus();
00274     } else if ( page == _pageAnswer ) {
00275         _answerText->setFocus();
00276     }
00277 }
00278 
00280 void KControlAddEdit::slotQuestionTypeChanged( int index )
00281 {
00282     switch( index+1 )
00283     {
00284     case 1:
00285         _questionPoint->setEnabled( false );
00286         _answerPoints->setEnabled( false );
00287         _questionPoint->setValue(0);
00288         _answerPoints->setValue(0);
00289         break;
00290     case 2:
00291         _questionPoint->setEnabled( true );
00292         _answerPoints->setEnabled( false );
00293         _answerPoints->setValue(0);
00294         break;
00295     case 3:
00296         _questionPoint->setEnabled( false );
00297         _answerPoints->setEnabled( true );
00298         _questionPoint->setValue(0);
00299         break;
00300     default:
00301         break;
00302     }
00303 }

keduca

Skip menu "keduca"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
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