1 // copyright (c) 2020-2021 hors<horsicq@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 //
21 #include "xhashwidget.h"
22 #include "ui_xhashwidget.h"
23
XHashWidget(QWidget * pParent)24 XHashWidget::XHashWidget(QWidget *pParent) :
25 XShortcutsWidget(pParent),
26 ui(new Ui::XHashWidget)
27 {
28 ui->setupUi(this);
29
30 g_hashData={};
31
32 #if QT_VERSION >= 0x050300
33 const QSignalBlocker blocker(ui->comboBoxMethod);
34 #else
35 const bool bBlocked1=ui->comboBoxMethod->blockSignals(true);
36 #endif
37
38 QList<XBinary::HASH> listHashMethods=XBinary::getHashMethodsAsList();
39
40 int nNumberOfMethods=listHashMethods.count();
41
42 for(int i=0;i<nNumberOfMethods;i++)
43 {
44 XBinary::HASH hash=listHashMethods.at(i);
45 ui->comboBoxMethod->addItem(XBinary::hashIdToString(hash),hash);
46 }
47
48 #if QT_VERSION < 0x050300
49 ui->comboBoxMethod->blockSignals(bBlocked1);
50 #endif
51 }
52
~XHashWidget()53 XHashWidget::~XHashWidget()
54 {
55 delete ui;
56 }
57
setData(QIODevice * pDevice,XBinary::FT fileType,qint64 nOffset,qint64 nSize,bool bAuto)58 void XHashWidget::setData(QIODevice *pDevice, XBinary::FT fileType, qint64 nOffset, qint64 nSize, bool bAuto)
59 {
60 this->g_pDevice=pDevice;
61 this->g_nOffset=nOffset;
62 this->g_nSize=nSize;
63
64 if(this->g_nSize==-1)
65 {
66 this->g_nSize=(pDevice->size())-(this->g_nOffset);
67 }
68
69 ui->lineEditOffset->setValue32_64(this->g_nOffset);
70 ui->lineEditSize->setValue32_64(this->g_nSize);
71
72 if(bAuto)
73 {
74 SubDevice subDevice(pDevice,this->g_nOffset,this->g_nSize);
75
76 if(subDevice.open(QIODevice::ReadOnly))
77 {
78 QSet<XBinary::FT> stFileType=XBinary::getFileTypes(pDevice,true);
79 stFileType.insert(XBinary::FT_COM);
80 QList<XBinary::FT> listFileTypes=XBinary::_getFileTypeListFromSet(stFileType);
81
82 XFormats::setFileTypeComboBox(ui->comboBoxType,&listFileTypes,fileType);
83
84 reload();
85
86 subDevice.close();
87 }
88 }
89 }
90
reload()91 void XHashWidget::reload()
92 {
93 g_hashData.hash=(XBinary::HASH)ui->comboBoxMethod->currentData().toInt();
94 g_hashData.fileType=(XBinary::FT)(ui->comboBoxType->currentData().toInt());
95 g_hashData.nOffset=g_nOffset;
96 g_hashData.nSize=g_nSize;
97
98 DialogHashProcess dhp(XOptions::getMainWidget(this),g_pDevice,&g_hashData);
99
100 if(dhp.exec()==QDialog::Accepted)
101 {
102 ui->lineEditHash->setText(g_hashData.sHash);
103
104 ui->tableWidgetRegions->clear();
105
106 int nNumberOfMemoryRecords=g_hashData.listMemoryRecords.count();
107
108 QStringList slHeader;
109 slHeader.append(tr("Name"));
110 slHeader.append(tr("Offset"));
111 slHeader.append(tr("Size"));
112 slHeader.append(tr("Hash"));
113
114 ui->tableWidgetRegions->setRowCount(nNumberOfMemoryRecords);
115 ui->tableWidgetRegions->setColumnCount(slHeader.size());
116
117 ui->tableWidgetRegions->setHorizontalHeaderLabels(slHeader);
118 ui->tableWidgetRegions->horizontalHeader()->setVisible(true);
119
120 for(int i=0;i<nNumberOfMemoryRecords;i++)
121 {
122 QTableWidgetItem *pItemName=new QTableWidgetItem;
123
124 pItemName->setText(g_hashData.listMemoryRecords.at(i).sName);
125 pItemName->setData(Qt::UserRole+0,g_hashData.listMemoryRecords.at(i).nOffset);
126 pItemName->setData(Qt::UserRole+1,g_hashData.listMemoryRecords.at(i).nSize);
127
128 ui->tableWidgetRegions->setItem(i,0,pItemName);
129
130 if(g_hashData.listMemoryRecords.at(i).nOffset!=-1)
131 {
132 QTableWidgetItem *pItemOffset=new QTableWidgetItem;
133
134 pItemOffset->setText(XLineEditHEX::getFormatString(g_hashData.mode,g_hashData.listMemoryRecords.at(i).nOffset));
135 pItemOffset->setTextAlignment(Qt::AlignRight);
136 ui->tableWidgetRegions->setItem(i,1,pItemOffset);
137 }
138
139 if(g_hashData.listMemoryRecords.at(i).nSize!=-1)
140 {
141 QTableWidgetItem *pItemSize=new QTableWidgetItem;
142
143 pItemSize->setText(XLineEditHEX::getFormatString(g_hashData.mode,g_hashData.listMemoryRecords.at(i).nSize));
144 pItemSize->setTextAlignment(Qt::AlignRight);
145 ui->tableWidgetRegions->setItem(i,2,pItemSize);
146 }
147
148 QTableWidgetItem *pItemHash=new QTableWidgetItem;
149
150 QString sHash=g_hashData.listMemoryRecords.at(i).sHash;
151
152 pItemHash->setText(sHash);
153 pItemHash->setTextAlignment(Qt::AlignLeft);
154 ui->tableWidgetRegions->setItem(i,3,pItemHash);
155 }
156
157 ui->tableWidgetRegions->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Interactive);
158 ui->tableWidgetRegions->horizontalHeader()->setSectionResizeMode(1,QHeaderView::Interactive);
159 ui->tableWidgetRegions->horizontalHeader()->setSectionResizeMode(2,QHeaderView::Interactive);
160 ui->tableWidgetRegions->horizontalHeader()->setSectionResizeMode(3,QHeaderView::Stretch);
161
162 qint32 nColumnSize=XLineEditHEX::getWidthFromMode(this,g_hashData.mode);
163
164 ui->tableWidgetRegions->setColumnWidth(0,150);
165 ui->tableWidgetRegions->setColumnWidth(1,nColumnSize);
166 ui->tableWidgetRegions->setColumnWidth(2,nColumnSize);
167 }
168 }
169
on_pushButtonReload_clicked()170 void XHashWidget::on_pushButtonReload_clicked()
171 {
172 reload();
173 }
174
on_comboBoxType_currentIndexChanged(int nIndex)175 void XHashWidget::on_comboBoxType_currentIndexChanged(int nIndex)
176 {
177 Q_UNUSED(nIndex)
178
179 reload();
180 }
181
on_comboBoxMethod_currentIndexChanged(int nIndex)182 void XHashWidget::on_comboBoxMethod_currentIndexChanged(int nIndex)
183 {
184 Q_UNUSED(nIndex)
185
186 reload();
187 }
188
registerShortcuts(bool bState)189 void XHashWidget::registerShortcuts(bool bState)
190 {
191 Q_UNUSED(bState)
192 // TODO
193 }
194
on_pushButtonSave_clicked()195 void XHashWidget::on_pushButtonSave_clicked()
196 {
197 QString sFileName=XBinary::getResultFileName(g_pDevice,QString("%1.txt").arg(tr("Hash")));
198
199 sFileName=QFileDialog::getSaveFileName(this,tr("Save"),sFileName,QString("%1 (*.txt);;%2 (*)").arg(tr("Text files"),tr("All files")));
200
201 if(!sFileName.isEmpty())
202 {
203 QAbstractItemModel *pModel=ui->tableWidgetRegions->model();
204
205 XOptions::saveTable(pModel,sFileName);
206 }
207 }
208