vymview.cpp
author insilmaril
Fri, 19 Feb 2010 13:47:03 +0000
changeset 823 0bba81dde1bc
parent 822 c2ce9944148c
child 825 1ad892c1a709
permissions -rw-r--r--
More fixes
     1 #include "vymview.h"
     2 
     3 #include <iostream>
     4 using namespace std;
     5 
     6 #include "branchitem.h"
     7 #include "findwidget.h"
     8 #include "mainwindow.h"
     9 #include "mapeditor.h"
    10 #include "treeeditor.h"
    11 
    12 extern Main *mainWindow;
    13 
    14 
    15 VymView::VymView(VymModel *m)
    16 {
    17 	model=m;
    18 
    19 	// Create findWidget
    20 	findWidget=new FindWidget (this);
    21 	findWidget->hide();
    22 
    23 	// Create TreeView
    24 	treeEditor=new TreeEditor (model);
    25 	//treeEditor->setModel ((QAbstractItemModel*)model);
    26 	//treeEditor->setMinimumWidth (50);
    27 
    28 	treeEditor->setColumnWidth (0,150);
    29 	treeEditor->setAnimated (true);
    30 
    31 	// FIXME-3 use proxySelModel=treeEditor->selectionModel();
    32 	selModel=new QItemSelectionModel (model);
    33 
    34 	//model->setSelectionModel (proxySelModel);
    35 	model->setSelectionModel (selModel);
    36 	treeEditor->setSelectionModel (selModel);
    37 
    38 	// Create good old MapEditor
    39 	mapEditor=model->getMapEditor();
    40 	if (!mapEditor) mapEditor=new MapEditor (model);
    41 
    42 	// Create Layout 
    43 	QVBoxLayout* mainLayout = new QVBoxLayout (this);
    44 	QSplitter *splitter= new QSplitter;
    45 
    46 	QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    47     //sizePolicy.setHorizontalStretch(0);
    48     //sizePolicy.setVerticalStretch(0);
    49     //sizePolicy.setHeightForWidth(this->sizePolicy().hasHeightForWidth());
    50     splitter->setSizePolicy(sizePolicy);
    51 	mainLayout->addWidget (splitter);
    52 	mainLayout->addWidget (findWidget);
    53 
    54 	// Connect selections
    55 
    56 		// Proxymodel changed
    57 		/*
    58 		connect (
    59 			proxySelModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    60 			this,SLOT (changeProxySelection(const QItemSelection &,const QItemSelection &)));
    61 */
    62 
    63 		// Selection in Model changed	
    64 		connect (
    65 			selModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    66 			this,SLOT (changeSelection(const QItemSelection &,const QItemSelection &)));
    67 
    68 		// Tell MapEditor to update selection
    69 		/* FIXME-3 done implicit here in VymView
    70 		connect (
    71 			selModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    72 			mapEditor,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
    73 			*/
    74 
    75 		// Needed to update selbox during animation
    76 		connect (
    77 			model, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    78 			mapEditor,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
    79 
    80 	// Connect data changed signals	
    81 	connect (
    82 		model, SIGNAL (dataChanged(const QModelIndex &, const QModelIndex &)), 
    83 		mapEditor,SLOT (updateData(const QModelIndex &) ) );
    84 
    85 	connect (
    86 		model, SIGNAL (sortFilterChanged (const QString &)),
    87 		treeEditor, SLOT (setSortFilter (const QString &) ) );
    88 
    89 	connect (
    90 		model, SIGNAL (noteHasChanged(QModelIndex) ),
    91 		mainWindow, SLOT (updateNoteEditor (QModelIndex) ) );
    92 		
    93 	connect (
    94 		model, SIGNAL (expandAll() ),
    95 		this, SLOT (expandAll () ) );
    96 		
    97 	connect (
    98 		model, SIGNAL (expandOneLevel() ),
    99 		this, SLOT (expandOneLevel() ) );
   100 		
   101 	connect (
   102 		model, SIGNAL (collapseOneLevel() ),
   103 		this, SLOT (collapseOneLevel() ) );
   104 		
   105 	connect (
   106 		model, SIGNAL (showSelection() ),
   107 		this, SLOT (showSelection() ) );
   108 		
   109 	// Find	
   110 	connect (
   111 		model, SIGNAL (showFindWidget() ),
   112 		this, SLOT (showFindWidget() ) );
   113 		
   114 	connect (
   115 		findWidget , SIGNAL (hideFindWidget() ),
   116 		this, SLOT (hideFindWidget() ) );
   117 		
   118 	connect (
   119 		findWidget, SIGNAL (nextButton (QString) ),
   120 		this, SLOT (findNext(QString) ) );
   121 		
   122 	mapEditor->setAntiAlias (mainWindow->isAliased());
   123 	mapEditor->setSmoothPixmap(mainWindow->hasSmoothPixmapTransform());
   124 
   125 	splitter->addWidget (treeEditor);
   126 	splitter->addWidget (mapEditor);
   127 
   128 	// Set geometry
   129 	QList <int> widths;
   130 	widths<<200;
   131 	widths<<600;
   132 	splitter->setSizes(widths);
   133 }
   134 
   135 VymView::~VymView()
   136 {
   137 	//cout << "Destructor VymView\n";
   138 }
   139 
   140 VymModel* VymView::getModel()
   141 {
   142 	return model;
   143 }
   144 
   145 MapEditor* VymView::getMapEditor()
   146 {
   147 	return mapEditor;
   148 }
   149 
   150 void VymView::initFocus()
   151 {
   152 	mapEditor->setFocus();
   153 }
   154 
   155 void VymView::changeSelection (const QItemSelection &newsel, const QItemSelection &oldsel)	
   156 {
   157 	//cout << "VV::changeSelection   newsel.count="<<newsel.indexes().count()<<endl;
   158 
   159 	mainWindow->changeSelection (model,newsel,oldsel);	
   160 	mapEditor->updateSelection (newsel,oldsel);
   161 
   162 	if (newsel.indexes().count()>0)
   163 	{
   164 
   165 	/* FIXME-3 use proxymodel
   166 		proxySelModel->select (
   167 			treeEditor->getProxyModel()->mapSelectionFromSource (newsel),
   168 			QItemSelectionModel::ClearAndSelect );
   169 		*/
   170 		
   171 		QModelIndex ix=newsel.indexes().first();
   172 		selModel->setCurrentIndex (ix,QItemSelectionModel::ClearAndSelect  );
   173 		treeEditor->setCurrentIndex (ix);
   174 		showSelection();
   175 		
   176 	}
   177 }
   178 
   179 void VymView::changeProxySelection (const QItemSelection &newsel, const QItemSelection &)
   180 {
   181 	// Notify mainwindow to update satellites, but map selection to 
   182 	// original model first
   183 
   184 	//cout << "VV::changeProxySelection   newsel.count="<<newsel.indexes().count()<<endl;
   185 	if (!newsel.indexes().isEmpty())
   186 	{
   187 	/* FIXME-3 need to set current, too
   188 	*/
   189 		proxySelModel->setCurrentIndex (
   190 			newsel.indexes().first(),
   191 			QItemSelectionModel::ClearAndSelect );
   192 	treeEditor->setCurrentIndex (newsel.indexes().first() );
   193 	}
   194 
   195 	// Re-emit but map selection first
   196 	selModel->select (
   197 		treeEditor->getProxyModel()->mapSelectionToSource (newsel),
   198 		QItemSelectionModel::ClearAndSelect );
   199 
   200 	showSelection();
   201 }
   202 
   203 void VymView::expandAll()
   204 {
   205 	treeEditor->expandAll();
   206 }
   207 
   208 void VymView::expandOneLevel()
   209 {
   210 	int level=999999;
   211 	int d;
   212 	BranchItem *cur=NULL;
   213 	BranchItem *prev=NULL;
   214 	QModelIndex pix;
   215 
   216 	// Find level to expand
   217 	model->nextBranch(cur,prev);
   218 	while (cur) 
   219 	{
   220 		// FIXME-3 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   221 		pix=model->index (cur);
   222 		d=cur->depth();
   223 		if (!treeEditor->isExpanded(pix) && d < level)
   224 			level=d;
   225 		model->nextBranch(cur,prev);	
   226 	}
   227 
   228 	// Expand all to level
   229 	cur=NULL;
   230 	prev=NULL;
   231 	model->nextBranch(cur,prev);
   232 	while (cur) 
   233 	{
   234 		// FIXME-3 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   235 		pix=model->index (cur);
   236 		d=cur->depth();
   237 		if (!treeEditor->isExpanded(pix) && d <= level && cur->branchCount()>0)
   238 			treeEditor->setExpanded(pix,true);
   239 		model->nextBranch(cur,prev);	
   240 	}
   241 	/* FIXME-3 optimize expanding by using flat version of next
   242 	model->nextBranch(cur,prev,false);
   243 	while (cur) 
   244 	{
   245 		cout << "ok: "<<cur->getHeadingStd()<<endl./re/videochristinaprison1_wmvl.wmv
   246 ;
   247 		model->nextBranch(cur,prev,false);	
   248 	}
   249 	*/
   250 }
   251 
   252 void VymView::collapseOneLevel()
   253 {
   254 	int level=-1;
   255 	int d;
   256 	BranchItem *cur=NULL;
   257 	BranchItem *prev=NULL;
   258 	QModelIndex pix;
   259 
   260 	// Find level to collapse
   261 	model->nextBranch(cur,prev);
   262 	while (cur) 
   263 	{
   264 		// FIXME-3 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   265 		pix=model->index (cur);
   266 		d=cur->depth();
   267 		if (treeEditor->isExpanded(pix) && d > level)
   268 			level=d;
   269 		model->nextBranch(cur,prev);	
   270 	}
   271 
   272 	// collapse all to level
   273 	cur=NULL;
   274 	prev=NULL;
   275 	model->nextBranch(cur,prev);
   276 	while (cur) 
   277 	{
   278 		// FIXME-3 use proxy pix=treeEditor->getProxyModel()->mapFromSource (model->index (cur));
   279 		pix=model->index (cur);
   280 		d=cur->depth();
   281 		if (treeEditor->isExpanded(pix) && d >= level)
   282 			treeEditor->setExpanded(pix,false);
   283 		model->nextBranch(cur,prev);	
   284 	}
   285 }
   286 
   287 void VymView::showSelection()
   288 {
   289 	QModelIndex ix=model->getSelectedIndex();
   290 	treeEditor->scrollTo( ix, QAbstractItemView::EnsureVisible);
   291 	mapEditor->scrollTo ( ix);	// FIXME-3 also called from MapEditor::updateSelection...
   292 }
   293 
   294 void VymView::showFindWidget()
   295 {
   296 	findWidget->popup();
   297 }
   298 
   299 void VymView::hideFindWidget()
   300 {
   301 	// findWidget hides itself, but we want to have focus back at mapEditor usually
   302 	if (mapEditor) mapEditor->setFocus();
   303 }
   304 
   305 void VymView::findNext (QString s)
   306 {
   307 	bool cs=false;
   308 	BranchItem *bi=model->findText(s, cs);
   309 	if (bi)
   310 		findWidget->setStatus (FindWidget::Success);
   311 	else
   312 		findWidget->setStatus (FindWidget::Failed);
   313 
   314 }
   315 
   316 void VymView::findReset()
   317 {
   318 	model->findReset();
   319 	if (mapEditor) mapEditor->setFocus();
   320 }
   321 
   322 void VymView::toggleTreeEditor()
   323 {
   324 	if (treeEditor->isVisible() )
   325 		treeEditor->hide();
   326 	else
   327 		treeEditor->show();
   328 }
   329 
   330