From 4549daa34926ca806daf6cbf4aea70bf24338f4a Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 3 Sep 2008 14:30:05 +0000 Subject: Did some work on the database widget. It now works with hardcoded medicine list. --- client/client.pro | 2 +- client/widgets/dbwidget.cc | 158 ++++++++++++++++++++++++++++++++------------- client/widgets/dbwidget.h | 10 ++- 3 files changed, 124 insertions(+), 46 deletions(-) (limited to 'client') diff --git a/client/client.pro b/client/client.pro index bb4711b..7ada8f1 100644 --- a/client/client.pro +++ b/client/client.pro @@ -4,7 +4,7 @@ TEMPLATE = app TARGET = pracro DEPENDPATH += . widgets INCLUDEPATH += . widgets -QT += core gui network xml +QT += core gui network xml sql # For debugging QMAKE_CXXFLAGS += -g -Wall -Werror diff --git a/client/widgets/dbwidget.cc b/client/widgets/dbwidget.cc index cb331c7..d0f4def 100644 --- a/client/widgets/dbwidget.cc +++ b/client/widgets/dbwidget.cc @@ -30,68 +30,56 @@ #include #include #include +#include +#include +#include +#include #include "common.h" -#include -#include -class ItemModel : public QAbstractItemModel { -public: - Qt::ItemFlags flags ( const QModelIndex & /*index*/ ) const { - return Qt::ItemIsEnabled | Qt::ItemIsSelectable; - } - QVariant data ( const QModelIndex & /*index*/, int /*role*/ ) const { - return QString("fisk"); - } - QVariant headerData ( int /*section*/, Qt::Orientation /*orientation*/, int /*role*/ ) const - { - return QString("æsel"); - } - int rowCount ( const QModelIndex & /*parent*/ ) const - { - return 10; - } - QModelIndex index ( int /*row*/, int /*column*/, const QModelIndex & /*parent*/ ) const - { - return QModelIndex(); - } - QModelIndex parent(const QModelIndex &/*index*/) const - { - return QModelIndex(); - } - int columnCount(const QModelIndex &/*parent*/) const - { - return 1; - } -}; - -ItemModel itemmodel; - - DBWidget::DBWidget(QDomNode &node, MacroWindow *macrowindow) : QComboBox(), Widget(node, macrowindow) { + QDomElement elem = node.toElement(); + + db = QSqlDatabase::addDatabase("QPSQL"); + db.setHostName("sensei.j.auh.dk"); + db.setDatabaseName("lms"); + db.setUserName("postgres"); + // db.setPassword(""); + bool ok = db.open(); + if(!ok) { + printf("DB connect failed!\n"); + } + setCommonAttributes(this, node); setInsertPolicy(QComboBox::InsertAlphabetically); + setEditable(true); // Make empty default selection. + addItem("Skriv noget i søgefeltet."); setCurrentIndex(-1); - QDomElement elem = node.toElement(); - - setEditable(true); - - QCompleter *completer = new QCompleter(&itemmodel, this); + QStringListModel *strlst = new QStringListModel(); + QCompleter *completer = new QCompleter(this); completer->setCaseSensitivity(Qt::CaseInsensitive); completer->setCompletionMode(QCompleter::PopupCompletion); + completer->setModel(strlst); setCompleter(completer); - + connect(this, SIGNAL(editTextChanged(QString)), this, SLOT(changed())); + connect((QWidget*)lineEdit(), SIGNAL(textEdited(QString)), this, SLOT(update_list(QString))); changed(); } +DBWidget::~DBWidget() +{ + db.close(); +} + + QString DBWidget::getValue() { QString value; @@ -103,7 +91,14 @@ QString DBWidget::getValue() return value; } - +/* +void DBWidget::focusInEvent(QFocusEvent *) +{ + if(currentText() == "Skriv noget i søgefeltet.") { + setEditText(""); + } +} +*/ void DBWidget::setValue(QString value) { int idx = findData(value); @@ -113,7 +108,11 @@ void DBWidget::setValue(QString value) bool DBWidget::isValid() { - return true;//rx.exactMatch(currentText()); + QSqlQuery query = db.exec("SELECT drugname, dosageform_code, strength_text FROM lms01 WHERE" + " LOWER(drugname || ' ' || strength_text || ' (' || dosageform_code || ')' )" + " = '" + currentText().toLower() + "';"); + + return query.size() != 0; } void DBWidget::changed() @@ -128,7 +127,42 @@ void DBWidget::changed() palette.setBrush(QPalette::Base, QBrush(QColor(230, 200, 200))); } - setPalette(palette); + lineEdit()->setPalette(palette); +} + +void DBWidget::update_list(QString prefix) +{ + /* + if(prefix == "") { + clear(); + addItem("Skriv noget i søgefeltet."); + setCurrentIndex(-1); + return; + } + */ + QSqlQuery query = db.exec("SELECT drugname, dosageform_code, strength_text FROM lms01 WHERE" + " LOWER(drugname || ' ' || strength_text || ' (' || dosageform_code || ')' )" + " LIKE '" + prefix.toLower() + "%';"); + + QStringList lst; + while(query.next()) { + QString drugname = query.value(0).toString(); + QString dosageform = query.value(1).toString(); + QString strength = query.value(2).toString(); + lst << drugname + " " + strength + " (" + dosageform + ")"; + } + + lst.sort(); + + QStringListModel *mdl = (QStringListModel *)completer()->model(); + if(mdl->stringList() != lst) { + QString val = currentText(); + clear(); + if(lst.size() != 0) addItems(lst); + else addItem("Søgningen passede ikke på noget."); + setEditText(val); + mdl->setStringList(lst); + } } void DBWidget::enable() @@ -140,3 +174,39 @@ void DBWidget::disable() { setEnabled(false); } +/* +$ psql -h sensei -d lms -U postgres +=================================================================== + List of relations + Schema | Name | Type | Owner +--------+-------+-------+---------- + public | lms01 | table | postgres +(1 row) + +lms=# \d lms01 + Table "public.lms01" + Column | Type | Modifiers +-------------------------------+-----------------------+----------- + drugid | character varying(32) | + producttype | character varying(14) | + productsubtype | character varying(14) | + sequencetext | character varying(28) | + specialitynumber | character varying(20) | + drugname | character varying(70) | + dosageform_text | character varying(50) | + dosageform_code | character varying(24) | + further_dos_info | character varying(24) | + strength_text | character varying(50) | + strength_numeric | character varying(30) | + strength_unit | character varying(16) | + marketing_authorisation_owner | character varying(22) | + importer | character varying(22) | + atc | character varying(26) | + route_of_administration | character varying(26) | + traffic_warning | character varying(12) | + substitution | character varying(12) | + substitution_group | character varying(18) | + dose_dispensing | character varying(12) | + deregistration_date | character varying(26) | + quarantine_date | character varying(26) | +*/ diff --git a/client/widgets/dbwidget.h b/client/widgets/dbwidget.h index d0105a8..0438ca5 100644 --- a/client/widgets/dbwidget.h +++ b/client/widgets/dbwidget.h @@ -31,12 +31,16 @@ #include #include #include +#include + +#include class DBWidget : public QComboBox, public Widget { Q_OBJECT public: DBWidget(QDomNode &node, MacroWindow *macrowindow); + ~DBWidget(); bool isValid(); @@ -48,9 +52,13 @@ public: public slots: void changed(); + void update_list(QString prefix); -private: +protected: + // void focusInEvent(QFocusEvent *); +private: + QSqlDatabase db; }; #endif/*__PRACRO_DBWIDGET_H__*/ -- cgit v1.2.3