diff --git a/SQLiteExplorer/HexWindow.cpp b/SQLiteExplorer/HexWindow.cpp index 905e325755e9e424450d55f0937743dfe7b245b6..df804a22070adc919b38e1f27c799edc6bfdbc46 100644 --- a/SQLiteExplorer/HexWindow.cpp +++ b/SQLiteExplorer/HexWindow.cpp @@ -36,7 +36,7 @@ HexWindow::HexWindow(QWidget *parent) : m_pPageView->setAlternatingRowColors(true); m_pPageView->setStyleSheet("QTableView{background-color: rgb(250, 250, 115);" "alternate-background-color: rgb(141, 163, 215);}"); - + connect(m_pPageView->selectionModel(),SIGNAL(currentRowChanged(const QModelIndex &,const QModelIndex &)), this, SLOT(onPageCurrentRowChanged(const QModelIndex &,const QModelIndex &))); m_pHSplitter->addWidget(m_pHexEdit); m_pHSplitter->addWidget(m_pPageView); @@ -47,6 +47,7 @@ HexWindow::HexWindow(QWidget *parent) : m_pSplitter->addWidget(m_pTableWdiget); m_pTableWdiget->setSelectionBehavior(QAbstractItemView::SelectRows); m_pTableWdiget->setSelectionMode(QAbstractItemView::SingleSelection); + connect(m_pTableWdiget, SIGNAL(currentCellChanged(int,int,int,int)), this, SLOT(onTableCurrentCellChanged(int,int,int,int))); setStyleSheet("QTableWidget::item:selected { background-color: rgb(143, 221, 119) }"); @@ -196,9 +197,9 @@ void HexWindow::setPageHdrData(PageType type, ContentArea& pageHeaderArea, Conte { if(m_pCurSQLite3DB == NULL) return; int base = 10; - QStandardItem* pghdr = new QStandardItem("PageHdr"); - m_pPageViewModel->appendRow(pghdr); int pghdrOffset = (pgno==1?100:0); + QStandardItem* pghdr = GetItem(pghdrOffset, pageHeaderArea.m_len, "PageHdr"); + m_pPageViewModel->appendRow(pghdr); int row = 0; int col = 1; m_pPageViewModel->setItem(pghdr->row(), col++, new QStandardItem(QString("PageHdr"))); @@ -283,7 +284,7 @@ void HexWindow::setPageHdrData(PageType type, ContentArea& pageHeaderArea, Conte pghdr->setChild(row, col++, new QStandardItem(upperHex(raw, pghdrOffset+8, 4))); } - QStandardItem* cellPtr = new QStandardItem("CellPtr"); + QStandardItem* cellPtr = GetItem(cellidxArea.m_startAddr, cellidxArea.m_len, "CellPtr"); m_pPageViewModel->appendRow(cellPtr); col = 1; @@ -375,7 +376,7 @@ QStandardItem* HexWindow::setCellData(QStandardItem* parentItem, CSQLite3Payload qDebug() << "1"; int col = 0; int row = cells->rowCount(); - QStandardItem* cell = new QStandardItem(QString("Cell[%1]").arg(row)); + QStandardItem* cell = GetItem(area.m_startAddr,area.m_len,QString("Cell[%1]").arg(row)); cells->setChild(row, col++, cell); QString cellDesc = QString::fromStdString(raw.substr(area.m_startAddr, area.m_len)); cellDesc = cellDesc.remove("\r").remove("\n"); @@ -546,6 +547,7 @@ QStandardItem *HexWindow::GetItem(int start, i64 len, QString txt) for(int i=start; iclear(); m_mapItems.clear(); + m_mapItemsPos.clear(); m_curPageNo = pgno; QStringList headers; @@ -1028,8 +1031,10 @@ void HexWindow::onCurrentAddressChanged(qint64 address) if(address >= start && address <= end) { int row = it - m_payloadArea.begin(); + m_pTableWdiget->blockSignals(true); m_pTableWdiget->selectRow(row); m_pTableWdiget->showRow(row); + m_pTableWdiget->blockSignals(false); break; } } @@ -1059,6 +1064,56 @@ void HexWindow::onCheckBoxStatChanged(bool stat) document->endMetadata(); } +void HexWindow::onPageCurrentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) +{ + auto item = m_pPageViewModel->itemFromIndex(current); + if (!item) return; + + // qDebug() << "current is " << current.row() << "," << current.column() << " " << item->text(); + if (m_mapItemsPos.contains(item)) + { + auto pos = m_mapItemsPos[item]; + // qDebug() << "address of select node is " << pos.first << " - " << pos.first + pos.second; + m_pHexEdit->blockSignals(true); + m_pHexEdit->document()->cursor()->setSelectionRange(pos.first, pos.second); + m_pHexEdit->blockSignals(false); + + m_pTableWdiget->blockSignals(true); + for(auto it=m_payloadArea.begin(); it!=m_payloadArea.end(); ++it) + { + i64 start = it->m_startAddr; + i64 end = start + it->m_len; + if(pos.first >= start && pos.first + pos.second <= end) + { + int row = it - m_payloadArea.begin(); + m_pTableWdiget->selectRow(row); + m_pTableWdiget->showRow(row); + break; + } + } + m_pTableWdiget->blockSignals(false); + } +} + +void HexWindow::onTableCurrentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn) +{ + if (currentRow != previousRow) + { + // qDebug() << "table current row changed to " << currentRow; + + if (m_payloadArea.size() <= currentRow) return; + + auto playload = m_payloadArea[currentRow]; + auto pItems = m_mapItemsPos.keys({playload.m_startAddr, playload.m_len}); + if (!pItems.empty()) + { + auto pItem = pItems[0]; + m_pPageView->selectionModel()->setCurrentIndex(pItem->index(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + m_pPageView->scrollTo(pItem->index(), QTreeView::PositionAtTop); + } + } +} + void HexWindow::setPushBtnStats() { int curIdx = ui->comboBox->currentIndex(); diff --git a/SQLiteExplorer/HexWindow.h b/SQLiteExplorer/HexWindow.h index 9cef0de656791879380e3e9fe1e85c858579ebc7..5e7012d2e6dce187a34b06e3d3d678f2d55c9b59 100644 --- a/SQLiteExplorer/HexWindow.h +++ b/SQLiteExplorer/HexWindow.h @@ -41,6 +41,7 @@ public: QStandardItem *setCellData(QStandardItem *parentItem, CSQLite3Payload& payload, ContentArea area, string raw); QStandardItem* GetItem(int start, i64 len, QString txt); + public slots: void onPageIdSelect(int pgno, PageType type); void onPageTypeChanged(const QString& item); @@ -52,6 +53,8 @@ public slots: void onCurrentAddressChanged(qint64 address); void onCheckBoxStatChanged(bool stat); + void onPageCurrentRowChanged(const QModelIndex ¤t, const QModelIndex &previous); + void onTableCurrentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn); private: void setPushBtnStats(); @@ -81,6 +84,7 @@ private: QStringList m_pageNoAndTypes; QMap m_mapItems; + QMap > m_mapItemsPos; uint m_curPageNo; }; diff --git a/SQLiteExplorer/main.cpp b/SQLiteExplorer/main.cpp index 59e21a91854785a4fb219dba9557078c027e8206..c1ebc6b9979989259978c430d588167e67881fb9 100644 --- a/SQLiteExplorer/main.cpp +++ b/SQLiteExplorer/main.cpp @@ -1,5 +1,7 @@ #include "mainwindow.h" #include +#include +#include int main(int argc, char *argv[]) { @@ -8,5 +10,18 @@ int main(int argc, char *argv[]) w.resize(1600, 800); w.show(); + if (argc > 1) + { + auto path = argv[1]; + QFileInfo fileInfo(path); + std::set validSuffix = { + "db","sqlite" + }; + if(fileInfo.isFile() && validSuffix.count(fileInfo.suffix().toLower()) > 0) + { + w.openDatabaseFile(path); + } + } + return a.exec(); } diff --git a/SQLiteExplorer/mainwindow.h b/SQLiteExplorer/mainwindow.h index fdfe2a6000f8c6d8a9f049279522b8a4195e069e..4ef62824866ddc6f7f7af7c5a4cad88e2133d817 100644 --- a/SQLiteExplorer/mainwindow.h +++ b/SQLiteExplorer/mainwindow.h @@ -51,6 +51,8 @@ public: return m_pCurSQLite3DB; } + bool openDatabaseFile(const QString& path); + protected: void dragEnterEvent(QDragEnterEvent* e); void dropEvent(QDropEvent* e); @@ -71,9 +73,6 @@ private Q_SLOTS: void onVacuumActionTriggered(); void onAboutActionTriggered(); -private: - bool openDatabaseFile(const QString& path); - private: // Menu and Tool QAction* m_pOpenAction;