File indexing completed on 2024-04-14 05:38:11

0001 /***************************************************************************
0002  *   Copyright (C) 2010 by Daniel Nicoletti                                *
0003  *   dantti12@gmail.com                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; see the file COPYING. If not, write to       *
0017  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0018  *   Boston, MA 02110-1301, USA.                                           *
0019  ***************************************************************************/
0020 
0021 #include "TransactionDelegate.h"
0022 #include "PkTransactionProgressModel.h"
0023 
0024 #include <QApplication>
0025 
0026 #include "PkStrings.h"
0027 
0028 #define UNIVERSAL_PADDING 2
0029 
0030 using namespace PackageKit;
0031 
0032 TransactionDelegate::TransactionDelegate(QObject *parent) :
0033     QStyledItemDelegate(parent),
0034     m_minWidth(0)
0035 {
0036 }
0037 
0038 void TransactionDelegate::paint(QPainter *painter,
0039                                 const QStyleOptionViewItem &option,
0040                                 const QModelIndex &index) const
0041 {
0042     if (index.column() == 0 && !index.data(PkTransactionProgressModel::RoleRepo).toBool()) {
0043         int  progress = index.data(PkTransactionProgressModel::RoleProgress).toInt();
0044         QString text  = index.data(Qt::DisplayRole).toString();
0045 
0046         QStyleOptionProgressBar progressBarOption;
0047         progressBarOption.state = QStyle::State_Enabled;
0048         progressBarOption.direction = QApplication::layoutDirection();
0049         progressBarOption.rect = option.rect;
0050         progressBarOption.fontMetrics = QApplication::fontMetrics();
0051         progressBarOption.minimum = 0;
0052         progressBarOption.maximum = 100;
0053         progressBarOption.progress = progress;
0054         progressBarOption.textAlignment = Qt::AlignCenter;
0055         progressBarOption.text = text;
0056         progressBarOption.textVisible = true;
0057 
0058         QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
0059     } else {
0060         QStyleOptionViewItem opt1(option);
0061         if (opt1.state & QStyle::State_HasFocus) {
0062             opt1.state ^= QStyle::State_HasFocus;
0063         }
0064         QStyledItemDelegate::paint(painter, opt1, index);
0065     }
0066 }
0067 
0068 QSize TransactionDelegate::sizeHint(const QStyleOptionViewItem &option,
0069                                     const QModelIndex &index) const
0070 {
0071     QSize size = QStyledItemDelegate::sizeHint(option, index);
0072     size.rheight() += 2 * UNIVERSAL_PADDING;
0073     size.rwidth()  += 2 * UNIVERSAL_PADDING;
0074     // The first column keeps resizing
0075     // this avoids it being smaller
0076     if (index.column() == 0) {
0077         if (size.width() < m_minWidth) {
0078             size.setWidth(m_minWidth);
0079         } else {
0080             TransactionDelegate *delegate = const_cast<TransactionDelegate*>(this);
0081             delegate->m_minWidth = size.width();
0082         }
0083     }
0084     return size;
0085 }
0086 
0087 #include "moc_TransactionDelegate.cpp"