00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "qgsapplication.h"
00018 #include "qgsmaplayerregistry.h"
00019 #include "qgsproviderregistry.h"
00020 #include "qgsexception.h"
00021
00022 #include <QDir>
00023 #include <QMessageBox>
00024 #include <QPalette>
00025 #include <QSettings>
00026
00027 #ifndef Q_WS_WIN
00028 #include <netinet/in.h>
00029 #else
00030 #include <winsock.h>
00031 #endif
00032
00033 #include "qgsconfig.h"
00034
00035 #include <ogr_api.h>
00036
00037 QString QgsApplication::mPrefixPath;
00038 QString QgsApplication::mPluginPath;
00039 QString QgsApplication::mPkgDataPath;
00040 QString QgsApplication::mThemeName;
00041 QStringList QgsApplication::mDefaultSvgPaths;
00042 QString QgsApplication::mConfigPath = QDir::homePath() + QString( "/.qgis/" );
00043
00057 QgsApplication::QgsApplication( int & argc, char ** argv, bool GUIenabled, QString customConfigPath )
00058 : QApplication( argc, argv, GUIenabled )
00059 {
00060 #if defined(Q_WS_MACX) || defined(Q_WS_WIN32) || defined(WIN32)
00061 setPrefixPath( applicationDirPath(), true );
00062 #else
00063 QDir myDir( applicationDirPath() );
00064 myDir.cdUp();
00065 QString myPrefix = myDir.absolutePath();
00066 setPrefixPath( myPrefix, true );
00067 #endif
00068
00069 if ( !customConfigPath.isEmpty() )
00070 {
00071 mConfigPath = customConfigPath + "/";
00072 }
00073
00074 mDefaultSvgPaths << mPkgDataPath + QString( "/svg/" );
00075 mDefaultSvgPaths << qgisSettingsDirPath() + QString( "svg/" );
00076 }
00077
00078 QgsApplication::~QgsApplication()
00079 {
00080 }
00081
00082 bool QgsApplication::notify( QObject * receiver, QEvent * event )
00083 {
00084
00085 bool done = true;
00086 try
00087 {
00088 done = QApplication::notify( receiver, event );
00089 }
00090 catch ( QgsException & e )
00091 {
00092 QMessageBox::critical( activeWindow(), tr( "Exception" ), e.what() );
00093 }
00094 catch ( std::exception & e )
00095 {
00096 QMessageBox::critical( activeWindow(), tr( "Exception" ), e.what() );
00097 }
00098 catch ( ... )
00099 {
00100 QMessageBox::critical( activeWindow(), tr( "Exception" ), tr( "unknown exception" ) );
00101 }
00102 return done;
00103 }
00104
00105 void QgsApplication::setPrefixPath( const QString thePrefixPath, bool useDefaultPaths )
00106 {
00107 mPrefixPath = thePrefixPath;
00108 #if defined(_MSC_VER)
00109 if ( mPrefixPath.endsWith( "/bin" ) )
00110 {
00111 mPrefixPath.chop( 4 );
00112 }
00113 #endif
00114 if ( useDefaultPaths )
00115 {
00116 setPluginPath( mPrefixPath + "/" + QString( QGIS_PLUGIN_SUBDIR ) );
00117 setPkgDataPath( mPrefixPath + "/" + QString( QGIS_DATA_SUBDIR ) );
00118 }
00119 }
00120
00121 void QgsApplication::setPluginPath( const QString thePluginPath )
00122 {
00123 mPluginPath = thePluginPath;
00124 }
00125
00126 void QgsApplication::setPkgDataPath( const QString thePkgDataPath )
00127 {
00128 mPkgDataPath = thePkgDataPath;
00129 mDefaultSvgPaths << mPkgDataPath + QString( "/svg/" );
00130 }
00131
00132 void QgsApplication::setDefaultSvgPaths( const QStringList& pathList )
00133 {
00134 mDefaultSvgPaths = pathList;
00135 }
00136
00137 const QString QgsApplication::prefixPath()
00138 {
00139 return mPrefixPath;
00140 }
00141 const QString QgsApplication::pluginPath()
00142 {
00143 return mPluginPath;
00144 }
00145 const QString QgsApplication::pkgDataPath()
00146 {
00147 return mPkgDataPath;
00148 }
00149 const QString QgsApplication::defaultThemePath()
00150 {
00151 return ":/images/themes/default/";
00152 }
00153 const QString QgsApplication::activeThemePath()
00154 {
00155 return ":/images/themes/" + mThemeName + "/";
00156 }
00157
00158
00159 QString QgsApplication::iconPath( QString iconFile )
00160 {
00161
00162 QString path = activeThemePath();
00163 if ( QFile::exists( path + iconFile ) )
00164 return path + iconFile;
00165
00166
00167 return defaultThemePath() + iconFile;
00168 }
00169
00173 void QgsApplication::setThemeName( const QString theThemeName )
00174 {
00175 QString myPath = ":/images/themes/" + theThemeName + "/";
00176
00177 if ( QFile::exists( myPath ) )
00178 {
00179 mThemeName = theThemeName;
00180 }
00181 else
00182 {
00183 mThemeName = "default";
00184 }
00185 }
00189 const QString QgsApplication::themeName()
00190 {
00191 return mThemeName;
00192 }
00196 const QString QgsApplication::authorsFilePath()
00197 {
00198 return mPkgDataPath + QString( "/doc/AUTHORS" );
00199 }
00203 const QString QgsApplication::contributorsFilePath()
00204 {
00205 return mPkgDataPath + QString( "/doc/CONTRIBUTORS" );
00206 }
00210 const QString QgsApplication::sponsorsFilePath()
00211 {
00212 return mPkgDataPath + QString( "/doc/SPONSORS" );
00213 }
00214
00218 const QString QgsApplication::donorsFilePath()
00219 {
00220 return mPkgDataPath + QString( "/doc/DONORS" );
00221 }
00222
00227 const QString QgsApplication::translatorsFilePath()
00228 {
00229 return mPkgDataPath + QString( "/doc/TRANSLATORS" );
00230 }
00234 const QString QgsApplication::developerPath()
00235 {
00236 return mPkgDataPath + QString( "/images/developers/" );
00237 }
00238
00242 const QString QgsApplication::helpAppPath()
00243 {
00244 QString helpAppPath = applicationDirPath();
00245 #ifdef Q_OS_MACX
00246 helpAppPath += "/bin/qgis_help.app/Contents/MacOS";
00247 #endif
00248 helpAppPath += "/qgis_help";
00249 return helpAppPath;
00250 }
00254 const QString QgsApplication::msexportAppPath()
00255 {
00256 QString msexportAppPath = applicationDirPath();
00257 #ifdef Q_OS_MACX
00258 msexportAppPath += "/bin/msexport.app/Contents/MacOS";
00259 #endif
00260 msexportAppPath += "/msexport";
00261 return msexportAppPath;
00262 }
00263
00267 const QString QgsApplication::i18nPath()
00268 {
00269 return mPkgDataPath + QString( "/i18n/" );
00270 }
00271
00275 const QString QgsApplication::qgisMasterDbFilePath()
00276 {
00277 return mPkgDataPath + QString( "/resources/qgis.db" );
00278 }
00279
00283 const QString QgsApplication::qgisSpatialiteDbTemplatePath()
00284 {
00285 return mPkgDataPath + QString( "/resources/spatialite.db" );
00286 }
00287
00291 const QString QgsApplication::qgisSettingsDirPath()
00292 {
00293 return mConfigPath;
00294 }
00295
00299 const QString QgsApplication::qgisUserDbFilePath()
00300 {
00301 return qgisSettingsDirPath() + QString( "qgis.db" );
00302 }
00303
00307 const QString QgsApplication::splashPath()
00308 {
00309 return mPkgDataPath + QString( "/images/splash/" );
00310 }
00311
00315 const QString QgsApplication::iconsPath()
00316 {
00317 return mPkgDataPath + QString( "/images/icons/" );
00318 }
00322 const QString QgsApplication::srsDbFilePath()
00323 {
00324 return mPkgDataPath + QString( "/resources/srs.db" );
00325 }
00326
00330 const QStringList QgsApplication::svgPaths()
00331 {
00332
00333
00334 QSettings settings;
00335 QStringList myPathList;
00336 QString myPaths = settings.value( "svg/searchPathsForSVG", "" ).toString();
00337 if ( !myPaths.isEmpty() )
00338 {
00339 myPathList = myPaths.split( "|" );
00340 }
00341
00342 myPathList << mDefaultSvgPaths;
00343 return myPathList;
00344 }
00345
00349 const QString QgsApplication::svgPath()
00350 {
00351 return mPkgDataPath + QString( "/svg/" );
00352 }
00353
00354 const QString QgsApplication::userStyleV2Path()
00355 {
00356 return qgisSettingsDirPath() + QString( "symbology-ng-style.xml" );
00357 }
00358
00359 const QString QgsApplication::defaultStyleV2Path()
00360 {
00361 return mPkgDataPath + QString( "/resources/symbology-ng-style.xml" );
00362 }
00363
00364 QgsApplication::endian_t QgsApplication::endian()
00365 {
00366 return ( htonl( 1 ) == 1 ) ? XDR : NDR ;
00367 }
00368
00369 void QgsApplication::initQgis()
00370 {
00371
00372 QgsProviderRegistry::instance( pluginPath() );
00373
00374
00375 QgsMapLayerRegistry::instance();
00376 }
00377
00378 void QgsApplication::exitQgis()
00379 {
00380 delete QgsMapLayerRegistry::instance();
00381 delete QgsProviderRegistry::instance();
00382 }
00383
00384 QString QgsApplication::showSettings()
00385 {
00386 QString myState = QString( "Application state:\n"
00387 "Prefix : %1\n"
00388 "Plugin Path : %2\n"
00389 "Package Data Path : %3\n"
00390 "Active Theme Name : %4\n"
00391 "Active Theme Path : %5\n"
00392 "Default Theme Path : %6\n"
00393 "SVG Search Paths : %7\n"
00394 "User DB Path : %8\n" )
00395 .arg( mPrefixPath )
00396 .arg( mPluginPath )
00397 .arg( mPkgDataPath )
00398 .arg( themeName() )
00399 .arg( activeThemePath() )
00400 .arg( defaultThemePath() )
00401 .arg( svgPaths().join( "\n" ) )
00402 .arg( qgisMasterDbFilePath() );
00403 return myState;
00404 }
00405
00406 QString QgsApplication::reportStyleSheet()
00407 {
00408
00409
00410
00411
00412 QColor myColor1 = palette().highlight().color();
00413 QColor myColor2 = myColor1;
00414 myColor2 = myColor2.lighter( 110 );
00415 QString myStyle;
00416 myStyle = ".glossy{ background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
00417 "stop: 0 " + myColor1.name() + ","
00418 "stop: 0.1 " + myColor2.name() + ","
00419 "stop: 0.5 " + myColor1.name() + ","
00420 "stop: 0.9 " + myColor2.name() + ","
00421 "stop: 1 " + myColor1.name() + ");"
00422 "color: white;"
00423 "padding-left: 4px;"
00424 "padding-top: 20px;"
00425 "padding-bottom: 8px;"
00426 "border: 1px solid #6c6c6c;"
00427 "}"
00428 ".overview{ font: 1.82em; font-weight: bold;}"
00429 "body{ background: white;"
00430 " color: black;"
00431 " font-family: arial,sans-serif;"
00432 "}"
00433 "h2{ background-color: #F6F6F6;"
00434 " color: #8FB171; "
00435 " font-size: medium; "
00436 " font-weight: normal;"
00437 " font-family: luxi serif, georgia, times new roman, times, serif;"
00438 " background: none;"
00439 " padding: 0.75em 0 0;"
00440 " margin: 0;"
00441 " line-height: 1.1em;"
00442 "}"
00443 "h3{ background-color: #F6F6F6;"
00444 " color: #729FCF;"
00445 " font-family: luxi serif, georgia, times new roman, times, serif;"
00446 " font-weight: bold;"
00447 " font-size: large;"
00448 " text-align: right;"
00449 " border-bottom: 5px solid #DCEB5C;"
00450 "}"
00451 "h4{ background-color: #F6F6F6;"
00452 " color: #729FCF;"
00453 " font-family: luxi serif, georgia, times new roman, times, serif;"
00454 " font-weight: bold;"
00455 " font-size: medium;"
00456 " text-align: right;"
00457 "}"
00458 "h5{ background-color: #F6F6F6;"
00459 " color: #729FCF;"
00460 " font-family: luxi serif, georgia, times new roman, times, serif;"
00461 " font-weight: bold;"
00462 " font-size: small;"
00463 " text-align: right;"
00464 "}"
00465 "a{ color: #729FCF;"
00466 " font-family: arial,sans-serif;"
00467 " font-size: small;"
00468 "}"
00469 "label{ background-color: #FFFFCC;"
00470 " border: 1px solid black;"
00471 " margin: 1px;"
00472 " padding: 0px 3px; "
00473 " font-size: small;"
00474 "}";
00475 return myStyle;
00476 }
00477
00478 void QgsApplication::registerOgrDrivers()
00479 {
00480 if ( 0 >= OGRGetDriverCount() )
00481 {
00482 OGRRegisterAll();
00483 }
00484 }
00485
00486 QString QgsApplication::absolutePathToRelativePath( QString aPath, QString targetPath )
00487 {
00488 #if defined( Q_OS_WIN )
00489 const Qt::CaseSensitivity cs = Qt::CaseInsensitive;
00490
00491 aPath.replace( "\\", "/" );
00492 if ( aPath.startsWith( "//" ) )
00493 {
00494
00495 aPath = "\\\\" + aPath.mid( 2 );
00496 }
00497
00498 targetPath.replace( "\\", "/" );
00499 if ( targetPath.startsWith( "//" ) )
00500 {
00501
00502 targetPath = "\\\\" + targetPath.mid( 2 );
00503 }
00504 #else
00505 const Qt::CaseSensitivity cs = Qt::CaseSensitive;
00506 #endif
00507
00508 QStringList targetElems = targetPath.split( "/", QString::SkipEmptyParts );
00509 QStringList aPathElems = aPath.split( "/", QString::SkipEmptyParts );
00510
00511 targetElems.removeAll( "." );
00512 aPathElems.removeAll( "." );
00513
00514
00515 int n = 0;
00516 while ( aPathElems.size() > 0 &&
00517 targetElems.size() > 0 &&
00518 aPathElems[0].compare( targetElems[0], cs ) == 0 )
00519 {
00520 aPathElems.removeFirst();
00521 targetElems.removeFirst();
00522 n++;
00523 }
00524
00525 if ( n == 0 )
00526 {
00527
00528 return aPath;
00529 }
00530
00531 if ( targetElems.size() > 0 )
00532 {
00533
00534 for ( int i = 0; i < targetElems.size(); i++ )
00535 {
00536 aPathElems.insert( 0, ".." );
00537 }
00538 }
00539 else
00540 {
00541
00542
00543 aPathElems.insert( 0, "." );
00544 }
00545
00546 return aPathElems.join( "/" );
00547 }
00548
00549 QString QgsApplication::relativePathToAbsolutePath( QString rpath, QString targetPath )
00550 {
00551
00552 if ( !rpath.startsWith( "./" ) && !rpath.startsWith( "../" ) )
00553 {
00554 return rpath;
00555 }
00556
00557 #if defined(Q_OS_WIN)
00558 rpath.replace( "\\", "/" );
00559 targetPath.replace( "\\", "/" );
00560
00561 bool uncPath = targetPath.startsWith( "//" );
00562 #endif
00563
00564 QStringList srcElems = rpath.split( "/", QString::SkipEmptyParts );
00565 QStringList targetElems = targetPath.split( "/", QString::SkipEmptyParts );
00566
00567 #if defined(Q_OS_WIN)
00568 if ( uncPath )
00569 {
00570 targetElems.insert( 0, "" );
00571 targetElems.insert( 0, "" );
00572 }
00573 #endif
00574
00575
00576 targetElems << srcElems;
00577 targetElems.removeAll( "." );
00578
00579
00580 int pos;
00581 while (( pos = targetElems.indexOf( ".." ) ) > 0 )
00582 {
00583
00584 targetElems.removeAt( pos - 1 );
00585 targetElems.removeAt( pos - 1 );
00586 }
00587
00588 #if !defined(Q_OS_WIN)
00589
00590 targetElems.prepend( "" );
00591 #endif
00592
00593 return targetElems.join( "/" );
00594 }