fix: finalize non sql logic
This commit is contained in:
@@ -13,8 +13,15 @@
|
||||
|
||||
const static int COLUMN_COUNT = 9;
|
||||
|
||||
namespace {
|
||||
quintptr licenseItemToInternalId(const LicenseModel::LicenseItem &item)
|
||||
{
|
||||
return reinterpret_cast<quintptr>(&item);
|
||||
}
|
||||
}
|
||||
|
||||
LicenseModel::LicenseModel(QObject* parent)
|
||||
: QAbstractTableModel(parent)
|
||||
: QAbstractItemModel(parent)
|
||||
{
|
||||
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
m_db.setDatabaseName(DB_PATH);
|
||||
@@ -55,17 +62,38 @@ int LicenseModel::columnCount(const QModelIndex &parent) const
|
||||
|
||||
QVariant LicenseModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
switch (role)
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_data.size())
|
||||
return {};
|
||||
|
||||
if (role == Qt::ToolTipRole && index.column() == 0)
|
||||
return m_data[index.row()].id;
|
||||
|
||||
if (role != Qt::DisplayRole)
|
||||
return {};
|
||||
|
||||
switch (index.column())
|
||||
{
|
||||
case Qt::DisplayRole:
|
||||
break;
|
||||
case Qt::ToolTipRole:
|
||||
return {}; // TODO: return client id
|
||||
case 0:
|
||||
return m_data[index.row()].lastName ;
|
||||
case 1:
|
||||
return m_data[index.row()].firstName ;
|
||||
case 2:
|
||||
return m_data[index.row()].patronymic ;
|
||||
case 3:
|
||||
return m_data[index.row()].email ;
|
||||
case 4:
|
||||
return m_data[index.row()].phone ;
|
||||
case 5:
|
||||
return m_data[index.row()].yourCompany ;
|
||||
case 6:
|
||||
return m_data[index.row()].city ;
|
||||
case 7:
|
||||
return m_data[index.row()].createdAtUtc ;
|
||||
case 8:
|
||||
return m_data[index.row()].comment ;
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
QVariant LicenseModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
@@ -101,6 +129,23 @@ QVariant LicenseModel::headerData(int section, Qt::Orientation orientation, int
|
||||
}
|
||||
}
|
||||
|
||||
QModelIndex LicenseModel::parent(const QModelIndex &index) const
|
||||
{
|
||||
Q_UNUSED(index)
|
||||
return {};
|
||||
}
|
||||
|
||||
QModelIndex LicenseModel::index(int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
if (row < 0 || row >= m_data.size() || column < 0 || column >= COLUMN_COUNT)
|
||||
return QModelIndex();
|
||||
|
||||
if (parent.isValid())
|
||||
return QModelIndex();
|
||||
|
||||
return createIndex(row, column, reinterpret_cast<quintptr>(&(m_data[row])));
|
||||
}
|
||||
|
||||
LicenseModel::Status LicenseModel::getStatus()
|
||||
{
|
||||
return m_status;
|
||||
@@ -113,13 +158,13 @@ QString LicenseModel::getStatusText()
|
||||
|
||||
bool LicenseModel::checkTables()
|
||||
{
|
||||
bool clienttableExist = false;
|
||||
bool clientTableExist = false;
|
||||
for (const auto &table : m_db.tables())
|
||||
{
|
||||
if (table == "clients")
|
||||
return true;
|
||||
}
|
||||
if (!clienttableExist)
|
||||
if (!clientTableExist)
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -151,15 +196,33 @@ bool LicenseModel::prepareDatabase()
|
||||
|
||||
void LicenseModel::addClient(const LicenseItem &item)
|
||||
{
|
||||
beginInsertRows({}, m_data.size() - 1, m_data.size() - 1);
|
||||
m_data.append(item);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void LicenseModel::deleteClient(int index)
|
||||
{
|
||||
beginRemoveRows({}, index, index);
|
||||
m_data.removeAt(index);
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void LicenseModel::editClient(const LicenseItem &item, int index)
|
||||
{
|
||||
m_data[index] = item;
|
||||
dataChanged(this->index(index, 0), this->index(index, COLUMN_COUNT));
|
||||
}
|
||||
|
||||
void LicenseModel::updateModel()
|
||||
{
|
||||
beginResetModel();
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
LicenseModel::LicenseItem LicenseModel::getItem(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_data.count())
|
||||
return LicenseItem();
|
||||
return m_data[index];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user