HAL
boolean_function_table.cpp
Go to the documentation of this file.
2 #include "gui/gui_globals.h"
4 #include <QHeaderView>
5 #include <QtWidgets/QMenu>
6 #include <QApplication>
7 #include <QClipboard>
10 
11 namespace hal {
13  mBooleanFunctionTableModel(new BooleanFunctionTableModel(this)), mCurrentGate(nullptr),
14  mCurrentGateId(0), mShowPlainDescr(false), mShowPlainPyDescr(false), mChangeBooleanFunc(false)
15  {
16  this->setModel(mBooleanFunctionTableModel);
19  this->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
21  this->verticalHeader()->setVisible(false);
22  this->horizontalHeader()->setVisible(false);
23  this->setShowGrid(false);
25 
26  connect(this, &QTableView::customContextMenuRequested, this, &BooleanFunctionTable::handleContextMenuRequest);
27  }
28 
30  {
31  return mBooleanFunctionTableModel;
32  }
33 
35  mBooleanFunctionTableModel->setEntries(entries);
36  adjustTableSizes();
37  this->clearSelection();
38  this->update();
39  }
40 
42  {
43  mCurrentGate = g;
44  mCurrentGateId = g ? g->get_id() : 0;
45  }
46 
47  void BooleanFunctionTable::resizeEvent(QResizeEvent *event)
48  {
50  adjustTableSizes();
51  }
52 
53  void BooleanFunctionTable::handleContextMenuRequest(const QPoint &pos)
54  {
55  QModelIndex idx = indexAt(pos);
56  if(!idx.isValid()){
57  return;
58  }
59 
60  QSharedPointer<BooleanFunctionTableEntry> entry = mBooleanFunctionTableModel->getEntryAtRow(idx.row());
61  QMenu menu;
62  QString menuText;
63  QString pythonCode;
64 
65  /*====================================
66  Plaintext to Clipboard
67  ====================================*/
68 
69  QString toClipboardText = entry->getEntryValueString();
70  QString description = mShowPlainDescr ? "Boolean function to clipboard" : entry->getEntryIdentifier() + " function to clipboard";
71  menu.addAction(
72  //"Copy plain function to clipboard",
73  description,
74  [toClipboardText]()
75  {
76  QApplication::clipboard()->setText( toClipboardText );
77  }
78  );
79 
80  if(mChangeBooleanFunc && mCurrentGate)
81  {
82  QString entryIdentifier = entry->getEntryIdentifier();
83  menu.addAction("Change Boolean function", [this, entryIdentifier](){
84  InputDialog ipd("Change Boolean function", "New function", "");
85  if(ipd.exec() == QDialog::Accepted && !ipd.textValue().isEmpty())
86  {
87  auto funcRes = BooleanFunction::from_string(ipd.textValue().toStdString());
88  if(funcRes.is_ok())
89  {
90  ActionAddBooleanFunction* act = new ActionAddBooleanFunction(entryIdentifier, funcRes.get(), mCurrentGateId);
91  act->exec();
92  }
93  }
94  });
95  }
96 
97  /*====================================
98  Python to Clipboard
99  ====================================*/
100  pythonCode = entry->getPythonCode();
101  QString pythonDesc = mShowPlainPyDescr ? "Get boolean function" : "Get " + entry->getEntryIdentifier() + " function";
102  if(!pythonCode.isEmpty())
103  {
104  menu.addAction(QIcon(":/icons/python"), pythonDesc, [pythonCode](){
105  QApplication::clipboard()->setText(pythonCode);
106  });
107  }
108 
109  menu.move(mapToGlobal(pos));
110  menu.exec();
111  }
112 
113  void BooleanFunctionTable::adjustTableSizes()
114  {
117  this->setWordWrap(true);
118  this->resizeRowsToContents();
119 
120  // Configure the widget height
121  int h = 0;
122  for (int i = 0; i < mBooleanFunctionTableModel->rowCount(); i++)
123  h += rowHeight(i);
124 
125  setMaximumHeight(h);
126  setMinimumHeight(h);
127  }
128 
129 } // namespace hal
static Result< BooleanFunction > from_string(const std::string &expression)
BooleanFunctionTable(QWidget *parent=nullptr)
BooleanFunctionTableModel * getModel()
void setEntries(QVector< QSharedPointer< BooleanFunctionTableEntry >> entries)
A model that holds BooleanFunctions and clear-preset behaviors.
QSharedPointer< BooleanFunctionTableEntry > getEntryAtRow(int row) const
void setEntries(QVector< QSharedPointer< BooleanFunctionTableEntry >> entries)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: gate.h:58
virtual bool event(QEvent *event) override
virtual void resizeEvent(QResizeEvent *event) override
void setSelectionMode(QAbstractItemView::SelectionMode mode)
void setText(const QString &text, QClipboard::Mode mode)
void setFrameStyle(int style)
QClipboard * clipboard()
void setSectionResizeMode(QHeaderView::ResizeMode mode)
virtual void setVisible(bool v) override
QAction * addAction(const QString &text)
QAction * exec()
bool isValid() const const
int row() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool isEmpty() const const
QHeaderView * horizontalHeader() const const
virtual QModelIndex indexAt(const QPoint &pos) const const override
void resizeColumnsToContents()
void resizeRowsToContents()
int rowHeight(int row) const const
virtual void setModel(QAbstractItemModel *model) override
void setShowGrid(bool show)
QHeaderView * verticalHeader() const const
void setWordWrap(bool on)
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
void customContextMenuRequested(const QPoint &pos)
void setFocusPolicy(Qt::FocusPolicy policy)
QPoint mapToGlobal(const QPoint &pos) const const
void setMaximumHeight(int maxh)
void setMinimumHeight(int minh)
void move(int x, int y)
void setSizePolicy(QSizePolicy)
void update()