Don't get the selection to check if it is empty

Every time the selection is changed, the selection text is retrieved to
check whether to enable or disable the copy actions. Besides that, the
selection text is also used for the web search context menu entries.

Better just check if the selection is empty and make a note that the
selection changed, so the next time the context menu is invoked it can
retrieve the current selection text, which should happen much less often
than selection changes.
This commit is contained in:
Luis Javier Merino Morán
2021-12-06 00:53:19 +01:00
committed by Tomaz Canabrava
parent 5df601d9ee
commit 5919c1fd0a
7 changed files with 32 additions and 13 deletions

View File

@@ -92,8 +92,8 @@ void Emulation::checkScreenInUse()
void Emulation::checkSelectedText()
{
QString text = _currentScreen->selectedText(Screen::PreserveLineBreaks);
Q_EMIT selectionChanged(text);
bool isEmpty = _currentScreen->isSelectionEmpty();
Q_EMIT selectionChanged(isEmpty);
}
Emulation::~Emulation()

View File

@@ -380,7 +380,7 @@ Q_SIGNALS:
/**
* Emitted when the text selection is changed
*/
void selectionChanged(const QString &text);
void selectionChanged(const bool selectionEmpty);
/**
* Emitted when terminal code requiring terminal's response received.

View File

@@ -1448,6 +1448,15 @@ bool Screen::isSelected(const int x, const int y) const
return pos >= _selTopLeft && pos <= _selBottomRight && columnInSelection;
}
bool Screen::isSelectionEmpty() const
{
if (!isSelectionValid()) {
return true;
}
return _selTopLeft == _selBottomRight;
}
QString Screen::selectedText(const DecodingOptions options) const
{
if (!isSelectionValid()) {

View File

@@ -467,6 +467,7 @@ public:
*/
bool isSelected(const int x, const int y) const;
bool isSelectionEmpty() const;
/**
* Convenience method. Returns the currently selected text.
* @param options See Screen::DecodingOptions

View File

@@ -734,7 +734,7 @@ Q_SIGNALS:
* This signal serves as a relayer of Emulation::selectedText(QString),
* making it usable for higher level component.
*/
void selectionChanged(const QString &text);
void selectionChanged(const bool selectionChanged);
/**
* Emitted when foreground request ("\033]10;?\a") terminal code received.

View File

@@ -116,6 +116,8 @@ SessionController::SessionController(Session *sessionParam, TerminalDisplay *vie
, _webSearchMenu(nullptr)
, _listenForScreenWindowUpdates(false)
, _preventClose(false)
, _selectionEmpty(false)
, _selectionChanged(true)
, _selectedText(QString())
, _showMenuAction(nullptr)
, _bookmarkValidProgramsToClear(QStringList())
@@ -448,19 +450,20 @@ void SessionController::setupPrimaryScreenSpecificActions(bool use)
selectLineAction->setEnabled(use);
}
void SessionController::selectionChanged(const QString &selectedText)
void SessionController::selectionChanged(const bool selectionEmpty)
{
_selectedText = selectedText;
updateCopyAction(selectedText);
_selectionChanged = true;
_selectionEmpty = selectionEmpty;
updateCopyAction(selectionEmpty);
}
void SessionController::updateCopyAction(const QString &selectedText)
void SessionController::updateCopyAction(const bool selectionEmpty)
{
QAction *copyAction = actionCollection()->action(QStringLiteral("edit_copy"));
QAction *copyContextMenu = actionCollection()->action(QStringLiteral("edit_copy_contextmenu"));
// copy action is meaningful only when some text is selected.
copyAction->setEnabled(!selectedText.isEmpty());
copyContextMenu->setVisible(!selectedText.isEmpty());
copyAction->setEnabled(!selectionEmpty);
copyContextMenu->setVisible(!selectionEmpty);
}
void SessionController::updateWebSearchMenu()
@@ -469,10 +472,14 @@ void SessionController::updateWebSearchMenu()
_webSearchMenu->setVisible(false);
_webSearchMenu->menu()->clear();
if (_selectedText.isEmpty()) {
if (_selectionEmpty) {
return;
}
if (_selectionChanged) {
_selectedText = view()->screenWindow()->selectedText(Screen::PreserveLineBreaks);
_selectionChanged = false;
}
QString searchText = _selectedText;
searchText = searchText.replace(QLatin1Char('\n'), QLatin1Char(' ')).replace(QLatin1Char('\r'), QLatin1Char(' ')).simplified();

View File

@@ -198,7 +198,7 @@ public Q_SLOTS:
/**
* update actions which are closely related with the selected text.
*/
void selectionChanged(const QString &selectedText);
void selectionChanged(const bool selectionEmpty);
/**
* close the associated session. This might involve user interaction for
@@ -293,7 +293,7 @@ private Q_SLOTS:
void zmodemUpload();
// update actions related with selected text
void updateCopyAction(const QString &selectedText);
void updateCopyAction(const bool selectionEmpty);
void updateWebSearchMenu();
private:
@@ -349,6 +349,8 @@ private:
bool _listenForScreenWindowUpdates;
bool _preventClose;
bool _selectionEmpty;
bool _selectionChanged;
QString _selectedText;
QAction *_showMenuAction;