From 56430c860affa68b4b7fe131a7679a4401eee16c Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Tue, 10 Jun 2014 02:05:44 +0200 Subject: [PATCH] refs #264, QPixmap enable list model base to simplify columns with icons * new constructor in column class * removed overridden data method, no longer needed * added icons for ATC station views * QPixmap comparison for such columns (free functions) --- src/blackgui/atcstationlistmodel.cpp | 2 ++ src/blackgui/columns.cpp | 20 ++++++++++++++++-- src/blackgui/columns.h | 27 ++++++++++++++++-------- src/blackgui/listmodelbase.cpp | 8 +++++++ src/blackgui/userlistmodel.cpp | 20 +----------------- src/blackgui/userlistmodel.h | 3 --- src/blackmisc/avatcstation.cpp | 5 ++++- src/blackmisc/avatcstation.h | 1 + src/blackmisc/blackmiscfreefunctions.cpp | 7 ++++++ 9 files changed, 59 insertions(+), 34 deletions(-) diff --git a/src/blackgui/atcstationlistmodel.cpp b/src/blackgui/atcstationlistmodel.cpp index 67bb6370e..437b55d67 100644 --- a/src/blackgui/atcstationlistmodel.cpp +++ b/src/blackgui/atcstationlistmodel.cpp @@ -38,6 +38,7 @@ namespace BlackGui case NotSet: case StationsOnline: this->m_columns.addColumn(CColumn("callsign", CAtcStation::IndexCallsignAsStringAsSet)); + this->m_columns.addColumn(CColumn(CAtcStation::IndexCallsignIcon, true)); this->m_columns.addColumn(CColumn("distance", CAtcStation::IndexDistance, Qt::AlignRight | Qt::AlignVCenter)); this->m_columns.addColumn(CColumn("frequency", CAtcStation::IndexFrequency, Qt::AlignRight | Qt::AlignVCenter)); this->m_columns.addColumn(CColumn("controllername", CAtcStation::IndexControllerRealName)); @@ -52,6 +53,7 @@ namespace BlackGui case StationsBooked: this->m_columns.addColumn(CColumn("callsign", CAtcStation::IndexCallsignAsStringAsSet)); + this->m_columns.addColumn(CColumn(CAtcStation::IndexCallsignIcon, true)); this->m_columns.addColumn(CColumn("controllername", CAtcStation::IndexControllerRealName)); this->m_columns.addColumn(CColumn("bookedfrom", CAtcStation::IndexBookedFrom)); this->m_columns.addColumn(CColumn("bookeduntil", CAtcStation::IndexBookedUntil)); diff --git a/src/blackgui/columns.cpp b/src/blackgui/columns.cpp index 5f7eef2c7..222769233 100644 --- a/src/blackgui/columns.cpp +++ b/src/blackgui/columns.cpp @@ -10,11 +10,18 @@ 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) + m_columnName(headerName), m_alignment(alignment), m_propertyIndex(propertyIndex), + m_editable(editable), m_icon(false) {} CColumn::CColumn(const QString &headerName, int propertyIndex, bool editable) : - m_columnName(headerName), m_alignment(-1), m_propertyIndex(propertyIndex), m_editable(editable) + m_columnName(headerName), m_alignment(-1), m_propertyIndex(propertyIndex), + m_editable(editable), m_icon(false) + {} + + CColumn::CColumn(int propertyIndex, bool isIcon) : + m_alignment(-1), m_propertyIndex(propertyIndex), + m_editable(false), m_icon(isIcon) {} const char *CColumn::getTranslationContextChar() const @@ -137,6 +144,15 @@ namespace BlackGui return this->m_columns.at(index.column()).isEditable(); } + /* + * Is icon? + */ + bool CColumns::isIcon(const QModelIndex &index) const + { + if (index.column() < 0 || index.column() >= this->m_columns.size()) return false; + return this->m_columns.at(index.column()).isIcon(); + } + /* * Aligment as QVariant */ diff --git a/src/blackgui/columns.h b/src/blackgui/columns.h index 30dcb7511..d5f553c48 100644 --- a/src/blackgui/columns.h +++ b/src/blackgui/columns.h @@ -33,22 +33,27 @@ namespace BlackGui /*! * \brief Constructor * \param headerName - * \param propertyIndex + * \param propertyIndex as in CValueObject::propertyByIndex * \param editable */ CColumn(const QString &headerName, int propertyIndex, bool editable); + /*! + * \brief Constructor column is icon + * \remarks only make sense with isIcon as true + * \param propertyIndex as in CValueObject::propertyByIndex + * \param isIcon icon, should be used with true only + */ + CColumn(int propertyIndex, bool isIcon); + //! Alignment for this column? - bool hasAlignment() const - { - return this->m_alignment >= 0; - } + bool hasAlignment() const { return this->m_alignment >= 0; } //! Editable? - bool isEditable() const - { - return this->m_editable; - } + bool isEditable() const { return this->m_editable; } + + //! Icon? + bool isIcon() const { return this->m_icon; } //! Aligment as QVariant QVariant aligmentAsQVariant() const; @@ -71,6 +76,7 @@ namespace BlackGui int m_alignment; int m_propertyIndex; // property index bool m_editable; + bool m_icon; const char *getTranslationContextChar() const; const char *getColumnNameChar() const; }; @@ -118,6 +124,9 @@ namespace BlackGui //! Is this column editable? bool isEditable(const QModelIndex &index) const; + //! Is icon? + bool isIcon(const QModelIndex &index) const; + //! Aligment as QVariant QVariant aligmentAsQVariant(const QModelIndex &index) const; diff --git a/src/blackgui/listmodelbase.cpp b/src/blackgui/listmodelbase.cpp index 8af7e814e..15ab7c0c6 100644 --- a/src/blackgui/listmodelbase.cpp +++ b/src/blackgui/listmodelbase.cpp @@ -74,11 +74,19 @@ namespace BlackGui if (role == Qt::DisplayRole) { + if (this->m_columns.isIcon(index)) return QVariant(); ObjectType obj = this->m_container[index.row()]; int propertyIndex = this->columnToPropertyIndex(index.column()); QString propertyString = obj.propertyByIndexAsString(propertyIndex, true); return QVariant::fromValue(propertyString); } + else if (role == Qt::DecorationRole) + { + if (!this->m_columns.isIcon(index)) return QVariant(); + ObjectType obj = this->m_container[index.row()]; + int propertyIndex = this->columnToPropertyIndex(index.column()); + return obj.propertyByIndex(propertyIndex); + } else if (role == Qt::TextAlignmentRole) { return this->m_columns.aligmentAsQVariant(index); diff --git a/src/blackgui/userlistmodel.cpp b/src/blackgui/userlistmodel.cpp index 078ba8061..3e0db34f1 100644 --- a/src/blackgui/userlistmodel.cpp +++ b/src/blackgui/userlistmodel.cpp @@ -33,7 +33,7 @@ namespace BlackGui { case NotSet: case UserDetailed: - this->m_columns.addColumn(CColumn("", CUser::IndexCallsignIcon)); + this->m_columns.addColumn(CColumn(CUser::IndexCallsignIcon, true)); this->m_columns.addColumn(CColumn("realname", CUser::IndexRealName)); this->m_columns.addColumn(CColumn("callsign", CUser::IndexCallsign)); this->m_columns.addColumn(CColumn("userid", CUser::IndexId)); @@ -51,22 +51,4 @@ namespace BlackGui break; } } - - /* - * Display icons - */ - QVariant CUserListModel::data(const QModelIndex &modelIndex, int role) const - { - // shortcut, fast check - if (role != Qt::DecorationRole) return CListModelBase::data(modelIndex, role); - if (this->columnToPropertyIndex(modelIndex.column()) == CUser::IndexCallsignIcon) - { - if (role == Qt::DecorationRole) - { - CUser u = this->at(modelIndex); - return u.toIcon(); - } - } - return CListModelBase::data(modelIndex, role); - } } diff --git a/src/blackgui/userlistmodel.h b/src/blackgui/userlistmodel.h index c1139fb7c..7435e1c49 100644 --- a/src/blackgui/userlistmodel.h +++ b/src/blackgui/userlistmodel.h @@ -29,9 +29,6 @@ namespace BlackGui //! \brief Destructor virtual ~CUserListModel() {} - //! \copydoc CListModelBase::data - QVariant data(const QModelIndex &modelIndex, int role = Qt::DisplayRole) const; - //! Set station mode void setUserMode(UserMode userMode); diff --git a/src/blackmisc/avatcstation.cpp b/src/blackmisc/avatcstation.cpp index a76eea8e6..fc35b891c 100644 --- a/src/blackmisc/avatcstation.cpp +++ b/src/blackmisc/avatcstation.cpp @@ -236,7 +236,8 @@ namespace BlackMisc /* * Frequency */ - void CAtcStation::setFrequency(const CFrequency &frequency) { + void CAtcStation::setFrequency(const CFrequency &frequency) + { this->m_frequency = frequency; this->m_frequency.setUnit(CFrequencyUnit::MHz()); } @@ -318,6 +319,8 @@ namespace BlackMisc return QVariant(this->m_callsign.asString()); case IndexCallsignAsStringAsSet: return QVariant(this->m_callsign.getStringAsSet()); + case IndexCallsignIcon: + return QVariant(this->m_callsign.toIcon()); case IndexController: return this->m_controller.toQVariant(); case IndexControllerRealName: diff --git a/src/blackmisc/avatcstation.h b/src/blackmisc/avatcstation.h index 7e01da4c9..53e0c0ce2 100644 --- a/src/blackmisc/avatcstation.h +++ b/src/blackmisc/avatcstation.h @@ -40,6 +40,7 @@ namespace BlackMisc IndexCallsign = 0, IndexCallsignAsString, IndexCallsignAsStringAsSet, + IndexCallsignIcon, IndexController, IndexControllerRealName, IndexControllerId, diff --git a/src/blackmisc/blackmiscfreefunctions.cpp b/src/blackmisc/blackmiscfreefunctions.cpp index aade33da8..86400f4e5 100644 --- a/src/blackmisc/blackmiscfreefunctions.cpp +++ b/src/blackmisc/blackmiscfreefunctions.cpp @@ -222,6 +222,13 @@ int BlackMisc:: compareQVariants(const QVariant &v1, const QVariant &v2) if (t1 == t2) return 0; return t1 < t2 ? -1 : 1; } + case QMetaType::QPixmap: + { + QPixmap p1 = v1.value(); + QPixmap p2 = v2.value(); + if (p1.width() == p2.width()) return 0; + return p1.width() < p2.width() ? -1 : 1; + } default: break; }