* @brief Converts a pixel position to a cursor position. */
| 875 | * @brief Converts a pixel position to a cursor position. |
| 876 | */ |
| 877 | QPoint Widgets::Terminal::positionToCursor(const QPoint& pos) const |
| 878 | { |
| 879 | int localY = (pos.y() - m_borderY) / m_cHeight; |
| 880 | int remainingY = localY; |
| 881 | |
| 882 | if (localY < 0) { |
| 883 | if (m_scrollOffsetY < m_data.size()) |
| 884 | return QPoint(0, m_scrollOffsetY); |
| 885 | |
| 886 | return QPoint(0, 0); |
| 887 | } |
| 888 | |
| 889 | for (int i = m_scrollOffsetY; i < m_data.size(); ++i) { |
| 890 | const QString& line = m_data[i]; |
| 891 | |
| 892 | if (line.isEmpty()) { |
| 893 | if (remainingY == 0) |
| 894 | return QPoint(0, i); |
| 895 | |
| 896 | remainingY--; |
| 897 | continue; |
| 898 | } |
| 899 | |
| 900 | const int lines = (line.length() + maxCharsPerLine() - 1) / maxCharsPerLine(); |
| 901 | if (remainingY >= lines) { |
| 902 | remainingY -= lines; |
| 903 | continue; |
| 904 | } |
| 905 | |
| 906 | const int segmentStart = qMax(0, remainingY) * maxCharsPerLine(); |
| 907 | const int segmentEnd = qMin(segmentStart + maxCharsPerLine(), line.length()); |
| 908 | |
| 909 | int relX = pos.x() - m_borderX; |
| 910 | if (Misc::Translator::instance().rtl()) |
| 911 | relX = (width() - m_borderX) - pos.x(); |
| 912 | |
| 913 | return QPoint(findCharAtPixelX(line, segmentStart, segmentEnd, relX), i); |
| 914 | } |
| 915 | |
| 916 | if (!m_data.isEmpty()) { |
| 917 | int lastLine = m_data.size() - 1; |
| 918 | int lastChar = m_data.last().length(); |
| 919 | return QPoint(lastChar, lastLine); |
| 920 | } |
| 921 | |
| 922 | return QPoint(0, 0); |
| 923 | } |
| 924 | |
| 925 | /** |
| 926 | * @brief Copies the currently selected text to the system clipboard. |