Qt Utilities  5.6.0
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
clearplaintextedit.cpp
Go to the documentation of this file.
1 #include "./clearplaintextedit.h"
2 
3 #include <QHBoxLayout>
4 #include <QScrollBar>
5 
6 using namespace std;
7 
8 namespace Widgets {
9 
18 ClearPlainTextEdit::ClearPlainTextEdit(QWidget *parent) :
19  QPlainTextEdit(parent),
20  ButtonOverlay(viewport())
21 {
22  // set alignment to show buttons in the bottom right corner
23  ButtonOverlay::buttonLayout()->setAlignment(Qt::AlignBottom | Qt::AlignRight);
25  connect(this, &QPlainTextEdit::textChanged, this, &ClearPlainTextEdit::handleTextChanged);
26  // ensure button layout is realigned when scrolling
27  connect(verticalScrollBar(), &QScrollBar::actionTriggered, this, &ClearPlainTextEdit::handleScroll);
28  connect(this, &QPlainTextEdit::cursorPositionChanged, this, &ClearPlainTextEdit::handleScroll);
29 }
30 
35 {}
36 
40 void ClearPlainTextEdit::handleTextChanged()
41 {
42  updateClearButtonVisibility(!document()->isEmpty());
43 }
44 
45 void ClearPlainTextEdit::handleClearButtonClicked()
46 {
47  // do no call clear() here to prevent clearing of undo history
48  QTextCursor cursor(document());
49  cursor.select(QTextCursor::Document);
50  cursor.removeSelectedText();
51 }
52 
53 void ClearPlainTextEdit::handleScroll()
54 {
55  buttonLayout()->update();
56 }
57 
59 {
60  return document()->isEmpty();
61 }
62 
63 } // namespace Widgets
~ClearPlainTextEdit()
Destroys the clear plain text edit.
STL namespace.
void updateClearButtonVisibility(bool visible)
Updates the visibility of the clear button.
QHBoxLayout * buttonLayout()
Returns the layout manager holding the buttons.
Definition: buttonoverlay.h:51
bool isCleared() const
Returns whether the related widget is cleared.
The ButtonOverlay class is used to display buttons on top of other widgets.
Definition: buttonoverlay.h:17
void setClearButtonEnabled(bool enabled)
Sets whether the clear button is enabled.
Provides a set of extended widgets such as ClearLineEdit and ClearComboBox.
Definition: buttonoverlay.h:13