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 Aug 28 2003
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 
34 #include "qucsedit.h"
35 
37 
38 // #########################################################################
39 // Loads the settings file and stores the settings.
41 {
42  bool result = true;
43 
44  QFile file(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/editrc"));
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 == "EditWindow") {
55  QucsSettings.x = Line.section(",",0,0).toInt();
56  QucsSettings.y = Line.section(",",1,1).toInt();
57  QucsSettings.dx = Line.section(",",2,2).toInt();
58  QucsSettings.dy = Line.section(",",3,3).toInt();
59  break; }
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 // #########################################################################
85 // Saves the settings in the settings file.
87 {
88  if(qucs->x() == QucsSettings.x)
89  if(qucs->y() == QucsSettings.y)
90  if(qucs->width() == QucsSettings.dx)
91  if(qucs->height() == QucsSettings.dy)
92  return true; // nothing has changed
93 
94 
95  QFile file(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/editrc"));
96  if(!file.open(IO_WriteOnly)) {
97  QMessageBox::warning(0, QObject::tr("Warning"),
98  QObject::tr("Cannot save settings !"));
99  return false;
100  }
101 
102  QString Line;
103  QTextStream stream(&file);
104 
105  stream << "Settings file, Qucs Editor " PACKAGE_VERSION "\n"
106  << "EditWindow=" << qucs->x() << ',' << qucs->y() << ','
107  << qucs->width() << ',' << qucs->height() << '\n';
108 
109  file.close();
110  return true;
111 }
112 
113 // #########################################################################
115 {
116  fprintf(stdout, QObject::tr("Qucs Editor Version ")+PACKAGE_VERSION+
117  QObject::tr("\nVery simple text editor for Qucs\n")+
118  QObject::tr("Copyright (C) 2004, 2005 by Michael Margraf\n")+
119  QObject::tr("\nUsage: qucsedit [-r] file\n")+
120  QObject::tr(" -h display this help and exit\n")+
121  QObject::tr(" -r open file read-only\n"));
122 }
123 
124 
125 // #########################################################################
126 // ########## ##########
127 // ########## Program Start ##########
128 // ########## ##########
129 // #########################################################################
130 
131 int main(int argc, char *argv[])
132 {
133  // apply default settings
134  QucsSettings.x = 200;
135  QucsSettings.y = 100;
136  QucsSettings.dx = 400;
137  QucsSettings.dy = 400;
138  QucsSettings.font = QFont("Helvetica", 12);
139 
140  // is application relocated?
141  char * var = getenv ("QUCSDIR");
142  if (var != NULL) {
143  QDir QucsDir = QDir (var);
144  QString QucsDirStr = QucsDir.canonicalPath ();
145  QucsSettings.BitmapDir =
146  QDir::convertSeparators (QucsDirStr + "/share/qucs/bitmaps/");
147  QucsSettings.LangDir =
148  QDir::convertSeparators (QucsDirStr + "/share/qucs/lang/");
149  } else {
150  QucsSettings.BitmapDir = BITMAPDIR;
151  QucsSettings.LangDir = LANGUAGEDIR;
152  }
153 
154  loadSettings();
155 
156  QApplication a(argc, argv);
157  a.setFont(QucsSettings.font);
158 
159  QTranslator tor( 0 );
160  QString lang = QucsSettings.Language;
161  if(lang.isEmpty())
162  lang = QTextCodec::locale();
163  tor.load( QString("qucs_") + lang, QucsSettings.LangDir);
164  a.installTranslator( &tor );
165 
166  bool readOnly = false;
167  QString FileName, s;
168  for(int i=1; i<argc; i++) {
169  s = argv[i];
170  if(s.at(0) == '-') {
171  if(s.length() != 2) {
172  fprintf(stdout, QObject::tr("Too long command line argument!\n\n"));
173  showOptions();
174  return -1;
175  }
176  switch(s.at(1).latin1()) {
177  case 'r': readOnly = true;
178  break;
179  case 'h': showOptions();
180  return 0;
181  default :
182  fprintf(stderr, QObject::tr("Wrong command line argument!\n\n"));
183  showOptions();
184  return -1;
185  }
186  }
187  else if(FileName.isEmpty()) FileName = s;
188  else {
189  fprintf(stderr, QObject::tr("Only one filename allowed!\n\n"));
190  showOptions();
191  return -1;
192  }
193  }
194 
195  QucsEdit *qucs = new QucsEdit(FileName, readOnly);
196  a.setMainWidget(qucs);
197  qucs->resize(QucsSettings.dx, QucsSettings.dy); // size and position ...
198  qucs->move(QucsSettings.x, QucsSettings.y); // ... before "show" !!!
199  qucs->show();
200  int result = a.exec();
201  saveApplSettings(qucs);
202  return result;
203 }