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