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

started with completer

parent b6dd4811
......@@ -6,10 +6,13 @@
*/
#include "command.h"
#include "tlibs/time/chrono.h"
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QLineEdit>
#include "tlibs/time/chrono.h"
#include <QtWidgets/QCompleter>
#include <QtWidgets/QAbstractItemView>
#include <QtGui/QStandardItemModel>
// ----------------------------------------------------------------------------
......@@ -25,6 +28,13 @@ CommandLineWidget::CommandLineWidget(QWidget *pParent, QSettings *pSettings)
m_pEditCLI->lineEdit()->setPlaceholderText("Enter Command");
m_pEditCLI->lineEdit()->setFocus();
m_pEditCLI->setCompleter(new QCompleter(this));
m_pEditCLI->completer()->setModel(new QStandardItemModel(m_pEditCLI->completer()));
//m_pEditCLI->completer()->popup()->setModel(m_pEditCLI->completer()->model());
m_pEditCLI->completer()->setCaseSensitivity(Qt::CaseSensitive);
m_pEditCLI->completer()->setModelSorting(QCompleter::CaseSensitivelySortedModel);
m_pEditCLI->completer()->setCompletionMode(QCompleter::PopupCompletion);
// ------------------------------------------------------------------------
// layout
......@@ -54,6 +64,27 @@ CommandLineWidget::~CommandLineWidget()
}
void CommandLineWidget::UpdateCompleter()
{
auto *mod = static_cast<QStandardItemModel*>(m_pEditCLI->completer()->model());
mod->clear();
// add user-defined items to list
for(const auto& str : m_completerItems)
{
auto item = new QStandardItem(str);
mod->appendRow(item);
}
// recent commands stored in combo box
for(int idx=0; idx<m_pEditCLI->count(); ++idx)
{
auto item = new QStandardItem(m_pEditCLI->itemIcon(idx), m_pEditCLI->itemText(idx));
mod->appendRow(item);
}
}
void CommandLineWidget::CommandEntered()
{
QString cmd = m_pEditCLI->currentText().trimmed();
......@@ -111,6 +142,9 @@ void CommandLineWidget::CommandEntered()
std::ostringstream ostrRes;
sym->print(ostrRes);
PrintOutput(0, ostrRes.str().c_str());
// add successful commands to completer
UpdateCompleter();
}
else
{
......
......@@ -29,6 +29,7 @@ private:
QTextEdit *m_pEditHistory = new QTextEdit(this);
QComboBox *m_pEditCLI = new QComboBox(this);
QStringList m_completerItems;
CliParserContext m_parsectx;
......@@ -36,6 +37,7 @@ private:
protected:
void CommandEntered();
void ScrollToEnd();
void UpdateCompleter();
public:
CommandLineWidget(QWidget *pParent = nullptr, QSettings *pSettings = nullptr);
......@@ -51,6 +53,8 @@ public:
(ostr << ... << std::forward<T>(msgs));
PrintOutputString(is_err, ostr.str().c_str());
}
void SetCompleterItems(const QStringList& lst) { m_completerItems = lst; UpdateCompleter(); }
};
......
......@@ -7,6 +7,7 @@
#include "mainwnd.h"
#include "globals.h"
#include "funcs.h"
MainWnd::MainWnd(QSettings* pSettings)
......@@ -46,10 +47,11 @@ MainWnd::MainWnd(QSettings* pSettings)
this->addDockWidget(Qt::BottomDockWidgetArea, m_pCurPlot);
// ------------------------------------------------------------------------
// connections
connect(m_pBrowser->GetWidget(), &FileBrowserWidget::TransferFiles,
m_pWS->GetWidget(), &WorkSpaceWidget::ReceiveFiles);
connect(m_pBrowser->GetWidget(), &FileBrowserWidget::TransferFiles, m_pWS->GetWidget(), &WorkSpaceWidget::ReceiveFiles);
connect(m_pWS->GetWidget(), &WorkSpaceWidget::PlotDataset, m_pCurPlot->GetWidget(), &Plotter::Plot);
connect(m_pBrowser->GetWidget(), &FileBrowserWidget::PlotDataset, m_pCurPlot->GetWidget(), &Plotter::Plot);
......@@ -75,6 +77,23 @@ MainWnd::MainWnd(QSettings* pSettings)
this->restoreState(m_pSettings->value("mainwnd/state").toByteArray());
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// add the built-in function list to the completer
QStringList lstFuncs;
for(const auto &pair : g_funcs_real_1arg) lstFuncs.push_back(pair.first.c_str());
for(const auto &pair : g_funcs_real_2args) lstFuncs.push_back(pair.first.c_str());
for(const auto &pair : g_funcs_arr_1arg) lstFuncs.push_back(pair.first.c_str());
for(const auto &pair : g_funcs_arr_2args) lstFuncs.push_back(pair.first.c_str());
for(const auto &pair : g_funcs_gen_0args) lstFuncs.push_back(pair.first.c_str());
for(const auto &pair : g_funcs_gen_1arg) lstFuncs.push_back(pair.first.c_str());
for(const auto &pair : g_funcs_gen_2args) lstFuncs.push_back(pair.first.c_str());
for(const auto &pair : g_funcs_gen_vararg) lstFuncs.push_back(pair.first.c_str());
for(const auto &pair : g_consts_real) lstFuncs.push_back(pair.first.c_str());
m_pCLI->GetWidget()->SetCompleterItems(lstFuncs);
// ------------------------------------------------------------------------
}
......
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