<commit_before>#include "cellml.h"
#include "centralwidget.h"
#include "fileorganiserwidget.h"
#include "tabwidget.h"

#include "ui_centralwidget.h"

#include <QDir>
#include <QDragEnterEvent>
#include <QFileInfo>
#include <QTextStream>
#include <QUrl>

#include <qsciscintilla.h>
#include <qscilexerxml.h>

CentralWidget::CentralWidget(QWidget *pParent) :
    QWidget(pParent),
    CommonWidget(pParent),
    mUi(new Ui::CentralWidget)
{
    // Set up the UI

    mUi->setupUi(this);

    // Allow for things to be dropped on us

    setAcceptDrops(true);

    // Create and add our tab widget

    mTabWidget = new TabWidget(this);

    mTabWidget->setUsesScrollButtons(true);
    // Note #1: the above property is style dependent and it happens that it's
    //          not enabled on Mac OS X, so... set it in all cases, even though
    //          it's already set on Windows and Linux, but one can never know
    //          what the future holds, so...
    // Note #2: if the above property is not enabled and many files are opened,
    //          then the central widget will widen reducing the width of any
    //          dock window which is clearly not what we want, so...

    mUi->verticalLayout->addWidget(mTabWidget);

    // A connection to handle the closing of a tab

    connect(mTabWidget, SIGNAL(tabCloseRequested(int)),
            this, SLOT(closeTab(int)));
}

CentralWidget::~CentralWidget()
{
    // Delete the UI

    delete mUi;
}

bool CentralWidget::openFile(const QString &pFileName)
{
    if (!QFileInfo(pFileName).exists())
        // The file doesn't exist, so...

        return false;

    // Create an editor for the file

    QsciScintilla *scintilla = new QsciScintilla(this);
    QsciLexerXML *lexer      = new QsciLexerXML;

    QFont defaultFont(DefaultFontFamily, DefaultFontSize);

    // The font that will be used for XML-like and non XML-like files, resp.

    lexer->setDefaultFont(defaultFont);
    lexer->setFont(defaultFont);

    // A few settings for the editor itself

    scintilla->setFolding(QsciScintilla::BoxedTreeFoldStyle);
    scintilla->setLexer(lexer);

    // Set the contents of the file to the editor

    QFile file(pFileName);

    file.open(QIODevice::ReadOnly);

    scintilla->setText(QTextStream(&file).readAll());
    scintilla->setReadOnly(!(QFile::permissions(pFileName) & QFile::WriteUser));

    file.close();

    // Create a new tab and have the editor as its contents

    QFileInfo fileInfo = pFileName;

    mTabWidget->setCurrentIndex(mTabWidget->addTab(scintilla,
                                                   (!fileInfo.completeSuffix().compare(CellmlFileExtension, Qt::CaseInsensitive))?
                                                       fileInfo.baseName():
                                                       fileInfo.fileName()));

    // Set the full name of the file as the tool tip for the new tab

    mTabWidget->setTabToolTip(mTabWidget->currentIndex(),
                              QDir::toNativeSeparators(pFileName));

    // Give the focus to the newly created editor

    mTabWidget->currentWidget()->setFocus();

    // Everything went fine, so...

    return true;
}

QString CentralWidget::closeFile(const int &pIndex)
{
    // Close the file at the given tab index or the current tab index, if no tab
    // index is provided, and then return the name of the file that was closed,
    // if any

    QsciScintilla *scintilla = qobject_cast<QsciScintilla *>(mTabWidget->widget((pIndex != -1)?
                                                                                    pIndex:
                                                                                    mTabWidget->currentIndex()));

    if (scintilla) {
        // There is a file currently opened, so first retrieve its filename

        QString fileName = mTabWidget->tabToolTip(mTabWidget->currentIndex());

        // Next, we must close the tab

        mTabWidget->removeTab(mTabWidget->currentIndex());

        // Then, we must release the allocated memory for the widget that the
        // tab used to contain

        delete scintilla;

        // Finally, we return the filename of the file we have just closed

        return fileName;
    } else {
        // There is no file currently opened, so...

        return QString();
    }
}<commit_msg>Fixed a small issue CentralWidget::closeFile (Oops!!).<commit_after>#include "cellml.h"
#include "centralwidget.h"
#include "fileorganiserwidget.h"
#include "tabwidget.h"

