1 /***************************************************************************
2 	File                 : ExtractDataDialog.cpp
3     Project              : QtiPlot
4     --------------------------------------------------------------------
5 	Copyright            : (C) 2010 by Ion Vasilief
6 	Email (use @ for *)  : ion_vasilief*yahoo.fr
7 	Description          : Extract data values dialog
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *  This program is free software; you can redistribute it and/or modify   *
14  *  it under the terms of the GNU General Public License as published by   *
15  *  the Free Software Foundation; either version 2 of the License, or      *
16  *  (at your option) any later version.                                    *
17  *                                                                         *
18  *  This program is distributed in the hope that it will be useful,        *
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
21  *  GNU General Public License for more details.                           *
22  *                                                                         *
23  *   You should have received a copy of the GNU General Public License     *
24  *   along with this program; if not, write to the Free Software           *
25  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
26  *   Boston, MA  02110-1301  USA                                           *
27  *                                                                         *
28  ***************************************************************************/
29 #include "ExtractDataDialog.h"
30 #include "Table.h"
31 #include <ScriptEdit.h>
32 #include <ApplicationWindow.h>
33 #include "muParserScripting.h"
34 
35 #include <QTableWidget>
36 #include <QTableWidgetSelectionRange>
37 #include <QList>
38 #include <QLayout>
39 #include <QSpinBox>
40 #include <QGroupBox>
41 #include <QPushButton>
42 #include <QLabel>
43 #include <QComboBox>
44 #include <QTextEdit>
45 #include <QTextCursor>
46 #include <QCompleter>
47 #include <QLineEdit>
48 
ExtractDataDialog(ScriptingEnv * env,QWidget * parent,Qt::WFlags fl)49 ExtractDataDialog::ExtractDataDialog( ScriptingEnv *env, QWidget* parent, Qt::WFlags fl )
50     : QDialog( parent, fl ), scripted(env)
51 {
52 	setName( "ExtractDataDialog" );
53 	setWindowTitle( tr( "QtiPlot") + " - " + tr("Extract Data"));
54 	setSizeGripEnabled(true);
55     setAttribute(Qt::WA_DeleteOnClose);
56 
57 	QHBoxLayout *hbox1 = new QHBoxLayout();
58 	hbox1->addWidget(new QLabel(tr("For row (i)")));
59 	start = new QSpinBox();
60 	start->setMinValue(1);
61     start->setMaxValue(INT_MAX);
62 	hbox1->addWidget(start);
63 
64 	hbox1->addWidget(new QLabel(tr("to")));
65 
66 	end = new QSpinBox();
67 	end->setMinValue(1);
68     end->setMaxValue(INT_MAX);
69 	hbox1->addWidget(end);
70 
71 	QGridLayout *gl1 = new QGridLayout();
72 	functions = new QComboBox();
73 	functions->addItems(muParserScripting::functionsList(true));
74 	gl1->addWidget(functions, 0, 0);
75 	btnAddFunction = new QPushButton(tr( "Add &function" ));
76 	gl1->addWidget(btnAddFunction, 0, 1);
77 	boxColumn = new QComboBox();
78 	gl1->addWidget(boxColumn, 1, 0);
79 	btnAddCol = new QPushButton(tr( "Add co&lumn" ));
80 	gl1->addWidget(btnAddCol, 1, 1);
81 	boxOperators = new QComboBox();
82 	boxOperators->addItems(QStringList() << ">" << ">=" << "<" << "<=" << "==" << "!=");
83 	gl1->addWidget(boxOperators, 2, 0);
84 	btnAddOp = new QPushButton(tr( "Add &operator" ));
85 	gl1->addWidget(btnAddOp, 2, 1);
86 
87 	QGroupBox *gb = new QGroupBox();
88 	QVBoxLayout *vbox1 = new QVBoxLayout();
89 	vbox1->addLayout(hbox1);
90 	vbox1->addLayout(gl1);
91 	gb->setLayout(vbox1);
92 	gb->setSizePolicy(QSizePolicy (QSizePolicy::Preferred, QSizePolicy::Preferred));
93 
94 	explain = new QTextEdit();
95 	explain->setReadOnly (true);
96 	explain->setSizePolicy(QSizePolicy (QSizePolicy::Preferred, QSizePolicy::Preferred));
97 	QPalette palette = explain->palette();
98 	palette.setColor(QPalette::Active, QPalette::Base, Qt::lightGray);
99 	explain->setPalette(palette);
100 
101 	QHBoxLayout *hbox2 = new QHBoxLayout();
102 	hbox2->addWidget(explain);
103 	hbox2->addWidget(gb);
104 
105 	commands = new ScriptEdit(scriptEnv);
106 	commands->setTabStopWidth(((ApplicationWindow *)parent)->d_notes_tab_length);
107     commands->setFont(((ApplicationWindow *)parent)->d_notes_font);
108 
109 	QVBoxLayout *vbox2 = new QVBoxLayout();
110 	btnApply = new QPushButton(tr( "&Apply" ));
111 	vbox2->addWidget(btnApply);
112 	buttonClearFormulas = new QPushButton(tr("Clea&r" ));
113 	vbox2->addWidget(buttonClearFormulas);
114 	btnCancel = new QPushButton(tr( "&Close" ));
115 	vbox2->addWidget(btnCancel);
116 	vbox2->addStretch();
117 
118 	QHBoxLayout *hbox4 = new QHBoxLayout();
119 	hbox4->addWidget(commands);
120 	hbox4->addLayout(vbox2);
121 
122 	QHBoxLayout *hbox5 = new QHBoxLayout();
123 	QLabel *l = new QLabel(tr("&Put into table"));
124 	hbox5->addWidget(l);
125 
126 	destNameBox = new QLineEdit;
127 	destNameBox->setText(tr("Table"));
128 	l->setBuddy(destNameBox);
129 	hbox5->addWidget(destNameBox);
130 
131 	QVBoxLayout* vbox3 = new QVBoxLayout();
132 	vbox3->addLayout(hbox2);
133 	vbox3->addLayout(hbox5);
134 
135 	QLabel *l2 = new QLabel(tr("Cond&ition:"));
136 	l2->setBuddy(commands);
137 	vbox3->addWidget(l2);
138 	vbox3->addLayout(hbox4);
139 
140 	setLayout(vbox3);
141 	setFocusProxy (commands);
142 	commands->setFocus();
143 
144 	if (functions->count() > 0)
145 		insertExplain(0);
146 
147 	connect(btnAddFunction, SIGNAL(clicked()),this, SLOT(insertFunction()));
148 	connect(btnAddCol, SIGNAL(clicked()),this, SLOT(insertCol()));
149 	connect(btnAddOp, SIGNAL(clicked()),this, SLOT(insertOp()));
150 	connect(btnApply, SIGNAL(clicked()),this, SLOT(apply()));
151 	connect(btnCancel, SIGNAL(clicked()),this, SLOT(close()));
152 	connect(functions, SIGNAL(activated(int)),this, SLOT(insertExplain(int)));
153 	connect(buttonClearFormulas, SIGNAL(clicked()), this, SLOT(clearFormulas()));
154 }
155 
sizeHint() const156 QSize ExtractDataDialog::sizeHint() const
157 {
158 	return QSize( 400, 190 );
159 }
160 
apply()161 void ExtractDataDialog::apply()
162 {
163 	Table *t = table->extractData(destNameBox->text(), commands->text(), start->value() - 1, end->value() - 1);
164 	if (!t)
165 		return;
166 
167 	t->show();
168 }
169 
insertExplain(int index)170 void ExtractDataDialog::insertExplain(int index)
171 {
172 
173 	explain->setText(muParserScripting::explainFunction(functions->text(index)));
174 }
175 
insertFunction()176 void ExtractDataDialog::insertFunction()
177 {
178 	commands->insertFunction(functions->currentText());
179 }
180 
insertCol()181 void ExtractDataDialog::insertCol()
182 {
183 	commands->insert(boxColumn->currentText());
184 }
185 
insertOp()186 void ExtractDataDialog::insertOp()
187 {
188 	commands->insert(boxOperators->currentText());
189 }
190 
setTable(Table * w)191 void ExtractDataDialog::setTable(Table* w)
192 {
193 	table = w;
194 	QStringList colNames = w->colNames();
195 	int cols = w->numCols();
196 	for (int i=0; i<cols; i++)
197 		boxColumn->insertItem("col(\""+colNames[i]+"\")", i);
198 
199 	int s = w->table()->currentSelection();
200 	if (s >= 0) {
201 		Q3TableSelection sel = w->table()->selection(s);
202 		w->setSelectedCol(sel.leftCol());
203 
204 		start->setValue(sel.topRow() + 1);
205 		end->setValue(sel.bottomRow() + 1);
206 	} else {
207 		start->setValue(1);
208 		end->setValue(w->numRows());
209 	}
210 
211 	commands->setContext(w);
212 }
213 
setCompleter(QCompleter * completer)214 void ExtractDataDialog::setCompleter(QCompleter *completer)
215 {
216     if (!completer)
217         return;
218 
219     commands->setCompleter(completer);
220 }
221 
clearFormulas()222 void ExtractDataDialog::clearFormulas()
223 {
224 	if (!table)
225 		return;
226 
227 	table->clearCommands();
228 	commands->clear();
229 }
230