HAL
python_editor_code_completion_dialog.cpp
Go to the documentation of this file.
2 
4 
5 #include <QAbstractItemView>
6 #include <QDebug>
7 #include <QHeaderView>
8 #include <QKeyEvent>
9 #include <QString>
10 #include <QStringList>
11 #include <QTableWidgetItem>
12 #include "hal_core/utilities/log.h"
13 
14 namespace hal
15 {
16  PythonEditorCodeCompletionDialog::PythonEditorCodeCompletionDialog(QWidget* parent, std::vector<std::tuple<std::string, std::string>> completions) : QDialog(parent), m_completions(completions)
17  {
18  //the parent has to be a layouter_view, needs a rework, for now its sufficient
19  mTable = new QTableWidget(this);
20  mTable->setParent(this);
21  mTable->setShowGrid(false);
22  mTable->verticalHeader()->setVisible(false);
23  mTable->horizontalHeader()->setVisible(false);
25  mTable->setColumnCount(1);
26  mTable->setRowCount(completions.size());
27  mTable->setStyleSheet("QTableView {background-color: black; color: white; selection-background-color: grey;};");
29 
30  int counter = 0;
31  for (const auto& tup : completions)
32  {
33  auto full_text = std::get<0>(tup);
34  mTable->setItem(counter, 0, new QTableWidgetItem(QString::fromStdString(full_text)));
35  counter++;
36  }
37 
38  mTable->selectRow(0);
39  mTable->resizeColumnsToContents();
40  mTable->resizeRowsToContents();
41 
42  //check if the calculated minimum size is bigger than the predefined maximum size, if yes, set the minimum size = the predefined maximum size, otherwise set the minimum size
43  //to the calculated size
44  QSize calculatedSize = tableWidgetSize(mTable);
45  if (calculatedSize.width() > 400)
46  mTable->setMinimumWidth(400);
47  else
48  mTable->setMinimumWidth(calculatedSize.width());
49 
50  if (calculatedSize.height() > 600)
51  mTable->setMinimumHeight(600);
52  else
53  mTable->setMinimumHeight(calculatedSize.height());
54 
55  //mTable->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
56  //mTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
57 
58  this->adjustSize();
59  this->setFocusProxy(mTable);
60  mTable->adjustSize();
63 
64  //these lines need to be put after the lines above, dunno why
65  this->setEnabled(true);
66  this->setModal(true);
67  this->setVisible(true);
68  }
69 
70  //list is focused, so no need to implement down/up button, because the list automatically does the right thing for you
72  {
73  if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
74  {
75  auto index = mTable->currentRow();
76  Q_EMIT completionSelected(m_completions.at(index));
77  this->close();
78  }
79 
80  if (event->key() == Qt::Key_Escape || event->key() == Qt::Key_Left)
81  {
82  this->close();
83  }
84  }
85 
86  QSize PythonEditorCodeCompletionDialog::tableWidgetSize(QTableWidget* table)
87  {
88  int width = 4;
89  for (int i = 0; i < table->columnCount(); i++)
90  width += table->columnWidth(i);
91 
92  int height = table->horizontalHeader()->height() + 8;
93  for (int i = 0; i < table->rowCount(); i++)
94  height += table->rowHeight(i);
95 
96  return QSize(width, height);
97  }
98 }
void completionSelected(std::tuple< std::string, std::string > selected)
PythonEditorCodeCompletionDialog(QWidget *parent, std::vector< std::tuple< std::string, std::string >> completions)
void setEditTriggers(QAbstractItemView::EditTriggers triggers)
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
void setModal(bool modal)
virtual void setVisible(bool visible) override
virtual void setVisible(bool v) override
int key() const const
Q_EMITQ_EMIT
int height() const const
int width() const const
QString fromStdString(const std::string &str)
Key_Enter
WA_DeleteOnClose
int columnWidth(int column) const const
QHeaderView * horizontalHeader() const const
void resizeColumnsToContents()
void resizeRowsToContents()
int rowHeight(int row) const const
void selectRow(int row)
void setShowGrid(bool show)
QHeaderView * verticalHeader() const const
int currentRow() const const
void setColumnCount(int columns)
void setItem(int row, int column, QTableWidgetItem *item)
void setRowCount(int rows)
void adjustSize()
bool close()
void setEnabled(bool)
virtual bool event(QEvent *event) override
void setMinimumHeight(int minh)
void setMinimumWidth(int minw)
void setAttribute(Qt::WidgetAttribute attribute, bool on)
void setFocusProxy(QWidget *w)
void setParent(QWidget *parent)
void setStyleSheet(const QString &styleSheet)
void setWindowFlags(Qt::WindowFlags type)