Qt Utilities: widgets/clearplaintextedit.cpp Source File

Qt Utilities 6.21.1 Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models

Loading...
Searching...
No Matches
clearplaintextedit.cpp
Go to the documentation of this file.
1#include "./clearplaintextedit.h"
2
3#include <QHBoxLayout>
4#include <QScrollBar>
5#include <QStyle>
6#include <QStyleOptionFrame>
7
8using namespace std;
9
10namespace QtUtilities {
11
16
20 ClearPlainTextEdit::ClearPlainTextEdit(QWidget *parent)
21 : QPlainTextEdit(parent)
22 , ButtonOverlay(viewport())
23{
24 handleCustomLayoutCreated();
25 ButtonOverlay::setClearButtonEnabled(true);
26}
27
31ClearPlainTextEdit::~ClearPlainTextEdit()
32{
33}
34
38void ClearPlainTextEdit::handleTextChanged()
39{
40 updateClearButtonVisibility(!document()->isEmpty());
41}
42
43void ClearPlainTextEdit::handleClearButtonClicked()
44{
45 // do no call clear() here to prevent clearing of undo history
46 QTextCursor cursor(document());
47 cursor.select(QTextCursor::Document);
48 cursor.removeSelectedText();
49}
50
51void ClearPlainTextEdit::handleCustomLayoutCreated()
52{
53 // set alignment to show buttons in the bottom right corner
54 ButtonOverlay::buttonLayout()->setAlignment(Qt::AlignBottom | Qt::AlignRight);
55 const QStyle *const s = style();
56 QStyleOptionFrame opt;
57 opt.initFrom(this);
58 setContentsMarginsFromEditFieldRectAndFrameWidth(s->subElementRect(QStyle::SE_FrameContents, &opt, this),
59 s->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, m_widget), s->pixelMetric(QStyle::PM_LayoutVerticalSpacing, &opt, m_widget));
60 connect(this, &QPlainTextEdit::textChanged, this, &ClearPlainTextEdit::handleTextChanged);
61 // ensure button layout is realigned when scrolling
62 connect(verticalScrollBar(), &QScrollBar::actionTriggered, this, &ClearPlainTextEdit::handleScroll);
63 connect(this, &QPlainTextEdit::cursorPositionChanged, this, &ClearPlainTextEdit::handleScroll);
64}
65
66void ClearPlainTextEdit::handleScroll()
67{
68 buttonLayout()->update();
69}
70
71 bool ClearPlainTextEdit::isCleared() const
72{
73 return document()->isEmpty();
74}
75
76} // namespace QtUtilities
QtUtilities::ButtonOverlay::updateClearButtonVisibility
void updateClearButtonVisibility(bool visible)
Updates the visibility of the clear button.
Definition buttonoverlay.cpp:283
QtUtilities::ButtonOverlay::buttonLayout
QHBoxLayout * buttonLayout()
Returns the layout manager holding the buttons.
Definition buttonoverlay.cpp:97
QtUtilities::ButtonOverlay::handleCustomLayoutCreated
virtual void handleCustomLayoutCreated()
Applies additional handling when the button layout has been created.
Definition buttonoverlay.cpp:305
QtUtilities::ButtonOverlay::ButtonOverlay
ButtonOverlay(QWidget *widget)
Constructs a button overlay for the specified widget.
Definition buttonoverlay.cpp:49
QtUtilities::ButtonOverlay::ClearPlainTextEdit
friend class ClearPlainTextEdit
Definition buttonoverlay.h:29
QtUtilities::ButtonOverlay::handleClearButtonClicked
virtual void handleClearButtonClicked()
Clears the related widget.
Definition buttonoverlay.cpp:295
QtUtilities::ButtonOverlay::setClearButtonEnabled
void setClearButtonEnabled(bool enabled)
Sets whether the clear button is enabled.
Definition buttonoverlay.cpp:125
QtUtilities::ClearPlainTextEdit::~ClearPlainTextEdit
~ClearPlainTextEdit() override
Destroys the clear plain text edit.
Definition clearplaintextedit.cpp:31
QtUtilities::ClearPlainTextEdit::isCleared
bool isCleared() const override
Returns whether the related widget is cleared.
Definition clearplaintextedit.cpp:71
clearplaintextedit.h
QtUtilities
!
Definition trylocker.h:8