Changeset View
Changeset View
Standalone View
Standalone View
layers/selections/text/kpTextSelection_Cursor.cpp
Show First 20 Lines • Show All 47 Lines • ▼ Show 20 Line(s) | |||||
48 | { | 48 | { | ||
49 | if (!pointIsInTextArea (point)) | 49 | if (!pointIsInTextArea (point)) | ||
50 | return -1; | 50 | return -1; | ||
51 | 51 | | |||
52 | const QFontMetrics fontMetrics (d->textStyle.fontMetrics ()); | 52 | const QFontMetrics fontMetrics (d->textStyle.fontMetrics ()); | ||
53 | 53 | | |||
54 | int row = (point.y () - textAreaRect ().y ()) / | 54 | int row = (point.y () - textAreaRect ().y ()) / | ||
55 | fontMetrics.lineSpacing (); | 55 | fontMetrics.lineSpacing (); | ||
56 | if (row >= (int) d->textLines.size ()) | 56 | if (row >= static_cast<int> (d->textLines.size ())) | ||
57 | row = d->textLines.size () - 1; | 57 | row = d->textLines.size () - 1; | ||
58 | 58 | | |||
59 | return row; | 59 | return row; | ||
60 | } | 60 | } | ||
61 | 61 | | |||
62 | // public | 62 | // public | ||
63 | int kpTextSelection::closestTextColForPoint (const QPoint &point) const | 63 | int kpTextSelection::closestTextColForPoint (const QPoint &point) const | ||
64 | { | 64 | { | ||
65 | int row = closestTextRowForPoint (point); | 65 | int row = closestTextRowForPoint (point); | ||
66 | if (row < 0 || row >= (int) d->textLines.size ()) | 66 | if (row < 0 || row >= static_cast<int> (d->textLines.size ())) | ||
67 | return -1; | 67 | return -1; | ||
68 | 68 | | |||
69 | const int localX = point.x () - textAreaRect ().x (); | 69 | const int localX = point.x () - textAreaRect ().x (); | ||
70 | 70 | | |||
71 | const QFontMetrics fontMetrics (d->textStyle.fontMetrics ()); | 71 | const QFontMetrics fontMetrics (d->textStyle.fontMetrics ()); | ||
72 | 72 | | |||
73 | // (should be 0 but call just in case) | 73 | // (should be 0 but call just in case) | ||
74 | int charLocalLeft = fontMetrics.width (d->textLines [row], 0); | 74 | int charLocalLeft = fontMetrics.width (d->textLines [row], 0); | ||
75 | 75 | | |||
76 | // OPT: binary search or guess location then move | 76 | // OPT: binary search or guess location then move | ||
77 | for (int col = 0; col < (int) d->textLines [row].length (); col++) | 77 | for (int col = 0; col < static_cast<int> (d->textLines [row].length ()); col++) | ||
78 | { | 78 | { | ||
79 | // OPT: fontMetrics::charWidth() might be faster | 79 | // OPT: fontMetrics::charWidth() might be faster | ||
80 | const int nextCharLocalLeft = fontMetrics.width (d->textLines [row], col + 1); | 80 | const int nextCharLocalLeft = fontMetrics.width (d->textLines [row], col + 1); | ||
81 | if (localX <= (charLocalLeft + nextCharLocalLeft) / 2) | 81 | if (localX <= (charLocalLeft + nextCharLocalLeft) / 2) | ||
82 | return col; | 82 | return col; | ||
83 | 83 | | |||
84 | charLocalLeft = nextCharLocalLeft; | 84 | charLocalLeft = nextCharLocalLeft; | ||
85 | } | 85 | } | ||
86 | 86 | | |||
87 | return d->textLines [row].length ()/*past end of line*/; | 87 | return d->textLines [row].length ()/*past end of line*/; | ||
88 | } | 88 | } | ||
89 | 89 | | |||
90 | //--------------------------------------------------------------------- | 90 | //--------------------------------------------------------------------- | ||
91 | 91 | | |||
92 | // public | 92 | // public | ||
93 | QPoint kpTextSelection::pointForTextRowCol (int row, int col) const | 93 | QPoint kpTextSelection::pointForTextRowCol (int row, int col) const | ||
94 | { | 94 | { | ||
95 | kpPreeditText preeditText = d->preeditText; | 95 | kpPreeditText preeditText = d->preeditText; | ||
96 | if ((row < 0 || col < 0) || | 96 | if ((row < 0 || col < 0) || | ||
97 | (preeditText.isEmpty () && | 97 | (preeditText.isEmpty () && | ||
98 | (row >= (int) d->textLines.size () || col > (int) d->textLines [row].length ()))) | 98 | (row >= static_cast<int> (d->textLines.size ()) || col > static_cast<int> (d->textLines [row].length ())))) | ||
99 | { | 99 | { | ||
100 | #if DEBUG_KP_SELECTION && 1 | 100 | #if DEBUG_KP_SELECTION && 1 | ||
101 | qCDebug(kpLogLayers) << "kpTextSelection::pointForTextRowCol(" | 101 | qCDebug(kpLogLayers) << "kpTextSelection::pointForTextRowCol(" | ||
102 | << row << "," | 102 | << row << "," | ||
103 | << col << ") out of range" | 103 | << col << ") out of range" | ||
104 | << " textLines='" | 104 | << " textLines='" | ||
105 | << text () | 105 | << text () | ||
106 | << "'" | 106 | << "'" | ||
Show All 20 Lines |