1 2 #include "filelistitem.h" 3 4 #include <QPainter> 5 6 7 FileListItem::FileListItem( QTreeWidget *parent, QTreeWidgetItem *after ) 8 : QTreeWidgetItem( parent, after ), 9 tags( 0 ) 10 { 11 conversionOptionsId = -1; 12 13 state = WaitingForConversion; 14 returnCode = Succeeded; 15 local = true; 16 track = -1; 17 tracks = 0; 18 19 length = 0; 20 21 logId = -1; 22 } 23 24 FileListItem::FileListItem( QTreeWidget *parent ) 25 : QTreeWidgetItem( parent ), 26 tags( 0 ) 27 { 28 conversionOptionsId = -1; 29 30 state = WaitingForConversion; 31 returnCode = Succeeded; 32 local = true; 33 track = -1; 34 tracks = 0; 35 36 length = 0; 37 38 logId = -1; 39 } 40 41 FileListItem::~FileListItem() 42 { 43 if( tags ) 44 delete tags; 45 46 if( lInfo.data() ) 47 delete lInfo.data(); 48 } 49 50 FileListItemDelegate::FileListItemDelegate( QObject *parent ) 51 : QItemDelegate( parent ) 52 {} 53 54 // TODO margin 55 void FileListItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const 56 { 57 FileListItem *item = static_cast<FileListItem*>( index.internalPointer() ); 58 59 QColor backgroundColor; 60 61 painter->save(); 62 63 QStyleOptionViewItem _option = option; 64 65 bool isConverting = false; 66 bool isFailed = false; 67 if( item ) 68 { 69 switch( item->state ) 70 { 71 case FileListItem::WaitingForConversion: 72 break; 73 case FileListItem::Ripping: 74 isConverting = true; 75 break; 76 case FileListItem::Converting: 77 isConverting = true; 78 break; 79 case FileListItem::ApplyingReplayGain: 80 isConverting = true; 81 break; 82 case FileListItem::WaitingForAlbumGain: 83 break; 84 case FileListItem::ApplyingAlbumGain: 85 isConverting = true; 86 break; 87 case FileListItem::Stopped: 88 switch( item->returnCode ) 89 { 90 case FileListItem::Succeeded: 91 break; 92 case FileListItem::SucceededWithProblems: 93 break; 94 case FileListItem::StoppedByUser: 95 break; 96 case FileListItem::BackendNeedsConfiguration: 97 isFailed = true; 98 break; 99 case FileListItem::DiscFull: 100 case FileListItem::CantWriteOutput: 101 isFailed = true; 102 break; 103 case FileListItem::Skipped: 104 break; 105 case FileListItem::Encrypted: 106 isFailed = true; 107 break; 108 case FileListItem::Failed: 109 isFailed = true; 110 break; 111 } 112 break; 113 } 114 } 115 116 if( isConverting ) 117 { 118 if( option.state & QStyle::State_Selected ) 119 { 120 backgroundColor = QColor(215,102,102); // hsv: 0, 134, 215 121 } 122 else 123 { 124 backgroundColor = QColor(255,234,234); // hsv: 0, 21, 255 125 } 126 } 127 else if( isFailed ) 128 { 129 if( option.state & QStyle::State_Selected ) 130 { 131 backgroundColor = QColor(235,154, 49); // hsv: 34, 202, 235 132 } 133 else 134 { 135 backgroundColor = QColor(255,204,156); // hsv: 29, 99, 255 136 } 137 } 138 else 139 { 140 if( option.state & QStyle::State_Selected ) 141 { 142 backgroundColor = option.palette.highlight().color(); 143 } 144 else 145 { 146 backgroundColor = option.palette.base().color(); 147 } 148 } 149 150 painter->fillRect( option.rect, backgroundColor ); 151 152 int m_left, m_top, m_right, m_bottom; 153 item->treeWidget()->getContentsMargins( &m_left, &m_top, &m_right, &m_bottom ); 154 //QRect m_rect = QRect( option.rect.x()+m_left, option.rect.y()+m_top, option.rect.width()-m_left-m_right, option.rect.height()-m_top-m_bottom ); 155 QRect m_rect = QRect( option.rect.x()+m_left, option.rect.y(), option.rect.width()-m_left-m_right, option.rect.height() ); 156 157 if( index.column() == 1 || index.column() == 2 ) 158 { 159 QRect textRect = painter->boundingRect( QRect(), Qt::AlignLeft|Qt::TextSingleLine, item->text(index.column()) ); 160 161 if ( textRect.width() < m_rect.width() ) 162 { 163 painter->drawText( m_rect, Qt::TextSingleLine|Qt::TextExpandTabs, item->text(index.column()) ); 164 } 165 else 166 { 167 //textRect = painter->boundingRect( QRect(), Qt::AlignLeft, "... " ); 168 painter->drawText( m_rect, Qt::AlignRight|Qt::TextSingleLine|Qt::TextExpandTabs, item->text(index.column()) ); 169 QLinearGradient linearGrad( QPoint(m_rect.x(),0), QPoint(m_rect.x()+15,0) ); 170 linearGrad.setColorAt( 0, backgroundColor ); 171 backgroundColor.setAlpha( 0 ); 172 linearGrad.setColorAt( 1, backgroundColor ); 173 painter->fillRect( m_rect.x(), m_rect.y(), 15, m_rect.height(), linearGrad ); 174 } 175 } 176 else 177 { 178 painter->drawText( m_rect, Qt::TextSingleLine|Qt::TextExpandTabs, item->text(index.column()) ); 179 } 180 181 //QItemDelegate::paint( painter, _option, index ); 182 183 painter->restore(); 184 185 // int progress = (index.row() != 0 ? 100 / index.row() : 0); 186 // 187 // // draw your cool progress bar here 188 // QStyleOptionProgressBar opt; 189 // opt.rect = option.rect; 190 // opt.minimum = 0; 191 // opt.maximum = 100; 192 // opt.progress = progress; 193 // opt.text = QString("%1%").arg(progress); 194 // opt.textVisible = true; 195 // QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, 0); 196 197 // maybe even let default implementation draw list item contents? 198 // QItemDelegate::paint(painter, option, index); 199 } 200