#include "EditorTab.h"
#include "CodeEditor.h"

#include <QFileInfo>

EditorTab::EditorTab(const QString &filePath, Settings *settings, QWidget *parent)
    : QWidget(parent)
    , m_filePath(filePath)
{
    m_layout = new QVBoxLayout(this);
    m_layout->setContentsMargins(0, 0, 0, 0);
    m_layout->setSpacing(0);

    m_editor = new CodeEditor(settings, this);
    m_editor->loadFile(filePath);

    m_layout->addWidget(m_editor);
}

QString EditorTab::filePath() const
{
    return m_filePath;
}

QString EditorTab::fileName() const
{
    return QFileInfo(m_filePath).fileName();
}

CodeEditor *EditorTab::editor() const
{
    return m_editor;
}

bool EditorTab::isModified() const
{
    return m_editor->isModified();
}

bool EditorTab::save()
{
    const bool ok = m_editor->save();
    // Path may have changed if this was an untitled buffer saved for the first time
    m_filePath = m_editor->filePath();
    return ok;
}

bool EditorTab::saveAs()
{
    const bool ok = m_editor->saveAs();
    m_filePath = m_editor->filePath();
    return ok;
}