#include "ui_centralwidget.h"

#include <QDir>
#include <QDragEnterEvent>
#include <QFileInfo>
#include <QTextStream>
#include <QUrl>

#include <qsciscintilla.h>
#include <qscilexerxml.h>

CentralWidget::CentralWidget(QWidget *pParent) :
    QWidget(pParent),
    CommonWidget(pParent),
    mUi(new Ui::CentralWidget)
{
    // Set up the UI

    mUi->setupUi(this);

    // Allow for things to be dropped on us

    setAcceptDrops(true);

    // Create and add our tab widget

    mTabWidget = new TabWidget(this);

    mTabWidget->setUsesScrollButtons(true);
    // Note #1: the above property is style dependent and it happens that it's
    //          not enabled on Mac OS X, so... set it in all cases, even though
    //          it's already set on Windows and Linux, but one can never know
    //          what the future holds, so...
    // Note #2: if the above property is not enabled and many files are opened,
    //          then the central widget will widen reducing the width of any
    //          dock window which is clearly not what we want, so...

    mUi->verticalLayout->addWidget(mTabWidget);

    // A connection to handle the closing of a tab

    connect(mTabWidget, SIGNAL(tabCloseRequested(int)),
            this, SLOT(closeTab(int)));
}

CentralWidget::~CentralWidget()
{
    // Delete the UI

    delete mUi;
}

bool CentralWidget::openFile(const QString &pFileName)
{
    if (!QFileInfo(pFileName).exists())
        // The file doesn't exist, so...

        return false;

    // Create an editor for the file

    QsciScintilla *scintilla = new QsciScintilla(this);
    QsciLexerXML *lexer      = new QsciLexerXML;

    QFont defaultFont(DefaultFontFamily, DefaultFontSize);

    // The font that will be used for XML-like and non XML-like files, resp.

    lexer->setDefaultFont(defaultFont);
    lexer->setFont(defaultFont);

    // A few settings for the editor itself

    scintilla->setFolding(QsciScintilla::BoxedTreeFoldStyle);
    scintilla->setLexer(lexer);

    // Set the contents of the file to the editor

    QFile file(pFileName);

    file.open(QIODevice::ReadOnly);

    scintilla->setText(QTextStream(&file).readAll());
    scintilla->setReadOnly(!(QFile::permissions(pFileName) & QFile::WriteUser));

    file.close();

    // Create a new tab and have the editor as its contents

    QFileInfo fileInfo = pFileName;

    mTabWidget->setCurrentIndex(mTabWidget->addTab(scintilla,
                                                   (!fileInfo.completeSuffix().compare(CellmlFileExtension, Qt::CaseInsensitive))?
                                                       fileInfo.baseName():
                                                       fileInfo.fileName()));

    // Set the full name of the file as the tool tip for the new tab

    mTabWidget->setTabToolTip(mTabWidget->currentIndex(),
                              QDir::toNativeSeparators(pFileName));

    // Give the focus to the newly created editor

    mTabWidget->currentWidget()->setFocus();

    // Everything went fine, so...

    return true;
}

QString CentralWidget::closeFile(const int &pIndex)
{
    // Close the file at the given tab index or the current tab index, if no tab
    // index is provided, and then return the name of the file that was closed,
    // if any

    int realIndex = (pIndex != -1)?pIndex:mTabWidget->currentIndex();

    QsciScintilla *scintilla = qobject_cast<QsciScintilla *>(mTabWidget->widget(realIndex));

    if (scintilla) {
        // There is a file currently opened, so first retrieve its filename

        QString fileName = mTabWidget->tabToolTip(realIndex);

        // Next, we must close the tab

        mTabWidget->removeTab(realIndex);

        // Then, we must release the allocated memory for the widget that the
        // tab used to contain

        delete scintilla;

        // Finally, we return the filename of the file we have just closed

        return fileName;
    } else {
        // There is no file currently opened, so...

        return QString();
    }
}
{'fraction_non_alphanumeric': 0.0981903093987157, 'fraction_numerical': 0.0007005253940455342, 'mean_word_length': 2.3804262036306234, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3067677', 'n_tokens_mistral': 2465, 'n_tokens_neox': 2338, 'n_words': 770}