$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r76455 - sandbox/icl/libs/xplore/br1/sqlbrowser
From: afojgo_at_[hidden]
Date: 2012-01-13 11:36:53
Author: jofaber
Date: 2012-01-13 11:36:51 EST (Fri, 13 Jan 2012)
New Revision: 76455
URL: http://svn.boost.org/trac/boost/changeset/76455
Log:
Starting a Qt style DAG model.
Added:
   sandbox/icl/libs/xplore/br1/sqlbrowser/dagitem.cpp   (contents, props changed)
   sandbox/icl/libs/xplore/br1/sqlbrowser/dagitem.h   (contents, props changed)
   sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp   (contents, props changed)
   sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h   (contents, props changed)
Binary files modified: 
   sandbox/icl/libs/xplore/br1/sqlbrowser/objects1.db
Text files modified: 
   sandbox/icl/libs/xplore/br1/sqlbrowser/ExtensibleObjectModel.sql |    24 +++++++++++++++++++++++-                
   sandbox/icl/libs/xplore/br1/sqlbrowser/browser.cpp               |     6 ++++--                                  
   sandbox/icl/libs/xplore/br1/sqlbrowser/sqlbrowser.pro            |    14 ++++++++++++--                          
   3 files changed, 39 insertions(+), 5 deletions(-)
Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/ExtensibleObjectModel.sql
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/ExtensibleObjectModel.sql	(original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/ExtensibleObjectModel.sql	2012-01-13 11:36:51 EST (Fri, 13 Jan 2012)
@@ -34,10 +34,32 @@
 insert into Dag values (3, 6); -- (3:Latin, 4:Blue Bossa)
 
 -- -----------------------------------------------------------------------------
-select Objects.name as Object, Types.name as Type, Dag.Child as Chld, Dag.Parent as Prnt, 
+select Objects.name as Object, Types.name as Type, Dag.Child as ChildId, Dag.Parent as PatentId, 
   (select Objects.name from Objects where Objects.id = Dag.Parent) as Parent
   from Dag
   inner join Objects on     Dag.Child = Objects.id
   inner join Types   on Objects.TypeOf = Types.id
 
+-- -----------------------------------------------------------------------------
+-- The Dag in orderly fashion (parent->chiled) starting from root
+select Dag.Parent as PatentId, Dag.Child as ChildId, 
+  (select Objects.name from Objects where Objects.id = Dag.Parent) as Parent,
+  Objects.name as Object, Types.name as Type 
+  from Dag
+  inner join Objects on     Dag.Child = Objects.id
+  inner join Types   on Objects.TypeOf = Types.id
+  where Dag.Child <> 0
+  order by Dag.Parent, Dag.Child
+
+
 -- ----------------------------------------------------------------------------
+create view Collections as
+select Dag.Parent as PatentId, Dag.Child as ChildId, 
+  (select Objects.name from Objects where Objects.id = Dag.Parent) as Parent,
+  Objects.name as Object, Types.name as Type 
+  from Dag
+  inner join Objects on     Dag.Child = Objects.id
+  inner join Types   on Objects.TypeOf = Types.id
+  where Dag.Child <> 0
+  order by Dag.Parent, Dag.Child
+
Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/browser.cpp
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/browser.cpp	(original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/browser.cpp	2012-01-13 11:36:51 EST (Fri, 13 Jan 2012)
@@ -205,8 +205,9 @@
     ext_table->resizeColumnsToContents();
     //JOFA additions ----------------------------------------------------------
 
-    //REV connect(table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(currentChanged()));
-    connect(ext_table->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(on_rowSelectChanged()));
+    connect(ext_table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(currentChanged()));
+    //JOFA experiment. Hiding rows
+    //connect(ext_table->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(on_rowSelectChanged()));
 
     updateActions();
     return model;
@@ -337,6 +338,7 @@
     ext_table->selectionModel()->select(toggleSelection, QItemSelectionModel::Toggle);
 }
 
+//JOFA: Experiments on row hiding. Currently deactivated.
 void Browser::on_rowSelectChanged()
 {
     QModelIndexList currentSelection = ext_table->selectionModel()->selectedIndexes();
Added: sandbox/icl/libs/xplore/br1/sqlbrowser/dagitem.cpp
==============================================================================
--- (empty file)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/dagitem.cpp	2012-01-13 11:36:51 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,143 @@
+/****************************************************************************
+**
+**
+****************************************************************************/
+
+/*
+    dagitem.cpp
+
+    A container for items of data supplied by a simple dag model.
+*/
+
+#include <QStringList>
+
+#include "dagitem.h"
+
+DagItem::DagItem(const QVector<QVariant> &data, DagItem *parent)
+{
+    parentItem = parent;
+    itemData = data;
+}
+
+/*JODO First step: Treat the dag like a tree. Introduce multiple parents later.
+DagItem::DagItem(const QVector<QVariant> &data, QList<DagItem*>& parents, int parentIdx)
+{
+    Q_ASSERT(0 < parents.size())
+    Q_ASSERT(0 <= parentIdx && parentIdx < parents.size());
+    m_parentItems = parents;
+    m_selectedParent = parentIdx;
+    itemData = data;
+}
+*/
+
+DagItem::~DagItem()
+{
+    qDeleteAll(childItems);
+}
+
+
+DagItem *DagItem::child(int number)
+{
+    return childItems.value(number);
+}
+
+
+int DagItem::childCount() const
+{
+    return childItems.count();
+}
+
+
+int DagItem::childNumber() const
+{
+    if (parentItem)
+        return parentItem->childItems.indexOf(const_cast<DagItem*>(this));
+
+    return 0;
+}
+
+
+int DagItem::columnCount() const
+{
+    return itemData.count();
+}
+
+
+QVariant DagItem::data(int column) const
+{
+    return itemData.value(column);
+}
+
+
+bool DagItem::insertChildren(int position, int count, int columns)
+{
+    if (position < 0 || position > childItems.size())
+        return false;
+
+    for (int row = 0; row < count; ++row) {
+        QVector<QVariant> data(columns);
+        DagItem *item = new DagItem(data, this);
+        childItems.insert(position, item);
+    }
+
+    return true;
+}
+
+
+bool DagItem::insertColumns(int position, int columns)
+{
+    if (position < 0 || position > itemData.size())
+        return false;
+
+    for (int column = 0; column < columns; ++column)
+        itemData.insert(position, QVariant());
+
+    foreach (DagItem *child, childItems)
+        child->insertColumns(position, columns);
+
+    return true;
+}
+
+
+DagItem *DagItem::parent()
+{
+    return parentItem;
+}
+
+
+bool DagItem::removeChildren(int position, int count)
+{
+    if (position < 0 || position + count > childItems.size())
+        return false;
+
+    for (int row = 0; row < count; ++row)
+        delete childItems.takeAt(position);
+
+    return true;
+}
+
+
+bool DagItem::removeColumns(int position, int columns)
+{
+    if (position < 0 || position + columns > itemData.size())
+        return false;
+
+    for (int column = 0; column < columns; ++column)
+        itemData.remove(position);
+
+    foreach (DagItem *child, childItems)
+        child->removeColumns(position, columns);
+
+    return true;
+}
+
+
+bool DagItem::setData(int column, const QVariant &value)
+{
+    if (column < 0 || column >= itemData.size())
+        return false;
+
+    itemData[column] = value;
+    return true;
+}
+
Added: sandbox/icl/libs/xplore/br1/sqlbrowser/dagitem.h
==============================================================================
--- (empty file)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/dagitem.h	2012-01-13 11:36:51 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,40 @@
+/****************************************************************************
+**
+**
+****************************************************************************/
+
+#ifndef DAGITEM_H
+#define DAGITEM_H
+
+#include <QList>
+#include <QVariant>
+#include <QVector>
+
+
+class DagItem
+{
+public:
+    DagItem(const QVector<QVariant> &data, DagItem *parent = 0);
+    ~DagItem();
+
+    DagItem *child(int number);
+    int childCount() const;
+    int columnCount() const;
+    QVariant data(int column) const;
+    bool insertChildren(int position, int count, int columns);
+    bool insertColumns(int position, int columns);
+    DagItem *parent();
+    bool removeChildren(int position, int count);
+    bool removeColumns(int position, int columns);
+    int childNumber() const;
+    bool setData(int column, const QVariant &value);
+
+private:
+    QList<DagItem*> childItems;
+    QVector<QVariant> itemData;
+    DagItem *parentItem;
+    // int m_selectedParent;
+    // QList<DagItem*> m_parentItems;
+};
+
+#endif
Added: sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp
==============================================================================
--- (empty file)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp	2012-01-13 11:36:51 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,322 @@
+/****************************************************************************
+**
+**
+****************************************************************************/
+
+#include <QtGui>
+#include <QtSql>
+
+#include "dagitem.h"
+#include "dagmodel.h"
+
+
+DagModel::DagModel(const QStringList &headers, const QString &data,
+                     QObject *parent)
+    : QAbstractItemModel(parent)
+{
+    QVector<QVariant> rootData;
+    foreach (QString header, headers)
+        rootData << header;
+
+    rootItem = new DagItem(rootData);
+    setupModelData(data.split(QString("\n")), rootItem);
+}
+
+
+DagModel::~DagModel()
+{
+    delete rootItem;
+}
+
+
+int DagModel::columnCount(const QModelIndex & /* parent */) const
+{
+    return rootItem->columnCount();
+}
+
+
+QVariant DagModel::data(const QModelIndex &index, int role) const
+{
+    if (!index.isValid())
+        return QVariant();
+
+    if (role != Qt::DisplayRole && role != Qt::EditRole)
+        return QVariant();
+
+    DagItem *item = getItem(index);
+
+    return item->data(index.column());
+}
+
+
+Qt::ItemFlags DagModel::flags(const QModelIndex &index) const
+{
+    if (!index.isValid())
+        return 0;
+
+    return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+}
+
+
+DagItem *DagModel::getItem(const QModelIndex &index) const
+{
+    if (index.isValid()) {
+        DagItem *item = static_cast<DagItem*>(index.internalPointer());
+        if (item) return item;
+    }
+    return rootItem;
+}
+
+
+QVariant DagModel::headerData(int section, Qt::Orientation orientation,
+                               int role) const
+{
+    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
+        return rootItem->data(section);
+
+    return QVariant();
+}
+
+
+QModelIndex DagModel::index(int row, int column, const QModelIndex &parent) const
+{
+    if (parent.isValid() && parent.column() != 0)
+        return QModelIndex();
+
+    DagItem *parentItem = getItem(parent);
+
+    DagItem *childItem = parentItem->child(row);
+    if (childItem)
+        return createIndex(row, column, childItem);
+    else
+        return QModelIndex();
+}
+
+
+bool DagModel::insertColumns(int position, int columns, const QModelIndex &parent)
+{
+    bool success;
+
+    beginInsertColumns(parent, position, position + columns - 1);
+    success = rootItem->insertColumns(position, columns);
+    endInsertColumns();
+
+    return success;
+}
+
+bool DagModel::insertRows(int position, int rows, const QModelIndex &parent)
+{
+    DagItem *parentItem = getItem(parent);
+    bool success;
+
+    beginInsertRows(parent, position, position + rows - 1);
+    success = parentItem->insertChildren(position, rows, rootItem->columnCount());
+    endInsertRows();
+
+    return success;
+}
+
+
+QModelIndex DagModel::parent(const QModelIndex &index) const
+{
+    if (!index.isValid())
+        return QModelIndex();
+
+    DagItem *childItem = getItem(index);
+    DagItem *parentItem = childItem->parent();
+
+    if (parentItem == rootItem)
+        return QModelIndex();
+
+    return createIndex(parentItem->childNumber(), 0, parentItem);
+}
+
+
+bool DagModel::removeColumns(int position, int columns, const QModelIndex &parent)
+{
+    bool success;
+
+    beginRemoveColumns(parent, position, position + columns - 1);
+    success = rootItem->removeColumns(position, columns);
+    endRemoveColumns();
+
+    if (rootItem->columnCount() == 0)
+        removeRows(0, rowCount());
+
+    return success;
+}
+
+bool DagModel::removeRows(int position, int rows, const QModelIndex &parent)
+{
+    DagItem *parentItem = getItem(parent);
+    bool success = true;
+
+    beginRemoveRows(parent, position, position + rows - 1);
+    success = parentItem->removeChildren(position, rows);
+    endRemoveRows();
+
+    return success;
+}
+
+
+int DagModel::rowCount(const QModelIndex &parent) const
+{
+    DagItem *parentItem = getItem(parent);
+
+    return parentItem->childCount();
+}
+
+
+bool DagModel::setData(const QModelIndex &index, const QVariant &value,
+                        int role)
+{
+    if (role != Qt::EditRole)
+        return false;
+
+    DagItem *item = getItem(index);
+    bool result = item->setData(index.column(), value);
+
+    if (result)
+        emit dataChanged(index, index);
+
+    return result;
+}
+
+bool DagModel::setHeaderData(int section, Qt::Orientation orientation,
+                              const QVariant &value, int role)
+{
+    if (role != Qt::EditRole || orientation != Qt::Horizontal)
+        return false;
+
+    bool result = rootItem->setData(section, value);
+
+    if (result)
+        emit headerDataChanged(orientation, section, section);
+
+    return result;
+}
+
+void DagModel::setupModelData(const QStringList &lines, DagItem *parent)
+{
+    QList<DagItem*> parents;
+    QList<int> indentations;
+    parents << parent;
+    indentations << 0;
+
+    int number = 0;
+
+    while (number < lines.count()) {
+        int position = 0;
+        while (position < lines[number].length()) {
+            if (lines[number].mid(position, 1) != " ")
+                break;
+            position++;
+        }
+
+        QString lineData = lines[number].mid(position).trimmed();
+
+        if (!lineData.isEmpty()) {
+            // Read the column data from the rest of the line.
+            QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
+            QVector<QVariant> columnData;
+            for (int column = 0; column < columnStrings.count(); ++column)
+                columnData << columnStrings[column];
+
+            if (position > indentations.last()) {
+                // The last child of the current parent is now the new parent
+                // unless the current parent has no children.
+
+                if (parents.last()->childCount() > 0) {
+                    parents << parents.last()->child(parents.last()->childCount()-1);
+                    indentations << position;
+                }
+            } else {
+                while (position < indentations.last() && parents.count() > 0) {
+                    parents.pop_back();
+                    indentations.pop_back();
+                }
+            }
+
+            // Append a new item to the current parent's list of children.
+            DagItem *parent = parents.last();
+            parent->insertChildren(parent->childCount(), 1, rootItem->columnCount());
+            for (int column = 0; column < columnData.size(); ++column)
+                parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
+        }
+
+        number++;
+    }
+}
+
+
+//JODO Populate a DagModel from an sql-query that provides the DAG as
+// (ParentId -> ChildId), ParentName, ChildName, ChildType
+void DagModel::fromSql(const QSqlQuery& query)
+{
+    //JODO create root node
+    if(!query.next())
+        return;
+    else
+    {
+        //JODO fill root with info
+        fromSql(query, rootItem, 0);
+    }
+}
+
+void DagModel::fromSql(const QSqlQuery& query, DagItem* parent, int depth)
+{
+    Q_ASSERT(node != NULL);
+
+    if(!query.next())
+        return;
+    else if(query.value(0).toInt() == parent->nodeId())
+    {   //Same node as before. Add a child. Recursing down
+        DagItem* newChild = addChild(parent, query); //Fill Data
+        //Ok, wont work. Building the DAG and traversing order of
+        //the representing order of the db-data must match.
+    }
+    else
+    {   //Next
+
+    }
+}
+
+/*
+void DagModel::fromSql(const QSqlQuery& query, DagItem* parent, int depth)
+{
+    Q_ASSERT(node != NULL);
+
+    if(!query.next())
+        return;
+    else
+        // Same parent-id
+        while(query.value(0).toInt() == parent->nodeId())
+        {   //Same node as before. Add a child
+            parent->addChild(parent, data)
+        }
+}
+*/
+
+//JOFA Iteration example: The container as String
+QString DagModel::toString()const
+{
+    return nodeToString(rootItem, 0);
+}
+
+QString DagModel::nodeToString(DagItem* node, int depth)const
+{
+    if(node->childCount()==0)
+        return tr("(%1)").arg(depth); //Print only structure and depth.
+        //return tr("(%1)").arg(node->data(0));
+    else
+    {
+        QString nodeRepr(tr("["));
+        for(int idx=0; idx<node->childCount(); idx++)
+            nodeRepr += nodeToString(node->child(idx), depth+1);
+
+        nodeRepr += tr("]");
+        return nodeRepr;
+    }
+}
+
+
Added: sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h
==============================================================================
--- (empty file)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h	2012-01-13 11:36:51 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,70 @@
+/****************************************************************************
+**
+**
+****************************************************************************/
+
+#ifndef DAGMODEL_H
+#define DAGMODEL_H
+
+#include <QAbstractItemModel>
+#include <QModelIndex>
+#include <QVariant>
+
+class QSqlQuery;
+class DagItem;
+
+class DagModel : public QAbstractItemModel
+{
+    Q_OBJECT
+
+public:
+    DagModel(const QStringList &headers, const QString &data,
+              QObject *parent = 0);
+    ~DagModel();
+
+    QVariant data(const QModelIndex &index, int role) const;
+    QVariant headerData(int section, Qt::Orientation orientation,
+                        int role = Qt::DisplayRole) const;
+
+    QModelIndex index(int row, int column,
+                      const QModelIndex &parent = QModelIndex()) const;
+    QModelIndex parent(const QModelIndex &index) const;
+
+    int rowCount(const QModelIndex &parent = QModelIndex()) const;
+    int columnCount(const QModelIndex &parent = QModelIndex()) const;
+
+    Qt::ItemFlags flags(const QModelIndex &index) const;
+    bool setData(const QModelIndex &index, const QVariant &value,
+                 int role = Qt::EditRole);
+    bool setHeaderData(int section, Qt::Orientation orientation,
+                       const QVariant &value, int role = Qt::EditRole);
+
+    bool insertColumns(int position, int columns,
+                       const QModelIndex &parent = QModelIndex());
+    bool removeColumns(int position, int columns,
+                       const QModelIndex &parent = QModelIndex());
+    bool insertRows(int position, int rows,
+                    const QModelIndex &parent = QModelIndex());
+    bool removeRows(int position, int rows,
+                    const QModelIndex &parent = QModelIndex());
+
+
+    //JOFA Populationg a DAG from an Sql-query
+    void fromSql(const QSqlQuery& query);
+
+    void fromSql(const QSqlQuery& query, DagItem* node, int depth);
+
+    //JOFA Iteration example: The container as String
+    QString toString()const;
+
+    QString nodeToString(DagItem* node, int depth)const;
+
+
+private:
+    void setupModelData(const QStringList &lines, DagItem *parent);
+    DagItem *getItem(const QModelIndex &index) const;
+
+    DagItem *rootItem;
+};
+
+#endif
Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/objects1.db
==============================================================================
Binary files. No diff available.
Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/sqlbrowser.pro
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/sqlbrowser.pro	(original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/sqlbrowser.pro	2012-01-13 11:36:51 EST (Fri, 13 Jan 2012)
@@ -4,9 +4,14 @@
 QT              += sql
 
 HEADERS         = browser.h connectionwidget.h qsqlconnectiondialog.h \
-    exttableview.h
+    exttableview.h \
+    dagitem.h \
+    dagmodel.h \
+    dagmodel.h
 SOURCES         = main.cpp browser.cpp connectionwidget.cpp qsqlconnectiondialog.cpp \
-    exttableview.cpp
+    exttableview.cpp \
+    dagitem.cpp \
+    dagmodel.cpp
 
 FORMS           = browserwidget.ui qsqlconnectiondialog.ui
 build_all:!build_pass {
@@ -31,3 +36,8 @@
 
 
 
+
+
+
+
+