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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/** Copyright (C) 2006, Ian Paul Larsen.
**
** 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.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along
** with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
**/
#include <iostream>
#include <QTextCursor>
#include <QFileDialog>
#include <QMessageBox>
#include <QStatusBar>
#include <QPrinter>
#include <QPrintDialog>
#include <QMessageBox>
#include <QFlags>
using namespace std;
#include "BasicEdit.h"
#include "Settings.h"
BasicEdit::BasicEdit(QMainWindow *mw)
{
QFont f;
QSettings settings(SETTINGSORG, SETTINGSAPP);
mainwin = mw;
f.setFamily("Sans");
f.setFixedPitch(true);
f.setPointSize(settings.value(SETTINGSFONTSIZE, SETTINGSFONTSIZEDEFAULT).toInt());
setFont(f);
currentMaxLine = 10;
currentLine = 1;
startPos = textCursor().position();
setAcceptRichText(false);
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(cursorMove()));
codeChanged = false;
}
void
BasicEdit::fontSmall()
{
changeFontSize(8);
}
void
BasicEdit::fontMedium()
{
changeFontSize(10);
}
void
BasicEdit::fontLarge()
{
changeFontSize(12);
}
void
BasicEdit::fontHuge()
{
changeFontSize(15);
}
void
BasicEdit::changeFontSize(unsigned int pointSize)
{
QFont f;
f.setFamily("Sans");
f.setFixedPitch(true);
f.setPointSize(pointSize);
setFont(f);
QSettings settings(SETTINGSORG, SETTINGSAPP);
settings.setValue(SETTINGSFONTSIZE, pointSize);
}
void
BasicEdit::cursorMove()
{
QTextCursor t(textCursor());
// get current column
int col = t.position();
t.movePosition(QTextCursor::StartOfLine);
col = col - t.position() + 1;
t.movePosition(QTextCursor::StartOfBlock);
// get line
int line = 1;
while (t.movePosition(QTextCursor::PreviousBlock))
{
line++;
}
currentLine = line;
//
mainwin->statusBar()->showMessage(tr("Line: ") + QString::number(currentLine) + tr(" Column: ") + QString::number(col));
}
void
BasicEdit::highlightLine(int hline)
{
QTextCursor t(textCursor());
t.setPosition(0);
int line = 1;
while (line < hline)
{
t.movePosition(QTextCursor::NextBlock);
line++;
}
t.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, 1);
setTextCursor(t);
}
void
BasicEdit::goToLine(int newLine)
{
QTextCursor t(textCursor());
t.setPosition(0);
int line = 1;
while (line < newLine && t.movePosition(QTextCursor::NextBlock))
{
line++;
}
setTextCursor(t);
setFocus();
}
void
BasicEdit::keyPressEvent(QKeyEvent *e)
{
e->accept();
codeChanged = true;
QTextEdit::keyPressEvent(e);
}
void
BasicEdit::newProgram()
{
bool donew = true;
if (codeChanged) {
QMessageBox msgBox;
msgBox.setText(tr("New Program?"));
msgBox.setInformativeText(tr("Are you sure you want to completely clear this program and start a new one?"));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
donew = (msgBox.exec() == QMessageBox::Yes);
}
if (donew)
{
clear();
mainwin->setWindowTitle(tr("Untitled - BASIC-256"));
filename = "";
codeChanged = false;
}
}
void
BasicEdit::saveProgram()
{
if (filename == "")
{
filename = QFileDialog::getSaveFileName(this, tr("Save file as"), ".", tr("BASIC-256 File ") + "(*.kbs);;" + tr("Any File ") + "(*.*)");
}
if (filename != "")
{
QRegExp rx("\\.[^\\/]*$");
if (rx.indexIn(filename) == -1)
{
filename += ".kbs";
}
QFile f(filename);
bool dooverwrite = true;
if (f.exists()) {
QMessageBox msgBox;
msgBox.setText(tr("The file ") + filename + tr(" already exists."));
msgBox.setInformativeText(tr("Do you want to overwrite?"));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
dooverwrite = (msgBox.exec() == QMessageBox::Yes);
}
if (dooverwrite) {
f.open(QIODevice::WriteOnly | QIODevice::Truncate);
f.write(this->document()->toPlainText().toUtf8());
f.close();
QFileInfo fi(f);
mainwin->setWindowTitle(fi.fileName() + tr(" - BASIC-256"));
QDir::setCurrent(fi.absolutePath());
codeChanged = false;
addFileToRecentList(filename);
}
}
}
void BasicEdit::addFileToRecentList(QString fn) {
// keep list of recently open or saved files
// put file name at position 0 on list
QSettings settings(SETTINGSORG, SETTINGSAPP);
settings.beginGroup(SETTINGSGROUPHIST);
// if program is at top then do nothing
if (settings.value(QString::number(0), "").toString() != fn) {
// find end of scootdown
int e;
for(e=1; e<SETTINGSGROUPHISTN && settings.value(QString::number(e), "").toString() != fn; e++) {}
// scoot entries down
for (int i=e; i>=1; i--) {
settings.setValue(QString::number(i), settings.value(QString::number(i-1), ""));
}
settings.setValue(QString::number(0), fn);
}
settings.endGroup();
// print out for debugging
//settings.beginGroup(SETTINGSGROUPHIST);
//for (int i=0; i<SETTINGSGROUPHISTN; i++) {
// printf("%i %s\n", i, settings.value(QString::number(i), "").toString().toUtf8().data());
//}
//settings.endGroup();
}
void
BasicEdit::saveAsProgram()
{
QString tempfilename = QFileDialog::getSaveFileName(this, tr("Save file as"), ".", tr("BASIC-256 File ")+ "(*.kbs);;" + tr("Any File ")+ "(*.*)");
if (tempfilename != "")
{
filename = tempfilename;
saveProgram();
}
}
void
BasicEdit::loadProgram()
{
QString s = QFileDialog::getOpenFileName(this, tr("Open a file"), ".", tr("BASIC-256 file ") + "(*.kbs);;" + tr("Any File ") + "(*.*)");
loadFile(s);
}
void BasicEdit::loadRecent0() { loadRecent(0); }
void BasicEdit::loadRecent1() { loadRecent(1); }
void BasicEdit::loadRecent2() { loadRecent(2); }
void BasicEdit::loadRecent3() { loadRecent(3); }
void BasicEdit::loadRecent4() { loadRecent(4); }
void BasicEdit::loadRecent5() { loadRecent(5); }
void BasicEdit::loadRecent6() { loadRecent(6); }
void BasicEdit::loadRecent7() { loadRecent(7); }
void BasicEdit::loadRecent8() { loadRecent(8); }
void
BasicEdit::loadRecent(int i)
{
QSettings settings(SETTINGSORG, SETTINGSAPP);
settings.beginGroup(SETTINGSGROUPHIST);
loadFile(settings.value(QString::number(i), "").toString());
settings.endGroup();
}
void
BasicEdit::loadFile(QString s)
{
if (s != NULL)
{
bool doload = true;
if (codeChanged) {
QMessageBox msgBox;
msgBox.setText(tr("Program modifications have not been saved."));
msgBox.setInformativeText(tr("Do you want to discard your changes?"));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
doload = (msgBox.exec() == QMessageBox::Yes);
}
if (doload) {
QFile f(s);
f.open(QIODevice::ReadOnly);
QByteArray ba = f.readAll();
this->setPlainText(QString::fromUtf8(ba.data()));
f.close();
filename = s;
QFileInfo fi(f);
mainwin->setWindowTitle(fi.fileName() + tr(" - BASIC-256"));
QDir::setCurrent(fi.absolutePath());
codeChanged = false;
addFileToRecentList(s);
}
}
}
void BasicEdit::slotPrint()
{
QTextDocument *document = this->document();
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer, this);
dialog->setWindowTitle(QObject::tr("Print Code"));
if (dialog->exec() == QDialog::Accepted)
{
if ((printer.printerState() != QPrinter::Error) && (printer.printerState() != QPrinter::Aborted))
{
document->print(&printer);
}
else
{
QMessageBox::warning(this, QObject::tr("Print Error"), QObject::tr("Unable to carry out printing.\nPlease check your printer settings."));
}
}
}
void BasicEdit::beautifyProgram()
{
QString program;
QStringList lines;
const int TAB = 3;
int indent = 0;
bool indentThisLine = true;
bool increaseIndent = false;
bool decreaseIndent = false;
program = this->document()->toPlainText();
lines = program.split(QRegExp("\\n"));
for (int i = 0; i < lines.size(); i++) {
QString line = lines.at(i);
line = line.trimmed();
if (line.contains(QRegExp("^\\S+[:]"))) {
// label - one line no indent
indentThisLine = false;
} else if (line.contains(QRegExp("^[Ff][Oo][Rr]\\s"))) {
// for - indent next (block of code)
increaseIndent = true;
} else if (line.contains(QRegExp("^[Nn][Ee][Xx][Tt]\\s"))) {
// next - come out of block - reduce indent
decreaseIndent = true;
} else if (line.contains(QRegExp("^[Ii][Ff]\\s.+\\s[Tt][Hh][Ee][Nn]$"))) {
// if/then (NOTHING FOLLOWING) - indent next (block of code)
increaseIndent = true;
} else if (line.contains(QRegExp("^[Ee][Ll][Ss][Ee]$"))) {
// else - come out of block and start new block
decreaseIndent = true;
increaseIndent = true;
} else if (line.contains(QRegExp("^[Ee][Nn][Dd]\\s*[Ii][Ff]$"))) {
// end if - come out of block - reduce indent
decreaseIndent = true;
} else if (line.contains(QRegExp("^[Ww][Hh][Ii][Ll][Ee]\\s"))) {
// while - indent next (block of code)
increaseIndent = true;
} else if (line.contains(QRegExp("^[Ee][Nn][Dd]\\s*[Ww][Hh][Ii][Ll][Ee]$"))) {
// endwhile - come out of block
decreaseIndent = true;
} else if (line.contains(QRegExp("^[Dd][Oo]$"))) {
// do - indent next (block of code)
increaseIndent = true;
} else if (line.contains(QRegExp("^[Uu][Nn][Tt][Ii][Ll]\\s"))) {
// until - come out of block
decreaseIndent = true;
}
//
if (decreaseIndent) {
indent -= TAB;
if (indent<0) indent=0;
decreaseIndent = false;
}
if (indentThisLine) {
line = QString(indent, QChar(' ')) + line;
} else {
indentThisLine = true;
}
if (increaseIndent) {
indent += TAB;
increaseIndent = false;
}
//
lines.replace(i, line);
}
this->setPlainText(lines.join("\n"));
codeChanged = true;
}
void BasicEdit::findString(QString s, bool reverse, bool casesens)
{
QTextDocument::FindFlags flag;
if (reverse) flag |= QTextDocument::FindBackward;
if (casesens) flag |= QTextDocument::FindCaseSensitively;
if (!find(s, flag)) {
QMessageBox msgBox;
msgBox.setText(tr("String not found."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
}
void BasicEdit::replaceString(QString from, QString to, bool casesens, bool doall)
{
bool doone = true;
QTextDocument::FindFlags flag;
if (casesens) flag |= QTextDocument::FindCaseSensitively;
while (doone) {
if (from.compare(this->textCursor().selectedText(),(casesens ? Qt::CaseSensitive : Qt::CaseInsensitive))!=0) {
if (!find(from, flag)) {
QMessageBox msgBox;
msgBox.setText(tr("Replace completed."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
doall = false;
}
} else {
this->textCursor().clearSelection();
this->textCursor().insertText(to);
codeChanged = true;
}
doone = doall;
}
}
|