2015-09-06 20:19:21 +02:00
|
|
|
#include "./clearplaintextedit.h"
|
2015-04-22 18:57:44 +02:00
|
|
|
|
|
|
|
#include <QHBoxLayout>
|
2016-03-06 17:12:06 +01:00
|
|
|
#include <QScrollBar>
|
|
|
|
|
|
|
|
using namespace std;
|
2015-04-22 18:57:44 +02:00
|
|
|
|
|
|
|
namespace Widgets {
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \class Widgets::ClearPlainTextEdit
|
|
|
|
* \brief A QPlainTextEdit with an embedded button for clearing its contents.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Constructs a clear plain text edit.
|
|
|
|
*/
|
2017-05-01 03:16:25 +02:00
|
|
|
ClearPlainTextEdit::ClearPlainTextEdit(QWidget *parent)
|
|
|
|
: QPlainTextEdit(parent)
|
|
|
|
, ButtonOverlay(viewport())
|
2015-04-22 18:57:44 +02:00
|
|
|
{
|
|
|
|
// set alignment to show buttons in the bottom right corner
|
|
|
|
ButtonOverlay::buttonLayout()->setAlignment(Qt::AlignBottom | Qt::AlignRight);
|
|
|
|
ButtonOverlay::setClearButtonEnabled(true);
|
2016-03-06 18:08:08 +01:00
|
|
|
connect(this, &QPlainTextEdit::textChanged, this, &ClearPlainTextEdit::handleTextChanged);
|
2016-03-06 17:12:06 +01:00
|
|
|
// ensure button layout is realigned when scrolling
|
2016-03-06 18:08:08 +01:00
|
|
|
connect(verticalScrollBar(), &QScrollBar::actionTriggered, this, &ClearPlainTextEdit::handleScroll);
|
|
|
|
connect(this, &QPlainTextEdit::cursorPositionChanged, this, &ClearPlainTextEdit::handleScroll);
|
2015-04-22 18:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Destroys the clear plain text edit.
|
|
|
|
*/
|
|
|
|
ClearPlainTextEdit::~ClearPlainTextEdit()
|
2017-05-01 03:16:25 +02:00
|
|
|
{
|
|
|
|
}
|
2015-04-22 18:57:44 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Updates the visibility of the clear button.
|
|
|
|
*/
|
|
|
|
void ClearPlainTextEdit::handleTextChanged()
|
|
|
|
{
|
|
|
|
updateClearButtonVisibility(!document()->isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearPlainTextEdit::handleClearButtonClicked()
|
|
|
|
{
|
|
|
|
// do no call clear() here to prevent clearing of undo history
|
|
|
|
QTextCursor cursor(document());
|
|
|
|
cursor.select(QTextCursor::Document);
|
|
|
|
cursor.removeSelectedText();
|
|
|
|
}
|
|
|
|
|
2016-03-06 17:12:06 +01:00
|
|
|
void ClearPlainTextEdit::handleScroll()
|
|
|
|
{
|
|
|
|
buttonLayout()->update();
|
|
|
|
}
|
|
|
|
|
2015-04-22 18:57:44 +02:00
|
|
|
bool ClearPlainTextEdit::isCleared() const
|
|
|
|
{
|
|
|
|
return document()->isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Widgets
|