<commit_before>/*
* Copyright (C) 2008-2014 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 "titlebar.h"
#include "messageformatter.h"
#include <QStyleOptionHeader>
#include <QPropertyAnimation>
#include <QStylePainter>
#include <IrcTextFormat>
#include <QApplication>
#include <QMouseEvent>
#include <QHeaderView>
#include <IrcCommand>
#include <IrcChannel>
#include <QTreeView>
#include <QStyle>
#include <QMenu>

class TitleMenu : public QMenu
{
public:
    TitleMenu(QWidget* parent) : QMenu(parent) { }

protected:
    void moveEvent(QMoveEvent*)
    {
        // TODO: looks scary - any better ideas?
        if (QWidget* parent = parentWidget()) {
            QRect rect = geometry();
            rect.moveTopRight(parent->mapToGlobal(parent->rect().bottomRight()));
#ifdef Q_OS_MAC
            rect.translate(0, -2); // TODO
#endif
            setGeometry(rect);
        }
    }
};

TitleBar::TitleBar(QWidget* parent) : QLabel(parent)
{
    d.buffer = 0;
    d.baseOffset = -1;
    d.editor = new QTextEdit(this);
    d.formatter = new MessageFormatter(this);

    setWordWrap(true);
    setOpenExternalLinks(true);
    setTextFormat(Qt::RichText);
    setAlignment(Qt::AlignLeft | Qt::AlignTop);

#ifdef Q_OS_MAC
    QFont font;
    font.setPointSize(11.0);
    setFont(font);
#endif

    d.editor->setVisible(false);
    d.editor->setAcceptRichText(false);
    d.editor->setTabChangesFocus(true);
    d.editor->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    d.editor->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    d.editor->installEventFilter(this);

    d.menuButton = new QToolButton(this);
    d.menuButton->setObjectName("menu");
    d.menuButton->setMenu(new TitleMenu(d.menuButton));
    d.menuButton->setPopupMode(QToolButton::InstantPopup);
    d.menuButton->adjustSize();

    adjustSize();
    relayout();
}

QMenu* TitleBar::menu() const
{
    return d.menuButton->menu();
}

QSize TitleBar::minimumSizeHint() const
{
    // QMacStyle wants a QHeaderView that is a child of QTreeView :/
    QTreeView tree;
    QStyleOptionHeader option;
    return style()->sizeFromContents(QStyle::CT_HeaderSection, &option, QSize(), tree.header());<commit_msg>TitleBar: prevent focus in the menu button<commit_after>/*
* Copyright (C) 2008-2014 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 "titlebar.h"
#include "messageformatter.h"
#include <QStyleOptionHeader>
#include <QPropertyAnimation>
#include <QStylePainter>
#include <IrcTextFormat>
#include <QApplication>
#include <QMouseEvent>
#include <QHeaderView>
#include <IrcCommand>
#include <IrcChannel>
#include <QTreeView>
#include <QStyle>
#include <QMenu>

class TitleMenu : public QMenu
{
public:
    TitleMenu(QWidget* parent) : QMenu(parent) { }

protected:
    void moveEvent(QMoveEvent*)
    {
        // TODO: looks scary - any better ideas?
        if (QWidget* parent = parentWidget()) {
            QRect rect = geometry();
            rect.moveTopRight(parent->mapToGlobal(parent->rect().bottomRight()));
#ifdef Q_OS_MAC
            rect.translate(0, -2); // TODO
#endif
            setGeometry(rect);
        }
    }
};

TitleBar::TitleBar(QWidget* parent) : QLabel(parent)
{
    d.buffer = 0;
    d.baseOffset = -1;
    d.editor = new QTextEdit(this);
    d.formatter = new MessageFormatter(this);

    setWordWrap(true);
    setOpenExternalLinks(true);
    setTextFormat(Qt::RichText);
    setAlignment(Qt::AlignLeft | Qt::AlignTop);

#ifdef Q_OS_MAC
    QFont font;
    font.setPointSize(11.0);
    setFont(font);
#endif

    d.editor->setVisible(false);
    d.editor->setAcceptRichText(false);
    d.editor->setTabChangesFocus(true);
    d.editor->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    d.editor->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    d.editor->installEventFilter(this);

    d.menuButton = new QToolButton(this);
    d.menuButton->setObjectName("menu");
    d.menuButton->setMenu(new TitleMenu(d.menuButton));
    d.menuButton->setPopupMode(QToolButton::InstantPopup);
    d.menuButton->setFocusPolicy(Qt::NoFocus);
    d.menuButton->adjustSize();

    adjustSize();
    relayout();
}

QMenu* TitleBar::menu() const
{
    return d.menuButton->menu();
}

QSize TitleBar::minimumSizeHint() const
{
    // QMacStyle wants a QHeaderView that is a child of QTreeView :/
    QTreeView tree;
    QStyleOptionHeader option;
    return style()->sizeFromContents(QStyle::CT_HeaderSection, &option, QSize(), tree.header());
{'fraction_non_alphanumeric': 0.12028824833702882, 'fraction_numerical': 0.005912786400591279, 'mean_word_length': 4.077861163227017, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 56, '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': '4410095', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1652, 'n_words': 434}