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

parley

multiplechoicewidget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 
00003     copyright     : (C) 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
00004                     (C) 2005-2007 Peter Hedlund <peter.hedlund@kdemail.net>
00005                     (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
00006 
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 "multiplechoicewidget.h"
00019 
00020 #include <keduvoctranslation.h>
00021 #include <keduvocexpression.h>
00022 #include <KDebug>
00023 
00024 #include <QStringListModel>
00025 #include <QDragEnterEvent>
00026 
00027 MultipleChoiceWidget::MultipleChoiceWidget(QWidget *parent) : QWidget(parent)
00028 {
00029     setupUi(this);
00030 
00031     connect(addChoiceButton, SIGNAL(clicked()), SLOT(slotAddChoiceButton()));
00032     connect(removeChoiceButton, SIGNAL(clicked()), SLOT(slotRemoveChoiceButton()));
00033 
00034     m_choicesModel = new QStringListModel(this);
00035     multipleChoiceListView->setModel(m_choicesModel);
00036 
00037     connect(m_choicesModel, SIGNAL(dataChanged ( const QModelIndex &, const QModelIndex & )), SLOT(slotDataChanged( const QModelIndex &, const QModelIndex & )));
00038 
00039     multipleChoiceListView->setAcceptDrops(true);
00040     multipleChoiceListView->installEventFilter(this);
00041 
00042     setEnabled(false);
00043 }
00044 
00045 
00046 void MultipleChoiceWidget::slotDataChanged( const QModelIndex & topLeft, const QModelIndex & bottomRight )
00047 {
00048     m_translation->multipleChoice() = m_choicesModel->stringList();
00049     removeChoiceButton->setEnabled(m_translation && m_translation->multipleChoice().count() > 0);
00050 }
00051 
00052 
00053 void MultipleChoiceWidget::setTranslation(KEduVocExpression * entry, int translation)
00054 {
00055     if (entry) {
00056         m_translation = entry->translation(translation);
00057     } else {
00058         m_translation = 0;
00059     }
00060 
00061     if (m_translation) {
00062         setEnabled(true);
00063         m_choicesModel->setStringList(m_translation->multipleChoice());
00064         removeChoiceButton->setEnabled(m_translation->multipleChoice().count() > 0);
00065     } else {
00066         setEnabled(false);
00067     }
00068     removeChoiceButton->setEnabled(m_translation && m_translation->multipleChoice().count() > 0);
00069 }
00070 
00071 
00072 void MultipleChoiceWidget::slotAddChoiceButton()
00073 {
00074     m_choicesModel->insertRow(m_choicesModel->rowCount());
00075     QModelIndex index(m_choicesModel->index(m_choicesModel->rowCount() - 1));
00076     m_choicesModel->setData(index, "");
00077     multipleChoiceListView->scrollTo(index);
00078     multipleChoiceListView->setCurrentIndex(index);
00079     multipleChoiceListView->edit(index);
00080 }
00081 
00082 
00083 void MultipleChoiceWidget::slotRemoveChoiceButton()
00084 {
00085     QModelIndex index = multipleChoiceListView->selectionModel()->currentIndex();
00086     if (index.isValid()) {
00087         m_choicesModel->removeRows(index.row(), 1, QModelIndex());
00088     } else {
00089         m_choicesModel->removeRows(m_choicesModel->rowCount(QModelIndex()) - 1, 1, QModelIndex());
00090     }
00091     m_translation->multipleChoice() = m_choicesModel->stringList();
00092     removeChoiceButton->setEnabled(m_translation && m_translation->multipleChoice().count() > 0);
00093 }
00094 
00095 
00096 bool MultipleChoiceWidget::eventFilter(QObject * obj, QEvent * event)
00097 {
00098     if (obj == multipleChoiceListView) {
00099         if (event->type() == QEvent::DragEnter) {
00100             QDragEnterEvent *dragEnterEvent = static_cast<QDragEnterEvent *>(event);
00101             kDebug() << "DragEnter mime format: " << dragEnterEvent->format();
00102             if (dragEnterEvent->mimeData()->hasText()) {
00103                 event->accept();
00104             }
00105             return true;
00106         }
00107 
00108         if (event->type() == QEvent::DragMove) {
00109             event->accept();
00110             return true;
00111         }
00112 
00113         if (event->type() == QEvent::Drop) {
00114             QDropEvent *dropEvent = static_cast<QDropEvent *>(event);
00115             kDebug() << "You dropped onto me: " << dropEvent->mimeData()->text();
00116 
00117             QStringList choices = dropEvent->mimeData()->text().split('\n');
00118             foreach(const QString &choice, choices) {
00119                 m_choicesModel->insertRow(multipleChoiceListView->model()->rowCount());
00120                 m_choicesModel->setData(m_choicesModel->index(multipleChoiceListView->model()->rowCount()-1), choice);
00121             }
00122             return true;
00123         }
00124     }
00125     return QObject::eventFilter(obj, event);
00126 }
00127 
00128 
00129 #include "multiplechoicewidget.moc"
00130 

parley

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

kdeedu

Skip menu "kdeedu"
  •     lib
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.9-20090814
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