<commit_before>/* This file is part of the KDE libraries
   Copyright (C) 2006 <NAME> <<EMAIL>>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "expandingdelegate.h"

#include <QtGui/QTextLine>
#include <QtGui/QPainter>
#include <QtGui/QBrush>
#include <QKeyEvent>
#include <QTreeView>
#include <QApplication>

#include <kdebug.h>

#include "expandingwidgetmodel.h"

ExpandingDelegate::ExpandingDelegate(ExpandingWidgetModel* model, QObject* parent)
  : QItemDelegate(parent)
  , m_model(model)
{
}

//Gets the background-color in the way QItemDelegate does it
static QColor getUsedBackgroundColor(const QStyleOptionViewItem & option, const QModelIndex& index) {
if (option.showDecorationSelected && (option.state & QStyle::State_Selected)) {
        QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
                                  ? QPalette::Normal : QPalette::Disabled;
        if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
            cg = QPalette::Inactive;

        return option.palette.brush(cg, QPalette::Highlight).color();
    } else {
        QVariant value = index.data(Qt::BackgroundRole);
        if (qVariantCanConvert<QBrush>(value))
	    return qvariant_cast<QBrush>(value).color();
    }
    
    return QColor();
}

static void dampColors(QColor& col) {
  //Reduce the colors that are less visible to the eye, because they are closer to black when it comes to contrast
  //The most significant color to the eye is green. Then comes red, and then blue, with blue _much_ less significant.
  
  col.setBlue(0);
  col.setRed(col.red() / 2);
}

//A hack to compute more eye-focused contrast values
static double readabilityContrast(QColor foreground, QColor background) {
  dampColors(foreground);
  dampColors(background);
  return abs(foreground.green()-background.green()) + abs(foreground.red()-background.red()) + abs(foreground.blue() - background.blue());
}

void ExpandingDelegate::paint( QPainter * painter, const QStyleOptionViewItem & optionOld, const QModelIndex & index ) const
{
  QStyleOptionViewItem option(optionOld);

  m_currentIndex = index;
  
  adjustStyle(index, option);
    
  if( index.column() == 0 )
    model()->placeExpandingWidget(index);

  //Make sure the decorations are painted at the top, because the center of expanded items will be filled with the embedded widget.
  if( model()->isPartiallyExpanded(index) == ExpandingWidgetModel::ExpandUpwards )
    m_cachedAlignment = Qt::AlignBottom;
  else
    m_cachedAlignment = Qt::AlignTop;
  
  option.decorationAlignment = m_cachedAlignment;
  option.displayAlignment = m_cachedAlignment;
  
  //kDebug( 13035 ) << "Painting row " << index.row() << ", column " << index.column() << ", internal " << index.internalPointer() << ", drawselected " << option.showDecorationSelected << ", selected " << (option.state & QStyle::State_Selected);

  m_cachedHighlights.clear();
  m_backgroundColor = getUsedBackgroundColor(option, index);

  if (model()->indexIsItem(index) ) {
    m_currentColumnStart = 0;
    m_cachedHighlights = createHighlighting(index, option);
  }

  /*kDebug( 13035 ) << "Highlights for line:";
  foreach (const QTextLayout::FormatRange& fr, m_cachedHighlights)
    kDebug( 13035 ) << fr.start << " len " << fr.length << " format ";*/

  QItemDelegate::paint(painter, option, index);
}

