void MoveDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &) const { QStyleOptionViewItem button = option; QRect rect = option.rect; QRect rectLeft(rect.x() + rect.width()/4 - 8, rect.y() + rect.height()/2 - 5, 30, 15); QRect rectRight(rect.x() + rect.width()*3/4 -8, rect.y() + rect.height()/2 - 5 , 30, 15); QPoint pointUp, pointDown; pointUp.setX(rect.x() + rect.width()/4 - 8); pointUp.setY(rect.y() + rect.height()/2 + 5); pointDown.setX(rect.x() + rect.width()*3/4 -8); pointDown.setY(rect.y() + rect.height()/2 + 5); painter->save(); painter->setPen(leftColor); //QColor(108, 162, 234) (254, 107, 107) painter->drawText(pointUp, leftText); painter->save(); painter->setPen(rightColor); painter->drawText(pointDown, rightText); painter->restore(); // this->drawDisplay(painter, button, rectBu, _text); this->drawFocus(painter, button, rectLeft); this->drawFocus(painter, button, rectRight); painter->restore(); }
static int findCursorPos(struct OzUITextEdit *textEdit, int x, int y) { int best, bx, by, i, x1, y1; best = textEdit->cursorPos; bx = rectLeft(&textEdit->layout->charLayout[best].rect); by = rectBottom(&textEdit->layout->charLayout[best].rect); for (i=0; i<textEdit->layout->chars; i++) { x1 = rectLeft(&textEdit->layout->charLayout[i].rect); y1 = rectBottom(&textEdit->layout->charLayout[i].rect); if ((ABS(by-y) > ABS(y1-y)) || ((ABS(by-y) == ABS(y1-y)) && (ABS(bx-x) > ABS(x1-x)))) { best = i; bx = x1; by = y1; } } return best; }
static void textEditPaint(struct OzUIWidget *widget) { struct Rect rect, cursorRect; struct OzUITextEdit *textEdit; struct Point p1, p2; int dx, dy; textEdit = (struct OzUITextEdit*)widget->d; initRect(&rect, 0, 0, widget->rect.w, widget->rect.h); //initRect(&tlc.rect, 0, 0, 100000, 100000); OzUIWidgetDrawRectangle(widget, &rect, &blackLS, &whiteFS); if (textEdit && textEdit->layout) { copyRect(&cursorRect, &textEdit->layout->charLayout[textEdit->cursorPos].rect); cursorRect.w = textEdit->fontSize/2; dx = dy = 0; if (rectLeft(&cursorRect) < rectLeft(&rect)) dx = rectLeft(&rect) - rectLeft(&cursorRect); if (rectRight(&cursorRect) > rectRight(&rect)) dx = rectRight(&rect) - rectRight(&cursorRect); if (rectTop(&cursorRect) < rectTop(&rect)) dy = rectTop(&rect) - rectTop(&cursorRect); if (rectBottom(&cursorRect) > rectBottom(&rect)) dy = rectBottom(&rect) - rectBottom(&cursorRect); OzUITextLayoutTransform(textEdit->layout, dx, dy); translateRect(&cursorRect, dx, dy); OzUIWidgetDrawTextLayout(widget, &tlc, &blackLS, textEdit->layout); if (widget->window->focusWidget == widget) { p1.x = rectLeft(&cursorRect); p1.y = rectBottom(&cursorRect); p2.x = rectRight(&cursorRect); p2.y = rectBottom(&cursorRect); OzUIWidgetDrawLine(widget, &p1, &p2, &blackLS); } } }
void SetInfoDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem button = option; QRect rect = option.rect; QRect rectAdd(rect.left() + rect.width()/2 - 15, rect.top() + rect.height()/2 - 5, 30, 15); QRect rectLeft(rect.x() + rect.width()/4 - 8, rect.y() + rect.height()/2 - 5, 30, 15); QRect rectRight(rect.x() + rect.width()*3/4 -8, rect.y() + rect.height()/2 - 5 , 30, 15); if(index.row() != rows){ QPoint pointUp, pointDown; pointUp.setX(rect.x() + rect.width()/4 - 8); pointUp.setY(rect.y() + rect.height()/2 + 5); pointDown.setX(rect.x() + rect.width()*3/4 -8); pointDown.setY(rect.y() + rect.height()/2 + 5); painter->save(); painter->setPen(QColor(108, 162, 234)); //QColor(108, 162, 234) (254, 107, 107) painter->drawText(pointUp, "修改"); painter->save(); painter->setPen(QColor(254, 107, 107)); painter->drawText(pointDown, "删除"); painter->restore(); // this->drawDisplay(painter, button, rectBu, _text); this->drawFocus(painter, button, rectLeft); this->drawFocus(painter, button, rectRight); painter->restore(); } if(index.row() == rows){ QPoint point; point.setX(rect.left()+ rect.width()/2 - 12); point.setY(rect.top() + rect.height()/2 + 5); painter->save(); painter->setPen(QColor(108, 162, 234));// (QColor(254, 107, 107)); //(254, 107, 107) button.displayAlignment = Qt::AlignCenter; painter->drawText(point, "添加"); this->drawFocus(painter, button, rectAdd); painter->restore(); } }
bool SetInfoDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { QRect rect = option.rect; QRect rectLeft(rect.x() + rect.width()/4 - 8, rect.y() + rect.height()/2 - 5 , 30, 15); QRect rectRight(rect.x() + rect.width()*3/4 -8, rect.y() + rect.height()/2 - 5 , 30, 15); QRect rectAdd(rect.left() + rect.width()/2 - 15, rect.top() + rect.height()/2 - 5, 30, 15); if (event->type() == QEvent::MouseMove) { // QMouseEvent* e =(QMouseEvent*)event; } if (event->type() == QEvent::MouseButtonRelease) { QMouseEvent* e =(QMouseEvent*)event; int row = index.row(); if(row != rows){ if (rectLeft.contains(e->x(), e->y())) { emit left(row); return true; }else if(rectRight.contains(e->x(), e->y())){ emit right(row); return true; } } if(row == rows){ if(rectAdd.contains(e->x(), e->y())){ emit currentRow(row); return true; } } } return false; }
static void textEditKeyEvent(struct OzUIWidget *widget, struct OzUIKeyEvent *keyEvent) { struct OzUITextEdit *textEdit; struct Rect cursorRect; wchar_t *text; wchar_t wch; textEdit = (struct OzUITextEdit*)widget->d; if (keyEvent->type != OZUI_KEY_EVENT_DOWN) return; switch (keyEvent->code) { case Key_Left: if (!keyEvent->mask) OzUITextEditSetCursorPos(textEdit, textEdit->cursorPos-1); break; case Key_Right: if (!keyEvent->mask) OzUITextEditSetCursorPos(textEdit, textEdit->cursorPos+1); break; case Key_Up: if (!keyEvent->mask) { getCursorRect(textEdit, &cursorRect); OzUITextEditSetCursorPos(textEdit, findCursorPos(textEdit, rectLeft(&cursorRect), rectBottom(&cursorRect) - textEdit->layout->height)); } break; case Key_Down: if (!keyEvent->mask) { getCursorRect(textEdit, &cursorRect); OzUITextEditSetCursorPos(textEdit, findCursorPos(textEdit, rectLeft(&cursorRect), rectBottom(&cursorRect) + textEdit->layout->height)); } break; case Key_Delete: if (textEdit->cursorPos < textEdit->layout->chars-1) { text = (wchar_t*)calloc(textEdit->layout->chars, sizeof(wchar_t)); wcsncpy(text, textEdit->text, textEdit->cursorPos); wcscpy(&text[textEdit->cursorPos], &textEdit->text[textEdit->cursorPos+1]); OzUITextEditSetText(textEdit, text); free(text); } break; case Key_Backspace: if (textEdit->cursorPos) { text = (wchar_t*)calloc(textEdit->layout->chars, sizeof(wchar_t)); wcsncpy(text, textEdit->text, textEdit->cursorPos-1); wcscpy(&text[textEdit->cursorPos-1], &textEdit->text[textEdit->cursorPos]); textEdit->cursorPos--; OzUITextEditSetText(textEdit, text); free(text); } break; case Key_Return: if (textEdit->allowMultiline) { wch = L'\n'; text = (wchar_t*)calloc(textEdit->layout->chars+1, sizeof(wchar_t)); wcsncpy(text, textEdit->text, textEdit->cursorPos); text[textEdit->cursorPos] = wch; wcscpy(&text[textEdit->cursorPos+1], &textEdit->text[textEdit->cursorPos]); textEdit->cursorPos++; OzUITextEditSetText(textEdit, text); free(text); } break; default: wch = OzUIKeyEventTranslate(keyEvent); if (wch) { text = (wchar_t*)calloc(textEdit->layout->chars+1, sizeof(wchar_t)); wcsncpy(text, textEdit->text, textEdit->cursorPos); text[textEdit->cursorPos] = wch; wcscpy(&text[textEdit->cursorPos+1], &textEdit->text[textEdit->cursorPos]); textEdit->cursorPos++; OzUITextEditSetText(textEdit, text); free(text); } } }