• 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
  • TransformEdges
TransformEdgesWidget.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright (C) 2011 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 "TransformEdgesWidget.h"
20 #include "ui_TransformEdgesWidget.h"
21 
22 #include <limits.h>
23 #include <KLocale>
24 #include <KDialog>
25 #include <KComboBox>
26 
27 #include <QtGui/QDesktopWidget>
28 #include <QtGui/QGridLayout>
29 #include <QtGui/QLabel>
30 #include <QtGui/QLineEdit>
31 #include <QtGui/QPushButton>
32 #include <QtGui/QSpinBox>
33 #include <QtCore/QMap>
34 #include <QtCore/QPair>
35 
36 #include <boost/graph/adjacency_list.hpp>
37 #include <boost/graph/topology.hpp>
38 
39 #include <Document.h>
40 #include <DataStructure.h>
41 #include <DocumentManager.h>
42 #include "../DataStructures/Graph/GraphStructure.h"
43 #include <Pointer.h>
44 #include <Data.h>
45 
46 
47 using namespace Rocs;
48 
49 class QPushButton;
50 
51 TransformEdgesWidget::TransformEdgesWidget(Document* graphDoc, QWidget* parent)
52  : KDialog(parent)
53 {
54  graphDoc_ = graphDoc;
55 
56  QWidget *widget = new QWidget(this);
57  ui = new Ui::TransformEdgesWidget;
58  ui->setupUi(widget);
59  setMainWidget(widget);
60 
61  // other KDialog options
62  setCaption(i18nc("@title:window", "Transform Edges"));
63  setButtons(KDialog::Cancel | KDialog::Ok);
64  KDialog::centerOnScreen(widget, -3);
65 
66  connect(this, SIGNAL(okClicked()), this, SLOT(executeTransform()));
67 }
68 
69 
70 TransformEdgesWidget::~TransformEdgesWidget()
71 {
72  delete ui;
73 }
74 
75 
76 void TransformEdgesWidget::addDataStructures(QStringList dsNames)
77 {
78  ui->dataStructuresCombo->insertItems(0, dsNames);
79 }
80 
81 
82 void TransformEdgesWidget::executeTransform()
83 {
84  DataStructurePtr graph;
85  QList< DataStructurePtr > dsList = DocumentManager::self().activeDocument()->dataStructures();
86 
87  // no graph datastructures given at document
88  if (ui->dataStructuresCombo->count() == 0)
89  return;
90 
91  graph = dsList[ui->dataStructuresCombo->currentIndex()];
92  if (!graph)
93  return;
94 
95  if (ui->radioButtonMakeComplete->isChecked())
96  makeComplete(graph);
97  if (ui->radioButtonEraseEdges->isChecked())
98  removeAllEdges(graph);
99  if (ui->radioButtonReverseEdges->isChecked())
100  reverseAllEdges(graph);
101  if (ui->radioButtonMakeSpanningtree->isChecked())
102  makeSpanningTree(graph);
103 }
104 
105 
106 void TransformEdgesWidget::makeComplete(DataStructurePtr graph)
107 {
108  bool directed = graph->document()->pointerType(0)->direction() == PointerType::Unidirectional;
109 
110  //FIXME only default pointer type considered
111  foreach(PointerPtr e, graph->pointers(0)) {
112  e->remove();
113  }
114 
115  int size_i = graph->dataList(0).size() - 1;
116  for (int i = 0; i < size_i; ++i) {
117  for (int e = i + 1; e < graph->dataList(0).size(); ++e) {
118  graph->createPointer(graph->dataList(0).at(i), graph->dataList(0).at(e), 0);
119  if (directed) {
120  graph->createPointer(graph->dataList(0).at(e), graph->dataList(0).at(i), 0);
121  }
122  }
123  }
124 }
125 
126 
127 void TransformEdgesWidget::removeAllEdges(DataStructurePtr graph)
128 {
129  if (graph) {
130  //FIXME only default pointer type considered
131  foreach(PointerPtr e, graph->pointers(0)) {
132  e->remove();
133  }
134  }
135 }
136 
137 
138 void TransformEdgesWidget::reverseAllEdges(DataStructurePtr graph)
139 {
140  foreach(int typeId, graph->document()->pointerTypeList()) {
141  if (graph->document()->pointerType(typeId)->direction() == PointerType::Bidirectional) {
142  continue;
143  }
144  QList< QPair< DataPtr, DataPtr > > newPointers;
145  foreach(PointerPtr e, graph->pointers(typeId)) {
146  newPointers << QPair< DataPtr, DataPtr >(e->to(), e->from());
147  e->remove();
148  }
149 
150  for (int i = 0; i < newPointers.count(); i++) {
151  graph->createPointer(newPointers[i].first, newPointers[i].second, typeId);
152  }
153  }
154 }
155 
156 
157 qreal TransformEdgesWidget::makeSpanningTree(DataStructurePtr graph)
158 {
159  boost::shared_ptr<GraphStructure> graphDS = boost::static_pointer_cast<GraphStructure>(graph);
160  if (!graphDS)
161  return 0;
162 
163  QList< DataPtr > vertices = graphDS->dataList(0);
164  int n = vertices.size();
165 
166  /*
167  * the resulting spanning tree (MST)
168  */
169  QList< QPair<int, int> > MST;
170 
171  /*
172  * distance[i] denotes the distance between node i and the minimum spanning
173  * tree; initially this distance is infinity. Note that if i is already element
174  * in MST distance[i] is only a temporary variable and its value is undefined.
175  */
176  QMap<int, qreal> distance;
177  for (int i = 0; i < vertices.size(); i++) {
178  distance[i] = std::numeric_limits<unsigned int>::max();
179  }
180 
181  /*
182  * Indicator variable that is true if node is in tree, false otherwise.
183  * Initially all nodes are marked to be not in MST.
184  */
185  QMap<int, bool> inTree;
186  for (int i = 0; i < vertices.size(); i++) {
187  inTree[i] = false;
188  }
189 
190  /* weight[i][j] denotes distance between nodes i and j. If no
191  * path is present between i and j in the previous tree the weight
192  * must be set to 0.
193  */
194  QMap< QPair<int, int>, qreal> weight;
195 
196  for (int i = 0; i < n; i++) {
197  for (int j = 0; j < n; j++) {
198  if (i == j) weight[QPair<int, int>(i, j)] = 0;
199 
200  PointerList out;
201  out = vertices[i]->pointerList();
202 
203  for (int k = 0; k < out.size(); k++) {
204  if (out[k]->to() == vertices[j]) {
205  if (!out[k]->property("value").toString().isEmpty())
206  weight[QPair<int, int>(i, j)] = out[k]->property("value").toDouble();
207  else
208  weight[QPair<int, int>(i, j)] = 1;
209 
210  }
211  }
212  }
213  }
214 
215  /*
216  * successor[i] denotes the index of the node, to which i must be
217  * linked to in order to get distance distance[i]
218  */
219  QMap< int, int> successor;
220 
221  // start with first graph node
222  inTree[0] = true;
223 
224  // update distances
225  for (int i = 0; i < n; ++i) {
226  if ((weight[QPair<int, int>(0, i)] != 0) && (distance[i] > weight[QPair<int, int>(0, i)])) {
227  distance[i] = weight[QPair<int, int>(0, i)] ;
228  successor[i] = 0;
229  }
230  }
231 
232  qreal total = 0;
233  for (int treeSize = 1; treeSize < n; treeSize++) {
234  // Find node with the smallest distance to the tree
235  int min = -1;
236  for (int i = 0; i < n; ++i) {
237  if (inTree[i] == false) {
238  if ((min == -1) || (distance[min] > distance[i])) {
239  min = i;
240  }
241  }
242  }
243 
244  // add node to tree
245  MST << QPair<int, int>(successor[min], min);
246  inTree[min] = 1;
247  total += distance[min];
248 
249  // update distances
250  for (int i = 0; i < n; ++i) {
251  if ((weight[QPair<int, int>(min, i)] != 0) && (distance[i] > weight[QPair<int, int>(min, i)])) {
252  distance[i] = weight[QPair<int, int>(min, i)];
253  successor[i] = min;
254  }
255  }
256  }
257 
258  // erase all graph edges
259  removeAllEdges(graph);
260 
261  // refill with MST edges
262  for (int i = 0; i < MST.size(); i++) {
263  PointerPtr ptr = graph->createPointer(vertices[MST[i].first], vertices[MST[i].second], 0);
264 
265  if (weight[QPair<int, int>(MST[i].first, MST[i].second)] != 1) {
266  QString s;
267  s.setNum(weight[QPair<int, int>(MST[i].first, MST[i].second)]);
268  ptr->setProperty("value", s);
269  }
270  }
271 
272  return total;
273 }
QWidget
TransformEdgesWidget::executeTransform
void executeTransform()
Definition: TransformEdgesWidget.cpp:82
QList::remove
iterator remove(iterator pos)
QMap
KDialog
QList::size
int size() const
QList::count
int count(const T &value) const
QObject::property
QVariant property(const char *name) const
QString::isEmpty
bool isEmpty() const
TransformEdgesWidget.h
QString
QList
QStringList
QPair
TransformEdgesWidget::TransformEdgesWidget
TransformEdgesWidget(Document *graphDoc, QWidget *parent=0)
Definition: TransformEdgesWidget.cpp:51
QString::setNum
QString & setNum(short n, int base)
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
QWidget::setCaption
void setCaption(const QString &c)
TransformEdgesWidget::addDataStructures
void addDataStructures(QStringList dsNames)
Add data structures to QComboBox of UI starting at position 0.
Definition: TransformEdgesWidget.cpp:76
QPushButton
TransformEdgesWidget::~TransformEdgesWidget
~TransformEdgesWidget()
Definition: TransformEdgesWidget.cpp:70
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
QVariant::toString
QString toString() 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