A mostly working version of the delegate which will be used for painting
the table
This commit is contained in:
		
					parent
					
						
							
								0d83d7b266
							
						
					
				
			
			
				commit
				
					
						97a9d6e4a5
					
				
			
		
					 2 changed files with 222 additions and 0 deletions
				
			
		
							
								
								
									
										151
									
								
								src/danboorupostdelegate.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										151
									
								
								src/danboorupostdelegate.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,151 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * <one line to give the library's name and an idea of what it does.>
 | 
				
			||||||
 | 
					 * Copyright 2013  Luca Beltrame <lbeltrame@kde.org>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is free software; you can redistribute it and/or
 | 
				
			||||||
 | 
					 * modify it under the terms of the GNU General Public License as
 | 
				
			||||||
 | 
					 * published by the Free Software Foundation; either version 2 of
 | 
				
			||||||
 | 
					 * the License or (at your option) version 3 or any later version
 | 
				
			||||||
 | 
					 * accepted by the membership of KDE e.V. (or its successor approved
 | 
				
			||||||
 | 
					 * by the membership of KDE e.V.), which shall act as a proxy
 | 
				
			||||||
 | 
					 * defined in Section 14 of version 3 of the license.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is distributed in the hope that it will be useful,
 | 
				
			||||||
 | 
					 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
				
			||||||
 | 
					 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
				
			||||||
 | 
					 * GNU General Public License for more details.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "danboorupostdelegate.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "libdanbooru/danboorupost.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <QListView>
 | 
				
			||||||
 | 
					#include <QPainter>
 | 
				
			||||||
 | 
					#include <QDebug>
 | 
				
			||||||
 | 
					#include <QApplication>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <KPushButton>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Danbooru {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const int DanbooruPostDelegate::MARGIN = 5;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    DanbooruPostDelegate::DanbooruPostDelegate(QListView* itemView):QStyledItemDelegate(itemView),
 | 
				
			||||||
 | 
					                                                                    m_itemView(itemView)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //FIXME: Copied directly from Gwenview without understanding
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        #define pm(x) itemView->style()->pixelMetric(QStyle::x)
 | 
				
			||||||
 | 
					        m_margin = pm(PM_ToolBarItemMargin);
 | 
				
			||||||
 | 
					        m_spacing = pm(PM_ToolBarItemSpacing);
 | 
				
			||||||
 | 
					        #undef pm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const int iconSize = KIconLoader::global()->currentSize(
 | 
				
			||||||
 | 
					            KIconLoader::Toolbar
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const QSize sz = itemView->style()->sizeFromContents(
 | 
				
			||||||
 | 
					            QStyle::CT_ToolButton, 0,
 | 
				
			||||||
 | 
					            QSize(iconSize, iconSize));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        m_buttonSize = qMax(sz.width(), sz.height());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        m_downloadButton =  new KPushButton(KIcon("download"),
 | 
				
			||||||
 | 
					                                            QString(), itemView);
 | 
				
			||||||
 | 
					        m_viewButton = new KPushButton(KIcon("view-preview"), QString(),
 | 
				
			||||||
 | 
					                                             itemView);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        m_downloadButton->hide();
 | 
				
			||||||
 | 
					        m_viewButton->hide();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        m_downloadButton->resize(m_buttonSize, m_buttonSize);
 | 
				
			||||||
 | 
					        m_viewButton->resize(m_buttonSize, m_buttonSize);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void DanbooruPostDelegate::paint(QPainter* painter,
 | 
				
			||||||
 | 
					                                     const QStyleOptionViewItem& option,
 | 
				
			||||||
 | 
					                                     const QModelIndex& index) const
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!index.isValid()) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QStyle *style = QApplication::style();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
 | 
				
			||||||
 | 
					        painter->setRenderHint(QPainter::Antialiasing);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QPixmap pixmap = index.data(Qt::DecorationRole).value<QPixmap>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (pixmap.isNull()) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Scaling is unavoidable to keep things in the right dimension
 | 
				
			||||||
 | 
					        //TODO: Replace fixed values by configurable values
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QPixmap scaled = pixmap.scaled(256-MARGIN, 256-MARGIN,
 | 
				
			||||||
 | 
					                                       Qt::KeepAspectRatio,
 | 
				
			||||||
 | 
					                                       Qt::SmoothTransformation);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QRect rect = option.rect;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Draw in the center
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QPoint centerCoordinate = rect.center() - QPoint(scaled.width() / 2,
 | 
				
			||||||
 | 
					                                                         scaled.height() / 2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Get the bottom coordinate for the buttons, made as center - half
 | 
				
			||||||
 | 
					        // of the pixmap width (left) then half of the height minus the button
 | 
				
			||||||
 | 
					        // 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(
 | 
				
			||||||
 | 
					            -(scaled.width() / 2) + (MARGIN / 2),
 | 
				
			||||||
 | 
					            (scaled.height() / 2) - m_buttonSize + (MARGIN * 1.5)
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QPoint bottomCoordinate = rect.center() + scaleFactor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        painter->drawPixmap(centerCoordinate, scaled);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (option.state & QStyle::State_MouseOver) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            m_downloadButton->move(centerCoordinate + QPoint(MARGIN / 2, 0));
 | 
				
			||||||
 | 
					            m_viewButton->move(bottomCoordinate);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            m_downloadButton->show();
 | 
				
			||||||
 | 
					            m_viewButton->show();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        } else if (!hoveredIndex().isValid()) {
 | 
				
			||||||
 | 
					            m_downloadButton->hide();
 | 
				
			||||||
 | 
					            m_viewButton->hide();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QSize DanbooruPostDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        Q_UNUSED(option)
 | 
				
			||||||
 | 
					        Q_UNUSED(index)
 | 
				
			||||||
 | 
					        return m_itemView->gridSize();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QModelIndex DanbooruPostDelegate::hoveredIndex() const
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        const QPoint pos = m_itemView->viewport()->mapFromGlobal(QCursor::pos());
 | 
				
			||||||
 | 
					        return m_itemView->indexAt(pos);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // namespace Danbooru
 | 
				
			||||||
							
								
								
									
										71
									
								
								src/danboorupostdelegate.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								src/danboorupostdelegate.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,71 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * This file is part of Danbooru Client.
 | 
				
			||||||
 | 
					 * Copyright 2013  Luca Beltrame <lbeltrame@kde.org>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is free software; you can redistribute it and/or
 | 
				
			||||||
 | 
					 * modify it under the terms of the GNU General Public License as
 | 
				
			||||||
 | 
					 * published by the Free Software Foundation; either version 2 of
 | 
				
			||||||
 | 
					 * the License or (at your option) version 3 or any later version
 | 
				
			||||||
 | 
					 * accepted by the membership of KDE e.V. (or its successor approved
 | 
				
			||||||
 | 
					 * by the membership of KDE e.V.), which shall act as a proxy
 | 
				
			||||||
 | 
					 * defined in Section 14 of version 3 of the license.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is distributed in the hope that it will be useful,
 | 
				
			||||||
 | 
					 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
				
			||||||
 | 
					 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
				
			||||||
 | 
					 * GNU General Public License for more details.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef DANBOORUPOSTDELEGATE_H
 | 
				
			||||||
 | 
					#define DANBOORUPOSTDELEGATE_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <QStyledItemDelegate>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class QListView;
 | 
				
			||||||
 | 
					class QPainter;
 | 
				
			||||||
 | 
					class KPushButton;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Danbooru {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class DanbooruPostDelegate : public QStyledItemDelegate
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Q_OBJECT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public:
 | 
				
			||||||
 | 
					       DanbooruPostDelegate(QListView* itemView);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					       void paint(QPainter* painter, const QStyleOptionViewItem& option,
 | 
				
			||||||
 | 
					                   const QModelIndex& index) const;
 | 
				
			||||||
 | 
					       QSize sizeHint(const QStyleOptionViewItem& option,
 | 
				
			||||||
 | 
					                       const QModelIndex& index) const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					       QModelIndex hoveredIndex() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					       QList<QWidget*> createItemWidgets() const;
 | 
				
			||||||
 | 
					       void updateItemWidgets( const QList<QWidget*> widgets,
 | 
				
			||||||
 | 
					                               const QStyleOptionViewItem& option,
 | 
				
			||||||
 | 
					                               const QPersistentModelIndex & index) const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QListView* m_itemView;
 | 
				
			||||||
 | 
					        KPushButton* m_downloadButton;
 | 
				
			||||||
 | 
					        KPushButton* m_viewButton;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        int m_buttonSize;
 | 
				
			||||||
 | 
					        int m_margin;
 | 
				
			||||||
 | 
					        int m_spacing;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        static const int MARGIN;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // namespace Danbooru
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // DANBOORUPOSTDELEGATE_H
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue