vymview.cpp
author insilmaril
Tue, 30 Mar 2010 17:30:39 +0000
changeset 842 bec082472471
parent 837 5ecd0462f76b
permissions -rw-r--r--
Much improved results in FindResultsWidget
     1 #include "vymview.h"
     2 
     3 #include "branchitem.h"
     4 #include "mainwindow.h"
     5 #include "mapeditor.h"
     6 #include "treeeditor.h"
     7 
     8 extern Main *mainWindow;
     9 
    10 
    11 VymView::VymView(VymModel *m)
    12 {
    13 	model=m;
    14 
    15 	// Create TreeView
    16 	treeEditor=new TreeEditor (model);
    17 	//treeEditor->setModel ((QAbstractItemModel*)model);
    18 	//treeEditor->setMinimumWidth (50);
    19 
    20 	treeEditor->setColumnWidth (0,150);
    21 	treeEditor->setAnimated (true);
    22 
    23 	selModel=new QItemSelectionModel (model);
    24 
    25 	model->setSelectionModel (selModel);
    26 	treeEditor->setSelectionModel (selModel);
    27 
    28 	// Create good old MapEditor
    29 	mapEditor=model->getMapEditor();
    30 	if (!mapEditor) mapEditor=new MapEditor (model);
    31 
    32 	// Create Layout 
    33 	QVBoxLayout* mainLayout = new QVBoxLayout (this); //FIXME-4 not needed
    34 	QSplitter *splitter= new QSplitter (this);
    35 
    36 	QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    37     splitter->setSizePolicy(sizePolicy);
    38 	mainLayout->addWidget (splitter);
    39 
    40 	// Connect selections
    41 
    42 		// Selection in Model changed	
    43 		connect (
    44 			selModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    45 			this,SLOT (changeSelection(const QItemSelection &,const QItemSelection &)));
    46 
    47 		// Tell MapEditor to update selection
    48 		/* FIXME-3 done implicit here in VymView
    49 		connect (
    50 			selModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    51 			mapEditor,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
    52 			*/
    53 
    54 		// Needed to update selbox during animation
    55 		connect (
    56 			model, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
    57 			mapEditor,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
    58 
    59 	// Connect data changed signals	
    60 	connect (
    61 		model, SIGNAL (dataChanged(const QModelIndex &, const QModelIndex &)), 
    62 		mapEditor,SLOT (updateData(const QModelIndex &) ) );
    63 
    64 	connect (
    65 		model, SIGNAL (sortFilterChanged (const QString &)),
    66 		treeEditor, SLOT (setSortFilter (const QString &) ) );
    67 
    68 	connect (
    69 		model, SIGNAL (noteHasChanged(QModelIndex) ),
    70 		mainWindow, SLOT (updateNoteEditor (QModelIndex) ) );
    71 		
    72 	connect (
    73 		model, SIGNAL (expandAll() ),
    74 		this, SLOT (expandAll () ) );
    75 		
    76 	connect (
    77 		model, SIGNAL (expandOneLevel() ),
    78 		this, SLOT (expandOneLevel() ) );
    79 		
    80 	connect (
    81 		model, SIGNAL (collapseOneLevel() ),
    82 		this, SLOT (collapseOneLevel() ) );
    83 		
    84 	connect (
    85 		model, SIGNAL (showSelection() ),
    86 		this, SLOT (showSelection() ) );
    87 		
    88 	connect (
    89 		model, SIGNAL (updateLayout() ),
    90 		mapEditor, SLOT (autoLayout() ) );
    91 		
    92 	mapEditor->setAntiAlias (mainWindow->isAliased());
    93 	mapEditor->setSmoothPixmap(mainWindow->hasSmoothPixmapTransform());
    94 
    95 	splitter->addWidget (treeEditor);
    96 	splitter->addWidget (mapEditor);
    97 
    98 	// Set geometry
    99 	QList <int> widths;
   100 	widths<<200;
   101 	widths<<600;
   102 	splitter->setSizes(widths);
   103 }
   104 
   105 VymView::~VymView()
   106 {
   107 	//cout << "Destructor VymView\n";
   108 }
   109 
   110 VymModel* VymView::getModel()
   111 {
   112 	return model;
   113 }
   114 
   115 MapEditor* VymView::getMapEditor()
   116 {
   117 	return mapEditor;
   118 }
   119 
   120 TreeEditor* VymView::getTreeEditor()
   121 {
   122 	return treeEditor;
   123 }
   124 
   125 void VymView::initFocus()
   126 {
   127 	mapEditor->setFocus();
   128 }
   129 
   130 void VymView::changeSelection (const QItemSelection &newsel, const QItemSelection &oldsel)	
   131 {
   132 	//cout << "VV::changeSelection   newsel.count="<<newsel.indexes().count()<<endl;
   133 
   134 	mainWindow->changeSelection (model,newsel,oldsel);	
   135 	mapEditor->updateSelection (newsel,oldsel);
   136 
   137 	if (newsel.indexes().count()>0)
   138 	{
   139 
   140 		
   141 		QModelIndex ix=newsel.indexes().first();
   142 		selModel->setCurrentIndex (ix,QItemSelectionModel::ClearAndSelect  );
   143 		treeEditor->setCurrentIndex (ix);
   144 		showSelection();
   145 		
   146 	}
   147 }
   148 
   149 void VymView::expandAll()
   150 {
   151 	treeEditor->expandAll();
   152 }
   153 
   154 void VymView::expandOneLevel()
   155 {
   156 	int level=999999;
   157 	int d;
   158 	BranchItem *cur=NULL;
   159 	BranchItem *prev=NULL;
   160 	QModelIndex pix;
   161 
   162 	// Find level to expand
   163 	model->nextBranch(cur,prev);
   164 	while (cur) 
   165 	{
   166 		pix=model->index (cur);
   167 		d=cur->depth();
   168 		if (!treeEditor->isExpanded(pix) && d < level)
   169 			level=d;
   170 		model->nextBranch(cur,prev);	
   171 	}
   172 
   173 	// Expand all to level
   174 	cur=NULL;
   175 	prev=NULL;
   176 	model->nextBranch(cur,prev);
   177 	while (cur) 
   178 	{
   179 		pix=model->index (cur);
   180 		d=cur->depth();
   181 		if (!treeEditor->isExpanded(pix) && d <= level && cur->branchCount()>0)
   182 			treeEditor->setExpanded(pix,true);
   183 		model->nextBranch(cur,prev);	
   184 	}
   185 	/* FIXME-3 optimize expanding by using flat version of next
   186 	model->nextBranch(cur,prev,false);
   187 	while (cur) 
   188 	{
   189 		cout << "ok: "<<cur->getHeadingStd()<<endl./re/videochristinaprison1_wmvl.wmv
   190 ;
   191 		model->nextBranch(cur,prev,false);	
   192 	}
   193 	*/
   194 }
   195 
   196 void VymView::collapseOneLevel()
   197 {
   198 	int level=-1;
   199 	int d;
   200 	BranchItem *cur=NULL;
   201 	BranchItem *prev=NULL;
   202 	QModelIndex pix;
   203 
   204 	// Find level to collapse
   205 	model->nextBranch(cur,prev);
   206 	while (cur) 
   207 	{
   208 		pix=model->index (cur);
   209 		d=cur->depth();
   210 		if (treeEditor->isExpanded(pix) && d > level)
   211 			level=d;
   212 		model->nextBranch(cur,prev);	
   213 	}
   214 
   215 	// collapse all to level
   216 	cur=NULL;
   217 	prev=NULL;
   218 	model->nextBranch(cur,prev);
   219 	while (cur) 
   220 	{
   221 		pix=model->index (cur);
   222 		d=cur->depth();
   223 		if (treeEditor->isExpanded(pix) && d >= level)
   224 			treeEditor->setExpanded(pix,false);
   225 		model->nextBranch(cur,prev);	
   226 	}
   227 }
   228 
   229 void VymView::showSelection()
   230 {
   231 	QModelIndex ix=model->getSelectedIndex();
   232 	treeEditor->scrollTo( ix, QAbstractItemView::EnsureVisible);
   233 	mapEditor->scrollTo ( ix);	// FIXME-3 also called from MapEditor::updateSelection...
   234 }
   235 
   236 void VymView::toggleTreeEditor()
   237 {
   238 	if (treeEditor->isVisible() )
   239 		treeEditor->hide();
   240 	else
   241 		treeEditor->show();
   242 }
   243 
   244