Kate
expandingdelegate.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "expandingdelegate.h"
00020
00021 #include <QtGui/QTextLine>
00022 #include <QtGui/QPainter>
00023 #include <QtGui/QBrush>
00024 #include <QKeyEvent>
00025 #include <QTreeView>
00026 #include <QApplication>
00027
00028 #include <kdebug.h>
00029
00030 #include "expandingwidgetmodel.h"
00031
00032 ExpandingDelegate::ExpandingDelegate(ExpandingWidgetModel* model, QObject* parent)
00033 : QItemDelegate(parent)
00034 , m_model(model)
00035 {
00036 }
00037
00038 void ExpandingDelegate::paint( QPainter * painter, const QStyleOptionViewItem & optionOld, const QModelIndex & index ) const
00039 {
00040 QStyleOptionViewItem option(optionOld);
00041
00042 adjustStyle(index, option);
00043
00044 if( index.column() == 0 )
00045 model()->placeExpandingWidget(index);
00046
00047
00048 if( model()->isPartiallyExpanded(index) == ExpandingWidgetModel::ExpandUpwards )
00049 m_cachedAlignment = Qt::AlignBottom;
00050 else
00051 m_cachedAlignment = Qt::AlignTop;
00052
00053 option.decorationAlignment = m_cachedAlignment;
00054 option.displayAlignment = m_cachedAlignment;
00055
00056
00057
00058 m_cachedHighlights.clear();
00059
00060 if (!model()->indexIsItem(index) )
00061 return QItemDelegate::paint(painter, option, index);
00062
00063 m_currentColumnStart = 0;
00064 m_cachedHighlights = createHighlighting(index, option);
00065
00066
00067
00068
00069
00070 QItemDelegate::paint(painter, option, index);
00071 }
00072
00073 QList<QTextLayout::FormatRange> ExpandingDelegate::createHighlighting(const QModelIndex& index, QStyleOptionViewItem& option) const {
00074 Q_UNUSED( index );
00075 Q_UNUSED( option );
00076 return QList<QTextLayout::FormatRange>();
00077 }
00078
00079 QSize ExpandingDelegate::basicSizeHint( const QModelIndex& index ) const {
00080 return QItemDelegate::sizeHint( QStyleOptionViewItem(), index );
00081 }
00082
00083 QSize ExpandingDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
00084 {
00085 QSize s = QItemDelegate::sizeHint( option, index );
00086 if( model()->isExpanded(index) && model()->expandingWidget( index ) )
00087 {
00088 QWidget* widget = model()->expandingWidget( index );
00089 QSize widgetSize = widget->size();
00090
00091 s.setHeight( widgetSize.height() + s.height() + 10 );
00092 } else if( model()->isPartiallyExpanded( index ) ) {
00093 s.setHeight( s.height() + 30 + 10 );
00094 }
00095 return s;
00096 }
00097
00098 void ExpandingDelegate::adjustStyle( const QModelIndex& index, QStyleOptionViewItem & option ) const
00099 {
00100 }
00101
00102 void ExpandingDelegate::drawDisplay( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text ) const
00103 {
00104
00105
00106
00107 QTextLayout layout(text, option.font, painter->device());
00108
00109 QRect textRect = rect.adjusted(1, 0, -1, 0);
00110
00111 QList<QTextLayout::FormatRange> additionalFormats;
00112
00113 for (int i = 0; i < m_cachedHighlights.count(); ++i) {
00114 if (m_cachedHighlights[i].start + m_cachedHighlights[i].length <= m_currentColumnStart)
00115 continue;
00116
00117 if (!additionalFormats.count())
00118 if (i != 0 && m_cachedHighlights[i - 1].start + m_cachedHighlights[i - 1].length > m_currentColumnStart) {
00119 QTextLayout::FormatRange before;
00120 before.start = 0;
00121 before.length = m_cachedHighlights[i - 1].start + m_cachedHighlights[i - 1].length - m_currentColumnStart;
00122 before.format = m_cachedHighlights[i - 1].format;
00123 additionalFormats.append(before);
00124 }
00125
00126
00127 QTextLayout::FormatRange format;
00128 format.start = m_cachedHighlights[i].start - m_currentColumnStart;
00129 format.length = m_cachedHighlights[i].length;
00130 format.format = m_cachedHighlights[i].format;
00131
00132 if(option.state & QStyle::State_Selected && !format.format.hasProperty(QTextFormat::BackgroundBrush))
00133 format.format.setForeground(option.palette.brush(QPalette::Normal, QPalette::HighlightedText));
00134
00135
00136
00137 additionalFormats.append(format);
00138 }
00139
00140 if (additionalFormats.isEmpty()) {
00141 QTextLayout::FormatRange format;
00142 format.start = 0;
00143 format.length = text.length();
00144 QTextCharFormat fm;
00145 fm.setForeground(option.palette.text());
00146 format.format = fm;
00147 additionalFormats.append(format);
00148 }
00149
00150
00151
00152
00153
00154 layout.setAdditionalFormats(additionalFormats);
00155
00156 QTextOption to;
00157
00158 to.setAlignment( m_cachedAlignment );
00159
00160 to.setWrapMode(QTextOption::WrapAnywhere);
00161 layout.setTextOption(to);
00162
00163 layout.beginLayout();
00164 QTextLine line = layout.createLine();
00165 line.setLineWidth(rect.width());
00166 layout.endLayout();
00167
00168
00169 if( to.alignment() & Qt::AlignBottom)
00170 layout.draw(painter, QPoint(rect.left(), rect.bottom() - (int)line.height()) );
00171 else
00172 layout.draw(painter, rect.topLeft() );
00173
00174 return;
00175
00176
00177
00178
00179 }
00180
00181 void ExpandingDelegate::drawBackground ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
00182 QStyleOptionViewItemV4 opt = option;
00183
00184
00185 QStyle *style = model()->treeView()->style() ? model()->treeView()->style() : QApplication::style();
00186 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter);
00187 }
00188
00189 ExpandingWidgetModel* ExpandingDelegate::model() const {
00190 return m_model;
00191 }
00192
00193 void ExpandingDelegate::heightChanged() const {
00194 }
00195
00196 bool ExpandingDelegate::editorEvent ( QEvent * event, QAbstractItemModel * , const QStyleOptionViewItem & , const QModelIndex & index )
00197 {
00198 QKeyEvent* keyEvent = 0;
00199 if( event->type() == QEvent::KeyPress )
00200 keyEvent = reinterpret_cast<QKeyEvent*>(event);
00201
00202 if( event->type() == QEvent::MouseButtonRelease )
00203 {
00204 event->accept();
00205 model()->setExpanded(index, !model()->isExpanded( index ));
00206 heightChanged();
00207
00208 return true;
00209 } else {
00210 event->ignore();
00211 }
00212
00213 return false;
00214 }
00215
00216 QList<QTextLayout::FormatRange> ExpandingDelegate::highlightingFromVariantList(const QList<QVariant>& customHighlights) const
00217 {
00218 QList<QTextLayout::FormatRange> ret;
00219
00220 for (int i = 0; i + 2 < customHighlights.count(); i += 3) {
00221 if (!customHighlights[i].canConvert(QVariant::Int) || !customHighlights[i+1].canConvert(QVariant::Int) || !customHighlights[i+2].canConvert<QTextFormat>()) {
00222 kWarning() << "Unable to convert triple to custom formatting.";
00223 continue;
00224 }
00225
00226 QTextLayout::FormatRange format;
00227 format.start = customHighlights[i].toInt();
00228 format.length = customHighlights[i+1].toInt();
00229 format.format = customHighlights[i+2].value<QTextFormat>().toCharFormat();
00230
00231 if(!format.format.isValid())
00232 kWarning() << "Format is not valid";
00233
00234 ret << format;
00235 }
00236 return ret;
00237 }
00238
00239 #include "expandingdelegate.moc"