<commit_before>/*
* Copyright (C) 2008-2013 The Communi Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*/

#include "highlighterplugin.h"
#include "textdocument.h"
#include "sharedtimer.h"
#include "treewidget.h"
#include "treeitem.h"
#include "treerole.h"
#include <IrcConnection>
#include <IrcMessage>
#include <IrcBuffer>
#include <QTimer>
#include <QEvent>

HighlighterPlugin::HighlighterPlugin(QObject* parent) : QObject(parent)
{
    d.tree = 0;
    d.shortcut = 0;
    d.blink = false;
}

void HighlighterPlugin::initialize(TreeWidget* tree)
{
    d.tree = tree;

    connect(tree, SIGNAL(bufferAdded(IrcBuffer*)), this, SLOT(onBufferAdded(IrcBuffer*)));
    connect(tree, SIGNAL(bufferRemoved(IrcBuffer*)), this, SLOT(onBufferRemoved(IrcBuffer*)));
    connect(tree, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(onItemExpanded(QTreeWidgetItem*)));
    connect(tree, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(onItemCollapsed(QTreeWidgetItem*)));
    connect(tree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
            this, SLOT(onCurrentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)));

    d.shortcut = new QShortcut(tree);
    d.shortcut->setKey(QKeySequence(tr("Ctrl+R")));
    connect(d.shortcut, SIGNAL(activated()), this, SLOT(resetItems()));
}

void HighlighterPlugin::onBufferAdded(IrcBuffer* buffer)
{
    connect(buffer, SIGNAL(messageReceived(IrcMessage*)), this, SLOT(onMessageReceived(IrcMessage*)));
}

void HighlighterPlugin::onBufferRemoved(IrcBuffer* buffer)
{
    d.items.remove(d.tree->bufferItem(buffer));
    disconnect(buffer, SIGNAL(messageReceived(IrcMessage*)), this, SLOT(onMessageReceived(IrcMessage*)));
}

void HighlighterPlugin::onMessageReceived(IrcMessage* message)
{
    if (message->type() == IrcMessage::Private || message->type() == IrcMessage::Notice) {
        IrcBuffer* buffer = qobject_cast<IrcBuffer*>(sender());

        if (message->property("content").toString().contains(message->connection()->nickName())) {
            TreeItem* item = d.tree->bufferItem(buffer);
            if (item && item != d.tree->currentItem())
                highlightItem(item);

            TextDocument* document = buffer->property("document").value<TextDocument*>();
            document->addHighlight(document->totalCount() - 2); // TODO: -2??
        }
    }
}

void HighlighterPlugin::onItemExpanded(QTreeWidgetItem* item)
{
    static_cast<TreeItem*>(item)->refresh();
}

void HighlighterPlugin::onItemCollapsed(QTreeWidgetItem* item)
{
    static_cast<TreeItem*>(item)->refresh();
}

void HighlighterPlugin::onCurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
{
    unhighlightItem(previous);
    unhighlightItem(current);
}

void HighlighterPlugin::highlightItem(QTreeWidgetItem* item)
{
    if (d.items.isEmpty())
        SharedTimer::instance()->registerReceiver(this, "blinkItems");
    d.items.insert(item);
    item->setData(0, TreeRole::Highlight, true);
    item->setData(0, Qt::ForegroundRole, QColor("#ff4040")); // TODO
}

void HighlighterPlugin::unhighlightItem(QTreeWidgetItem* item)
{
    if (item) {
        item->setData(0, TreeRole::Highlight, false);
        item->setData(0, Qt::ForegroundRole, QVariant());

        if (d.items.remove(item) && d.items.isEmpty())
            SharedTimer::instance()->unregisterReceiver(this, "blinkItems");
    }
}

void HighlighterPlugin::resetItems()
{
    foreach (QTreeWidgetItem* item, d.items)
        unhighlightItem(item);
}

void HighlighterPlugin::blinkItems()
{
    foreach (QTreeWidgetItem* item, d.items) {
        item->setData(0, Qt::ForegroundRole, d.blink ? QColor("#ff4040") : QVariant()); // TODO
        QTreeWidgetItem* p = item->parent();
        if (p)
            p->setData(0, Qt::ForegroundRole, d.blink && !p->isExpanded() ? QColor("#ff4040") : QVariant()); // TODO
    }
    d.blink = !d.blink;
}

#if QT_VERSION < 0x050000
Q_EXPORT_STATIC_PLUGIN(HighlighterPlugin)
#endif
<commit_msg>Make the badges blink too<commit_after>/*
* Copyright (C) 2008-2013 The Communi Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*/

#include "highlighterplugin.h"
#include "textdocument.h"
#include "sharedtimer.h"
#include "treewidget.h"
#include "treeitem.h"
#include "treerole.h"
#include <IrcConnection>
#include <IrcMessage>
#include <IrcBuffer>
#include <QTimer>
#include <QEvent>

