tcdelegate.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. #include "tcdelegate.h"
  2. #include <QCalendarWidget>
  3. #include <QCheckBox>
  4. #include <QComboBox>
  5. #include <QDateTimeEdit>
  6. #include <QEvent>
  7. #include <QLineEdit>
  8. #include <QPainter>
  9. #include <QSpinBox>
  10. #include <QApplication>
  11. #include <QDebug>
  12. QString formatNumber(qreal value, const QString &format, int precision = -1);
  13. QString formatValue(const QVariant &value, const QString &format);
  14. QString formatNumber(qreal value, const QString &format, int precision)
  15. {
  16. QString result;
  17. int start = 0;
  18. QString fmt(format);
  19. if (value < 0.00 && !fmt.startsWith("-"))
  20. fmt.insert(0, '-');
  21. bool showNegative = fmt.startsWith('-');
  22. if (showNegative)
  23. start = 1;
  24. for (int i = start; i < fmt.length(); ++i) {
  25. QChar c = fmt[i];
  26. switch (c.unicode()) {
  27. case '.':
  28. case ',':
  29. case '#':
  30. case '0':
  31. case '?': {
  32. bool grouping = false;
  33. bool gotDot = false;
  34. bool gotE = false;
  35. bool gotFraction = false;
  36. int decimalPlaces = 0;
  37. int integerDigits = 0;
  38. int optionalDecimalPlaces = 0;
  39. int optionalIntegerDigits = 0;
  40. int exponentDigits = 0;
  41. int numeratorDigits = 0;
  42. int denominatorDigits = 0;
  43. char ch = fmt[i].toLatin1();
  44. do {
  45. if (ch == '.') {
  46. gotDot = true;
  47. } else if (ch == ',') {
  48. grouping = true;
  49. } else if (ch == 'E' || ch == 'e') {
  50. // SET_TYPE_OR_RETURN(KoGenStyle::NumericScientificStyle);
  51. if (i >= fmt.length() - 1)
  52. break;
  53. const char chN = fmt[i + 1].toLatin1();
  54. if (chN == '-' || chN == '+') {
  55. gotE = true;
  56. ++i;
  57. }
  58. } else if (ch == '0' && gotE) {
  59. ++exponentDigits;
  60. } else if (ch == '0' && !gotDot && !gotFraction) {
  61. ++integerDigits;
  62. } else if (ch == '#' && !gotDot && !gotFraction) {
  63. ++optionalIntegerDigits;
  64. } else if (ch == '0' && gotDot && !gotFraction) {
  65. ++decimalPlaces;
  66. } else if (ch == '#' && gotDot && !gotFraction) {
  67. ++optionalDecimalPlaces;
  68. } else if (ch == '?' && !gotFraction) {
  69. ++numeratorDigits;
  70. } else if (ch == '?' && gotFraction) {
  71. ++denominatorDigits;
  72. } else if (ch == '/') {
  73. // SET_TYPE_OR_RETURN(KoGenStyle::NumericFractionStyle);
  74. if (gotDot)
  75. return QString(); // invalid
  76. gotFraction = true;
  77. }
  78. if (i >= fmt.length() - 1)
  79. break;
  80. ch = fmt[++i].toLatin1();
  81. if (ch == ' ') {
  82. // spaces are not allowed - but there's an exception: if this is a fraction. Let's check for '?' or '/'
  83. const char c = fmt[i + 1].toLatin1();
  84. if (c == '?' || c == '/')
  85. ch = fmt[++i].toLatin1();
  86. }
  87. } while (i < fmt.length()
  88. && (ch == '.' || ch == ',' || ch == '#' || ch == '0' || ch == 'E' || ch == 'e'
  89. || ch == '?' || ch == '/'));
  90. if (!(ch == '.' || ch == ',' || ch == '#' || ch == '0' || ch == 'E' || ch == 'e'
  91. || ch == '?' || ch == '/')) {
  92. --i;
  93. }
  94. QString v(QString::number(qAbs(value),
  95. 'f',
  96. precision >= 0 ? precision
  97. : (optionalDecimalPlaces + decimalPlaces)));
  98. int p = v.indexOf('.');
  99. QString integerValue = p >= 0 ? v.left(p) : v;
  100. if (integerValue.length() < integerDigits)
  101. integerValue.prepend(QString().fill('0', integerDigits - integerValue.length()));
  102. QString decimalValue = p >= 0 ? v.mid(p + 1) : QString();
  103. if (decimalValue.length() < decimalPlaces)
  104. decimalValue.append(QString().fill('0', decimalPlaces - decimalValue.length()));
  105. if (grouping && integerValue.size() > 3) {
  106. QLocale lc;
  107. int y = -1, sz = integerValue.size();
  108. for (int i = sz; i > 0; i--) {
  109. y++;
  110. if (y == 3) {
  111. integerValue.insert(i, lc.groupSeparator());
  112. y = 0;
  113. }
  114. }
  115. }
  116. if (showNegative && value < 0)
  117. result.append('-');
  118. result.append(integerValue);
  119. if (!decimalValue.isEmpty())
  120. result.append('.' + decimalValue);
  121. } break;
  122. case '\\': { // backslash escapes the next char
  123. if (i < fmt.length() - 1) {
  124. result.append(fmt[++i]);
  125. }
  126. } break;
  127. default:
  128. result.append(c);
  129. break;
  130. }
  131. }
  132. return result;
  133. }
  134. QString formatValue(const QVariant &value, const QString &format)
  135. {
  136. switch (value.type()) {
  137. case QVariant::Bool:
  138. break;
  139. case QVariant::Int:
  140. case QVariant::UInt:
  141. case QVariant::LongLong:
  142. case QVariant::ULongLong:
  143. case QVariant::Double:
  144. return formatNumber(value.toDouble(), format);
  145. case QVariant::String:
  146. return value.toString();
  147. case QVariant::Date: {
  148. QDate dt(value.toDate());
  149. return dt.toString(format.isEmpty() ? "yyyy-MM-dd" : format);
  150. }
  151. case QVariant::Time: {
  152. QTime dt(value.toTime());
  153. return dt.toString(format.isEmpty() ? "HH:mm:ss" : format);
  154. }
  155. case QVariant::DateTime: {
  156. QDateTime dt(value.toDateTime());
  157. return dt.toString(format.isEmpty() ? "yyyy-MM-dd HH:mm:ss" : format);
  158. }
  159. default:
  160. return value.toString();
  161. }
  162. return "";
  163. }
  164. class TCDateTimeDelegatePrivate
  165. {
  166. public:
  167. TCDateTimeDelegatePrivate() {}
  168. TCItemDelegate::ItemType type = TCItemDelegate::ItemType::LINEEDIT;
  169. QColor textColor = "0";
  170. QString checkText = "true";
  171. QString uncheckText = "false";
  172. QStringList comboboxLists;
  173. QStringList comboboxUserDateList;
  174. double min = 0;
  175. double max = 999;
  176. QString format;
  177. public:
  178. void paintProgress(QPainter *painter,
  179. const QStyleOptionViewItem &option,
  180. const QModelIndex &index) const;
  181. void paintButton(QPainter *painter,
  182. const QStyleOptionViewItem &option,
  183. const QModelIndex &index) const;
  184. void drawCheckBox(QPainter *painter,
  185. const QStyleOptionViewItem &option,
  186. const QModelIndex &index) const;
  187. //日期时间
  188. void drawDateTimeEdit(QPainter *painter,
  189. const QStyleOptionViewItem &option,
  190. const QModelIndex &index) const;
  191. //日期
  192. void drawDateEdit(QPainter *painter,
  193. const QStyleOptionViewItem &option,
  194. const QModelIndex &index) const;
  195. //时间
  196. void drawTimeEdit(QPainter *painter,
  197. const QStyleOptionViewItem &option,
  198. const QModelIndex &index) const;
  199. void drawColor(QPainter *painter,
  200. const QStyleOptionViewItem &option,
  201. const QModelIndex &index) const;
  202. void drawLineEdit(QPainter *painter,
  203. const QStyleOptionViewItem &option,
  204. const QModelIndex &index) const;
  205. void drawCombobox(QPainter *painter,
  206. const QStyleOptionViewItem &option,
  207. const QModelIndex &index) const;
  208. };
  209. void TCDateTimeDelegatePrivate::paintProgress(QPainter *painter,
  210. const QStyleOptionViewItem &option,
  211. const QModelIndex &index) const
  212. {
  213. int value = index.data().toInt();
  214. QStyleOptionProgressBar progressBarOption;
  215. progressBarOption.rect = option.rect.adjusted(4, 4, -4, -4);
  216. progressBarOption.minimum = 0;
  217. progressBarOption.maximum = 100;
  218. progressBarOption.textAlignment = Qt::AlignRight;
  219. progressBarOption.textVisible = true;
  220. progressBarOption.progress = value;
  221. progressBarOption.text = QString("%1%").arg(progressBarOption.progress);
  222. painter->save();
  223. if (option.state & QStyle::State_Selected) {
  224. painter->fillRect(option.rect, option.palette.highlight());
  225. painter->setBrush(option.palette.highlightedText());
  226. }
  227. QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
  228. painter->restore();
  229. }
  230. void TCDateTimeDelegatePrivate::paintButton(QPainter *painter,
  231. const QStyleOptionViewItem &option,
  232. const QModelIndex &index) const
  233. {
  234. QString value = index.data().toString();
  235. QStyleOptionButton button;
  236. button.text = value;
  237. button.rect = option.rect.adjusted(4, 4, -4, -4);
  238. button.state |= QStyle::State_Enabled;
  239. painter->save();
  240. if (option.state & QStyle::State_Selected)
  241. painter->fillRect(option.rect, option.palette.highlight());
  242. painter->restore();
  243. QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
  244. }
  245. void TCDateTimeDelegatePrivate::drawCheckBox(QPainter *painter,
  246. const QStyleOptionViewItem &option,
  247. const QModelIndex &index) const
  248. {
  249. bool data = index.data(Qt::DisplayRole).toBool();
  250. QColor bgColorOff, textColorOff; //关闭时背景颜色
  251. QColor bgColorOn, textColorOn; //打开时背景颜色
  252. bgColorOff = QColor(111, 122, 126);
  253. bgColorOn = QColor(21, 156, 119);
  254. textColorOff = QColor(250, 250, 250);
  255. textColorOn = QColor(255, 255, 255);
  256. QColor bgColor = data ? bgColorOn : bgColorOff;
  257. painter->setBrush(bgColor);
  258. QRect rect = option.rect;
  259. painter->drawRoundedRect(rect, 0, 0);
  260. if (data) {
  261. painter->setPen(textColorOn);
  262. painter->drawText(rect, Qt::AlignCenter, checkText);
  263. } else {
  264. painter->setPen(textColorOff);
  265. painter->drawText(rect, Qt::AlignCenter, uncheckText);
  266. }
  267. // QStyleOption styleOption;
  268. // styleOption.rect = option.rect;
  269. // styleOption.state = data ? QStyle::State_On : QStyle::State_Off;
  270. // styleOption.state |= QStyle::State_Enabled;
  271. // QCheckBox widget;
  272. // QApplication::style()->drawPrimitive(QStyle::PE_IndicatorCheckBox,
  273. // &styleOption,
  274. // painter,
  275. // &widget);
  276. }
  277. void TCDateTimeDelegatePrivate::drawDateTimeEdit(QPainter *painter,
  278. const QStyleOptionViewItem &option,
  279. const QModelIndex &index) const
  280. {
  281. const QVariant dateTime = index.data(Qt::DisplayRole).toDateTime();
  282. painter->drawText(option.rect, Qt::AlignLeft | Qt::AlignVCenter, formatValue(dateTime, format));
  283. }
  284. void TCDateTimeDelegatePrivate::drawDateEdit(QPainter *painter,
  285. const QStyleOptionViewItem &option,
  286. const QModelIndex &index) const
  287. {
  288. const QVariant dateTime = index.data(Qt::DisplayRole).toDate();
  289. painter->drawText(option.rect, Qt::AlignLeft | Qt::AlignVCenter, formatValue(dateTime, format));
  290. }
  291. void TCDateTimeDelegatePrivate::drawTimeEdit(QPainter *painter,
  292. const QStyleOptionViewItem &option,
  293. const QModelIndex &index) const
  294. {
  295. const QVariant time = index.data(Qt::DisplayRole).toTime();
  296. painter->drawText(option.rect, Qt::AlignLeft | Qt::AlignVCenter, formatValue(time, format));
  297. }
  298. void TCDateTimeDelegatePrivate::drawColor(QPainter *painter,
  299. const QStyleOptionViewItem &option,
  300. const QModelIndex &index) const
  301. {
  302. QString data = index.data(Qt::DisplayRole).toString();
  303. data = (data == "transparent") ? "black" : data;
  304. QColor color(data);
  305. if (color.isValid()) {
  306. //根据背景色自动计算合适的前景色
  307. double gray = (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255;
  308. QColor textColor = gray > 0.5 ? Qt::black : Qt::white;
  309. painter->save();
  310. painter->fillRect(option.rect, color);
  311. painter->setPen(textColor);
  312. painter->drawText(option.rect, Qt::AlignCenter, color.name().toUpper());
  313. painter->restore();
  314. }
  315. }
  316. void TCDateTimeDelegatePrivate::drawLineEdit(QPainter *painter,
  317. const QStyleOptionViewItem &option,
  318. const QModelIndex &index) const
  319. {
  320. QString data = index.data(Qt::DisplayRole).toString();
  321. int len = data.length();
  322. QString text;
  323. for (int i = 0; i < len; i++) {
  324. text += "-";
  325. }
  326. QRect rect = option.rect;
  327. rect.setX(rect.x() + 2);
  328. painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, text);
  329. }
  330. void TCDateTimeDelegatePrivate::drawCombobox(QPainter * /*painter*/,
  331. const QStyleOptionViewItem & /*option*/,
  332. const QModelIndex & /*index*/) const
  333. {
  334. // TODO: 处理翻译
  335. // const QString displayText = index.data(Qt::DisplayRole).toString();
  336. // const QString userText = index.data(Qt::UserRole).toString();
  337. // const QString text = userText.isEmpty() ? displayText : userText;
  338. // qDebug() << displayText << userText << text;
  339. // painter->fillRect(option.rect, index.data(Qt::BackgroundRole).value<QColor>());
  340. // painter->drawText(option.rect, Qt::AlignLeft | Qt::AlignVCenter, text);
  341. // qApp->translate(text.constData()));
  342. // painter->restore();
  343. }
  344. TCItemDelegate::TCItemDelegate(QObject *parent)
  345. : QStyledItemDelegate(parent)
  346. , d(new TCDateTimeDelegatePrivate)
  347. {
  348. QWidget *w = qobject_cast<QWidget *>(parent);
  349. if (w) {
  350. d->textColor = w->palette().color(QPalette::WindowText).name();
  351. // selectBgColor = w->palette().color(QPalette::Window).name();
  352. }
  353. }
  354. TCItemDelegate::TCItemDelegate(ItemType type, QObject *parent)
  355. : QStyledItemDelegate(parent)
  356. , d(new TCDateTimeDelegatePrivate)
  357. {
  358. d->type = type;
  359. QWidget *w = qobject_cast<QWidget *>(parent);
  360. if (w) {
  361. d->textColor = w->palette().color(QPalette::WindowText).name();
  362. // selectBgColor = w->palette().color(QPalette::Window).name();
  363. }
  364. }
  365. TCItemDelegate::~TCItemDelegate()
  366. {
  367. delete d;
  368. }
  369. void TCItemDelegate::setCheckText(const QString &checkText, const QString &uncheckText)
  370. {
  371. d->checkText = checkText;
  372. d->uncheckText = uncheckText;
  373. }
  374. void TCItemDelegate::addComboxItems(const QStringList &texts, const QStringList &userDate)
  375. {
  376. d->comboboxLists = texts;
  377. if (userDate.isEmpty()) {
  378. d->comboboxUserDateList = texts;
  379. } else {
  380. if (texts.size() != userDate.size()) {
  381. qDebug() << "size err ";
  382. d->comboboxUserDateList = texts;
  383. } else {
  384. d->comboboxUserDateList = userDate;
  385. }
  386. }
  387. }
  388. void TCItemDelegate::setSpinBoxRange(double min, double max)
  389. {
  390. d->min = min;
  391. d->max = max;
  392. }
  393. void TCItemDelegate::setFormat(const QString &format)
  394. {
  395. d->format = format;
  396. }
  397. QWidget *TCItemDelegate::createEditor(QWidget *parent,
  398. const QStyleOptionViewItem & /*option*/,
  399. const QModelIndex & /*index*/) const
  400. {
  401. switch (d->type) {
  402. case TCItemDelegate::LINEEDIT: {
  403. return new QLineEdit(parent);
  404. }
  405. case TCItemDelegate::CHECKBOX: {
  406. return nullptr; // new QCheckBox(parent);
  407. }
  408. case TCItemDelegate::INTSPIN: {
  409. QSpinBox *editor = new QSpinBox(parent);
  410. editor->setMinimum(d->min);
  411. editor->setMaximum(d->max);
  412. return editor;
  413. }
  414. case TCItemDelegate::DOUBLESPIN: {
  415. QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
  416. editor->setMinimum(d->min);
  417. editor->setMaximum(d->max);
  418. return editor;
  419. }
  420. case TCItemDelegate::PROGRESS: {
  421. QSpinBox *editor = new QSpinBox(parent);
  422. return editor;
  423. }
  424. case TCItemDelegate::BUTTON: {
  425. return new QLineEdit(parent);
  426. }
  427. case TCItemDelegate::COMBOBOX: {
  428. QComboBox *combobox = new QComboBox(parent);
  429. for (int var = 0; var < d->comboboxLists.size(); ++var) {
  430. combobox->addItem(d->comboboxLists[var], d->comboboxUserDateList[var]);
  431. }
  432. return combobox;
  433. }
  434. case TCItemDelegate::DATETIME: {
  435. QDateTimeEdit *dateTime = new QDateTimeEdit(parent);
  436. dateTime->setCalendarPopup(true);
  437. dateTime->setDisplayFormat("yyyy-MM-dd HH:mm:ss");
  438. dateTime->calendarWidget()->setLocale(QLocale::Chinese);
  439. return dateTime;
  440. }
  441. case TCItemDelegate::DATE: {
  442. QDateEdit *date = new QDateEdit(parent);
  443. date->setCalendarPopup(true);
  444. date->setDisplayFormat("yyyy-MM-dd");
  445. date->calendarWidget()->setLocale(QLocale::Chinese);
  446. return date;
  447. }
  448. case TCItemDelegate::TIME: {
  449. QTimeEdit *time = new QTimeEdit(parent);
  450. time->setDisplayFormat("HH:mm:ss");
  451. return time;
  452. }
  453. default:
  454. return nullptr;
  455. }
  456. }
  457. void TCItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  458. {
  459. QVariant value = index.data();
  460. if (!value.isValid()) {
  461. QStyledItemDelegate::setEditorData(editor, index);
  462. return;
  463. }
  464. switch (d->type) {
  465. case TCItemDelegate::LINEEDIT: {
  466. QLineEdit *pEdit = static_cast<QLineEdit *>(editor);
  467. if (pEdit) {
  468. pEdit->setText(value.toString());
  469. }
  470. } break;
  471. // case TCItemDelegate::CHECKBOX: {
  472. // QCheckBox *pEdit = static_cast<QCheckBox *>(editor);
  473. // if (pEdit) {
  474. // pEdit->setChecked(value.toInt() == 1);
  475. // }
  476. // } break;
  477. case TCItemDelegate::INTSPIN: {
  478. QSpinBox *pEdit = static_cast<QSpinBox *>(editor);
  479. if (pEdit) {
  480. pEdit->setValue(value.toInt());
  481. }
  482. } break;
  483. case TCItemDelegate::DOUBLESPIN: {
  484. QDoubleSpinBox *pEdit = static_cast<QDoubleSpinBox *>(editor);
  485. if (pEdit) {
  486. pEdit->setValue(value.toDouble());
  487. }
  488. } break;
  489. case TCItemDelegate::PROGRESS: {
  490. QSpinBox *pEdit = static_cast<QSpinBox *>(editor);
  491. if (pEdit) {
  492. pEdit->setValue(value.toInt());
  493. }
  494. } break;
  495. case TCItemDelegate::BUTTON: {
  496. QLineEdit *pEdit = static_cast<QLineEdit *>(editor);
  497. if (pEdit) {
  498. pEdit->setText(value.toString());
  499. }
  500. } break;
  501. case TCItemDelegate::COMBOBOX: {
  502. QComboBox *pEdit = static_cast<QComboBox *>(editor);
  503. if (pEdit) {
  504. pEdit->setCurrentText(value.toString());
  505. }
  506. } break;
  507. case TCItemDelegate::DATETIME: {
  508. QDateTime dateTime = index.data().toDateTime();
  509. QDateTimeEdit *pEdit = static_cast<QDateTimeEdit *>(editor);
  510. if (pEdit) {
  511. pEdit->setDateTime(dateTime);
  512. }
  513. } break;
  514. case TCItemDelegate::DATE: {
  515. QDate date = index.data().toDate();
  516. QDateEdit *dateEdit = static_cast<QDateEdit *>(editor);
  517. if (dateEdit) {
  518. dateEdit->setDate(date);
  519. }
  520. } break;
  521. case TCItemDelegate::TIME: {
  522. QTime time = index.data().toTime();
  523. QTimeEdit *timeEdit = static_cast<QTimeEdit *>(editor);
  524. if (timeEdit) {
  525. timeEdit->setTime(time);
  526. }
  527. } break;
  528. default:
  529. QStyledItemDelegate::setEditorData(editor, index);
  530. }
  531. }
  532. void TCItemDelegate::setModelData(QWidget *editor,
  533. QAbstractItemModel *model,
  534. const QModelIndex &index) const
  535. {
  536. QVariant data = QVariant();
  537. switch (d->type) {
  538. case TCItemDelegate::LINEEDIT: {
  539. QLineEdit *pEdit = static_cast<QLineEdit *>(editor);
  540. if (pEdit) {
  541. data = pEdit->text();
  542. }
  543. } break;
  544. // case TCItemDelegate::CHECKBOX: {
  545. // QCheckBox *pEdit = static_cast<QCheckBox *>(editor);
  546. // if (pEdit) {
  547. // data = pEdit->isChecked() ? 1 : 0;
  548. // }
  549. // } break;
  550. case TCItemDelegate::INTSPIN: {
  551. QSpinBox *pEdit = static_cast<QSpinBox *>(editor);
  552. if (pEdit) {
  553. pEdit->interpretText();
  554. data = pEdit->value();
  555. }
  556. } break;
  557. case TCItemDelegate::DOUBLESPIN: {
  558. QDoubleSpinBox *pEdit = static_cast<QDoubleSpinBox *>(editor);
  559. if (pEdit) {
  560. pEdit->interpretText();
  561. data = pEdit->value();
  562. }
  563. } break;
  564. case TCItemDelegate::PROGRESS: {
  565. QSpinBox *pEdit = static_cast<QSpinBox *>(editor);
  566. if (pEdit) {
  567. pEdit->interpretText();
  568. data = pEdit->value();
  569. }
  570. } break;
  571. case TCItemDelegate::BUTTON: {
  572. QLineEdit *pEdit = static_cast<QLineEdit *>(editor);
  573. if (pEdit) {
  574. data = pEdit->text();
  575. }
  576. } break;
  577. case TCItemDelegate::COMBOBOX: {
  578. QComboBox *pEdit = static_cast<QComboBox *>(editor);
  579. if (pEdit) {
  580. data = pEdit->currentData();
  581. }
  582. } break;
  583. case TCItemDelegate::DATETIME: {
  584. QDateTimeEdit *dateTime = static_cast<QDateTimeEdit *>(editor);
  585. if (dateTime) {
  586. data = dateTime->dateTime();
  587. //.toString("yyyy-MM-dd HH:mm:ss");
  588. }
  589. } break;
  590. case TCItemDelegate::DATE: {
  591. QDateEdit *date = static_cast<QDateEdit *>(editor);
  592. if (date) {
  593. data = date->date();
  594. //.toString("yyyy-MM-dd");
  595. }
  596. } break;
  597. case TCItemDelegate::TIME: {
  598. QTimeEdit *time = static_cast<QTimeEdit *>(editor);
  599. if (time) {
  600. data = time->time();
  601. //.toString("HH:mm:ss");
  602. }
  603. } break;
  604. default:
  605. data = index.data();
  606. }
  607. model->setData(index, data);
  608. }
  609. void TCItemDelegate::updateEditorGeometry(QWidget *editor,
  610. const QStyleOptionViewItem &option,
  611. const QModelIndex &) const
  612. {
  613. editor->setGeometry(option.rect);
  614. }
  615. bool TCItemDelegate::editorEvent(QEvent *event,
  616. QAbstractItemModel *model,
  617. const QStyleOptionViewItem &option,
  618. const QModelIndex &index)
  619. {
  620. if (event->type() == QEvent::MouseButtonRelease) {
  621. switch (d->type) {
  622. case TCItemDelegate::CHECKBOX: {
  623. QVariant value = index.data();
  624. if (value.isValid()) {
  625. bool checked = value.toBool();
  626. model->setData(index, !checked, Qt::EditRole);
  627. }
  628. } break;
  629. default:
  630. return QStyledItemDelegate::editorEvent(event, model, option, index);
  631. }
  632. }
  633. return QStyledItemDelegate::editorEvent(event, model, option, index);
  634. }
  635. void TCItemDelegate::paint(QPainter *painter,
  636. const QStyleOptionViewItem &option,
  637. const QModelIndex &index) const
  638. {
  639. painter->setPen(d->textColor);
  640. switch (d->type) {
  641. // case TCItemDelegate::COLOR:
  642. // break;
  643. // case TCItemDelegate::COMBOBOX:
  644. // d->drawCombobox(painter, option, index);
  645. // break;
  646. case TCItemDelegate::LINEEDIT:
  647. d->drawLineEdit(painter, option, index);
  648. break;
  649. case TCItemDelegate::CHECKBOX:
  650. d->drawCheckBox(painter, option, index);
  651. // QStyledItemDelegate::paint(painter, option, index);
  652. break;
  653. case TCItemDelegate::INTSPIN:
  654. QStyledItemDelegate::paint(painter, option, index);
  655. break;
  656. case TCItemDelegate::DOUBLESPIN:
  657. QStyledItemDelegate::paint(painter, option, index);
  658. break;
  659. case TCItemDelegate::PROGRESS:
  660. d->paintProgress(painter, option, index);
  661. break;
  662. case TCItemDelegate::BUTTON:
  663. d->paintButton(painter, option, index);
  664. break;
  665. case TCItemDelegate::DATETIME:
  666. d->drawDateTimeEdit(painter, option, index);
  667. break;
  668. case TCItemDelegate::DATE:
  669. d->drawDateEdit(painter, option, index);
  670. break;
  671. case TCItemDelegate::TIME:
  672. d->drawTimeEdit(painter, option, index);
  673. break;
  674. default: {
  675. QStyledItemDelegate::paint(painter, option, index);
  676. break;
  677. }
  678. }
  679. }