0

I'm trying to create in GitAhead a different view of the diff files. They provide already a Treemodel which works fine. I'm trying to put a wrapper between the model and the view to show only the changed files. For the moment I would like to just forward the model, but the view does not show anything. rowCount() is called fine and then also index() is called, but data() is never called. Does anybody know why I'm not able to forward the model?

TreeWrapper.h:

#include "TreeWrapper.h"
#include "TreeModel.h"
#include "conf/Settings.h"
#include "git/Blob.h"
#include "git/Diff.h"
#include "git/RevWalk.h"
#include "git/Submodule.h"
#include <QStringBuilder>
#include <QUrl>

namespace {

const QString kLinkFmt = "<a href='%1'>%2</a>";

} // anon. namespace

TreeWrapper::TreeWrapper(TreeModel* model, QObject *parent):
    mModel(model),
    QAbstractItemModel(parent)
{

}

TreeWrapper::~TreeWrapper()
{
}

void TreeWrapper::setTree(const git::Tree &tree, const git::Diff &diff)
{
  beginResetModel();

  mModel->setTree(tree, diff);

  endResetModel();
}

int TreeWrapper::rowCount(const QModelIndex &parent) const
{
int count = mModel->rowCount(parent);
  return count;
}

int TreeWrapper::columnCount(const QModelIndex &parent) const
{
  return mModel->columnCount(parent);
}

bool TreeWrapper::hasChildren(const QModelIndex &parent) const
{
    bool children = mModel->hasChildren(parent);
  return children;
}

QModelIndex TreeWrapper::parent(const QModelIndex &index) const
{
  return mModel->parent(index);
}

QModelIndex TreeWrapper::index(
  int row,
  int column,
  const QModelIndex &parent) const
{
    QModelIndex index = mModel->index(row, column, parent);
    bool valid = index.isValid();
    return index;
}

QVariant TreeWrapper::data(const QModelIndex &index, int role) const
{
  return mModel->data(index, role);
}

bool TreeWrapper::setData(
  const QModelIndex &index,
  const QVariant &value,
  int role)
{
  return mModel->setData(index, value, role);
}

Qt::ItemFlags TreeWrapper::flags(const QModelIndex &index) const
{
  return mModel->flags(index);
}

TreeWrapper.h:

#ifndef TREEWRAPPER
#define TREEWRAPPER

#include "git/Diff.h"
#include "git/Index.h"
#include "git/Tree.h"
#include "git/Repository.h"
#include <QAbstractItemModel>
#include <QFileIconProvider>

class TreeModel;

class TreeWrapper : public QAbstractItemModel
{
  Q_OBJECT

public:
  TreeWrapper(TreeModel* model, QObject *parent = nullptr);
  virtual ~TreeWrapper();

  void setTree(const git::Tree &tree, const git::Diff &diff = git::Diff());

  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
  bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;

  QModelIndex parent(const QModelIndex &index) const override;
  QModelIndex index(
    int row,
    int column,
    const QModelIndex &parent = QModelIndex()) const override;

  QVariant data(
    const QModelIndex &index,
    int role = Qt::DisplayRole) const override;
  bool setData(
    const QModelIndex &index,
    const QVariant &value,
    int role = Qt::EditRole) override;

  Qt::ItemFlags flags(const QModelIndex &index) const override;

private:
  TreeModel* mModel{nullptr};
};

#endif // TREEWRAPPER

The lower treeview uses directly the Treemodel, in the upper one, I would like to put the Wrapper between the model and the view. enter image description here

Murmi
  • 91
  • 10
  • 2
    QModelIndex instances from one model are not valid in the other. Consider using a proxy model, e.g. QIdentityProxyModel. – Frank Osterfeld Mar 15 '20 at 10:11
  • Thank you Frank. But I forward only. This should have an influence or? At the end I would like to filter, so I need an QSortFilterProxyModel or? The idea is to show in one treeview the data of the staged files and in the other the unstaged files – Murmi Mar 15 '20 at 14:44
  • Thank you Frank. A proxy is doing exactly what I wanna do – Murmi Mar 15 '20 at 14:58

1 Answers1

0

I solved the problem by putting a QSortFilterProxyModel between the model and the view: https://github.com/Murmele/gitahead/blob/AlternativeTreeView/src/ui/TreeProxy.cpp

This Proxy is used twice. One time the proxy filters the stagend and the secind the unstaged.

Murmi
  • 91
  • 10