1 // copyright (c) 2019-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 "invwidget.h"
22 
InvWidget(QWidget * pParent,TYPE type)23 InvWidget::InvWidget(QWidget *pParent, TYPE type) :
24     XShortcutsWidget(pParent)
25 {
26     g_pHexPushButton=nullptr;
27     g_pDisasmPushButton=nullptr;
28 
29     QHBoxLayout *pLayot=new QHBoxLayout(this);
30     pLayot->setContentsMargins(0,0,0,0);
31 
32     if(type==TYPE_HEX)
33     {
34         g_pHexPushButton=new QPushButton(tr("Hex"),this);
35 
36         connect(g_pHexPushButton,SIGNAL(clicked()),this,SLOT(showHexSlot()));
37 
38         pLayot->addWidget(g_pHexPushButton);
39     }
40     else if(type==TYPE_DISASM)
41     {
42         g_pDisasmPushButton=new QPushButton(tr("Disasm"),this);
43 
44         connect(g_pDisasmPushButton,SIGNAL(clicked()),this,SLOT(showDisasmSlot()));
45 
46         pLayot->addWidget(g_pDisasmPushButton);
47     }
48 
49     setLayout(pLayot);
50 
51     g_nOffset=0;
52     g_nSize=0;
53 }
54 
~InvWidget()55 InvWidget::~InvWidget()
56 {
57 
58 }
59 
setOffsetAndSize(XBinary * pBinary,qint64 nOffset,qint64 nSize,bool bNotNull)60 void InvWidget::setOffsetAndSize(XBinary *pBinary, qint64 nOffset, qint64 nSize, bool bNotNull)
61 {
62     bool bValid=false;
63 
64     if((bNotNull)&&(nOffset==0))
65     {
66         bValid=false;
67     }
68     else if(pBinary->isOffsetValid(nOffset))
69     {
70         bValid=true;
71     }
72 
73     if(bValid)
74     {
75         _setEnabled(true);
76 
77         this->g_nOffset=nOffset;
78         this->g_nSize=nSize;
79     }
80     else
81     {
82         _setEnabled(false);
83 
84         this->g_nOffset=0;
85         this->g_nSize=0;
86     }
87 }
88 
setAddressAndSize(XBinary * pBinary,qint64 nAddress,qint64 nSize,bool bNotNull)89 void InvWidget::setAddressAndSize(XBinary *pBinary, qint64 nAddress, qint64 nSize, bool bNotNull)
90 {
91     bool bValid=false;
92 
93     this->g_nAddress=nAddress;
94 
95     XBinary::_MEMORY_MAP memoryMap=pBinary->getMemoryMap();
96 
97     if((bNotNull)&&(nAddress==0))
98     {
99         bValid=false;
100     }
101     else if(pBinary->isAddressPhysical(&memoryMap,nAddress))
102     {
103         bValid=true;
104     }
105 
106     if(bValid)
107     {
108         _setEnabled(true);
109 
110         this->g_nOffset=pBinary->addressToOffset(&memoryMap,nAddress);
111         this->g_nSize=nSize;
112     }
113     else
114     {
115         _setEnabled(false);
116 
117         this->g_nOffset=0;
118         this->g_nSize=0;
119     }
120 }
121 
_setEnabled(bool bState)122 void InvWidget::_setEnabled(bool bState)
123 {
124     if(g_pHexPushButton)
125     {
126         g_pHexPushButton->setEnabled(bState);
127     }
128 
129     if(g_pDisasmPushButton)
130     {
131         g_pDisasmPushButton->setEnabled(bState);
132     }
133 }
134 
showHexSlot()135 void InvWidget::showHexSlot()
136 {
137     emit showHex(g_nOffset,g_nSize);
138 }
139 
showDisasmSlot()140 void InvWidget::showDisasmSlot()
141 {
142     emit showDisasm(g_nAddress);
143 }
144 
registerShortcuts(bool bState)145 void InvWidget::registerShortcuts(bool bState)
146 {
147     Q_UNUSED(bState)
148 }
149