// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 #pragma once #include "terminalsearch.h" #include "terminalsurface.h" #include #include #include #include #include #include #include #include #include #include namespace Terminal { class TerminalWidget : public QAbstractScrollArea { friend class CellIterator; Q_OBJECT public: TerminalWidget(QWidget *parent = nullptr, const Utils::Terminal::OpenTerminalParameters &openParameters = {}); void setFont(const QFont &font); void copyToClipboard(); void pasteFromClipboard(); void copyLinkToClipboard(); void clearSelection(); void zoomIn(); void zoomOut(); void moveCursorWordLeft(); void moveCursorWordRight(); void clearContents(); TerminalSearch *search() { return m_search.get(); } struct Selection { int start; int end; bool final{false}; bool operator!=(const Selection &other) const { return start != other.start || end != other.end || final != other.final; } bool operator==(const Selection &other) const { return !operator!=(other); } }; struct LinkSelection : public Selection { Utils::Link link; bool operator!=(const LinkSelection &other) const { return link != other.link || Selection::operator!=(other); } }; void setShellName(const QString &shellName); QString shellName() const; Utils::FilePath cwd() const; Utils::CommandLine currentCommand() const; std::optional identifier() const; QProcess::ProcessState processState() const; void restart(const Utils::Terminal::OpenTerminalParameters &openParameters); signals: void started(qint64 pid); void cwdChanged(const Utils::FilePath &cwd); void commandChanged(const Utils::CommandLine &cmd); protected: void paintEvent(QPaintEvent *event) override; void keyPressEvent(QKeyEvent *event) override; void keyReleaseEvent(QKeyEvent *event) override; void resizeEvent(QResizeEvent *event) override; void wheelEvent(QWheelEvent *event) override; void focusInEvent(QFocusEvent *event) override; void focusOutEvent(QFocusEvent *event) override; void inputMethodEvent(QInputMethodEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; void mouseDoubleClickEvent(QMouseEvent *event) override; void dragEnterEvent(QDragEnterEvent *event) override; void dropEvent(QDropEvent *event) override; void showEvent(QShowEvent *event) override; bool event(QEvent *event) override; protected: void onReadyRead(bool forceFlush); void setupSurface(); void setupFont(); void setupPty(); void setupColors(); void setupActions(); void writeToPty(const QByteArray &data); int paintCell(QPainter &p, const QRectF &cellRect, QPoint gridPos, const Internal::TerminalCell &cell, QFont &f, QList::const_iterator &searchIt) const; void paintCells(QPainter &painter, QPaintEvent *event) const; void paintCursor(QPainter &painter) const; void paintPreedit(QPainter &painter) const; bool paintFindMatches(QPainter &painter, QList::const_iterator &searchIt, const QRectF &cellRect, const QPoint gridPos) const; bool paintSelection(QPainter &painter, const QRectF &cellRect, const QPoint gridPos) const; void paintDebugSelection(QPainter &painter, const Selection &selection) const; qreal topMargin() const; QPoint viewportToGlobal(QPoint p) const; QPoint globalToViewport(QPoint p) const; QPoint globalToGrid(QPointF p) const; QPointF gridToGlobal(QPoint p, bool bottom = false, bool right = false) const; QRect gridToViewport(QRect rect) const; void updateViewport(); void updateViewportRect(const QRect &rect); int textLineFromPixel(int y) const; std::optional textPosFromPoint(const QTextLayout &textLayout, QPoint p) const; std::optional selectionToFormatRange( TerminalWidget::Selection selection, const QTextLayout &layout, int rowOffset) const; bool checkLinkAt(const QPoint &pos); struct TextAndOffsets { int start; int end; std::u32string text; }; TextAndOffsets textAt(const QPoint &pos) const; void applySizeChange(); void updateScrollBars(); void flushVTerm(bool force); bool setSelection(const std::optional &selection, bool scroll = true); QString textFromSelection() const; void configBlinkTimer(); QColor toQColor(std::variant color) const; void updateCopyState(); private: std::unique_ptr m_process; std::unique_ptr m_surface; std::unique_ptr m_shellIntegration; QString m_shellName; Utils::Id m_identifier; QFont m_font; QSizeF m_cellSize; bool m_ignoreScroll{false}; QString m_preEditString; std::optional m_selection; std::optional m_linkSelection; struct { QPoint start; QPoint end; } m_activeMouseSelect; QTimer m_flushDelayTimer; QTimer m_scrollTimer; int m_scrollDirection{0}; std::array m_currentColors; Utils::Terminal::OpenTerminalParameters m_openParameters; std::chrono::system_clock::time_point m_lastFlush; std::chrono::system_clock::time_point m_lastDoubleClick; bool m_selectLineMode{false}; Internal::Cursor m_cursor; QTimer m_cursorBlinkTimer; bool m_cursorBlinkState{true}; Utils::FilePath m_cwd; Utils::CommandLine m_currentCommand; using TerminalSearchPtr = std::unique_ptr>; TerminalSearchPtr m_search; Aggregation::Aggregate *m_aggregate{nullptr}; SearchHit m_lastSelectedHit{}; }; } // namespace Terminal