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

rocs/VisualEditor

  • sources
  • kde-4.14
  • kdeedu
  • rocs
  • VisualEditor
  • Tools
  • AssignValues
AssignValuesWidget.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright (C) 2011-2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License as
7  published by the Free Software Foundation; either version 2 of
8  the License, or (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "AssignValuesWidget.h"
20 #include "ui_AssignValuesWidget.h"
21 
22 
23 #include "Document.h"
24 #include "DataStructure.h"
25 #include "DocumentManager.h"
26 #include "Pointer.h"
27 #include "Data.h"
28 #include "Modifiers/ValueModifier.h"
29 
30 #include <limits.h>
31 #include <KLocale>
32 #include <KDialog>
33 #include <KComboBox>
34 #include <KPushButton>
35 
36 #include <QtGui/QDesktopWidget>
37 #include <QtGui/QGridLayout>
38 #include <QtGui/QLabel>
39 #include <QtGui/QLineEdit>
40 #include <QtGui/QPushButton>
41 #include <QtGui/QSpinBox>
42 #include <QtCore/QMap>
43 #include <QtCore/QPair>
44 
45 #include <boost/random/mersenne_twister.hpp>
46 #include <boost/random/uniform_int.hpp>
47 #include <boost/random/uniform_real.hpp>
48 #include <boost/random/variate_generator.hpp>
49 
50 
51 class QPushButton;
52 
53 AssignValuesWidget::AssignValuesWidget(Document* graphDoc, QWidget* parent)
54  : KDialog(parent)
55 {
56  QWidget *widget = new QWidget(this);
57  ui = new Ui::AssignValuesWidget;
58  ui->setupUi(widget);
59  setMainWidget(widget);
60 
61  setCaption(i18n("Assign Values"));
62  setButtons(KDialog::Apply | KDialog::Cancel | KDialog::Ok);
63  ui->buttonShowAdvanced->setIcon(KIcon("rocsadvancedsetup"));
64  KDialog::centerOnScreen(widget, -3);
65 
66  // set types
67  Document* document = DocumentManager::self().activeDocument();
68  ui->dataType->addItem(i18n("All Data Types"), -1);
69  foreach (int typeId, document->dataTypeList()) {
70  ui->dataType->addItem(document->dataType(typeId)->name(), typeId);
71  }
72  ui->connectionType->addItem(i18n("All Connection Types"), -1);
73  foreach (int typeId, document->pointerTypeList()) {
74  ui->connectionType->addItem(document->pointerType(typeId)->name(), typeId);
75  }
76 
77  // set selection combos only enabled if they are used for assignment
78  ui->dataType->setEnabled(ui->applyToDataElements->isChecked());
79  ui->connectionType->setEnabled(ui->applyToConnections->isChecked());
80  connect(ui->applyToConnections, SIGNAL(toggled(bool)), ui->connectionType, SLOT(setEnabled(bool)));
81  connect(ui->applyToDataElements, SIGNAL(toggled(bool)), ui->dataType, SLOT(setEnabled(bool)));
82 
83  // set all available properties as possible completes
84  KCompletion *complete = ui->propertyName->completionObject();
85  foreach (int typeId, document->dataTypeList()) {
86  complete->insertItems(document->dataType(typeId)->properties());
87  }
88  foreach (int typeId, document->pointerTypeList()) {
89  complete->insertItems(document->pointerType(typeId)->properties());
90  }
91  ui->propertyName->setContextMenuPolicy(Qt::DefaultContextMenu);
92  ui->propertyName->setPlaceholderText(i18n("Enter Property Name"));
93  connect(ui->propertyName, SIGNAL(textChanged(QString)), this, SLOT(updateApplyButtonStates()));
94 
95  connect(this, SIGNAL(applyClicked()), this, SLOT(assignValues()));
96  connect(this, SIGNAL(okClicked()), this, SLOT(assignValues()));
97 
98  graphDoc_ = graphDoc;
99  updateApplyButtonStates();
100 
101  // set random seeds
102  qint64 currentTime = QDateTime::currentMSecsSinceEpoch();
103  uint badRandomSeed = qHash(currentTime) % 99999;
104  badRandomSeed = (badRandomSeed == 0) ? 1 : badRandomSeed;
105  ui->spinBoxIntegerGeneratorSeed->setValue(badRandomSeed);
106  ui->spinBoxFloatGeneratorSeed->setValue(badRandomSeed);
107 
108  // set visibility for advanced options
109  // TODO move to containers for easier handling
110  ui->checkBoxOverwriteValues->setVisible(false);
111  ui->label->setVisible(false);
112  ui->spinBoxIntegerGeneratorSeed->setVisible(false);
113  ui->label_7->setVisible(false);
114  ui->spinBoxFloatGeneratorSeed->setVisible(false);
115  ui->label_9->setVisible(false);
116  ui->LabelInitialString->setVisible(false);
117  ui->LineEditInitialString->setVisible(false);
118 }
119 
120 
121 AssignValuesWidget::~AssignValuesWidget()
122 {
123  delete ui;
124 }
125 
126 
127 void AssignValuesWidget::addDataStructures(const QStringList& dsNames)
128 {
129  ui->dataStructuresCombo->insertItems(0, dsNames);
130 }
131 
132 
133 void AssignValuesWidget::updateApplyButtonStates()
134 {
135  if (Document::isValidIdentifier(ui->propertyName->text())) {
136  button(KDialog::Apply)->setEnabled(true);
137  button(KDialog::Ok)->setEnabled(true);
138  }
139  else {
140  button(KDialog::Apply)->setEnabled(false);
141  button(KDialog::Ok)->setEnabled(false);
142  }
143 }
144 
145 
146 void AssignValuesWidget::assignValues()
147 {
148  DataStructurePtr ds;
149  QList< DataStructurePtr > dsList = DocumentManager::self().activeDocument()->dataStructures();
150 
151  // no data structures present at active document
152  if (ui->dataStructuresCombo->count() == 0) {
153  return;
154  }
155 
156  ds = dsList[ui->dataStructuresCombo->currentIndex()];
157  QString property = ui->propertyName->text();
158  if (!ds || property.isEmpty()) {
159  return;
160  }
161 
162  bool overrideValues = ui->checkBoxOverwriteValues->isChecked();
163 
164  // select all pointer lists and (if necessary) add specified property to them
165  DataList dataList;
166  PointerList pointerList;
167  if (ui->applyToDataElements) {
168  int typeSelection = ui->dataType->itemData(ui->dataType->currentIndex()).toInt();
169  // case: all properties are selected
170  if (typeSelection == -1) {
171  foreach (int typeId, ds->document()->dataTypeList()) {
172  if (!ds->document()->dataType(typeId)->properties().contains(property)) {
173  ds->document()->dataType(typeId)->addProperty(property);
174  }
175  dataList << ds->dataList(typeId);
176  }
177  }
178  if (typeSelection >= 0) {
179  if (!ds->document()->dataType(typeSelection)->properties().contains(property)) {
180  ds->document()->dataType(typeSelection)->addProperty(property);
181  }
182  dataList = ds->dataList(typeSelection);
183  }
184  }
185  if (ui->applyToConnections) {
186  int typeSelection = ui->connectionType->itemData(ui->connectionType->currentIndex()).toInt();
187  // case: all properties are selected
188  if (typeSelection == -1) {
189  foreach (int typeId, ds->document()->pointerTypeList()) {
190  if (!ds->document()->pointerType(typeId)->properties().contains(property)) {
191  ds->document()->pointerType(typeId)->addProperty(property);
192  }
193  pointerList << ds->pointers(typeId);
194  }
195  }
196  if (typeSelection >= 0) {
197  if (!ds->document()->pointerType(typeSelection)->properties().contains(property)) {
198  ds->document()->pointerType(typeSelection)->addProperty(property);
199  }
200  pointerList = ds->pointers(typeSelection);
201  }
202  }
203 
204  // assign values
205  ValueModifier modifier;
206  switch ((AssignMethod) ui->comboBoxMethod->currentIndex()) {
207  case ID: {
208  int start = ui->spinBoxIDStartValue->value();
209 
210  if (ui->applyToDataElements->isChecked()) {
211  modifier.enumerate(dataList, property, start, "", overrideValues);
212  }
213  if (ui->applyToConnections->isChecked()) {
214  modifier.enumerate(pointerList, property, start, "", overrideValues);
215  }
216  break;
217  }
218  case ALPHA: {
219  QString start = ui->LineEditInitialString->text();
220 
221  if (ui->applyToDataElements->isChecked()) {
222  modifier.enumerateAlpha(dataList, property, start, overrideValues);
223  }
224  if (ui->applyToConnections->isChecked()) {
225  modifier.enumerateAlpha(pointerList, property, start, overrideValues);
226  }
227  break;
228  }
229  case ID_ALPHA: {
230  int start = ui->SpinBoxAlphaNumericIDStart->value();
231  QString prefix = ui->LineEditAlphaNumericPrefix->text();
232 
233  if (ui->applyToDataElements->isChecked()) {
234  modifier.enumerate(dataList, property, start, prefix, overrideValues);
235  }
236  if (ui->applyToConnections->isChecked()) {
237  modifier.enumerate(pointerList, property, start, prefix, overrideValues);
238  }
239  break;
240  }
241  case UNIFORM_INTEGER: {
242  int seed = ui->spinBoxIntegerGeneratorSeed->value();
243  int lowerLimit = ui->spinBoxIntegerLowerLimit->value();
244  int upperLimit = ui->spinBoxIntegerUpperLimit->value();
245 
246  if (ui->applyToDataElements->isChecked()) {
247  modifier.assignRandomIntegers(dataList, property, lowerLimit, upperLimit, seed, overrideValues);
248  }
249  if (ui->applyToConnections->isChecked()) {
250  modifier.assignRandomIntegers(pointerList, property, lowerLimit, upperLimit, seed, overrideValues);
251  }
252  break;
253  }
254  case UNIFORM_FLOAT: {
255  int seed = ui->spinBoxFloatGeneratorSeed->value();
256  qreal lowerLimit = ui->spinBoxFloatLowerLimit->value();
257  qreal upperLimit = ui->spinBoxFloatUpperLimit->value();
258 
259  if (ui->applyToDataElements->isChecked()) {
260  modifier.assignRandomReals(dataList, property, lowerLimit, upperLimit, seed, overrideValues);
261  }
262  if (ui->applyToConnections->isChecked()) {
263  modifier.assignRandomReals(pointerList, property, lowerLimit, upperLimit, seed, overrideValues);
264  }
265  break;
266  }
267  case CONSTANT: {
268  QString constant = ui->lineEditConstantValue->text();
269 
270  if (ui->applyToDataElements->isChecked()) {
271  modifier.assignConstantValue(dataList, property, constant, overrideValues);
272  }
273  if (ui->applyToConnections->isChecked()) {
274  modifier.assignConstantValue(pointerList, property, constant, overrideValues);
275  }
276  }
277  }
278 }
QWidget
KDialog
AssignValuesWidget::addDataStructures
void addDataStructures(const QStringList &dsNames)
Add data structures to QComboBox of UI starting at position 0.
Definition: AssignValuesWidget.cpp:127
QDateTime::currentMSecsSinceEpoch
qint64 currentMSecsSinceEpoch()
QWidget::setEnabled
void setEnabled(bool)
QObject::property
QVariant property(const char *name) const
AssignValuesWidget::AssignValuesWidget
AssignValuesWidget(Document *graphDoc, QWidget *parent=0)
Definition: AssignValuesWidget.cpp:53
QString
QList
AssignValuesWidget::assignValues
void assignValues()
Assign values as specified at the UI.
Definition: AssignValuesWidget.cpp:146
AssignValuesWidget.h
QStringList
AssignValuesWidget::updateApplyButtonStates
void updateApplyButtonStates()
Only enable Apply/Ok buttons if the specified property is valid.
Definition: AssignValuesWidget.cpp:133
AssignValuesWidget::~AssignValuesWidget
~AssignValuesWidget()
Definition: AssignValuesWidget.cpp:121
QAbstractButton::toggled
void toggled(bool checked)
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
QWidget::setCaption
void setCaption(const QString &c)
QPushButton
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/VisualEditor

Skip menu "rocs/VisualEditor"
  • Main Page
  • 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