fixes and cleanups for text editing

This commit is contained in:
ws 2013-02-14 16:27:36 +01:00
parent db47bf6fac
commit 4e3954808a
8 changed files with 61 additions and 79 deletions

View file

@ -430,7 +430,7 @@ void Harmony::setHarmony(const QString& s)
qreal Harmony::baseLine() const
{
return (_editMode || textList.isEmpty()) ? Text::baseLine() : 0.0;
return (editMode() || textList.isEmpty()) ? Text::baseLine() : 0.0;
}
//---------------------------------------------------------
@ -529,7 +529,7 @@ bool Harmony::isEmpty() const
void Harmony::layout()
{
if (_editMode || textList.isEmpty())
if (editMode() || textList.isEmpty())
Text::layout1();
else {
// textStyle().layout(this);
@ -588,7 +588,7 @@ QPainterPath Harmony::shape() const
void Harmony::draw(QPainter* painter) const
{
painter->setPen(curColor());
if (_editMode || textList.isEmpty()) {
if (editMode() || textList.isEmpty()) {
Text::draw(painter);
return;
}

View file

@ -45,10 +45,6 @@ SimpleText::SimpleText(const SimpleText& st)
_editMode = false;
}
SimpleText::~SimpleText()
{
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
@ -76,7 +72,7 @@ QRectF SimpleText::cursorRect() const
{
QFontMetricsF fm(_textStyle.fontPx(spatium()));
int line = _cursor.line;
qreal lh = lineHeight();
// qreal lh = lineHeight();
QPointF pt = _layout[line].pos;
qreal xo = fm.width(_layout[line].text.left(_cursor.column));
@ -313,7 +309,6 @@ void SimpleText::startEdit(MuseScoreView*, const QPointF& pt)
{
_cursor.line = 0;
_cursor.column = 0;
_editMode = true;
setCursor(pt);
}
@ -323,7 +318,6 @@ void SimpleText::startEdit(MuseScoreView*, const QPointF& pt)
void SimpleText::endEdit()
{
_editMode = false;
static const qreal w = 2.0;
score()->addRefresh(canvasBoundingRect().adjusted(-w, -w, w, w));
_text.clear();
@ -497,19 +491,31 @@ bool SimpleText::movePosition(QTextCursor::MoveOperation op,
{
switch(op) {
case QTextCursor::Left:
if (_cursor.column == 0)
return false;
if (curLine().at(_cursor.column-1).isLowSurrogate())
if (_cursor.column == 0) {
if (_cursor.line == 0)
return false;
--_cursor.line;
_cursor.column = curLine().size();
}
else {
if (curLine().at(_cursor.column-1).isLowSurrogate())
--_cursor.column;
--_cursor.column;
--_cursor.column;
}
break;
case QTextCursor::Right:
if (_cursor.column >= curLine().size())
return false;
if (curLine().at(_cursor.column).isHighSurrogate())
if (_cursor.column >= curLine().size()) {
if (_cursor.line >= _layout.size()-1)
return false;
++_cursor.line;
_cursor.column = 0;
}
else {
if (curLine().at(_cursor.column).isHighSurrogate())
++_cursor.column;
++_cursor.column;
++_cursor.column;
}
break;
case QTextCursor::Up:
@ -529,7 +535,7 @@ bool SimpleText::movePosition(QTextCursor::MoveOperation op,
break;
case QTextCursor::Start:
_cursor.line = 0;
_cursor.line = 0;
_cursor.column = 0;
break;
@ -537,6 +543,7 @@ bool SimpleText::movePosition(QTextCursor::MoveOperation op,
_cursor.line = _layout.size() - 1;
_cursor.column = curLine().size();
break;
default:
qDebug("SimpleText::movePosition: not implemented");
return false;
@ -544,24 +551,6 @@ bool SimpleText::movePosition(QTextCursor::MoveOperation op,
return true;
}
//---------------------------------------------------------
// moveCursorToEnd
//---------------------------------------------------------
void SimpleText::moveCursorToEnd()
{
printf("moveCursorToEnd\n");
}
//---------------------------------------------------------
// moveCursor
//---------------------------------------------------------
void SimpleText::moveCursor(int col)
{
printf("moveCursor\n");
}
//---------------------------------------------------------
// addChar
//---------------------------------------------------------
@ -608,5 +597,6 @@ bool SimpleText::setCursor(const QPointF& p, QTextCursor::MoveMode mode)
}
}
score()->setUpdateAll(true);
return true;
}

View file

@ -66,11 +66,12 @@ class SimpleText : public Element {
void drawFrame(QPainter* painter) const;
QColor textColor() const;
void layoutFrame();
virtual void draw(QPainter*) const;
public:
SimpleText(Score*);
SimpleText(const SimpleText&);
~SimpleText();
~SimpleText() {}
SimpleText &operator=(const SimpleText&);
@ -81,7 +82,8 @@ class SimpleText : public Element {
void setText(const QString& s);
QString getText() const;
virtual void draw(QPainter*) const;
bool editMode() const { return _editMode; }
void setEditMode(bool val) { _editMode = val; }
virtual void layout();
qreal lineSpacing() const;
@ -107,8 +109,6 @@ class SimpleText : public Element {
void startEdit(MuseScoreView*, const QPointF&);
void endEdit();
bool edit(MuseScoreView*, int, int key, Qt::KeyboardModifiers, const QString&);
void moveCursorToEnd();
void moveCursor(int col);
void addChar(int code);
bool deletePreviousChar();
bool deleteChar();
@ -116,6 +116,8 @@ class SimpleText : public Element {
QTextCursor::MoveMode mode = QTextCursor::MoveAnchor);
bool setCursor(const QPointF& p,
QTextCursor::MoveMode mode = QTextCursor::MoveAnchor);
void moveCursorToEnd() { movePosition(QTextCursor::Start); };
void moveCursorToStart() { movePosition(QTextCursor::End); };
};
#endif

View file

@ -91,7 +91,6 @@ Text::Text(Score* s)
{
setFlag(ELEMENT_MOVABLE, true);
_doc = 0;
_editMode = false;
_styleIndex = TEXT_STYLE_DEFAULT;
}
@ -103,7 +102,6 @@ Text::Text(const Text& e)
else
_doc = 0;
_styleIndex = e._styleIndex;
_editMode = false;
}
Text::~Text()
@ -121,7 +119,6 @@ void Text::setText(const QString& s)
SimpleText::setText(s);
else
setUnstyledText(s);
textChanged();
}
//---------------------------------------------------------
@ -151,7 +148,6 @@ void Text::setUnstyledText(const QString& s)
tf.setFont(textStyle().font(spatium()));
c.setBlockCharFormat(tf);
c.insertText(s);
textChanged();
}
//---------------------------------------------------------
@ -172,7 +168,6 @@ void Text::setHtml(const QString& s)
setUnstyled();
_doc->clear();
_doc->setHtml(s);
textChanged();
}
//---------------------------------------------------------
@ -604,11 +599,11 @@ void Text::spatiumChanged(qreal oldVal, qreal newVal)
void Text::startEdit(MuseScoreView* view, const QPointF& p)
{
setEditMode(true);
if (styled()) {
SimpleText::startEdit(view, p);
return;
}
_editMode = true;
_cursor = new QTextCursor(_doc);
_cursor->setVisualNavigation(true);
setCursor(p);
@ -628,8 +623,8 @@ bool Text::edit(MuseScoreView* view, int grip, int key, Qt::KeyboardModifiers mo
return SimpleText::edit(view, grip, key, modifiers, s);
if (MScore::debugMode)
qDebug("Text::edit(%p) key 0x%x mod 0x%x\n", this, key, int(modifiers));
if (!_editMode || !_cursor) {
qDebug("Text::edit(%p): not in edit mode: %d %p", this, _editMode, _cursor);
if (!editMode() || !_cursor) {
qDebug("Text::edit(%p): not in edit mode: %d %p", this, editMode(), _cursor);
return false;
}
score()->setLayoutAll(type() == INSTRUMENT_NAME);
@ -806,6 +801,21 @@ void Text::replaceSpecialChars()
}
}
//---------------------------------------------------------
// moveCursorToStart
//---------------------------------------------------------
void Text::moveCursorToStart()
{
if (styled()) {
SimpleText::moveCursorToStart();
return;
}
if (_cursor)
_cursor->movePosition(QTextCursor::Start);
}
//---------------------------------------------------------
// moveCursorToEnd
//---------------------------------------------------------
@ -821,21 +831,6 @@ void Text::moveCursorToEnd()
_cursor->movePosition(QTextCursor::End);
}
//---------------------------------------------------------
// moveCursor
//---------------------------------------------------------
void Text::moveCursor(int col)
{
if (styled()) {
SimpleText::moveCursor(col);
return;
}
if (_cursor)
_cursor->setPosition(col);
}
//---------------------------------------------------------
// shape
//---------------------------------------------------------
@ -1308,7 +1303,7 @@ void Text::setTextStyleType(int st)
_styleIndex = st;
if (st != TEXT_STYLE_UNKNOWN)
setTextStyle(score()->textStyle(st));
if (_doc && !_doc->isEmpty() && !_editMode) {
if (_doc && !_doc->isEmpty() && !editMode()) {
SimpleText::setText(_doc->toPlainText());
delete _doc;
_doc = 0;
@ -1324,11 +1319,11 @@ void Text::setUnstyled()
if (!styled())
return;
_styleIndex = TEXT_STYLE_UNSTYLED;
if (_editMode)
return;
createDoc();
if (!SimpleText::isEmpty())
setUnstyledText(SimpleText::getText());
if (editMode())
_cursor = new QTextCursor(_doc);
}
//---------------------------------------------------------
@ -1355,13 +1350,12 @@ QTextCursor* Text::startCursorEdit()
void Text::endEdit()
{
setEditMode(false);
if (styled())
SimpleText::endEdit();
else {
_editMode = false;
endCursorEdit();
layoutEdit();
textChanged();
}
}

View file

@ -40,7 +40,6 @@ class Text : public SimpleText {
bool isSimpleText() const;
protected:
bool _editMode;
static QTextCursor* _cursor;
bool setCursor(const QPointF& p, QTextCursor::MoveMode mm = QTextCursor::MoveAnchor);
@ -49,6 +48,8 @@ class Text : public SimpleText {
Text(const Text&);
~Text();
virtual void draw(QPainter*) const;
Text &operator=(const Text&);
virtual Text* clone() const { return new Text(*this); }
virtual ElementType type() const { return TEXT; }
@ -86,8 +87,6 @@ class Text : public SimpleText {
void setBold(bool);
void setSize(qreal);
virtual void draw(QPainter*) const;
virtual void startEdit(MuseScoreView*, const QPointF&);
virtual bool edit(MuseScoreView*, int grip, int key, Qt::KeyboardModifiers, const QString&);
QTextCursor* startCursorEdit();
@ -105,8 +104,8 @@ class Text : public SimpleText {
virtual bool mousePress(const QPointF&, QMouseEvent* ev);
qreal lineSpacing() const;
qreal lineHeight() const;
void moveCursorToStart();
void moveCursorToEnd();
void moveCursor(int val);
virtual QLineF dragAnchor() const;
@ -118,7 +117,6 @@ class Text : public SimpleText {
virtual void spatiumChanged(qreal oldValue, qreal newValue);
void dragTo(const QPointF&p);
bool editMode() const { return _editMode; }
bool styled() const { return _styleIndex != -1; }
int textStyleType() const { return _styleIndex; }
@ -133,8 +131,6 @@ class Text : public SimpleText {
virtual void setScore(Score* s);
friend class TextProperties;
virtual void textChanged() {}
QTextCursor* cursor() { return _cursor; }
QTextDocument* doc() const { return _doc; }

View file

@ -2095,7 +2095,6 @@ void EditText::undoRedo()
oldText = s;
}
else {
text->textChanged();
if (text->type() == Element::TEMPO_TEXT) {
TempoText* tt = static_cast<TempoText*>(text);
tt->score()->setTempo(tt->segment(), tt->tempo());

View file

@ -4217,7 +4217,7 @@ void ScoreView::lyricsUpDown(bool up, bool end)
if (end)
((Lyrics*)editObject)->moveCursorToEnd();
else
((Lyrics*)editObject)->moveCursor(0);
((Lyrics*)editObject)->moveCursorToStart();
_score->setLayoutAll(true);
_score->end2();
@ -4326,7 +4326,7 @@ void ScoreView::lyricsTab(bool back, bool end, bool moveOnly)
if (end)
((Lyrics*)editObject)->moveCursorToEnd();
else
((Lyrics*)editObject)->moveCursor(0);
((Lyrics*)editObject)->moveCursorToStart();
_score->setLayoutAll(true);
_score->end2();

View file

@ -535,6 +535,7 @@ void TextTools::styleChanged(int idx)
baselineAlign->setEnabled(unstyled);
vcenterAlign->setEnabled(unstyled);
blockAllSignals(false);
updateText();
}
//---------------------------------------------------------