Exemplo n.º 1
0
void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg )
{
  QDomImplementation DomImplementation;
  QDomDocumentType documentType = DomImplementation.createDocumentType( "qgis", "http://mrcc.com/qgis.dtd", "SYSTEM" );
  QDomDocument myDocument( documentType );

  QDomElement myRootNode = myDocument.createElement( "qgis" );
  myRootNode.setAttribute( "version", QString( "%1" ).arg( QGis::QGIS_VERSION ) );
  myDocument.appendChild( myRootNode );

  myRootNode.setAttribute( "hasScaleBasedVisibilityFlag", hasScaleBasedVisibility() ? 1 : 0 );
  myRootNode.setAttribute( "minimumScale", QString::number( minimumScale() ) );
  myRootNode.setAttribute( "maximumScale", QString::number( maximumScale() ) );

#if 0
  // <transparencyLevelInt>
  QDomElement transparencyLevelIntElement = myDocument.createElement( "transparencyLevelInt" );
  QDomText    transparencyLevelIntText    = myDocument.createTextNode( QString::number( getTransparency() ) );
  transparencyLevelIntElement.appendChild( transparencyLevelIntText );
  myRootNode.appendChild( transparencyLevelIntElement );
#endif

  if ( !writeSymbology( myRootNode, myDocument, errorMsg ) )
  {
    errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
    return;
  }
  doc = myDocument;
}
Exemplo n.º 2
0
bool CDlgUIList::InitDialogListFromXML(const char* szFilename)
{
	TiXmlDocument myDocument( strFilename );
	myDocument.LoadFile(TIXML_ENCODING_UTF8);
	if ( myDocument.Error() )
	{
		return false;
	}
	std::list<std::wstring> listString;
	//获得根元素,即root。
	TiXmlElement *pRootElement = myDocument.RootElement();
	//获得第一个dialog节点。
	TiXmlElement *pDialogElement = pRootElement->FirstChildElement("dialog");
	while ( pDialogElement )
	{
		if ( pDialogElement->Attribute("id") )
		{
			WCHAR strText[256]={0};
			MultiByteToWideChar(CP_UTF8,0,pDialogElement->Attribute("id"),-1,strText,256);
			listString.push_back(strText);
		}
		// 查找下一个dialog
		pDialogElement = pDialogElement->NextSiblingElement("dialog");
	}
	//
	listString.sort();
	for(std::list<std::wstring>::iterator it=listString.begin(); it!=listString.end(); it++)
	{
		m_ListBox.AddItem(*it);
	}
	return true;
}
Exemplo n.º 3
0
bool MadSVCrop::fromXml(const QString theXml)
{
    QDomDocument myDocument("mydocument");
    myDocument.setContent(theXml);
    QDomElement myTopElement = myDocument.firstChildElement("svcrop");
    if (myTopElement.isNull())
    {
        //TODO - just make this a warning
        qDebug("the top element couldn't be found!");
        setGuid(myTopElement.attribute("guid"));
        //QDomElement myCategory;
        //QDomElement myDetails;

        //myCategory=QString(myTopElement.firstChildElement("agrbiomass").text());
        //myDetails=QString(myCategory.firstChildElement("details").text());
        //mWeightOrgans.depth()=QString(myDetails.firstChildElement("weightorgans").text()).toFloat();

        //MadSubCategory mySVCropDetails;
        //mySVCropDetails = QString(myTopElement.firstChildElement("details").text.());

        //mName=MadUtils::xmlDecode(myTopElement.firstChildElement("name").text());
        //mDescription=MadUtils::xmlDecode(myTopElement.firstChildElement("description").text());
        //mImageFile=QString(myTopElement.firstChildElement("imageFile").text());
        return true;
    }
    else
    return false;
}
Exemplo n.º 4
0
bool wXMlParse::ParseConfing(const char * filename, wConfigs &configs)
{

	configs.clear();
	TiXmlDocument myDocument(filename);
    bool bRet = myDocument.LoadFile();

	if(bRet){
		TiXmlElement *RootElement = myDocument.RootElement();
		if(RootElement && strcmp(RootElement->Value(),"config") == 0){
			//char useConfigName[RUNTOOL_CONFIG_BUFF_SIZE] = "";
			TiXmlElement *node = RootElement->FirstChildElement();
			while(node){
				if(strcmp(node->Value(),"config") == 0){
					ParseButtonConfig(node,configs);
				}else if(strcmp(node->Value(),"useconfig") == 0){
					strcpy_s(configs.useConfigName,node->FirstChild()->Value());
				}
				node = node->NextSiblingElement();
			}
			return true;
		}
	}
	else
	{
		qDebug() << myDocument.ErrorDesc();
	}
	return false;
}
Exemplo n.º 5
0
QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &theResultFlag )
{
  QgsDebugMsg( QString( "uri = %1 myURI = %2" ).arg( theURI ).arg( publicSource() ) );

  theResultFlag = false;

  QDomDocument myDocument( "qgis" );

  // location of problem associated with errorMsg
  int line, column;
  QString myErrorMessage;

  QFile myFile( theURI );
  if ( myFile.open( QFile::ReadOnly ) )
  {
    // read file
    theResultFlag = myDocument.setContent( &myFile, &myErrorMessage, &line, &column );
    if ( !theResultFlag )
      myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
    myFile.close();
  }
  else
  {
    QFileInfo project( QgsProject::instance()->fileName() );
    QgsDebugMsg( QString( "project fileName: %1" ).arg( project.absoluteFilePath() ) );

    QString qml;
    if ( loadNamedStyleFromDb( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ), theURI, qml ) ||
         ( project.exists() && loadNamedStyleFromDb( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), theURI, qml ) ) ||
         loadNamedStyleFromDb( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( "resources/qgis.qmldb" ), theURI, qml ) )
    {
      theResultFlag = myDocument.setContent( qml, &myErrorMessage, &line, &column );
      if ( !theResultFlag )
      {
        myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
      }
    }
    else
    {
      myErrorMessage = tr( "style not found in database" );
    }
  }

  if ( !theResultFlag )
  {
    return myErrorMessage;
  }

  // get style file version string, if any
  QgsProjectVersion fileVersion( myDocument.firstChildElement( "qgis" ).attribute( "version" ) );
  QgsProjectVersion thisVersion( QGis::QGIS_VERSION );

  if ( thisVersion > fileVersion )
  {
    QgsLogger::warning( "Loading a style file that was saved with an older "
                        "version of qgis (saved in " + fileVersion.text() +
                        ", loaded in " + QGis::QGIS_VERSION +
                        "). Problems may occur." );

    QgsProjectFileTransform styleFile( myDocument, fileVersion );
    // styleFile.dump();
    styleFile.updateRevision( thisVersion );
    // styleFile.dump();
  }

  // now get the layer node out and pass it over to the layer
  // to deserialise...
  QDomElement myRoot = myDocument.firstChildElement( "qgis" );
  if ( myRoot.isNull() )
  {
    myErrorMessage = tr( "Error: qgis element could not be found in %1" ).arg( theURI );
    theResultFlag = false;
    return myErrorMessage;
  }

  // use scale dependent visibility flag
  toggleScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
  setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() );
  setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() );

#if 0
  //read transparency level
  QDomNode transparencyNode = myRoot.namedItem( "transparencyLevelInt" );
  if ( ! transparencyNode.isNull() )
  {
    // set transparency level only if it's in project
    // (otherwise it sets the layer transparent)
    QDomElement myElement = transparencyNode.toElement();
    setTransparency( myElement.text().toInt() );
  }
#endif

  QString errorMsg;
  theResultFlag = readSymbology( myRoot, errorMsg );
  if ( !theResultFlag )
  {
    myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( theURI ).arg( errorMsg );
    return myErrorMessage;
  }

  return "";
}
Exemplo n.º 6
0
bool CDlgUIList::createDialogStringFromXML(const char* szXMLFilename, const char* szStringFilename)
{
	TiXmlDocument myDocument( strXMLFilename );
	myDocument.LoadFile(TIXML_ENCODING_UTF8);
	if ( myDocument.Error() )
	{
		return false;
	}
	std::ofstream file;
	file.open(strStringFilename.c_str(), std::ios::out);

	//获得根元素,即root。
	TiXmlElement *pRootElement = myDocument.RootElement();
	//获得第一个dialog节点。
	TiXmlElement *pDialogElement = pRootElement->FirstChildElement("dialog");
	WCHAR wszText[256]={0};
	while ( pDialogElement )
	{
		if ( pDialogElement->Attribute("id") )
		{
			std::wstring wstrDialog;
			{
				MultiByteToWideChar(CP_UTF8,0,pDialogElement->Attribute("id"),-1,wszText,256);
				std::wstring wstrDialog = wszText;
				file<<"["<<ws2s(wstrDialog)<<"]"<<std::endl;
			}
			TiXmlElement *pCaptionElement = pDialogElement->FirstChildElement("caption");
			if ( pCaptionElement&&pCaptionElement->GetText() )
			{
				std::wstring wstrCaption;
				MultiByteToWideChar(CP_UTF8,0,pCaptionElement->GetText(),-1,wszText,256);
				wstrCaption = wszText;
				file<<"CAPTION="<<ws2s(wstrCaption)<<std::endl;
			}
			{
				TiXmlElement *pControlElement = pDialogElement->FirstChildElement("element");
				while (pControlElement)
				{
					std::wstring wstrControl,wstrText,wstrTip;
					if ( pControlElement->Attribute("id") )
					{
						MultiByteToWideChar(CP_UTF8,0,pControlElement->Attribute("id"),-1,wszText,256);
						wstrControl = wszText;

						//////////////////////////////////////////////////////////////////////////
						if ( pControlElement->GetText() )
						{
							MultiByteToWideChar(CP_UTF8,0,pControlElement->GetText(),-1,wszText,256);
							wstrText = wszText;
							file<<"TEXT_"<<ws2s(wstrControl)<<"="<<ws2s(wstrText)<<std::endl;
						}
					}
					pControlElement = pControlElement->NextSiblingElement("element");
				}
			}
			{
				TiXmlElement *pControlElement = pDialogElement->FirstChildElement("element");
				while (pControlElement)
				{
					std::wstring wstrControl,wstrText,wstrTip;
					if ( pControlElement->Attribute("id") )
					{
						MultiByteToWideChar(CP_UTF8,0,pControlElement->Attribute("id"),-1,wszText,256);
						wstrControl = wszText;

						//////////////////////////////////////////////////////////////////////////
						if ( pControlElement->Attribute("tip") )
						{
							MultiByteToWideChar(CP_UTF8,0,pControlElement->Attribute("tip"),-1,wszText,256);
							wstrTip = wszText;
							file<<"TIP_"<<ws2s(wstrControl)<<"="<<ws2s(wstrTip)<<std::endl;
						}
					}
					pControlElement = pControlElement->NextSiblingElement("element");
				}
			}
			file<<std::endl;
		}
		// 查找下一个dialog
		pDialogElement = pDialogElement->NextSiblingElement("dialog");
	}
	file.close();
	//
// 	listString.sort();
// 	for(std::list<std::wstring>::iterator it=listString.begin(); it!=listString.end(); it++)
// 	{
// 		m_ListBox.AddItem(*it);
// 	}
	return true;
}
Exemplo n.º 7
0
bool CDlgUIList::createDialogCodeFromXML(const char* szFilename,const char* szDialogName)
{
	TiXmlDocument myDocument( strFilename );
	myDocument.LoadFile(TIXML_ENCODING_UTF8);
	if ( myDocument.Error() )
	{
		return false;
	}
	std::list<std::wstring> listString;
	//获得根元素,即root。
	TiXmlElement *pRootElement = myDocument.RootElement();
	//获得第一个dialog节点。
	TiXmlElement *pDialogElement = pRootElement->FirstChildElement("dialog");
	while ( pDialogElement )
	{
		if ( pDialogElement->Attribute("id") )
		{
			WCHAR strText[256]={0};
			MultiByteToWideChar(CP_UTF8,0,pDialogElement->Attribute("id"),-1,strText,256);

			if (strDialogName==ws2s(strText))
			{
				{
					setlocale(LC_ALL,"Chinese-simplified");
					std::ofstream ofs("dialog_code.h");
					assert(ofs);
					ofs<<"class CDlg"<<id2name(strDialogName)<<": public CUIDialog"<<std::endl;
					ofs<<"{"<<std::endl;
					ofs<<"public:"<<std::endl;
					ofs<<"	virtual void OnControlRegister();"<<std::endl;
					ofs<<"private:"<<std::endl;

					ofs<<"	// Controls' event."<<std::endl;
					TiXmlElement *pControlElement = pDialogElement->FirstChildElement("element");
					while (pControlElement)
					{
						if (pControlElement->Attribute("id"))
						{
							std::string strID = pControlElement->Attribute("id");
							ofs<<"	void On"<<id2name(strID)<<"();"<<std::endl;
						}
						pControlElement = pControlElement->NextSiblingElement("element");
					}
					ofs<<"	// Controls' variable."<<std::endl;
					pControlElement = pDialogElement->FirstChildElement("element");
					while (pControlElement)
					{
						std::string strType = pControlElement->Attribute("type");
						if (pControlElement->Attribute("id"))
						{
							std::string strID = pControlElement->Attribute("id");
							ofs<<"	"<<type2ControlClassName(strType)<<"	m_"<<id2name(strID)<<";"<<std::endl;
						}
						pControlElement = pControlElement->NextSiblingElement("element");
					}
					ofs<<"}";
					ofs.close();
				}
				//////////////////////////////////////////////////////////////////////////
				{
					setlocale(LC_ALL,"Chinese-simplified");
					std::ofstream ofs("dialog_code.cpp");
					assert(ofs);
					ofs<<"#include \"Dlg"<<id2name(strDialogName)<<".h\""<<std::endl;
					ofs<<std::endl;
					ofs<<"void CDlg"<<id2name(strDialogName)<<"::OnControlRegister()"<<std::endl;
					ofs<<"{"<<std::endl;
					TiXmlElement *pControlElement = pDialogElement->FirstChildElement("element");
					while (pControlElement)
					{
						if (pControlElement->Attribute("id"))
						{
							std::string strID = pControlElement->Attribute("id");
							ofs<<"	RegisterControl(\""<<strID<<"\",	m_"<<id2name(strID)<<");"<<std::endl;
						}
						pControlElement = pControlElement->NextSiblingElement("element");
					}
					ofs<<std::endl;
					pControlElement = pDialogElement->FirstChildElement("element");
					while (pControlElement)
					{
						if (pControlElement->Attribute("id"))
						{
							std::string strID = pControlElement->Attribute("id");
							ofs<<"	RegisterControlEvent(\""<<strID<<"\",	(PEVENT)&CDlg"<<id2name(strDialogName)<<"::On"<<id2name(strID)<<");"<<std::endl;
						}
						pControlElement = pControlElement->NextSiblingElement("element");
					}
					ofs<<"}";
					pControlElement = pDialogElement->FirstChildElement("element");
					while (pControlElement)
					{
						if (pControlElement->Attribute("id"))
						{
							std::string strID = pControlElement->Attribute("id");
							ofs<<std::endl<<std::endl;
							ofs<<"void CDlg"<<id2name(strDialogName)<<"::On"<<id2name(strID)<<"()"<<std::endl;
							ofs<<"{"<<std::endl;
							ofs<<"	// Do it."<<std::endl;
							ofs<<"}";
						}
						pControlElement = pControlElement->NextSiblingElement("element");
					}
					ofs.close();
				}
				return true;
				break;
			}
		}
		// 查找下一个dialog
		pDialogElement = pDialogElement->NextSiblingElement("dialog");
	}
	return true;
}
Exemplo n.º 8
0
QString QgsMapLayer::saveNamedStyle( const QString theURI, bool & theResultFlag )
{
  QString myErrorMessage;

  QDomImplementation DomImplementation;
  QDomDocumentType documentType =
    DomImplementation.createDocumentType(
      "qgis", "http://mrcc.com/qgis.dtd", "SYSTEM" );
  QDomDocument myDocument( documentType );
  QDomElement myRootNode = myDocument.createElement( "qgis" );
  myRootNode.setAttribute( "version", QString( "%1" ).arg( QGis::QGIS_VERSION ) );
  myDocument.appendChild( myRootNode );

  // use scale dependent visibility flag
  myRootNode.setAttribute( "hasScaleBasedVisibilityFlag", hasScaleBasedVisibility() ? 1 : 0 );
  myRootNode.setAttribute( "minimumScale", minimumScale() );
  myRootNode.setAttribute( "maximumScale", maximumScale() );

  // <transparencyLevelInt>
  QDomElement transparencyLevelIntElement = myDocument.createElement( "transparencyLevelInt" );
  QDomText    transparencyLevelIntText    = myDocument.createTextNode( QString::number( getTransparency() ) );
  transparencyLevelIntElement.appendChild( transparencyLevelIntText );
  myRootNode.appendChild( transparencyLevelIntElement );
  // now append layer node to map layer node

  QString errorMsg;
  if ( !writeSymbology( myRootNode, myDocument, errorMsg ) )
  {
    return tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
  }

  // check if the uri is a file or ends with .qml,
  // which indicates that it should become one
  // everything else goes to the database
  QString filename;

  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
  if ( vlayer && vlayer->providerType() == "ogr" )
  {
    QStringList theURIParts = theURI.split( "|" );
    filename = theURIParts[0];
  }
  else if ( vlayer && vlayer->providerType() == "delimitedtext" )
  {
    filename = QUrl::fromEncoded( theURI.toAscii() ).toLocalFile();
  }
  else
  {
    filename = theURI;
  }

  QFileInfo myFileInfo( filename );
  if ( myFileInfo.exists() || filename.endsWith( ".qml", Qt::CaseInsensitive ) )
  {
    QFileInfo myDirInfo( myFileInfo.path() );  //excludes file name
    if ( !myDirInfo.isWritable() )
    {
      return tr( "The directory containing your dataset needs to be writable!" );
    }

    // now construct the file name for our .qml style file
    QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".qml";

    QFile myFile( myFileName );
    if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
    {
      QTextStream myFileStream( &myFile );
      // save as utf-8 with 2 spaces for indents
      myDocument.save( myFileStream, 2 );
      myFile.close();
      theResultFlag = true;
      return tr( "Created default style file as %1" ).arg( myFileName );
    }
    else
    {
      theResultFlag = false;
      return tr( "ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName );
    }
  }
  else
  {
    QString qml = myDocument.toString();

    // read from database
    sqlite3 *myDatabase;
    sqlite3_stmt *myPreparedStatement;
    const char *myTail;
    int myResult;

    myResult = sqlite3_open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ).toUtf8().data(), &myDatabase );
    if ( myResult != SQLITE_OK )
    {
      return tr( "User database could not be opened." );
    }

    QByteArray param0 = theURI.toUtf8();
    QByteArray param1 = qml.toUtf8();

    QString mySql = "create table if not exists tbl_styles(style varchar primary key,qml varchar)";
    myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
    if ( myResult == SQLITE_OK )
    {
      if ( sqlite3_step( myPreparedStatement ) != SQLITE_DONE )
      {
        sqlite3_finalize( myPreparedStatement );
        sqlite3_close( myDatabase );
        theResultFlag = false;
        return tr( "The style table could not be created." );
      }
    }

    sqlite3_finalize( myPreparedStatement );

    mySql = "insert into tbl_styles(style,qml) values (?,?)";
    myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
    if ( myResult == SQLITE_OK )
    {
      if ( sqlite3_bind_text( myPreparedStatement, 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
           sqlite3_bind_text( myPreparedStatement, 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
           sqlite3_step( myPreparedStatement ) == SQLITE_DONE )
      {
        theResultFlag = true;
        myErrorMessage = tr( "The style %1 was saved to database" ).arg( theURI );
      }
    }

    sqlite3_finalize( myPreparedStatement );

    if ( !theResultFlag )
    {
      QString mySql = "update tbl_styles set qml=? where style=?";
      myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
      if ( myResult == SQLITE_OK )
      {
        if ( sqlite3_bind_text( myPreparedStatement, 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
             sqlite3_bind_text( myPreparedStatement, 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
             sqlite3_step( myPreparedStatement ) == SQLITE_DONE )
        {
          theResultFlag = true;
          myErrorMessage = tr( "The style %1 was updated in the database." ).arg( theURI );
        }
        else
        {
          theResultFlag = false;
          myErrorMessage = tr( "The style %1 could not be updated in the database." ).arg( theURI );
        }
      }
      else
      {
        theResultFlag = false;
        myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( theURI );
      }

      sqlite3_finalize( myPreparedStatement );
    }

    sqlite3_close( myDatabase );
  }

  return myErrorMessage;
}