* @brief Copies the currently selected text to the system clipboard. */
| 926 | * @brief Copies the currently selected text to the system clipboard. |
| 927 | */ |
| 928 | void Widgets::Terminal::copy() |
| 929 | { |
| 930 | if (!copyAvailable()) |
| 931 | return; |
| 932 | |
| 933 | QString copiedText; |
| 934 | QPoint start = m_selectionStart; |
| 935 | QPoint end = m_selectionEnd; |
| 936 | |
| 937 | if (start.y() > end.y() || (start.y() == end.y() && start.x() > end.x())) |
| 938 | std::swap(start, end); |
| 939 | |
| 940 | for (int lineIndex = start.y(); lineIndex <= end.y(); ++lineIndex) { |
| 941 | const QString& line = m_data[lineIndex]; |
| 942 | |
| 943 | int startX = (lineIndex == start.y()) ? start.x() : 0; |
| 944 | int endX = (lineIndex == end.y()) ? end.x() : line.size(); |
| 945 | |
| 946 | if (start.y() == end.y() && start.x() > end.x()) |
| 947 | std::swap(startX, endX); |
| 948 | |
| 949 | if (lineIndex != start.y() && lineIndex != end.y()) { |
| 950 | startX = 0; |
| 951 | endX = line.size(); |
| 952 | } |
| 953 | |
| 954 | if (startX < endX) |
| 955 | copiedText.append(line.mid(startX, endX - startX)); |
| 956 | |
| 957 | if (lineIndex != end.y() || (startX == 0 && endX == line.size())) |
| 958 | copiedText.append('\n'); |
| 959 | } |
| 960 | |
| 961 | QClipboard* clipboard = QGuiApplication::clipboard(); |
| 962 | clipboard->setText(copiedText); |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * @brief Clears the terminal's content. |
no test coverage detected