Skip to content

Commit

Permalink
Boolean function editor added
Browse files Browse the repository at this point in the history
  • Loading branch information
joern274 committed Jun 8, 2024
1 parent f7dd78c commit 6d00d6c
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,44 @@
#include <QLineEdit>
#include <QTextEdit>
#include <QLabel>
#include <QSet>

namespace hal {
class GateLibraryWizard;

class BoolWizardPage;

class BooleanFunctionEdit : public QLineEdit
{
Q_OBJECT
public:

// Would like to define the state as enum, however,
// something seems to go wrong when QSS style converts it to string
// enum State {Empty, Valid, Invalid};
// Q_ENUM(State)
Q_PROPERTY(QString state READ state WRITE setState NOTIFY stateChanged);

private:
QString mState;
std::set<std::string> mLegalVariables;

Q_SIGNALS:
void stateChanged(QString s);
private Q_SLOTS:
void handleEditingFinished();
public:
BooleanFunctionEdit(std::set<std::string>& legalVar, QWidget* parent = nullptr);
QString state() const { return mState; }
void setState(const QString& s);
};

class BoolWizardPage:public QWizardPage
{
friend BooleanFunctionEdit;
public:
BoolWizardPage(QWidget* parent = nullptr);
void initializePage() override;
bool validatePage() override;
void setData(GateType* gate);

private:
Expand Down
6 changes: 4 additions & 2 deletions plugins/gui/include/gui/pin_model/pin_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ namespace hal

u32 getId() const;
QString getName() const;
QString getType() const;
QString getDirection() const;
PinType getPinType() const;
PinDirection getDirection() const;
QString getPinTypeAsText() const;
QString getDirectionAsText() const;

TreeItemType getItemType() const;

Expand Down
4 changes: 2 additions & 2 deletions plugins/gui/include/gui/pin_model/pin_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ namespace hal
* @param index index of the PinItem
* @param direction new direction
*/
void handleEditDirection(QModelIndex index, const QString& direction);
void handleEditDirection(QModelIndex index, const QString& directionString);

/**
* should be called when the items type is changed via the delegate
* @param index index of the PinItem
* @param type new type
*/
void handleEditType(QModelIndex index, const QString& type);
void handleEditType(QModelIndex index, const QString& typeString);

void handleDeleteItem(QModelIndex index);

Expand Down
21 changes: 21 additions & 0 deletions plugins/gui/resources/stylesheet/dark.qss
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,27 @@ hal--GatelibraryManagementDialog QHeaderView::section
background: rgb(35, 35, 35);
}

hal--BooleanFunctionEdit
{
background : black;
color : #E0F0FF;
font-family : "Iosevka";
font-size : 15px;
font-style : "Heavy";
font-weight : 700;
}

hal--BooleanFunctionEdit[state="Empty"]
{
background : #171e22;
color : #909090;
}

hal--BooleanFunctionEdit[state="Invalid"]
{
background: red;
}

hal--GuiPluginManager
{
qproperty-loadIconPath : ":/icons/insert-plugin";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,56 @@

namespace hal
{
BooleanFunctionEdit::BooleanFunctionEdit(std::set<std::string> &legalVar, QWidget *parent)
: QLineEdit(parent), mState("Valid"), mLegalVariables(legalVar)
{
connect(this, &QLineEdit::editingFinished, this, &BooleanFunctionEdit::handleEditingFinished);
setState("Empty");
}

void BooleanFunctionEdit::setState(const QString &s)
{
if (s == mState) return;
mState = s;
Q_EMIT stateChanged(s);
QStyle* sty = style();

sty->unpolish(this);
sty->polish(this);
}

void BooleanFunctionEdit::handleEditingFinished()
{
if (text().isEmpty())
{
setState("Empty");
return;
}

QString nextState = "Valid"; // think positive

auto bfres = BooleanFunction::from_string(text().toStdString());
if(bfres.is_error())
nextState = "Invalid";
else
{
BooleanFunction bf = bfres.get();
std::set<std::string> var_names = bf.get_variable_names();

for(std::string vname : var_names)
{
if (mLegalVariables.find(vname) == mLegalVariables.end())
{
nextState = "Invalid";
break;
}
}
}
if (mState != nextState)
setState(nextState);
}

//--------------------------------------------
BoolWizardPage::BoolWizardPage(QWidget* parent) : QWizardPage(parent)
{
setTitle("Step 4: Boolean functions");
Expand All @@ -15,14 +65,19 @@ namespace hal
mWizard = static_cast<GateLibraryWizard*>(wizard());
QList<PinItem*> pinGroups = mWizard->getPingroups();

QList<PinItem*> inputPins = mWizard->mPinModel->getInputPins();
std::set<std::string> input_pins;
for (PinItem* pi : inputPins)
input_pins.insert(pi->getName().toStdString());

if(mGate != nullptr){
auto boolFunctions = mGate->get_boolean_functions();
auto list = QList<QPair<QString, BooleanFunction>>();
int boolFuncCnt = 0;

for(std::pair<const std::basic_string<char>, BooleanFunction> bf : boolFunctions){
QLabel* label = new QLabel(QString::fromStdString(bf.first));
QLineEdit* lineEdit = new QLineEdit(this);
BooleanFunctionEdit* lineEdit = new BooleanFunctionEdit(input_pins, this);
mLayout->addWidget(label, boolFuncCnt, 0);
mLayout->addWidget(lineEdit, boolFuncCnt, 1);
lineEdit->setText(QString::fromStdString(bf.second.to_string()));
Expand All @@ -34,20 +89,19 @@ namespace hal
{
int rowCount = 0;
for(PinItem* pinGroup : pinGroups){
if(pinGroup->getItemType() != PinItem::TreeItemType::GroupCreator && pinGroup->getDirection() == "output"){
if(pinGroup->getItemType() != PinItem::TreeItemType::GroupCreator && pinGroup->getDirection() == PinDirection::output){
for(auto item : pinGroup->getChildren())
{
PinItem* pin = static_cast<PinItem*>(item);
if(pin->getItemType() != PinItem::TreeItemType::PinCreator){
QLabel* label = new QLabel(pin->getName());
QString name = label->text();
QLineEdit* lineEdit = new QLineEdit(this);
BooleanFunctionEdit* lineEdit = new BooleanFunctionEdit(input_pins, this);
mLayout->addWidget(label, rowCount, 0);
mLayout->addWidget(lineEdit, rowCount, 1);
rowCount++;
}
}

}
}
}
Expand All @@ -60,23 +114,4 @@ namespace hal
mGate = gate;
}

bool BoolWizardPage::validatePage(){

int rowCount = 0;
QList<PinItem*> inputPins = mWizard->mPinModel->getInputPins();
QList<PinItem*> outputPins = mWizard->mPinModel->getOutputPins();
while(mLayout->itemAtPosition(rowCount, 1) != nullptr){
QLabel* label = static_cast<QLabel*>(mLayout->itemAtPosition(rowCount, 1)->widget());
auto bfres = BooleanFunction::from_string(label->text().toStdString());
if(bfres.is_error()) return false;
BooleanFunction bf = bfres.get();
std::set<std::string> names = bf.get_variable_names();
for(auto item : inputPins)
{
if(names.find(item->getName().toStdString()) == names.end()) return false;
}
rowCount++;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ void RAMPortWizardPage::initializePage(){
//create empty lines for ram_port for each data/address pair
//assumption at this point: #data fields = #address fields
for (int i=0; i<pinGroups.length()-1; i++) { //-1 for dummy entries
QString type = pinGroups[i]->getType();
if(type == "data")
PinType type = pinGroups[i]->getPinType();
if(type == PinType::data)
{
ramPortCnt++;
mLayout->addWidget(new GateLibraryLabel(false, QString("RAM Port %1").arg(ramPortCnt), this), 6*(ramPortCnt-1), 0);
Expand Down
4 changes: 2 additions & 2 deletions plugins/gui/src/pin_model/pin_delegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ namespace hal
if(itemType != PinItem::TreeItemType::Pin && itemType != PinItem::TreeItemType::InvalidPin)
break;
auto comboBox = static_cast<QComboBox*>(editor);
comboBox->setCurrentText(pinItem->getDirection());
comboBox->setCurrentText(pinItem->getDirectionAsText());
break;
}
case 2: {
//type editor
if(itemType == PinItem::TreeItemType::PinCreator || itemType == PinItem::TreeItemType::GroupCreator)
break;
auto comboBox = static_cast<QComboBox*>(editor);
comboBox->setCurrentText(pinItem->getType());
comboBox->setCurrentText(pinItem->getPinTypeAsText());
break;
}
}
Expand Down
14 changes: 12 additions & 2 deletions plugins/gui/src/pin_model/pin_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,22 @@ namespace hal
return mName;
}

QString PinItem::getType() const
PinType PinItem::getPinType() const
{
return mType;
}

QString PinItem::getPinTypeAsText() const
{
return QString::fromStdString(enum_to_string(mType));
}

QString PinItem::getDirection() const
PinDirection PinItem::getDirection() const
{
return mDirection;
}

QString PinItem::getDirectionAsText() const
{
return QString::fromStdString(enum_to_string(mDirection));
}
Expand Down
Loading

0 comments on commit 6d00d6c

Please sign in to comment.