/** Redefined to always display the same height for albums, even for those without one. */ QSize LibraryItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { SettingsPrivate *settingsPrivate = SettingsPrivate::instance(); QStandardItem *item = _libraryModel->itemFromIndex(_proxy->mapToSource(index)); if (item->type() == Miam::IT_Album) { QFontMetrics fmf(settingsPrivate->font(SettingsPrivate::FF_Library)); return QSize(option.rect.width(), qMax(fmf.height(), Settings::instance()->coverSizeLibraryTree() + 2)); } else { QFontMetrics fmf(settingsPrivate->font(SettingsPrivate::FF_Library)); return QSize(option.rect.width(), fmf.height()); } }
/** Check if color needs to be inverted then paint text. */ void LibraryItemDelegate::paintText(QPainter *p, const QStyleOptionViewItem &opt, const QRect &rectText, const QString &text, const QStandardItem *item) const { p->save(); if (text.isEmpty()) { p->setPen(opt.palette.mid().color()); QFontMetrics fmf(SettingsPrivate::instance()->font(SettingsPrivate::FF_Library)); p->drawText(rectText, Qt::AlignVCenter, fmf.elidedText(tr("(empty)"), Qt::ElideRight, rectText.width())); } else { if (opt.state.testFlag(QStyle::State_Selected) || opt.state.testFlag(QStyle::State_MouseOver)) { if (SettingsPrivate::instance()->isCustomTextColorOverriden()) { p->setPen(opt.palette.highlightedText().color()); } else if (qAbs(opt.palette.highlight().color().lighter(lighterValue).value() - opt.palette.highlightedText().color().value()) < 128) { p->setPen(opt.palette.text().color()); } else { p->setPen(opt.palette.highlightedText().color()); } } if (item->data(Miam::DF_Highlighted).toBool()) { QFont f = p->font(); f.setBold(true); p->setFont(f); } p->drawText(rectText, Qt::AlignVCenter, text); } p->restore(); }
void tst_QFontMetrics::averageCharWidth() { QFont f; QFontMetrics fm(f); QVERIFY(fm.averageCharWidth() != 0); QFontMetricsF fmf(f); QVERIFY(fmf.averageCharWidth() != 0); }
void MiamItemDelegate::drawTrack(QPainter *painter, QStyleOptionViewItem &option, TrackItem *track) const { int trackNumber = track->data(Miam::DF_TrackNumber).toInt(); if (trackNumber > 0) { option.text = QString("%1").arg(trackNumber, 2, 10, QChar('0')).append(". ").append(track->text()); } else { option.text = track->text(); } QFontMetrics fmf(SettingsPrivate::instance()->font(SettingsPrivate::FF_Library)); option.textElideMode = Qt::ElideRight; QString s; QRect rectText; if (QGuiApplication::isLeftToRight()) { QPoint topLeft(option.rect.x() + 5, option.rect.y()); rectText = QRect(topLeft, option.rect.bottomRight()); s = fmf.elidedText(option.text, Qt::ElideRight, rectText.width()); } else { rectText = QRect(option.rect.x(), option.rect.y(), option.rect.width() - 5, option.rect.height()); s = fmf.elidedText(option.text, Qt::ElideRight, rectText.width()); } this->paintText(painter, option, rectText, s, track); }
void LibraryItemDelegate::drawArtist(QPainter *painter, QStyleOptionViewItem &option, QStandardItem *item) const { auto settings = SettingsPrivate::instance(); QFontMetrics fmf(settings->font(SettingsPrivate::FF_Library)); option.textElideMode = Qt::ElideRight; QRect rectText; QString s; if (QGuiApplication::isLeftToRight()) { QPoint topLeft(option.rect.x() + 5, option.rect.y()); rectText = QRect(topLeft, option.rect.bottomRight()); QString custom = item->data(Miam::DF_CustomDisplayText).toString(); if (!custom.isEmpty() && settings->isReorderArtistsArticle()) { /// XXX: paint articles like ", the" in gray? Could be nice s = fmf.elidedText(custom, Qt::ElideRight, rectText.width()); } else { s = fmf.elidedText(option.text, Qt::ElideRight, rectText.width()); } } else { rectText = QRect(option.rect.x(), option.rect.y(), option.rect.width() - 5, option.rect.height()); s = fmf.elidedText(option.text, Qt::ElideRight, rectText.width()); } this->paintText(painter, option, rectText, s, item); }
/** Albums have covers usually. */ void LibraryItemDelegate::drawAlbum(QPainter *painter, QStyleOptionViewItem &option, QStandardItem *item) const { /// XXX: reload cover with high resolution when one has increased coverSize (every 64px) static QImageReader imageReader; SettingsPrivate *settingsPrivate = SettingsPrivate::instance(); // Album has no picture yet bool itemHasNoIcon = item->icon().isNull(); if (itemHasNoIcon) { // Check first if an inner cover should be displayed if (item->data(Miam::DF_InternalCover).toString().isEmpty()) { QString coverPath = item->data(Miam::DF_CoverPath).toString(); if (!coverPath.isEmpty()) { // qDebug() << Q_FUNC_INFO << "loading external cover from harddrive"; imageReader.setFileName(QDir::fromNativeSeparators(coverPath)); imageReader.setScaledSize(QSize(_coverSize, _coverSize)); item->setIcon(QPixmap::fromImage(imageReader.read())); itemHasNoIcon = false; } } else { FileHelper fh(item->data(Miam::DF_InternalCover).toString()); std::unique_ptr<Cover> cover(fh.extractCover()); if (cover) { QPixmap p; if (p.loadFromData(cover->byteArray(), cover->format())) { if (!p.isNull()) { item->setIcon(p); itemHasNoIcon = false; } //} else { // qDebug() << Q_FUNC_INFO << "couldn't load data into QPixmap"; } //} else { // qDebug() << Q_FUNC_INFO << "couldn't extract inner cover"; } } } painter->save(); QRect cover; if (QGuiApplication::isLeftToRight()) { cover = QRect(option.rect.x() + 1, option.rect.y() + 1, _coverSize, _coverSize); } else { cover = QRect(option.rect.width() + 19 - _coverSize - 1, option.rect.y() + 1, _coverSize, _coverSize); } // If font size is greater than the cover, align it if (_coverSize < option.rect.height() - 2) { painter->translate(0, (option.rect.height() - 1 - _coverSize) / 2); } if (itemHasNoIcon) { if (_iconOpacity <= 0.25) { painter->setOpacity(_iconOpacity); } else { painter->setOpacity(0.25); } painter->drawPixmap(cover, QPixmap(":/icons/disc")); } else { painter->setOpacity(_iconOpacity); QPixmap p = option.icon.pixmap(QSize(_coverSize, _coverSize)); painter->drawPixmap(cover, p); } painter->restore(); // Add an icon on the right if album is from some remote location bool isRemote = item->data(Miam::DF_IsRemote).toBool(); int offsetWidth = 0; if (isRemote) { int iconSize = 31; QRect iconRemoteRect(option.rect.x() + option.rect.width() - (iconSize + 4), (option.rect.height() - iconSize)/ 2 + option.rect.y() + 2, iconSize, iconSize); QPixmap iconRemote(item->data(Miam::DF_IconPath).toString()); painter->save(); painter->setOpacity(0.5); painter->drawPixmap(iconRemoteRect, iconRemote); painter->restore(); offsetWidth = iconSize; } option.textElideMode = Qt::ElideRight; QRect rectText; // It's possible to have missing covers in your library, so we need to keep alignment. if (QGuiApplication::isLeftToRight()) { rectText = QRect(option.rect.x() + _coverSize + 5, option.rect.y(), option.rect.width() - (_coverSize + 7) - offsetWidth, option.rect.height() - 1); } else { rectText = QRect(option.rect.x(), option.rect.y(), option.rect.width() - _coverSize - 5, option.rect.height()); } QFontMetrics fmf(settingsPrivate->font(SettingsPrivate::FF_Library)); QString s = fmf.elidedText(option.text, Qt::ElideRight, rectText.width()); this->paintText(painter, option, rectText, s, item); }