mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-05 01:05:34 +08:00
refs #139, added a class describing a single column, allowing to set certain columns as editable
This commit is contained in:
@@ -5,107 +5,153 @@
|
||||
|
||||
#include "columns.h"
|
||||
#include <QModelIndex>
|
||||
#include <QCoreApplication>
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
CColumn::CColumn(const QString &headerName, int propertyIndex, int alignment, bool editable) :
|
||||
m_columnName(headerName), m_alignment(alignment), m_propertyIndex(propertyIndex), m_editable(editable)
|
||||
{}
|
||||
|
||||
/*
|
||||
* Header
|
||||
*/
|
||||
CColumns::CColumns(const QString &translationContext, QObject *parent) :
|
||||
QObject(parent), m_translationContext(translationContext)
|
||||
{
|
||||
// void
|
||||
}
|
||||
CColumn::CColumn(const QString &headerName, int propertyIndex, bool editable) :
|
||||
m_columnName(headerName), m_alignment(-1), m_propertyIndex(propertyIndex), m_editable(editable)
|
||||
{}
|
||||
|
||||
/*
|
||||
* Add column name
|
||||
*/
|
||||
void CColumns::addColumn(int propertyIndex, const QString &name, int alignment)
|
||||
{
|
||||
this->m_headerNames.append(name);
|
||||
this->m_propertyIndexes.append(propertyIndex);
|
||||
this->m_alignments.append(alignment);
|
||||
}
|
||||
const char *CColumn::getTranslationContextChar() const
|
||||
{
|
||||
return this->m_translationContext.toUtf8().constData();
|
||||
}
|
||||
|
||||
/*
|
||||
* Property index to name
|
||||
*/
|
||||
QString CColumns::propertyIndexToName(int propertyIndex) const
|
||||
{
|
||||
int column = this->propertyIndexToColumn(propertyIndex);
|
||||
return this->m_headerNames.at(column);
|
||||
}
|
||||
const char *CColumn::getColumnNameChar() const
|
||||
{
|
||||
return this->m_columnName.toUtf8().constData();
|
||||
}
|
||||
|
||||
/*
|
||||
* Index to name
|
||||
*/
|
||||
QString CColumns::columnToName(int column) const
|
||||
{
|
||||
Q_ASSERT(column >= 0 && column < this->m_headerNames.size());
|
||||
return this->m_headerNames.at(column);
|
||||
}
|
||||
QVariant CColumn::aligmentAsQVariant() const
|
||||
{
|
||||
if (this->hasAlignment()) return QVariant(this->m_alignment);
|
||||
return QVariant(Qt::AlignVCenter | Qt::AlignLeft); // default
|
||||
}
|
||||
|
||||
/*
|
||||
* Get property index
|
||||
*/
|
||||
int CColumns::columnToPropertyIndex(int column) const
|
||||
{
|
||||
Q_ASSERT(column >= 0 && column < this->m_propertyIndexes.size());
|
||||
return this->m_propertyIndexes.at(column);
|
||||
}
|
||||
QString CColumn::getColumnName(bool i18n) const
|
||||
{
|
||||
if (!i18n || this->m_translationContext.isEmpty()) return this->m_columnName;
|
||||
return QCoreApplication::translate(this->getTranslationContextChar(), this->getColumnNameChar());
|
||||
}
|
||||
|
||||
/*
|
||||
* Property index to column
|
||||
*/
|
||||
int CColumns::propertyIndexToColumn(int propertyIndex) const
|
||||
{
|
||||
return this->m_propertyIndexes.indexOf(propertyIndex);
|
||||
}
|
||||
/*
|
||||
* Header
|
||||
*/
|
||||
CColumns::CColumns(const QString &translationContext, QObject *parent) :
|
||||
QObject(parent), m_translationContext(translationContext)
|
||||
{
|
||||
// void
|
||||
}
|
||||
|
||||
/*
|
||||
* Name to property index
|
||||
*/
|
||||
int CColumns::nameToPropertyIndex(const QString &name) const
|
||||
{
|
||||
int column = this->m_headerNames.indexOf(name);
|
||||
if (column < 0) return -1;
|
||||
return this->m_propertyIndexes.at(column);
|
||||
}
|
||||
/*
|
||||
* Add column
|
||||
*/
|
||||
void CColumns::addColumn(CColumn column)
|
||||
{
|
||||
Q_ASSERT(!this->m_translationContext.isEmpty());
|
||||
column.setTranslationContext(this->m_translationContext);
|
||||
this->m_columns.push_back(column);
|
||||
}
|
||||
|
||||
/*
|
||||
* Size
|
||||
*/
|
||||
int CColumns::size() const
|
||||
{
|
||||
return this->m_headerNames.size();
|
||||
}
|
||||
/*
|
||||
* Property index to name
|
||||
*/
|
||||
QString CColumns::propertyIndexToColumnName(int propertyIndex, bool i18n) const
|
||||
{
|
||||
int column = this->propertyIndexToColumn(propertyIndex);
|
||||
return this->m_columns.at(column).getColumnName(i18n);
|
||||
}
|
||||
|
||||
/*
|
||||
* Alignmet
|
||||
*/
|
||||
bool CColumns::hasAlignment(const QModelIndex &index) const
|
||||
{
|
||||
if (index.column() < 0 || index.column() >= this->m_alignments.size()) return false;
|
||||
return this->m_alignments.at(index.column()) >= 0;
|
||||
}
|
||||
/*
|
||||
* Index to name
|
||||
*/
|
||||
QString CColumns::columnToName(int column, bool i18n) const
|
||||
{
|
||||
Q_ASSERT(column >= 0 && column < this->m_columns.size());
|
||||
return this->m_columns.at(column).getColumnName(i18n);
|
||||
}
|
||||
|
||||
/*
|
||||
* Aligmnet as QVariant
|
||||
*/
|
||||
QVariant CColumns::aligmentAsQVariant(const QModelIndex &index) const
|
||||
{
|
||||
if (index.column() < 0 || index.column() >= this->m_alignments.size()) return QVariant();
|
||||
if (!this->hasAlignment(index)) return QVariant(Qt::AlignVCenter | Qt::AlignLeft); // default
|
||||
return QVariant(this->m_alignments.at(index.column()));
|
||||
}
|
||||
/*
|
||||
* Get property index
|
||||
*/
|
||||
int CColumns::columnToPropertyIndex(int column) const
|
||||
{
|
||||
Q_ASSERT(column >= 0 && column < this->m_columns.size());
|
||||
return this->m_columns.at(column).getPropertyIndex();
|
||||
}
|
||||
|
||||
/*
|
||||
* Context
|
||||
*/
|
||||
const char *CColumns::getTranslationContext() const
|
||||
{
|
||||
return this->m_translationContext.toUtf8().constData();
|
||||
}
|
||||
/*
|
||||
* Property index to column
|
||||
*/
|
||||
int CColumns::propertyIndexToColumn(int propertyIndex) const
|
||||
{
|
||||
for (int i = 0; i < this->m_columns.size(); i++)
|
||||
{
|
||||
if (this->m_columns.at(i).getPropertyIndex() == propertyIndex)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Name to property index
|
||||
*/
|
||||
int CColumns::nameToPropertyIndex(const QString &name) const
|
||||
{
|
||||
for (int i = 0; i < this->m_columns.size(); i++)
|
||||
{
|
||||
if (this->m_columns.at(i).getColumnName(false) == name)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Size
|
||||
*/
|
||||
int CColumns::size() const
|
||||
{
|
||||
return this->m_columns.size();
|
||||
}
|
||||
|
||||
/*
|
||||
* Alignment?
|
||||
*/
|
||||
bool CColumns::hasAlignment(const QModelIndex &index) const
|
||||
{
|
||||
if (index.column() < 0 || index.column() >= this->m_columns.size()) return false;
|
||||
return this->m_columns.at(index.column()).hasAlignment();
|
||||
}
|
||||
|
||||
/*
|
||||
* Editable?
|
||||
*/
|
||||
bool CColumns::isEditable(const QModelIndex &index) const
|
||||
{
|
||||
if (index.column() < 0 || index.column() >= this->m_columns.size()) return false;
|
||||
return this->m_columns.at(index.column()).isEditable();
|
||||
}
|
||||
|
||||
/*
|
||||
* Aligment as QVariant
|
||||
*/
|
||||
QVariant CColumns::aligmentAsQVariant(const QModelIndex &index) const
|
||||
{
|
||||
if (index.column() < 0 || index.column() >= this->m_columns.size()) return QVariant();
|
||||
return this->m_columns.at(index.column()).aligmentAsQVariant();
|
||||
}
|
||||
|
||||
/*
|
||||
* Context
|
||||
*/
|
||||
const char *CColumns::getTranslationContextChar() const
|
||||
{
|
||||
return this->m_translationContext.toUtf8().constData();
|
||||
}
|
||||
|
||||
} // namespace BlackGui
|
||||
|
||||
Reference in New Issue
Block a user