refs #139, added a class describing a single column, allowing to set certain columns as editable

This commit is contained in:
Klaus Basan
2014-02-17 02:02:14 +01:00
parent 4ab59f77a5
commit 3df3c86386
2 changed files with 212 additions and 123 deletions

View File

@@ -5,107 +5,153 @@
#include "columns.h" #include "columns.h"
#include <QModelIndex> #include <QModelIndex>
#include <QCoreApplication>
namespace BlackGui 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)
{}
/* CColumn::CColumn(const QString &headerName, int propertyIndex, bool editable) :
* Header m_columnName(headerName), m_alignment(-1), m_propertyIndex(propertyIndex), m_editable(editable)
*/ {}
CColumns::CColumns(const QString &translationContext, QObject *parent) :
QObject(parent), m_translationContext(translationContext)
{
// void
}
/* const char *CColumn::getTranslationContextChar() const
* Add column name {
*/ return this->m_translationContext.toUtf8().constData();
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::getColumnNameChar() const
* Property index to name {
*/ return this->m_columnName.toUtf8().constData();
QString CColumns::propertyIndexToName(int propertyIndex) const }
{
int column = this->propertyIndexToColumn(propertyIndex);
return this->m_headerNames.at(column);
}
/* QVariant CColumn::aligmentAsQVariant() const
* Index to name {
*/ if (this->hasAlignment()) return QVariant(this->m_alignment);
QString CColumns::columnToName(int column) const return QVariant(Qt::AlignVCenter | Qt::AlignLeft); // default
{ }
Q_ASSERT(column >= 0 && column < this->m_headerNames.size());
return this->m_headerNames.at(column);
}
/* QString CColumn::getColumnName(bool i18n) const
* Get property index {
*/ if (!i18n || this->m_translationContext.isEmpty()) return this->m_columnName;
int CColumns::columnToPropertyIndex(int column) const return QCoreApplication::translate(this->getTranslationContextChar(), this->getColumnNameChar());
{ }
Q_ASSERT(column >= 0 && column < this->m_propertyIndexes.size());
return this->m_propertyIndexes.at(column);
}
/* /*
* Property index to column * Header
*/ */
int CColumns::propertyIndexToColumn(int propertyIndex) const CColumns::CColumns(const QString &translationContext, QObject *parent) :
{ QObject(parent), m_translationContext(translationContext)
return this->m_propertyIndexes.indexOf(propertyIndex); {
} // void
}
/* /*
* Name to property index * Add column
*/ */
int CColumns::nameToPropertyIndex(const QString &name) const void CColumns::addColumn(CColumn column)
{ {
int column = this->m_headerNames.indexOf(name); Q_ASSERT(!this->m_translationContext.isEmpty());
if (column < 0) return -1; column.setTranslationContext(this->m_translationContext);
return this->m_propertyIndexes.at(column); this->m_columns.push_back(column);
} }
/* /*
* Size * Property index to name
*/ */
int CColumns::size() const QString CColumns::propertyIndexToColumnName(int propertyIndex, bool i18n) const
{ {
return this->m_headerNames.size(); int column = this->propertyIndexToColumn(propertyIndex);
} return this->m_columns.at(column).getColumnName(i18n);
}
/* /*
* Alignmet * Index to name
*/ */
bool CColumns::hasAlignment(const QModelIndex &index) const QString CColumns::columnToName(int column, bool i18n) const
{ {
if (index.column() < 0 || index.column() >= this->m_alignments.size()) return false; Q_ASSERT(column >= 0 && column < this->m_columns.size());
return this->m_alignments.at(index.column()) >= 0; return this->m_columns.at(column).getColumnName(i18n);
} }
/* /*
* Aligmnet as QVariant * Get property index
*/ */
QVariant CColumns::aligmentAsQVariant(const QModelIndex &index) const int CColumns::columnToPropertyIndex(int column) const
{ {
if (index.column() < 0 || index.column() >= this->m_alignments.size()) return QVariant(); Q_ASSERT(column >= 0 && column < this->m_columns.size());
if (!this->hasAlignment(index)) return QVariant(Qt::AlignVCenter | Qt::AlignLeft); // default return this->m_columns.at(column).getPropertyIndex();
return QVariant(this->m_alignments.at(index.column())); }
}
/* /*
* Context * Property index to column
*/ */
const char *CColumns::getTranslationContext() const int CColumns::propertyIndexToColumn(int propertyIndex) const
{ {
return this->m_translationContext.toUtf8().constData(); 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 } // namespace BlackGui

View File

@@ -11,11 +11,71 @@
#define BLACKGUI_COLUMNS_H #define BLACKGUI_COLUMNS_H
#include "blackmisc/valueobject.h" // for qHash overload, include before Qt stuff due GCC issue #include "blackmisc/valueobject.h" // for qHash overload, include before Qt stuff due GCC issue
#include "blackmisc/collection.h"
#include <QObject> #include <QObject>
#include <QHash> #include <QHash>
namespace BlackGui namespace BlackGui
{ {
/*!
* \brief Single column
*/
class CColumn
{
public:
/*!
* \brief Constructor
* \param headerName
* \param propertyIndex as in CValueObject::propertyByIndex
* \param alignment Qt::Alignment
* \param editable
*/
CColumn(const QString &headerName, int propertyIndex, int alignment = -1, bool editable = false);
/*!
* \brief Constructor
* \param headerName
* \param propertyIndex
* \param editable
*/
CColumn(const QString &headerName, int propertyIndex, bool editable);
//! \brief Alignment for this column?
bool hasAlignment() const
{
return this->m_alignment >= 0;
}
//! \brief Editable?
bool isEditable() const
{
return this->m_editable;
}
//! \brief Aligment as QVariant
QVariant aligmentAsQVariant() const;
//! \brief Column name
QString getColumnName(bool i18n = false) const;
//! \brief Property index
int getPropertyIndex() const { return this->m_propertyIndex;}
//! \brief Translation context
void setTranslationContext(const QString &translationContext)
{
this->m_translationContext = translationContext;
}
private:
QString m_translationContext;
QString m_columnName;
int m_alignment;
int m_propertyIndex; // property index
bool m_editable;
const char *getTranslationContextChar() const;
const char *getColumnNameChar() const;
};
/*! /*!
* \brief Header data for a table * \brief Header data for a table
@@ -30,25 +90,14 @@ namespace BlackGui
*/ */
CColumns(const QString &translationContext, QObject *parent = nullptr); CColumns(const QString &translationContext, QObject *parent = nullptr);
/*! //! \brief Add a column
* \brief Add a column name void addColumn(CColumn column);
* \param propertyIndex
* \param name
* \param alignment
*/
void addColumn(int propertyIndex, const QString &name, int alignment = -1);
/*! //! \brief Property index to name
* \brief Property index to name QString propertyIndexToColumnName(int propertyIndex, bool i18n = false) const;
* \param propertyIndex
*/
QString propertyIndexToName(int propertyIndex) const;
/*! //! \brief Column index to name
* \brief Column index to name QString columnToName(int column, bool i18n = false) const;
* \param column
*/
QString columnToName(int column) const;
//! \brief Column to property index //! \brief Column to property index
int columnToPropertyIndex(int column) const; int columnToPropertyIndex(int column) const;
@@ -65,31 +114,25 @@ namespace BlackGui
//! \brief Size (number of columns) //! \brief Size (number of columns)
int size() const; int size() const;
/*! //! \brief Alignment for this column?
* \brief Alignment for this column?
* \param index
* \return
*/
bool hasAlignment(const QModelIndex &index) const; bool hasAlignment(const QModelIndex &index) const;
/*! //! \brief Is this column editable?
* \brief Aligment as QVariant bool isEditable(const QModelIndex &index) const;
* \param index
* \return //! \brief Aligment as QVariant
*/
QVariant aligmentAsQVariant(const QModelIndex &index) const; QVariant aligmentAsQVariant(const QModelIndex &index) const;
/*! //! \brief Column at position
* \brief Translation context const CColumn &at(int columnNumber) const
* \return {
*/ return this->m_columns.at(columnNumber);
const char *getTranslationContext() const; }
private: private:
QList<CColumn> m_columns;
QString m_translationContext; QString m_translationContext;
QList<QString> m_headerNames; const char *getTranslationContextChar() const;
QList<int> m_propertyIndexes; // column to property index
QList<int> m_alignments;
}; };
} // namespace BlackGui } // namespace BlackGui