Skip to content
Snippets Groups Projects
Commit a07180b9 authored by konstantin.kukharenko's avatar konstantin.kukharenko Committed by Alexandr Mruchok
Browse files

[+]New custom QEditText

parent 910d8463
No related branches found
No related tags found
1 merge request!17Develop old
#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);
}
#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
......@@ -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 \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment