diff options
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp')
| -rw-r--r-- | SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp | 87 |
1 files changed, 33 insertions, 54 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp b/SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp index b3656a6..50189d4 100644 --- a/SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp +++ b/SQLiteStudio3/guiSQLiteStudio/sqleditor.cpp @@ -17,6 +17,7 @@ #include "searchtextlocator.h" #include "services/codeformatter.h" #include "sqlitestudio.h" +#include "style.h" #include "dbtree/dbtreeitem.h" #include "dbtree/dbtree.h" #include "dbtree/dbtreemodel.h" @@ -31,6 +32,7 @@ #include <QScrollBar> #include <QFileDialog> #include <QtConcurrent/QtConcurrent> +#include <QStyle> CFG_KEYS_DEFINE(SqlEditor) @@ -69,10 +71,9 @@ void SqlEditor::init() connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); connect(this, SIGNAL(textChanged()), this, SLOT(checkContentSize())); connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(cursorMoved())); - connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); updateLineNumberAreaWidth(); - highlightCurrentLine(); + highlightCurrentCursorContext(); completer = new CompleterWindow(this); connect(completer, SIGNAL(accepted()), this, SLOT(completeSelected())); @@ -87,11 +88,11 @@ void SqlEditor::init() connect(autoCompleteTrigger, SIGNAL(triggered()), this, SLOT(checkForAutoCompletion())); queryParserTrigger = new LazyTrigger(queryParserDelay, this); - connect(autoCompleteTrigger, SIGNAL(triggered()), this, SLOT(parseContents())); + connect(queryParserTrigger, SIGNAL(triggered()), this, SLOT(parseContents())); connect(this, SIGNAL(textChanged()), this, SLOT(scheduleQueryParser())); - queryParser = new Parser(Dialect::Sqlite3); + queryParser = new Parser(); connect(this, &QWidget::customContextMenuRequested, this, &SqlEditor::customContextMenuRequested); connect(CFG_UI.Fonts.SqlEditor, SIGNAL(changed(QVariant)), this, SLOT(changeFont(QVariant))); @@ -215,8 +216,7 @@ bool SqlEditor::handleValidObjectContextMenu(const QPoint& pos) if (!obj) return false; - Dialect dialect = getDialect(); - QString objName = stripObjName(toPlainText().mid(obj->from, (obj->to - obj->from + 1)), dialect); + QString objName = stripObjName(toPlainText().mid(obj->from, (obj->to - obj->from + 1))); validObjContextMenu->clear(); @@ -492,12 +492,12 @@ void SqlEditor::completeSelected() ExpectedTokenPtr token = completer->getSelected(); QString value = token->value; if (token->needsWrapping()) - value = wrapObjIfNeeded(value, getDialect()); + value = wrapObjIfNeeded(value); if (!token->prefix.isNull()) { value.prepend("."); - value.prepend(wrapObjIfNeeded(token->prefix, getDialect())); + value.prepend(wrapObjIfNeeded(token->prefix)); } insertPlainText(value); @@ -508,7 +508,7 @@ void SqlEditor::checkForAutoCompletion() if (!db || !autoCompletion || deletionKeyPressed || !richFeaturesEnabled) return; - Lexer lexer(getDialect()); + Lexer lexer; QString sql = toPlainText(); int curPos = textCursor().position(); TokenList tokens = lexer.tokenize(sql.left(curPos)); @@ -546,11 +546,6 @@ void SqlEditor::refreshValidObjects() }); } -Dialect SqlEditor::getDialect() -{ - return !db ? Dialect::Sqlite3 : db->getDialect(); -} - void SqlEditor::setObjectLinks(bool enabled) { objectLinksEnabled = enabled; @@ -579,7 +574,7 @@ void SqlEditor::clearDbObjects() void SqlEditor::lineNumberAreaPaintEvent(QPaintEvent* event) { QPainter painter(lineNumberArea); - painter.fillRect(event->rect(), CFG_UI.Colors.SqlEditorLineNumAreaBg.get()); + painter.fillRect(event->rect(), STYLE->extendedPalette().editorLineBase()); QTextBlock block = firstVisibleBlock(); int blockNumber = block.blockNumber(); int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top(); @@ -589,7 +584,7 @@ void SqlEditor::lineNumberAreaPaintEvent(QPaintEvent* event) if (block.isVisible() && bottom >= event->rect().top()) { QString number = QString::number(blockNumber + 1); - painter.setPen(Qt::black); + painter.setPen(style()->standardPalette().text().color()); painter.drawText(0, top, lineNumberArea->width()-2, fontMetrics().height(), Qt::AlignRight, number); } @@ -610,27 +605,15 @@ int SqlEditor::lineNumberAreaWidth() digits++; } - int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; + int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits; return space; } -void SqlEditor::highlightParenthesis() +void SqlEditor::highlightParenthesis(QList<QTextEdit::ExtraSelection>& selections) { if (!richFeaturesEnabled) return; - // Clear extra selections - QList<QTextEdit::ExtraSelection> selections = extraSelections(); - - // Just keep "current line" highlighting - QMutableListIterator<QTextEdit::ExtraSelection> it(selections); - while (it.hasNext()) - { - if (!it.next().format.property(QTextFormat::FullWidthSelection).toBool()) - it.remove(); - } - setExtraSelections(selections); - // Find out parenthesis under the cursor int curPos = textCursor().position(); TextBlockData* data = dynamic_cast<TextBlockData*>(textCursor().block().userData()); @@ -664,6 +647,13 @@ void SqlEditor::highlightParenthesis() // Mark new match markMatchedParenthesis(thePar->position, matchedPar->position, selections); +} + +void SqlEditor::highlightCurrentCursorContext() +{ + QList<QTextEdit::ExtraSelection> selections; + highlightCurrentLine(selections); + highlightParenthesis(selections); setExtraSelections(selections); } @@ -671,7 +661,8 @@ void SqlEditor::markMatchedParenthesis(int pos1, int pos2, QList<QTextEdit::Extr { QTextEdit::ExtraSelection selection; - selection.format.setBackground(CFG_UI.Colors.SqlEditorParenthesisBg.get()); + selection.format.setBackground(style()->standardPalette().windowText()); + selection.format.setForeground(style()->standardPalette().window()); QTextCursor cursor = textCursor(); @@ -841,11 +832,6 @@ void SqlEditor::parseContents() if (!richFeaturesEnabled) return; - // Updating dialect according to current database (if any) - Dialect dialect = Dialect::Sqlite3; - if (db && db->isValid()) - dialect = db->getDialect(); - QString sql = toPlainText(); if (!virtualSqlExpression.isNull()) { @@ -855,7 +841,6 @@ void SqlEditor::parseContents() sql = virtualSqlExpression.arg(sql); } - queryParser->setDialect(dialect); if (richFeaturesEnabled) { queryParser->parse(sql); @@ -863,7 +848,6 @@ void SqlEditor::parseContents() checkForSyntaxErrors(); highlighter->rehighlight(); } - } void SqlEditor::checkForSyntaxErrors() @@ -903,7 +887,6 @@ void SqlEditor::checkForValidObjects() return; QMutexLocker lock(&objectsInNamedDbMutex); - Dialect dialect = db->getDialect(); QList<SqliteStatement::FullObject> fullObjects; QString dbName; for (SqliteQueryPtr query : queryParser->getQueries()) @@ -911,18 +894,18 @@ void SqlEditor::checkForValidObjects() fullObjects = query->getContextFullObjects(); for (const SqliteStatement::FullObject& fullObj : fullObjects) { - dbName = fullObj.database ? stripObjName(fullObj.database->value, dialect) : "main"; + dbName = fullObj.database ? stripObjName(fullObj.database->value) : "main"; if (!objectsInNamedDb.contains(dbName)) continue; if (fullObj.type == SqliteStatement::FullObject::DATABASE) { // Valid db name - addDbObject(sqlIndex(fullObj.database->start), sqlIndex(fullObj.database->end), QString::null); + addDbObject(sqlIndex(fullObj.database->start), sqlIndex(fullObj.database->end), QString()); continue; } - if (!objectsInNamedDb[dbName].contains(stripObjName(fullObj.object->value, dialect))) + if (!objectsInNamedDb[dbName].contains(stripObjName(fullObj.object->value))) continue; // Valid object name @@ -999,22 +982,18 @@ void SqlEditor::updateLineNumberAreaWidth() setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); } -void SqlEditor::highlightCurrentLine() +void SqlEditor::highlightCurrentLine(QList<QTextEdit::ExtraSelection>& selections) { - QList<QTextEdit::ExtraSelection> selections; - if (!isReadOnly() && isEnabled()) { QTextEdit::ExtraSelection selection; - selection.format.setBackground(CFG_UI.Colors.SqlEditorCurrentLineBg.get()); + selection.format.setBackground(STYLE->extendedPalette().editorLineBase()); selection.format.setProperty(QTextFormat::FullWidthSelection, true); selection.cursor = textCursor(); selection.cursor.clearSelection(); selections.append(selection); } - - setExtraSelections(selections); } void SqlEditor::updateLineNumberArea(const QRect& rect, int dy) @@ -1036,8 +1015,7 @@ void SqlEditor::updateLineNumberArea(const QRect& rect, int dy) void SqlEditor::cursorMoved() { - highlightParenthesis(); - + highlightCurrentCursorContext(); if (!cursorMovingByLocator) { textLocator->setStartPosition(textCursor().position()); @@ -1321,6 +1299,7 @@ void SqlEditor::changeFont(const QVariant& font) void SqlEditor::configModified() { highlighter->rehighlight(); + highlightCurrentCursorContext(); } void SqlEditor::toggleComment() @@ -1503,7 +1482,7 @@ void SqlEditor::mousePressEvent(QMouseEvent* e) if (obj && e->button() == Qt::LeftButton) { QString objName = toPlainText().mid(obj->from, (obj->to - obj->from + 1)); - openObject(obj->dbName, stripObjName(objName, getDialect())); + openObject(obj->dbName, stripObjName(objName)); } } @@ -1597,7 +1576,7 @@ void SqlEditor::setVirtualSqlExpression(const QString& value) if (virtualSqlOffset == -1) { virtualSqlOffset = 0; - virtualSqlExpression = QString::null; + virtualSqlExpression = QString(); qWarning() << "Tried to set invalid virtualSqlExpression for SqlEditor. Ignored."; return; } @@ -1657,8 +1636,8 @@ void SqlEditor::LineNumberArea::paintEvent(QPaintEvent* event) void SqlEditor::changeEvent(QEvent* e) { - if (e->type() == QEvent::EnabledChange) - highlightCurrentLine(); +// if (e->type() == QEvent::EnabledChange) +// highlightCurrentLine(); QPlainTextEdit::changeEvent(e); } |
