vymview.cpp
author insilmaril
Wed, 25 Nov 2009 10:58:21 +0000
changeset 807 f9f7922989d8
parent 806 2a33304714ba
child 808 b163492fda17
permissions -rw-r--r--
Added demos/vym-contribute.vym, fixes for selecting items
     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 		/* FIXME-3 done implicit here in VymView
    53 		connect (
    54 			selModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    55 			mapEditor,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
    56 			*/
    57 
    58 		// FIXME-2 testing, if that reenables updating selbox during animation
    59 		connect (
    60 			model, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    61 			mapEditor,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
    62 
    63 	// Connect data changed signals	
    64 	connect (
    65 		model, SIGNAL (dataChanged(const QModelIndex &, const QModelIndex &)), 
    66 		mapEditor,SLOT (updateData(const QModelIndex &) ) );
    67 
    68 	connect (
    69 		model, SIGNAL (sortFilterChanged (const QString &)),
    70 		treeEditor, SLOT (setSortFilter (const QString &) ) );
    71 
    72 	connect (
    73 		model, SIGNAL (noteHasChanged(QModelIndex) ),
    74 		mainWindow, SLOT (updateNoteEditor (QModelIndex) ) );
    75 		
    76 	connect (
    77 		model, SIGNAL (expandAll() ),
    78 		this, SLOT (expandAll () ) );
    79 		
    80 	connect (
    81 		model, SIGNAL (expandOneLevel() ),
    82 		this, SLOT (expandOneLevel() ) );
    83 		
    84 	connect (
    85 		model, SIGNAL (collapseOneLevel() ),
    86 		this, SLOT (collapseOneLevel() ) );
    87 		
    88 	connect (
    89 		model, SIGNAL (showSelection() ),
    90 		this, SLOT (showSelection() ) );
    91 		
    92 
    93 	mapEditor->setAntiAlias (mainWindow->isAliased());
    94 	mapEditor->setSmoothPixmap(mainWindow->hasSmoothPixmapTransform());
    95 
    96 	addWidget (treeEditor);
    97 	addWidget (mapEditor);
    98 
    99 	// Set geometry
   100 	QList <int> widths;
   101 	widths<<200;
   102 	widths<<600;
   103 	setSizes(widths);
   104 }
   105 
   106 VymView::~VymView()
   107 {
   108 	//cout << "Destructor VymView\n";
   109 }
   110 
   111 VymModel* VymView::getModel()
   112 {
   113 	return model;
   114 }
   115 
   116 MapEditor* VymView::getMapEditor()
   117 {
   118 	return mapEditor;
   119 }
   120 
   121 void VymView::initFocus()
   122 {
   123 	mapEditor->setFocus();
   124 }
   125 
   126 void VymView::changeSelection (const QItemSelection &newsel, const QItemSelection &oldsel)	
   127 {
   128 	//cout << "VV::changeSelection   newsel.count="<<newsel.indexes().count()<<endl;
   129 
   130 	mainWindow->changeSelection (model,newsel,oldsel);	
   131 	mapEditor->updateSelection (newsel,oldsel);
   132 
   133 	if (newsel.indexes().count()>0)
   134 	{
   135 
   136 	/* FIXME-2 use proxymodel
   137 		proxySelModel->select (
   138 			treeEditor->getProxyModel()->mapSelectionFromSource (newsel),
   139 			QItemSelectionModel::ClearAndSelect );
   140 		*/
   141 		
   142 		QModelIndex ix=newsel.indexes().first();
   143 		selModel->setCurrentIndex (ix,QItemSelectionModel::ClearAndSelect  );
   144 		treeEditor->setCurrentIndex (ix);
   145 		showSelection();
   146 		
   147 	}
   148 }
   149 
   150 void VymView::changeProxySelection (const QItemSelection &newsel, const QItemSelection &oldsel)
   151 {
   152 	// Notify mainwindow to update satellites, but map selection to 
   153 	// original model first
   154 
   155 	//cout << "VV::changeProxySelection   newsel.count="<<newsel.indexes().count()<<endl;
   156 	if (!newsel.indexes().isEmpty())
   157 	{
   158 	/* FIXME-2 need to set current, too
   159 	*/
   160 		proxySelModel->setCurrentIndex (
   161 			newsel.indexes().first(),
   162 			QItemSelectionModel::ClearAndSelect );
   163 	treeEditor->setCurrentIndex (newsel.indexes().first() );
   164 	}
   165 
   166 	// Re-emit but map selection first
   167 	selModel->select (
   168 		treeEditor->getProxyModel()->mapSelectionToSource (newsel),
   169 		QItemSelectionModel::ClearAndSelect );
   170 
   171 	showSelection();
   172 }
   173 
   174 void VymView::expandAll()
   175 {
   176 	treeEditor->expandAll();
   177 }
   178 
   179 void VymView::expandOneLevel()
   180 {
   181 	int level=999999;
   182 	int d;
   183 	BranchItem *cur=NULL;
   184 	BranchItem *prev=NULL;
   185 	QModelIndex pix;
   186 
   187 	// Find level to expand
   188 	model->nextBranch(cur,prev);
   189 	while (cur) 
   190 	{
   191 		// FIXME-2 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   192 		pix=model->index (cur);
   193 		d=cur->depth();
   194 		if (!treeEditor->isExpanded(pix) && d < level)
   195 			level=d;
   196 		model->nextBranch(cur,prev);	
   197 	}
   198 
   199 	// Expand all to level
   200 	cur=NULL;
   201 	prev=NULL;
   202 	model->nextBranch(cur,prev);
   203 	while (cur) 
   204 	{
   205 		// FIXME-2 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   206 		pix=model->index (cur);
   207 		d=cur->depth();
   208 		if (!treeEditor->isExpanded(pix) && d <= level && cur->branchCount()>0)
   209 			treeEditor->setExpanded(pix,true);
   210 		model->nextBranch(cur,prev);	
   211 	}
   212 	/* FIXME-3 optimize expanding by using flat version of next
   213 	model->nextBranch(cur,prev,false);
   214 	while (cur) 
   215 	{
   216 		cout << "ok: "<<cur->getHeadingStd()<<endl./re/videochristinaprison1_wmvl.wmv
   217 ;
   218 		model->nextBranch(cur,prev,false);	
   219 	}
   220 	*/
   221 }
   222 
   223 void VymView::collapseOneLevel()
   224 {
   225 	int level=-1;
   226 	int d;
   227 	BranchItem *cur=NULL;
   228 	BranchItem *prev=NULL;
   229 	QModelIndex pix;
   230 
   231 	// Find level to collapse
   232 	model->nextBranch(cur,prev);
   233 	while (cur) 
   234 	{
   235 		// FIXME-2 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   236 		pix=model->index (cur);
   237 		d=cur->depth();
   238 		if (treeEditor->isExpanded(pix) && d > level)
   239 			level=d;
   240 		model->nextBranch(cur,prev);	
   241 	}
   242 
   243 	// collapse all to level
   244 	cur=NULL;
   245 	prev=NULL;
   246 	model->nextBranch(cur,prev);
   247 	while (cur) 
   248 	{
   249 		// FIXME-2 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   250 		pix=model->index (cur);
   251 		d=cur->depth();
   252 		if (treeEditor->isExpanded(pix) && d >= level)
   253 			treeEditor->setExpanded(pix,false);
   254 		model->nextBranch(cur,prev);	
   255 	}
   256 }
   257 
   258 void VymView::showSelection()
   259 {
   260 	QModelIndex ix=model->getSelectedIndex();
   261 	treeEditor->scrollTo( ix, QAbstractItemView::EnsureVisible);
   262 	mapEditor->scrollTo ( ix);	// FIXME-3 also called from MapEditor::updateSelection...
   263 }
   264