Completer refactored

This commit is contained in:
Alexander Arin 2021-11-03 00:40:16 +03:00
parent 8efb6792a3
commit 37b929459b
4 changed files with 35 additions and 31 deletions

View File

@ -583,7 +583,7 @@ QString DataSourceManager::replaceFields(QString query, QMap<QString,QString> &a
match = rx.match(query); match = rx.match(query);
} }
} }
// TODO: Qt6 port - possible done // TODO: Qt6 port - done
#else #else
QRegExp rx(Const::FIELD_RX); QRegExp rx(Const::FIELD_RX);
if (query.contains(rx)){ if (query.contains(rx)){

View File

@ -470,7 +470,7 @@ void ReportRender::extractGroupFuntionsFromItem(ContentItemDesignIntf* contentIt
} }
} }
// TODO: Qt6 port - possible done // TODO: Qt6 port - done
#else #else
if ( contentItem && contentItem->content().contains(QRegExp("\\$S\\s*\\{.*\\}"))){ if ( contentItem && contentItem->content().contains(QRegExp("\\$S\\s*\\{.*\\}"))){
foreach(const QString &functionName, m_datasources->groupFunctionNames()){ foreach(const QString &functionName, m_datasources->groupFunctionNames()){
@ -556,7 +556,7 @@ void ReportRender::replaceGroupFunctionsInItem(ContentItemDesignIntf* contentIte
match = rx.match(content, pos + match.capturedLength()); match = rx.match(content, pos + match.capturedLength());
} }
} }
// TODO: Qt6 port - possible done // TODO: Qt6 port - done
#else #else
QRegExp rx(QString(Const::GROUP_FUNCTION_RX).arg(functionName)); QRegExp rx(QString(Const::GROUP_FUNCTION_RX).arg(functionName));
rx.setMinimal(true); rx.setMinimal(true);

View File

@ -15,7 +15,7 @@
namespace LimeReport{ namespace LimeReport{
CodeEditor::CodeEditor(QWidget *parent) CodeEditor::CodeEditor(QWidget *parent)
: QPlainTextEdit(parent), m_compleater(0) : QPlainTextEdit(parent), m_completer(0)
{ {
lineNumberArea = new LineNumberArea(this); lineNumberArea = new LineNumberArea(this);
@ -33,12 +33,13 @@ CodeEditor::CodeEditor(QWidget *parent)
void CodeEditor::setCompleter(QCompleter *value) void CodeEditor::setCompleter(QCompleter *value)
{ {
if (value) disconnect(value,0,this,0); if (value) disconnect(value,0,this,0);
m_compleater = value; m_completer = value;
if (!m_compleater) return; if (!m_completer) return;
m_compleater->setWidget(this); m_completer->setWidget(this);
m_compleater->setCompletionMode(QCompleter::PopupCompletion); m_completer->setCompletionMode(QCompleter::PopupCompletion);
m_compleater->setCaseSensitivity(Qt::CaseInsensitive); m_completer->setCaseSensitivity(Qt::CaseInsensitive);
connect(m_compleater,SIGNAL(activated(QString)),this,SLOT(insertCompletion(QString)));
connect(m_completer,SIGNAL(activated(QString)),this,SLOT(insertCompletion(QString)));
} }
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent* event) void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent* event)
@ -86,12 +87,11 @@ int CodeEditor::lineNumberAreaWidth()
void CodeEditor::keyPressEvent(QKeyEvent *e) void CodeEditor::keyPressEvent(QKeyEvent *e)
{ {
if (m_compleater && m_compleater->popup()->isVisible()) { if (m_completer && m_completer->popup()->isVisible()) {
switch (e->key()) { switch (e->key()) {
case Qt::Key_Enter: case Qt::Key_Enter:
case Qt::Key_Return: case Qt::Key_Return:
case Qt::Key_Escape: case Qt::Key_Escape:
m_compleater->popup()->close();
case Qt::Key_Tab: case Qt::Key_Tab:
case Qt::Key_Backtab: case Qt::Key_Backtab:
case Qt::Key_Right: case Qt::Key_Right:
@ -106,10 +106,10 @@ void CodeEditor::keyPressEvent(QKeyEvent *e)
} }
bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space); bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space);
if (!m_compleater || !isShortcut) QPlainTextEdit::keyPressEvent(e); if (!m_completer || !isShortcut) QPlainTextEdit::keyPressEvent(e);
const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier); const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
if (!m_compleater || (ctrlOrShift && e->text().isEmpty())) if (!m_completer || (ctrlOrShift && e->text().isEmpty()))
return; return;
bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift; bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
@ -120,36 +120,36 @@ void CodeEditor::keyPressEvent(QKeyEvent *e)
|| Const::EOW.contains(e->text().right(1))) || Const::EOW.contains(e->text().right(1)))
) )
{ {
m_compleater->popup()->hide(); m_completer->popup()->hide();
return; return;
} }
if (completionPrefix != m_compleater->completionPrefix()) { if (completionPrefix != m_completer->completionPrefix()) {
m_compleater->setCompletionPrefix(completionPrefix); m_completer->setCompletionPrefix(completionPrefix);
m_compleater->popup()->setCurrentIndex(m_compleater->completionModel()->index(0, 0)); m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0));
} }
QModelIndex ci = m_compleater->completionModel()->index(0,0); QModelIndex ci = m_completer->completionModel()->index(0,0);
if (ci.isValid() && m_compleater->completionModel()->data(ci).toString().compare(completionPrefix) == 0){ if (ci.isValid() && m_completer->completionModel()->data(ci).toString().compare(completionPrefix) == 0){
m_compleater->popup()->hide(); m_completer->popup()->hide();
return; return;
} }
QRect cr = cursorRect(); QRect cr = cursorRect();
cr.setWidth(m_compleater->popup()->sizeHintForColumn(0) cr.setWidth(m_completer->popup()->sizeHintForColumn(0)
+ m_compleater->popup()->verticalScrollBar()->sizeHint().width()); + m_completer->popup()->verticalScrollBar()->sizeHint().width());
m_compleater->complete(cr); m_completer->complete(cr);
if (!completionPrefix.isEmpty() && if (!completionPrefix.isEmpty() &&
completionPrefix.at(completionPrefix.length()-1) == '.') completionPrefix.at(completionPrefix.length()-1) == '.')
{ {
m_compleater->popup(); m_completer->popup();
} }
} }
void CodeEditor::focusInEvent(QFocusEvent *e) void CodeEditor::focusInEvent(QFocusEvent *e)
{ {
if (m_compleater) m_compleater->setWidget(this); if (m_completer) m_completer->setWidget(this);
QPlainTextEdit::focusInEvent(e); QPlainTextEdit::focusInEvent(e);
} }
@ -279,13 +279,17 @@ QChar CodeEditor::getParenthesisReverceChar(QChar parenthesisChar)
void CodeEditor::insertCompletion(const QString &completion) void CodeEditor::insertCompletion(const QString &completion)
{ {
if (m_compleater->widget() != this) if (m_completer->widget() != this)
return; return;
QTextCursor tc = textCursor(); QTextCursor tc = textCursor();
int extra = completion.length() - m_compleater->completionPrefix().length(); int extra = completion.length() - m_completer->completionPrefix().length();
//tc.movePosition(QTextCursor::Left); //tc.movePosition(QTextCursor::Left);
//tc.movePosition(QTextCursor::EndOfWord); //tc.movePosition(QTextCursor::EndOfWord);
tc.insertText(completion.right(extra)); for (int i=0; i < m_completer->completionPrefix().length(); ++i ) {
tc.deletePreviousChar();
}
tc.insertText(completion);
//tc.insertText(completion.right(extra));
setTextCursor(tc); setTextCursor(tc);
} }

View File

@ -20,7 +20,7 @@ class CodeEditor :public QPlainTextEdit
public: public:
CodeEditor(QWidget* parent=0); CodeEditor(QWidget* parent=0);
void setCompleter(QCompleter* value); void setCompleter(QCompleter* value);
QCompleter* compleater() const{ return m_compleater;} QCompleter* compleater() const{ return m_completer;}
void lineNumberAreaPaintEvent(QPaintEvent *event); void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth(); int lineNumberAreaWidth();
protected: protected:
@ -41,7 +41,7 @@ private slots:
void updateLineNumberArea(const QRect &rect, int dy); void updateLineNumberArea(const QRect &rect, int dy);
void matchParentheses(); void matchParentheses();
private: private:
QCompleter* m_compleater; QCompleter* m_completer;
QWidget *lineNumberArea; QWidget *lineNumberArea;
}; };