##// END OF EJS Templates
Fusion
Jeandet Alexis -
r56:d8f52eebbac3 merge default
parent child
Show More
@@ -0,0 +1,175
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the SocExplorer Software
3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
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 Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Alexis Jeandet
20 -- Mail : alexis.jeandet@member.fsf.org
21 ----------------------------------------------------------------------------*/
22 #include "binaryfile.h"
23 #include "srecfile.h"
24
25 binaryFile::binaryFile()
26 {
27 }
28
29 binaryFile::binaryFile(const QString &File)
30 {
31 openFile(File);
32 }
33
34 binaryFile::binaryFile(const QStringList &Files)
35 {
36 openFiles(Files);
37 }
38
39 binaryFile::~binaryFile()
40 {
41
42 }
43
44 bool binaryFile::openFile(const QString &File)
45 {
46 return openFiles(QStringList()<<File);
47 }
48
49 bool binaryFile::openFiles(const QStringList &Files)
50 {
51 for(int i=0;i<Files.count();i++)
52 {
53 this->p_files.append(new QFile(Files.at(i)));
54 this->p_files.at(i)->open(QIODevice::ReadOnly);
55 loadFile(this->p_files.at(i));
56 }
57 return true;
58 }
59
60 bool binaryFile::isopened()
61 {
62 bool opened = true;
63 for(int i=0;i<this->p_files.count();i++)
64 {
65 opened &= p_files.at(i)->isOpen();
66 }
67 return opened;
68 }
69
70 int binaryFile::closeFile()
71 {
72 for(int i=0;i<p_files.count();i++)
73 {
74 delete p_files.at(i);
75 for(int j=0;j<p_fragments.count();j++)
76 {
77 if(p_fragments.at(j)->header == p_files.at(i)->fileName())
78 {
79 codeFragment* fragment = p_fragments.at(j);
80 p_fragments.removeAt(j);
81 delete fragment;
82 }
83 }
84 }
85 p_files.clear();
86 p_fileName.clear();
87 return 0;
88 }
89
90 QList<codeFragment *> binaryFile::getFragments()
91 {
92 return p_fragments;
93 }
94
95 int binaryFile::getFragmentsCount()
96 {
97 return p_fragments.count();
98 }
99
100 int binaryFile::getFragmentAddress(int index)
101 {
102 if((index>=0)&&(index<p_fragments.count()))
103 return p_fragments.at(index)->address;
104 return 0;
105 }
106
107 int binaryFile::getFragmentSize(int index)
108 {
109 if((index>=0)&&(index<p_fragments.count()))
110 return p_fragments.at(index)->size;
111 return 0;
112 }
113
114 QString binaryFile::getFragmentHeader(int index)
115 {
116 if((index>=0)&&(index<p_fragments.count()))
117 return p_fragments.at(index)->header;
118 return "";
119 }
120
121 codeFragment *binaryFile::getFragment(int index)
122 {
123 if((index>=0)&&(index<p_fragments.count()))
124 return p_fragments.at(index);
125 return NULL;
126 }
127
128 bool binaryFile::getFragmentData(int index, char **buffer)
129 {
130 if((index>=0)&&(index<p_fragments.count()))
131 {
132 *buffer = p_fragments.at(index)->data;
133 return true;
134 }
135 return false;
136 }
137
138 bool binaryFile::toSrec(const QString &fileName)
139 {
140 srecFile::toSrec(p_fragments,fileName);
141 }
142
143 bool binaryFile::toBinary(const QString &fileName)
144 {
145 toBinary(p_fragments,fileName);
146 }
147
148 bool binaryFile::toBinary(QList<codeFragment *> fragments, const QString &File)
149 {
150 QFile file(File);
151 file.open(QIODevice::WriteOnly);
152 if(file.isOpen())
153 {
154 for(int i=0;i<fragments.count();i++)
155 {
156 file.write(fragments.at(i)->data,fragments.at(i)->size);
157 }
158 return true;
159 }
160 return false;
161 }
162
163 void binaryFile::loadFile(QFile *file)
164 {
165 if (file->isOpen())
166 {
167 codeFragment* fragment = new codeFragment();
168 fragment->header = file->fileName();
169 fragment->address = 0;
170 fragment->size = file->size();
171 fragment->data = (char*)malloc(file->size());
172 file->read(fragment->data,file->size());
173 p_fragments.append(fragment);
174 }
175 }
@@ -0,0 +1,64
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the SocExplorer Software
3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
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 Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Alexis Jeandet
20 -- Mail : alexis.jeandet@member.fsf.org
21 ----------------------------------------------------------------------------*/
22 #ifndef BINARYFILE_H
23 #define BINARYFILE_H
24 #include "abstractbinfile.h"
25 #include <QObject>
26 #include <QString>
27 #include <QList>
28 #include <QStringList>
29 #include <QFile>
30
31 class binaryFile : public abstractBinFile
32 {
33 Q_OBJECT
34 public:
35 explicit binaryFile();
36 binaryFile(const QString& File);
37 binaryFile(const QStringList& Files);
38 ~binaryFile();
39 bool openFile(const QString& File);
40 bool openFiles(const QStringList& Files);
41 bool isopened();
42 int closeFile();
43 QList<codeFragment*> getFragments();
44 int getFragmentsCount();
45 int getFragmentAddress(int index);
46 int getFragmentSize(int index);
47 QString getFragmentHeader(int index);
48 codeFragment* getFragment(int index);
49 bool getFragmentData(int index, char **buffer);
50 bool toSrec(const QString& fileName);
51 bool toBinary(const QString& fileName);
52 static bool toBinary(QList<codeFragment*> fragments,const QString& File);
53 signals:
54
55 public slots:
56
57 private:
58 void loadFile(QFile *file);
59 QStringList p_fileNames;
60 QList<QFile*>p_files;
61 QList<codeFragment*> p_fragments;
62 };
63
64 #endif // BINARYFILE_H
@@ -0,0 +1,150
1 #include "binaryfilewidget.h"
2 #include "ui_binaryfilewidget.h"
3 #include "qtablewidgetintitem.h"
4 #include <QtWidgets/QTableWidget>
5 #include <QtWidgets/QFileDialog>
6 #include "srecfile.h"
7
8 binaryFileWidget::binaryFileWidget(QWidget *parent) :
9 abstractBinFileWidget(parent),
10 ui(new Ui::binaryFileWidget)
11 {
12 ui->setupUi(this);
13 connect(this->ui->fragmentList,SIGNAL(cellActivated(int,int)),this,SLOT(fragmentCellActivated(int,int)));
14 connect(this->ui->fragmentList,SIGNAL(cellChanged(int,int)),this,SLOT(fragmentCellChanged(int,int)));
15 exportToSREC_action = new QAction(tr("Export to SREC"),this);
16 exportToBIN_action = new QAction(tr("Export to Binary"),this);
17 this->ui->fragmentList->addAction(exportToBIN_action);
18 this->ui->fragmentList->addAction(exportToSREC_action);
19 connect(this->exportToBIN_action,SIGNAL(triggered()),this,SLOT(exportToBIN()));
20 connect(this->exportToSREC_action,SIGNAL(triggered()),this,SLOT(exportToSREC()));
21 }
22
23 binaryFileWidget::~binaryFileWidget()
24 {
25 delete ui;
26 }
27
28 void binaryFileWidget::setFile(abstractBinFile *file)
29 {
30 this->p_binfile = (binaryFile*)file;
31 if(p_binfile->isopened())
32 {
33 reloadFile();
34 }
35 }
36
37 void binaryFileWidget::reloadFile()
38 {
39 this->ui->fragmentList->clear();
40 this->ui->fragmentList->setRowCount(p_binfile->getFragmentsCount());
41 this->ui->fragmentList->setHorizontalHeaderLabels(QStringList()<<"File"<<"Size"<<"Address");
42 for(int i=0;i<p_binfile->getFragmentsCount();i++)
43 {
44 QTableWidgetItem *newItem = new QTableWidgetItem(p_binfile->getFragmentHeader(i));
45 newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);
46 this->ui->fragmentList->setItem(i, 0, newItem);
47
48 newItem = (QTableWidgetItem*)new QTableWidgetIntItem(QString("%1").arg(p_binfile->getFragmentSize(i)),DecimalItem);
49 newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);
50 this->ui->fragmentList->setItem(i, 1, newItem);
51
52 newItem = (QTableWidgetItem*)new QTableWidgetIntItem(QString("0x%1").arg(p_binfile->getFragmentAddress(i),8,16).replace(" ","0"),HexaDecimalItem);
53 // newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);
54 this->ui->fragmentList->setItem(i, 2, newItem);
55
56 }
57 this->ui->fragmentList->resizeColumnsToContents();
58 }
59
60 void binaryFileWidget::fragmentCellActivated(int row, int column)
61 {
62 Q_UNUSED(column)
63 char* buff=NULL;
64 int index = this->ui->fragmentList->item(row,0)->text().toInt();
65 if(index!=-1)
66 {
67 this->p_binfile->getFragmentData(index,&buff);
68 this->ui->hexViewer->setData(QByteArray(buff,this->p_binfile->getFragmentSize(index)));
69 this->ui->hexViewer->setAddressOffset(this->p_binfile->getFragmentAddress(index));
70 }
71 }
72
73 void binaryFileWidget::fragmentCellChanged(int row, int column)
74 {
75 if(column==2)
76 {
77 QString newAddressStr = this->ui->fragmentList->item(row,column)->text();
78 int newAddress = 0;
79 newAddressStr.remove(" ");
80 if(newAddressStr.at(0)=='0' && newAddressStr.at(1)=='x')
81 {
82 newAddress = newAddressStr.remove("0x").toUInt(0,16);
83 }
84 else
85 {
86 newAddress = newAddressStr.toUInt();
87 }
88 this->p_binfile->getFragments().at(row)->address = newAddress;
89 }
90 }
91
92 void binaryFileWidget::exportToSREC()
93 {
94 QList<codeFragment *> SelectedFragmentsList=getSelectedFragments();
95 if(SelectedFragmentsList.count()>0)
96 {
97 QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
98 NULL,
99 tr("SREC Files (*.srec)"));
100 if(!fileName.isEmpty())
101 {
102 srecFile::toSrec(SelectedFragmentsList,fileName);
103 }
104 }
105 }
106
107 void binaryFileWidget::exportToBIN()
108 {
109 QList<codeFragment *> SelectedFragmentsList=getSelectedFragments();
110 if(SelectedFragmentsList.count()>0)
111 {
112 QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
113 NULL,
114 tr("Binary Files (*.bin)"));
115 if(!fileName.isEmpty())
116 {
117 binaryFile::toBinary(SelectedFragmentsList,fileName);
118 }
119 }
120 }
121
122 QStringList binaryFileWidget::getSelectedFilesNames()
123 {
124 QStringList SelectedFilesList;
125 QList<QTableWidgetItem*> items = this->ui->fragmentList->selectedItems();
126 for(int i=0;i<items.count();i++)
127 {
128 QString file = p_binfile->getFragmentHeader(items.at(i)->row());
129 if(!SelectedFilesList.contains(file))
130 {
131 SelectedFilesList.append(file);
132 }
133 }
134 return SelectedFilesList;
135 }
136
137 QList<codeFragment *> binaryFileWidget::getSelectedFragments()
138 {
139 QList<codeFragment *> SelectedFragmentsList;
140 QList<QTableWidgetItem*> items = this->ui->fragmentList->selectedItems();
141 for(int i=0;i<items.count();i++)
142 {
143 codeFragment * fragment = p_binfile->getFragment(items.at(i)->row());
144 if(!SelectedFragmentsList.contains(fragment))
145 {
146 SelectedFragmentsList.append(fragment);
147 }
148 }
149 return SelectedFragmentsList;
150 }
@@ -0,0 +1,60
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the SocExplorer Software
3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
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 Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Alexis Jeandet
20 -- Mail : alexis.jeandet@member.fsf.org
21 ----------------------------------------------------------------------------*/
22 #ifndef BINARYFILEWIDGET_H
23 #define BINARYFILEWIDGET_H
24
25 #include <QWidget>
26 #include "binaryfile.h"
27 #include <QAction>
28
29 namespace Ui {
30 class binaryFileWidget;
31 }
32
33 class binaryFileWidget : public abstractBinFileWidget
34 {
35 Q_OBJECT
36
37 public:
38 explicit binaryFileWidget(QWidget *parent = 0);
39 ~binaryFileWidget();
40
41 public slots:
42 void setFile(abstractBinFile* file);
43 void reloadFile();
44
45 private slots:
46 void fragmentCellActivated(int row, int column);
47 void fragmentCellChanged(int row, int column);
48 void exportToSREC();
49 void exportToBIN();
50
51 private:
52 QStringList getSelectedFilesNames();
53 QList<codeFragment *> getSelectedFragments();
54 Ui::binaryFileWidget *ui;
55 binaryFile* p_binfile;
56 QAction* exportToSREC_action;
57 QAction* exportToBIN_action;
58 };
59
60 #endif // BINARYFILEWIDGET_H
@@ -0,0 +1,64
1 <?xml version="1.0" encoding="UTF-8"?>
2 <ui version="4.0">
3 <class>binaryFileWidget</class>
4 <widget class="QWidget" name="binaryFileWidget">
5 <property name="geometry">
6 <rect>
7 <x>0</x>
8 <y>0</y>
9 <width>637</width>
10 <height>342</height>
11 </rect>
12 </property>
13 <property name="windowTitle">
14 <string>Form</string>
15 </property>
16 <layout class="QGridLayout" name="gridLayout">
17 <item row="0" column="0">
18 <widget class="QSplitter" name="splitter">
19 <property name="orientation">
20 <enum>Qt::Horizontal</enum>
21 </property>
22 <widget class="QHexEdit" name="hexViewer" native="true">
23 <property name="minimumSize">
24 <size>
25 <width>200</width>
26 <height>0</height>
27 </size>
28 </property>
29 </widget>
30 <widget class="QTableWidget" name="fragmentList">
31 <property name="contextMenuPolicy">
32 <enum>Qt::ActionsContextMenu</enum>
33 </property>
34 <column>
35 <property name="text">
36 <string>File</string>
37 </property>
38 </column>
39 <column>
40 <property name="text">
41 <string>Size</string>
42 </property>
43 </column>
44 <column>
45 <property name="text">
46 <string>Address</string>
47 </property>
48 </column>
49 </widget>
50 </widget>
51 </item>
52 </layout>
53 </widget>
54 <customwidgets>
55 <customwidget>
56 <class>QHexEdit</class>
57 <extends>QWidget</extends>
58 <header location="global">qhexedit.h</header>
59 <container>1</container>
60 </customwidget>
61 </customwidgets>
62 <resources/>
63 <connections/>
64 </ui>
General Comments 0
You need to be logged in to leave comments. Login now