00001 #include "datecalendar.h"
00002 #include <QApplication>
00003 #include "../Widgets/alineedit.h"
00004
00005 DateCalendar::DateCalendar(QWidget *parent): QCalendarWidget(parent)
00006 {
00007 setVisible(false);
00008 connect(this, SIGNAL(clicked(const QDate&)), this, SLOT(dateClicked(const QDate&)));
00009 connect(QApplication::instance(), SIGNAL(focusChanged(QWidget *, QWidget *)), this, SLOT(focusChanged(QWidget *, QWidget *)));
00010 current = NULL;
00011
00012 mainWindow = parent;
00013
00014 setGeometry(QRect(150, 140, 224, 163));
00015 setFirstDayOfWeek(Qt::Monday);
00016 singleSkip = false;
00017 }
00018
00019 void DateCalendar::enableOn(ALineEdit *editor)
00020 {
00021 connect(editor, SIGNAL(returnPressed()), this, SLOT(editReturnPressed()));
00022 connect(editor, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(const QString&)));
00023 editors.append(editor);
00024 }
00025
00026 void DateCalendar::disableOn(ALineEdit *editor)
00027 {
00028 for(int i = 0; i < editors.size(); i++)
00029 {
00030 if(editors[i] == editor)
00031 {
00032 editors.removeAt(i);
00033 break;
00034 }
00035 }
00036 }
00037
00038 void DateCalendar::textChanged(const QString &text)
00039 {
00040 QDate d = QDate::fromString(text, "d.M.yyyy");
00041 if(d.isValid() && isVisible())
00042 {
00043 setSelectedDate(d);
00044 }
00045 }
00046
00047 void DateCalendar::editReturnPressed()
00048 {
00049 QDate d = QDate::fromString(current->text(), "d.M.yyyy");
00050 if(d.isValid())
00051 {
00052 current->setText(d.toString("dd.MM.yyyy"));
00053 setVisible(false);
00054 if(mainWindow)
00055 mainWindow->setFocus();
00056 }
00057 }
00058
00059 void DateCalendar::dateClicked(const QDate &date)
00060 {
00061 current->setText(date.toString("dd.MM.yyyy"));
00062 setVisible(false);
00063 singleSkip = true;
00064 current->setFocus();
00065 if(mainWindow)
00066 mainWindow->setFocus();
00067 }
00068
00069 QPoint DateCalendar::addPS(const QPoint& p, const QSize& s)
00070 {
00071 return QPoint(p.x(), p.y() + s.height());
00072 }
00073
00074 QPoint DateCalendar::subPS(const QPoint& p, const QSize& s)
00075 {
00076 return QPoint(p.x(), p.y() - s.height());
00077 }
00078
00079 void DateCalendar::focusChanged(QWidget *old, QWidget *now)
00080 {
00081 ALineEdit *lineNow = dynamic_cast<ALineEdit *>(now);
00082 bool nowParentNotThis = true;
00083 bool parentThis = false;
00084
00085 if(singleSkip)
00086 {
00087 singleSkip = false;
00088 return;
00089 }
00090
00091 if(now)
00092 {
00093 nowParentNotThis = now->parentWidget() != this;
00094 if(now->parentWidget())
00095 {
00096 if(now->parentWidget()->parentWidget() == this)
00097 {
00098
00099 current->setText(selectedDate().toString("dd.MM.yyyy"));
00100
00101 nowParentNotThis = false;
00102 }
00103 }
00104 }
00105 if(old)
00106 {
00107 parentThis = old->parentWidget() == this;
00108 if(old->parentWidget())
00109 {
00110 if(old->parentWidget()->parentWidget() == this)
00111 parentThis = true;
00112 }
00113 }
00114
00115 if(!nowParentNotThis)
00116 {
00117
00118 setVisible(true);
00119 }
00120
00121 if(isVisible() && ((old == current && current && now != this && nowParentNotThis) || ((old == this || parentThis) && now != current && nowParentNotThis)))
00122 {
00123 current->setText(selectedDate().toString("dd.MM.yyyy"));
00124 setVisible(false);
00125 }
00126 if(!isVisible())
00127 {
00128 if(!lineNow)
00129 {
00130 return;
00131 }
00132 if(editors.contains(lineNow))
00133 {
00134 current = lineNow;
00135
00136 move(addPS(widgetPos(current), current->size()));
00137 QDate d = QDate::fromString(current->text(), "dd.MM.yyyy");
00138 if(d.isValid())
00139 setSelectedDate(d);
00140 setVisible(true);
00141 }
00142 }
00143 }
00144
00145 QPoint DateCalendar::widgetPos(QWidget *widget, int depth)
00146 {
00147 if(widget == mainWindow || depth == 0 || widget == NULL)
00148 return QPoint(0, 0);
00149
00150 return widget->pos() + widgetPos(widget->parentWidget(), depth - 1);
00151 }
00152
00153 DateCalendar::~DateCalendar()
00154 {
00155
00156 }