diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2014-05-30 22:15:28 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2014-05-30 22:15:28 +0200 |
| commit | a1737ecbd0aaea0aa8ce34557663cf0b4cdb8910 (patch) | |
| tree | d8a9cbc97462889f8b8d72bbcb23f13d3c7bd663 | |
| parent | 79af684323abfa10abfc31003ab47fd89a03d625 (diff) | |
| download | generic-gui-a1737ecbd0aaea0aa8ce34557663cf0b4cdb8910.tar.gz generic-gui-a1737ecbd0aaea0aa8ce34557663cf0b4cdb8910.tar.bz2 generic-gui-a1737ecbd0aaea0aa8ce34557663cf0b4cdb8910.zip | |
Unified and simplified the widget creation api.
| -rw-r--r-- | form.hpp | 27 | ||||
| -rw-r--r-- | main.cpp | 11 | ||||
| -rw-r--r-- | qt_adapter.hpp | 37 |
3 files changed, 47 insertions, 28 deletions
@@ -144,7 +144,7 @@ private slots: //This base is required because an QObject may not be templated and Form is a class template. //Slots can be virtual however. -class QtFormBase : public QObject +class QtFormBase : public QMdiArea { Q_OBJECT @@ -159,12 +159,12 @@ template <typename T> struct Form : public QtFormBase, public FusionModelObserver { std::shared_ptr<T> model; - QMdiArea* area; QVBoxLayout* layout; QFormLayout* form_layout; QHBoxLayout* button_layout; QPushButton* prev; QLineEdit* jump_edit; + QLabel* label; QPushButton* next; std::vector<std::shared_ptr<FormUpdateHandler>> updatehandlers; std::map<int, QLineEdit*> line_edits; @@ -173,12 +173,12 @@ struct Form : public QtFormBase, public FusionModelObserver { Form(std::shared_ptr<T> model) : model(model) - , area(new QMdiArea()) , layout(new QVBoxLayout()) , form_layout(new QFormLayout()) , button_layout(new QHBoxLayout()) , prev(new QPushButton("&Previous")) , jump_edit(new QLineEdit()) + , label(new QLabel()) , next(new QPushButton("&Next")) , updatehandlers() , line_edits() @@ -196,18 +196,20 @@ struct Form : public QtFormBase, public FusionModelObserver { layout->addLayout(form_layout); layout->addLayout(button_layout); + label->setText("/" + QString::number(model->row_count())); + label->setMaximumWidth(100); + button_layout->addWidget(prev); button_layout->addWidget(jump_edit); + button_layout->addWidget(label); button_layout->addWidget(next); button_layout->setAlignment(Qt::AlignHCenter); - area->setLayout(layout); + setLayout(layout); setup_gui(); fill_data(); - area->updateGeometry(); - set_button_state(); connect( prev, SIGNAL(clicked()), @@ -310,17 +312,4 @@ struct Form : public QtFormBase, public FusionModelObserver { int i = clamp_index(current_index + 1); set_current_index(i); } - - QWidget* get_widget() - { - return area; - } }; - -template <typename Model> -std::shared_ptr<Form<Model>> make_form(std::shared_ptr<Model> model) -{ - auto ret = std::make_shared<Form<Model>>(model); - model->add_observer(ret); - return ret; -} @@ -85,16 +85,15 @@ int main() MainWindow w; - auto widget1 = std::make_shared<CustomDataModelWidget>(model); - auto widget2 = make_qt_widget(model); - auto widget3 = make_qt_widget(mapping); + auto widget1 = make_qt_widget<CustomDataModelWidget>(model); + auto widget2 = make_qt_widget<Gui::Table>(model); + auto widget3 = make_qt_widget<Gui::Table>(mapping); + auto widget4 = make_qt_widget<Gui::Form>(model); - auto form = make_form(model); - - w.add_widget(form->get_widget()); w.add_widget(widget1.get()); w.add_widget(widget2.get()); w.add_widget(widget3.get()); + w.add_widget(widget4.get());; int ret = w.show_and_run(); diff --git a/qt_adapter.hpp b/qt_adapter.hpp index 9a5c383..405f4f9 100644 --- a/qt_adapter.hpp +++ b/qt_adapter.hpp @@ -4,6 +4,7 @@ #include "boost_any_qvariant_convert.hpp" #include "meta_types.hpp" #include "gui_item_delegate.hpp" +#include "form.hpp" #include <QAbstractTableModel> #include <QTableView> @@ -16,6 +17,12 @@ #include <vector> #include <map> +namespace Gui +{ + struct Table{}; + struct Form{}; +} + template <typename T> struct QtModelType { @@ -215,9 +222,33 @@ struct WidgetType { typedef typename AdapterType<Model>::type::widget type; }; -template <typename Model> -std::shared_ptr<typename WidgetType<Model>::type> make_qt_widget(std::shared_ptr<Model> x) +//Generate a default Table-based widget. +template <typename GuiType, typename Model, typename... Args> +typename std::enable_if<std::is_same<Gui::Table, GuiType>::value, std::shared_ptr<typename WidgetType<Model>::type>>::type +make_qt_widget(std::shared_ptr<Model> model, Args... args) { - auto widget_ptr = std::make_shared<typename WidgetType<Model>::type>(x); + auto widget_ptr = std::make_shared<typename WidgetType<Model>::type>(model, args...); return widget_ptr; } + +//Generate a default Form based widget. +template <typename GuiType, typename Model, typename... Args> +typename std::enable_if<std::is_same<Gui::Form, GuiType>::value, std::shared_ptr<Form<Model>>>::type +make_qt_widget(std::shared_ptr<Model> model, Args... args) +{ + auto widget_ptr = std::make_shared<Form<Model>>(model, args...); + return widget_ptr; +} + +//Generate a custom widget thats is neither a default Form or Table, for total specialisation of the function. +template <typename GuiType, typename Model, typename... Args> +typename std::enable_if< + !std::is_same<Gui::Form, GuiType>::value && + !std::is_same<Gui::Table, GuiType>::value, std::shared_ptr<GuiType> +>::type +make_qt_widget(std::shared_ptr<Model> model, Args... args) +{ + auto widget_ptr = std::make_shared<GuiType>(model, args...); + return widget_ptr; +} + |
