diff --git a/controls/CustomLineHeightTextEdit.cpp b/controls/CustomLineHeightTextEdit.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ada3bc828f0aebef5096978d15781dd74198986 --- /dev/null +++ b/controls/CustomLineHeightTextEdit.cpp @@ -0,0 +1,81 @@ +#include "CustomLineHeightTextEdit.h" + + +CustomLineHeightTextEdit::CustomLineHeightTextEdit(QWidget *a_parent) + :QTextEdit (a_parent) +{ +} + +void CustomLineHeightTextEdit::setPlaceholderText(const QString &placeholderText) +{ + setProperty("state",STATE_DEFAULT); + style()->unpolish(this); + style()->polish(this); + update(); + m_placeholderText = placeholderText; + QTextEdit::setText(textToHtml(placeholderText)); + //setReadOnly(true); +} + +void CustomLineHeightTextEdit::mousePressEvent(QMouseEvent *e) +{ + if(property("state").toString()==STATE_DEFAULT) + { + setProperty("state",STATE_HOVER); + style()->unpolish(this); + style()->polish(this); + update(); + QString htmlText = QString("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0//EN' 'http://www.w3.org/TR/REC-html40/strict.dtd'>" + "<html><head><meta name='qrichtext' content='1' /><style type='text/css'>" + "p, li { white-space: pre-wrap; }" + "</style></head><body>" + "<p style='line-height:%1;'></p></body></html>").arg(m_lineHeight); + + QTextEdit::setText(htmlText); + } + QTextEdit::mousePressEvent(e); +} + +void CustomLineHeightTextEdit::focusOutEvent(QFocusEvent *e) +{ + if(toPlainText().isEmpty()) + { + setProperty("state",STATE_DEFAULT); + style()->unpolish(this); + style()->polish(this); + update(); + QTextEdit::setText(textToHtml(m_placeholderText)); + } + QTextEdit::focusOutEvent(e); +} + +QString CustomLineHeightTextEdit::toPlainText() const +{ + if(property("state").toString()==STATE_DEFAULT) + return QString(""); + + return QTextEdit::toPlainText(); + +} + +void CustomLineHeightTextEdit::setObjectName(const QString &name) +{ + QObject::setObjectName(name); + + updateStyleSheets(); +} + +void CustomLineHeightTextEdit::updateStyleSheets() +{ + StyleSheatSearchPar searchPar; + searchPar.widgetName = "#" + this->objectName(); + + QString stylesheet = AppStyleSheetHandler::getWidgetStyleSheet(searchPar); + m_lineHeight = AppStyleSheetHandler::getValueFromStylesheet(stylesheet, "line-height"); +} + +QString CustomLineHeightTextEdit::textToHtml(const QString &text) +{ + if(m_lineHeight.isEmpty())m_lineHeight = "100"; + return QString("<p style = 'line-height:%1;'> %2 </p>").arg(m_lineHeight).arg(text); +} diff --git a/controls/CustomLineHeightTextEdit.h b/controls/CustomLineHeightTextEdit.h new file mode 100644 index 0000000000000000000000000000000000000000..d7453eb87507c4d210a9cfb099b50a399c4f8d1b --- /dev/null +++ b/controls/CustomLineHeightTextEdit.h @@ -0,0 +1,77 @@ +#ifndef CUSTOMLINEHEIGHTTEXTEDIT_H +#define CUSTOMLINEHEIGHTTEXTEDIT_H + +#include <QTextEdit> +#include "AppStyleSheetHandler.h" +#include <QStyle> + +/** @brief QGraphicsDropShadowEffect that can be adjusted from css + * + * @details Set style in .css file in format + * + *> #elementName + *> { + *> + *> line-height: 100%; (100% or 100) + *>} + * To specify PlaceholderText parameters + *> #elementName[state="0"] + *> { + *> color:#A1A1A1; + *> + *>} + * To specify text parameters + *> #elementName[state="1"] + *> { + *>color:#000000; + *> + *>} + * +*/ + +class CustomLineHeightTextEdit: public QTextEdit +{ +public: + explicit CustomLineHeightTextEdit(QWidget *a_parent = Q_NULLPTR); + /** @brief Sets the object name and updates the styles. + * @param placeholderText + */ + void setObjectName(const QString &name); + /** @brief Updates the styles. + */ + void updateStyleSheets(); + /** @brief The redefined method returns plain text as html for setting the line spacing. + * @param placeholderText + */ + void setPlaceholderText(const QString &placeholderText); + /** @brief Returns text or empty string. + */ + QString toPlainText() const; +protected: + /** @brief The redefined method returns plain text as html for setting the line spacing. + * @param e + */ + void focusOutEvent(QFocusEvent *e); + /** @brief By clicking the mouse button, removes text from the screen and updates the settings for text. + * @param e + */ + void mousePressEvent(QMouseEvent *e); + + const QString STATE_HOVER = "hover"; + const QString STATE_DEFAULT = "default"; + +private: + /** @brief Adds a style to the text from the html line height. + * @param text + */ + QString textToHtml(const QString &text); + /** @brief The value of the row height from the style sheet. + */ + QString m_lineHeight; + /** @brief Default text. + */ + QString m_placeholderText; + +}; + +#endif // CUSTOMLINEHEIGHTTEXTEDIT_H diff --git a/controls/controls.pri b/controls/controls.pri index 60ea7401a948fdb22a3c3fc03d757c23ab464eb6..8abc0a38b242286a77ec423ab7f6269d448c14fb 100644 --- a/controls/controls.pri +++ b/controls/controls.pri @@ -5,6 +5,7 @@ SOURCES += \ $$PWD/CustomComboBox.cpp \ $$PWD/CustomComboBoxPopup.cpp \ $$PWD/CustomLineHeightLabel.cpp \ + $$PWD/CustomLineHeightTextEdit.cpp \ $$PWD/CustomPlacementButton.cpp \ $$PWD/CustomWidget.cpp \ $$PWD/ListModel.cpp \ @@ -21,6 +22,7 @@ HEADERS += \ $$PWD/CustomComboBox.h \ $$PWD/CustomComboBoxPopup.h \ $$PWD/CustomLineHeightLabel.h \ + $$PWD/CustomLineHeightTextEdit.h \ $$PWD/CustomPlacementButton.h \ $$PWD/CustomWidget.h \ $$PWD/ListModel.h \