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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
#include "multieditordatetime.h"
#include "common/utils.h"
#include "common/unused.h"
#include <QDateTimeEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QVariant>
#include <QString>
#include <QDebug>
#include <QCalendarWidget>
#include <QWheelEvent>
#include <QTableView>
#include <QLabel>
QStringList MultiEditorDateTime::formats;
MultiEditorDateTime::MultiEditorDateTime(QWidget *parent) :
MultiEditorWidget(parent)
{
QVBoxLayout* vbox = new QVBoxLayout();
setLayout(vbox);
dateTimeEdit = new QDateTimeEdit();
dateTimeLabel = new QLabel();
calendar = new QCalendarWidget();
// Extending width, becuase day labels are truncated on some systems.
calendar->setFixedSize(calendar->sizeHint() + QSize(80, 0));
vbox->addWidget(dateTimeEdit);
vbox->addWidget(dateTimeLabel);
vbox->addWidget(calendar);
setDisplayFormat(formats.first());
connect(calendar, &QCalendarWidget::selectionChanged, this, &MultiEditorDateTime::calendarDateChanged);
connect(dateTimeEdit, &QDateTimeEdit::dateChanged, this, &MultiEditorDateTime::dateChanged);
connect(dateTimeEdit, &QDateTimeEdit::timeChanged, this, &MultiEditorDateTime::timeChanged);
setFocusProxy(dateTimeEdit);
updateCalendarDisplay();
}
void MultiEditorDateTime::staticInit()
{
formats << "yyyy-MM-dd hh:mm:ss"
<< "yyyy-MM-dd hh:mm"
<< "yyyy-MM-dd"
<< "yyyy-MM-dd hh:mm:ss.z"
<< "yyyy-MM-ddThh:mm"
<< "yyyy-MM-ddThh:mm:ss"
<< "yyyy-MM-ddThh:mm:ss.z";
}
void MultiEditorDateTime::setDisplayFormat(const QString& format)
{
dateTimeEdit->setDisplayFormat(format);
dateTimeEdit->setMaximumWidth(dateTimeEdit->sizeHint().width());
}
void MultiEditorDateTime::setValue(const QVariant& value)
{
switch (value.userType())
{
case QVariant::DateTime:
dateTimeEdit->setDateTime(value.toDateTime());
break;
case QVariant::Date:
dateTimeEdit->setDate(value.toDate());
break;
default:
{
dateTimeEdit->setDateTime(fromString(value.toString()));
break;
}
}
updateReadOnlyDisplay();
}
QVariant MultiEditorDateTime::getValue()
{
if (formatType == STRING)
return dateTimeEdit->dateTime().toString(originalValueFormat);
else if (formatType == UNIXTIME)
return dateTimeEdit->dateTime().toTime_t();
else if (formatType == JULIAN_DAY)
return toJulian(dateTimeEdit->dateTime());
else
return dateTimeEdit->dateTime().toString(dateTimeEdit->displayFormat());
}
QList<QWidget*> MultiEditorDateTime::getNoScrollWidgets()
{
QList<QWidget*> list;
list << dateTimeEdit << calendar;
QObject* obj = calendar->findChild<QTableView*>("qt_calendar_calendarview");
if (obj)
{
QTableView* view = dynamic_cast<QTableView*>(obj);
if (view)
list << view->viewport();
}
return list;
}
QDateTime MultiEditorDateTime::fromString(const QString& value)
{
QDateTime dateTime;
for (const QString& format : getParsingFormats())
{
dateTime = QDateTime::fromString(value, format);
if (dateTime.isValid())
{
formatType = STRING;
originalValueFormat = format;
return dateTime;
}
}
// Try with unixtime
bool ok;
uint unixtime = value.toUInt(&ok);
if (ok)
{
dateTime = QDateTime::fromTime_t(unixtime);
formatType = UNIXTIME;
return dateTime;
}
// Try with Julian day
double jd = value.toDouble(&ok);
if (ok)
{
dateTime = toGregorian(jd);
formatType = JULIAN_DAY;
return dateTime;
}
formatType = OTHER;
return QDateTime();
}
void MultiEditorDateTime::calendarDateChanged()
{
if (updatingCalendar)
return;
dateTimeEdit->setDate(calendar->selectedDate());
emit valueModified();
}
void MultiEditorDateTime::dateChanged(const QDate& date)
{
updatingCalendar = true;
calendar->setSelectedDate(date);
updatingCalendar = false;
emit valueModified();
}
void MultiEditorDateTime::timeChanged(const QTime& time)
{
UNUSED(time);
emit valueModified();
}
bool MultiEditorDateTime::getReadOnly() const
{
return readOnly;
}
void MultiEditorDateTime::setReadOnly(bool value)
{
readOnly = value;
dateTimeEdit->setVisible(!readOnly);
dateTimeLabel->setVisible(readOnly);
updateReadOnlyDisplay();
}
void MultiEditorDateTime::focusThisWidget()
{
dateTimeEdit->setFocus();
}
QStringList MultiEditorDateTime::getParsingFormats()
{
return formats;
}
void MultiEditorDateTime::updateReadOnlyDisplay()
{
if (!readOnly)
return;
dateTimeLabel->setText(getValue().toString());
QDate date = dateTimeEdit->date();
calendar->setMinimumDate(date);
calendar->setMaximumDate(date);
calendar->setSelectedDate(date);
}
void MultiEditorDateTime::updateCalendarDisplay()
{
if (!showCalendars)
{
calendar->setVisible(false);
return;
}
}
MultiEditorWidget*MultiEditorDateTimePlugin::getInstance()
{
return new MultiEditorDateTime();
}
bool MultiEditorDateTimePlugin::validFor(const DataType& dataType)
{
switch (dataType.getType())
{
case DataType::BLOB:
case DataType::BOOLEAN:
case DataType::BIGINT:
case DataType::DECIMAL:
case DataType::DOUBLE:
case DataType::INTEGER:
case DataType::INT:
case DataType::NUMERIC:
case DataType::REAL:
case DataType::NONE:
case DataType::STRING:
case DataType::TEXT:
case DataType::CHAR:
case DataType::VARCHAR:
case DataType::TIME:
case DataType::ANY:
case DataType::unknown:
break;
case DataType::DATE:
case DataType::DATETIME:
return true;
}
return false;
}
int MultiEditorDateTimePlugin::getPriority(const DataType& dataType)
{
switch (dataType.getType())
{
case DataType::BLOB:
case DataType::BOOLEAN:
case DataType::BIGINT:
case DataType::DECIMAL:
case DataType::DOUBLE:
case DataType::INTEGER:
case DataType::INT:
case DataType::NUMERIC:
case DataType::REAL:
case DataType::NONE:
case DataType::STRING:
case DataType::TEXT:
case DataType::CHAR:
case DataType::VARCHAR:
case DataType::TIME:
case DataType::ANY:
case DataType::unknown:
break;
case DataType::DATE:
return 2;
case DataType::DATETIME:
return 1;
}
return 10;
}
QString MultiEditorDateTimePlugin::getTabLabel()
{
return tr("Date & time");
}
|