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

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • editor
multiplechoicewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  copyright : (C) 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
4  (C) 2005-2007 Peter Hedlund <peter.hedlund@kdemail.net>
5  (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
6 
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "multiplechoicewidget.h"
19 
20 #include <keduvoctranslation.h>
21 #include <keduvocexpression.h>
22 #include <KDebug>
23 
24 #include <QStringListModel>
25 #include <QDragEnterEvent>
26 
27 using namespace Editor;
28 
29 MultipleChoiceWidget::MultipleChoiceWidget(QWidget *parent) : QWidget(parent)
30 {
31  setupUi(this);
32 
33  connect(addChoiceButton, SIGNAL(clicked()), SLOT(slotAddChoiceButton()));
34  connect(removeChoiceButton, SIGNAL(clicked()), SLOT(slotRemoveChoiceButton()));
35 
36  m_choicesModel = new QStringListModel(this);
37  multipleChoiceListView->setModel(m_choicesModel);
38 
39  connect(m_choicesModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), SLOT(slotDataChanged(const QModelIndex &, const QModelIndex &)));
40 
41  multipleChoiceListView->setAcceptDrops(true);
42  multipleChoiceListView->installEventFilter(this);
43 
44  setEnabled(false);
45 }
46 
47 
48 void MultipleChoiceWidget::slotDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight)
49 {
50  Q_UNUSED(topLeft)
51  Q_UNUSED(bottomRight)
52  m_translation->multipleChoice() = m_choicesModel->stringList();
53  removeChoiceButton->setEnabled(m_translation && m_translation->multipleChoice().count() > 0);
54 }
55 
56 
57 void MultipleChoiceWidget::setTranslation(KEduVocExpression * entry, int translation)
58 {
59  if (entry) {
60  m_translation = entry->translation(translation);
61  } else {
62  m_translation = 0;
63  }
64 
65  if (m_translation) {
66  setEnabled(true);
67  m_choicesModel->setStringList(m_translation->multipleChoice());
68  removeChoiceButton->setEnabled(m_translation->multipleChoice().count() > 0);
69  } else {
70  setEnabled(false);
71  }
72  removeChoiceButton->setEnabled(m_translation && m_translation->multipleChoice().count() > 0);
73 }
74 
75 
76 void MultipleChoiceWidget::slotAddChoiceButton()
77 {
78  m_choicesModel->insertRow(m_choicesModel->rowCount());
79  QModelIndex index(m_choicesModel->index(m_choicesModel->rowCount() - 1));
80  m_choicesModel->setData(index, "");
81  multipleChoiceListView->scrollTo(index);
82  multipleChoiceListView->setCurrentIndex(index);
83  multipleChoiceListView->edit(index);
84 }
85 
86 
87 void MultipleChoiceWidget::slotRemoveChoiceButton()
88 {
89  QModelIndex index = multipleChoiceListView->selectionModel()->currentIndex();
90  if (index.isValid()) {
91  m_choicesModel->removeRows(index.row(), 1, QModelIndex());
92  } else {
93  m_choicesModel->removeRows(m_choicesModel->rowCount(QModelIndex()) - 1, 1, QModelIndex());
94  }
95  m_translation->multipleChoice() = m_choicesModel->stringList();
96  removeChoiceButton->setEnabled(m_translation && m_translation->multipleChoice().count() > 0);
97 }
98 
99 
100 bool MultipleChoiceWidget::eventFilter(QObject * obj, QEvent * event)
101 {
102  if (obj == multipleChoiceListView) {
103  if (event->type() == QEvent::DragEnter) {
104  QDragEnterEvent *dragEnterEvent = static_cast<QDragEnterEvent *>(event);
105  kDebug() << "DragEnter mime format: " << dragEnterEvent->format();
106  if (dragEnterEvent->mimeData()->hasText()) {
107  event->accept();
108  }
109  return true;
110  }
111 
112  if (event->type() == QEvent::DragMove) {
113  event->accept();
114  return true;
115  }
116 
117  if (event->type() == QEvent::Drop) {
118  QDropEvent *dropEvent = static_cast<QDropEvent *>(event);
119  kDebug() << "You dropped onto me: " << dropEvent->mimeData()->text();
120 
121  QStringList choices = dropEvent->mimeData()->text().split('\n');
122  foreach(const QString & choice, choices) {
123  m_choicesModel->insertRow(multipleChoiceListView->model()->rowCount());
124  m_choicesModel->setData(m_choicesModel->index(multipleChoiceListView->model()->rowCount() - 1), choice);
125  }
126  return true;
127  }
128  }
129  return QObject::eventFilter(obj, event);
130 }
131 
132 
133 #include "multiplechoicewidget.moc"
134 
QAbstractItemModel::insertRow
bool insertRow(int row, const QModelIndex &parent)
Editor::MultipleChoiceWidget::eventFilter
bool eventFilter(QObject *obj, QEvent *event)
Definition: multiplechoicewidget.cpp:100
QModelIndex
QEvent
QWidget
QEvent::type
Type type() const
QWidget::dropEvent
virtual void dropEvent(QDropEvent *event)
QWidget::setupUi
void setupUi(QWidget *widget)
QDropEvent::mimeData
const QMimeData * mimeData() const
multiplechoicewidget.h
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QWidget::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *event)
QDropEvent::format
virtual const char * format(int n) const
QStringListModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
QMimeData::hasText
bool hasText() const
QStringListModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
QModelIndex::isValid
bool isValid() const
QStringListModel::stringList
QStringList stringList() const
QWidget::setEnabled
void setEnabled(bool)
QStringListModel::removeRows
virtual bool removeRows(int row, int count, const QModelIndex &parent)
QMimeData::text
QString text() const
Editor::MultipleChoiceWidget::setTranslation
void setTranslation(KEduVocExpression *entry, int translation)
Definition: multiplechoicewidget.cpp:57
QObject
QAbstractListModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QDropEvent
QModelIndex::row
int row() const
QObject::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
QString
QStringListModel
QStringList
QDragEnterEvent
Editor::MultipleChoiceWidget
Definition: multiplechoicewidget.h:40
Editor::MultipleChoiceWidget::MultipleChoiceWidget
MultipleChoiceWidget(QWidget *parent=0)
Definition: multiplechoicewidget.cpp:29
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QWidget::event
virtual bool event(QEvent *event)
QStringListModel::setStringList
void setStringList(const QStringList &strings)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal