SVN commit 890998 by mecir: Finally fixed all the problems related to the range markup displayed when editing cells in either editor. BUG: 175373 M +5 -0 Selection.cpp M +6 -0 Selection.h M +4 -0 ui/CellToolBase.cpp M +2 -2 ui/CellToolBase_p.cpp M +55 -36 ui/Editors.cpp M +2 -1 ui/Editors.h --- trunk/koffice/kspread/Selection.cpp #890997:890998 @@ -952,6 +952,11 @@ return d->referenceMode; } +bool Selection::referenceSelection() const +{ + return (!d->formerSelection.isEmpty()); +} + void Selection::emitAboutToModify() { emit aboutToModify(*this); --- trunk/koffice/kspread/Selection.h #890997:890998 @@ -268,10 +268,16 @@ void setLastEditorWithFocus(Editor type); Editor lastEditorWithFocus() const; + /** Start using a reference selection instead of normal one. */ void startReferenceSelection(const Region& region = Region()); + /** End using reference selection. */ void endReferenceSelection(); + /** Enable/disable reference choosing mode. */ void setReferenceSelectionMode(bool enable); + /** Are we in reference choosing mode ? */ bool referenceSelectionMode() const; + /** Are we currently using a reference selection ? */ + bool referenceSelection() const; void emitAboutToModify(); void emitModified(); --- trunk/koffice/kspread/ui/CellToolBase.cpp #890997:890998 @@ -1228,6 +1228,10 @@ if (!d->locationComboBox) { return; } + // if we're in ref viewing mode, do nothing here + if (editor() && selection()->referenceSelection() && (!selection()->referenceSelectionMode())) + return; + const Cell cell = Cell(selection()->activeSheet(), selection()->cursor()); if (!cell) { return; --- trunk/koffice/kspread/ui/CellToolBase_p.cpp #890997:890998 @@ -945,7 +945,7 @@ void CellToolBase::Private::paintSelection(QPainter &painter, const QRectF &viewRect) { - if (q->selection()->referenceSelectionMode() || q->editor()) { + if (q->selection()->referenceSelection() || q->editor()) { return; } @@ -1064,7 +1064,7 @@ void CellToolBase::Private::paintReferenceSelection(QPainter &painter, const QRectF &viewRect) { Q_UNUSED(viewRect); - if (!q->selection()->referenceSelectionMode()) { + if (!q->selection()->referenceSelection()) { return; } // save painter state --- trunk/koffice/kspread/ui/Editors.cpp #890997:890998 @@ -963,43 +963,51 @@ if ( text[0] != '=' ) { selection()->setReferenceSelectionMode(false); +// d->checkChoice = true; + return true; } - else - { - int cur = d->textEdit->textCursor().position(); - Tokens tokens = d->highlighter->formulaTokens(); + // switch to reference selection mode if we haven't yet + selection()->startReferenceSelection(); - // switch to reference selection mode if we haven't yet - selection()->startReferenceSelection(); - if (tokens.count()) // formula not empty? + // if we don't have focus, we show highlighting, but don't do the rest + if (!hasFocus()) { + selection()->setReferenceSelectionMode(false); +// d->checkChoice = true; + return true; + } + + int cur = d->textEdit->textCursor().position(); + + Tokens tokens = d->highlighter->formulaTokens(); + + if (tokens.count()) // formula not empty? + { + Token token; + for (int i = 0; i < tokens.count(); ++i) { - Token token; - for (int i = 0; i < tokens.count(); ++i) + if (tokens[i].pos() >= cur - 1) // without '=' { - if (tokens[i].pos() >= cur - 1) // without '=' - { - break; - } - token = tokens[i]; - d->currentToken = i; + break; } + token = tokens[i]; + d->currentToken = i; + } - Token::Type type = token.type(); - if (type == Token::Operator && token.asOperator() != Token::RightPar) - { - selection()->setReferenceSelectionMode(true); - } - else if (type == Token::Cell || type == Token::Range) - { - d->length_namecell = token.text().length(); - selection()->setReferenceSelectionMode(true); - } - else - { - selection()->setReferenceSelectionMode(false); - } + Token::Type type = token.type(); + if (type == Token::Operator && token.asOperator() != Token::RightPar) + { + selection()->setReferenceSelectionMode(true); } + else if (type == Token::Cell || type == Token::Range) + { + d->length_namecell = token.text().length(); + selection()->setReferenceSelectionMode(true); + } + else + { + selection()->setReferenceSelectionMode(false); + } } // d->checkChoice = true; @@ -1145,17 +1153,17 @@ return d->textEdit->toPlainText(); } -void CellEditor::setText(const QString& text) +void CellEditor::setText(const QString& text, int cursorPos) { - setCheckChoice (false); + if (text == d->textEdit->toPlainText()) return; + d->textEdit->setPlainText(text); //Usability : It is usually more convenient if the cursor is positioned at the end of the text so it can //be quickly deleted using the backspace key // This also ensures that the caret is sized correctly for the text - setCursorPosition( text.length() ); - setCheckChoice (true); - kDebug() <<"text cursor positioned at the end"; + if ((cursorPos < 0) || cursorPos > text.length()) cursorPos = text.length(); + setCursorPosition (cursorPos); if (d->fontLength == 0) { @@ -1513,8 +1521,9 @@ d->cellTool->createEditor(false /* keep content */, false /* no focus */); } - // the Enter key is handled by the embedded editor - if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter)) { + // the Enter and Esc key are handled by the embedded editor + if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter) || + (event->key() == Qt::Key_Escape)) { d->cellTool->editor()->setFocus(); QApplication::sendEvent (d->cellTool->editor(), event); event->accept(); @@ -1528,6 +1537,16 @@ KTextEdit::keyPressEvent (event); } +void ExternalEditor::focusInEvent(QFocusEvent* event) +{ + Q_ASSERT(d->cellTool); + // when the external editor gets focus, create also the internal editor + // this in turn means that ranges will be instantly highlighted right + if (!d->cellTool->editor()) + d->cellTool->createEditor(false /* keep content */, false /* no focus */); + KTextEdit::focusInEvent(event); +} + void ExternalEditor::focusOutEvent(QFocusEvent* event) { Q_ASSERT(d->cellTool); --- trunk/koffice/kspread/ui/Editors.h #890997:890998 @@ -229,7 +229,7 @@ void modificationChanged(bool changed); public slots: - void setText(const QString& text); + void setText(const QString& text, int cursorPos = -1); private slots: void slotTextChanged(); @@ -325,6 +325,7 @@ protected: void keyPressEvent(QKeyEvent *event); + void focusInEvent(QFocusEvent *event); void focusOutEvent(QFocusEvent *event); private slots: