|
| 1 | +#include "code.h" |
| 2 | + |
| 3 | +#include <Qsci/qscilexerlua.h> |
| 4 | +#include <Qsci/qsciapis.h> |
| 5 | + |
| 6 | +#include <QAction> |
| 7 | +#include <QFileDialog> |
| 8 | +#include <QTextStream> |
| 9 | +#include <QMessageBox> |
| 10 | +#include <QKeyEvent> |
| 11 | +#include <QDebug> |
| 12 | + |
| 13 | +Code::Code(QSettings *s, QWidget *parent) |
| 14 | + : QsciScintilla(parent) |
| 15 | +{ |
| 16 | + QString family = s->value("editor/fontfamily", "Courier").toString(); |
| 17 | + uint size = s->value("editor/fontsize", 10).toUInt(); |
| 18 | + |
| 19 | + QsciLexerLua *l = new QsciLexerLua(this); |
| 20 | + |
| 21 | + QsciAPIs* api = new QsciAPIs(l); |
| 22 | + |
| 23 | + //l->setColor(Qt::blue, QsciLexerLua::Keyword); |
| 24 | + //l->setColor(QColor(0xff, 0x80, 0x00), QsciLexerLua::Number); |
| 25 | + //l->setColor(QColor(0xff, 0x80, 0x00), QsciLexerLua::); |
| 26 | + //l->setPaper(QColor(0x0, 0x0, 128)); |
| 27 | + |
| 28 | + /* |
| 29 | + QFont font = QFont(); |
| 30 | + font.setFamily(family); |
| 31 | + //font.setFixedPitch(true); |
| 32 | + font.setPointSize(size); |
| 33 | +
|
| 34 | + */ |
| 35 | + api->prepare(); |
| 36 | + //l->setDefaultFont(font); |
| 37 | + setLexer(l); |
| 38 | + |
| 39 | + /* |
| 40 | + QsciScintilla::setFont(font); |
| 41 | + setMarginsFont(font); |
| 42 | + */ |
| 43 | + |
| 44 | + // Line Highlight |
| 45 | + setCaretLineVisible(true); |
| 46 | + //setCaretForegroundColor(QColor("yellow")); |
| 47 | + //setCaretLineBackgroundColor(QColor("blue")); |
| 48 | + |
| 49 | + // # Margin 0 is used for line numbers |
| 50 | + //QFontMetrics fontmetrics = QFontMetrics(font); |
| 51 | + //setMarginsFont(font); |
| 52 | + //setMarginWidth(0, fontmetrics.width("0000") + 6); |
| 53 | + setMarginLineNumbers(0, true); |
| 54 | + //setMarginsBackgroundColor(QColor("blue")); |
| 55 | + |
| 56 | + // setEdgeMode(QsciScintilla::EdgeLine); setEdgeColumn(80); setEdgeColor(QColor("#FF0000")); |
| 57 | + |
| 58 | + setFolding(QsciScintilla::BoxedTreeFoldStyle); |
| 59 | + setBraceMatching(QsciScintilla::SloppyBraceMatch); |
| 60 | + |
| 61 | + //setFoldMarginColors(QColor("#99CC66"), QColor("#333300")); |
| 62 | + |
| 63 | + SendScintilla(SCI_SETHSCROLLBAR, 0); |
| 64 | + setBraceMatching(SloppyBraceMatch); |
| 65 | + |
| 66 | + QAction* a = new QAction(tr("Open a file"), this); |
| 67 | + a->setShortcut(tr("Ctrl+1")); |
| 68 | + a->setShortcutContext(Qt::WidgetShortcut); |
| 69 | + addAction(a); |
| 70 | + connect(a, SIGNAL(triggered()), this, SLOT(load())); |
| 71 | + |
| 72 | + a = new QAction(tr("Save file"), this); |
| 73 | + a->setShortcut(tr("Ctrl+2")); |
| 74 | + a->setShortcutContext(Qt::WidgetShortcut); |
| 75 | + addAction(a); |
| 76 | + connect(a, SIGNAL(triggered()), this, SLOT(save())); |
| 77 | + |
| 78 | + a = new QAction(tr("Save to file"), this); |
| 79 | + a->setShortcut(tr("Ctrl+3")); |
| 80 | + a->setShortcutContext(Qt::WidgetShortcut); |
| 81 | + addAction(a); |
| 82 | + connect(a, SIGNAL(triggered()), this, SLOT(saveAs())); |
| 83 | +} |
| 84 | + |
| 85 | +bool Code::load(QString filename) { |
| 86 | + if (filename.isEmpty()) { |
| 87 | + filename = QString(".lua"); |
| 88 | + filename = QFileDialog::getOpenFileName(this, "Open a script", |
| 89 | + script_filename, "Lua source (*.lua);;All files (*)"); |
| 90 | + if (filename.isEmpty()) |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + QFile file(filename); |
| 95 | + if (!file.open(QIODevice::ReadOnly)) { |
| 96 | + |
| 97 | + /* |
| 98 | + QMessageBox::warning(this, tr("Application error"), |
| 99 | + tr("Cannot read file %1\n").arg(filename)); |
| 100 | + */ |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + QTextStream os(&file); |
| 105 | + QString p = os.readAll(); |
| 106 | + file.close(); |
| 107 | + |
| 108 | + setText(p); |
| 109 | + |
| 110 | + script_filename = filename; |
| 111 | + emit scriptLoaded(); |
| 112 | + |
| 113 | + return true; |
| 114 | +} |
| 115 | + |
| 116 | +bool Code::saveAs(QString filename) { |
| 117 | + if (filename.isEmpty()) { |
| 118 | + QFileDialog dialog(this, tr("Save a script"), script_filename, |
| 119 | + tr("Lua source (*.lua);;All files (*)")); |
| 120 | + dialog.setAcceptMode(QFileDialog::AcceptSave); |
| 121 | + dialog.setDefaultSuffix("lua"); |
| 122 | + dialog.selectFile(script_filename); |
| 123 | + |
| 124 | + if (dialog.exec()) |
| 125 | + filename = dialog.selectedFiles().first(); |
| 126 | + else |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + QFile file(filename); |
| 131 | + if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { |
| 132 | + QMessageBox::warning(this, tr("Application error"), |
| 133 | + tr("Cannot write file %1\n").arg(filename)) \ |
| 134 | + ; |
| 135 | + return false; |
| 136 | + } |
| 137 | + QTextStream os(&file); |
| 138 | + os << toPlainText(); |
| 139 | + file.close(); |
| 140 | + script_filename = filename; |
| 141 | + |
| 142 | + emit scriptSaved(); |
| 143 | + |
| 144 | + return true; |
| 145 | +} |
| 146 | + |
| 147 | +bool Code::save() { |
| 148 | + if (QString("no_name") == script_filename) { |
| 149 | + return saveAs(""); |
| 150 | + } else { |
| 151 | + return saveAs(script_filename); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +void Code::clear() { |
| 156 | + script_filename = QString(); |
| 157 | + QsciScintilla::clear(); |
| 158 | +} |
| 159 | + |
| 160 | +QString Code::toPlainText() { |
| 161 | + return text(); |
| 162 | +} |
| 163 | + |
| 164 | +void Code::appendLine(QString line) { |
| 165 | + append("\n" + line); |
| 166 | + SendScintilla(QsciScintilla::SCI_GOTOLINE, toPlainText().lastIndexOf("\n") + 1); |
| 167 | +} |
| 168 | + |
| 169 | +void Code::setFont(QString family, int size) { |
| 170 | + // qDebug() << " setFont " << family << size; |
| 171 | + //XXX set Lexer font here, too. |
| 172 | + //QFont *f = new QFont(family, size); |
| 173 | + //f->setFixedPitch(true); |
| 174 | + //QsciScintilla::setFont(*f); |
| 175 | + //QWidget::setFont(*f); |
| 176 | +} |
| 177 | + |
| 178 | +void Code::keyPressEvent(QKeyEvent *e) { |
| 179 | + QsciScintilla::keyPressEvent(e); |
| 180 | + |
| 181 | + if (e->isAccepted()) { |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + int keyInt = e->key(); |
| 186 | + Qt::Key key = static_cast<Qt::Key>(keyInt); |
| 187 | + |
| 188 | + if (key == Qt::Key_unknown) { |
| 189 | + qDebug() << "Unknown key from a macro probably"; |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + // the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta. |
| 194 | + if (key == Qt::Key_Control || |
| 195 | + key == Qt::Key_Shift || |
| 196 | + key == Qt::Key_Alt || |
| 197 | + key == Qt::Key_Meta) |
| 198 | + { |
| 199 | + // qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta"; |
| 200 | + // qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText); |
| 201 | + // return; |
| 202 | + } |
| 203 | + |
| 204 | + // check for a combination of user clicks |
| 205 | + Qt::KeyboardModifiers modifiers = e->modifiers(); |
| 206 | + QString keyText = e->text(); |
| 207 | + // if the keyText is empty than it's a special key like F1, F5, ... |
| 208 | + // qDebug() << "Pressed Key:" << keyText; |
| 209 | + |
| 210 | + QList<Qt::Key> modifiersList; |
| 211 | + if (modifiers & Qt::ShiftModifier) |
| 212 | + keyInt += Qt::SHIFT; |
| 213 | + if (modifiers & Qt::ControlModifier) |
| 214 | + keyInt += Qt::CTRL; |
| 215 | + if (modifiers & Qt::AltModifier) |
| 216 | + keyInt += Qt::ALT; |
| 217 | + if (modifiers & Qt::MetaModifier) |
| 218 | + keyInt += Qt::META; |
| 219 | + |
| 220 | + QString seq = QKeySequence(keyInt).toString(QKeySequence::NativeText); |
| 221 | + |
| 222 | + // qDebug() << "CodeEditor::keyPressed(" << seq << ")"; |
| 223 | + |
| 224 | + emit keyPressed(e); |
| 225 | +} |
0 commit comments