![]() |
Mixxx
|
00001 /*************************************************************************** 00002 stardelegate.cpp 00003 ------------------- 00004 copyright : (C) 2010 Tobias Rafreider 00005 copyright : (C) 2009 Nokia Corporation 00006 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 00019 #include <QtDebug> 00020 #include <QtGui> 00021 00022 #include "stardelegate.h" 00023 #include "stareditor.h" 00024 #include "starrating.h" 00025 00026 /* 00027 * The function is invoked once for each item, represented by a QModelIndex object from the model. 00028 * If the data stored in the item is a StarRating, we paint it use a star editor for displaying; 00029 * otherwise, we let QStyledItemDelegate paint it for us. 00030 * This ensures that the StarDelegate can handle the most common data types. 00031 */ 00032 void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 00033 { 00034 if (!qVariantCanConvert<StarRating>(index.data())) { 00035 QStyledItemDelegate::paint(painter, option, index); 00036 return; 00037 } 00038 00039 // Populate the correct colors based on the styling 00040 QStyleOptionViewItem newOption = option; 00041 initStyleOption(&newOption, index); 00042 00043 // Set the palette appropriately based on whether the row is selected or 00044 // not. We also have to check if it is inactive or not and use the 00045 // appropriate ColorGroup. 00046 if (newOption.state & QStyle::State_Selected) { 00047 QPalette::ColorGroup colorGroup = 00048 newOption.state & QStyle::State_Active ? 00049 QPalette::Active : QPalette::Inactive; 00050 painter->fillRect(newOption.rect, 00051 newOption.palette.color(colorGroup, QPalette::Highlight)); 00052 painter->setBrush(newOption.palette.color( 00053 colorGroup, QPalette::HighlightedText)); 00054 } else { 00055 painter->fillRect(newOption.rect, newOption.palette.base()); 00056 painter->setBrush(newOption.palette.text()); 00057 } 00058 00059 StarRating starRating = qVariantValue<StarRating>(index.data()); 00060 starRating.paint(painter, newOption.rect, newOption.palette, StarRating::ReadOnly); 00061 } 00062 00063 QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 00064 { 00065 if (qVariantCanConvert<StarRating>(index.data())) { 00066 StarRating starRating = qVariantValue<StarRating>(index.data()); 00067 return starRating.sizeHint(); 00068 } else { 00069 return QStyledItemDelegate::sizeHint(option, index); 00070 } 00071 } 00072 /* 00073 * If the item is a StarRating, we create a StarEditor and connect 00074 * its editingFinished() signal to our commitAndCloseEditor() slot, 00075 * so we can update the model when the editor closes. 00076 */ 00077 QWidget *StarDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const 00078 { 00079 // Populate the correct colors based on the styling 00080 //QStyleOptionViewItem newOption = option; 00081 //initStyleOption(&newOption, index); 00082 00083 if (qVariantCanConvert<StarRating>(index.data())) { 00084 StarEditor *editor = new StarEditor(parent, option); 00085 connect(editor, SIGNAL(editingFinished()), 00086 this, SLOT(commitAndCloseEditor())); 00087 return editor; 00088 } else { 00089 return QStyledItemDelegate::createEditor(parent, option, index); 00090 } 00091 } 00092 00093 void StarDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const 00094 { 00095 if (qVariantCanConvert<StarRating>(index.data())) { 00096 StarRating starRating = qVariantValue<StarRating>(index.data()); 00097 StarEditor *starEditor = qobject_cast<StarEditor *>(editor); 00098 starEditor->setStarRating(starRating); 00099 } else { 00100 QStyledItemDelegate::setEditorData(editor, index); 00101 } 00102 } 00103 00104 void StarDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 00105 { 00106 if (qVariantCanConvert<StarRating>(index.data())) { 00107 StarEditor *starEditor = qobject_cast<StarEditor *>(editor); 00108 model->setData(index, qVariantFromValue(starEditor->starRating())); 00109 } else { 00110 QStyledItemDelegate::setModelData(editor, model, index); 00111 } 00112 } 00113 /* 00114 * When the user is done editing, we emit commitData() and closeEditor() (both declared in QAbstractItemDelegate), 00115 * to tell the model that there is edited data and to inform the view that the editor is no longer needed. 00116 */ 00117 void StarDelegate::commitAndCloseEditor() 00118 { 00119 StarEditor *editor = qobject_cast<StarEditor *>(sender()); 00120 emit commitData(editor); 00121 emit closeEditor(editor); 00122 }