-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
main.cpp
762 lines (668 loc) · 25.5 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/***************************************************************************
main.cpp - description
-------------------
begin : Fri Jun 21 10:48:28 AKDT 2002
copyright : (C) 2002 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
//qt includes
#include <QBitmap>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFont>
#include <QMessageBox>
#include <QPixmap>
#include <QLocale>
#include <QSettings>
#include <QSplashScreen>
#include <QString>
#include <QStringList>
#include <QStyle>
#include <QPlastiqueStyle>
#include <QTranslator>
#include <QImageReader>
#include "qgscustomization.h"
#include "qgspluginregistry.h"
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
// Open files in binary mode
#include <fcntl.h> /* _O_BINARY */
#ifdef MSVC
#undef _fmode
int _fmode = _O_BINARY;
#else
// Only do this if we are not building on windows with msvc.
// Recommended method for doing this with msvc is with a call to _set_fmode
// which is the first thing we do in main().
// Similarly, with MinGW set _fmode in main().
#endif //_MSC_VER
#else
#include <getopt.h>
#endif
#ifdef Q_OS_MACX
#include <ApplicationServices/ApplicationServices.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1050
typedef SInt32 SRefCon;
#endif
#endif
#include "qgisapp.h"
#include "qgsmapcanvas.h"
#include "qgsapplication.h"
#include <qgsconfig.h>
#include <qgscustomization.h>
#include <qgsversion.h>
#include "qgsexception.h"
#include "qgsproject.h"
#include "qgsrectangle.h"
#include "qgslogger.h"
// (if Windows/Mac, use icon from resource)
#if ! defined(Q_WS_WIN) && ! defined(Q_WS_MAC)
#include "../../images/themes/default/qgis.xpm" // Linux
#include <QIcon>
#endif
/** print usage text
*/
void usage( std::string const & appName )
{
std::cerr << "Quantum GIS - " << VERSION << " '" << RELEASE_NAME << "' ("
<< QGSVERSION << ")\n"
<< "Quantum GIS (QGIS) is a viewer for spatial data sets, including\n"
<< "raster and vector data.\n"
<< "Usage: " << appName << " [options] [FILES]\n"
<< " options:\n"
<< "\t[--snapshot filename]\temit snapshot of loaded datasets to given file\n"
<< "\t[--width width]\twidth of snapshot to emit\n"
<< "\t[--height height]\theight of snapshot to emit\n"
<< "\t[--lang language]\tuse language for interface text\n"
<< "\t[--project projectfile]\tload the given QGIS project\n"
<< "\t[--extent xmin,ymin,xmax,ymax]\tset initial map extent\n"
<< "\t[--nologo]\thide splash screen\n"
<< "\t[--noplugins]\tdon't restore plugins on startup\n"
<< "\t[--nocustomization]\tdon't apply GUI customization\n"
<< "\t[--optionspath path]\tuse the given QSettings path\n"
<< "\t[--configpath path]\tuse the given path for all user configuration\n"
<< "\t[--help]\t\tthis text\n\n"
<< " FILES:\n"
<< " Files specified on the command line can include rasters,\n"
<< " vectors, and QGIS project files (.qgs): \n"
<< " 1. Rasters - Supported formats include GeoTiff, DEM \n"
<< " and others supported by GDAL\n"
<< " 2. Vectors - Supported formats include ESRI Shapefiles\n"
<< " and others supported by OGR and PostgreSQL layers using\n"
<< " the PostGIS extension\n" ; // OK
} // usage()
/////////////////////////////////////////////////////////////////
// Command line options 'behaviour' flag setup
////////////////////////////////////////////////////////////////
// These two are global so that they can be set by the OpenDocuments
// AppleEvent handler as well as by the main routine argv processing
// This behaviour will cause QGIS to autoload a project
static QString myProjectFileName = "";
// This is the 'leftover' arguments collection
static QStringList myFileList;
/* Test to determine if this program was started on Mac OS X by double-clicking
* the application bundle rather then from a command line. If clicked, argv[1]
* contains a process serial number in the form -psn_0_1234567. Don't process
* the command line arguments in this case because argv[1] confuses the processing.
*/
bool bundleclicked( int argc, char *argv[] )
{
return ( argc > 1 && memcmp( argv[1], "-psn_", 5 ) == 0 );
}
/*
* Hook into the qWarning/qFatal mechanism so that we can channel messages
* from libpng to the user.
*
* Some JPL WMS images tend to overload the libpng 1.2.2 implementation
* somehow (especially when zoomed in)
* and it would be useful for the user to know why their picture turned up blank
*
* Based on qInstallMsgHandler example code in the Qt documentation.
*
*/
void myMessageOutput( QtMsgType type, const char *msg )
{
switch ( type )
{
case QtDebugMsg:
fprintf( stderr, "Debug: %s\n", msg );
break;
case QtCriticalMsg:
fprintf( stderr, "Critical: %s\n", msg );
break;
case QtWarningMsg:
fprintf( stderr, "Warning: %s\n", msg );
// TODO: Verify this code in action.
if ( 0 == strncmp( msg, "libpng error:", 13 ) )
{
// Let the user know
QMessageBox::warning( 0, "libpng Error", msg );
}
break;
case QtFatalMsg:
fprintf( stderr, "Fatal: %s\n", msg );
abort(); // deliberately core dump
}
}
int main( int argc, char *argv[] )
{
QgsDebugMsg( QString( "Starting qgis main" ) );
#ifdef WIN32 // Windows
#ifdef _MSC_VER
_set_fmode( _O_BINARY );
#else //MinGW
_fmode = _O_BINARY;
#endif // _MSC_VER
#endif // WIN32
#if !defined(ANDROID) && !defined(_MSC_VER)
// Set up the custom qWarning/qDebug custom handler
qInstallMsgHandler( myMessageOutput );
#endif
/////////////////////////////////////////////////////////////////
// Command line options 'behaviour' flag setup
////////////////////////////////////////////////////////////////
//
// Parse the command line arguments, looking to see if the user has asked for any
// special behaviours. Any remaining non command arguments will be kept aside to
// be passed as a list of layers and / or a project that should be loaded.
//
// This behaviour is used to load the app, snapshot the map,
// save the image to disk and then exit
QString mySnapshotFileName = "";
int mySnapshotWidth = 800;
int mySnapshotHeight = 600;
bool myHideSplash = false;
bool myRestorePlugins = true;
bool myCustomization = true;
// This behaviour will set initial extent of map canvas, but only if
// there are no command line arguments. This gives a usable map
// extent when qgis starts with no layers loaded. When layers are
// loaded, we let the layers define the initial extent.
QString myInitialExtent = "";
if ( argc == 1 )
myInitialExtent = "-1,-1,1,1";
// This behaviour will allow you to force the use of a translation file
// which is useful for testing
QString myTranslationCode;
// The user can specify a path which will override the default path of custom
// user settings (~/.qgis) and it will be used for QSettings INI file
QString configpath;
#if defined(ANDROID)
QgsDebugMsg( QString( "Android: All params stripped" ) );// Param %1" ).arg( argv[0] ) );
#elif defined(Q_WS_WIN)
for ( int i = 1; i < argc; i++ )
{
QString arg = argv[i];
if ( arg == "--help" || arg == "-?" )
{
usage( argv[0] );
return 2;
}
else if ( arg == "-nologo" || arg == "-n" )
{
myHideSplash = true;
}
else if ( arg == "--noplugins" || arg == "-P" )
{
myRestorePlugins = false;
}
else if ( arg == "--nocustomization" || arg == "-C" )
{
myCustomization = false;
}
else if ( i + 1 < argc && ( arg == "--snapshot" || arg == "-s" ) )
{
mySnapshotFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[++i] ) ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == "--width" || arg == "-w" ) )
{
mySnapshotWidth = QString( argv[++i] ).toInt();
}
else if ( i + 1 < argc && ( arg == "--height" || arg == "-h" ) )
{
mySnapshotHeight = QString( argv[++i] ).toInt();
}
else if ( i + 1 < argc && ( arg == "--lang" || arg == "-l" ) )
{
myTranslationCode = argv[++i];
}
else if ( i + 1 < argc && ( arg == "--project" || arg == "-p" ) )
{
myProjectFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[++i] ) ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == "--extent" || arg == "-e" ) )
{
myInitialExtent = argv[++i];
}
else if ( i + 1 < argc && ( arg == "--optionspath" || arg == "-o" ) )
{
QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, argv[++i] );
}
else if ( i + 1 < argc && ( arg == "--configpath" || arg == "-c" ) )
{
configpath = argv[++i];
QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, configpath );
}
else
{
myFileList.append( QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[i] ) ).absoluteFilePath() ) );
}
}
#else
if ( !bundleclicked( argc, argv ) )
{
////////////////////////////////////////////////////////////////
// Use the GNU Getopts utility to parse cli arguments
// Invokes ctor `GetOpt (int argc, char **argv, char *optstring);'
///////////////////////////////////////////////////////////////
int optionChar;
while ( 1 )
{
static struct option long_options[] =
{
/* These options set a flag. */
{"help", no_argument, 0, '?'},
{"nologo", no_argument, 0, 'n'},
{"noplugins", no_argument, 0, 'P'},
{"nocustomization", no_argument, 0, 'C'},
/* These options don't set a flag.
* We distinguish them by their indices. */
{"snapshot", required_argument, 0, 's'},
{"width", required_argument, 0, 'w'},
{"height", required_argument, 0, 'h'},
{"lang", required_argument, 0, 'l'},
{"project", required_argument, 0, 'p'},
{"extent", required_argument, 0, 'e'},
{"optionspath", required_argument, 0, 'o'},
{"configpath", required_argument, 0, 'c'},
{"android", required_argument, 0, 'a'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
optionChar = getopt_long( argc, argv, "swhlpeoc",
long_options, &option_index );
QgsDebugMsg( QString( "Qgis main Debug" ) + optionChar );
/* Detect the end of the options. */
if ( optionChar == -1 )
break;
switch ( optionChar )
{
case 0:
/* If this option set a flag, do nothing else now. */
if ( long_options[option_index].flag != 0 )
break;
printf( "option %s", long_options[option_index].name );
if ( optarg )
printf( " with arg %s", optarg );
printf( "\n" );
break;
case 's':
mySnapshotFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( optarg ) ).absoluteFilePath() );
break;
case 'w':
mySnapshotWidth = QString( optarg ).toInt();
break;
case 'h':
mySnapshotHeight = QString( optarg ).toInt();
break;
case 'n':
myHideSplash = true;
break;
case 'l':
myTranslationCode = optarg;
break;
case 'p':
myProjectFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( optarg ) ).absoluteFilePath() );
break;
case 'P':
myRestorePlugins = false;
break;
case 'C':
myCustomization = false;
break;
case 'e':
myInitialExtent = optarg;
break;
case 'o':
QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, optarg );
break;
case 'c':
configpath = optarg;
break;
case '?':
usage( argv[0] );
return 2; // XXX need standard exit codes
break;
default:
QgsDebugMsg( QString( "%1: getopt returned character code %2" ).arg( argv[0] ).arg( optionChar ) );
return 1; // XXX need standard exit codes
}
}
// Add any remaining args to the file list - we will attempt to load them
// as layers in the map view further down....
QgsDebugMsg( QString( "Files specified on command line: %1" ).arg( optind ) );
if ( optind < argc )
{
while ( optind < argc )
{
#ifdef QGISDEBUG
int idx = optind;
QgsDebugMsg( QString( "%1: %2" ).arg( idx ).arg( argv[idx] ) );
#endif
myFileList.append( QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[optind++] ) ).absoluteFilePath() ) );
}
}
}
#endif
/////////////////////////////////////////////////////////////////////
// Now we have the handlers for the different behaviours...
////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// Initialise the application and the translation stuff
/////////////////////////////////////////////////////////////////////
#ifdef Q_WS_X11
bool myUseGuiFlag = getenv( "DISPLAY" ) != 0;
#else
bool myUseGuiFlag = true;
#endif
if ( !myUseGuiFlag )
{
std::cerr << QObject::tr(
"QGIS starting in non-interactive mode not supported.\n"
"You are seeing this message most likely because you "
"have no DISPLAY environment variable set.\n"
).toUtf8().constData();
exit( 1 ); //exit for now until a version of qgis is capabable of running non interactive
}
if ( !configpath.isEmpty() )
{
// tell QSettings to use INI format and save the file in custom config path
QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, configpath );
}
// GUI customization is enabled by default unless --nocustomization argument is used
QgsCustomization::instance()->setEnabled( myCustomization );
QgsApplication myApp( argc, argv, myUseGuiFlag, configpath );
// (if Windows/Mac, use icon from resource)
#if ! defined(Q_WS_WIN) && ! defined(Q_WS_MAC)
myApp.setWindowIcon( QPixmap( qgis_xpm ) ); // Linux
#endif
//
// Set up the QSettings environment must be done after qapp is created
QCoreApplication::setOrganizationName( "QuantumGIS" );
QCoreApplication::setOrganizationDomain( "qgis.org" );
QCoreApplication::setApplicationName( "QGIS" );
QCoreApplication::setAttribute( Qt::AA_DontShowIconsInMenus, false );
// Load and set possible default customization, must be done afterQgsApplication init and QSettings ( QCoreApplication ) init
QgsCustomization::instance()->loadDefault();
#ifdef Q_OS_MACX
// If the GDAL plugins are bundled with the application and GDAL_DRIVER_PATH
// is not already defined, use the GDAL plugins in the application bundle.
QString gdalPlugins( QCoreApplication::applicationDirPath().append( "/lib/gdalplugins" ) );
if ( QFile::exists( gdalPlugins ) && !getenv( "GDAL_DRIVER_PATH" ) )
{
setenv( "GDAL_DRIVER_PATH", gdalPlugins.toUtf8(), 1 );
}
#endif
QSettings mySettings;
// Set the application style. If it's not set QT will use the platform style except on Windows
// as it looks really ugly so we use QPlastiqueStyle.
QString style = mySettings.value( "/qgis/style" ).toString();
if ( !style.isNull() )
QApplication::setStyle( style );
#ifdef Q_WS_WIN
else
QApplication::setStyle( new QPlastiqueStyle );
#endif
/* Translation file for QGIS.
*/
QString i18nPath = QgsApplication::i18nPath();
QString myUserLocale = mySettings.value( "locale/userLocale", "" ).toString();
bool myLocaleOverrideFlag = mySettings.value( "locale/overrideFlag", false ).toBool();
QString myLocale;
//
// Priority of translation is:
// - command line
// - user secified in options dialog (with group checked on)
// - system locale
//
// When specifying from the command line it will change the user
// specified user locale
//
if ( !myTranslationCode.isNull() && !myTranslationCode.isEmpty() )
{
mySettings.setValue( "locale/userLocale", myTranslationCode );
}
else
{
if ( !myLocaleOverrideFlag || myUserLocale.isEmpty() )
{
myTranslationCode = QLocale::system().name();
//setting the locale/userLocale when the --lang= option is not set will allow third party
//plugins to always use the same locale as the QGIS, otherwise they can be out of sync
mySettings.setValue( "locale/userLocale", myTranslationCode );
}
else
{
myTranslationCode = myUserLocale;
}
}
#ifdef QGISDEBUG
// QgsDebugMsg(QString("Setting translation to %1/qgis_%2").arg(i18nPath).arg(myTranslationCode));
#endif
QTranslator qgistor( 0 );
if ( qgistor.load( QString( "qgis_" ) + myTranslationCode, i18nPath ) )
{
myApp.installTranslator( &qgistor );
}
else
{
qWarning( "loading of qgis translation failed [%s]", QString( "%1/qgis_%2" ).arg( i18nPath ).arg( myTranslationCode ).toLocal8Bit().constData() );
}
/* Translation file for Qt.
* The strings from the QMenuBar context section are used by Qt/Mac to shift
* the About, Preferences and Quit items to the Mac Application menu.
* These items must be translated identically in both qt_ and qgis_ files.
*/
QTranslator qttor( 0 );
if ( qttor.load( QString( "qt_" ) + myTranslationCode, QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
{
myApp.installTranslator( &qttor );
}
else
{
qWarning( "loading of qt translation failed [%s]", QString( "%1/qt_%2" ).arg( QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ).arg( myTranslationCode ).toLocal8Bit().constData() );
}
//set up splash screen
QString mySplashPath( QgsApplication::splashPath() );
QPixmap myPixmap( mySplashPath + QString( "splash.png" ) );
QSplashScreen *mypSplash = new QSplashScreen( myPixmap );
if ( mySettings.value( "/qgis/hideSplash" ).toBool() || myHideSplash )
{
//splash screen hidden
}
else
{
//for win and linux we can just automask and png transparency areas will be used
mypSplash->setMask( myPixmap.mask() );
mypSplash->show();
}
// For non static builds on mac and win (static builds are not supported)
// we need to be sure we can find the qt image
// plugins. In mac be sure to look in the
// application bundle...
#ifdef Q_WS_WIN
QCoreApplication::addLibraryPath( QApplication::applicationDirPath()
+ QDir::separator() + "qtplugins" );
#endif
#ifdef Q_OS_MACX
//qDebug("Adding qt image plugins to plugin search path...");
CFURLRef myBundleRef = CFBundleCopyBundleURL( CFBundleGetMainBundle() );
CFStringRef myMacPath = CFURLCopyFileSystemPath( myBundleRef, kCFURLPOSIXPathStyle );
const char *mypPathPtr = CFStringGetCStringPtr( myMacPath, CFStringGetSystemEncoding() );
CFRelease( myBundleRef );
CFRelease( myMacPath );
QString myPath( mypPathPtr );
// if we are not in a bundle assume that the app is built
// as a non bundle app and that image plugins will be
// in system Qt frameworks. If the app is a bundle
// lets try to set the qt plugin search path...
QFileInfo myInfo( myPath );
if ( myInfo.isBundle() )
{
// First clear the plugin search paths so we can be sure
// only plugins from the bundle are being used
QStringList myPathList;
QCoreApplication::setLibraryPaths( myPathList );
// Now set the paths inside the bundle
myPath += "/Contents/plugins";
QCoreApplication::addLibraryPath( myPath );
//next two lines should not be needed, testing only
//QCoreApplication::addLibraryPath( myPath + "/imageformats" );
//QCoreApplication::addLibraryPath( myPath + "/sqldrivers" );
//foreach (myPath, myApp.libraryPaths())
//{
//qDebug("Path:" + myPath.toLocal8Bit());
//}
//qDebug( "Added %s to plugin search path", qPrintable( myPath ) );
//QList<QByteArray> myFormats = QImageReader::supportedImageFormats();
//for ( int x = 0; x < myFormats.count(); ++x ) {
// qDebug("Format: " + myFormats[x]);
//}
}
#endif
QgisApp *qgis = new QgisApp( mypSplash, myRestorePlugins ); // "QgisApp" used to find canonical instance
qgis->setObjectName( "QgisApp" );
myApp.connect(
&myApp, SIGNAL( preNotify( QObject *, QEvent *, bool * ) ),
//qgis, SLOT( preNotify( QObject *, QEvent *))
QgsCustomization::instance(), SLOT( preNotify( QObject *, QEvent *, bool * ) )
);
/////////////////////////////////////////////////////////////////////
// If no --project was specified, parse the args to look for a //
// .qgs file and set myProjectFileName to it. This allows loading //
// of a project file by clicking on it in various desktop managers //
// where an appropriate mime-type has been set up. //
/////////////////////////////////////////////////////////////////////
if ( myProjectFileName.isEmpty() )
{
// check for a .qgs
for ( int i = 0; i < argc; i++ )
{
QString arg = QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[i] ) ).absoluteFilePath() );
if ( arg.contains( ".qgs" ) )
{
myProjectFileName = arg;
break;
}
}
}
/////////////////////////////////////////////////////////////////////
// Load a project file if one was specified
/////////////////////////////////////////////////////////////////////
if ( ! myProjectFileName.isEmpty() )
{
qgis->openProject( myProjectFileName );
}
/////////////////////////////////////////////////////////////////////
// autoload any file names that were passed in on the command line
/////////////////////////////////////////////////////////////////////
QgsDebugMsg( QString( "Number of files in myFileList: %1" ).arg( myFileList.count() ) );
for ( QStringList::Iterator myIterator = myFileList.begin(); myIterator != myFileList.end(); ++myIterator )
{
QgsDebugMsg( QString( "Trying to load file : %1" ).arg(( *myIterator ) ) );
QString myLayerName = *myIterator;
// don't load anything with a .qgs extension - these are project files
if ( !myLayerName.contains( ".qgs" ) )
{
qgis->openLayer( myLayerName );
}
}
/////////////////////////////////////////////////////////////////////
// Set initial extent if requested
/////////////////////////////////////////////////////////////////////
if ( ! myInitialExtent.isEmpty() )
{
double coords[4];
int pos, posOld = 0;
bool ok = true;
// XXX is it necessary to switch to "C" locale?
// parse values from string
// extent is defined by string "xmin,ymin,xmax,ymax"
for ( int i = 0; i < 3; i++ )
{
// find comma and get coordinate
pos = myInitialExtent.indexOf( ',', posOld );
if ( pos == -1 )
{
ok = false;
break;
}
coords[i] = QString( myInitialExtent.mid( posOld, pos - posOld ) ).toDouble( &ok );
if ( !ok )
break;
posOld = pos + 1;
}
// parse last coordinate
if ( ok )
coords[3] = QString( myInitialExtent.mid( posOld ) ).toDouble( &ok );
if ( !ok )
{
QgsDebugMsg( "Error while parsing initial extent!" );
}
else
{
// set extent from parsed values
QgsRectangle rect( coords[0], coords[1], coords[2], coords[3] );
qgis->setExtent( rect );
if ( qgis->mapCanvas() )
{
qgis->mapCanvas()->refresh();
}
}
}
/////////////////////////////////////////////////////////////////////
// Take a snapshot of the map view then exit if snapshot mode requested
/////////////////////////////////////////////////////////////////////
if ( mySnapshotFileName != "" )
{
/*You must have at least one paintEvent() delivered for the window to be
rendered properly.
It looks like you don't run the event loop in non-interactive mode, so the
event is never occuring.
To achieve this without running the event loop: show the window, then call
qApp->processEvents(), grab the pixmap, save it, hide the window and exit.
*/
//qgis->show();
myApp.processEvents();
QPixmap * myQPixmap = new QPixmap( mySnapshotWidth, mySnapshotHeight );
myQPixmap->fill();
qgis->saveMapAsImage( mySnapshotFileName, myQPixmap );
myApp.processEvents();
qgis->hide();
QgsPluginRegistry::instance()->unloadAll();
return 1;
}
/////////////////////////////////////////////////////////////////////
// Continue on to interactive gui...
/////////////////////////////////////////////////////////////////////
qgis->show();
myApp.connect( &myApp, SIGNAL( lastWindowClosed() ), &myApp, SLOT( quit() ) );
mypSplash->finish( qgis );
delete mypSplash;
qgis->completeInitialization();
int retval = myApp.exec();
delete qgis;
return retval;
}