UModelIndex integrated

- can be used instead of QModelIndex for non-Qt builds
This commit is contained in:
Nikolaj Schlej 2016-07-05 18:19:04 +02:00
parent 71ba5fe582
commit 12029c768c
11 changed files with 153 additions and 69 deletions

View File

@ -18,7 +18,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <QDir> #include <QDir>
#include "../common/ubytearray.h" #include "../common/ubytearray.h"
#include "../common/ustring.h" #include "../common/ustring.h"
#include "../common/umodelindex.h"
#include "../common/basetypes.h" #include "../common/basetypes.h"
#include "../common/treemodel.h" #include "../common/treemodel.h"
#include "../common/ffs.h" #include "../common/ffs.h"

View File

@ -45,7 +45,6 @@ HEADERS += \
../common/utility.h \ ../common/utility.h \
../common/LZMA/LzmaDecompress.h \ ../common/LZMA/LzmaDecompress.h \
../common/Tiano/EfiTianoDecompress.h \ ../common/Tiano/EfiTianoDecompress.h \
../common/umodelindex.h \
../common/ubytearray.h \ ../common/ubytearray.h \
../common/ustring.h \ ../common/ustring.h \
../bstrlib/bstrlib.h \ ../bstrlib/bstrlib.h \

View File

@ -12,6 +12,7 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include "ffsparser.h" #include "ffsparser.h"
#include <map>
#include <cmath> #include <cmath>
#include <algorithm> #include <algorithm>
#include <inttypes.h> #include <inttypes.h>
@ -1027,7 +1028,7 @@ USTATUS FfsParser::parseVolumeHeader(const UByteArray & volume, const UINT32 par
UINT32 alignment = 65536; // Default volume alignment is 64K UINT32 alignment = 65536; // Default volume alignment is 64K
if (volumeHeader->Revision == 1) { if (volumeHeader->Revision == 1) {
// Acquire alignment capability bit // Acquire alignment capability bit
bool alignmentCap = volumeHeader->Attributes & EFI_FVB_ALIGNMENT_CAP; BOOLEAN alignmentCap = volumeHeader->Attributes & EFI_FVB_ALIGNMENT_CAP;
if (!alignmentCap) { if (!alignmentCap) {
if ((volumeHeader->Attributes & 0xFFFF0000)) if ((volumeHeader->Attributes & 0xFFFF0000))
msgAlignmentBitsSet = true; msgAlignmentBitsSet = true;
@ -1230,7 +1231,7 @@ USTATUS FfsParser::getVolumeSize(const UByteArray & bios, UINT32 volumeOffset, U
entry += 1; entry += 1;
} }
volumeSize = volumeHeader->FvLength; volumeSize = (UINT32)volumeHeader->FvLength;
bmVolumeSize = calcVolumeSize; bmVolumeSize = calcVolumeSize;
if (volumeSize == 0) if (volumeSize == 0)
@ -1604,7 +1605,7 @@ USTATUS FfsParser::parseFileHeader(const UByteArray & file, const UINT32 parentO
} }
// Construct parsing data // Construct parsing data
bool fixed = fileHeader->Attributes & FFS_ATTRIB_FIXED; bool fixed(fileHeader->Attributes & FFS_ATTRIB_FIXED);
pdata.offset += parentOffset; pdata.offset += parentOffset;
@ -2863,8 +2864,8 @@ USTATUS FfsParser::parseTeImageSectionBody(const UModelIndex & index)
// Get data from parsing data // Get data from parsing data
PARSING_DATA pdata = parsingDataFromUModelIndex(index); PARSING_DATA pdata = parsingDataFromUModelIndex(index);
pdata.section.teImage.imageBase = teHeader->ImageBase; pdata.section.teImage.imageBase = (UINT32)teHeader->ImageBase;
pdata.section.teImage.adjustedImageBase = teHeader->ImageBase + teHeader->StrippedSize - sizeof(EFI_IMAGE_TE_HEADER); pdata.section.teImage.adjustedImageBase = (UINT32)(teHeader->ImageBase + teHeader->StrippedSize - sizeof(EFI_IMAGE_TE_HEADER));
// Update parsing data // Update parsing data
model->setParsingData(index, parsingDataToUByteArray(pdata)); model->setParsingData(index, parsingDataToUByteArray(pdata));
@ -3647,7 +3648,7 @@ USTATUS FfsParser::getStoreSize(const UByteArray & data, const UINT32 storeOffse
} }
else { // Header with 64 bit WriteQueueSize else { // Header with 64 bit WriteQueueSize
const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64* ftw64Header = (const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64*)signature; const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64* ftw64Header = (const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64*)signature;
storeSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + ftw64Header->WriteQueueSize; storeSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + (UINT32)ftw64Header->WriteQueueSize;
} }
} }
else if (*signature == NVRAM_PHOENIX_FLASH_MAP_SIGNATURE_PART1) { // Phoenix SCT flash map else if (*signature == NVRAM_PHOENIX_FLASH_MAP_SIGNATURE_PART1) { // Phoenix SCT flash map
@ -3743,7 +3744,7 @@ USTATUS FfsParser::parseFtwStoreHeader(const UByteArray & store, const UINT32 pa
has32bitHeader = true; has32bitHeader = true;
} }
else { // Header with 64 bit WriteQueueSize else { // Header with 64 bit WriteQueueSize
ftwBlockSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + ftw64BlockHeader->WriteQueueSize; ftwBlockSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + (UINT32)ftw64BlockHeader->WriteQueueSize;
has32bitHeader = false; has32bitHeader = false;
} }
if (dataSize < ftwBlockSize) { if (dataSize < ftwBlockSize) {

View File

@ -17,7 +17,6 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include "ustring.h" #include "ustring.h"
#include "ubytearray.h" #include "ubytearray.h"
#include "umodelindex.h"
#include "basetypes.h" #include "basetypes.h"
#include "treemodel.h" #include "treemodel.h"
#include "utility.h" #include "utility.h"

View File

@ -18,7 +18,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include "../common/ubytearray.h" #include "../common/ubytearray.h"
#include "../common/ustring.h" #include "../common/ustring.h"
#include "../common/umodelindex.h"
#include "basetypes.h" #include "basetypes.h"
#include "treemodel.h" #include "treemodel.h"
#include "ffs.h" #include "ffs.h"

View File

@ -17,7 +17,6 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include "ustring.h" #include "ustring.h"
#include "ubytearray.h" #include "ubytearray.h"
#include "umodelindex.h"
#include "treemodel.h" #include "treemodel.h"
#include "utility.h" #include "utility.h"
#include "parsingdata.h" #include "parsingdata.h"

View File

@ -14,25 +14,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include "treeitem.h" #include "treeitem.h"
#include "treemodel.h" #include "treemodel.h"
TreeModel::TreeModel(QObject *parent) #if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL)
: QAbstractItemModel(parent)
{
rootItem = new TreeItem(Types::Root, 0, UString(), UString(), UString(), UByteArray(), UByteArray(), UByteArray(), TRUE, FALSE, UByteArray());
}
TreeModel::~TreeModel()
{
delete rootItem;
}
int TreeModel::columnCount(const UModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
else
return rootItem->columnCount();
}
QVariant TreeModel::data(const UModelIndex &index, int role) const QVariant TreeModel::data(const UModelIndex &index, int role) const
{ {
if (!index.isValid()) if (!index.isValid())
@ -78,6 +60,53 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
return QVariant(); return QVariant();
} }
#else
UString TreeModel::data(const UModelIndex &index, int role) const
{
if (!index.isValid())
return UString();
if (role != 0 && role != 0x0100)
return UString();
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (role == 0)
return item->data(index.column());
else
return item->info();
}
UString TreeModel::headerData(int section, int orientation,
int role) const
{
if (orientation == 1 && role == 0) {
switch (section)
{
case 0:
return UString("Name");
case 1:
return UString("Action");
case 2:
return UString("Type");
case 3:
return UString("Subtype");
case 4:
return UString("Text");
}
}
return UString();
}
#endif
int TreeModel::columnCount(const UModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
else
return rootItem->columnCount();
}
UModelIndex TreeModel::index(int row, int column, const UModelIndex &parent) const UModelIndex TreeModel::index(int row, int column, const UModelIndex &parent) const
{ {

View File

@ -14,7 +14,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#ifndef TREEMODEL_H #ifndef TREEMODEL_H
#define TREEMODEL_H #define TREEMODEL_H
#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL)
// Use Qt classes
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant> #include <QVariant>
#include <QObject> #include <QObject>
@ -22,22 +25,103 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include "ubytearray.h" #include "ubytearray.h"
#include "basetypes.h" #include "basetypes.h"
#include "types.h" #include "types.h"
#include "umodelindex.h" #include "treeitem.h"
class TreeItem; #define UModelIndex QModelIndex
#else
// Use own implementation
#include "ustring.h"
#include "ubytearray.h"
#include "basetypes.h"
#include "types.h"
#include "treeitem.h"
class UModelIndex
{
friend class TreeModel;
public:
inline UModelIndex() : r(-1), c(-1), i(0), m(0) {}
// compiler-generated copy/move ctors/assignment operators are fine!
inline int row() const { return r; }
inline int column() const { return c; }
inline uint64_t internalId() const { return i; }
inline void *internalPointer() const { return reinterpret_cast<void*>(i); }
inline UModelIndex parent() const;
inline UModelIndex child(int row, int column) const;
inline CBString data(int role) const;
inline const TreeModel *model() const { return m; }
inline bool isValid() const { return (r >= 0) && (c >= 0) && (m != 0); }
inline bool operator==(const UModelIndex &other) const { return (other.r == r) && (other.i == i) && (other.c == c) && (other.m == m); }
inline bool operator!=(const UModelIndex &other) const { return !(*this == other); }
inline bool operator<(const UModelIndex &other) const
{
return r < other.r
|| (r == other.r && (c < other.c
|| (c == other.c && (i < other.i
|| (i == other.i && m < other.m)))));
}
private:
inline UModelIndex(int arow, int acolumn, void *ptr, const TreeModel *amodel)
: r(arow), c(acolumn), i(reinterpret_cast<uint64_t>(ptr)), m(amodel) {}
inline UModelIndex(int arow, int acolumn, uint64_t id, const TreeModel *amodel)
: r(arow), c(acolumn), i(id), m(amodel) {}
int r, c;
uint64_t i;
const TreeModel *m;
};
#endif
#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL)
class TreeModel : public QAbstractItemModel class TreeModel : public QAbstractItemModel
{ {
Q_OBJECT Q_OBJECT
private:
TreeItem *rootItem;
public: public:
TreeModel(QObject *parent = 0);
~TreeModel();
QVariant data(const UModelIndex &index, int role) const; QVariant data(const UModelIndex &index, int role) const;
Qt::ItemFlags flags(const UModelIndex &index) const; Qt::ItemFlags flags(const UModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const; int role = Qt::DisplayRole) const;
TreeModel(QObject *parent = 0) : QAbstractItemModel(parent) {
rootItem = new TreeItem(Types::Root, 0, UString(), UString(), UString(), UByteArray(), UByteArray(), UByteArray(), TRUE, FALSE, UByteArray());
}
#else
#define emit
class TreeModel
{
private:
TreeItem *rootItem;
void dataChanged(const UModelIndex &, const UModelIndex &) {}
void layoutAboutToBeChanged() {}
void layoutChanged() {}
public:
UString data(const UModelIndex &index, int role) const;
UString headerData(int section, int orientation, int role = 0) const;
TreeModel() {
rootItem = new TreeItem(Types::Root, 0, UString(), UString(), UString(), UByteArray(), UByteArray(), UByteArray(), TRUE, FALSE, UByteArray());
}
bool hasIndex(int row, int column, const UModelIndex &parent = UModelIndex()) const {
if (row < 0 || column < 0)
return false;
return row < rowCount(parent) && column < columnCount(parent);
}
UModelIndex createIndex(int row, int column, void *data) const { return UModelIndex(row, column, data, this); }
#endif
~TreeModel() {
delete rootItem;
}
UModelIndex index(int row, int column, UModelIndex index(int row, int column,
const UModelIndex &parent = UModelIndex()) const; const UModelIndex &parent = UModelIndex()) const;
UModelIndex parent(const UModelIndex &index) const; UModelIndex parent(const UModelIndex &index) const;
@ -70,7 +154,6 @@ public:
bool hasEmptyParsingData(const UModelIndex &index) const; bool hasEmptyParsingData(const UModelIndex &index) const;
UINT8 action(const UModelIndex &index) const; UINT8 action(const UModelIndex &index) const;
bool fixed(const UModelIndex &index) const; bool fixed(const UModelIndex &index) const;
bool compressed(const UModelIndex &index) const; bool compressed(const UModelIndex &index) const;
UModelIndex addItem(const UINT8 type, const UINT8 subtype, UModelIndex addItem(const UINT8 type, const UINT8 subtype,
@ -80,9 +163,14 @@ public:
const UModelIndex & parent = UModelIndex(), const UINT8 mode = CREATE_MODE_APPEND); const UModelIndex & parent = UModelIndex(), const UINT8 mode = CREATE_MODE_APPEND);
UModelIndex findParentOfType(const UModelIndex & index, UINT8 type) const; UModelIndex findParentOfType(const UModelIndex & index, UINT8 type) const;
private:
TreeItem *rootItem;
}; };
#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL)
// Nothing required here
#else
inline UModelIndex UModelIndex::parent() const { return m ? m->parent(*this) : UModelIndex(); }
inline UModelIndex UModelIndex::child(int row, int column) const { return m ? m->index(row, column, *this) : UModelIndex(); }
inline UString UModelIndex::data(int role) const { return m ? m->data(*this, role) : UString(); }
#endif
#endif // TREEMODEL_H #endif // TREEMODEL_H

View File

@ -13,8 +13,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#ifndef UBYTEARRAY_H #ifndef UBYTEARRAY_H
#define UBYTEARRAY_H #define UBYTEARRAY_H
//#define U_USE_QBYTEARRAY
#if defined(QT_CORE_LIB) && defined(U_USE_QBYTEARRAY) #if defined(QT_CORE_LIB) && defined(U_USE_QBYTEARRAY)
// Use Qt class, if Qt is available // Use Qt class, if Qt is available
#include <QByteArray> #include <QByteArray>

View File

@ -1,26 +0,0 @@
/* umodelindex.h
Copyright (c) 2016, Nikolaj Schlej. All rights reserved.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*/
#ifndef UMODELINDEX_H
#define UMODELINDEX_H
#if defined(QT_CORE_LIB)
// Use Qt class, if Qt is available
#include <QModelIndex>
#define UModelIndex QModelIndex
#else
// Use own implementation
#endif // QT_CORE_LIB
#endif // UMODELINDEX_H

View File

@ -15,7 +15,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#define UTILITY_H #define UTILITY_H
#include "ustring.h" #include "ustring.h"
#include "umodelindex.h"
#include "basetypes.h" #include "basetypes.h"
#include "parsingdata.h" #include "parsingdata.h"