714 changes: 473 additions & 241 deletions mythtv/libs/libmythui/mythuitext.cpp

Large diffs are not rendered by default.

30 changes: 22 additions & 8 deletions mythtv/libs/libmythui/mythuitext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MYTHUI_TEXT_H_

// QT headers
#include <QTextLayout>
#include <QColor>

// Mythdb headers
Expand Down Expand Up @@ -45,6 +46,7 @@ class MUI_PUBLIC MythUIText : public MythUIType, public StorageUser
void UseAlternateArea(bool useAlt);

virtual void Pulse(void);
QPoint CursorPosition(int text_offset);

// StorageUser
void SetDBValue(const QString &text) { SetText(text); }
Expand Down Expand Up @@ -79,20 +81,26 @@ class MUI_PUBLIC MythUIText : public MythUIType, public StorageUser
void SetPosition(const MythPoint &pos);
MythRect GetDrawRect(void) { return m_drawRect; }

void SetDrawRectSize(const int width, const int height);
void SetDrawRectPosition(const int x, const int y);
void MoveDrawRect(const int x, const int y);

bool MakeNarrow(QRect &min_rect);
bool MakeShort(QRect &min_rect);
void SetCanvasPosition(int x, int y);
void ShiftCanvas(int x, int y);

bool Layout(QString & paragraph, QTextLayout *layout,
bool & overflow, qreal width, qreal & height,
qreal & last_line_width, QRectF & min_rect, int & num_lines);
bool LayoutParagraphs(const QStringList & paragraphs,
const QTextOption & textoption,
qreal width, qreal & height, QRectF & min_rect,
qreal & last_line_width, int & num_lines);
bool GetNarrowWidth(const QStringList & paragraphs,
const QTextOption & textoption, qreal & width);
void FillCutMessage(bool reset_size = false);
QString cutDown(const QString &data, MythFontProperties *font,
bool multiline = false);

int m_Justification;
MythRect m_OrigDisplayRect;
MythRect m_AltDisplayRect;
MythRect m_Canvas;
MythRect m_drawRect;
QPoint m_cursorPos;

QString m_Message;
QString m_CutMessage;
Expand All @@ -103,6 +111,12 @@ class MUI_PUBLIC MythUIText : public MythUIType, public StorageUser
bool m_ShrinkNarrow;
bool m_Cutdown;
bool m_MultiLine;
int m_Leading;
int m_extraLeading;
int m_lineHeight;
int m_textCursor;

QVector<QTextLayout *> m_Layouts;

MythFontProperties* m_Font;
QMap<QString, MythFontProperties> m_FontStates;
Expand Down
114 changes: 15 additions & 99 deletions mythtv/libs/libmythui/mythuitextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ bool MythUITextEdit::ParseElement(
void MythUITextEdit::Finalize()
{
SetInitialStates();

// Give it something to chew on, so it can position the initial
// cursor in the right place
SetText(".", false);
m_cursorImage->SetPosition(m_Text->CursorPosition(0));
SetText("", false);
}

void MythUITextEdit::SetInitialStates()
Expand Down Expand Up @@ -176,6 +182,7 @@ void MythUITextEdit::SetInitialStates()

if (m_backgroundState && !m_backgroundState->DisplayState("active"))
LOG(VB_GENERAL, LOG_ERR, LOC + "active state doesn't exist");
m_Text->SetCutDown(false);

QFontMetrics fm(m_Text->GetFontProperties()->face());
int height = fm.height();
Expand All @@ -191,20 +198,6 @@ void MythUITextEdit::SetInitialStates()

m_cursorImage->ForceSize(QSize(width, height));
}

MythRect textrect = m_Text->GetArea();

if (textrect.isNull())
textrect = MythRect(5, 5, m_Area.width() - 10, m_Area.height() - 10);

textrect.setWidth(textrect.width() - m_cursorImage->GetArea().width());

if (textrect.isValid())
m_Text->SetArea(textrect);

m_Text->SetCutDown(false);

m_cursorImage->SetPosition(textrect.x(), textrect.y());
}

void MythUITextEdit::SetMaxLength(const int length)
Expand All @@ -223,9 +216,7 @@ void MythUITextEdit::SetText(const QString &text, bool moveCursor)
{
QString obscured;

while (obscured.size() < m_Message.size())
obscured.append("*");

obscured.fill('*', m_Message.size());
m_Text->SetText(obscured);
}
else
Expand Down Expand Up @@ -292,108 +283,35 @@ void MythUITextEdit::RemoveCharacter(int position)
QString newmessage = m_Message;

newmessage.remove(position, 1);
SetText(newmessage, false);

if (position == m_Position)
MoveCursor(MoveLeft);

SetText(newmessage, false);
}

bool MythUITextEdit::MoveCursor(MoveDirection moveDir)
{
if (!m_Text || !m_Text->GetFontProperties() || !m_cursorImage)
if (!m_Text || !m_cursorImage)
return false;

QFontMetrics fm(m_Text->GetFontProperties()->face());

int cursorPos = m_cursorImage->GetArea().x();
int cursorWidth = m_cursorImage->GetArea().width();
MythRect textRect = m_Text->GetArea();
MythRect drawRect = m_Text->GetDrawRect();
int newcursorPos = 0;

QString string;

if (m_isPassword)
{
while (string.size() < m_Message.size())
string.append("*");
}
else
string = m_Message;

QSize stringSize = fm.size(Qt::TextSingleLine, string);
m_Text->SetDrawRectSize(stringSize.width() + 1, textRect.height());
QSize charSize;

switch (moveDir)
{
case MoveLeft:

if (m_Position < 0)
return false;

charSize = fm.size(Qt::TextSingleLine, string.mid(m_Position, 1));

newcursorPos = cursorPos - charSize.width();

if (newcursorPos < (textRect.x() + (textRect.width() / 2)))
{
if (m_Position == 0 || (drawRect.x() + charSize.width() > textRect.x()))
m_Text->SetDrawRectPosition(0, 0);
else
m_Text->MoveDrawRect(charSize.width(), 0);

if (drawRect.x() < textRect.x())
newcursorPos = cursorPos;
}

m_Position--;
break;
case MoveRight:

if (m_Position == (string.size() - 1))
if (m_Position == (m_Message.size() - 1))
return false;

charSize = fm.size(Qt::TextSingleLine, string.mid(m_Position + 1, 1));

newcursorPos = cursorPos + charSize.width();

if (newcursorPos > textRect.width())
{
m_Text->MoveDrawRect(-(charSize.width()), 0);
newcursorPos = cursorPos;
}

m_Position++;
break;
case MoveEnd:
int messageWidth = stringSize.width();

if ((messageWidth + cursorWidth)
>= textRect.width())
{
int newx = drawRect.width() -
(messageWidth + cursorWidth);
m_Text->MoveDrawRect(newx, 0);
newcursorPos = messageWidth + newx + textRect.x();
}
else
{
m_Text->SetDrawRectPosition(0, 0);

if (messageWidth <= 0)
newcursorPos = textRect.x();
else
newcursorPos = messageWidth + textRect.x();
}

m_Position = string.size() - 1;

m_Position = m_Message.size() - 1;
break;
}

m_cursorImage->SetPosition(newcursorPos, textRect.y());
m_cursorImage->SetPosition(m_Text->CursorPosition(m_Position + 1));

SetRedraw();
return true;
Expand Down Expand Up @@ -442,13 +360,11 @@ bool MythUITextEdit::keyPressEvent(QKeyEvent *e)

if (action == "LEFT")
{
if (!MoveCursor(MoveLeft))
handled = false;
MoveCursor(MoveLeft);
}
else if (action == "RIGHT")
{
if (!MoveCursor(MoveRight))
handled = false;
MoveCursor(MoveRight);
}
else if (action == "DELETE")
{
Expand Down