mainwindow.cpp
changeset 725 7ea31701156e
parent 724 cf14046909cd
child 726 7f43b93242aa
     1.1 --- a/mainwindow.cpp	Mon Aug 04 13:35:54 2008 +0000
     1.2 +++ b/mainwindow.cpp	Tue Aug 05 07:36:53 2008 +0000
     1.3 @@ -74,6 +74,7 @@
     1.4  extern QString iconPath;
     1.5  extern QString flagsPath;
     1.6  
     1.7 +
     1.8  Main::Main(QWidget* parent, const char* name, Qt::WFlags f) :
     1.9      QMainWindow(parent,name,f)
    1.10  {
    1.11 @@ -201,9 +202,6 @@
    1.12  	connect( tabWidget, SIGNAL( currentChanged( QWidget * ) ), 
    1.13  		this, SLOT( editorChanged( QWidget * ) ) );
    1.14  
    1.15 -	lineedit=new QLineEdit (this);
    1.16 -	lineedit->hide();
    1.17 -
    1.18  	setCentralWidget(tabWidget);	
    1.19  
    1.20      setupFileActions();
    1.21 @@ -1691,22 +1689,16 @@
    1.22  
    1.23  MapEditor* Main::currentMapEditor() const
    1.24  {
    1.25 -	// FIXME currentMapEditor should return the latest used editor for a model, or NULL if no editor is open
    1.26 -    if ( tabWidget->currentPage() &&
    1.27 -	 tabWidget->currentPage()->inherits( "MapEditor" ) )
    1.28 -		return (MapEditor*)tabWidget->currentPage();
    1.29 +    if ( tabWidget->currentPage())
    1.30 +		return tabModel.at(tabWidget->currentIndex())->getMapEditor();
    1.31      return NULL;	
    1.32  }
    1.33  
    1.34  VymModel* Main::currentModel() const
    1.35  {
    1.36 -	// FIXME better get currentModel from a maintained list,
    1.37 -	// just in case we allow other views in tabs later
    1.38 -	MapEditor *me=currentMapEditor();
    1.39 -	if (me) 
    1.40 -		return me->getModel();
    1.41 -	else
    1.42 -		return NULL;
    1.43 +    if ( tabWidget->currentPage())
    1.44 +		return tabModel.at(tabWidget->currentIndex());
    1.45 +    return NULL;	
    1.46  }
    1.47  
    1.48  
    1.49 @@ -1714,12 +1706,9 @@
    1.50  {
    1.51  	// Unselect all possibly selected objects
    1.52  	// (Important to update note editor)
    1.53 -	MapEditor *me;
    1.54  	for (int i=0;i<=tabWidget->count() -1;i++)
    1.55  	{
    1.56 -		
    1.57 -		me=(MapEditor*)tabWidget->page(i);
    1.58 -		me->getModel()->unselect();
    1.59 +		tabModel.at(i)->unselect();
    1.60  	}	
    1.61  	VymModel *m=currentModel();
    1.62  	if (m) m->reselect();
    1.63 @@ -1728,18 +1717,43 @@
    1.64  	updateActions();
    1.65  }
    1.66  
    1.67 +VymView *Main::createView (VymModel *model)
    1.68 +{
    1.69 +	VymView *vm=new VymView;
    1.70 +
    1.71 +	// Create TreeView
    1.72 +	QTreeView *tv=new QTreeView;
    1.73 +	tv->setModel (model->getTreeModel() );
    1.74 +
    1.75 +	// Create good old MapEditor
    1.76 +	MapEditor* me=model->getMapEditor();
    1.77 +	if (!me) me=new MapEditor (model);
    1.78 +	//me->viewport()->setFocus();
    1.79 +	me->setAntiAlias (actionViewToggleAntiAlias->isOn());
    1.80 +	me->setSmoothPixmap(actionViewToggleSmoothPixmapTransform->isOn());
    1.81 +
    1.82 +	vm->addWidget (tv);
    1.83 +	vm->addWidget (me);
    1.84 +
    1.85 +	// Set geometry
    1.86 +	QList <int> sizes;
    1.87 +	sizes.append (150);
    1.88 +	sizes.append (600);
    1.89 +	vm->setSizes (sizes);
    1.90 +
    1.91 +	return vm;
    1.92 +}
    1.93 +
    1.94  void Main::fileNew()
    1.95  {
    1.96  	VymModel *m=new VymModel;
    1.97 -	models.append (m);
    1.98 +	tabModel.append (m);
    1.99  	MapEditor* me = new MapEditor (m);
   1.100  	me->setObjectName ("MapEditor");
   1.101 -	QString fn="unnamed";
   1.102 -	tabWidget->addTab (me,fn);
   1.103 -	tabWidget->showPage(me);
   1.104 -	me->viewport()->setFocus();
   1.105 -	me->setAntiAlias (actionViewToggleAntiAlias->isOn());
   1.106 -	me->setSmoothPixmap(actionViewToggleSmoothPixmapTransform->isOn());
   1.107 +
   1.108 +	VymView *view=createView (m);
   1.109 +	tabWidget->addTab (view,tr("unnamed","MainWindow: name for new and empty file"));
   1.110 +	tabWidget->setCurrentIndex (tabModel.count() );
   1.111  	
   1.112  	// For the very first map we do not have flagrows yet...
   1.113  	m->select("mc:");
   1.114 @@ -1748,25 +1762,14 @@
   1.115  void Main::fileNewCopy()
   1.116  {
   1.117  	QString fn="unnamed";
   1.118 -	MapEditor* oldME =currentMapEditor();
   1.119 -	if (oldME)
   1.120 +	VymModel *srcModel=currentModel();
   1.121 +	if (srcModel)
   1.122  	{
   1.123 -		oldME->getModel()->copy();
   1.124 -		VymModel *m=new VymModel;
   1.125 -		models.append (m);
   1.126 -		MapEditor* newME = new MapEditor ( m);
   1.127 -		if (newME)
   1.128 -		{
   1.129 -			tabWidget->addTab (newME,fn);
   1.130 -			tabWidget->showPage(newME);
   1.131 -			newME->viewport()->setFocus();
   1.132 -			newME->setAntiAlias (actionViewToggleAntiAlias->isOn());
   1.133 -			newME->setSmoothPixmap(actionViewToggleSmoothPixmapTransform->isOn());
   1.134 -			// For the very first map we do not have flagrows yet...
   1.135 -			m->select("mc:");
   1.136 -			m->load (clipboardDir+"/"+clipboardFile,ImportReplace, VymMap);
   1.137 -		}
   1.138 -
   1.139 +		srcModel->copy();
   1.140 +		fileNew();
   1.141 +		VymModel *dstModel=tabModel.last ();
   1.142 +		dstModel->select("mc:");
   1.143 +		dstModel->load (clipboardDir+"/"+clipboardFile,ImportReplace, VymMap);
   1.144  	}
   1.145  }
   1.146  
   1.147 @@ -1788,8 +1791,7 @@
   1.148  		int i=0;
   1.149  		while (i<=tabWidget->count() -1)
   1.150  		{
   1.151 -			me=(MapEditor*)tabWidget->page(i);
   1.152 -			if (me->getModel()->getFilePath() == fn)
   1.153 +			if (tabModel.at(i)->getFilePath() == fn)
   1.154  			{
   1.155  				// Already there, ask for confirmation
   1.156  				QMessageBox mb( vymName,
   1.157 @@ -1806,7 +1808,7 @@
   1.158  				switch( mb.exec() ) 
   1.159  				{
   1.160  					case QMessageBox::Yes:
   1.161 -						// load anyway
   1.162 +						// end loop and load anyway
   1.163  						i=tabWidget->count();
   1.164  						break;
   1.165  					case QMessageBox::Cancel:
   1.166 @@ -1818,26 +1820,24 @@
   1.167  			i++;
   1.168  		}
   1.169  	}
   1.170 -
   1.171 +	
   1.172 +	int tabIndex=tabWidget->currentPageIndex();
   1.173  
   1.174  	// Try to load map
   1.175      if ( !fn.isEmpty() )
   1.176  	{
   1.177  		me = currentMapEditor();
   1.178 -		int tabIndex=tabWidget->currentPageIndex();
   1.179  		// Check first, if mapeditor exists
   1.180  		// If it is not default AND we want a new map, 
   1.181  		// create a new mapeditor in a new tab
   1.182  		if ( lmode==NewMap && (!me || !me->getModel()->isDefault() )  )
   1.183  		{
   1.184  			VymModel *m=new VymModel;
   1.185 -			models.append (m);
   1.186 -			me= new MapEditor ( m);
   1.187 -			tabWidget->addTab (me,fn);
   1.188 -			tabIndex=tabWidget->indexOf (me);
   1.189 +			tabModel.append (m);
   1.190 +			VymView *view=createView (m);
   1.191 +			tabWidget->addTab (view,fn);
   1.192 +			tabIndex=tabWidget->count()-1;
   1.193  			tabWidget->setCurrentPage (tabIndex);
   1.194 -			me->setAntiAlias (actionViewToggleAntiAlias->isOn());
   1.195 -			me->setSmoothPixmap(actionViewToggleSmoothPixmapTransform->isOn());
   1.196  		}
   1.197  		
   1.198  		// Check, if file exists (important for creating new files
   1.199 @@ -1858,7 +1858,7 @@
   1.200  				case QMessageBox::Yes:
   1.201  					// Create new map
   1.202  					currentMapEditor()->getModel()->setFilePath(fn);
   1.203 -					tabWidget->setTabLabel (currentMapEditor(),
   1.204 +					tabWidget->setTabText (tabIndex,
   1.205  						currentMapEditor()->getModel()->getFileName() );
   1.206  					statusBar()->message( "Created " + fn , statusbarTime );
   1.207  					return success;
   1.208 @@ -1873,7 +1873,7 @@
   1.209  
   1.210  
   1.211  		//tabWidget->currentPage() won't be NULL here, because of above...
   1.212 -		tabWidget->showPage(me);
   1.213 +		tabWidget->setCurrentIndex (tabIndex);
   1.214  		me->viewport()->setFocus();
   1.215  
   1.216  		if (err!=aborted)
   1.217 @@ -1900,7 +1900,7 @@
   1.218  			if (lmode==NewMap)
   1.219  			{
   1.220  				me->getModel()->setFilePath (fn);
   1.221 -				tabWidget->changeTab(tabWidget->page(tabIndex), me->getModel()->getFileName());
   1.222 +				tabWidget->setTabText (tabIndex, me->getModel()->getFileName());
   1.223  				if (!isInTmpDir (fn))
   1.224  				{
   1.225  					// Only append to lastMaps if not loaded from a tmpDir
   1.226 @@ -1990,7 +1990,7 @@
   1.227  		// call fileSaveAs() now, this will call fileSave() 
   1.228  		// again.
   1.229  		// First switch to editor
   1.230 -		tabWidget->setCurrentWidget (m->getMapEditor());
   1.231 +		//FIXME needed???  tabWidget->setCurrentWidget (m->getMapEditor());
   1.232  		fileSaveAs(savemode);
   1.233  	}
   1.234  
   1.235 @@ -2068,13 +2068,13 @@
   1.236  
   1.237  
   1.238  			// Save now
   1.239 -			currentModel()->setFilePath(fn);
   1.240 -			fileSave(currentModel(), savemode);
   1.241 -
   1.242 -			// Set name of tab
   1.243 +			VymModel *m=currentModel();
   1.244 +			m->setFilePath(fn);
   1.245 +			fileSave(m, savemode);
   1.246 +
   1.247 +			// Set name of tab, assuming current tab is the one we just saved
   1.248  			if (savemode==CompleteMap)
   1.249 -				tabWidget->setTabLabel (currentMapEditor(),
   1.250 -					currentModel()->getFileName() );
   1.251 +				tabWidget->setTabText (tabWidget->currentIndex(), m->getFileName() );
   1.252  			return;
   1.253  		} 
   1.254  	}
   1.255 @@ -2342,7 +2342,8 @@
   1.256  			}
   1.257  		} 
   1.258  		me->close();
   1.259 -		tabWidget->removePage(me);
   1.260 +		tabModel.removeAt (tabWidget->currentIndex() );
   1.261 +		tabWidget->removeTab (tabWidget->currentIndex() );
   1.262          delete me;	// FIXME if event was triggered _in_ ME this causes warning message
   1.263  		updateActions();
   1.264  	}
   1.265 @@ -2357,15 +2358,11 @@
   1.266  void Main::fileExitVYM()
   1.267  {
   1.268  	// Check if one or more editors have changed
   1.269 -	MapEditor *me;
   1.270  	int i;
   1.271 -	for (i=0;i<=tabWidget->count() -1;i++)
   1.272 +	for (i=0;i<=tabModel.count() -1;i++)
   1.273  	{
   1.274 -		
   1.275 -		me=(MapEditor*)tabWidget->page(i);
   1.276 -
   1.277  		// If something changed, ask what to do
   1.278 -		if (me->getModel()->hasChanged())
   1.279 +		if (tabModel.at(i)->hasChanged())
   1.280  		{
   1.281  			tabWidget->setCurrentPage(i);
   1.282  			QMessageBox mb( vymName,
   1.283 @@ -2740,11 +2737,9 @@
   1.284  		// compare path with already loaded maps
   1.285  		int index=-1;
   1.286  		int i;
   1.287 -		MapEditor *me;
   1.288 -		for (i=0;i<=tabWidget->count() -1;i++)
   1.289 +		for (i=0;i<=tabModel.count() -1;i++)
   1.290  		{
   1.291 -			me=(MapEditor*)tabWidget->page(i);
   1.292 -			if (vl.at(j)==me->getModel()->getFilePath() )
   1.293 +			if (vl.at(j)==tabModel.at(i)->getFilePath() )
   1.294  			{
   1.295  				index=i;
   1.296  				break;
   1.297 @@ -2759,11 +2754,11 @@
   1.298  			else
   1.299  			{
   1.300  				fileLoad (vl.at(j), NewMap);
   1.301 -				tabWidget->setCurrentPage (tabWidget->count()-1);	
   1.302 +				tabWidget->setCurrentIndex (tabWidget->count()-1);	
   1.303  			}
   1.304  		} else
   1.305  			// Go to tab containing the map
   1.306 -			tabWidget->setCurrentPage (index);	
   1.307 +			tabWidget->setCurrentIndex (index);	
   1.308  	}
   1.309  }
   1.310  
   1.311 @@ -2884,7 +2879,7 @@
   1.312  void Main::editAddMapCenter()
   1.313  {
   1.314  	VymModel *m=currentModel();
   1.315 -	if (!lineedit->isVisible() && m)
   1.316 +	if (m)
   1.317  	{
   1.318  		m->addMapCenter ();
   1.319  	}	
   1.320 @@ -2893,7 +2888,7 @@
   1.321  void Main::editNewBranch()
   1.322  {
   1.323  	VymModel *m=currentModel();
   1.324 -	if (!lineedit->isVisible() && m)
   1.325 +	if (m)
   1.326  	{
   1.327  		BranchObj *bo=(BranchObj*)m->getSelection();
   1.328  		BranchObj *newbo=m->addNewBranch(0);
   1.329 @@ -2920,7 +2915,7 @@
   1.330  void Main::editNewBranchBefore()
   1.331  {
   1.332  	VymModel *m=currentModel();
   1.333 -	if (!lineedit->isVisible() &&  m)
   1.334 +	if (m)
   1.335  	{
   1.336  		BranchObj *bo=(BranchObj*)m->getSelection();
   1.337  		BranchObj *newbo=m->addNewBranchBefore();
   1.338 @@ -2942,7 +2937,7 @@
   1.339  void Main::editNewBranchAbove()
   1.340  {
   1.341  	VymModel *m=currentModel();
   1.342 -	if (!lineedit->isVisible() &&  m)
   1.343 +	if ( m)
   1.344  	{
   1.345  		BranchObj *bo=(BranchObj*)m->getSelection();
   1.346  		BranchObj *newbo=m->addNewBranch (-1);
   1.347 @@ -2964,7 +2959,7 @@
   1.348  void Main::editNewBranchBelow()
   1.349  {
   1.350  	VymModel *m=currentModel();
   1.351 -	if (!lineedit->isVisible() &&  m)
   1.352 +	if (m)
   1.353  	{
   1.354  		BranchObj *bo=(BranchObj*)m->getSelection();
   1.355  		BranchObj *newbo=m->addNewBranch (1);
   1.356 @@ -3372,10 +3367,10 @@
   1.357  {
   1.358  	bool b=actionViewToggleAntiAlias->isOn();
   1.359  	MapEditor *me;
   1.360 -	for (int i=0;i<tabWidget->count();i++)
   1.361 +	for (int i=0;i<tabModel.count();i++)
   1.362  	{
   1.363 -		me=(MapEditor*)tabWidget->page(i);
   1.364 -		me->setAntiAlias(b);
   1.365 +		me=tabModel.at(i)->getMapEditor();
   1.366 +		if (me) me->setAntiAlias(b);
   1.367  	}	
   1.368  
   1.369  }
   1.370 @@ -3384,11 +3379,11 @@
   1.371  {
   1.372  	bool b=actionViewToggleSmoothPixmapTransform->isOn();
   1.373  	MapEditor *me;
   1.374 -	for (int i=0;i<tabWidget->count();i++)
   1.375 +	for (int i=0;i<tabModel.count();i++)
   1.376  	{
   1.377  		
   1.378 -		me=(MapEditor*)tabWidget->page(i);
   1.379 -		me->setSmoothPixmap(b);
   1.380 +		me=tabModel.at(i)->getMapEditor();
   1.381 +		if (me) me->setSmoothPixmap(b);
   1.382  	}	
   1.383  }
   1.384  
   1.385 @@ -3679,14 +3674,14 @@
   1.386  
   1.387  void Main::windowNextEditor()
   1.388  {
   1.389 -	if (tabWidget->currentPageIndex() < tabWidget->count())
   1.390 -		tabWidget->setCurrentPage (tabWidget->currentPageIndex() +1);
   1.391 +	if (tabWidget->currentIndex() < tabWidget->count())
   1.392 +		tabWidget->setCurrentIndex (tabWidget->currentIndex() +1);
   1.393  }
   1.394  
   1.395  void Main::windowPreviousEditor()
   1.396  {
   1.397 -	if (tabWidget->currentPageIndex() >0)
   1.398 -		tabWidget->setCurrentPage (tabWidget->currentPageIndex() -1);
   1.399 +	if (tabWidget->currentIndex() >0)
   1.400 +		tabWidget->setCurrentIndex (tabWidget->currentIndex() -1);
   1.401  }
   1.402  
   1.403  void Main::standardFlagChanged()