00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <QtGui/QMessageBox>
00043 #include <QtGui/QMessageBox>
00044 #include <QString>
00045 #include <QTextEdit>
00046 #include <QFile>
00047 #include <QFont>
00048 #include <QMenu>
00049 #include <QMenuBar>
00050 #include <QFileDialog>
00051 #include <QCoreApplication>
00052 #include <QTextDocument>
00053 #include <QTextCursor>
00054 #include <QDebug>
00055 #include <QRegExp>
00056 #include <QDir>
00057 #include <QFileInfo>
00058 #include <QStringList>
00059
00060 #include "TextEdit.h"
00061 #include "TSystem.h"
00062
00063 #include "StGeomHighlighter.h"
00064
00066
00067 TextEdit::TextEdit(QWidget *parent)
00068 : QMainWindow(parent)
00069 {
00070 setupFileMenu();
00071 setupHelpMenu();
00072 setupEditor();
00073
00074 setCentralWidget(editor);
00075 setWindowTitle(tr("Syntax Highlighter"));
00076 }
00078
00079
00080 void TextEdit::about()
00081 {
00082 QMessageBox::about(this, tr("About Syntax Highlighter"),
00083 tr("<p>The <b>Syntax Highlighter</b> example shows how " \
00084 "to perform simple syntax highlighting by subclassing " \
00085 "the QSyntaxHighlighter class and describing " \
00086 "highlighting rules using regular expressions.</p>"));
00087 }
00088
00089
00090 void TextEdit::newFile()
00091 {
00092 editor->clear();
00093 }
00094
00095
00096 void TextEdit::openFile(const QString &path)
00097 {
00098 QString fileName = path;
00099
00100 if (fileName.isNull()) {
00101 static QString filetypes = "MORTRAN Geometry (*.g);"
00102 ";STAR Geometry macro (*.C)"
00103 ";";
00104
00105 QStringList paths; paths << "." << gSystem->Getenv("STAR");
00106 QDir::setSearchPaths("geometry",paths);
00107 QFileInfo file("geometry:pams/geometry");
00108 QString defaultDir = file.exists() ? file.absoluteFilePath () : "";
00109 fileName = QFileDialog::getOpenFileName(this,
00110 tr("Open Geometry File"), defaultDir ,filetypes);
00111 }
00112
00113 if (!fileName.isEmpty()) {
00114 QFile file(fileName);
00115 if (file.open(QFile::ReadOnly | QFile::Text))
00116 editor->setPlainText(file.readAll());
00117 }
00118 }
00119
00121
00122 void TextEdit::setupEditor()
00123 {
00124 QFont font;
00125 font.setFamily("Courier");
00126 font.setFixedPitch(true);
00127 font.setPointSize(12);
00128
00129 editor = new QTextEdit;
00130 editor->setFont(font);
00131
00132 highlighter = new StGeomHighlighter(editor->document());
00133 }
00135
00136
00137 void TextEdit::setupFileMenu()
00138 {
00139 QMenu *fileMenu = new QMenu(tr("&File"), this);
00140 menuBar()->addMenu(fileMenu);
00141
00142 fileMenu->addAction(tr("&New"), this, SLOT(newFile()),
00143 QKeySequence(tr("Ctrl+N",
00144 "File|New")));
00145 fileMenu->addAction(tr("&Open..."), this, SLOT(openFile()),
00146 QKeySequence(tr("Ctrl+O",
00147 "File|Open")));
00148
00149
00150
00151 }
00152
00153
00154 void TextEdit::setupHelpMenu()
00155 {
00156 QMenu *helpMenu = new QMenu(tr("&Help"), this);
00157 menuBar()->addMenu(helpMenu);
00158
00159 helpMenu->addAction(tr("&About"), this, SLOT(about()));
00160
00161 }
00162
00163 void TextEdit::load( const QString &f )
00164 {
00165 if ( QFile::exists( f ) ) {
00166 QFile file(f);
00167 if (file.open(QFile::ReadOnly | QFile::Text)) {
00168 editor->setPlainText(file.readAll());
00169 } else {
00170 QMessageBox::critical(
00171 this,
00172 tr("Open failed"),
00173 tr("Could not open file for reading: %1").arg(QCoreApplication::translate("QFile",file.errorString()) )
00174 );
00175 }
00176 }
00177 }
00178
00179
00180 void TextEdit::findBlock(const QString & expr)
00181 {
00182 QRegExp exp(QString("^Block\\s+%1").arg(expr),Qt::CaseInsensitive);
00183 if (QTextDocument *d = editor->document() )
00184 {
00185 QTextCursor block(d);
00186 while (!block.isNull() ) {
00187 block = d->find(exp,block, QTextDocument::FindWholeWords);
00188 if ( ! block.isNull() )
00189 {
00190 QTextCursor volumeName = d->find(QRegExp("^EndBlock",Qt::CaseInsensitive),block,QTextDocument::FindWholeWords);
00191 if (! volumeName.isNull() ) {
00192 block.setPosition(volumeName.position(), QTextCursor::KeepAnchor);
00193 editor->setTextCursor(block);
00194 editor->ensureCursorVisible();
00195 break;
00196 }
00197 }
00198 break;
00199 }
00200 }
00201 }