From 97d32901efc2b6cbec3ab41f78fa409d2ce78804 Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 14 Mar 2011 11:38:48 +0000 Subject: Some more changes to LineEdit lua methods to make medicine macro (db looked up suggestions to lineedit) work as expected. --- client/luawidget.cc | 89 +++++++++++++++++++++++++++++++++++++++++++++- client/widgets/lineedit.cc | 40 +++++++++++++++++++++ client/widgets/lineedit.h | 8 +++++ 3 files changed, 136 insertions(+), 1 deletion(-) (limited to 'client') diff --git a/client/luawidget.cc b/client/luawidget.cc index 86dc2bb..944120d 100644 --- a/client/luawidget.cc +++ b/client/luawidget.cc @@ -66,6 +66,7 @@ static int wdg_name(lua_State *L) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "Widget"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "CheckBox"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "ComboBox"); + if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); luaL_argcheck(L, wdgu, 1, "widget expected"); lua_pushstring(L, wdgu->widget->name().toStdString().c_str()); @@ -80,6 +81,7 @@ static int wdg_type(lua_State *L) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "Widget"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "CheckBox"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "ComboBox"); + if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); luaL_argcheck(L, wdgu, 1, "widget expected"); // return error code @@ -95,6 +97,7 @@ static int wdg_value(lua_State *L) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "Widget"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "CheckBox"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "ComboBox"); + if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); luaL_argcheck(L, wdgu, 1, "widget expected"); lua_pushstring(L, wdgu->widget->value().toStdString().c_str()); @@ -109,6 +112,7 @@ static int wdg_set_value(lua_State *L) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "Widget"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "CheckBox"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "ComboBox"); + if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); luaL_argcheck(L, wdgu, 1, "widget expected"); const char *val = luaL_checkstring(L, 2); @@ -125,6 +129,7 @@ static int wdg_enabled(lua_State *L) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "Widget"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "CheckBox"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "ComboBox"); + if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); luaL_argcheck(L, wdgu, 1, "widget expected"); lua_pushboolean(L, wdgu->widget->enabled()); @@ -139,6 +144,7 @@ static int wdg_set_enabled(lua_State *L) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "Widget"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "CheckBox"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "ComboBox"); + if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); luaL_argcheck(L, wdgu, 1, "widget expected"); bool val = luaL_checkbool(L, 2); @@ -155,6 +161,7 @@ static int wdg_visible(lua_State *L) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "Widget"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "CheckBox"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "ComboBox"); + if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); luaL_argcheck(L, wdgu, 1, "widget expected"); lua_pushboolean(L, wdgu->widget->visible()); @@ -169,6 +176,7 @@ static int wdg_set_visible(lua_State *L) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "Widget"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "CheckBox"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "ComboBox"); + if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); luaL_argcheck(L, wdgu, 1, "widget expected"); bool val = luaL_checkbool(L, 2); @@ -185,6 +193,7 @@ static int wdg_valid(lua_State *L) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "Widget"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "CheckBox"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "ComboBox"); + if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); luaL_argcheck(L, wdgu, 1, "widget expected"); lua_pushboolean(L, wdgu->widget->valid()); @@ -199,6 +208,7 @@ static int wdg_set_valid(lua_State *L) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "Widget"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "CheckBox"); if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "ComboBox"); + if(!wdgu) wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); luaL_argcheck(L, wdgu, 1, "widget expected"); bool val = luaL_checkbool(L, 2); @@ -288,7 +298,7 @@ static int cmb_le_value(lua_State *L) luaL_argcheck(L, wdgu, 1, "combobox expected"); ComboBox *cmb = (ComboBox*)wdgu->widget; - lua_pushstring(L, cmb->lineEditValue().toStdString().c_str()); + lua_pushstring(L, cmb->lineEditValue().toStdString().c_str()); return 1; } @@ -314,6 +324,71 @@ static int cmb_le_set_value(lua_State *L) {"lineEditValue", cmb_le_value},\ {"setLineEditValue", cmb_le_set_value} +static int lin_clear_suggestions(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); + luaL_argcheck(L, wdgu, 1, "lineedit expected"); + + LineEdit *led = (LineEdit*)wdgu->widget; + led->clearSuggestions(); + + return 0; +} + +static int lin_show_suggestions(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); + luaL_argcheck(L, wdgu, 1, "lineedit expected"); + + LineEdit *led = (LineEdit*)wdgu->widget; + led->showSuggestions(); + + return 0; +} + +static int lin_is_suggested(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); + luaL_argcheck(L, wdgu, 1, "lineedit expected"); + + const char *val = luaL_checkstring(L, 2); + + LineEdit *led = (LineEdit*)wdgu->widget; + + lua_pushboolean(L, led->isSuggested(val)); + + return 1; +} + +static int lin_add_suggestion(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_isudata(L, 1, "LineEdit"); + luaL_argcheck(L, wdgu, 1, "lineedit expected"); + + const char *val = luaL_checkstring(L, 2); + + LineEdit *led = (LineEdit*)wdgu->widget; + led->addSuggestion(val); + + return 0; +} + + +#define LINEDT_METHS \ + {"clearSuggestions", lin_clear_suggestions},\ + {"showSuggestions", lin_show_suggestions},\ + {"isSuggested", lin_is_suggested},\ + {"addSuggestion", lin_add_suggestion} + + static const struct luaL_Reg wdg_meths[] = { WDG_METHS, {NULL, NULL} }; @@ -323,6 +398,9 @@ static const struct luaL_Reg chkbox_meths[] = static const struct luaL_Reg cmbbox_meths[] = { WDG_METHS, CMBBOX_METHS, {NULL, NULL} }; +static const struct luaL_Reg linedt_meths[] = + { WDG_METHS, LINEDT_METHS, {NULL, NULL} }; + int wdg_make_widget(lua_State *L, Widget *widget) { wdg_userdata *wdgu; @@ -330,6 +408,7 @@ int wdg_make_widget(lua_State *L, Widget *widget) if(widget->type() == "combobox") luaL_getmetatable(L, "ComboBox"); else if(widget->type() == "checkbox") luaL_getmetatable(L, "CheckBox"); + else if(widget->type() == "lineedit") luaL_getmetatable(L, "LineEdit"); else luaL_getmetatable(L, "Widget"); lua_setmetatable(L, -2); @@ -364,5 +443,13 @@ void register_widget(lua_State *L) luaL_register(L, NULL, cmbbox_meths); // lua_pop(L, 2); + + luaL_newmetatable(L, "LineEdit"); + lua_pushliteral(L, "__index"); + lua_pushvalue(L, -2); + lua_rawset(L, -3); + luaL_register(L, NULL, linedt_meths); + + // lua_pop(L, 2); } diff --git a/client/widgets/lineedit.cc b/client/widgets/lineedit.cc index 97fb92b..d6aa619 100644 --- a/client/widgets/lineedit.cc +++ b/client/widgets/lineedit.cc @@ -32,6 +32,9 @@ #include #include +#include +#include + #include "common.h" #include "debug.h" @@ -62,6 +65,13 @@ LineEdit::LineEdit(QDomNode &node, MacroWindow *macrowindow) connect(lineedit, SIGNAL(textEdited(QString)), this, SLOT(user_changed())); lineedit->installEventFilter(this); // Detect keyboard input. + + QCompleter *comp = new QCompleter(); + comp->setCaseSensitivity(Qt::CaseInsensitive); + QStringListModel *mdl = new QStringListModel(); + comp->setModel(mdl); + // comp->popup()->setWindowOpacity(0.3); + lineedit->setCompleter(comp); } LineEdit::~LineEdit() @@ -71,6 +81,7 @@ LineEdit::~LineEdit() void LineEdit::changed() { + // if(lineedit->completer()->popup()->isVisible()) return; emit eventOnChange(); } @@ -133,3 +144,32 @@ void LineEdit::setWdgValid(bool valid) lineedit->setPalette(palette); } + +void LineEdit::clearSuggestions() +{ + QCompleter *comp = lineedit->completer(); + QStringListModel *mdl = (QStringListModel *)comp->model(); + suggestions.clear(); + mdl->setStringList(suggestions); +} + +void LineEdit::addSuggestion(QString suggestion) +{ + suggestions << suggestion; + suggestions.sort(); + + QCompleter *comp = lineedit->completer(); + QStringListModel *mdl = (QStringListModel *)comp->model(); + mdl->setStringList(suggestions); +} + +bool LineEdit::isSuggested(QString val) +{ + return suggestions.contains(val); +} + +void LineEdit::showSuggestions() +{ + QCompleter *comp = lineedit->completer(); + comp->complete(); +} diff --git a/client/widgets/lineedit.h b/client/widgets/lineedit.h index 90fac75..8159aad 100644 --- a/client/widgets/lineedit.h +++ b/client/widgets/lineedit.h @@ -27,6 +27,8 @@ #ifndef __PRACRO_LINEEDIT_H__ #define __PRACRO_LINEEDIT_H__ +#include + #include "widget.h" class QLineEdit; @@ -43,6 +45,11 @@ public: void setWdgValid(bool valid); + void clearSuggestions(); + void addSuggestion(QString suggestion); + bool isSuggested(QString value); + void showSuggestions(); + public slots: void changed(); void user_changed(); @@ -54,6 +61,7 @@ private: bool eventFilter(QObject *, QEvent *event); QLineEdit *lineedit; + QStringList suggestions; }; #endif/*__PRACRO_LINEEDIT_H__*/ -- cgit v1.2.3