void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QPixmap& pixmap) { const IconMetrics& iconMetrics = defaultIconMetrics(); QGraphicsPixmapItem *iconItem = 0; double yValue = title->boundingRect().height() + iconMetrics.spacing; Q_FOREACH (ToolTip t, toolTips) { yValue += t.second->boundingRect().height(); } if (entryToolTip.second) { yValue += entryToolTip.second->boundingRect().height(); } iconItem = new QGraphicsPixmapItem(this); if (!icon.isNull()) { iconItem->setPixmap(icon.pixmap(iconMetrics.sz_small, iconMetrics.sz_small)); } else if (!pixmap.isNull()) { iconItem->setPixmap(pixmap); } const int sp2 = iconMetrics.spacing * 2; iconItem->setPos(sp2, yValue); QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this); textItem->setPos(sp2 + iconMetrics.sz_small + sp2, yValue); textItem->setBrush(QBrush(Qt::white)); textItem->setFlag(ItemIgnoresTransformations); toolTips.push_back(qMakePair(iconItem, textItem)); }
SPixmap scaleImages(const QString &s) { static QHash <QString, QImage > cache; SPixmap ret; ret.first = s; if (cache.contains(s)) { ret.second = cache.value(s); } else { int dim = defaultIconMetrics().sz_pic; QImage p = QImage(s).scaled(dim, dim, Qt::KeepAspectRatio); cache.insert(s, p); ret.second = p; } return ret; }
SPixmap scaleImages(picturepointer picture) { static QHash <QString, QImage > cache; SPixmap ret; ret.first = picture; if (cache.contains(picture->filename) && !cache.value(picture->filename).isNull()) { ret.second = cache.value(picture->filename); } else { int dim = defaultIconMetrics().sz_pic; QImage p = SHashedImage(picture); if(!p.isNull()) p = p.scaled(dim, dim, Qt::KeepAspectRatio); cache.insert(picture->filename, p); ret.second = p; } return ret; }
void StarWidget::mouseReleaseEvent(QMouseEvent *event) { if (readOnly) { return; } int starClicked = event->pos().x() / defaultIconMetrics().sz_small + 1; if (starClicked > TOTALSTARS) starClicked = TOTALSTARS; if (current == starClicked) current -= 1; else current = starClicked; Q_EMIT valueChanged(current); update(); }
StarWidget::StarWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f), current(0), readOnly(false) { int dim = defaultIconMetrics().sz_small; if (activeStar.isNull()) { QSvgRenderer render(QString(":star")); QPixmap renderedStar(dim, dim); renderedStar.fill(Qt::transparent); QPainter painter(&renderedStar); render.render(&painter, QRectF(0, 0, dim, dim)); activeStar = renderedStar.toImage(); } if (inactiveStar.isNull()) { inactiveStar = grayImage(activeStar); } setFocusPolicy(Qt::StrongFocus); }
void StarWidget::paintEvent(QPaintEvent *event) { QPainter p(this); QImage star = hasFocus() ? focusedImage(starActive()) : starActive(); QPixmap selected = QPixmap::fromImage(star); QPixmap inactive = QPixmap::fromImage(starInactive()); const IconMetrics& metrics = defaultIconMetrics(); for (int i = 0; i < current; i++) p.drawPixmap(i * metrics.sz_small + metrics.spacing, 0, selected); for (int i = current; i < TOTALSTARS; i++) p.drawPixmap(i * metrics.sz_small + metrics.spacing, 0, inactive); if (hasFocus()) { QStyleOptionFocusRect option; option.initFrom(this); option.backgroundColor = palette().color(QPalette::Background); style()->drawPrimitive(QStyle::PE_FrameFocusRect, &option, &p, this); } }
void StarWidgetsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyledItemDelegate::paint(painter, option, index); if (!index.isValid()) return; QVariant value = index.model()->data(index, DiveTripModel::STAR_ROLE); if (!value.isValid()) return; int rating = value.toInt(); int deltaY = option.rect.height() / 2 - StarWidget::starActive().height() / 2; painter->save(); painter->setRenderHint(QPainter::Antialiasing, true); const QPixmap active = QPixmap::fromImage(StarWidget::starActive()); const QPixmap inactive = QPixmap::fromImage(StarWidget::starInactive()); const IconMetrics& metrics = defaultIconMetrics(); for (int i = 0; i < rating; i++) painter->drawPixmap(option.rect.x() + i * metrics.sz_small + metrics.spacing, option.rect.y() + deltaY, active); for (int i = rating; i < TOTALSTARS; i++) painter->drawPixmap(option.rect.x() + i * metrics.sz_small + metrics.spacing, option.rect.y() + deltaY, inactive); painter->restore(); }
void DiveEventItem::setupPixmap() { const IconMetrics& metrics = defaultIconMetrics(); int sz_bigger = metrics.sz_med + metrics.sz_small; // ex 40px int sz_pix = sz_bigger/2; // ex 20px #define EVENT_PIXMAP(PIX) QPixmap(QString(PIX)).scaled(sz_pix, sz_pix, Qt::KeepAspectRatio, Qt::SmoothTransformation) #define EVENT_PIXMAP_BIGGER(PIX) QPixmap(QString(PIX)).scaled(sz_bigger, sz_bigger, Qt::KeepAspectRatio, Qt::SmoothTransformation) if (same_string(internalEvent->name, "")) { setPixmap(EVENT_PIXMAP(":warning")); } else if (internalEvent->type == SAMPLE_EVENT_BOOKMARK) { setPixmap(EVENT_PIXMAP(":flag")); } else if (strcmp(internalEvent->name, "heading") == 0 || (same_string(internalEvent->name, "SP change") && internalEvent->time.seconds == 0)) { // 2 cases: // a) some dive computers have heading in every sample // b) at t=0 we might have an "SP change" to indicate dive type // in both cases we want to get the right data into the tooltip but don't want the visual clutter // so set an "almost invisible" pixmap (a narrow but somewhat tall, basically transparent pixmap) // that allows tooltips to work when we don't want to show a specific // pixmap for an event, but want to show the event value in the tooltip QPixmap transparentPixmap(4, 20); transparentPixmap.fill(QColor::fromRgbF(1.0, 1.0, 1.0, 0.01)); setPixmap(transparentPixmap); } else if (event_is_gaschange(internalEvent)) { if (internalEvent->gas.mix.he.permille) setPixmap(EVENT_PIXMAP_BIGGER(":gaschangeTrimix")); else if (gasmix_is_air(&internalEvent->gas.mix)) setPixmap(EVENT_PIXMAP_BIGGER(":gaschangeAir")); else setPixmap(EVENT_PIXMAP_BIGGER(":gaschangeNitrox")); } else { setPixmap(EVENT_PIXMAP(":warning")); } #undef EVENT_PIXMAP }
StarWidgetsDelegate::StarWidgetsDelegate(QWidget *parent) : QStyledItemDelegate(parent), parentWidget(parent) { const IconMetrics& metrics = defaultIconMetrics(); minStarSize = QSize(metrics.sz_small * TOTALSTARS + metrics.spacing * (TOTALSTARS - 1), metrics.sz_small); }
QSize StarWidget::sizeHint() const { const IconMetrics& metrics = defaultIconMetrics(); return QSize(metrics.sz_small * TOTALSTARS + metrics.spacing * (TOTALSTARS - 1), metrics.sz_small); }
QVariant DiveItem::data(int column, int role) const { QVariant retVal; struct dive *dive = get_dive_by_uniq_id(diveId); if (!dive) return QVariant(); switch (role) { case Qt::TextAlignmentRole: retVal = dive_table_alignment(column); break; case DiveTripModel::SORT_ROLE: Q_ASSERT(dive != NULL); switch (column) { case NR: retVal = (qulonglong)dive->when; break; case DATE: retVal = (qulonglong)dive->when; break; case RATING: retVal = dive->rating; break; case DEPTH: retVal = dive->maxdepth.mm; break; case DURATION: retVal = dive->duration.seconds; break; case TEMPERATURE: retVal = dive->watertemp.mkelvin; break; case TOTALWEIGHT: retVal = total_weight(dive); break; case SUIT: retVal = QString(dive->suit); break; case CYLINDER: retVal = QString(dive->cylinder[0].type.description); break; case GAS: retVal = nitrox_sort_value(dive); break; case SAC: retVal = dive->sac; break; case OTU: retVal = dive->otu; break; case MAXCNS: retVal = dive->maxcns; break; case LOCATION: retVal = QString(get_dive_location(dive)); break; } break; case Qt::DisplayRole: Q_ASSERT(dive != NULL); switch (column) { case NR: retVal = dive->number; break; case DATE: retVal = displayDate(); break; case DEPTH: retVal = displayDepth(); break; case DURATION: retVal = displayDuration(); break; case TEMPERATURE: retVal = displayTemperature(); break; case TOTALWEIGHT: retVal = displayWeight(); break; case SUIT: retVal = QString(dive->suit); break; case CYLINDER: retVal = QString(dive->cylinder[0].type.description); break; case SAC: retVal = displaySac(); break; case OTU: retVal = dive->otu; break; case MAXCNS: retVal = dive->maxcns; break; case LOCATION: retVal = QString(get_dive_location(dive)); break; case GAS: const char *gas_string = get_dive_gas_string(dive); retVal = QString(gas_string); free((void*)gas_string); break; } break; case Qt::DecorationRole: if (column == LOCATION) if (dive_has_gps_location(dive)) { IconMetrics im = defaultIconMetrics(); retVal = QIcon(":satellite").pixmap(im.sz_small, im.sz_small); } break; case Qt::ToolTipRole: switch (column) { case NR: retVal = tr("#"); break; case DATE: retVal = tr("Date"); break; case RATING: retVal = tr("Rating"); break; case DEPTH: retVal = tr("Depth(%1)").arg((get_units()->length == units::METERS) ? tr("m") : tr("ft")); break; case DURATION: retVal = tr("Duration"); break; case TEMPERATURE: retVal = tr("Temp(%1%2)").arg(UTF8_DEGREE).arg((get_units()->temperature == units::CELSIUS) ? "C" : "F"); break; case TOTALWEIGHT: retVal = tr("Weight(%1)").arg((get_units()->weight == units::KG) ? tr("kg") : tr("lbs")); break; case SUIT: retVal = tr("Suit"); break; case CYLINDER: retVal = tr("Cyl"); break; case GAS: retVal = tr("Gas"); break; case SAC: const char *unit; get_volume_units(0, NULL, &unit); retVal = tr("SAC(%1)").arg(QString(unit).append(tr("/min"))); break; case OTU: retVal = tr("OTU"); break; case MAXCNS: retVal = tr("Max CNS"); break; case LOCATION: retVal = tr("Location"); break; } break; } if (role == DiveTripModel::STAR_ROLE) { Q_ASSERT(dive != NULL); retVal = dive->rating; } if (role == DiveTripModel::DIVE_ROLE) { retVal = QVariant::fromValue<void *>(dive); } if (role == DiveTripModel::DIVE_IDX) { Q_ASSERT(dive != NULL); retVal = get_divenr(dive); } return retVal; }
const QPixmap &trashForbiddenIcon() { static QPixmap trash = QPixmap(":trashForbidden").scaledToHeight(defaultIconMetrics().sz_small); return trash; }