Initial commit to git repository
[liteman.git] / src / queryeditordialog.cpp
blob7cf237e9f52eacb026b94da9da0d0bbdd43db36b
1 /*
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>
23 #include <QLineEdit>
24 #include <QComboBox>
25 #include <QGroupBox>
26 #include <QCheckBox>
27 #include <QLabel>
29 #include <QGridLayout>
30 #include <QHBoxLayout>
31 #include <QVBoxLayout>
33 #include "queryeditordialog.h"
34 #include "termeditor.h"
35 #include "database.h"
37 /*!
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)
44 db = dbase;
45 curMode = mode;
47 initUI();
49 QStringList tables = db->getTables();
50 tableList->addItems(tables);
52 // If a database has at least one table. auto select it
53 if(tables.size() > 0)
54 tableSelected(tables[0]);
56 if(mode == CreateView)
57 setWindowTitle(tr("Create View"));
58 else
59 setWindowTitle(tr("Build Query"));
62 QueryEditorDialog::~QueryEditorDialog()
66 /*!
67 * @brief generates a valid SQL statement using the values in the dialog
69 QString QueryEditorDialog::statement()
71 QString logicWord;
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();
79 if(!widget)
80 continue;
82 QCheckBox * check = qobject_cast<QCheckBox *>(widget);
83 if(check)
84 if(check->checkState() == Qt::Checked)
85 sql += (check->text() + ", ");
87 sql = sql.remove(sql.size() - 2, 2); // cut the extra ", "
89 // Add table name
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";
98 sql += " WHERE ";
100 for(int i = 0; i < termsLayout->count(); i++)
102 QWidget * widget = termsLayout->itemAt(i)->widget();
104 if(!widget)
105 continue;
107 TermEditor * term = qobject_cast<TermEditor *>(widget);
108 if(term)
110 sql += term->selectedField();
112 switch(term->selectedRelation())
114 case 0: // Contains
115 sql += (" LIKE '%" + term->selectedValue() + "%'");
116 break;
118 case 1: // Doesn't contain
119 sql += (" NOT LIKE '%" + term->selectedValue() + "%'");
120 break;
122 case 2: // Equals
123 sql += (" = '" + term->selectedValue() + "'");
124 break;
126 case 3: // Not equals
127 sql += (" <> '" + term->selectedValue() + "'");
128 break;
130 case 4: // Bigger than
131 sql += (" > '" + term->selectedValue() + "'");
132 break;
134 case 5: // Smaller than
135 sql += (" < '" + term->selectedValue() + "'");
136 break;
139 sql += (" " + logicWord + " ");
141 sql = sql.remove(sql.size() - (logicWord.size() + 2), logicWord.size() + 2); // cut the extra " AND " or " OR "
143 sql += ";";
145 return sql;
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()));
183 // Layout
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);
237 curTable = table;
239 // Clear checkLayout
240 QLayoutItem * child;
241 while((child = checkLayout->takeAt(0)) != 0)
243 delete child->widget();
244 delete child;
246 repaint(0, 0, height(), width());
248 int row = 0;
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);
257 if((i + 1) % 2 == 0)
258 row++;
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);
273 if(child)
275 delete child->widget();
276 delete child;
279 if(termsLayout->count() == 0)
280 lessButton->setEnabled(false);