Almost working delegate version with text. Needs a little more polishing though
This commit is contained in:
		
					parent
					
						
							
								1886dbf53c
							
						
					
				
			
			
				commit
				
					
						04f1c4a76f
					
				
			
		
					 1 changed files with 60 additions and 21 deletions
				
			
		| 
						 | 
					@ -35,6 +35,8 @@
 | 
				
			||||||
// KDE
 | 
					// KDE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <KPushButton>
 | 
					#include <KPushButton>
 | 
				
			||||||
 | 
					#include <KLocale>
 | 
				
			||||||
 | 
					#include <KLocalizedString>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Danbooru {
 | 
					namespace Danbooru {
 | 
				
			||||||
| 
						 | 
					@ -92,6 +94,12 @@ namespace Danbooru {
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QStyleOptionViewItemV4 opt(option);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Pixmap
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        painter->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        QStyle *style = QApplication::style();
 | 
					        QStyle *style = QApplication::style();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
 | 
					        style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
 | 
				
			||||||
| 
						 | 
					@ -103,40 +111,43 @@ namespace Danbooru {
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Scaling is unavoidable to keep things in the right dimension
 | 
					        QRect rect = option.rect;
 | 
				
			||||||
        //TODO: Replace fixed values by configurable values
 | 
					        QFontMetrics metrics = option.fontMetrics;
 | 
				
			||||||
 | 
					        QRect textRect(rect.left(), rect.bottom() - 3 * metrics.height(),
 | 
				
			||||||
 | 
					                       rect.width(), 3 * metrics.height());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        QPixmap scaled = pixmap.scaled(256-MARGIN, 256-MARGIN,
 | 
					        // Scaling is unavoidable to keep things in the right dimension
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QPixmap scaled = pixmap.scaled(rect.width() - MARGIN,
 | 
				
			||||||
 | 
					                                       rect.height() - MARGIN,
 | 
				
			||||||
                                       Qt::KeepAspectRatio,
 | 
					                                       Qt::KeepAspectRatio,
 | 
				
			||||||
                                       Qt::SmoothTransformation);
 | 
					                                       Qt::SmoothTransformation);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QRect pixRect = scaled.rect();
 | 
				
			||||||
 | 
					        pixRect.moveCenter(rect.center());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        QRect rect = option.rect;
 | 
					//         painter->drawRect(option.rect);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Draw in the center
 | 
					        // move the pixmap up to accomodate some lines of text
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        QPoint centerCoordinate = rect.center() - QPoint(scaled.width() / 2,
 | 
					        pixRect.moveTo(pixRect.left(),
 | 
				
			||||||
                                                         scaled.height() / 2);
 | 
					                       pixRect.top() + textRect.height() / 2);
 | 
				
			||||||
 | 
					        pixRect.setBottom(textRect.top() - MARGIN);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Get the bottom coordinate for the buttons, made as center - half
 | 
					        painter->drawPixmap(pixRect, scaled);
 | 
				
			||||||
        // of the pixmap width (left) then half of the height minus the button
 | 
					        painter->restore();
 | 
				
			||||||
        // size and margin (because the move() call later on uses the
 | 
					 | 
				
			||||||
        // coordinates as TOP coordinates, while we want the bottom of the
 | 
					 | 
				
			||||||
        // button to align with the bottom of the pixmap)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        QPoint scaleFactor = QPoint(
 | 
					        // Buttons
 | 
				
			||||||
            -(scaled.width() / 2) + (MARGIN / 2),
 | 
					 | 
				
			||||||
            (scaled.height() / 2) - m_buttonSize + (MARGIN * 1.5)
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        QPoint bottomCoordinate = rect.center() + scaleFactor;
 | 
					        painter->save();
 | 
				
			||||||
 | 
					 | 
				
			||||||
        painter->drawPixmap(centerCoordinate, scaled);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (option.state & QStyle::State_MouseOver) {
 | 
					        if (option.state & QStyle::State_MouseOver) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            m_downloadButton->move(centerCoordinate + QPoint(MARGIN / 2, 0));
 | 
					            // Get the bottom coordinate for the buttons
 | 
				
			||||||
            m_viewButton->move(bottomCoordinate);
 | 
					
 | 
				
			||||||
 | 
					            m_downloadButton->move(pixRect.topLeft());
 | 
				
			||||||
 | 
					            m_viewButton->move(pixRect.bottomLeft() - QPoint(
 | 
				
			||||||
 | 
					                0, m_viewButton->height() - 1.5 * MARGIN));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            m_downloadButton->show();
 | 
					            m_downloadButton->show();
 | 
				
			||||||
            m_viewButton->show();
 | 
					            m_viewButton->show();
 | 
				
			||||||
| 
						 | 
					@ -146,6 +157,34 @@ namespace Danbooru {
 | 
				
			||||||
            m_viewButton->hide();
 | 
					            m_viewButton->hide();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        painter->restore();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Text (resolution)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        DanbooruPost* post = index.data().value<Danbooru::DanbooruPost*>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        painter->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        int imageHeight = post->height();
 | 
				
			||||||
 | 
					        int imageWidth = post->width();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QString imageText = i18n(
 | 
				
			||||||
 | 
					            "File size: %1",
 | 
				
			||||||
 | 
					            KGlobal::locale()->formatByteSize(post->size()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        KLocalizedString sizestr = ki18np("1 pixel", "%1 pixels");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        imageText += "\n";
 | 
				
			||||||
 | 
					        imageText += i18n("Resolution: %1 x %2",
 | 
				
			||||||
 | 
					                          sizestr.subs(imageWidth).toString(),
 | 
				
			||||||
 | 
					                          sizestr.subs(imageHeight).toString());
 | 
				
			||||||
 | 
					        imageText += "\n";
 | 
				
			||||||
 | 
					        imageText += i18n("Rating: %1", post->rating());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        painter->drawText(textRect, imageText);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        painter->restore();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QSize DanbooruPostDelegate::sizeHint(const QStyleOptionViewItem& option,
 | 
					    QSize DanbooruPostDelegate::sizeHint(const QStyleOptionViewItem& option,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue