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