Skip to content

Commit

Permalink
#956 open project/schematics by commandLine
Browse files Browse the repository at this point in the history
  • Loading branch information
oxmon-2500 committed Mar 7, 2020
1 parent 830c996 commit 0c26661
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 93 deletions.
247 changes: 174 additions & 73 deletions qucs/qucs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ void createListComponentEntry(){
Comps = Category::getModules(category);

// \fixme, crash with diagrams, skip
if(category == "diagrams") break;
if(category == "diagrams" || category == "paintings") continue;

char * File;
QString Name;
Expand All @@ -474,6 +474,9 @@ void createListComponentEntry(){

// FIXME: cleanup
QTextStream s;
if (c->getSchematic()==nullptr){
continue;
}
c->getSchematic()->saveComponent(s, c);
QString qucsEntry = *(s.string());
fprintf(stdout, "%s; qucs ; %s\n", c->obsolete_model_hack().toAscii().data(), qucsEntry.toAscii().data());
Expand Down Expand Up @@ -504,6 +507,8 @@ void createListComponentEntry(){
// ########## Program Start ##########
// ########## ##########
// #########################################################################
QString convertPath(char *sbf);

int main(int argc, char *argv[])
{
qInstallMsgHandler(qucsMessageOutput);
Expand Down Expand Up @@ -684,88 +689,165 @@ int main(int argc, char *argv[])
int dpi = 96;
QString color = "RGB";
QString orientation = "portrait";
QString argvProjectPath;
QStringList argvFileList;
QStringList hasErrors;

// simple command line parser
for (int i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
fprintf(stdout,
"Usage: %s [-hv] \n"
" qucs -n -i FILENAME -o FILENAME\n"
" qucs -p -i FILENAME -o FILENAME.[pdf|png|svg|eps] \n\n"
" -h, --help display this help and exit\n"
" -v, --version display version information and exit\n"
" -n, --netlist convert Qucs schematic into netlist\n"
" -p, --print print Qucs schematic to file (eps needs inkscape)\n"
" -q, --quit exit\n"
" --page [A4|A3|B4|B5] set print page size (default A4)\n"
" --dpi NUMBER set dpi value (default 96)\n"
" --color [RGB|RGB] set color mode (default RGB)\n"
" --orin [portraid|landscape] set orientation (default portraid)\n"
" -i FILENAME use file as input schematic\n"
" -o FILENAME use file as output netlist\n"
" -icons create component icons under ./bitmaps_generated\n"
" -doc dump data for documentation:\n"
" * file with of categories: categories.txt\n"
" * one directory per category (e.g. ./lumped components/)\n"
" - CSV file with component data ([comp#]_data.csv)\n"
" - CSV file with component properties. ([comp#]_props.csv)\n"
" -list-entries list component entry formats for schematic and netlist\n"
, argv[0]);
return 0;
}else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
if (argv[i][0]=='-')
{
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
fprintf(stdout,
"Usage: %s [-h|--help] [-v|--version] \n"
" qucs -n -i FILENAME -o FILENAME\n"
" qucs -p -i FILENAME -o FILENAME.[pdf|png|svg|eps] \n"
" qucs [ projectPath] [ filePath1 [ filePath2 ... ] ] ]\n\n"
" -h, --help display this help and exit\n"
" -v, --version display version information and exit\n"
" -n, --netlist convert Qucs schematic into netlist\n"
" -p, --print print Qucs schematic to file (eps needs inkscape)\n"
" --page [A4|A3|B4|B5] set print page size (default A4)\n"
" --dpi NUMBER set dpi value (default 96)\n"
" --color [RGB|RGB] set color mode (default RGB)\n"
" --orin [portrait|landscape] set orientation (default portrait)\n"
" -i FILENAME use file as input schematic\n"
" -o FILENAME use file as output netlist\n"
" -icons create component icons under ./bitmaps_generated\n"
" -doc dump data for documentation:\n"
" * file with of categories: categories.txt\n"
" * one directory per category (e.g. ./lumped components/)\n"
" - CSV file with component data ([comp#]_data.csv)\n"
" - CSV file with component properties. ([comp#]_props.csv)\n"
" -a plugin load (attach) plugin -- under construction"
//FIXME " -list-entries list component entry formats for schematic and netlist\n"
" -q, --quit exit\n"
"\n"
" Examples:\n"
" qucs -n -i schematic.sch -o netlist.net\n"
" qucs -p -i schematic.sch -o output.png --page A4 --dpi 120 --orin landscape\n"
" qucs -icons\n"
" qucs -doc\n"
//FIXME " qucs -list-entries\n"
" qucs ../qucs-test/testsuite/AC_bandpass_prj/ bandpass.sch opa227.sch\n"
" qucs ~/proj/qucs-test/testsuite/DC_AC_gain_prj \"*.sch\"\n"
, argv[0]);
return 0;
}else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
#ifdef GIT
fprintf(stdout, "Qucs " PACKAGE_VERSION " (" GIT ")" "\n");
#else
fprintf(stdout, "Qucs " PACKAGE_VERSION "\n");
#endif
return 0;
}
else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--netlist")) {
netlist_flag = true;
}
else if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--print")) {
print_flag = true;
}
else if (!strcmp(argv[i], "--page")) {
page = argv[++i];
}
else if (!strcmp(argv[i], "--dpi")) {
dpi = QString(argv[++i]).toInt();
}
else if (!strcmp(argv[i], "--color")) {
color = argv[++i];
}
else if (!strcmp(argv[i], "--orin")) {
orientation = argv[++i];
}
else if (!strcmp(argv[i], "-a")) {
attach(argv[++i]);
}
else if(!strcmp(argv[i], "-q") || !strcmp(argv[i], "--quit")) {
exit(0);
}
else if (!strcmp(argv[i], "-i")) {
inputfile = argv[++i];
}
else if (!strcmp(argv[i], "-o")) {
outputfile = argv[++i];
}
else if(!strcmp(argv[i], "-icons")) {
createIcons();
return 0;
}
else if(!strcmp(argv[i], "-doc")) {
createDocData();
return 0;
return 0;
}
else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--netlist")) {
netlist_flag = true;
}
else if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--print")) {
print_flag = true;
}
else if (!strcmp(argv[i], "--page")) {
page = argv[++i];
}
else if (!strcmp(argv[i], "--dpi")) {
dpi = QString(argv[++i]).toInt();
}
else if (!strcmp(argv[i], "--color")) {
color = argv[++i];
}
else if (!strcmp(argv[i], "--orin")) {
orientation = argv[++i];
// for comatibility with existing scripts
if (orientation=="portraid"){
orientation=="portrait";
}
}
else if (!strcmp(argv[i], "-a")) {
attach(argv[++i]);
}
else if (!strcmp(argv[i], "-q") || !strcmp(argv[i], "--quit")) {
exit(0);
}
else if (!strcmp(argv[i], "-i")) {
inputfile = argv[++i];
}
else if (!strcmp(argv[i], "-o")) {
outputfile = argv[++i];
}
else if(!strcmp(argv[i], "-icons")) {
createIcons();
return 0;
}
else if(!strcmp(argv[i], "-doc")) {
createDocData();
return 0;
}
else if(!strcmp(argv[i], "-list-entries")) {
createListComponentEntry();
return 0;
}
else {
fprintf(stderr, "Error: Unknown option: %s\n", argv[i]);
return -1;
}
}
else if(!strcmp(argv[i], "-list-entries")) {
createListComponentEntry();
return 0;
else
{
//
QFileInfo filePath(convertPath(argv[i]));
if(filePath.isDir())
{
if (argvProjectPath!=nullptr){
hasErrors.append("Err: multiple path definitions");
}
argvProjectPath = filePath.canonicalFilePath()+"/"; // i.e . -> /home/usr/qucs/test01_prj
}
else
if(filePath.isFile())
{
argvFileList.append(filePath.filePath());
}
else
{
//is not dir nor file (isSymLink?)
bool exist = false;
//doesn't exist alone, try in project path
if (!argvProjectPath.isNull())
{
if (!filePath.isAbsolute()){
QFileInfo fullpath(argvProjectPath + filePath.filePath());
if (fullpath.isFile())
{
argvFileList.append(fullpath.absoluteFilePath());
exist = true;
}else{
//try wildcard *.sch
QDir prjPathDir(argvProjectPath);
QStringList wildCard;
wildCard.append(argv[i]);
QFileInfoList fList = prjPathDir.entryInfoList(wildCard, QDir::Files);
foreach(QFileInfo fi, fList){
argvFileList.append(fi.absoluteFilePath());
}
exist = fList.size()>0;
}
}
}
if (!exist)
{
hasErrors.append("Error: File doesn't exist: "+QString(argv[i]));
}
}
}
else {
fprintf(stderr, "Error: Unknown option: %s\n", argv[i]);
return -1;
} // for argv

if (hasErrors.size()>0)
{
foreach(QString el, hasErrors) {
fprintf(stderr, "-----------> %s\n", el.toAscii().data());
}
return -1;
}

// check operation and its required arguments
Expand All @@ -791,10 +873,29 @@ int main(int argc, char *argv[])
}

