2013-04-10 18:14:10 +00:00
|
|
|
#include "scriptsubview.hpp"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2015-04-29 10:24:17 +00:00
|
|
|
#include <QStatusBar>
|
|
|
|
#include <QStackedLayout>
|
|
|
|
#include <QLabel>
|
|
|
|
|
2013-04-10 18:14:10 +00:00
|
|
|
#include "../../model/doc/document.hpp"
|
|
|
|
#include "../../model/world/universalid.hpp"
|
|
|
|
#include "../../model/world/data.hpp"
|
|
|
|
#include "../../model/world/columnbase.hpp"
|
|
|
|
#include "../../model/world/commands.hpp"
|
|
|
|
#include "../../model/world/idtable.hpp"
|
2015-04-29 20:32:03 +00:00
|
|
|
#include "../../model/settings/usersettings.hpp"
|
2013-04-10 18:14:10 +00:00
|
|
|
|
2014-02-15 18:52:40 +00:00
|
|
|
#include "scriptedit.hpp"
|
2013-04-10 20:49:22 +00:00
|
|
|
|
2013-04-10 18:14:10 +00:00
|
|
|
CSVWorld::ScriptSubView::ScriptSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document)
|
2015-04-29 10:24:17 +00:00
|
|
|
: SubView (id), mDocument (document), mColumn (-1), mBottom(0), mStatus(0)
|
2013-04-10 18:14:10 +00:00
|
|
|
{
|
2015-04-29 10:24:17 +00:00
|
|
|
QVBoxLayout *layout = new QVBoxLayout;
|
|
|
|
layout->setContentsMargins (QMargins (0, 0, 0, 0));
|
|
|
|
|
|
|
|
mBottom = new QWidget(this);
|
|
|
|
QStackedLayout *bottmLayout = new QStackedLayout(mBottom);
|
|
|
|
bottmLayout->setContentsMargins (0, 0, 0, 0);
|
|
|
|
QStatusBar *statusBar = new QStatusBar(mBottom);
|
|
|
|
mStatus = new QLabel(mBottom);
|
|
|
|
statusBar->addWidget (mStatus);
|
|
|
|
bottmLayout->addWidget (statusBar);
|
|
|
|
mBottom->setLayout (bottmLayout);
|
|
|
|
|
|
|
|
layout->addWidget (mBottom, 0);
|
|
|
|
layout->insertWidget (0, mEditor = new ScriptEdit (mDocument, ScriptHighlighter::Mode_General, this), 2);
|
|
|
|
|
|
|
|
QWidget *widget = new QWidget;
|
|
|
|
widget->setLayout (layout);
|
|
|
|
setWidget (widget);
|
2013-04-10 18:14:10 +00:00
|
|
|
|
|
|
|
mModel = &dynamic_cast<CSMWorld::IdTable&> (
|
|
|
|
*document.getData().getTableModel (CSMWorld::UniversalId::Type_Scripts));
|
|
|
|
|
|
|
|
for (int i=0; i<mModel->columnCount(); ++i)
|
|
|
|
if (mModel->headerData (i, Qt::Horizontal, CSMWorld::ColumnBase::Role_Display)==
|
2015-04-04 17:55:53 +00:00
|
|
|
CSMWorld::ColumnBase::Display_ScriptFile)
|
2013-04-10 18:14:10 +00:00
|
|
|
{
|
|
|
|
mColumn = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mColumn==-1)
|
|
|
|
throw std::logic_error ("Can't find script column");
|
|
|
|
|
|
|
|
mEditor->setPlainText (mModel->data (mModel->getModelIndex (id.getId(), mColumn)).toString());
|
|
|
|
|
|
|
|
connect (mEditor, SIGNAL (textChanged()), this, SLOT (textChanged()));
|
|
|
|
|
|
|
|
connect (mModel, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
|
|
|
|
this, SLOT (dataChanged (const QModelIndex&, const QModelIndex&)));
|
|
|
|
|
2013-04-10 19:31:14 +00:00
|
|
|
connect (mModel, SIGNAL (rowsAboutToBeRemoved (const QModelIndex&, int, int)),
|
|
|
|
this, SLOT (rowsAboutToBeRemoved (const QModelIndex&, int, int)));
|
2015-04-29 10:24:17 +00:00
|
|
|
|
|
|
|
updateStatusBar();
|
|
|
|
connect(mEditor, SIGNAL(cursorPositionChanged()), this, SLOT(updateStatusBar()));
|
|
|
|
}
|
|
|
|
|
2015-04-29 20:32:03 +00:00
|
|
|
void CSVWorld::ScriptSubView::updateUserSetting (const QString& name, const QStringList& value)
|
2015-04-29 10:24:17 +00:00
|
|
|
{
|
2015-04-30 20:08:04 +00:00
|
|
|
if (name == "script-editor/show-linenum")
|
|
|
|
{
|
|
|
|
std::string showLinenum = value.at(0).toStdString();
|
|
|
|
mEditor->showLineNum(showLinenum == "true");
|
|
|
|
mBottom->setVisible(showLinenum == "true");
|
|
|
|
}
|
|
|
|
else if (name == "script-editor/mono-font")
|
|
|
|
{
|
|
|
|
mEditor->setMonoFont(value.at(0).toStdString() == "true");
|
|
|
|
}
|
2015-04-29 10:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::ScriptSubView::updateStatusBar ()
|
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
|
|
|
|
stream << "(" << mEditor->textCursor().blockNumber() + 1 << ", "
|
|
|
|
<< mEditor->textCursor().columnNumber() + 1 << ")";
|
|
|
|
|
|
|
|
mStatus->setText (QString::fromUtf8 (stream.str().c_str()));
|
2013-04-10 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::ScriptSubView::setEditLock (bool locked)
|
|
|
|
{
|
|
|
|
mEditor->setReadOnly (locked);
|
|
|
|
}
|
|
|
|
|
2014-12-08 11:29:23 +00:00
|
|
|
void CSVWorld::ScriptSubView::useHint (const std::string& hint)
|
|
|
|
{
|
|
|
|
if (hint.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (hint[0]=='l')
|
|
|
|
{
|
|
|
|
std::istringstream stream (hint.c_str()+1);
|
|
|
|
|
|
|
|
char ignore;
|
|
|
|
int line;
|
|
|
|
int column;
|
|
|
|
|
|
|
|
if (stream >> ignore >> line >> column)
|
|
|
|
{
|
|
|
|
QTextCursor cursor = mEditor->textCursor();
|
|
|
|
|
|
|
|
cursor.movePosition (QTextCursor::Start);
|
|
|
|
if (cursor.movePosition (QTextCursor::Down, QTextCursor::MoveAnchor, line))
|
|
|
|
cursor.movePosition (QTextCursor::Right, QTextCursor::MoveAnchor, column);
|
|
|
|
|
2015-03-16 02:07:37 +00:00
|
|
|
mEditor->setFocus();
|
2014-12-08 11:29:23 +00:00
|
|
|
mEditor->setTextCursor (cursor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 18:14:10 +00:00
|
|
|
void CSVWorld::ScriptSubView::textChanged()
|
|
|
|
{
|
2014-08-21 12:50:13 +00:00
|
|
|
if (mEditor->isChangeLocked())
|
2013-09-19 12:26:09 +00:00
|
|
|
return;
|
|
|
|
|
2014-08-21 12:50:13 +00:00
|
|
|
ScriptEdit::ChangeLock lock (*mEditor);
|
2013-04-10 18:14:10 +00:00
|
|
|
|
|
|
|
mDocument.getUndoStack().push (new CSMWorld::ModifyCommand (*mModel,
|
|
|
|
mModel->getModelIndex (getUniversalId().getId(), mColumn), mEditor->toPlainText()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::ScriptSubView::dataChanged (const QModelIndex& topLeft, const QModelIndex& bottomRight)
|
|
|
|
{
|
2014-08-21 12:50:13 +00:00
|
|
|
if (mEditor->isChangeLocked())
|
2013-04-10 18:14:10 +00:00
|
|
|
return;
|
|
|
|
|
2014-08-21 12:50:13 +00:00
|
|
|
ScriptEdit::ChangeLock lock (*mEditor);
|
2013-09-19 12:26:09 +00:00
|
|
|
|
2013-04-10 18:14:10 +00:00
|
|
|
QModelIndex index = mModel->getModelIndex (getUniversalId().getId(), mColumn);
|
|
|
|
|
|
|
|
if (index.row()>=topLeft.row() && index.row()<=bottomRight.row() &&
|
|
|
|
index.column()>=topLeft.column() && index.column()<=bottomRight.column())
|
|
|
|
{
|
|
|
|
QTextCursor cursor = mEditor->textCursor();
|
|
|
|
mEditor->setPlainText (mModel->data (index).toString());
|
|
|
|
mEditor->setTextCursor (cursor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::ScriptSubView::rowsAboutToBeRemoved (const QModelIndex& parent, int start, int end)
|
|
|
|
{
|
2013-04-10 19:31:14 +00:00
|
|
|
QModelIndex index = mModel->getModelIndex (getUniversalId().getId(), mColumn);
|
2013-04-10 18:14:10 +00:00
|
|
|
|
2013-04-10 19:31:14 +00:00
|
|
|
if (!parent.isValid() && index.row()>=start && index.row()<=end)
|
2014-10-25 16:13:56 +00:00
|
|
|
emit closeRequest();
|
2013-09-19 12:26:09 +00:00
|
|
|
}
|
|
|
|
|