2 * This file is part of LineMan.
4 * Copyright 2006 Igor Khanin
6 * LiteMan is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * LiteMan is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with LiteMan; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <QRadioButton>
22 #include <QPushButton>
29 #include <QGridLayout>
30 #include <QHBoxLayout>
31 #include <QVBoxLayout>
33 #include "queryeditordialog.h"
34 #include "termeditor.h"
38 * @brief Creates the query editor.
40 * @param parent The parent widget for the dialog.
42 QueryEditorDialog::QueryEditorDialog(Database
* dbase
, Mode mode
, QWidget
* parent
): QDialog(parent
)
49 QStringList tables
= db
->getTables();
50 tableList
->addItems(tables
);
52 // If a database has at least one table. auto select it
54 tableSelected(tables
[0]);
56 if(mode
== CreateView
)
57 setWindowTitle(tr("Create View"));
59 setWindowTitle(tr("Build Query"));
62 QueryEditorDialog::~QueryEditorDialog()
67 * @brief generates a valid SQL statement using the values in the dialog
69 QString
QueryEditorDialog::statement()
72 QString sql
= "SELECT ";
74 // Add checked fields list
75 for(int i
= 0; i
< checkLayout
->count(); i
++)
77 QWidget
* widget
= checkLayout
->itemAt(i
)->widget();
82 QCheckBox
* check
= qobject_cast
<QCheckBox
*>(widget
);
84 if(check
->checkState() == Qt::Checked
)
85 sql
+= (check
->text() + ", ");
87 sql
= sql
.remove(sql
.size() - 2, 2); // cut the extra ", "
90 sql
+= (" FROM '" + tableList
->currentText() + "'");
92 // Optionaly add terms
93 if(termsLayout
->count() > 0)
95 // But first determine what is the chosen logic word (And/Or)
96 (andButton
->isChecked()) ? logicWord
= "AND" : logicWord
= "OR";
100 for(int i
= 0; i
< termsLayout
->count(); i
++)
102 QWidget
* widget
= termsLayout
->itemAt(i
)->widget();
107 TermEditor
* term
= qobject_cast
<TermEditor
*>(widget
);
110 sql
+= term
->selectedField();
112 switch(term
->selectedRelation())
115 sql
+= (" LIKE '%" + term
->selectedValue() + "%'");
118 case 1: // Doesn't contain
119 sql
+= (" NOT LIKE '%" + term
->selectedValue() + "%'");
123 sql
+= (" = '" + term
->selectedValue() + "'");
126 case 3: // Not equals
127 sql
+= (" <> '" + term
->selectedValue() + "'");
130 case 4: // Bigger than
131 sql
+= (" > '" + term
->selectedValue() + "'");
134 case 5: // Smaller than
135 sql
+= (" < '" + term
->selectedValue() + "'");
139 sql
+= (" " + logicWord
+ " ");
141 sql
= sql
.remove(sql
.size() - (logicWord
.size() + 2), logicWord
.size() + 2); // cut the extra " AND " or " OR "
148 QString
QueryEditorDialog::viewName()
150 return viewNameEdit
->text();
153 void QueryEditorDialog::initUI()
155 QPushButton
* okButton
= new QPushButton(tr("OK"));
156 QPushButton
* cancelButton
= new QPushButton(tr("Cancel"));
158 connect(okButton
, SIGNAL(clicked()), this, SLOT(accept()));
159 connect(cancelButton
, SIGNAL(clicked()), this, SLOT(reject()));
161 QLabel
* viewLabel
= new QLabel(tr("View name: "));
162 viewNameEdit
= new QLineEdit();
164 QLabel
* tableLabel
= new QLabel(tr("Table to Query: "));
165 tableList
= new QComboBox();
167 connect(tableList
, SIGNAL(activated(const QString
&)), this, SLOT(tableSelected(const QString
&)));
169 andButton
= new QRadioButton(tr("Match all of the following terms"));
170 orButton
= new QRadioButton(tr("Match any of the following terms"));
172 andButton
->setChecked(true);
174 QPushButton
* moreButton
= new QPushButton(tr("More"));
175 lessButton
= new QPushButton(tr("Less"));
177 lessButton
->setEnabled(false);
179 connect(moreButton
, SIGNAL(clicked()), this, SLOT(moreTerms()));
180 connect(lessButton
, SIGNAL(clicked()), this, SLOT(lessTerms()));
185 checkLayout
= new QGridLayout();
186 termsLayout
= new QVBoxLayout();
188 QGridLayout
* topLayout
= new QGridLayout();
190 if(curMode
== CreateView
)
192 topLayout
->addWidget(viewLabel
, 0, 0);
193 topLayout
->addWidget(viewNameEdit
, 0, 1);
195 topLayout
->addWidget(tableLabel
, 1, 0);
196 topLayout
->addWidget(tableList
, 1, 1);
198 QGroupBox
* fieldsBox
= new QGroupBox(tr("Fields"));
199 fieldsBox
->setLayout(checkLayout
);
201 QHBoxLayout
* andOrLayout
= new QHBoxLayout();
202 andOrLayout
->addWidget(andButton
);
203 andOrLayout
->addWidget(orButton
);
205 QHBoxLayout
* termButtonsLayout
= new QHBoxLayout();
206 termButtonsLayout
->addStretch(1);
207 termButtonsLayout
->addWidget(moreButton
);
208 termButtonsLayout
->addWidget(lessButton
);
210 QVBoxLayout
* outerTermsLayout
= new QVBoxLayout();
211 outerTermsLayout
->addLayout(andOrLayout
);
212 outerTermsLayout
->addLayout(termsLayout
, 1);
213 outerTermsLayout
->addLayout(termButtonsLayout
);
215 QGroupBox
* termsBox
= new QGroupBox(tr("Terms"));
216 termsBox
->setLayout(outerTermsLayout
);
218 QHBoxLayout
* buttonLayout
= new QHBoxLayout();
219 buttonLayout
->addStretch(1);
220 buttonLayout
->addWidget(okButton
);
221 buttonLayout
->addWidget(cancelButton
);
223 QVBoxLayout
* mainLayout
= new QVBoxLayout();
224 mainLayout
->addLayout(topLayout
);
225 mainLayout
->addWidget(fieldsBox
);
226 mainLayout
->addWidget(termsBox
);
227 mainLayout
->addStretch(1);
228 mainLayout
->addLayout(buttonLayout
);
230 setLayout(mainLayout
);
233 void QueryEditorDialog::tableSelected(const QString
& table
)
235 FieldList fields
= db
->tableFields(table
);
241 while((child
= checkLayout
->takeAt(0)) != 0)
243 delete child
->widget();
246 repaint(0, 0, height(), width());
249 for(int i
= 0; i
< fields
.size(); i
++)
251 DatabaseTableField field
= fields
[i
];
252 QCheckBox
* check
= new QCheckBox(field
.name
);
254 check
->setCheckState(Qt::Checked
);
255 checkLayout
->addWidget(check
, row
, i
% 2);
262 void QueryEditorDialog::moreTerms()
264 TermEditor
* term
= new TermEditor(db
->tableFields(curTable
));
266 termsLayout
->addWidget(term
);
267 lessButton
->setEnabled(true);
270 void QueryEditorDialog::lessTerms()
272 QLayoutItem
* child
= termsLayout
->takeAt(termsLayout
->count() - 1);
275 delete child
->widget();
279 if(termsLayout
->count() == 0)
280 lessButton
->setEnabled(false);