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
3  ----------
4  begin : Sat May 28 2005
5  copyright : (C) 2005 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
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 "qucslib.h"
35 
38 
39 // Loads the settings file and stores the settings.
41 {
42  bool result = true;
43 
44  QFile file(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/librc"));
45  if(!file.open(IO_ReadOnly))
46  result = false; // settings file doesn't exist
47  else {
48  QTextStream stream(&file);
49  QString Line, Setting;
50  while(!stream.atEnd()) {
51  Line = stream.readLine();
52  Setting = Line.section('=',0,0);
53  Line = Line.section('=',1,1);
54  if(Setting == "Position") {
55  QucsSettings.x = Line.section(",",0,0).toInt();
56  QucsSettings.y = Line.section(",",1,1).toInt(); }
57  else if(Setting == "Size") {
58  QucsSettings.dx = Line.section(",",0,0).toInt();
59  QucsSettings.dy = Line.section(",",1,1).toInt(); }
60  }
61  file.close();
62  }
63 
64  file.setName(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/qucsrc"));
65  if(!file.open(IO_ReadOnly))
66  result = true; // qucs settings not necessary
67  else {
68  QTextStream stream(&file);
69  QString Line, Setting;
70  while(!stream.atEnd()) {
71  Line = stream.readLine();
72  Setting = Line.section('=',0,0);
73  Line = Line.section('=',1,1).stripWhiteSpace();
74  if(Setting == "Font")
75  QucsSettings.font.fromString(Line);
76  else if(Setting == "Language")
77  QucsSettings.Language = Line;
78  }
79  file.close();
80  }
81  return result;
82 }
83 
84 // Saves the settings in the settings file.
86 {
87  QFile file(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/librc"));
88  if(!file.open(IO_WriteOnly)) {
89  QMessageBox::warning(0, QObject::tr("Warning"),
90  QObject::tr("Cannot save settings !"));
91  return false;
92  }
93 
94  QString Line;
95  QTextStream stream(&file);
96 
97  stream << "Settings file, QucsLib " PACKAGE_VERSION "\n"
98  << "Position=" << qucs->x() << "," << qucs->y() << "\n"
99  << "Size=" << qucs->width() << "," << qucs->height() << "\n";
100 
101  file.close();
102  return true;
103 }
104 
105 // #########################################################################
106 // ########## ##########
107 // ########## Program Start ##########
108 // ########## ##########
109 // #########################################################################
110 
111 int main(int argc, char *argv[])
112 {
113  // apply default settings
114  QucsSettings.x = 100;
115  QucsSettings.y = 50;
116  QucsSettings.dx = 600;
117  QucsSettings.dy = 350;
118  QucsSettings.font = QFont("Helvetica", 12);
119 
120  // is application relocated?
121  char * var = getenv ("QUCSDIR");
122  if (var != NULL) {
123  QDir QucsDir = QDir (var);
124  QString QucsDirStr = QucsDir.canonicalPath ();
125  QucsSettings.BitmapDir =
126  QDir::convertSeparators (QucsDirStr + "/share/qucs/bitmaps/");
127  QucsSettings.LangDir =
128  QDir::convertSeparators (QucsDirStr + "/share/qucs/lang/");
129  QucsSettings.LibDir =
130  QDir::convertSeparators (QucsDirStr + "/share/qucs/library/");
131  } else {
132  QucsSettings.BitmapDir = BITMAPDIR;
133  QucsSettings.LangDir = LANGUAGEDIR;
134  QucsSettings.LibDir = LIBRARYDIR;
135  }
136  UserLibDir.setPath (QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/user_lib"));
137  loadSettings();
138 
139  QApplication a(argc, argv);
140  a.setFont(QucsSettings.font);
141 
142  QTranslator tor( 0 );
143  QString lang = QucsSettings.Language;
144  if(lang.isEmpty())
145  lang = QTextCodec::locale();
146  tor.load( QString("qucs_") + lang, QucsSettings.LangDir);
147  a.installTranslator( &tor );
148 
149  QucsLib *qucs = new QucsLib();
150  a.setMainWidget(qucs);
151  qucs->resize(QucsSettings.dx, QucsSettings.dy); // size and position ...
152  qucs->move(QucsSettings.x, QucsSettings.y); // ... before "show" !!!
153  qucs->show();
154 
155  int result = a.exec();
156  saveApplSettings(qucs);
157  delete qucs;
158  return result;
159 }