QucsMain = new QucsApp();

QucsMain->initProjectSchematics(argvProjectPath, argvFileList);
QucsMain->show();
int result = a.exec();
//saveApplSettings(QucsMain);
return result;
}
/*
* translate:
* \a\b\c -> /a/b/c
* \a\b\c\ -> /a/b/c/
*/
QString convertPath(char *sbf){
//From Qt 4.6 QDir documentation,
// Qt uses "/" as a universal directory separator in the same
// way that "/" is used as a path separator in URLs.
// If you always use "/" as a directory separator, Qt will
// translate your paths to conform to the underlying operating system.
int l = strlen(sbf);
char tbf[l+1];
for(int i=0; i<l+1; ++i){ // including terminating 0
tbf[i] = (sbf[i]=='\\')? '/' : sbf[i];
}
QString ret(tbf);
return ret;
}
// vim:ts=8:sw=2:noet
60 changes: 40 additions & 20 deletions qucs/qucs/qucs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,22 @@ QucsApp::QucsApp()
switchSchematicDoc(true); // "untitled" document is schematic

lastExportFilename = QDir::homePath() + QDir::separator() + "export.png";

// load documents given as command line arguments
for(int z=1; z<qApp->arguments().size(); z++) {
QString arg = qApp->arguments()[z];
QByteArray ba = arg.toLatin1();
const char *c_arg= ba.data();
if(*(c_arg) != '-') {
QFileInfo Info(arg);
QucsSettings.QucsWorkDir.setPath(Info.absoluteDir().absolutePath());
arg = QucsSettings.QucsWorkDir.filePath(Info.fileName());
gotoPage(arg);
}
}
}

QucsApp::~QucsApp()
{
Module::unregisterModules ();
}

void QucsApp::initProjectSchematics(const QString argvProjectPath, const QStringList argvFileList){
initProjects(argvProjectPath);
// load documents given as command line arguments
foreach(QString f, argvFileList) {
QFileInfo info(f);
QucsSettings.QucsWorkDir.setPath(info.absoluteDir().absolutePath());
gotoPage(QucsSettings.QucsWorkDir.filePath(info.fileName()));
}
}

// #######################################################################
// ########## ##########
Expand Down Expand Up @@ -353,22 +349,41 @@ void QucsApp::initView()
//m_proxyModel->setDynamicSortFilter(true);
// show all directories (project and non-project)
m_homeDirModel->setFilter(QDir::NoDot | QDir::AllDirs);

// ............................................
QString path = QucsSettings.QucsHomeDir.absolutePath();
QDir ProjDir(path);
}
void QucsApp::initProjects(const QString argvProjsDir)
{
QString path = argvProjsDir.isNull()? QucsSettings.QucsHomeDir.absolutePath() : argvProjsDir;
// initial projects directory is the Qucs home directory
QucsSettings.projsDir = path;

QDir projDir(path);
// create home dir if not exist
if(!ProjDir.exists()) {
if(!ProjDir.mkdir(path)) {
if(!projDir.exists()) {
if(!projDir.mkdir(path)) {
QMessageBox::warning(this, tr("Warning"),
tr("Cannot create work directory !"));
tr("Cannot create work directory !")+" "+path);
return;
}
}
readProjects(); // reads all projects and inserts them into the ListBox

if (!argvProjsDir.isNull()){
if (argvProjsDir.endsWith("_prj/")){
int lx0=0;
for (int lx=0; lx<argvProjsDir.length()-1; ++lx){
if(argvProjsDir.at(lx)=='/'){
lx0 = lx;
}
}
path = argvProjsDir.mid(0, lx0); //cat "/name_prj
}
QucsSettings.projsDir = path;
readProjects();
if (argvProjsDir.endsWith("_prj/")){
//QucsSettings.projsDir = argvProjsDir;
openProject(argvProjsDir);
}
}
}

// Put all available libraries into ComboBox.
Expand Down Expand Up @@ -1177,6 +1192,11 @@ void QucsApp::openProject(const QString& Path)
octave->adjustDirectory();

Content->setProjPath(QucsSettings.QucsWorkDir.absolutePath());
//-------------------------------------------------------------------------------------------------------
// select (highlight) project in QListView (projects)
QModelIndex idx = m_homeDirModel->index(openProjName, 0); //FIXME
Projects->selectionModel()->select(idx, QItemSelectionModel::Select); //FIXME
//-------------------------------------------------------------------------------------------------------

TabView->setCurrentIndex(1); // switch to "Content"-Tab

Expand Down
Loading

0 comments on commit 0c26661

Please sign in to comment.