Commit ce98e6c2 authored by Tobias WEBER's avatar Tobias WEBER
Browse files

added variable renaming

parent d19af3cd
......@@ -118,6 +118,9 @@ std::shared_ptr<Symbol> call_realfunc_1arg_pointwise(const std::string& ident, s
// ----------------------------------------------------------------------------
/**
* typeof function
......@@ -155,6 +158,57 @@ static std::shared_ptr<Symbol> func_sizeof(CliParserContext & ctx, std::shared_p
}
/**
* convert a dataset to an array
*/
std::shared_ptr<Symbol> func_array(CliParserContext & ctx, std::shared_ptr<Symbol> _sym)
{
if(_sym->GetType() == SymbolType::DATASET)
{
std::vector<std::shared_ptr<Symbol>> arr;
const auto& dataset = dynamic_cast<SymbolDataset&>(*_sym).GetValue();
// iterate channels
for(std::size_t ch=0; ch<dataset.GetNumChannels(); ++ch)
{
const auto& dat = dataset.GetChannel(ch);
if(dat.GetNumAxes()==0 || dat.GetNumCounters()==0)
{
ctx.PrintError("Invalid x axis or counter data in channel ", ch, ".");
return nullptr;
}
const auto& xvals = dat.GetAxis(0);
const auto& yvals = dat.GetCounter(0);
const auto& yerrs = dat.GetCounterErrors(0);
std::vector<std::shared_ptr<Symbol>> x, y, yerr;
for(std::size_t i=0; i<xvals.size(); ++i)
{
x.emplace_back(std::make_shared<SymbolReal>(xvals[i]));
y.emplace_back(std::make_shared<SymbolReal>(yvals[i]));
yerr.emplace_back(std::make_shared<SymbolReal>(yerrs[i]));
}
// add x, y, yerr to channel
std::vector<std::shared_ptr<Symbol>> arrCh;
arrCh.emplace_back(std::make_shared<SymbolList>(x, false));
arrCh.emplace_back(std::make_shared<SymbolList>(y, false));
arrCh.emplace_back(std::make_shared<SymbolList>(yerr, false));
// add channel to array
arr.emplace_back(std::make_shared<SymbolList>(arrCh, false));
}
return std::make_shared<SymbolList>(arr, false);
}
return nullptr;
}
/**
* help
*/
......@@ -380,9 +434,10 @@ std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)
{
std::make_pair("typeof", std::make_tuple(&func_typeof, "return symbol type")),
std::make_pair("sizeof", std::make_tuple(&func_sizeof, "return symbol size")),
//std::make_pair("clear", std::make_tuple(&func_clear, "removes a variable")),
std::make_pair("toarray", std::make_tuple(&func_array, "convert a dataset to an array")),
};
/**
* map of general functions with two arguments
*/
......@@ -396,6 +451,10 @@ std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)
// ----------------------------------------------------------------------------
/**
* dot product
......
......@@ -36,6 +36,7 @@ WorkSpaceWidget::WorkSpaceWidget(QWidget *pParent, QSettings *pSettings)
// connections
connect(m_pListFiles, &QListWidget::currentItemChanged, this, &WorkSpaceWidget::ItemSelected);
connect(m_pListFiles, &QListWidget::itemDoubleClicked, this, &WorkSpaceWidget::ItemDoubleClicked);
connect(m_pListFiles->itemDelegate(), &QAbstractItemDelegate::commitData, this, &WorkSpaceWidget::ItemEdited);
// ------------------------------------------------------------------------
......@@ -88,6 +89,61 @@ void WorkSpaceWidget::ItemDoubleClicked(QListWidgetItem* pCur)
}
/**
* an item in the list was edited
*/
void WorkSpaceWidget::ItemEdited()
{
// the edited item is the one where the text does not match to the user data
for(int item=0; item<m_pListFiles->count(); ++item)
{
auto* pItem = m_pListFiles->item(item);
auto newName = pItem->text();
auto oldName = pItem->data(Qt::UserRole).toString();
// TODO: check if newName is a valid identifier
if(newName == "")
{
print_err("\"", newName.toStdString(), "\" is an invalid identifier.");
pItem->setText(oldName); // rename back
continue;
}
// try to change name
if(newName != oldName)
{
// update the workspace map
auto node = m_workspace.extract(oldName.toStdString());
if(!node)
{
print_err("Variable \"", oldName.toStdString(), "\" cannot be found.");
continue;
}
node.key() = newName.toStdString();
auto inserted = m_workspace.insert(std::move(node));
if(inserted.inserted)
{
// sync data with new name
pItem->setData(Qt::UserRole, newName);
}
else
{
// renaming failed, undo changes
inserted.node.key() = oldName.toStdString();
m_workspace.insert(std::move(inserted.node));
pItem->setText(oldName);
print_err("Variable \"", oldName.toStdString(), "\" cannot be renamed due to a conflict.");
}
print_out("Variable renamed: \"", oldName.toStdString(), "\" -> \"", newName.toStdString(), "\".");
}
}
}
/**
* transfer a file from the file browser and convert it into the internal format
......@@ -124,7 +180,7 @@ void WorkSpaceWidget::ReceiveFiles(const std::vector<std::string> &files)
*/
void WorkSpaceWidget::UpdateList()
{
// add missing symbols to workspace list
// add missing symbols to list widget
for(const auto& [ident, symdataset] : m_workspace)
{
// skip real or string variables
......@@ -139,11 +195,12 @@ void WorkSpaceWidget::UpdateList()
auto *pItem = new QListWidgetItem(m_pListFiles);
pItem->setText(qident);
//pItem->setData(Qt::UserRole, qident);
pItem->setData(Qt::UserRole, qident); // also set identifier as data (used in renaming)
pItem->setFlags(Qt::ItemIsEditable | pItem->flags());
}
// remove superfluous symbols from workspace list
// remove superfluous symbols from list widget
for(int idx=m_pListFiles->count()-1; idx>=0; --idx)
{
std::string ident = m_pListFiles->item(idx)->text().toStdString();
......
......@@ -44,6 +44,7 @@ public:
protected:
void ItemSelected(QListWidgetItem* pCur);
void ItemDoubleClicked(QListWidgetItem* pCur);
void ItemEdited();
bool eventFilter(QObject *pObj, QEvent *pEvt);
public:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment