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