<commit_before>//==============================================================================
// Property editor widget
//==============================================================================

#include "propertyeditorwidget.h"

//==============================================================================

#include <float.h>

//==============================================================================

#include <QHeaderView>
#include <QKeyEvent>
#include <QScrollBar>
#include <QSettings>
#include <QStandardItem>
#include <QVariant>

//==============================================================================

namespace OpenCOR {
namespace Core {

//==============================================================================

NumberEditorWidget::NumberEditorWidget(QWidget *pParent) :
    QLineEdit(pParent)
{
#ifdef Q_OS_MAC
    setAttribute(Qt::WA_MacShowFocusRect, 0);
    // Note: the above removes the focus border since it messes up the look of
    //       our editor
#endif
    setFrame(QFrame::NoFrame);
}

//==============================================================================

void NumberEditorWidget::keyPressEvent(QKeyEvent *pEvent)
{
    // Check some key combinations

    if (   !(pEvent->modifiers() & Qt::ShiftModifier)
        && !(pEvent->modifiers() & Qt::ControlModifier)
        && !(pEvent->modifiers() & Qt::AltModifier)
        && !(pEvent->modifiers() & Qt::MetaModifier)) {
        // None of the modifiers is selected

        if (pEvent->key() == Qt::Key_Up) {
            // The user wants to go to the previous property

            emit goToPreviousPropertyRequested();

            // Accept the event

            pEvent->accept();
        } else if (pEvent->key() == Qt::Key_Down) {
            // The user wants to go to the next property

            emit goToNextPropertyRequested();

            // Accept the event

            pEvent->accept();
        } else {
            // Default handling of the event

            QLineEdit::keyPressEvent(pEvent);
        }
    } else {
        // Default handling of the event

        QLineEdit::keyPressEvent(pEvent);
    }
}

//==============================================================================

IntegerEditorWidget::IntegerEditorWidget(QWidget *pParent) :
    NumberEditorWidget(pParent)
{
    // Set a validator which accepts any integer

    setValidator(new QRegExpValidator(QRegExp("^[+-]?[0-9]*([eE][+-]?[0-9]+)?$"), this));
}

//==============================================================================

DoubleEditorWidget::DoubleEditorWidget(QWidget *pParent) :
    NumberEditorWidget(pParent)
{
    // Set a validator which accepts any double

    setValidator(new QRegExpValidator(QRegExp("^[+-]?[0-9]*\\.?[0-9]+([eE][+-]?[0-9]+)?$"), this));
}

//==============================================================================

ListEditorWidget::ListEditorWidget(QWidget *pParent) :
    QComboBox(pParent)
{
}

//==============================================================================

void ListEditorWidget::keyPressEvent(QKeyEvent *pEvent)
{
    // Check some key combinations

    if (   !(pEvent->modifiers() & Qt::ShiftModifier)
        && !(pEvent->modifiers() & Qt::ControlModifier)
        && !(pEvent->modifiers() & Qt::AltModifier)
        && !(pEvent->modifiers() & Qt::MetaModifier)) {
        // None of the modifiers is selected

        if (pEvent->key() == Qt::Key_Up) {
            // The user wants to go to the previous property

            emit goToPreviousPropertyRequested();

            // Accept the event

            pEvent->accept();
        } else if (pEvent->key() == Qt::Key_Down) {
            // The user wants to go to the next property

            emit goToNextPropertyRequested();

            // Accept the event

            pEvent->accept();
        } else {
            // Default handling of the event

            QComboBox::keyPressEvent(pEvent);
        }
    } else {
        // Default handling of the event

        QComboBox::keyPressEvent(pEvent);
    }
}

//==============================================================================

void ListEditorWidget::mouseDoubleClickEvent(QMouseEvent *pEvent)
{
    // We want to go to the next item in the list (and go back to the first one
    // if we are at the end of the list), so determine the new current index

    int newCurrentIndex = currentIndex()+1;

    if (newCurrentIndex == count())
        newCurrentIndex = 0;

    // Set the new current index

    setCurrentIndex(newCurrentIndex);

    // Accept the event

    pEvent->accept();
}

//==============================================================================

void ListEditorWidget::mousePressEvent(QMouseEvent *pEvent)
{
    // Check whether the user clicked on the arrow and, if so, allow the default
    // handling of the event (so that the list of items gets displayed) while do
    // nothing if the user clicked somewhere else (this to so that if the user
    // double clicks on the widget, then we can select the next item)
    // Note: sadly, this just doesn't work on OS X, but maybe it's 'normal'?...

    QStyleOptionComboBox styleOption;

    initStyleOption(&styleOption);

    if (style()->hitTestComplexControl(QStyle::CC_ComboBox, &styleOption,
                                       pEvent->pos(), this) == QStyle::SC_ComboBoxArrow)
        // The mouse is over the arrow, so...

        QComboBox::mousePressEvent(pEvent);

    // Accept the event

    pEvent->accept();
}

//==============================================================================<commit_msg>Got our list editor widget for our property editor widget to work as we want on OS X.<commit_after>//==============================================================================
// Property editor widget
//==============================================================================

#include "propertyeditorwidget.h"

//==============================================================================

#include <float.h>

//==============================================================================

#include <QHeaderView>
#include <QKeyEvent>
#include <QScrollBar>
#include <QSettings>
#include <QStandardItem>
#include <QVariant>

//==============================================================================

namespace OpenCOR {
namespace Core {

//==============================================================================

NumberEditorWidget::NumberEditorWidget(QWidget *pParent) :
    QLineEdit(pParent)
{
#ifdef Q_OS_MAC
    setAttribute(Qt::WA_MacShowFocusRect, 0);
    // Note: the above removes the focus border since it messes up the look of
    //       our editor
#endif
    setFrame(QFrame::NoFrame);
}

//==============================================================================

void NumberEditorWidget::keyPressEvent(QKeyEvent *pEvent)
{
    // Check some key combinations

    if (   !(pEvent->modifiers() & Qt::ShiftModifier)
        && !(pEvent->modifiers() & Qt::ControlModifier)
        && !(pEvent->modifiers() & Qt::AltModifier)
        && !(pEvent->modifiers() & Qt::MetaModifier)) {
        // None of the modifiers is selected

        if (pEvent->key() == Qt::Key_Up) {
            // The user wants to go to the previous property

            emit goToPreviousPropertyRequested();

            // Accept the event

            pEvent->accept();
        } else if (pEvent->key() == Qt::Key_Down) {
            // The user wants to go to the next property

            emit goToNextPropertyRequested();

            // Accept the event

            pEvent->accept();
        } else {
            // Default handling of the event

            QLineEdit::keyPressEvent(pEvent);
        }
    } else {
        // Default handling of the event

        QLineEdit::keyPressEvent(pEvent);
    }
}

//==============================================================================

IntegerEditorWidget::IntegerEditorWidget(QWidget *pParent) :
    NumberEditorWidget(pParent)
{
    // Set a validator which accepts any integer

    setValidator(new QRegExpValidator(QRegExp("^[+-]?[0-9]*([eE][+-]?[0-9]+)?$"), this));
}

//==============================================================================

DoubleEditorWidget::DoubleEditorWidget(QWidget *pParent) :
    NumberEditorWidget(pParent)
{
    // Set a validator which accepts any double

    setValidator(new QRegExpValidator(QRegExp("^[+-]?[0-9]*\\.?[0-9]+([eE][+-]?[0-9]+)?$"), this));
}

//==============================================================================

ListEditorWidget::ListEditorWidget(QWidget *pParent) :
    QComboBox(pParent)
{
}

//==============================================================================

void ListEditorWidget::keyPressEvent(QKeyEvent *pEvent)
{
    // Check some key combinations

    if (   !(pEvent->modifiers() & Qt::ShiftModifier)
        && !(pEvent->modifiers() & Qt::ControlModifier)
        && !(pEvent->modifiers() & Qt::AltModifier)
        && !(pEvent->modifiers() & Qt::MetaModifier)) {
        // None of the modifiers is selected

        if (pEvent->key() == Qt::Key_Up) {
            // The user wants to go to the previous property

            emit goToPreviousPropertyRequested();

            // Accept the event

            pEvent->accept();
        } else if (pEvent->key() == Qt::Key_Down) {
            // The user wants to go to the next property

            emit goToNextPropertyRequested();

            // Accept the event

            pEvent->accept();
        } else {
            // Default handling of the event

            QComboBox::keyPressEvent(pEvent);
        }
    } else {
        // Default handling of the event

        QComboBox::keyPressEvent(pEvent);
    }
}

//==============================================================================

void ListEditorWidget::mouseDoubleClickEvent(QMouseEvent *pEvent)
{
    // We want to go to the next item in the list (and go back to the first one
    // if we are at the end of the list), so determine the new current index

    int newCurrentIndex = currentIndex()+1;

    if (newCurrentIndex == count())
        newCurrentIndex = 0;

    // Set the new current index

    setCurrentIndex(newCurrentIndex);

    // Accept the event

    pEvent->accept();
}

//==============================================================================

void ListEditorWidget::mousePressEvent(QMouseEvent *pEvent)
{
    // Check whether the user clicked on the arrow and, if so, allow the default
    // handling of the event (so that the list of items gets displayed) while do
    // nothing if the user clicked somewhere else (this to so that if the user
    // double clicks on the widget, then we can select the next item)
    // Note: we would normally call style()->hitTestComplexControl() and, if it
    //       returns QStyle::SC_ComboBoxArrow, then allow the default handling
    //       of the event, but if this works fine on Windows and Linux, it just
    //       doesn't work on OS X. Indeed, no matter where we are over the
    //       widget, style()->hitTestComplexControl() will always (and as
    //       expected; see [QtSources]/gui/styles/qmacstyle_mac.mm) return
    //       QStyle::SC_ComboBoxArrow. So, to get the behaviour we are after, we
    //       do what is done in [QtSources]/gui/styles/qcommonstyle.cpp...

    // Retrieve our style option

    QStyleOptionComboBox styleOption;

    initStyleOption(&styleOption);

    // Go through the different sub controls (starting with the arrow) and
    // determine over which one we are

    uint subControl = QStyle::SC_ComboBoxArrow;
    QRect subControlRect;

    while (subControl) {
        // Retrieve the sub control's region

        subControlRect = style()->subControlRect(QStyle::CC_ComboBox,
                                                 &styleOption,
                                                 QStyle::SubControl(subControl),
                                                 this);

        // Check whether the sub control exists (i.e. its region is valid) and,
        // if so, whether we ore over it

        if (subControlRect.isValid() && subControlRect.contains(pEvent->pos()))
            // The sub control exists and we are over it, so...

            break;

        // Either the sub control doesn't exist or we are not over it, so try
        // the next sub control

        subControl >>= 1;
    }

    // Check whether the 'current' sub control is the arrow we are after

    if (QStyle::SubControl(subControl) == QStyle::SC_ComboBoxArrow)
        // It is the arrow we are after, so...

        QComboBox::mousePressEvent(pEvent);

    // Accept the event

    pEvent->accept();
}

//==============================================================================
{'fraction_non_alphanumeric': 0.269176800748363, 'fraction_numerical': 0.002104770813844715, 'mean_word_length': 3.0739917434106063, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3395507', 'n_tokens_mistral': 3271, 'n_tokens_neox': 3089, 'n_words': 912}