| 1 | #include "FieldTip.h" |
| 2 | #include "HerculesStudio.h" |
| 3 | |
| 4 | #include <QPainter> |
| 5 | |
| 6 | const QBrush backgroundBrush(QColor(255, 255, 225)); |
| 7 | const QPen outlinePen(Qt::black, 1); |
| 8 | const int textPadding = 6; |
| 9 | int tipHeight = 20; |
| 10 | |
| 11 | |
| 12 | FieldTip::FieldTip(QWidget *parent, bool top) : |
| 13 | QWidget(parent), mTop(top),attachedField() |
| 14 | { |
| 15 | setAttribute(Qt::WA_TranslucentBackground); |
| 16 | expireTimer.setSingleShot(true); |
| 17 | connect(&expireTimer, SIGNAL(timeout()), this, SLOT(deleteLater())); |
| 18 | } |
| 19 | |
| 20 | FieldTip::~FieldTip() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | void FieldTip::showMessage(const QString& message, int timeout/*ms*/) { |
| 25 | if (!attachedField) return; |
| 26 | |
| 27 | setWindowTitle(message); |
| 28 | calculatePosition(); |
| 29 | expireTimer.start(timeout); |
| 30 | show(); |
| 31 | update(); |
| 32 | } |
| 33 | |
| 34 | QSize FieldTip::findBestSize(const QFontMetrics& fontMetrics, const QString& message) { |
| 35 | int width = 100; |
| 36 | QRect rect = fontMetrics.boundingRect(0, 0, width, 0, |
| 37 | Qt::AlignLeft | Qt::TextWordWrap, message); |
| 38 | int count = 0; |
| 39 | float ratio = float(rect.width())/float(rect.height()); |
| 40 | while ((ratio < 3.0 || ratio > 4.0) && count < 50) { |
| 41 | width = ratio < 3.0 ? width+20 : width-20; |
| 42 | rect = fontMetrics.boundingRect(0, 0, width, 0, |
| 43 | Qt::AlignLeft | Qt::TextWordWrap, message); |
| 44 | ratio = float(rect.width())/float(rect.height()); |
| 45 | count++; |
| 46 | } |
| 47 | return rect.size(); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void FieldTip::calculatePosition() { |
| 52 | if (!attachedField) return; |
| 53 | |
| 54 | QPoint fieldPos = attachedField->mapToParent(QPoint(0,0)); |
| 55 | QSize size = findBestSize(this->fontMetrics(), windowTitle()); |
| 56 | hOutDebug(1,"size: " << size.width() << "," << size.height()); |
| 57 | size.setWidth(size.width() + (textPadding*2)); |
| 58 | size.setHeight(size.height() + (textPadding*2) + tipHeight); |
| 59 | |
| 60 | if (mTop) |
| 61 | { |
| 62 | int x = fieldPos.x() + attachedField->width()/2 - size.width() +15; |
| 63 | int y = fieldPos.y() + attachedField->height() ; |
| 64 | move(x, y); resize(size); |
| 65 | hOutDebug(2,fieldPos.x() << "," << fieldPos.y()); |
| 66 | hOutDebug(2,"size: " << size.width() << "," << size.height()); |
| 67 | hOutDebug(2,x << "," << y); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | int x = fieldPos.x() + attachedField->width()/2 - size.width() + 15; |
| 72 | int y = fieldPos.y() - size.height(); |
| 73 | move(x, y); resize(size); |
| 74 | hOutDebug(2,fieldPos.x() << "," << fieldPos.y()); |
| 75 | hOutDebug(2,"size: " << size.width() << "," << size.height()); |
| 76 | hOutDebug(2,x << "," << y); |
| 77 | |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void FieldTip::paintEvent(QPaintEvent*) { |
| 82 | QPainter painter(this); |
| 83 | const int roundness = 12; |
| 84 | |
| 85 | painter.setRenderHint(QPainter::Antialiasing); |
| 86 | |
| 87 | int xbase = width()-15; |
| 88 | int ybase = height() - tipHeight; |
| 89 | QPoint p1, p2, p3; |
| 90 | if (mTop) |
| 91 | { |
| 92 | p1 = QPoint(width()-tipHeight-15, tipHeight+2); // bottom left |
| 93 | p2 = QPoint(xbase, 0); // top |
| 94 | p3 = QPoint(xbase, tipHeight+2); // bottom right |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | p1 = QPoint(width()-tipHeight-15, ybase-2); // top right |
| 99 | p2 = QPoint(xbase, height()); // bottom |
| 100 | p3 = QPoint(xbase, ybase-2); // top left |
| 101 | } |
| 102 | QPolygon triangle; |
| 103 | triangle << p1 << p2 << p3; |
| 104 | |
| 105 | painter.setBrush(backgroundBrush); |
| 106 | painter.setPen(outlinePen); |
| 107 | if (mTop) |
| 108 | painter.drawRoundedRect(1, 1+tipHeight, width()-2, ybase-2, roundness, roundness); |
| 109 | else |
| 110 | painter.drawRoundedRect(1, 1, width()-2, ybase-2, roundness, roundness); |
| 111 | painter.setPen(Qt::transparent); |
| 112 | painter.drawPolygon(triangle); |
| 113 | painter.setPen(outlinePen); |
| 114 | painter.drawLine(p1, p2); |
| 115 | painter.drawLine(p2, p3); |
| 116 | |
| 117 | painter.drawText(textPadding, textPadding, |
| 118 | width()-textPadding, mTop ? ybase+textPadding + tipHeight : ybase-textPadding , |
| 119 | Qt::AlignLeft | Qt::AlignVCenter | Qt::TextWordWrap, |
| 120 | windowTitle()); |
| 121 | } |