vymview.cpp
author insilmaril
Fri, 13 Nov 2009 08:32:03 +0000
changeset 804 14f2b1b15242
parent 803 338ebdc9b947
child 806 2a33304714ba
permissions -rw-r--r--
Several fixes, see tex/vym.changelog for details
     1 #include "vymview.h"
     2 
     3 #include <iostream>
     4 using namespace std;
     5 
     6 #include "branchitem.h"
     7 #include "mainwindow.h"
     8 #include "mapeditor.h"
     9 #include "treeeditor.h"
    10 
    11 extern Main *mainWindow;
    12 
    13 
    14 VymView::VymView(VymModel *m)
    15 {
    16 	model=m;
    17 
    18 	// Create TreeView
    19 	treeEditor=new TreeEditor (model);
    20 	//treeEditor->setModel ((QAbstractItemModel*)model);
    21 	//treeEditor->setMinimumWidth (50);
    22 
    23 	treeEditor->setColumnWidth (0,150);
    24 	treeEditor->setAnimated (true);
    25 
    26 	// FIXME-2 use proxySelModel=treeEditor->selectionModel();
    27 	selModel=new QItemSelectionModel (model);
    28 
    29 	//model->setSelectionModel (proxySelModel);
    30 	model->setSelectionModel (selModel);
    31 	treeEditor->setSelectionModel (selModel);
    32 
    33 	// Create good old MapEditor
    34 	mapEditor=model->getMapEditor();
    35 	if (!mapEditor) mapEditor=new MapEditor (model);
    36 
    37 	// Connect selections
    38 
    39 		// Proxymodel changed
    40 		/*
    41 		connect (
    42 			proxySelModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    43 			this,SLOT (changeProxySelection(const QItemSelection &,const QItemSelection &)));
    44 */
    45 
    46 		// Selection in Model changed	
    47 		connect (
    48 			selModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    49 			this,SLOT (changeSelection(const QItemSelection &,const QItemSelection &)));
    50 
    51 		// Tell MapEditor to update selection
    52 		connect (
    53 			selModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    54 			mapEditor,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
    55 
    56 		// FIXME-2 testing, if that reenables updating selbox during animation
    57 		connect (
    58 			model, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    59 			mapEditor,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
    60 
    61 	// Connect data changed signals	
    62 	connect (
    63 		model, SIGNAL (dataChanged(const QModelIndex &, const QModelIndex &)), 
    64 		mapEditor,SLOT (updateData(const QModelIndex &) ) );
    65 
    66 	connect (
    67 		model, SIGNAL (sortFilterChanged (const QString &)),
    68 		treeEditor, SLOT (setSortFilter (const QString &) ) );
    69 
    70 	connect (
    71 		model, SIGNAL (noteHasChanged(QModelIndex) ),
    72 		mainWindow, SLOT (updateNoteEditor (QModelIndex) ) );
    73 		
    74 	connect (
    75 		model, SIGNAL (expandAll() ),
    76 		this, SLOT (expandAll () ) );
    77 		
    78 	connect (
    79 		model, SIGNAL (expandOneLevel() ),
    80 		this, SLOT (expandOneLevel() ) );
    81 		
    82 	connect (
    83 		model, SIGNAL (collapseOneLevel() ),
    84 		this, SLOT (collapseOneLevel() ) );
    85 		
    86 	connect (
    87 		model, SIGNAL (showSelection() ),
    88 		this, SLOT (showSelection() ) );
    89 		
    90 
    91 	mapEditor->setAntiAlias (mainWindow->isAliased());
    92 	mapEditor->setSmoothPixmap(mainWindow->hasSmoothPixmapTransform());
    93 
    94 	addWidget (treeEditor);
    95 	addWidget (mapEditor);
    96 
    97 	// Set geometry
    98 	QList <int> widths;
    99 	widths<<200;
   100 	widths<<600;
   101 	setSizes(widths);
   102 }
   103 
   104 VymView::~VymView()
   105 {
   106 	//cout << "Destructor VymView\n";
   107 }
   108 
   109 VymModel* VymView::getModel()
   110 {
   111 	return model;
   112 }
   113 
   114 MapEditor* VymView::getMapEditor()
   115 {
   116 	return mapEditor;
   117 }
   118 
   119 void VymView::initFocus()
   120 {
   121 	mapEditor->setFocus();
   122 }
   123 
   124 void VymView::changeSelection (const QItemSelection &newsel, const QItemSelection &oldsel)
   125 {
   126 	// Notify mainwindow to update satellites like NoteEditor, if needed (model==currenModel...)
   127 	mainWindow->changeSelection (model,newsel,oldsel);	// FIXME-5 maybe connect VymModel <-> MainWindow directly?
   128 	// would require to also get current model in mainWindow
   129 
   130 	cout << "VV::changeSelection   newsel.count="<<newsel.indexes().count()<<endl;
   131 
   132 	if (newsel.indexes().count()>0)
   133 	{
   134 
   135 	/* FIXME-2 use proxymodel
   136 		proxySelModel->select (
   137 			treeEditor->getProxyModel()->mapSelectionFromSource (newsel),
   138 			QItemSelectionModel::ClearAndSelect );
   139 		*/
   140 		QModelIndex ix=newsel.indexes().first();
   141 		selModel->setCurrentIndex (ix,QItemSelectionModel::ClearAndSelect  );
   142 		treeEditor->setCurrentIndex (ix);
   143 		showSelection();
   144 	}
   145 }
   146 
   147 void VymView::changeProxySelection (const QItemSelection &newsel, const QItemSelection &oldsel)
   148 {
   149 	// Notify mainwindow to update satellites, but map selection to 
   150 	// original model first
   151 
   152 	cout << "VV::changeProxySelection   newsel.count="<<newsel.indexes().count()<<endl;
   153 	if (!newsel.indexes().isEmpty())
   154 	{
   155 	/* FIXME-2 need to set current, too
   156 	*/
   157 		proxySelModel->setCurrentIndex (
   158 			newsel.indexes().first(),
   159 			QItemSelectionModel::ClearAndSelect );
   160 	treeEditor->setCurrentIndex (newsel.indexes().first() );
   161 	}
   162 
   163 	// Re-emit but map selection first
   164 	selModel->select (
   165 		treeEditor->getProxyModel()->mapSelectionToSource (newsel),
   166 		QItemSelectionModel::ClearAndSelect );
   167 
   168 	showSelection();
   169 }
   170 
   171 void VymView::expandAll()
   172 {
   173 	treeEditor->expandAll();
   174 }
   175 
   176 void VymView::expandOneLevel()
   177 {
   178 	int level=999999;
   179 	int d;
   180 	BranchItem *cur=NULL;
   181 	BranchItem *prev=NULL;
   182 	QModelIndex pix;
   183 
   184 	// Find level to expand
   185 	model->nextBranch(cur,prev);
   186 	while (cur) 
   187 	{
   188 		// FIXME-2 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   189 		pix=model->index (cur);
   190 		d=cur->depth();
   191 		if (!treeEditor->isExpanded(pix) && d < level)
   192 			level=d;
   193 		model->nextBranch(cur,prev);	
   194 	}
   195 
   196 	// Expand all to level
   197 	cur=NULL;
   198 	prev=NULL;
   199 	model->nextBranch(cur,prev);
   200 	while (cur) 
   201 	{
   202 		// FIXME-2 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   203 		pix=model->index (cur);
   204 		d=cur->depth();
   205 		if (!treeEditor->isExpanded(pix) && d <= level && cur->branchCount()>0)
   206 			treeEditor->setExpanded(pix,true);
   207 		model->nextBranch(cur,prev);	
   208 	}
   209 	/* FIXME-3 optimize expanding by using flat version of next
   210 	model->nextBranch(cur,prev,false);
   211 	while (cur) 
   212 	{
   213 		cout << "ok: "<<cur->getHeadingStd()<<endl./re/videochristinaprison1_wmvl.wmv
   214 ;
   215 		model->nextBranch(cur,prev,false);	
   216 	}
   217 	*/
   218 }
   219 
   220 void VymView::collapseOneLevel()
   221 {
   222 	int level=-1;
   223 	int d;
   224 	BranchItem *cur=NULL;
   225 	BranchItem *prev=NULL;
   226 	QModelIndex pix;
   227 
   228 	// Find level to collapse
   229 	model->nextBranch(cur,prev);
   230 	while (cur) 
   231 	{
   232 		// FIXME-2 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   233 		pix=model->index (cur);
   234 		d=cur->depth();
   235 		if (treeEditor->isExpanded(pix) && d > level)
   236 			level=d;
   237 		model->nextBranch(cur,prev);	
   238 	}
   239 
   240 	// collapse all to level
   241 	cur=NULL;
   242 	prev=NULL;
   243 	model->nextBranch(cur,prev);
   244 	while (cur) 
   245 	{
   246 		// FIXME-2 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   247 		pix=model->index (cur);
   248 		d=cur->depth();
   249 		if (treeEditor->isExpanded(pix) && d >= level)
   250 			treeEditor->setExpanded(pix,false);
   251 		model->nextBranch(cur,prev);	
   252 	}
   253 }
   254 
   255 void VymView::showSelection()
   256 {
   257 	QModelIndex ix=model->getSelectedIndex();
   258 	treeEditor->scrollTo( ix, QAbstractItemView::EnsureVisible);
   259 	mapEditor->scrollTo ( ix);
   260 }
   261