Commit 13ddcd8f authored by Tyler Wilson's avatar Tyler Wilson
Browse files

Created SortFilterProxyModel class for filtering/sorting of selected...

Created SortFilterProxyModel class for filtering/sorting of selected ProjectItems from the main project tree in the BOSS tab tree.
parent 0b41cac1
Loading
Loading
Loading
Loading
+28 −15
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@
#include "ProjectItem.h"
#include "ProjectItemProxyModel.h"
#include "SpecialPixel.h"
#include "SubTreeProxyModel.h"
#include "SortFilterProxyModel.h"
#include "ui_JigsawSetupDialog.h"

namespace Isis {
@@ -1136,37 +1136,50 @@ namespace Isis {


  void JigsawSetupDialog::createObservationSolveSettingsTreeView() {
    // Proof-of-


    QList<ProjectItem *> selectedItems = m_project->directory()->model()->selectedItems();

    foreach(ProjectItem *item,selectedItems){
      qDebug() << "Selected Item:  " << item->text();
    }
    qDebug() << "JigsawSetupDialog::createObservationSolveSettingsTreeView()";

//    m_ui->treeView->setModel((QAbstractItemModel*)(m_project->directory()->model()));
    ProjectItemModel *model = m_project->directory()->model();

    SubTreeProxyModel *osspm = new SubTreeProxyModel;
    SortFilterProxyModel *osspm = new SortFilterProxyModel;
    osspm->setSourceModel(model);
    QList<ProjectItem*> selected = model->selectedItems();

    //selected.append(selectedItems[0]->parent() );

     //QModelIndex SubTreeProxyModel::mapFromSource(const QModelIndex &sourceIndex)
    // find the root "Images" and set it in the proxy
    //QStandardItem *item = model->invisibleRootItem()->child(0)->child(1);
    //qDebug() << "ITEM: " << item << ", " << item->text();
    //qDebug() << "PARENT: " << item->parent() << ", " << item->parent()->text();
    qDebug() << "Selected items:  ";
    foreach(ProjectItem * item,selected) {
      qDebug() << item->index().data(0).toString();

    }


    // i think source model tries to add top root item, which is invalid???
    //osspm->setDynamicSortFilter(true);

    m_ui->treeView->setRootIsDecorated(true);

    osspm->setSelectedItems(selected );
    m_ui->treeView->setModel(osspm);


    qDebug() << "Parent=" << selectedItems[0]->parent()->index().data(0).toString();


    //Set the root index to display the subtree we are interested in.  This requires
    //computing the proxy index from the source model.
    if (selectedItems.count() > 0) {
      m_ui->treeView->setRootIndex(osspm->mapFromSource(selectedItems[0]->index() ));
      //qDebug() << "parent = " << selectedItems[0]->parent()->text();
      osspm->setRoot(selectedItems[0]->parent());



      //if ( selectedItems[0]->parent()->index().data(0).toString().isEmpty()) {
          m_ui->treeView->setRootIndex(osspm->mapFromSource(selectedItems[0]->parent()->index()));

          qDebug() << "Setting parent to:  " << (selectedItems[0]->parent()->index()).data(0).toString();


    }

+7 −0
Original line number Diff line number Diff line
ifeq ($(ISISROOT), $(BLANK))
.SILENT:
error:
	echo "Please set ISISROOT";
else
	include $(ISISROOT)/make/isismake.objs
endif
 No newline at end of file
+127 −0
Original line number Diff line number Diff line
#include "SortFilterProxyModel.h"

#include <QAbstractItemModel>
#include <QIdentityProxyModel>
#include <QModelIndex>
#include <QObject>
#include <QPersistentModelIndex>
#include <QSortFilterProxyModel>
#include <QStandardItem>
#include <QVariant>

#include "ProjectItem.h"
#include "ProjectItemModel.h"

namespace Isis {

  SortFilterProxyModel::SortFilterProxyModel(QObject *parent) :
      QSortFilterProxyModel(parent) {
  }


  void SortFilterProxyModel::setSelectedItems(QList<ProjectItem *> selected){

    QList<QModelIndex> selIx;
    foreach(ProjectItem * item,selected) {
      selIx.append(item->index() );

    }

    foreach(QModelIndex ix,selIx) {
      selectedIndexRows.append(ix.row() );
    }

    selectedIndices=selIx;

  }

  void SortFilterProxyModel::setSourceModel(ProjectItemModel *newSourceModel) {
     
     QPersistentModelIndex persistentIndex(newSourceModel->index(0,0,QModelIndex()));
    


     if (persistentIndex.isValid()) {
       qDebug() << "persistent index is valid: " << persistentIndex;
      
       m_root = persistentIndex;
     }
     else {
       qDebug() << "persistent index NOT valid.";
       m_root = QPersistentModelIndex(QModelIndex());
     }

     // // qDebug() << "can convert to qstandarditem: " << data.canConvert<QStandardItem *>();
     // if (data.canConvert<ProjectItem *>()) {
     //  m_root = data.value<ProjectItem *>();
     // }
     QSortFilterProxyModel::setSourceModel(newSourceModel);
   }




  bool SortFilterProxyModel::setRoot(const QStandardItem *item) {

    m_root = QPersistentModelIndex(item->index());
    qDebug() << "Setting m_root to:  " << m_root.data(0).toString();
    //root = item->index();
    return true;

    //qDebug() << "m_root = " << m_root;

    //if (m_root.isValid()) {
    //  qDebug() <<"m_root is valid";
    //  return true;
   // }
    //else {
      //qDebug() <<"m_root is not valid";

      //return false;
    //}
  }


  bool SortFilterProxyModel::filterAcceptsRow(int sourceRow,
                                                        const QModelIndex &sourceParent) const {

    static QList<QModelIndex> accepted = selectedIndices;

    qDebug() << "filterAcceptsRow";
    bool accept(false);

    if (this->sourceModel()!=nullptr) {

       QModelIndex ix = this->sourceModel()->index( sourceRow, 0, sourceParent );
       if (ix.isValid() ) {
         if (this->sourceModel()->hasChildren(ix) ) {
           qDebug() << "Has children:  " << ix.data(0).toString();
           accept = true;
         }
         if (accepted.contains(ix)  && (this->sourceModel()->hasChildren(ix))) {
           qDebug() << "Accepted (has children):" << ix.data(0).toString();
           int numChildren = this->sourceModel()->rowCount(ix);
           for (int i = 0; i < numChildren;i++) {

             QModelIndex ixchild = this->sourceModel()->index(i,0,ix);
             accepted.append(ixchild);

             }
           }
         }
         if (accepted.contains(ix) ) {
           qDebug() << "Accepted:  " << ix.data(0).toString();
           accept = true;
         }

         qDebug() << "Rejected:  " << ix.data(0).toString();
       }

       return accept;

    }


}//end namespace

+86 −0
Original line number Diff line number Diff line

#ifndef SortFilterProxyModel_h
#define SortFilterProxyModel_h

/**
 * @file
 * $Date: 2018/06/88 16:40:33 $ $Revision: 1.0 $
 *
 *  Unless noted otherwise, the portions of Isis written by the USGS are public domain. See
 *  individual third-party library and package descriptions for intellectual property information,
 *  user agreements, and related information.
 *
 *  Although Isis has been used by the USGS, no warranty, expressed or implied, is made by the
 *  USGS as to the accuracy and functioning of such software and related material nor shall the
 *  fact of distribution constitute any such warranty, and no responsibility is assumed by the
 *  USGS in connection therewith.
 *
 *  For additional information, launch $ISISROOT/doc//documents/Disclaimers/Disclaimers.html
 *  in a browser or see the Privacy &amp; Disclaimers page on the Isis website,
 *  http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
 *  http://www.usgs.gov/privacy.html.
 */

#include <QList>
#include <QIdentityProxyModel>
#include <QModelIndex>
#include <QPersistentModelIndex>
#include <QSortFilterProxyModel>

class QAbstractProxyModel;
class QObject;
class QStandardItem;

class QVariant;

namespace Isis {

  /**
   * @brief A proxy class for filtering data within the JigsawSetupDialog
   *    Bundle Observation Solve Settings (BOSS) tab.
   *   
   * @ingroup
   *
   * @author 2018-06-18 Tyler Wilson
   *
   * @internal
   *   @history 2018-06-18 Tyler Wilson - Original version.
   */
  
   class ProjectItem;
   class ProjectItemModel;
 
    class SortFilterProxyModel : public QSortFilterProxyModel  {
    Q_OBJECT

    public:
      explicit SortFilterProxyModel(QObject *parent = 0);

      //QModelIndex mapFromSource(const QModelIndex &sourceIndex) const Q_DECL_OVERRIDE;
      //QModelIndex mapToSource(const QModelIndex &proxyIndex) const Q_DECL_OVERRIDE;

      //void setSourceModel(ProjectItemModel *newSourceModel) Q_DECL_OVERRIDE;
      void setSourceModel(ProjectItemModel *newSourceModel);

      bool setRoot(const QStandardItem *item);

      void setSelectedItems(QList<ProjectItem*> selected);


     protected:
       bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;

    private:
      //QList<ProjectItem *> selectedItems;
      QList<QModelIndex> selectedIndices;
      QList<int> selectedIndexRows;
      QPersistentModelIndex m_root;
      QModelIndex root;


  };

};


#endif
+166 −0
Original line number Diff line number Diff line
#include "SubTreeProxyModel.h"

#include <QAbstractItemModel>
#include <QIdentityProxyModel>
#include <QModelIndex>
#include <QObject>
#include <QPersistentModelIndex>
#include <QSortFilterProxyModel>
#include <QStandardItem>
#include <QVariant>

#include "ProjectItem.h"
#include "ProjectItemModel.h"

namespace Isis {

  SubTreeProxyModel::SubTreeProxyModel(QObject *parent) :
      QSortFilterProxyModel(parent) {
  }



  void SubTreeProxyModel::setSelectedItems(QList<ProjectItem *> selected){

    QList<QModelIndex> selIx;
    foreach(ProjectItem * item,selected) {
      selIx.append(item->index() );

    }

    foreach(QModelIndex ix,selIx) {
      selectedIndexRows.append(ix.row() );
    }

    selectedIndices=selIx;

  }

  void SubTreeProxyModel::setSourceModel(ProjectItemModel *newSourceModel) {
     // QVariant data = newSourceModel->data(newSourceModel->index(0,0,QModelIndex()));
     // qDebug() << data;
     // qDebug() << "can convert to project item: " << data.canConvert<ProjectItem *>();
     //qDebug() << "newSourceModel.name:  "<<typeid(*newSourceModel).name();

     // QStandardItem *item = static_cast<QStandardItemModel *>(newSourceModel)->invisibleRootItem();
     QPersistentModelIndex persistentIndex(newSourceModel->index(0,0,QModelIndex()));
     // QPersistentModelIndex persistentIndex(item->index());
     // qDebug() << "root item: " << item;
     // qDebug() << "root item index: " << item->index();


     if (persistentIndex.isValid()) {
       qDebug() << "persistent index is valid: " << persistentIndex;
       qDebug() <<"m_root = " << persistentIndex.data(0).toString();
       qDebug() << "parent index: " << persistentIndex.parent();
       m_root = persistentIndex;
     }
     else {
       qDebug() << "persistent index NOT valid.";
       m_root = QPersistentModelIndex(QModelIndex());
     }

     // // qDebug() << "can convert to qstandarditem: " << data.canConvert<QStandardItem *>();
     // if (data.canConvert<ProjectItem *>()) {
     //  m_root = data.value<ProjectItem *>();
     // }
     QSortFilterProxyModel::setSourceModel(newSourceModel);
   }




  bool SubTreeProxyModel::setRoot(const QStandardItem *item) {

    m_root = QPersistentModelIndex(item->index());
    qDebug() << "Setting m_root to:  " << m_root.data(0).toString();
    //root = item->index();
    return true;

    //qDebug() << "m_root = " << m_root;

    //if (m_root.isValid()) {
    //  qDebug() <<"m_root is valid";
    //  return true;
   // }
    //else {
      //qDebug() <<"m_root is not valid";

      //return false;
    //}
  }


  bool SubTreeProxyModel::filterAcceptsRow(int sourceRow,
                                                        const QModelIndex &sourceParent) const {

    static QList<QModelIndex> accepted = selectedIndices;

    qDebug() << "filterAcceptsRow";
    bool accept(false);

    if (this->sourceModel()!=nullptr) {

       QModelIndex ix = this->sourceModel()->index( sourceRow, 0, sourceParent );
       if (ix.isValid() ) {
         if (this->sourceModel()->hasChildren(ix) ) {
           qDebug() << "Has children:  " << ix.data(0).toString();
           accept = true;
         }
         if (accepted.contains(ix)  && (this->sourceModel()->hasChildren(ix))) {
           qDebug() << "Accepted (has children):" << ix.data(0).toString();
           int numChildren = this->sourceModel()->rowCount(ix);
           for (int i = 0; i < numChildren;i++) {

             QModelIndex ixchild = this->sourceModel()->index(i,0,ix);
             accepted.append(ixchild);

             }
           }
         }
         if (accepted.contains(ix) ) {
           qDebug() << "Accepted:  " << ix.data(0).toString();
           accept = true;
         }

         qDebug() << "Rejected:  " << ix.data(0).toString();
       }

       return accept;

    }






}//end namespace

#if 0

  
   bool SubTreeProxyModel::filterAcceptsRow(int sourceRow,
                                                         const QModelIndex &sourceParent) const {


     if (selectedIndexRows.contains(sourceRow ) ) {



        qDebug() << "Accept:  sourceRow = " << sourceRow << "  sourceParent = " << sourceParent.data(0).toString();

       return true;
     }


     qDebug() << "Reject:  sourceRow = " << sourceRow << "  sourceParent = " << sourceParent.data(0).toString();
     return false;
     //QModelIndex mIndex = sourceModel()->index(sourceRow, 0, sourceParent);


   }


}
#endif
Loading