My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
qucsedit.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qucsedit.cpp
3  --------------
4  begin : Mon Nov 17 2003
5  copyright : (C) 2003 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 "qucsedit.h"
23 
24 #include <qtextedit.h>
25 #include <qlabel.h>
26 #include <qlayout.h>
27 #include <qhbox.h>
28 #include <qpushbutton.h>
29 #include <qfile.h>
30 #include <qtextstream.h>
31 #include <qmessagebox.h>
32 #include <qtoolbutton.h>
33 #include <qimage.h>
34 #include <qfiledialog.h>
35 #include <qfont.h>
36 
37 
38 QucsEdit::QucsEdit(const QString& FileName_, bool readOnly)
39 {
40  // set application icon
41  setIcon (QPixmap(QucsSettings.BitmapDir + "big.qucs.xpm"));
42  setCaption("Qucs Editor " PACKAGE_VERSION " - " + tr("File: "));
43 
44  QVBoxLayout *v = new QVBoxLayout(this);
45 
46  QHBox *h = new QHBox(this);
47  v->addWidget(h);
48 
49  QToolButton *ButtLoad = new QToolButton(h);
50  ButtLoad->setIconSet(
51  QIconSet(QImage(QucsSettings.BitmapDir + "fileopen.png")));
52  connect(ButtLoad, SIGNAL(clicked()), SLOT(slotLoad()));
53 
54  QToolButton *ButtSave = new QToolButton(h);
55  ButtSave->setIconSet(
56  QIconSet(QImage(QucsSettings.BitmapDir + "filesave.png")));
57  connect(ButtSave, SIGNAL(clicked()), SLOT(slotSave()));
58  ButtSave->setDisabled(readOnly);
59 
60  h->setStretchFactor(new QWidget(h),5); // stretchable placeholder
61  PosText = new QLabel(tr("Line: %1 - Column: %2").arg(1).arg(1), h);
62  h->setStretchFactor(new QWidget(h),5); // stretchable placeholder
63 
64  QPushButton *ButtAbout = new QPushButton(tr("About"),h);
65  connect(ButtAbout, SIGNAL(clicked()), SLOT(slotAbout()));
66 
67  QPushButton *ButtOK = new QPushButton(tr("Quit"),h);
68  connect(ButtOK, SIGNAL(clicked()), SLOT(slotQuit()));
69  ButtOK->setFocus();
70 
71  // try using same-sized mono-spaced font in the textarea
72  QFont fedit = QFont("Courier New");
73  fedit.setPointSize(QucsSettings.font.pointSize()-1);
74  fedit.setStyleHint(QFont::Courier);
75  fedit.setFixedPitch(true);
76 
77  text = new QTextEdit(this);
78  text->setTextFormat(Qt::PlainText);
79  text->setReadOnly(readOnly);
80  text->setWordWrap(QTextEdit::NoWrap);
81  text->setMinimumSize(300,200);
82  text->setFont(fedit);
83  text->setCurrentFont(fedit);
84  v->addWidget(text);
85  connect(text, SIGNAL(cursorPositionChanged(int, int)),
86  SLOT(slotPrintCursorPosition(int, int)));
87 
88  // .................................................
89  loadFile(FileName_);
90 }
91 
93 {
94 }
95 
96 // ************************************************************
97 void QucsEdit::slotPrintCursorPosition(int Para, int Pos)
98 {
99  PosText->setText(tr("Line: %1 - Column: %2").arg(Para+1).arg(Pos+1));
100 }
101 
102 // ************************************************************
103 void QucsEdit::slotAbout()
104 {
105  QMessageBox::about(this, tr("About..."),
106  "QucsEdit Version " PACKAGE_VERSION+
107  tr("\nVery simple text editor for Qucs\n")+
108  tr("Copyright (C) 2004, 2005 by Michael Margraf\n")+
109  "\nThis is free software; see the source for copying conditions."
110  "\nThere is NO warranty; not even for MERCHANTABILITY or "
111  "\nFITNESS FOR A PARTICULAR PURPOSE.");
112 }
113 
114 // ************************************************************
115 void QucsEdit::slotLoad()
116 {
117  static QString lastDir; // to remember last directory and file
118 
119  QString s = QFileDialog::getOpenFileName(
120  lastDir.isEmpty() ? QString(".") : lastDir,
121  "*", this, "", tr("Enter a Filename"));
122  if(s.isEmpty()) return;
123  lastDir = s; // remember last directory and file
124  if(!closeFile()) return;
125  loadFile(s);
126 }
127 
128 // ************************************************************
129 void QucsEdit::slotSave()
130 {
131  if(FileName.isEmpty()) {
132  FileName = QFileDialog::getSaveFileName(".", QString::null,
133  this, "", tr("Enter a Document Name"));
134  if(FileName.isEmpty()) return;
135  }
136 
137  QFile file(FileName);
138  if(!file.open(IO_WriteOnly)) {
139  QMessageBox::critical(this, tr("Error"),
140  tr("Cannot write file: ")+FileName);
141  return;
142  }
143 
144  QTextStream stream(&file);
145  stream << text->text();
146  text->setModified(false);
147  file.close();
148 }
149 
150 // ************************************************************
151 void QucsEdit::slotQuit()
152 {
153  if(!closeFile()) return;
154 
155  int tmp;
156  tmp = x(); // call size and position function in order to ...
157  tmp = y(); // ... set them correctly before closing the ...
158  tmp = width(); // dialog !!! Otherwise the frame of the window ...
159  tmp = height(); // will not be recognized (a X11 problem).
160 
161  accept();
162 }
163 
164 // ************************************************************
165 // To get all close events.
166 void QucsEdit::closeEvent(QCloseEvent*)
167 {
168  slotQuit();
169 }
170 
171 // ************************************************************
172 bool QucsEdit::loadFile(const QString& Name)
173 {
174  if(Name.isEmpty()) return false;
175  QFile file(Name);
176  if(!file.open(IO_ReadOnly)) {
177  QMessageBox::critical(this, tr("Error"),
178  tr("Cannot read file: ")+Name);
179  return false;
180  }
181 
182  QTextStream stream(&file);
183  text->setText(stream.read());
184  file.close();
185 
186  FileName = Name;
187 // QFileInfo info(Name);
188 // FileName = info.fileName();
189  setCaption("Qucs Editor " PACKAGE_VERSION " - " + tr("File: ")+FileName);
190  return true;
191 }
192 
193 
194 // ************************************************************
195 bool QucsEdit::closeFile()
196 {
197  if(text->isModified()) {
198  switch(QMessageBox::warning(this,tr("Closing document"),
199  tr("The text contains unsaved changes!\n")+
200  tr("Do you want to save the changes?"),
201  tr("&Save"), tr("&Discard"), tr("&Cancel"), 0, 2)) {
202  case 0: slotSave();
203  if(FileName.isEmpty()) return false;
204  return true;
205  case 2: return false;
206  }
207  }
208  return true;
209 }