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 : Thu Jun 24 2004
5  copyright : (C) 2004 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 #include <qtextcodec.h>
34 
35 #include "qucshelp.h"
36 
37 QDir QucsHelpDir; // directory to find helps files
38 tQucsSettings QucsSettings; // application settings
39 
40 // #########################################################################
41 // Loads the settings file and stores the settings.
43 {
44  bool result = true;
45 
46  QFile file(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/helprc"));
47  if(!file.open(IO_ReadOnly))
48  result = false; // settings file doesn't exist
49  else {
50  QTextStream stream(&file);
51  QString Line, Setting;
52  while(!stream.atEnd()) {
53  Line = stream.readLine();
54  Setting = Line.section('=',0,0);
55  Line = Line.section('=',1,1);
56  if(Setting == "HelpWindow") {
57  QucsSettings.x = Line.section(",",0,0).toInt();
58  QucsSettings.y = Line.section(",",1,1).toInt();
59  QucsSettings.dx = Line.section(",",2,2).toInt();
60  QucsSettings.dy = Line.section(",",3,3).toInt();
61  break; }
62  }
63  file.close();
64  }
65 
66  file.setName(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/qucsrc"));
67  if(!file.open(IO_ReadOnly))
68  result = true; // qucs settings not necessary
69  else {
70  QTextStream stream(&file);
71  QString Line, Setting;
72  while(!stream.atEnd()) {
73  Line = stream.readLine();
74  Setting = Line.section('=',0,0);
75  Line = Line.section('=',1,1).stripWhiteSpace();
76  if(Setting == "Font")
77  QucsSettings.font.fromString(Line);
78  else if(Setting == "Language")
79  QucsSettings.Language = Line;
80  }
81  file.close();
82  }
83  return result;
84 }
85 
86 // #########################################################################
87 // Saves the settings in the settings file.
89 {
90  if(qucs->x() == QucsSettings.x)
91  if(qucs->y() == QucsSettings.y)
92  if(qucs->width() == QucsSettings.dx)
93  if(qucs->height() == QucsSettings.dy)
94  return true; // nothing has changed
95 
96 
97  QFile file(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/helprc"));
98  if(!file.open(IO_WriteOnly)) {
99  QMessageBox::warning(0, QObject::tr("Warning"),
100  QObject::tr("Cannot save settings !"));
101  return false;
102  }
103 
104  QString Line;
105  QTextStream stream(&file);
106 
107  stream << "Settings file, Qucs Help System " PACKAGE_VERSION "\n"
108  << "HelpWindow=" << qucs->x() << ',' << qucs->y() << ','
109  << qucs->width() << ',' << qucs->height() << '\n';
110 
111  file.close();
112  return true;
113 }
114 
115 
116 // #########################################################################
117 // ########## ##########
118 // ########## Program Start ##########
119 // ########## ##########
120 // #########################################################################
121 
122 int main(int argc, char *argv[])
123 {
124  // apply default settings
125  QucsSettings.x = 60;
126  QucsSettings.y = 30;
127  QucsSettings.dx = 640;
128  QucsSettings.dy = 400;
129  QucsSettings.font = QFont("Helvetica", 12);
130 
131  // is application relocated?
132  char * var = getenv ("QUCSDIR");
133  if (var != NULL) {
134  QDir QucsDir = QDir (var);
135  QString QucsDirStr = QucsDir.canonicalPath ();
136  QucsSettings.DocDir =
137  QDir::convertSeparators (QucsDirStr + "/share/qucs/docs/");
138  QucsSettings.BitmapDir =
139  QDir::convertSeparators (QucsDirStr + "/share/qucs/bitmaps/");
140  QucsSettings.LangDir =
141  QDir::convertSeparators (QucsDirStr + "/share/qucs/lang/");
142  } else {
143  QucsSettings.DocDir = DOCDIR;
144  QucsSettings.BitmapDir = BITMAPDIR;
145  QucsSettings.LangDir = LANGUAGEDIR;
146  }
147 
148  loadSettings();
149 
150  QApplication a(argc, argv);
151  a.setFont(QucsSettings.font);
152 
153  QTranslator tor( 0 );
154  QString locale = QucsSettings.Language;
155  if(locale.isEmpty())
156  locale = QTextCodec::locale();
157  tor.load( QString("qucs_") + locale, QucsSettings.LangDir);
158  a.installTranslator( &tor );
159 
160  QucsHelpDir = QucsSettings.DocDir + locale;
161  if (!QucsHelpDir.exists () || !QucsHelpDir.isReadable ()) {
162  int p = locale.find ('_');
163  if (p != -1) {
164  QucsHelpDir = QucsSettings.DocDir + locale.left (p);
165  if (!QucsHelpDir.exists () || !QucsHelpDir.isReadable ()) {
166  QucsHelpDir = QucsSettings.DocDir + "en";
167  }
168  }
169  else QucsHelpDir = QucsSettings.DocDir + "en";
170  }
171 
172  QString Page;
173  if(argc > 1) Page = argv[1];
174 
175  QucsHelp *qucs = new QucsHelp(Page);
176  a.setMainWidget(qucs);
177  qucs->resize(QucsSettings.dx, QucsSettings.dy); // size and position ...
178  qucs->move(QucsSettings.x, QucsSettings.y); // ... before "show" !!!
179  qucs->show();
180  int result = a.exec();
181  saveApplSettings(qucs);
182  return result;
183 }