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