1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/* -*- 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 <QDomNodeList>
#include <QCompleter>
#include <QRegExpValidator>
#include <QRegExp>
#include <QStringListModel>
#include <QSqlQuery>
#include <QSqlError>
#include <QLineEdit>
#include <QComboBox>
#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);
}
|