forked from wolfwind521/IndoorMapEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
110 lines (92 loc) · 3.96 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
#include "mainwindow.h"
#include <QApplication>
#include <QCommandLineParser>
#include "io/iomanager.h"
#include <iostream>
#include <QFileInfo>
#include <QDebug>
#include <QDir>
#include <QFileInfoList>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QCoreApplication::setApplicationName("IndoorMap Editor. ");
QCoreApplication::setApplicationVersion("1.14");
//解析命令行参数
QCommandLineParser parser;
parser.setApplicationDescription("author: GaiMeng");
parser.addHelpOption();
parser.addVersionOption();
// open and save options -o="../SomeDir" -o="somefile.json" -s=dir
QCommandLineOption openfileOption(QStringList() << "o" << "open",
QCoreApplication::translate("main", "open a file or all files under a directory"),
QCoreApplication::translate("main", "openpath"));
parser.addOption(openfileOption);
QCommandLineOption savefileOption(QStringList() << "s" << "save",
QCoreApplication::translate("main", "save to a file or to a directory"),
QCoreApplication::translate("main", "savepath"));
parser.addOption(savefileOption);
//开始解析
parser.process(a);
if(parser.isSet(openfileOption)){
QString openPath = parser.value(openfileOption);
QString savePath = parser.value(savefileOption);
QFileInfo openInfo = QFileInfo(openPath);
QFileInfo saveInfo = QFileInfo(savePath);
if(openInfo.isFile() && !(openInfo.suffix().compare("json", Qt::CaseInsensitive))){ //a json file
qDebug()<<"loading "<< openPath;
if(IOManager::loadFile(openPath, w.currentDocument())){
qDebug()<<"file "<< openPath <<"opened successfully";
//TODO: handle more commands
}else{
qDebug()<<"fail to open file";
a.quit();return 0;
}
}else if(openInfo.isDir()){ //a dir
if(saveInfo.isDir()){
qDebug()<<"open files under "<< openPath ;
QDir inDir(openPath);
if(!inDir.exists()){
qDebug()<<"dir is not existed "<< openPath ;
a.quit();return 0;
}
QDir outDir(savePath);
if(!outDir.exists()){
outDir.mkdir(savePath);
}
inDir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
QFileInfoList list = inDir.entryInfoList();
for(int i=0; i<list.size(); i++){
QFileInfo fileInfo = list.at(i);
QString suffix = fileInfo.suffix();
if(!suffix.compare("json", Qt::CaseInsensitive) ){
qDebug()<<"loading "<<fileInfo.absoluteFilePath();
if(IOManager::loadFile(fileInfo.absoluteFilePath(), w.currentDocument())){
qDebug()<<"file "<< fileInfo.absoluteFilePath() <<"opened successfully";
//TODO: handle more commands
QString saveFileName = savePath+"/"+fileInfo.fileName();
if(IOManager::saveFile(saveFileName, w.currentDocument())){
qDebug()<<"file "<< saveFileName <<"saved successfully";
}else{
qDebug()<<"faile to save file "<<saveFileName;
}
}else{
qDebug()<<"fail to open file"+fileInfo.absoluteFilePath();
}
}
}
qDebug()<<"All done!";
a.quit();return 0;
}else{
qDebug()<<"invalid saving dir";
a.quit();return 0;
}
}else{
qDebug()<<"wrong -o input";
a.quit();return 0;
}
}
w.showMaximized();
return a.exec();
}