qucs_s/qucs-lib/main.cpp
2006-06-09 05:58:31 +00:00

160 lines
5.0 KiB
C++

/***************************************************************************
main.cpp
----------
begin : Sat May 28 2005
copyright : (C) 2005 by Michael Margraf
email : michael.margraf@alumni.tu-berlin.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <qapplication.h>
#include <qstring.h>
#include <qtextcodec.h>
#include <qtranslator.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qmessagebox.h>
#include <qdir.h>
#include <qfont.h>
#include "qucslib.h"
tQucsSettings QucsSettings;
QDir UserLibDir;
// Loads the settings file and stores the settings.
bool loadSettings()
{
bool result = true;
QFile file(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/librc"));
if(!file.open(IO_ReadOnly))
result = false; // settings file doesn't exist
else {
QTextStream stream(&file);
QString Line, Setting;
while(!stream.atEnd()) {
Line = stream.readLine();
Setting = Line.section('=',0,0);
Line = Line.section('=',1,1);
if(Setting == "Position") {
QucsSettings.x = Line.section(",",0,0).toInt();
QucsSettings.y = Line.section(",",1,1).toInt(); }
else if(Setting == "Size") {
QucsSettings.dx = Line.section(",",0,0).toInt();
QucsSettings.dy = Line.section(",",1,1).toInt(); }
}
file.close();
}
file.setName(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/qucsrc"));
if(!file.open(IO_ReadOnly))
result = true; // qucs settings not necessary
else {
QTextStream stream(&file);
QString Line, Setting;
while(!stream.atEnd()) {
Line = stream.readLine();
Setting = Line.section('=',0,0);
Line = Line.section('=',1,1).stripWhiteSpace();
if(Setting == "Font")
QucsSettings.font.fromString(Line);
else if(Setting == "Language")
QucsSettings.Language = Line;
}
file.close();
}
return result;
}
// Saves the settings in the settings file.
bool saveApplSettings(QucsLib *qucs)
{
QFile file(QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/librc"));
if(!file.open(IO_WriteOnly)) {
QMessageBox::warning(0, QObject::tr("Warning"),
QObject::tr("Cannot save settings !"));
return false;
}
QString Line;
QTextStream stream(&file);
stream << "Settings file, QucsLib " PACKAGE_VERSION "\n"
<< "Position=" << qucs->x() << "," << qucs->y() << "\n"
<< "Size=" << qucs->width() << "," << qucs->height() << "\n";
file.close();
return true;
}
// #########################################################################
// ########## ##########
// ########## Program Start ##########
// ########## ##########
// #########################################################################
int main(int argc, char *argv[])
{
// apply default settings
QucsSettings.x = 100;
QucsSettings.y = 50;
QucsSettings.dx = 600;
QucsSettings.dy = 350;
QucsSettings.font = QFont("Helvetica", 12);
// is application relocated?
char * var = getenv ("QUCSDIR");
if (var != NULL) {
QDir QucsDir = QDir (var);
QString QucsDirStr = QucsDir.canonicalPath ();
QucsSettings.BitmapDir =
QDir::convertSeparators (QucsDirStr + "/share/qucs/bitmaps/");
QucsSettings.LangDir =
QDir::convertSeparators (QucsDirStr + "/share/qucs/lang/");
QucsSettings.LibDir =
QDir::convertSeparators (QucsDirStr + "/share/qucs/library/");
} else {
QucsSettings.BitmapDir = BITMAPDIR;
QucsSettings.LangDir = LANGUAGEDIR;
QucsSettings.LibDir = LIBRARYDIR;
}
UserLibDir.setPath (QDir::homeDirPath()+QDir::convertSeparators ("/.qucs/user_lib"));
loadSettings();
QApplication a(argc, argv);
a.setFont(QucsSettings.font);
QTranslator tor( 0 );
QString lang = QucsSettings.Language;
if(lang.isEmpty())
lang = QTextCodec::locale();
tor.load( QString("qucs_") + lang, QucsSettings.LangDir);
a.installTranslator( &tor );
QucsLib *qucs = new QucsLib();
a.setMainWidget(qucs);
qucs->resize(QucsSettings.dx, QucsSettings.dy); // size and position ...
qucs->move(QucsSettings.x, QucsSettings.y); // ... before "show" !!!
qucs->show();
int result = a.exec();
saveApplSettings(qucs);
delete qucs;
return result;
}