00001 #include "alineedit.h"
00002
00003 ALineEdit::ALineEdit(const QString &defaultValue, const QString &text, QWidget *parent) : QLineEdit(parent)
00004 {
00005 defaultText = defaultValue;
00006 if(text != "")
00007 {
00008 setText(text, true);
00009 }
00010 else
00011 {
00012 showHintText();
00013 }
00014 }
00015
00016 ALineEdit::ALineEdit(const QString &defaultValue, QWidget *parent) : QLineEdit(parent)
00017 {
00018 defaultText = defaultValue;
00019 showHintText();
00020 }
00021
00022 ALineEdit::ALineEdit(QWidget *parent) : QLineEdit(parent)
00023 {
00024 defaultText = "";
00025 showHintText();
00026 }
00027
00028 QString ALineEdit::hintText() const
00029 {
00030 return defaultText;
00031 }
00032
00033 void ALineEdit::setText(const QString &newText, bool created)
00034 {
00035 if(!created)
00036 {
00037 setBlackStyle();
00038 }
00039 QLineEdit::setText(newText);
00040 }
00041
00042 void ALineEdit::setHintText(const QString &hintText)
00043 {
00044
00045 if(hintText == this->text())
00046 {
00047 return;
00048 }
00049 if(this->text() == defaultText)
00050 {
00051 defaultText = hintText;
00052 this->setText("");
00053 }
00054 else
00055 {
00056 defaultText = hintText;
00057 }
00058 if(!hasFocus())
00059 {
00060 showHintText();
00061 }
00062 else
00063 {
00064 hideHintText();
00065 }
00066 }
00067
00068 void ALineEdit::showHintText()
00069 {
00070 if(this->text() == "")
00071 {
00072 this->setText(defaultText, true);
00073 setGreyHintStyle();
00074 }
00075 }
00076
00077 void ALineEdit::hideHintText()
00078 {
00079 if(this->text() == "" || this->text() == defaultText)
00080 {
00081 this->setText("");
00082 setBlackStyle();
00083 }
00084 }
00085
00086 void ALineEdit::focusInEvent(QFocusEvent *e)
00087 {
00088 hideHintText();
00089 QLineEdit::focusInEvent(e);
00090 }
00091
00092 void ALineEdit::focusOutEvent(QFocusEvent *e)
00093 {
00094
00095 showHintText();
00096 QLineEdit::focusOutEvent(e);
00097 }
00098
00099 void ALineEdit::setBlackStyle()
00100 {
00101 QPalette palette;
00102 palette.setColor(QPalette::Text, QColor(0,0,0));
00103 setPalette(palette);
00104
00105 hintStyleActive = false;
00106 }
00107
00108 void ALineEdit::setGreyHintStyle()
00109 {
00110 QPalette palette;
00111 QColor color;
00112 color.setNamedColor("#BBBBBB");
00113 palette.setColor(QPalette::Text, color);
00114 setPalette(palette);
00115
00116 hintStyleActive = true;
00117 }
00118
00119 void ALineEdit::setWrong()
00120 {
00121 QPalette palette;
00122 palette.setColor(backgroundRole(), QColor(255, 0, 0, 55));
00123
00124 if(hintStyleActive)
00125 {
00126 QColor color;
00127 color.setNamedColor("#BBBBBB");
00128 palette.setColor(QPalette::Text, color);
00129 }
00130 else
00131 {
00132 QColor color;
00133 color.setNamedColor("#000000");
00134 palette.setColor(QPalette::Text, color);
00135 }
00136
00137 setPalette(palette);
00138 }
00139
00140 void ALineEdit::resetStyle()
00141 {
00142 if(hintStyleActive)
00143 {
00144 setGreyHintStyle();
00145 }
00146 else
00147 {
00148 setBlackStyle();
00149 }
00150 }