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