1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| /*
* File: DateTimeEditNullable.cpp
* Author: Morpheus
*
* Created on 5 janvier 2012, 18:21
*/
#include "../include/DateTimeEditNullable.h"
#include <QLineEdit>
#include <QStyleOptionSpinBox>
#include <private/qdatetimeedit_p.h>
#include <QDebug>
DateTimeEditNullable::DateTimeEditNullable(QWidget* parent)
: QDateTimeEdit(parent) {
m_checkBox = new QCheckBox(this);
connect(m_checkBox, SIGNAL( toggled(bool) ), this, SLOT( checkBox_toggled(bool) ));
m_checkBox->setChecked( isEnabled() );
}
/*!
* \brief returns date, if empty date is invalid
* \return date, if empty date is invalid
*/
QDateTime DateTimeEditNullable::dateTime() const
{
if (!m_checkBox->isChecked()) {
return QDateTime();
} else {
return QDateTimeEdit::dateTime();
}
}
/*!
* \brief returns date, if empty date is invalid
* \return date, if empty date is invalid
*/
QDate DateTimeEditNullable::date() const
{
if (!m_checkBox->isChecked()) {
return QDate();
} else {
return QDateTimeEdit::date();
}
}
/*!
* \brief returns date, if empty date is invalid
* \return date, if empty date is invalid
*/
QTime DateTimeEditNullable::time() const
{
if (!m_checkBox->isChecked()) {
return QTime();
} else {
return QDateTimeEdit::time();
}
}
void DateTimeEditNullable::keyPressEvent(QKeyEvent *event)
{
if (!m_checkBox->isChecked())
return;
QDateTimeEdit::keyPressEvent(event);
}
/*!
\reimp
*/
void DateTimeEditNullable::mousePressEvent(QMouseEvent *event)
{
if(!m_checkBox->isChecked())
return;
QDateTimeEdit::mousePressEvent(event);
}
/*!
\reimp
*/
#ifndef QT_NO_WHEELEVENT
void DateTimeEditNullable::wheelEvent(QWheelEvent* event)
{
if(!m_checkBox->isChecked())
return;
QDateTimeEdit::wheelEvent(event);
}
#endif
/*!
\reimp
*/
bool DateTimeEditNullable::focusNextPrevChild(bool next)
{
if (!m_checkBox->isChecked()){
return QAbstractSpinBox::focusNextPrevChild(next);
} else {
return QDateTimeEdit::focusNextPrevChild(next);
}
}
/*!
* \brief sets a date, if date is invalid a
* empty date is shown
*/
void DateTimeEditNullable::setDateTime(const QDateTime &dateTime)
{
if(dateTime.isValid() != m_checkBox->isChecked()){
m_checkBox->setChecked(dateTime.isValid());
// checkBox_toggled(dateTime.isValid());
}else
checkBox_toggled(dateTime.isValid());
if (dateTime.isValid()) {
QDateTimeEdit::setDateTime(dateTime);
}
}
/*!
* \brief sets a date, if date is invalid a
* empty date is shown
*/
void DateTimeEditNullable::setDate(const QDate &date)
{
// qDebug() << Q_FUNC_INFO;
// qDebug() << date << date.isValid() << m_checkBox->isChecked();
if(date.isValid() != m_checkBox->isChecked()){
m_checkBox->setChecked(date.isValid());
// checkBox_toggled(date.isValid());
}else
checkBox_toggled(date.isValid());
if (date.isValid()) {
QDateTimeEdit::setDate(date);
}
}
/*!
* \brief sets a date, if date is invalid a
* empty date is shown
*/
void DateTimeEditNullable::setTime(const QTime &time)
{
if(time.isValid() != m_checkBox->isChecked()){
m_checkBox->setChecked(time.isValid());
// ;
}else
checkBox_toggled(time.isValid());
if (time.isValid()) {
QDateTimeEdit::setTime(time);
}
}
void DateTimeEditNullable::checkBox_toggled(bool value){
// qDebug() << Q_FUNC_INFO << value;
setEnabled(value);
if(!value){
QLineEdit* m_lineEdit = const_cast<QLineEdit*>(lineEdit());
m_lineEdit->setText("");
}else{
QDateTimeEdit::setDateTime(QDateTime(QDATETIMEEDIT_DATE_INITIAL, QDATETIMEEDIT_TIME_MIN));
}
}
void DateTimeEditNullable::setEnabled(bool value){
QWidgetList children = qFindChildren<QWidget*>(this);
children.removeOne(m_checkBox);
foreach(QWidget* child, children){
child->setEnabled(value);
}
// m_checkBox->setChecked(value);
}
void DateTimeEditNullable::setDisabled(bool value){
setEnabled(!value);
}
QSize DateTimeEditNullable::sizeHint() const{
QSize size = QDateTimeEdit::sizeHint();
QStyleOptionFrameV2 panel;
QRect checkBoxClickRect = m_checkBox->style()->subElementRect(QStyle::SE_CheckBoxClickRect, &panel,m_checkBox);
int m_leftWidgetHeight = checkBoxClickRect.height()*1.5;
size.rwidth() += m_leftWidgetHeight;
return size;
// return QDateTimeEdit::sizeHint();
}
void DateTimeEditNullable::resizeEvent(QResizeEvent *event){
Q_ASSERT(m_checkBox);
updateGeometries();
QDateTimeEdit::resizeEvent(event);
}
void DateTimeEditNullable::updateGeometries(){
QStyleOptionFrameV2 panel;
// initStyleOption(&panel);
// QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
// QStyleOptionSpinBox panel;
// initStyleOption(&panel);
// QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
QRect checkBoxClickRect = m_checkBox->style()->subElementRect(QStyle::SE_CheckBoxClickRect, &panel,m_checkBox);
QLineEdit* m_lineEdit = const_cast<QLineEdit*>(lineEdit());
QRect rect = contentsRect();
int height = rect.height();
// int width = rect.width();
int m_leftWidgetHeight = checkBoxClickRect.height();
m_checkBox->setGeometry(rect.x() + 3, rect.y() + (height - m_leftWidgetHeight)/2,
m_leftWidgetHeight, m_leftWidgetHeight);
{ // changer les marges
QMargins marg = m_lineEdit->contentsMargins();
marg.setLeft(marg.right() + m_leftWidgetHeight*1.5);
m_lineEdit->setContentsMargins(marg);
}
// int clearButtonWidth = this->height();
// m_lineEdit->setGeometry(m_checkBox->x() + m_checkBox->width(), 0,
// width + m_checkBox->width(), this->height());
// m_clearButton->setGeometry(this->width() - clearButtonWidth, 0,
// clearButtonWidth, this->height());
} |
Partager