My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
main.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  main.cpp - description
3  -------------------
4  begin : Sun Feb 27 2005
5  copyright : (C) 2005 by Stefan Jahn
6  email : stefan@lkcc.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include <stdlib.h>
23 
24 #include <qapplication.h>
25 #include <qstring.h>
26 #include <qtextcodec.h>
27 #include <qtranslator.h>
28 #include <qfile.h>
29 #include <qtextstream.h>
30 #include <qmessagebox.h>
31 #include <qdir.h>
32 #include <qfont.h>
33 
34 #include "qucstrans.h"
35 
37 extern QDir QucsWorkDir;
38 extern struct TransUnit TransUnits[];
39 
40 // Loads the settings file and stores the settings.
42 {
43  bool result = true;
44 
45  QFile file(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/transrc"));
46  if(!file.open(IO_ReadOnly))
47  result = false; // settings file doesn't exist
48  else {
49  QTextStream stream(&file);
50  QString Line, Setting;
51  while(!stream.atEnd()) {
52  Line = stream.readLine();
53  Setting = Line.section('=',0,0);
54  Line = Line.section('=',1,1);
55  if(Setting == "Mode") {
56  QucsSettings.Mode = Line.simplifyWhiteSpace();
57  }
58  else if(Setting == "Frequency") {
59  Line = Line.simplifyWhiteSpace();
60  QucsSettings.freq_unit = QucsTranscalc::translateUnit(Line,0);
61  }
62  else if(Setting == "Length") {
63  Line = Line.simplifyWhiteSpace();
64  QucsSettings.length_unit = QucsTranscalc::translateUnit(Line,1);
65  }
66  else if(Setting == "Resistance") {
67  Line = Line.simplifyWhiteSpace();
68  QucsSettings.res_unit = QucsTranscalc::translateUnit(Line,2);
69  }
70  else if(Setting == "Angle") {
71  Line = Line.simplifyWhiteSpace();
72  QucsSettings.ang_unit = QucsTranscalc::translateUnit(Line,3);
73  }
74  else if(Setting == "TransWindow") {
75  QucsSettings.x = Line.section(",",0,0).toInt();
76  QucsSettings.y = Line.section(",",1,1).toInt();
77  QucsSettings.dx = Line.section(",",2,2).toInt();
78  QucsSettings.dy = Line.section(",",3,3).toInt();
79  break;
80  }
81  }
82  file.close();
83  }
84 
85  file.setName(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/qucsrc"));
86  if(!file.open(IO_ReadOnly))
87  result = true; // qucs settings not necessary
88  else {
89  QTextStream stream(&file);
90  QString Line, Setting;
91  while(!stream.atEnd()) {
92  Line = stream.readLine();
93  Setting = Line.section('=',0,0);
94  Line = Line.section('=',1,1).stripWhiteSpace();
95  if(Setting == "Font")
96  QucsSettings.font.fromString(Line);
97  else if(Setting == "Language")
98  QucsSettings.Language = Line;
99  }
100  file.close();
101  }
102  return result;
103 }
104 
105 // Saves the settings in the settings file.
107 {
108  QFile file(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/transrc"));
109  if(!file.open(IO_WriteOnly)) {
110  QMessageBox::warning(0, QObject::tr("Warning"),
111  QObject::tr("Cannot save settings !"));
112  return false;
113  }
114 
115  QString Line;
116  QTextStream stream(&file);
117 
118  stream << "Settings file, QucsTranscalc " PACKAGE_VERSION "\n"
119  << "Mode=" << qucs->getMode() << "\n"
120  << "Frequency=" << TransUnits[0].units[QucsSettings.freq_unit] << "\n"
121  << "Length=" << TransUnits[1].units[QucsSettings.length_unit] << "\n"
122  << "Resistance=" << TransUnits[2].units[QucsSettings.res_unit] << "\n"
123  << "Angle=" << TransUnits[3].units[QucsSettings.ang_unit] << "\n"
124  << "TransWindow=" << qucs->x() << ',' << qucs->y() << ','
125  << qucs->width() << ',' << qucs->height() << '\n';
126  qucs->saveModes(stream);
127 
128  file.close();
129  return true;
130 }
131 
132 // #########################################################################
133 // ########## ##########
134 // ########## Program Start ##########
135 // ########## ##########
136 // #########################################################################
137 
138 int main(int argc, char *argv[])
139 {
140  // apply default settings
141  QucsSettings.x = 100;
142  QucsSettings.y = 50;
143  QucsSettings.dx = 540;
144  QucsSettings.dy = 400;
145  QucsSettings.font = QFont("Helvetica", 12);
146  QucsSettings.length_unit = 0;
147  QucsSettings.res_unit = 0;
148  QucsSettings.ang_unit = 0;
149  QucsSettings.freq_unit = 0;
150 
151  // is application relocated?
152  char * var = getenv ("QUCSDIR");
153  if (var != NULL) {
154  QDir QucsDir = QDir (var);
155  QString QucsDirStr = QucsDir.canonicalPath ();
156  QucsSettings.BitmapDir =
157  QDir::convertSeparators (QucsDirStr + "/share/qucs/bitmaps/");
158  QucsSettings.LangDir =
159  QDir::convertSeparators (QucsDirStr + "/share/qucs/lang/");
160  } else {
161  QucsSettings.BitmapDir = BITMAPDIR;
162  QucsSettings.LangDir = LANGUAGEDIR;
163  }
164  QucsWorkDir.setPath (QDir::homeDirPath()+QDir::convertSeparators ("/.qucs"));
165  loadSettings();
166 
167  QApplication a(argc, argv);
168  a.setFont(QucsSettings.font);
169 
170  QTranslator tor( 0 );
171  QString lang = QucsSettings.Language;
172  if(lang.isEmpty())
173  lang = QTextCodec::locale();
174  tor.load( QString("qucs_") + lang, QucsSettings.LangDir);
175  a.installTranslator( &tor );
176 
177  QucsTranscalc *qucs = new QucsTranscalc();
178  a.setMainWidget(qucs);
179  qucs->resize(QucsSettings.dx, QucsSettings.dy); // size and position ...
180  qucs->move(QucsSettings.x, QucsSettings.y); // ... before "show" !!!
181  qucs->show();
182 
183  qucs->loadFile(QDir::homeDirPath()+"/.qucs/transrc");
184  qucs->setMode(QucsSettings.Mode);
185 
186  // optional file argument
187  if (argc > 1) {
188  int _mode = 0;
189  QString File = argv[1];
190  qucs->loadFile(File,&_mode);
191  }
192 
193  int result = a.exec();
194  saveApplSettings(qucs);
195  delete qucs;
196  return result;
197 }