##// END OF EJS Templates
Added Drag and Drop for opening files.
Jeandet Alexis -
r5:4b45cc285cfb default
parent child
Show More
@@ -0,0 +1,37
1 #include "filelist.h"
2 #include <QMimeData>
3
4 FileList::FileList(QWidget *parent) :
5 QTableWidget(parent)
6 {
7 setHorizontalHeaderLabels(QStringList()<<"File"<<"Type");
8 this->setAcceptDrops(true);
9 }
10
11 void FileList::dragEnterEvent(QDragEnterEvent *event)
12 {
13 event->acceptProposedAction();
14 }
15
16 void FileList::dragMoveEvent(QDragMoveEvent *event)
17 {
18 event->acceptProposedAction();
19 }
20
21 void FileList::dropEvent(QDropEvent *event)
22 {
23 const QMimeData* mimeData = event->mimeData();
24
25 if (mimeData->hasUrls())
26 {
27 QStringList pathList;
28 QList<QUrl> urlList = mimeData->urls();
29
30 for (int i = 0; i < urlList.size() && i < 32; ++i)
31 {
32 pathList.append(urlList.at(i).toLocalFile());
33 }
34 emit openFiles(pathList);
35 event->acceptProposedAction();
36 }
37 }
@@ -0,0 +1,26
1 #ifndef FILELIST_H
2 #define FILELIST_H
3
4 #include <QTableWidget>
5 #include <QDropEvent>
6 #include <QDragEnterEvent>
7 #include <QDragMoveEvent>
8 #include <QString>
9 #include <QStringList>
10
11 class FileList : public QTableWidget
12 {
13 Q_OBJECT
14 public:
15 explicit FileList(QWidget *parent = 0);
16 void dragEnterEvent(QDragEnterEvent *event);
17 void dragMoveEvent(QDragMoveEvent *event);
18 void dropEvent(QDropEvent *event);
19
20 signals:
21 void openFiles(const QStringList& files);
22 public slots:
23
24 };
25
26 #endif // FILELIST_H
@@ -1,185 +1,190
1 #include "genericbinaryfilewidget.h"
1 #include "genericbinaryfilewidget.h"
2 #include "ui_genericbinaryfilewidget.h"
2 #include "ui_genericbinaryfilewidget.h"
3 #include <QFileDialog>
3 #include <QFileDialog>
4 #include <QFile>
4 #include <QFile>
5 #include "srec/srecfile.h"
5 #include "srec/srecfile.h"
6 #include "srec/srecfilewidget.h"
6 #include "srec/srecfilewidget.h"
7 #include "BinFile/binaryfile.h"
7 #include "BinFile/binaryfile.h"
8 #include "BinFile/binaryfilewidget.h"
8 #include "BinFile/binaryfilewidget.h"
9 #include "elf/elffile.h"
9 #include "elf/elffile.h"
10 #include "elf/elffilewidget.h"
10 #include "elf/elffilewidget.h"
11
11
12
12
13 genericBinaryFileWidget::genericBinaryFileWidget(QWidget *parent) :
13 genericBinaryFileWidget::genericBinaryFileWidget(QWidget *parent) :
14 QWidget(parent),
14 QWidget(parent),
15 ui(new Ui::genericBinaryFileWidget)
15 ui(new Ui::genericBinaryFileWidget)
16 {
16 {
17 ui->setupUi(this);
17 ui->setupUi(this);
18 connect(this->ui->openFileQpb,SIGNAL(clicked()),this,SLOT(openFile()));
18 connect(this->ui->openFileQpb,SIGNAL(clicked()),this,SLOT(openFile()));
19 connect(this->ui->removeFileQpb,SIGNAL(clicked()),this,SLOT(removeFiles()));
19 connect(this->ui->removeFileQpb,SIGNAL(clicked()),this,SLOT(removeFiles()));
20 connect(this->ui->fileList,SIGNAL(cellActivated(int,int)),this,SLOT(fileCellActivated(int,int)));
20 connect(this->ui->fileList,SIGNAL(cellActivated(int,int)),this,SLOT(fileCellActivated(int,int)));
21 connect(this->ui->fileList,SIGNAL(openFiles(QStringList)),this,SLOT(openFile(QStringList)));
21 }
22 }
22
23
23 genericBinaryFileWidget::~genericBinaryFileWidget()
24 genericBinaryFileWidget::~genericBinaryFileWidget()
24 {
25 {
25 delete ui;
26 delete ui;
26 }
27 }
27
28
28 void genericBinaryFileWidget::openFile()
29 void genericBinaryFileWidget::openFile()
29 {
30 {
30 QStringList filesNames = QFileDialog::getOpenFileNames(
31 QStringList filesNames = QFileDialog::getOpenFileNames(
31 this,
32 this,
32 "Select one or more files to open",
33 "Select one or more files to open",
33 NULL,
34 NULL,
34 "Binary Files (*.bin);;SREC Files (*.srec);;Elf Files (*)");
35 "Binary Files (*.bin);;SREC Files (*.srec);;Elf Files (*)");
35
36
36 for(int i=0;i<filesNames.count();i++)
37 openFile(filesNames);
38 }
39
40 void genericBinaryFileWidget::openFile(const QStringList &FileList)
41 {
42 for(int i=0;i<FileList.count();i++)
37 {
43 {
38 bool fileOpened = false;
44 bool fileOpened = false;
39 for(int l=0;l<files.count();l++)
45 for(int l=0;l<files.count();l++)
40 {
46 {
41 if(filesNames.at(i)==files.at(l)->fileName)
47 if(FileList.at(i)==files.at(l)->fileName)
42 {
48 {
43 fileOpened = true;
49 fileOpened = true;
44 }
50 }
45 }
51 }
46 QFile file(filesNames.at(i));
52 QFile file(FileList.at(i));
47 if(!fileOpened && file.open(QIODevice::ReadOnly))
53 if(!fileOpened && file.open(QIODevice::ReadOnly))
48 {
54 {
49 char magic[4];
55 char magic[4];
50 file.read(magic,4);
56 file.read(magic,4);
51 QString line;
57 QString line;
52 switch((int)magic[0])
58 switch((int)magic[0])
53 {
59 {
54 case 0x7F:
60 case 0x7F:
55 if((magic[1]=='E') && (magic[2]=='L') && (magic[3]=='F'))
61 if((magic[1]=='E') && (magic[2]=='L') && (magic[3]=='F'))
56 {
62 {
57 files.append(new FileListElement(filesNames.at(i),false,Type_Elf,NULL,NULL));
63 files.append(new FileListElement(FileList.at(i),false,Type_Elf,NULL,NULL));
58 }
64 }
59 break;
65 break;
60 case 'S':
66 case 'S':
61 file.seek(0);
67 file.seek(0);
62 line = file.readLine();
68 line = file.readLine();
63 if(srecFile::checkSum(line))
69 if(srecFile::checkSum(line))
64 {
70 {
65 files.append(new FileListElement(filesNames.at(i),false,Type_SREC,NULL,NULL));
71 files.append(new FileListElement(FileList.at(i),false,Type_SREC,NULL,NULL));
66 }
72 }
67 break;
73 break;
68 default:
74 default:
69 files.append(new FileListElement(filesNames.at(i),false,Type_Bin,NULL,NULL));
75 files.append(new FileListElement(FileList.at(i),false,Type_Bin,NULL,NULL));
70 break;
76 break;
71 }
77 }
72 }
78 }
73 }
79 }
74 updateFileList();
80 updateFileList();
75
76 }
81 }
77
82
78 void genericBinaryFileWidget::updateFileList()
83 void genericBinaryFileWidget::updateFileList()
79 {
84 {
80 this->ui->fileList->clear();
85 this->ui->fileList->clear();
81 this->ui->fileList->setRowCount(files.count());
86 this->ui->fileList->setRowCount(files.count());
82 this->ui->fileList->setHorizontalHeaderLabels(QStringList()<<"File"<<"Type");
87 this->ui->fileList->setHorizontalHeaderLabels(QStringList()<<"File"<<"Type");
83 for(int i=0;i<files.count();i++)
88 for(int i=0;i<files.count();i++)
84 {
89 {
85 QTableWidgetItem *newItem = new QTableWidgetItem(files.at(i)->fileName);
90 QTableWidgetItem *newItem = new QTableWidgetItem(files.at(i)->fileName);
86 newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);
91 newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);
87 this->ui->fileList->setItem(i, 0, newItem);
92 this->ui->fileList->setItem(i, 0, newItem);
88
93
89 newItem = new QTableWidgetItem(files.at(i)->type());
94 newItem = new QTableWidgetItem(files.at(i)->type());
90 newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);
95 newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable);
91 this->ui->fileList->setItem(i, 1, newItem);
96 this->ui->fileList->setItem(i, 1, newItem);
92 }
97 }
93 this->ui->fileList->resizeColumnsToContents();
98 this->ui->fileList->resizeColumnsToContents();
94 }
99 }
95
100
96 void genericBinaryFileWidget::removeFiles()
101 void genericBinaryFileWidget::removeFiles()
97 {
102 {
98 QList<QTableWidgetItem*> items = this->ui->fileList->selectedItems();
103 QList<QTableWidgetItem*> items = this->ui->fileList->selectedItems();
99 for(int i=0;i<items.count();i++)
104 for(int i=0;i<items.count();i++)
100 {
105 {
101 QString filename = this->ui->fileList->item(items.at(i)->row(),0)->text();
106 QString filename = this->ui->fileList->item(items.at(i)->row(),0)->text();
102 for(int l=0;l<files.count();l++)
107 for(int l=0;l<files.count();l++)
103 {
108 {
104 if(files.at(l)->fileName==filename)
109 if(files.at(l)->fileName==filename)
105 {
110 {
106 if(files.at(l)->isOpened)
111 if(files.at(l)->isOpened)
107 {
112 {
108 for(int m=0;m<this->ui->fileViewerTab->count();m++)
113 for(int m=0;m<this->ui->fileViewerTab->count();m++)
109 {
114 {
110 if(this->ui->fileViewerTab->widget(m)==this->files.at(l)->viewer)
115 if(this->ui->fileViewerTab->widget(m)==this->files.at(l)->viewer)
111 {
116 {
112 this->ui->fileViewerTab->removeTab(m);
117 this->ui->fileViewerTab->removeTab(m);
113 }
118 }
114 }
119 }
115 delete this->files.at(l)->viewer;
120 delete this->files.at(l)->viewer;
116 delete this->files.at(l)->parser;
121 delete this->files.at(l)->parser;
117 }
122 }
118 delete files.at(l);
123 delete files.at(l);
119 files.removeAt(l);
124 files.removeAt(l);
120 }
125 }
121 }
126 }
122 }
127 }
123 updateFileList();
128 updateFileList();
124 }
129 }
125
130
126 void genericBinaryFileWidget::fileCellActivated(int row, int column)
131 void genericBinaryFileWidget::fileCellActivated(int row, int column)
127 {
132 {
128 Q_UNUSED(column)
133 Q_UNUSED(column)
129 QString fileName = this->ui->fileList->item(row,0)->text();
134 QString fileName = this->ui->fileList->item(row,0)->text();
130 if(fileName!="")
135 if(fileName!="")
131 {
136 {
132 for(int l=0;l<files.count();l++)
137 for(int l=0;l<files.count();l++)
133 {
138 {
134 FileListElement* file = files.at(l);
139 FileListElement* file = files.at(l);
135 if(file->fileName==fileName)
140 if(file->fileName==fileName)
136 {
141 {
137 if(!file->isOpened)
142 if(!file->isOpened)
138 {
143 {
139 if(file->parser==NULL)
144 if(file->parser==NULL)
140 {
145 {
141 switch (file->FileType)
146 switch (file->FileType)
142 {
147 {
143 case Type_Bin:
148 case Type_Bin:
144 file->parser = new binaryFile(file->fileName);
149 file->parser = new binaryFile(file->fileName);
145 if(file->viewer==NULL)
150 if(file->viewer==NULL)
146 {
151 {
147 file->viewer = new binaryFileWidget();
152 file->viewer = new binaryFileWidget();
148 file->viewer->setFile(file->parser);
153 file->viewer->setFile(file->parser);
149 this->ui->fileViewerTab->addTab(file->viewer,file->fileName);
154 this->ui->fileViewerTab->addTab(file->viewer,file->fileName);
150 }
155 }
151 file->isOpened = true;
156 file->isOpened = true;
152 break;
157 break;
153 case Type_Elf:
158 case Type_Elf:
154 file->parser = new ElfFile(file->fileName);
159 file->parser = new ElfFile(file->fileName);
155 if(file->viewer==NULL)
160 if(file->viewer==NULL)
156 {
161 {
157 file->viewer = new elfFileWidget();
162 file->viewer = new elfFileWidget();
158 file->viewer->setFile(file->parser);
163 file->viewer->setFile(file->parser);
159 this->ui->fileViewerTab->addTab(file->viewer,file->fileName);
164 this->ui->fileViewerTab->addTab(file->viewer,file->fileName);
160 }
165 }
161 file->isOpened = true;
166 file->isOpened = true;
162 break;
167 break;
163 case Type_SREC:
168 case Type_SREC:
164 file->parser = new srecFile(file->fileName);
169 file->parser = new srecFile(file->fileName);
165 if(file->viewer==NULL)
170 if(file->viewer==NULL)
166 {
171 {
167 file->viewer = new srecFileWidget();
172 file->viewer = new srecFileWidget();
168 file->viewer->setFile(file->parser);
173 file->viewer->setFile(file->parser);
169 this->ui->fileViewerTab->addTab(file->viewer,file->fileName);
174 this->ui->fileViewerTab->addTab(file->viewer,file->fileName);
170 }
175 }
171 file->isOpened = true;
176 file->isOpened = true;
172 break;
177 break;
173 default:
178 default:
174 break;
179 break;
175 }
180 }
176 }
181 }
177 }
182 }
178 }
183 }
179 }
184 }
180 }
185 }
181 }
186 }
182
187
183
188
184
189
185
190
@@ -1,63 +1,64
1 #ifndef GENERICBINARYFILEWIDGET_H
1 #ifndef GENERICBINARYFILEWIDGET_H
2 #define GENERICBINARYFILEWIDGET_H
2 #define GENERICBINARYFILEWIDGET_H
3
3
4 #include <QWidget>
4 #include <QWidget>
5 #include <QString>
5 #include <QString>
6 #include <QStringList>
6 #include <QStringList>
7 #include <abstractbinfile.h>
7 #include <abstractbinfile.h>
8
8
9 namespace Ui {
9 namespace Ui {
10 class genericBinaryFileWidget;
10 class genericBinaryFileWidget;
11 }
11 }
12
12
13 typedef enum {Type_SREC,Type_Bin,Type_Elf}FileTypeEnum;
13 typedef enum {Type_SREC,Type_Bin,Type_Elf}FileTypeEnum;
14 class FileListElement
14 class FileListElement
15 {
15 {
16 public:
16 public:
17 FileListElement() {}
17 FileListElement() {}
18 FileListElement(QString fileName,bool isOpened,FileTypeEnum FileType,abstractBinFileWidget* viewer=0,abstractBinFile* parser=0)
18 FileListElement(QString fileName,bool isOpened,FileTypeEnum FileType,abstractBinFileWidget* viewer=0,abstractBinFile* parser=0)
19 :fileName(fileName),isOpened(isOpened),FileType(FileType),viewer(viewer),parser(parser){}
19 :fileName(fileName),isOpened(isOpened),FileType(FileType),viewer(viewer),parser(parser){}
20 QString type()
20 QString type()
21 {
21 {
22 switch (this->FileType) {
22 switch (this->FileType) {
23 case Type_SREC:
23 case Type_SREC:
24 return "Srec";
24 return "Srec";
25 break;
25 break;
26 case Type_Bin:
26 case Type_Bin:
27 return "Binary";
27 return "Binary";
28 break;
28 break;
29 case Type_Elf:
29 case Type_Elf:
30 return "Elf";
30 return "Elf";
31 break;
31 break;
32 default:
32 default:
33 return "Unknow";
33 return "Unknow";
34 break;
34 break;
35 }
35 }
36 }
36 }
37 QString fileName;
37 QString fileName;
38 bool isOpened;
38 bool isOpened;
39 FileTypeEnum FileType;
39 FileTypeEnum FileType;
40 abstractBinFileWidget* viewer;
40 abstractBinFileWidget* viewer;
41 abstractBinFile* parser;
41 abstractBinFile* parser;
42 };
42 };
43
43
44 class genericBinaryFileWidget : public QWidget
44 class genericBinaryFileWidget : public QWidget
45 {
45 {
46 Q_OBJECT
46 Q_OBJECT
47
47
48 public:
48 public:
49 explicit genericBinaryFileWidget(QWidget *parent = 0);
49 explicit genericBinaryFileWidget(QWidget *parent = 0);
50 ~genericBinaryFileWidget();
50 ~genericBinaryFileWidget();
51
51
52 public slots:
52 public slots:
53 void openFile();
53 void openFile();
54 void openFile(const QStringList& FileList);
54 void updateFileList();
55 void updateFileList();
55 void removeFiles();
56 void removeFiles();
56 void fileCellActivated(int row, int column);
57 void fileCellActivated(int row, int column);
57
58
58 private:
59 private:
59 Ui::genericBinaryFileWidget *ui;
60 Ui::genericBinaryFileWidget *ui;
60 QList<FileListElement*> files;
61 QList<FileListElement*> files;
61 };
62 };
62
63
63 #endif // GENERICBINARYFILEWIDGET_H
64 #endif // GENERICBINARYFILEWIDGET_H
@@ -1,103 +1,119
1 <?xml version="1.0" encoding="UTF-8"?>
1 <?xml version="1.0" encoding="UTF-8"?>
2 <ui version="4.0">
2 <ui version="4.0">
3 <class>genericBinaryFileWidget</class>
3 <class>genericBinaryFileWidget</class>
4 <widget class="QWidget" name="genericBinaryFileWidget">
4 <widget class="QWidget" name="genericBinaryFileWidget">
5 <property name="geometry">
5 <property name="geometry">
6 <rect>
6 <rect>
7 <x>0</x>
7 <x>0</x>
8 <y>0</y>
8 <y>0</y>
9 <width>878</width>
9 <width>878</width>
10 <height>360</height>
10 <height>360</height>
11 </rect>
11 </rect>
12 </property>
12 </property>
13 <property name="acceptDrops">
13 <property name="acceptDrops">
14 <bool>false</bool>
14 <bool>false</bool>
15 </property>
15 </property>
16 <property name="windowTitle">
16 <property name="windowTitle">
17 <string>Form</string>
17 <string>Form</string>
18 </property>
18 </property>
19 <layout class="QGridLayout" name="gridLayout_2">
19 <layout class="QGridLayout" name="gridLayout_2">
20 <item row="0" column="0">
20 <item row="0" column="0">
21 <widget class="QSplitter" name="splitter">
21 <widget class="QSplitter" name="splitter">
22 <property name="orientation">
22 <property name="orientation">
23 <enum>Qt::Horizontal</enum>
23 <enum>Qt::Horizontal</enum>
24 </property>
24 </property>
25 <widget class="QWidget" name="widget" native="true">
25 <widget class="QWidget" name="widget" native="true">
26 <layout class="QGridLayout" name="gridLayout">
26 <layout class="QGridLayout" name="gridLayout">
27 <item row="1" column="0">
27 <item row="1" column="0">
28 <widget class="QPushButton" name="openFileQpb">
28 <widget class="QPushButton" name="openFileQpb">
29 <property name="text">
29 <property name="text">
30 <string/>
30 <string/>
31 </property>
31 </property>
32 <property name="icon">
32 <property name="icon">
33 <iconset resource="genericBinaryFiles.qrc">
33 <iconset resource="genericBinaryFiles.qrc">
34 <normaloff>:/img/ressources/Gnome-list-add.svg</normaloff>:/img/ressources/Gnome-list-add.svg</iconset>
34 <normaloff>:/img/ressources/Gnome-list-add.svg</normaloff>:/img/ressources/Gnome-list-add.svg</iconset>
35 </property>
35 </property>
36 <property name="iconSize">
36 <property name="iconSize">
37 <size>
37 <size>
38 <width>32</width>
38 <width>24</width>
39 <height>32</height>
39 <height>24</height>
40 </size>
40 </size>
41 </property>
41 </property>
42 </widget>
42 </widget>
43 </item>
43 </item>
44 <item row="1" column="1">
44 <item row="1" column="1">
45 <widget class="QPushButton" name="removeFileQpb">
45 <widget class="QPushButton" name="removeFileQpb">
46 <property name="text">
46 <property name="text">
47 <string/>
47 <string/>
48 </property>
48 </property>
49 <property name="icon">
49 <property name="icon">
50 <iconset resource="genericBinaryFiles.qrc">
50 <iconset resource="genericBinaryFiles.qrc">
51 <normaloff>:/img/ressources/Gnome-user-trash.svg</normaloff>:/img/ressources/Gnome-user-trash.svg</iconset>
51 <normaloff>:/img/ressources/Gnome-user-trash.svg</normaloff>:/img/ressources/Gnome-user-trash.svg</iconset>
52 </property>
52 </property>
53 <property name="iconSize">
53 <property name="iconSize">
54 <size>
54 <size>
55 <width>32</width>
55 <width>24</width>
56 <height>32</height>
56 <height>24</height>
57 </size>
57 </size>
58 </property>
58 </property>
59 </widget>
59 </widget>
60 </item>
60 </item>
61 <item row="1" column="2">
61 <item row="1" column="2">
62 <spacer name="horizontalSpacer">
62 <spacer name="horizontalSpacer">
63 <property name="orientation">
63 <property name="orientation">
64 <enum>Qt::Horizontal</enum>
64 <enum>Qt::Horizontal</enum>
65 </property>
65 </property>
66 <property name="sizeHint" stdset="0">
66 <property name="sizeHint" stdset="0">
67 <size>
67 <size>
68 <width>40</width>
68 <width>40</width>
69 <height>20</height>
69 <height>20</height>
70 </size>
70 </size>
71 </property>
71 </property>
72 </spacer>
72 </spacer>
73 </item>
73 </item>
74 <item row="0" column="0" colspan="3">
74 <item row="0" column="0" colspan="3">
75 <widget class="QTableWidget" name="fileList">
75 <widget class="FileList" name="fileList">
76 <property name="dragEnabled">
77 <bool>false</bool>
78 </property>
76 <column>
79 <column>
77 <property name="text">
80 <property name="text">
78 <string>File</string>
81 <string>File</string>
79 </property>
82 </property>
80 </column>
83 </column>
81 <column>
84 <column>
82 <property name="text">
85 <property name="text">
83 <string>Type</string>
86 <string>Type</string>
84 </property>
87 </property>
85 </column>
88 </column>
86 </widget>
89 </widget>
87 </item>
90 </item>
88 </layout>
91 </layout>
89 </widget>
92 </widget>
90 <widget class="QTabWidget" name="fileViewerTab">
93 <widget class="QTabWidget" name="fileViewerTab">
94 <property name="minimumSize">
95 <size>
96 <width>300</width>
97 <height>0</height>
98 </size>
99 </property>
91 <property name="currentIndex">
100 <property name="currentIndex">
92 <number>-1</number>
101 <number>-1</number>
93 </property>
102 </property>
94 </widget>
103 </widget>
95 </widget>
104 </widget>
96 </item>
105 </item>
97 </layout>
106 </layout>
98 </widget>
107 </widget>
108 <customwidgets>
109 <customwidget>
110 <class>FileList</class>
111 <extends>QTableWidget</extends>
112 <header>filelist.h</header>
113 </customwidget>
114 </customwidgets>
99 <resources>
115 <resources>
100 <include location="genericBinaryFiles.qrc"/>
116 <include location="genericBinaryFiles.qrc"/>
101 </resources>
117 </resources>
102 <connections/>
118 <connections/>
103 </ui>
119 </ui>
General Comments 0
You need to be logged in to leave comments. Login now