diff gui/src/workspacemodel.cc @ 14703:f86884be20fc gui

Renamed all source files of the gui to lowercase and .cc to be conform with the octave sources.
author Jacob Dawid <jacob.dawid@googlemail.com>
date Thu, 31 May 2012 20:53:56 +0200
parents gui/src/WorkspaceModel.cpp@79c9a6d06590
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gui/src/workspacemodel.cc	Thu May 31 20:53:56 2012 +0200
@@ -0,0 +1,172 @@
+/* OctaveGUI - A graphical user interface for Octave
+ * Copyright (C) 2011 Jacob Dawid (jacob.dawid@googlemail.com)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "workspacemodel.h"
+#include <QTreeWidget>
+#include <QTime>
+#include "octavelink.h"
+
+WorkspaceModel::WorkspaceModel(QObject *parent)
+  : QAbstractItemModel(parent)
+{
+  QList<QVariant> rootData;
+  rootData << tr ("Name") << tr ("Type") << tr ("Value");
+  _rootItem = new TreeItem(rootData);
+}
+
+WorkspaceModel::~WorkspaceModel()
+{
+  delete _rootItem;
+}
+
+QModelIndex
+WorkspaceModel::index(int row, int column, const QModelIndex &parent) const
+{
+  if (!hasIndex(row, column, parent))
+    return QModelIndex();
+
+  TreeItem *parentItem;
+
+  if (!parent.isValid())
+    parentItem = _rootItem;
+  else
+    parentItem = static_cast<TreeItem*>(parent.internalPointer());
+
+  TreeItem *childItem = parentItem->child(row);
+  if (childItem)
+    return createIndex(row, column, childItem);
+  else
+    return QModelIndex();
+}
+
+QModelIndex
+WorkspaceModel::parent(const QModelIndex &index) const
+{
+  if (!index.isValid())
+    return QModelIndex();
+
+  TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
+  TreeItem *parentItem = childItem->parent();
+
+  if (parentItem == _rootItem)
+    return QModelIndex();
+
+  return createIndex(parentItem->row(), 0, parentItem);
+}
+
+int
+WorkspaceModel::rowCount(const QModelIndex &parent) const
+{
+  TreeItem *parentItem;
+  if (parent.column() > 0)
+    return 0;
+
+  if (!parent.isValid())
+    parentItem = _rootItem;
+  else
+    parentItem = static_cast<TreeItem*>(parent.internalPointer());
+
+  return parentItem->childCount();
+}
+
+int
+WorkspaceModel::columnCount(const QModelIndex &parent) const
+{
+  if (parent.isValid())
+    return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
+  else
+    return _rootItem->columnCount();
+}
+
+void
+WorkspaceModel::insertTopLevelItem(int at, TreeItem *treeItem)
+{
+  _rootItem->insertChildItem(at, treeItem);
+}
+
+TreeItem *
+WorkspaceModel::topLevelItem (int at)
+{
+  return _rootItem->child(at);
+}
+
+Qt::ItemFlags
+WorkspaceModel::flags(const QModelIndex &index) const
+{
+  if (!index.isValid())
+    return 0;
+
+  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+}
+
+QVariant
+WorkspaceModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
+    return _rootItem->data(section);
+
+  return QVariant();
+}
+
+QVariant
+WorkspaceModel::data(const QModelIndex &index, int role) const
+{
+  if (!index.isValid())
+    return QVariant();
+
+  if (role != Qt::DisplayRole)
+    return QVariant();
+
+  TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
+
+  return item->data(index.column());
+}
+
+
+void
+WorkspaceModel::updateFromSymbolTable ()
+{
+  topLevelItem (0)->deleteChildItems ();
+  topLevelItem (1)->deleteChildItems ();
+  topLevelItem (2)->deleteChildItems ();
+  topLevelItem (3)->deleteChildItems ();
+
+  OctaveLink::instance ()-> acquireSymbolInformation();
+  const QList <SymbolInformation>& symbolInformation = OctaveLink::instance() ->symbolInformation ();
+
+  foreach (const SymbolInformation& s, symbolInformation)
+    {
+      TreeItem *child = new TreeItem ();
+
+      child->setData (0, s._symbol);
+      child->setData (1, s._type);
+      child->setData (2, s._value);
+
+      switch (s._scope)
+        {
+          case SymbolInformation::Local:       topLevelItem (0)->addChild (child); break;
+          case SymbolInformation::Global:      topLevelItem (1)->addChild (child); break;
+          case SymbolInformation::Persistent:  topLevelItem (2)->addChild (child); break;
+          case SymbolInformation::Hidden:      topLevelItem (3)->addChild (child); break;
+        }
+    }
+
+  OctaveLink::instance ()-> releaseSymbolInformation();
+
+  reset();
+  emit expandRequest();
+}