/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /*************************************************************************** * dbwidget.cc * * Mon Sep 1 16:23:54 CEST 2008 * Copyright 2008 Bent Bisballe Nyeng * deva@aasimon.org ****************************************************************************/ /* * This file is part of Pracro. * * Pracro is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Pracro is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Pracro; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include "dbwidget.h" #include #include #include #include #include #include #include #include #include #include "formatparser.h" #include "common.h" #define EMPTY_STRING "Write something in the searchfield" DBWidget::DBWidget(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { combobox = new QComboBox(); widget = combobox; changedByUser = true; QDomElement elem = node.toElement(); if(!elem.hasAttribute("driver") || !elem.hasAttribute("server") || !elem.hasAttribute("user") || !elem.hasAttribute("database") || !elem.hasAttribute("select") || !elem.hasAttribute("from") || !elem.hasAttribute("where") || !elem.hasAttribute("format")) { printf("Error: missing tag!\n"); } select = elem.attribute("select"); from = elem.attribute("from"); where = elem.attribute("where"); format = elem.attribute("format"); db = QSqlDatabase::addDatabase(elem.attribute("driver")); db.setHostName(elem.attribute("server")); if(elem.hasAttribute("port")) db.setPort(elem.attribute("port").toInt()); db.setDatabaseName(elem.attribute("database")); db.setUserName(elem.attribute("user")); if(elem.hasAttribute("password")) db.setPassword(elem.attribute("password")); db.setConnectOptions("connect_timeout=2000"); bool ok = db.open(); if(!ok) { printf("DB connect failed!\n"); } setCommonAttributes(combobox, node); combobox->setInsertPolicy(QComboBox::InsertAlphabetically); combobox->setEditable(true); // Make empty default selection. combobox->addItem(tr("Write something in the searchfield")); combobox->setCurrentIndex(-1); QStringListModel *strlst = new QStringListModel(); QCompleter *completer = new QCompleter(this); completer->setCaseSensitivity(Qt::CaseInsensitive); completer->setCompletionMode(QCompleter::PopupCompletion); completer->setModel(strlst); combobox->setCompleter(completer); connect(combobox, SIGNAL(editTextChanged(QString)), this, SLOT(changed())); connect((QWidget*)combobox->lineEdit(), SIGNAL(textEdited(QString)), this, SLOT(update_list(QString))); changed(); } DBWidget::~DBWidget() { db.close(); db = QSqlDatabase(); // delete combobox; } QString DBWidget::value() { QString value; value = combobox->currentText(); return value; } void DBWidget::setValue(QString value, QString source) { changedByUser = false; if(isUserSource(source)) emit wasChanged(); combobox->setEditText(value); setInitialValue(value); changedByUser = true; } bool DBWidget::preValid() { return combobox->currentText() != tr(EMPTY_STRING) && combobox->currentText() != "" && combobox->findText(combobox->currentText()) != -1 ; } void DBWidget::changed() { if(changedByUser) emit wasChanged(); eventOnChange(); } void DBWidget::update_list(QString prefix) { if(prefix == "") { combobox->clear(); combobox->addItem(tr(EMPTY_STRING)); combobox->setCurrentIndex(-1); return; } QSqlQuery query = db.exec("SELECT " + select + " FROM " + from + " WHERE LOWER(" + where + ")" " LIKE '" + prefix.toLower() + "%';"); QStringList lst; while(query.next()) { lst << format_parser(format, query); } lst.sort(); QStringListModel *mdl = (QStringListModel *)combobox->completer()->model(); if(mdl->stringList() != lst) { QString val = combobox->currentText(); combobox->clear(); if(lst.size() == 0) lst << "Søgningen passede ikke på noget."; combobox->addItems(lst); combobox->setEditText(val); mdl->setStringList(lst); //showPopup(); } } void DBWidget::setWdgValid(bool valid) { QPalette palette; if(valid) { // valid string palette.setBrush(QPalette::Base, QBrush(QColor(255, 255, 255))); } else { // invalid string palette.setBrush(QPalette::Base, QBrush(QColor(230, 200, 200))); } combobox->lineEdit()->setPalette(palette); combobox->setPalette(palette); }