HighlighterPlugin::HighlighterPlugin(QObject* parent) : QObject(parent)
{
    d.tree = 0;
    d.shortcut = 0;
    d.blink = false;
}

void HighlighterPlugin::initialize(TreeWidget* tree)
{
    d.tree = tree;

    connect(tree, SIGNAL(bufferAdded(IrcBuffer*)), this, SLOT(onBufferAdded(IrcBuffer*)));
    connect(tree, SIGNAL(bufferRemoved(IrcBuffer*)), this, SLOT(onBufferRemoved(IrcBuffer*)));
    connect(tree, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(onItemExpanded(QTreeWidgetItem*)));
    connect(tree, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(onItemCollapsed(QTreeWidgetItem*)));
    connect(tree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
            this, SLOT(onCurrentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)));

    d.shortcut = new QShortcut(tree);
    d.shortcut->setKey(QKeySequence(tr("Ctrl+R")));
    connect(d.shortcut, SIGNAL(activated()), this, SLOT(resetItems()));
}

void HighlighterPlugin::onBufferAdded(IrcBuffer* buffer)
{
    connect(buffer, SIGNAL(messageReceived(IrcMessage*)), this, SLOT(onMessageReceived(IrcMessage*)));
}

void HighlighterPlugin::onBufferRemoved(IrcBuffer* buffer)
{
    d.items.remove(d.tree->bufferItem(buffer));
    disconnect(buffer, SIGNAL(messageReceived(IrcMessage*)), this, SLOT(onMessageReceived(IrcMessage*)));
}

void HighlighterPlugin::onMessageReceived(IrcMessage* message)
{
    if (message->type() == IrcMessage::Private || message->type() == IrcMessage::Notice) {
        IrcBuffer* buffer = qobject_cast<IrcBuffer*>(sender());

        if (message->property("content").toString().contains(message->connection()->nickName())) {
            TreeItem* item = d.tree->bufferItem(buffer);
            if (item && item != d.tree->currentItem())
                highlightItem(item);

            TextDocument* document = buffer->property("document").value<TextDocument*>();
            document->addHighlight(document->totalCount() - 2); // TODO: -2??
        }
    }
}

void HighlighterPlugin::onItemExpanded(QTreeWidgetItem* item)
{
    static_cast<TreeItem*>(item)->refresh();
}

void HighlighterPlugin::onItemCollapsed(QTreeWidgetItem* item)
{
    static_cast<TreeItem*>(item)->refresh();
}

void HighlighterPlugin::onCurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
{
    unhighlightItem(previous);
    unhighlightItem(current);
}

void HighlighterPlugin::highlightItem(QTreeWidgetItem* item)
{
    if (item && !d.items.contains(item)) {
        if (d.items.isEmpty())
            SharedTimer::instance()->registerReceiver(this, "blinkItems");
        d.items.insert(item);
        item->setData(0, TreeRole::Highlight, true);
        item->setData(0, Qt::ForegroundRole, QColor("#ff4040")); // TODO
        item->setData(1, Qt::BackgroundRole, QColor("#ff4040").lighter(125)); // TODO
    }
}

void HighlighterPlugin::unhighlightItem(QTreeWidgetItem* item)
{
    if (item && d.items.contains(item)) {
        d.items.remove(item);
        if (d.items.isEmpty())
            SharedTimer::instance()->unregisterReceiver(this, "blinkItems");
        item->setData(0, TreeRole::Highlight, false);
        item->setData(0, Qt::ForegroundRole, QVariant());
        item->setData(1, Qt::BackgroundRole, QVariant());
    }
}

void HighlighterPlugin::resetItems()
{
    foreach (QTreeWidgetItem* item, d.items)
        unhighlightItem(item);
}

void HighlighterPlugin::blinkItems()
{
    foreach (QTreeWidgetItem* item, d.items) {
        item->setData(0, Qt::ForegroundRole, d.blink ? QColor("#ff4040") : QVariant()); // TODO
        item->setData(1, Qt::BackgroundRole, d.blink ? QColor("#ff4040").lighter(125) : QVariant()); // TODO
        QTreeWidgetItem* p = item->parent();
        if (p)
            p->setData(0, Qt::ForegroundRole, d.blink && !p->isExpanded() ? QColor("#ff4040") : QVariant()); // TODO
    }
    d.blink = !d.blink;
}

#if QT_VERSION < 0x050000
Q_EXPORT_STATIC_PLUGIN(HighlighterPlugin)
#endif

{'fraction_non_alphanumeric': 0.1472472581170594, 'fraction_numerical': 0.010098816375285048, 'mean_word_length': 4.699257425742574, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2549962', 'n_tokens_mistral': 2989, 'n_tokens_neox': 2883, 'n_words': 575}