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