1 /***************************************************************************
2 * Copyright (C) 2008 by Alexey Balakin *
3 * mathgl.abalakin@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <QLayout>
21 #include <QTableWidget>
22 #include <QToolBar>
23 #include <QInputDialog>
24 #include <QMessageBox>
25 #include <mgl2/mgl.h>
26 //-----------------------------------------------------------------------------
27 #include "mem_pnl.h"
28 #include "info_dlg.h"
29 #undef sprintf // fix libintl bug of defining sprintf
30 //-----------------------------------------------------------------------------
31 #include "xpm/table.xpm"
32 #include "xpm/preview.xpm"
33 //-----------------------------------------------------------------------------
34 extern bool mglAutoSave;
35 extern mglParse parser;
36 QWidget *newDataWnd(InfoDialog *inf, QWidget *wnd, mglDataA *v);
37 void refreshData(QWidget *w);
38 //-----------------------------------------------------------------------------
createMemPanel(QWidget * p)39 QWidget *createMemPanel(QWidget *p) // NOTE: parent should be MainWindow
40 {
41 MemPanel *m = new MemPanel(p);
42 m->wnd = p; return m;
43 }
44 //-----------------------------------------------------------------------------
refreshMemPanel(QWidget * p)45 void refreshMemPanel(QWidget *p)
46 {
47 MemPanel *m = dynamic_cast<MemPanel *>(p);
48 if(m) m->refresh();
49 }
50 //-----------------------------------------------------------------------------
MemPanel(QWidget * parent)51 MemPanel::MemPanel(QWidget *parent) : QWidget(parent)
52 {
53 infoDlg = new InfoDialog(this);
54 infoDlg->setModal(true); infoDlg->allowRefresh=false;
55
56 QToolBar *t = new QToolBar(this); t->setMovable(false);
57 QVBoxLayout *v = new QVBoxLayout(this); v->addWidget(t);
58 t->addAction(QPixmap(":/png/document-new.png"), _("Create new data array"), this, SLOT(newTable()));
59 t->addAction(QPixmap(table_xpm), _("Edit selected data array"), this, SLOT(editData()));
60 t->addAction(QPixmap(":/png/edit-delete.png"), _("Delete selected data array"), this, SLOT(delData()));
61 t->addAction(QPixmap(preview_xpm), _("Properties of selected data array"), this, SLOT(infoData()));
62 t->addAction(QPixmap(":/png/view-refresh.png"), _("Update list of data arrays"), this, SLOT(refresh()));
63 t->addSeparator();
64 t->addAction(QPixmap(":/png/edit-clear.png"), _("Delete ALL data arrays"), this, SLOT(delAllData()));
65
66 colSort = 0;
67 tab = new QTableWidget(this); tab->setColumnCount(3); v->addWidget(tab);
68 QStringList sl; sl<<_("Name")<<_("Sizes")<<_("Memory");
69 tab->setHorizontalHeaderLabels(sl);
70 connect(tab, SIGNAL(cellClicked(int,int)), this, SLOT(tableClicked(int,int)));
71 connect(tab, SIGNAL(cellDoubleClicked(int,int)), this, SLOT(tableDClicked(int,int)));
72
73 setWindowTitle(_("Memory"));
74 }
75 //-----------------------------------------------------------------------------
tableClicked(int,int col)76 void MemPanel::tableClicked(int, int col)
77 { colSort = col; tab->sortItems(col); }
78 //-----------------------------------------------------------------------------
tableDClicked(int row,int)79 void MemPanel::tableDClicked(int row, int) { editData(row); }
80 //-----------------------------------------------------------------------------
newTable()81 void MemPanel::newTable()
82 {
83 bool ok;
84 QString name = QInputDialog::getText(this, _("UDAV - New variable"),
85 _("Enter name for new variable"), QLineEdit::Normal, "", &ok);
86 if(!ok || name.isEmpty()) return;
87 mglDataA *v = parser.AddVar(name.toLocal8Bit().constData());
88 QWidget *t;
89 if(v->o) t = (QWidget *)v->o;
90 else t = newDataWnd(infoDlg,wnd,v);
91 t->showMaximized(); t->activateWindow();
92 refresh();
93 }
94 //-----------------------------------------------------------------------------
editData(int n)95 void MemPanel::editData(int n)
96 {
97 if(tab->rowCount()<1) return;
98 if(n<0) n = tab->currentRow();
99 if(n<0) n = 0;
100 mglDataA *v = parser.FindVar(tab->item(n,0)->text().toLocal8Bit().constData());
101 if(!v) return;
102 QWidget *t;
103 if(v->o) t = (QWidget *)v->o;
104 else t = newDataWnd(infoDlg,wnd,v);
105 t->showMaximized(); t->activateWindow();
106 }
107 //-----------------------------------------------------------------------------
delData()108 void MemPanel::delData()
109 {
110 if(tab->rowCount()<1) return;
111 int n = tab->currentRow();
112 if(n<0) n = 0;
113 mglDataA *v = parser.FindVar(tab->item(n,0)->text().toLocal8Bit().constData());
114 if(v && v->o) ((QWidget *)v->o)->close();
115 parser.DeleteVar(tab->item(n,0)->text().toLocal8Bit().constData());
116 refresh();
117 }
118 //-----------------------------------------------------------------------------
delAllData()119 void MemPanel::delAllData()
120 {
121 if(QMessageBox::information(this, _("UDAV - delete all data"),
122 _("Do you want to delete all data?"), QMessageBox::No,
123 QMessageBox::Yes)!=QMessageBox::Yes) return;
124 parser.DeleteAll(); refresh();
125 }
126 //-----------------------------------------------------------------------------
infoData()127 void MemPanel::infoData()
128 {
129 if(tab->rowCount()<1) return;
130 int n = tab->currentRow();
131 if(n<0) n = 0;
132 mglDataA *v = parser.FindVar(tab->item(n,0)->text().toLocal8Bit().constData());
133 if(!v) return;
134 infoDlg->setVar(v);
135 QString s = QString::fromWCharArray(v->Name());
136 infoDlg->setWindowTitle(s + _(" - UDAV preview"));
137 infoDlg->refresh();
138 infoDlg->show();
139 }
140 //-----------------------------------------------------------------------------
refresh()141 void MemPanel::refresh()
142 {
143 long n = parser.GetNumVar(), m=0;
144 for(long i=0;i<n;i++) if(parser.GetVar(i)) m++;
145 tab->setRowCount(m);
146 QString s;
147 QTableWidgetItem *it;
148 Qt::ItemFlags flags=Qt::ItemIsSelectable|Qt::ItemIsEnabled;
149 for(long i=m=0;i<n;i++)
150 {
151 mglDataA *v = parser.GetVar(i);
152 if(!v) continue;
153 s = QString::fromWCharArray(v->Name());
154 it = new QTableWidgetItem(s);
155 tab->setItem(m,0,it); it->setFlags(flags);
156 s.asprintf("%ld * %ld * %ld", v->GetNx(), v->GetNy(), v->GetNz());
157 it = new QTableWidgetItem(s);
158 tab->setItem(m,1,it); it->setFlags(flags);
159 it->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
160 long sv = 0;
161 if(dynamic_cast<mglData*>(v)) sv = v->GetNN()*sizeof(mreal)+sizeof(mglData);
162 else if(dynamic_cast<mglDataC*>(v)) sv = v->GetNN()*sizeof(dual)+sizeof(mglDataC);
163 else if(dynamic_cast<mglDataV*>(v)) sv = sizeof(mglDataV);
164 else if(dynamic_cast<mglDataW*>(v)) sv = sizeof(mglDataW);
165 else if(dynamic_cast<mglDataF*>(v)) sv = sizeof(mglDataF);
166 else if(dynamic_cast<mglDataR*>(v)) sv = sizeof(mglDataR);
167 else if(dynamic_cast<mglDataT*>(v)) sv = sizeof(mglDataT);
168 if(sv==0) s = _("unknown");
169 #if MGL_SIZEOF_LONG>4
170 // else if((sv>>80L)>0) s.asprintf("%ld Yb",sv>>80L);
171 // else if((sv>>70L)>0) s.asprintf("%ld Zb",sv>>70L);
172 else if((sv>>60L)>0) s.asprintf("%ld Eb",sv>>60L);
173 else if((sv>>50L)>0) s.asprintf("%ld Pb",sv>>50L);
174 else if((sv>>40L)>0) s.asprintf("%ld Tb",sv>>40L);
175 #endif
176 else if((sv>>30L)>0) s.asprintf("%ld Gb",sv>>30L);
177 else if((sv>>20L)>0) s.asprintf("%ld Mb",sv>>20L);
178 else if((sv>>10L)>0) s.asprintf("%ld Kb",sv>>10L);
179 else s.asprintf("%ld b",sv);
180 it = new QTableWidgetItem(s);
181 tab->setItem(m,2,it); it->setFlags(flags);
182 it->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
183 if(v->o) refreshData((QWidget *)v->o);
184 m++;
185 }
186 tab->sortItems(colSort);
187 }
188 //-----------------------------------------------------------------------------
189