File indexing completed on 2024-10-06 04:26:01
0001 /* 0002 SPDX-FileCopyrightText: 2009 Sebastian Trueg <trueg@k3b.org> 0003 SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl> 0004 SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "k3bmediumdelegate.h" 0010 #include "k3bmedium.h" 0011 0012 #include <KLocalizedString> 0013 0014 #include <QModelIndex> 0015 #include <QPainter> 0016 #include <QApplication> 0017 #include <QStyle> 0018 0019 Q_DECLARE_METATYPE(K3b::Medium) 0020 0021 namespace 0022 { 0023 0024 QFont cloneFont( const QFont& font, int pointSize, bool bold ) 0025 { 0026 QFont cloned( font ); 0027 cloned.setPointSize( pointSize ); 0028 cloned.setBold( bold ); 0029 return cloned; 0030 } 0031 0032 struct FontsAndMetrics 0033 { 0034 FontsAndMetrics( const QFont& font ); 0035 0036 QFont titleFont; 0037 QFontMetrics fontM; 0038 QFontMetrics titleFontM; 0039 int margin; 0040 }; 0041 0042 FontsAndMetrics::FontsAndMetrics( const QFont& font ) 0043 : 0044 titleFont( cloneFont( font, font.pointSize()+2, true ) ), 0045 fontM( font ), 0046 titleFontM( font ), 0047 margin( fontM.descent() ) 0048 { 0049 } 0050 0051 } // namespace 0052 0053 K3b::MediumDelegate::MediumDelegate( QObject* parent ) 0054 : QStyledItemDelegate( parent ) 0055 { 0056 } 0057 0058 0059 K3b::MediumDelegate::~MediumDelegate() 0060 { 0061 } 0062 0063 0064 QSize K3b::MediumDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const 0065 { 0066 Medium medium = index.data( MediumRole ).value<Medium>(); 0067 if( medium.isValid() ) { 0068 const FontsAndMetrics fam( option.font ); 0069 0070 int height = fam.margin + fam.titleFontM.height() + fam.fontM.height() + fam.margin; 0071 if( medium.diskInfo().diskState() == K3b::Device::STATE_COMPLETE || 0072 medium.diskInfo().diskState() == K3b::Device::STATE_INCOMPLETE ) 0073 height += fam.fontM.height(); 0074 if( medium.diskInfo().diskState() == K3b::Device::STATE_EMPTY || 0075 medium.diskInfo().diskState() == K3b::Device::STATE_INCOMPLETE ) 0076 height += fam.fontM.height(); 0077 if( !medium.diskInfo().empty() && medium.diskInfo().rewritable() ) 0078 height += fam.fontM.height(); 0079 0080 return QSize( 0, height ); 0081 } 0082 else { 0083 return QStyledItemDelegate::sizeHint( option, index ); 0084 } 0085 } 0086 0087 0088 void K3b::MediumDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const 0089 { 0090 Medium medium = index.data( MediumRole ).value<Medium>(); 0091 if( medium.isValid() ) { 0092 painter->save(); 0093 0094 const FontsAndMetrics fam( option.font ); 0095 const QPalette::ColorRole textRole = (option.state & QStyle::State_Selected) ? 0096 QPalette::HighlightedText : QPalette::Text; 0097 0098 QStyle* style = QApplication::style(); 0099 0100 style->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter ); 0101 0102 const QRect rect = option.rect.adjusted( fam.margin, fam.margin, -fam.margin, -fam.margin ); 0103 const QRect titleRect( rect.left(), rect.top(), 0104 rect.width(), fam.titleFontM.height() ); 0105 const QRect contentRect( rect.left(), rect.top() + fam.titleFontM.height(), 0106 rect.width(), fam.fontM.height() ); 0107 const QPixmap icon = medium.icon().pixmap( rect.height() ); 0108 0109 painter->save(); 0110 painter->setOpacity( 0.3 ); 0111 style->drawItemPixmap( painter, rect, Qt::AlignRight | Qt::AlignVCenter, icon ); 0112 painter->restore(); 0113 0114 painter->setFont( fam.titleFont ); 0115 style->drawItemText( painter, titleRect, option.displayAlignment, option.palette, 0116 option.state & QStyle::State_Enabled, 0117 fam.titleFontM.elidedText( medium.shortString(), option.textElideMode, titleRect.width() ), 0118 textRole ); 0119 0120 painter->setFont( option.font ); 0121 0122 QString contentText = QString("(%1)").arg( medium.contentTypeString() ); 0123 style->drawItemText( painter, contentRect, option.displayAlignment, option.palette, 0124 option.state & QStyle::State_Enabled, 0125 fam.titleFontM.elidedText( contentText, option.textElideMode, contentRect.width() ), 0126 textRole ); 0127 0128 int previousHeights = fam.titleFontM.height() + fam.fontM.height(); 0129 if( medium.diskInfo().diskState() == K3b::Device::STATE_COMPLETE || 0130 medium.diskInfo().diskState() == K3b::Device::STATE_INCOMPLETE ) { 0131 const QRect tracksRect( rect.left(), rect.top() + previousHeights, 0132 rect.width(), fam.fontM.height() ); 0133 previousHeights += tracksRect.height(); 0134 QString tracksText = i18np("%2 in %1 track", "%2 in %1 tracks", 0135 medium.toc().count(), 0136 KIO::convertSize(medium.diskInfo().size().mode1Bytes()) ); 0137 if( medium.diskInfo().numSessions() > 1 ) 0138 tracksText += i18np(" and %1 session", " and %1 sessions", medium.diskInfo().numSessions() ); 0139 0140 style->drawItemText( painter, tracksRect, option.displayAlignment, option.palette, 0141 option.state & QStyle::State_Enabled, 0142 fam.titleFontM.elidedText( tracksText, option.textElideMode, tracksRect.width() ), 0143 textRole ); 0144 } 0145 0146 if( medium.diskInfo().diskState() == K3b::Device::STATE_EMPTY || 0147 medium.diskInfo().diskState() == K3b::Device::STATE_INCOMPLETE ) { 0148 const QRect freeSpaceRect( rect.left(), rect.top() + previousHeights, 0149 rect.width(), fam.fontM.height() ); 0150 previousHeights += freeSpaceRect.height(); 0151 QString freeSpaceText = i18n("Free space: %1", 0152 KIO::convertSize( medium.diskInfo().remainingSize().mode1Bytes() ) ); 0153 style->drawItemText( painter, freeSpaceRect, option.displayAlignment, option.palette, 0154 option.state & QStyle::State_Enabled, 0155 fam.titleFontM.elidedText( freeSpaceText, option.textElideMode, freeSpaceRect.width() ), 0156 textRole ); 0157 } 0158 0159 if( !medium.diskInfo().empty() && medium.diskInfo().rewritable() ) { 0160 const QRect capacityRect( rect.left(), rect.top() + previousHeights, 0161 rect.width(), fam.fontM.height() ); 0162 previousHeights += capacityRect.height(); 0163 QString capacityText = i18n("Capacity: %1", 0164 KIO::convertSize( medium.diskInfo().capacity().mode1Bytes() ) ); 0165 style->drawItemText( painter, capacityRect, option.displayAlignment, option.palette, 0166 option.state & QStyle::State_Enabled, 0167 fam.titleFontM.elidedText( capacityText, option.textElideMode, capacityRect.width() ), 0168 textRole ); 0169 } 0170 0171 painter->restore(); 0172 } 0173 else { 0174 QStyledItemDelegate::paint( painter, option, index ); 0175 } 0176 } 0177 0178 #include "moc_k3bmediumdelegate.cpp"