mapeditor.cpp
changeset 129 9b9c7e8b9147
parent 128 0dc9286ad3d4
child 130 30cd58b70d02
     1.1 --- a/mapeditor.cpp	Wed Jul 13 11:36:15 2005 +0000
     1.2 +++ b/mapeditor.cpp	Tue Jul 19 14:44:30 2005 +0000
     1.3 @@ -18,6 +18,7 @@
     1.4  #include <qdragobject.h>
     1.5  #include <qurloperator.h>
     1.6  #include <qnetworkprotocol.h>
     1.7 +#include <qregexp.h>
     1.8  
     1.9  #include <iostream>
    1.10  #include <cstdlib>
    1.11 @@ -611,37 +612,93 @@
    1.12  	*/
    1.13  }
    1.14  
    1.15 -void MapEditor::parseAtom(const QString &s)
    1.16 +void MapEditor::parseAtom(const QString &atom)
    1.17  {
    1.18  	API api;
    1.19 -	QString c,p,p0;
    1.20 -	api.parseCommand (s,c,p);
    1.21 -	api.getString(p,p0);
    1.22 -	/* FIXME testing
    1.23 -	cout <<"ME::parseAtom  s="<<s<<endl;
    1.24 -	cout <<"ME::parseAtom  c="<<c<<endl;
    1.25 -	cout <<"ME::parseAtom  p="<<p<<endl;
    1.26 -	*/
    1.27 -
    1.28 +	QString s;
    1.29 +	int x,y;
    1.30 +	bool ok;
    1.31 +
    1.32 +	// Split string s into command and parameters
    1.33 +	api.parseCommand (atom);
    1.34 +	QString com=api.command();
    1.35 +	
    1.36  	// External commands
    1.37 -	if (c==QString("moveBranchUp"))
    1.38 +	if (com==QString("moveBranchUp"))
    1.39  		moveBranchUp();
    1.40 -	else if (c=="moveBranchDown")
    1.41 +	else if (com=="moveBranchDown")
    1.42  		moveBranchDown();
    1.43 -	else if (c=="setHeading")
    1.44 -		setHeading (p0);
    1.45 +	else if (com=="move")
    1.46 +	{
    1.47 +		if (api.checkParamCount(2) &&
    1.48 +			selection && 
    1.49 +			typeid(*selection) == typeid(BranchObj) ) 
    1.50 +		{	
    1.51 +			x=api.parInt (ok,0);
    1.52 +			if (ok)
    1.53 +			{
    1.54 +				y=api.parInt (ok,1);
    1.55 +				if (ok) ((BranchObj*)(selection))->move (x,y);
    1.56 +			}
    1.57 +		}	
    1.58 +	}
    1.59 +	else if (com=="linkBranchToPos")
    1.60 +	{
    1.61 +		if (selection && typeid(*selection) == typeid(BranchObj) ) 
    1.62 +		{
    1.63 +			if (api.checkParamCount(4))
    1.64 +			{
    1.65 +				s=api.parString(ok,0);
    1.66 +				LinkableMapObj *dst=mapCenter->findObjBySelect (s);
    1.67 +				if (dst)
    1.68 +				{	
    1.69 +					if (typeid(*dst) == typeid(BranchObj) ) 
    1.70 +						((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),-1);
    1.71 +					if (typeid(*dst) == typeid(MapCenterObj) ) 
    1.72 +					{
    1.73 +						((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),-1);
    1.74 +						x=api.parInt (ok,2);
    1.75 +						if (ok)
    1.76 +						{
    1.77 +							y=api.parInt (ok,3);
    1.78 +							if (ok) ((BranchObj*)(selection))->move (x,y);
    1.79 +						}
    1.80 +					}	
    1.81 +				}	
    1.82 +			}
    1.83 +		}
    1.84 +	} else if (com=="setHeading")
    1.85 +	{
    1.86 +		if (api.checkParamCount(1))
    1.87 +		{
    1.88 +			s=api.parString (ok,0);
    1.89 +			if (ok) setHeading (s);
    1.90 +		}	
    1.91 +	}	
    1.92  	// Internal commands, used for undo etc.	
    1.93 -	else if (c==QString("undoMap"))
    1.94 +	else if (com==QString("undoMap"))
    1.95  		undoXML("");
    1.96 -	else if (c==QString("undoPart"))
    1.97 -		undoXML(p0);
    1.98 -	else if (c=="select")
    1.99 -		select (p0);
   1.100 +	else if (com==QString("undoPart"))
   1.101 +	{
   1.102 +		if (api.checkParamCount(1))
   1.103 +		{
   1.104 +			s=api.parString (ok,0);
   1.105 +			undoXML(s);
   1.106 +		}
   1.107 +	} else if (com=="select")
   1.108 +		if (api.checkParamCount(1))
   1.109 +		{
   1.110 +			s=api.parString(ok,0);
   1.111 +			if (ok) select (s);
   1.112 +		}	
   1.113  	else
   1.114 +		api.setError ("Unknown command in: "+atom);
   1.115 +
   1.116 +	// Any errors?
   1.117 +	if (api.error())
   1.118  	{
   1.119  		cout << "MapEditor::parseAtom: Error!\n";
   1.120 -		cout << "  Command unknown: \""<<c<<"\""<<endl;
   1.121 -		cout << "     Used in atom: \""<<s<<"\""<<endl;
   1.122 +		cout << "  "<<api.errorDesc()<<endl;
   1.123  	}	
   1.124  }
   1.125  
   1.126 @@ -1233,11 +1290,13 @@
   1.127  	if (lineedit) finishedLineEditNoSave();
   1.128  
   1.129  	// Unselect
   1.130 +	/*FIXME testing
   1.131  	if (selection) 
   1.132  	{
   1.133  		selection->unselect();
   1.134  		selection=NULL;
   1.135  	}	
   1.136 +	*/	
   1.137  		
   1.138  	parseAtom (undoCommand);
   1.139  	mapCenter->reposition();
   1.140 @@ -1415,6 +1474,21 @@
   1.141  	}	
   1.142  }
   1.143  
   1.144 +void MapEditor::linkBranchToPos (LinkableMapObj *dst, const int &pos, const QPoint &p)
   1.145 +{
   1.146 +	// FIXME  no saveState, because this is only internal at undo so far
   1.147 +	if (selection && typeid(*selection) == typeid(BranchObj) ) 
   1.148 +	{
   1.149 +		if (dst  &&  typeid(*dst) == typeid(BranchObj) ) 
   1.150 +			((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),-1);
   1.151 +		if (dst  &&  typeid(*dst) == typeid(MapCenterObj) ) 
   1.152 +		{
   1.153 +			((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),-1);
   1.154 +			((BranchObj*)(selection))->move (p);
   1.155 +		}	
   1.156 +	}
   1.157 +}
   1.158 +
   1.159  void MapEditor::editHeading()
   1.160  {
   1.161  	// Finish open lineEdits
   1.162 @@ -3006,7 +3080,6 @@
   1.163  			selection->select();
   1.164  				
   1.165  			adjustCanvasSize();
   1.166 -			
   1.167  		}
   1.168  
   1.169  		// Check, if systemFlag clicked
   1.170 @@ -3035,6 +3108,11 @@
   1.171  		// Left Button	    Move Branches
   1.172  		if (e->button() == QMouseEvent::LeftButton )
   1.173  		{
   1.174 +			movingObj_start.setX( p.x() - selection->x() );	
   1.175 +			movingObj_start.setY( p.y() - selection->y() );	
   1.176 +			movingObj_orgPos.setX (lmo->x() );
   1.177 +			movingObj_orgPos.setY (lmo->y() );
   1.178 +
   1.179  			// If modMode==copy, then we want to "move" the _new_ object around
   1.180  			// then we need the offset from p to the _old_ selection, because of tmp
   1.181  			if (actionModModeCopy->isOn() &&
   1.182 @@ -3043,20 +3121,13 @@
   1.183  				if (typeid(*selection)==typeid(BranchObj) )
   1.184  				{
   1.185  					copyingObj=true;
   1.186 -					movingObj_start.setX( p.x() - selection->x() );	
   1.187 -					movingObj_start.setY( p.y() - selection->y() );	
   1.188  					mapCenter->addBranch ((BranchObj*)(selection));
   1.189  					unselect();
   1.190  					selection=mapCenter->getLastBranch();
   1.191  					selection->select();
   1.192  					mapCenter->reposition();
   1.193  				}
   1.194 -			}	else
   1.195 -			{
   1.196 -				movingObj_start.setX( p.x() - selection->x() );	
   1.197 -				movingObj_start.setY( p.y() - selection->y() );	
   1.198 -			}
   1.199 -			
   1.200 +			}	
   1.201  			movingObj=selection;	
   1.202  		} else
   1.203  			// Middle Button    Toggle Scroll
   1.204 @@ -3137,7 +3208,6 @@
   1.205  					// setLinkStyle calls updateLink, only set it once
   1.206  					if (fio->getLinkStyle()!=fio->getDefLinkStyle() ) 
   1.207  						fio->setLinkStyle (fio->getDefLinkStyle());
   1.208 -
   1.209  				}	
   1.210  				// TODO if (typeid(*selection) == typeid(FloatTextObj))
   1.211  			}
   1.212 @@ -3156,7 +3226,7 @@
   1.213  				{
   1.214  					// depth==1, mainbranch
   1.215  					setChanged();
   1.216 -					saveState(lmosel);
   1.217 +					saveState("move "+qpointToString(movingObj_orgPos));
   1.218  					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   1.219  				} else
   1.220  				{
   1.221 @@ -3299,50 +3369,34 @@
   1.222  
   1.223  
   1.224  			copyingObj=false;	
   1.225 -			if (!dst ) 
   1.226 +			if (dst ) 
   1.227  			{
   1.228 -				if (copyingObj)
   1.229 -				{
   1.230 -					// remove the current selection, if we have no destination
   1.231 -					selection->unselect();
   1.232 -					((BranchObj*)(selection->getParObj()))->removeBranch ((BranchObj*)(selection));
   1.233 -					if (selectionLast)
   1.234 -					{
   1.235 -						selection=selectionLast;
   1.236 -						selectionLast=NULL;
   1.237 -						selection->select();
   1.238 -					}
   1.239 -				}
   1.240 -			} else
   1.241 -			{
   1.242 +				BranchObj* bs=((BranchObj*)(selection));
   1.243  				setChanged();
   1.244 -				saveState();
   1.245 +				saveState ("linkBranchToPos (\""+ 
   1.246 +					(bs->getParObj())->getSelectString()+
   1.247 +					"\","+
   1.248 +					QString("%1").arg(bs->getNum())+
   1.249 +					","+
   1.250 +					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+
   1.251 +					")");
   1.252  				// TODO we also could check, if dest and src are on same branch,
   1.253  				// then it would be sufficient to saveState of this branch
   1.254  
   1.255  				// Modifiers allow to insert above/below dst
   1.256  				if (e->state() & QMouseEvent::ShiftButton)
   1.257  				{
   1.258 -						((BranchObj*)(selection))->moveBranchTo 
   1.259 -						(
   1.260 -							(BranchObj*)(dst),
   1.261 -							((BranchObj*)(dst))->getNum()
   1.262 -						);
   1.263 -					//if (selection) selection->select();
   1.264 +					bs->moveBranchTo ( (BranchObj*)(dst), ((BranchObj*)(dst))->getNum());
   1.265  				} else 
   1.266  				if (e->state() & QMouseEvent::ControlButton)
   1.267  				{
   1.268 -					((BranchObj*)(selection))->moveBranchTo 
   1.269 -					(
   1.270 -						(BranchObj*)(dst),
   1.271 -						((BranchObj*)(dst))->getNum()+1
   1.272 -					);
   1.273 -					//if (selection) selection->select();
   1.274 +					bs->moveBranchTo ( (BranchObj*)(dst), ((BranchObj*)(dst))->getNum()+1);
   1.275  				} else	
   1.276  				{
   1.277 -					((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),-1);
   1.278 +					bs->moveBranchTo ((BranchObj*)(dst),-1);
   1.279  					if (dst->getDepth()==0) 
   1.280 -						((BranchObj*)(selection))->move (savePos);
   1.281 +						bs->move (savePos);
   1.282 +						cout << "ME::release  start was: "<<movingObj_orgPos<<endl;
   1.283  				} 
   1.284  			}	
   1.285  			// Draw the original link, before selection was moved around
   1.286 @@ -3390,6 +3444,10 @@
   1.287  
   1.288  void MapEditor::contentsDragEnterEvent(QDragEnterEvent *event) 
   1.289  {
   1.290 +
   1.291 +//  for (unsigned int i=0;event->format(i);i++) // Debug mime type
   1.292 +//    cerr << event->format(i) << endl;
   1.293 +
   1.294    if (selection && 
   1.295        (typeid(*selection) == typeid(BranchObj)) || 
   1.296        (typeid(*selection) == typeid(MapCenterObj))) {
   1.297 @@ -3414,13 +3472,30 @@
   1.298      }
   1.299      
   1.300  	// If Uri are dragged from firefox 
   1.301 -    if (event->provides("text/x-moz-url") ){
   1.302 +    if (event->provides("_NETSCAPE_URL")){
   1.303        event->accept();
   1.304        return;
   1.305      }
   1.306 +
   1.307 +    // If QTextDrag can decode mime type
   1.308 +    if (QTextDrag::canDecode(event)) {
   1.309 +      event->accept();
   1.310 +      return;
   1.311 +    }
   1.312 +
   1.313    }
   1.314    event->ignore();
   1.315  }
   1.316 +
   1.317 +bool isUnicode16(const QByteArray &d) 
   1.318 +{
   1.319 +  // FIXME: make more precise check for unicode 16.
   1.320 +  // Guess unicode16 if any of second bytes are zero
   1.321 +  unsigned int length = max(0,d.size()-2)/2;
   1.322 +  for (unsigned int i = 0; i<length ; i++)
   1.323 +    if (d.at(i*2+1)==0) return true;
   1.324 +  return false;
   1.325 +}
   1.326        
   1.327  void MapEditor::contentsDropEvent(QDropEvent *event) 
   1.328  {
   1.329 @@ -3430,6 +3505,7 @@
   1.330  	{
   1.331  		bool update=false;
   1.332  		QStrList uris;
   1.333 +		QString heading;
   1.334  		if (event->provides("image/png")) 
   1.335  		{
   1.336  			QPixmap pix;
   1.337 @@ -3444,7 +3520,7 @@
   1.338  		} else if (event->provides("application/x-moz-file-promise-url") && 
   1.339  			 event->provides("application/x-moz-nativeimage")) 
   1.340  		{
   1.341 -			// Contains url to the img src in UTF-16
   1.342 +			// Contains url to the img src in unicode16
   1.343  			QByteArray d = event->encodedData("application/x-moz-file-promise-url");
   1.344  			QString url = QString((const QChar*)d.data(),d.size()/2);
   1.345  			fetchImage(url);
   1.346 @@ -3453,14 +3529,40 @@
   1.347  		} else if (event->provides ("text/uri-list"))
   1.348  		{	// Uris provided e.g. by konqueror
   1.349  			QUriDrag::decode (event,uris);
   1.350 -		} else if (event->provides ("text/x-moz-url-data"))
   1.351 +		} else if (event->provides ("_NETSCAPE_URL"))
   1.352  		{	// Uris provided by Mozilla
   1.353 -			QString str;
   1.354 -			QTextDrag::decode (event,str);
   1.355 -			uris.append(str);
   1.356 +		  QStringList l = QStringList::split("\n", event->encodedData("_NETSCAPE_URL"));
   1.357 +		  uris.append(l[0]);
   1.358 +		  heading = l[1];
   1.359 +		} else if (event->provides("text/html")) {
   1.360 +
   1.361 +		  // Handels text mime types
   1.362 +		  // Look like firefox allways handle text as unicode16 (2 bytes per char.)
   1.363 +		  QByteArray d = event->encodedData("text/html");
   1.364 +		  QString text;
   1.365 +		  if (isUnicode16(d)) 
   1.366 +		    text = QString((const QChar*)d.data(),d.size()/2);
   1.367 +		  else 
   1.368 +		    text = QString(d);
   1.369 +
   1.370 +		  textEditor->setText(text);
   1.371 +
   1.372 +		  event->accept();
   1.373 +		  update=true;
   1.374 +		} else if (event->provides("text/plain")) {
   1.375 +		  QByteArray d = event->encodedData("text/plain");
   1.376 +		  QString text;
   1.377 +		  if (isUnicode16(d))
   1.378 +		    text = QString((const QChar*)d.data(),d.size()/2);
   1.379 +		  else 
   1.380 +		    text = QString(d);
   1.381 +
   1.382 +		  textEditor->setText(text);
   1.383 +		  
   1.384 +		  event->accept();
   1.385 +		  update= true;
   1.386  		}
   1.387  
   1.388 -		
   1.389  		if (uris.count()>0)
   1.390  		{
   1.391  			QStringList files;
   1.392 @@ -3473,12 +3575,23 @@
   1.393  				if (bo)
   1.394  				{
   1.395  					s=QUriDrag::uriToLocalFile(u);
   1.396 -					if (s) 
   1.397 -						files.append(QDir::convertSeparators(s));
   1.398 -					else
   1.399 -						urls.append (u);
   1.400 -					bo->setHeading(u);
   1.401 -					bo->setURL (u);
   1.402 +					if (s) {
   1.403 +                       QString file = QDir::convertSeparators(s);
   1.404 +                       heading = QFileInfo(file).baseName();
   1.405 +                       files.append(file);
   1.406 +                       if (file.endsWith(".vym", false))
   1.407 +                           bo->setVymLink(file);
   1.408 +                       else
   1.409 +                           bo->setURL(u);
   1.410 +                   } else {
   1.411 +                       urls.append (u);
   1.412 +                       bo->setURL(u);
   1.413 +                   }
   1.414 +
   1.415 +                   if (heading)
   1.416 +                       bo->setHeading(heading);
   1.417 +                   else
   1.418 +                       bo->setHeading(u);
   1.419  				}
   1.420  			}
   1.421  			update=true;
   1.422 @@ -3486,6 +3599,7 @@
   1.423  
   1.424  		if (update) 
   1.425  		{
   1.426 +			setChanged();
   1.427  			mapCenter->reposition();
   1.428  			adjustCanvasSize();
   1.429  			canvas()->update();
   1.430 @@ -3515,8 +3629,7 @@
   1.431  }
   1.432  
   1.433  
   1.434 -void 
   1.435 -MapEditor::imageDataFetched(const QByteArray &a, QNetworkOperation */*nop*/) 
   1.436 +void MapEditor::imageDataFetched(const QByteArray &a, QNetworkOperation */*nop*/) 
   1.437  {
   1.438    if (!imageBuffer) imageBuffer = new QBuffer();
   1.439    if (!imageBuffer->isOpen()) {
   1.440 @@ -3526,21 +3639,24 @@
   1.441  }
   1.442  
   1.443  
   1.444 -void
   1.445 -MapEditor::imageDataFinished(QNetworkOperation *nop) 
   1.446 +void MapEditor::imageDataFinished(QNetworkOperation *nop) 
   1.447  {
   1.448 -  imageBuffer->close();
   1.449 -  if (nop->state()==QNetworkProtocol::StDone) {
   1.450 -    QPixmap img(imageBuffer->buffer());
   1.451 -    addFloatImage(img);
   1.452 -  }
   1.453 -
   1.454 -  delete imageBuffer;
   1.455 -  imageBuffer = 0;
   1.456 +	if (nop->state()==QNetworkProtocol::StDone) {
   1.457 +		QPixmap img(imageBuffer->buffer());
   1.458 +		addFloatImage(img);
   1.459 +	}
   1.460 +
   1.461 +	if (imageBuffer) {
   1.462 +		imageBuffer->close();
   1.463 +		if (imageBuffer) {
   1.464 +			imageBuffer->close();
   1.465 +			delete imageBuffer;
   1.466 +			imageBuffer = 0;
   1.467 +		}
   1.468 +	}
   1.469  }
   1.470  
   1.471 -void
   1.472 -MapEditor::fetchImage(const QString &url) 
   1.473 +void MapEditor::fetchImage(const QString &url) 
   1.474  {
   1.475    if (urlOperator) {
   1.476      urlOperator->stop();