• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Kate

expandingdelegate.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2006 Hamish Rodda <rodda@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
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   //Make sure the decorations are painted at the top, because the center of expanded items will be filled with the embedded widget.
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   //kDebug( 13035 ) << "Painting row " << index.row() << ", column " << index.column() << ", internal " << index.internalPointer() << ", drawselected " << option.showDecorationSelected << ", selected " << (option.state & QStyle::State_Selected);
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   /*kDebug( 13035 ) << "Highlights for line:";
00067   foreach (const QTextLayout::FormatRange& fr, m_cachedHighlights)
00068     kDebug( 13035 ) << fr.start << " len " << fr.length << " format ";*/
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 ); //10 is the sum that must match exactly the offsets used in ExpandingWidgetModel::placeExpandingWidgets
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 /*  if (m_cachedRow == -1)
00105     return QItemDelegate::drawDisplay(painter, option, rect, text);
00106 */
00107   QTextLayout layout(text, option.font, painter->device());
00108 
00109   QRect textRect = rect.adjusted(1, 0, -1, 0); // remove width padding
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))  //If the item is selected, we must override the color, because else we will have contrast problems
00133         format.format.setForeground(option.palette.brush(QPalette::Normal, QPalette::HighlightedText));
00134 
00135 /*    kDebug( 13035 ) << "using highlight for " << format.start << " len " << format.length;*/
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   /*kDebug( 13035 ) << "Highlights for text [" << text << "] col start " << m_currentColumnStart << ":";
00151   foreach (const QTextLayout::FormatRange& fr, additionalFormats)
00152     kDebug( 13035 ) << fr.start << " len " << fr.length << "foreground" << fr.format.foreground() << "background" << fr.format.background();*/
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   //We need to do some hand layouting here
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   //if (painter->fontMetrics().width(text) > textRect.width() && !text.contains(QLatin1Char('\n')))
00177       //str = elidedText(option.fontMetrics, textRect.width(), option.textElideMode, text);
00178   //qt_format_text(option.font, textRect, option.displayAlignment, str, 0, 0, 0, 0, painter);
00179 }
00180 
00181 void ExpandingDelegate::drawBackground ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
00182     QStyleOptionViewItemV4 opt = option;
00183     //initStyleOption(&opt, index);
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 * /*model*/, const QStyleOptionViewItem & /*option*/, 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"

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal