From 3df3c86386ac9d7359ade6c68518eef1b0223049 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 17 Feb 2014 02:02:14 +0100 Subject: [PATCH] refs #139, added a class describing a single column, allowing to set certain columns as editable --- src/blackgui/columns.cpp | 222 +++++++++++++++++++++++---------------- src/blackgui/columns.h | 113 ++++++++++++++------ 2 files changed, 212 insertions(+), 123 deletions(-) diff --git a/src/blackgui/columns.cpp b/src/blackgui/columns.cpp index 34238925c..5f7eef2c7 100644 --- a/src/blackgui/columns.cpp +++ b/src/blackgui/columns.cpp @@ -5,107 +5,153 @@ #include "columns.h" #include +#include 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 diff --git a/src/blackgui/columns.h b/src/blackgui/columns.h index 4a6d72fc9..bda3f3a4e 100644 --- a/src/blackgui/columns.h +++ b/src/blackgui/columns.h @@ -11,11 +11,71 @@ #define BLACKGUI_COLUMNS_H #include "blackmisc/valueobject.h" // for qHash overload, include before Qt stuff due GCC issue +#include "blackmisc/collection.h" #include #include 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 @@ -30,25 +90,14 @@ namespace BlackGui */ CColumns(const QString &translationContext, QObject *parent = nullptr); - /*! - * \brief Add a column name - * \param propertyIndex - * \param name - * \param alignment - */ - void addColumn(int propertyIndex, const QString &name, int alignment = -1); + //! \brief Add a column + void addColumn(CColumn column); - /*! - * \brief Property index to name - * \param propertyIndex - */ - QString propertyIndexToName(int propertyIndex) const; + //! \brief Property index to name + QString propertyIndexToColumnName(int propertyIndex, bool i18n = false) const; - /*! - * \brief Column index to name - * \param column - */ - QString columnToName(int column) const; + //! \brief Column index to name + QString columnToName(int column, bool i18n = false) const; //! \brief Column to property index int columnToPropertyIndex(int column) const; @@ -65,31 +114,25 @@ namespace BlackGui //! \brief Size (number of columns) int size() const; - /*! - * \brief Alignment for this column? - * \param index - * \return - */ + //! \brief Alignment for this column? bool hasAlignment(const QModelIndex &index) const; - /*! - * \brief Aligment as QVariant - * \param index - * \return - */ + //! \brief Is this column editable? + bool isEditable(const QModelIndex &index) const; + + //! \brief Aligment as QVariant QVariant aligmentAsQVariant(const QModelIndex &index) const; - /*! - * \brief Translation context - * \return - */ - const char *getTranslationContext() const; + //! \brief Column at position + const CColumn &at(int columnNumber) const + { + return this->m_columns.at(columnNumber); + } private: + QList m_columns; QString m_translationContext; - QList m_headerNames; - QList m_propertyIndexes; // column to property index - QList m_alignments; + const char *getTranslationContextChar() const; }; } // namespace BlackGui