diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2014-04-28 15:25:20 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2014-04-28 16:04:45 +0200 |
| commit | 42442fd5025b69c4b7800c71d6f568ae255e7b00 (patch) | |
| tree | 8e35f36410e9b9cb7d7e5367a62d24b5c5f181c1 /qt_adapter.hpp | |
| parent | d834eba65c2c65c1540d13c9c39ced51bd87cf83 (diff) | |
| download | generic-gui-42442fd5025b69c4b7800c71d6f568ae255e7b00.tar.gz generic-gui-42442fd5025b69c4b7800c71d6f568ae255e7b00.tar.bz2 generic-gui-42442fd5025b69c4b7800c71d6f568ae255e7b00.zip | |
Adds support for a std::map<std::string, T> model.
Diffstat (limited to 'qt_adapter.hpp')
| -rw-r--r-- | qt_adapter.hpp | 62 |
1 files changed, 55 insertions, 7 deletions
diff --git a/qt_adapter.hpp b/qt_adapter.hpp index 2843667..6e0de17 100644 --- a/qt_adapter.hpp +++ b/qt_adapter.hpp @@ -3,14 +3,16 @@ #include "fusion_model.hpp" #include "boost_any_to_qvariant.hpp" #include "meta_types.hpp" +#include "gui_item_delegate.hpp" #include <QAbstractTableModel> #include <QTableView> +#include <QVariant> #include <QMetaType> #include <memory> - - +#include <string> +#include <iostream> template <typename T> struct QtModelType @@ -24,6 +26,34 @@ struct QtModelType<std::vector<V>> typedef QAbstractTableModel type; }; +template <typename V> +struct QtModelType<std::map<std::string, V>> +{ + typedef QAbstractTableModel type; +}; + +template <typename T> +struct widget_type { + typedef void type; +}; + +template <typename T> +struct widget_type<std::unique_ptr<T>> { + typedef typename T::widget type; +}; + +template <typename T> +struct QtWidget : public T { + + GuiItemDelegate delegate; + + QtWidget(QWidget* parent = nullptr) + : T(parent) + { + T::setItemDelegate(&delegate); + } +}; + template <typename T, typename Q> struct QtAdapter : public Q { @@ -37,6 +67,7 @@ template <typename T> struct QtAdapter<T, QAbstractTableModel> : public QAbstractTableModel { typedef QTableView view; + typedef QtWidget<view> widget; T model; @@ -54,16 +85,33 @@ struct QtAdapter<T, QAbstractTableModel> : public QAbstractTableModel return model.column_count(); } - virtual QVariant data(QModelIndex const& index, int role) const + virtual QVariant data(QModelIndex const& index, int role) const override { if(role != Qt::DisplayRole) return QVariant(); - return to_qvariant<typename T::data_type::value_type>(model.get_cell(index.row(), index.column()), index.column()); + return to_qvariant<typename T::row_type>(model.get_cell(index.row(), index.column()), index.column()); + } + + virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override + { + if(role != Qt::DisplayRole) { + return QVariant(); + } + + if(orientation == Qt::Horizontal && T::has_header_h) { + return QVariant(QString::fromStdString(model.field_name(section))); + } + + if(orientation == Qt::Vertical && T::has_header_v) { + return QVariant(QString::fromStdString(model.key(section))); + } + + return QVariant(); } }; - template <typename T> -std::shared_ptr<QtAdapter<T, typename QtModelType<typename T::data_type>::type>> make_qt_adapter(T value) { - return std::make_shared<QtAdapter<T, typename QtModelType<typename T::data_type>::type>>(value); +std::unique_ptr<QtAdapter<T, typename QtModelType<typename T::data_type>::type>> make_qt_adapter(T value) { + typedef QtAdapter<T, typename QtModelType<typename T::data_type>::type> type; + return std::unique_ptr<type>(new type(value)); } |