QList<QTextLayout::FormatRange> ExpandingDelegate::createHighlighting(const QModelIndex& index, QStyleOptionViewItem& option) const {
  Q_UNUSED( index );
  Q_UNUSED( option );
  return QList<QTextLayout::FormatRange>();
}
<commit_msg>Workaround a bug in the Qt 4.5 raster paint engine<commit_after>/* This file is part of the KDE libraries
   Copyright (C) 2006 <NAME> <<EMAIL>>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "expandingdelegate.h"

#include <QtGui/QTextLine>
#include <QtGui/QPainter>
#include <QtGui/QBrush>
#include <QKeyEvent>
#include <QTreeView>
#include <QApplication>

#include <kdebug.h>

#include "expandingwidgetmodel.h"

ExpandingDelegate::ExpandingDelegate(ExpandingWidgetModel* model, QObject* parent)
  : QItemDelegate(parent)
  , m_model(model)
{
}

//Gets the background-color in the way QItemDelegate does it
static QColor getUsedBackgroundColor(const QStyleOptionViewItem & option, const QModelIndex& index) {
if (option.showDecorationSelected && (option.state & QStyle::State_Selected)) {
        QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
                                  ? QPalette::Normal : QPalette::Disabled;
        if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
            cg = QPalette::Inactive;

        return option.palette.brush(cg, QPalette::Highlight).color();
    } else {
        QVariant value = index.data(Qt::BackgroundRole);
        if (qVariantCanConvert<QBrush>(value))
	    return qvariant_cast<QBrush>(value).color();
    }
    
    return QColor();
}

static void dampColors(QColor& col) {
  //Reduce the colors that are less visible to the eye, because they are closer to black when it comes to contrast
  //The most significant color to the eye is green. Then comes red, and then blue, with blue _much_ less significant.
  
  col.setBlue(0);
  col.setRed(col.red() / 2);
}

//A hack to compute more eye-focused contrast values
static double readabilityContrast(QColor foreground, QColor background) {
  dampColors(foreground);
  dampColors(background);
  return abs(foreground.green()-background.green()) + abs(foreground.red()-background.red()) + abs(foreground.blue() - background.blue());
}

void ExpandingDelegate::paint( QPainter * painter, const QStyleOptionViewItem & optionOld, const QModelIndex & index ) const
{
  QStyleOptionViewItem option(optionOld);

  m_currentIndex = index;
  
  adjustStyle(index, option);
    
  if( index.column() == 0 )
    model()->placeExpandingWidget(index);

  //Make sure the decorations are painted at the top, because the center of expanded items will be filled with the embedded widget.
  if( model()->isPartiallyExpanded(index) == ExpandingWidgetModel::ExpandUpwards )
    m_cachedAlignment = Qt::AlignBottom;
  else
    m_cachedAlignment = Qt::AlignTop;
  
  option.decorationAlignment = m_cachedAlignment;
  option.displayAlignment = m_cachedAlignment;
  
  //kDebug( 13035 ) << "Painting row " << index.row() << ", column " << index.column() << ", internal " << index.internalPointer() << ", drawselected " << option.showDecorationSelected << ", selected " << (option.state & QStyle::State_Selected);

  m_cachedHighlights.clear();
  m_backgroundColor = getUsedBackgroundColor(option, index);

  if (model()->indexIsItem(index) ) {
    m_currentColumnStart = 0;
    m_cachedHighlights = createHighlighting(index, option);
  }

  /*kDebug( 13035 ) << "Highlights for line:";
  foreach (const QTextLayout::FormatRange& fr, m_cachedHighlights)
    kDebug( 13035 ) << fr.start << " len " << fr.length << " format ";*/

  QItemDelegate::paint(painter, option, index);

  ///This is a bug workaround for the Qt raster paint engine: It paints over widgets embedded into the viewport when updating due to mouse events
  ///@todo report to Qt Software
  if( model()->isExpanded(index) && model()->expandingWidget( index ) )
    model()->expandingWidget( index )->update();
}

QList<QTextLayout::FormatRange> ExpandingDelegate::createHighlighting(const QModelIndex& index, QStyleOptionViewItem& option) const {
  Q_UNUSED( index );
  Q_UNUSED( option );
  return QList<QTextLayout::FormatRange>();
}

{'fraction_non_alphanumeric': 0.10804077010192525, 'fraction_numerical': 0.008154020385050963, 'mean_word_length': 4.2816985645933014, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7243922', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2530, 'n_words': 796}