##// END OF EJS Templates
Updates tab widget so it can be deleted when closed
Alexandre Leroux -
r247:83d15158feaa
parent child
Show More
@@ -1,99 +1,102
1 #include "Visualization/VisualizationTabWidget.h"
1 #include "Visualization/VisualizationTabWidget.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 #include "ui_VisualizationTabWidget.h"
3 #include "ui_VisualizationTabWidget.h"
4
4
5 #include "Visualization/VisualizationZoneWidget.h"
5 #include "Visualization/VisualizationZoneWidget.h"
6
6
7 Q_LOGGING_CATEGORY(LOG_VisualizationTabWidget, "VisualizationTabWidget")
7 Q_LOGGING_CATEGORY(LOG_VisualizationTabWidget, "VisualizationTabWidget")
8
8
9 namespace {
9 namespace {
10
10
11 /// Generates a default name for a new zone, according to the number of zones already displayed in
11 /// Generates a default name for a new zone, according to the number of zones already displayed in
12 /// the tab
12 /// the tab
13 QString defaultZoneName(const QLayout &layout)
13 QString defaultZoneName(const QLayout &layout)
14 {
14 {
15 auto count = 0;
15 auto count = 0;
16 for (auto i = 0; i < layout.count(); ++i) {
16 for (auto i = 0; i < layout.count(); ++i) {
17 if (dynamic_cast<VisualizationZoneWidget *>(layout.itemAt(i)->widget())) {
17 if (dynamic_cast<VisualizationZoneWidget *>(layout.itemAt(i)->widget())) {
18 count++;
18 count++;
19 }
19 }
20 }
20 }
21
21
22 return QObject::tr("Zone %1").arg(count + 1);
22 return QObject::tr("Zone %1").arg(count + 1);
23 }
23 }
24
24
25 } // namespace
25 } // namespace
26
26
27 struct VisualizationTabWidget::VisualizationTabWidgetPrivate {
27 struct VisualizationTabWidget::VisualizationTabWidgetPrivate {
28 explicit VisualizationTabWidgetPrivate(const QString &name) : m_Name{name} {}
28 explicit VisualizationTabWidgetPrivate(const QString &name) : m_Name{name} {}
29
29
30 QString m_Name;
30 QString m_Name;
31 };
31 };
32
32
33 VisualizationTabWidget::VisualizationTabWidget(const QString &name, QWidget *parent)
33 VisualizationTabWidget::VisualizationTabWidget(const QString &name, QWidget *parent)
34 : QWidget{parent},
34 : QWidget{parent},
35 ui{new Ui::VisualizationTabWidget},
35 ui{new Ui::VisualizationTabWidget},
36 impl{spimpl::make_unique_impl<VisualizationTabWidgetPrivate>(name)}
36 impl{spimpl::make_unique_impl<VisualizationTabWidgetPrivate>(name)}
37 {
37 {
38 ui->setupUi(this);
38 ui->setupUi(this);
39
40 // Widget is deleted when closed
41 setAttribute(Qt::WA_DeleteOnClose);
39 }
42 }
40
43
41 VisualizationTabWidget::~VisualizationTabWidget()
44 VisualizationTabWidget::~VisualizationTabWidget()
42 {
45 {
43 delete ui;
46 delete ui;
44 }
47 }
45
48
46 void VisualizationTabWidget::addZone(VisualizationZoneWidget *zoneWidget)
49 void VisualizationTabWidget::addZone(VisualizationZoneWidget *zoneWidget)
47 {
50 {
48 this->layout()->addWidget(zoneWidget);
51 this->layout()->addWidget(zoneWidget);
49 }
52 }
50
53
51 VisualizationZoneWidget *VisualizationTabWidget::createZone(std::shared_ptr<Variable> variable)
54 VisualizationZoneWidget *VisualizationTabWidget::createZone(std::shared_ptr<Variable> variable)
52 {
55 {
53 auto zoneWidget = new VisualizationZoneWidget{defaultZoneName(*layout()), this};
56 auto zoneWidget = new VisualizationZoneWidget{defaultZoneName(*layout()), this};
54 this->addZone(zoneWidget);
57 this->addZone(zoneWidget);
55
58
56 // Creates a new graph into the zone
59 // Creates a new graph into the zone
57 zoneWidget->createGraph(variable);
60 zoneWidget->createGraph(variable);
58
61
59 return zoneWidget;
62 return zoneWidget;
60 }
63 }
61
64
62 void VisualizationTabWidget::removeZone(VisualizationZoneWidget *zone)
65 void VisualizationTabWidget::removeZone(VisualizationZoneWidget *zone)
63 {
66 {
64 }
67 }
65
68
66 void VisualizationTabWidget::accept(IVisualizationWidgetVisitor *visitor)
69 void VisualizationTabWidget::accept(IVisualizationWidgetVisitor *visitor)
67 {
70 {
68 if (visitor) {
71 if (visitor) {
69 visitor->visitEnter(this);
72 visitor->visitEnter(this);
70
73
71 // Apply visitor to zone children
74 // Apply visitor to zone children
72 for (auto i = 0; i < layout()->count(); ++i) {
75 for (auto i = 0; i < layout()->count(); ++i) {
73 if (auto item = layout()->itemAt(i)) {
76 if (auto item = layout()->itemAt(i)) {
74 // Widgets different from zones are not visited (no action)
77 // Widgets different from zones are not visited (no action)
75 if (auto visualizationZoneWidget
78 if (auto visualizationZoneWidget
76 = dynamic_cast<VisualizationZoneWidget *>(item->widget())) {
79 = dynamic_cast<VisualizationZoneWidget *>(item->widget())) {
77 visualizationZoneWidget->accept(visitor);
80 visualizationZoneWidget->accept(visitor);
78 }
81 }
79 }
82 }
80 }
83 }
81
84
82 visitor->visitLeave(this);
85 visitor->visitLeave(this);
83 }
86 }
84 else {
87 else {
85 qCCritical(LOG_VisualizationTabWidget()) << tr("Can't visit widget : the visitor is null");
88 qCCritical(LOG_VisualizationTabWidget()) << tr("Can't visit widget : the visitor is null");
86 }
89 }
87 }
90 }
88
91
89 bool VisualizationTabWidget::canDrop(const Variable &variable) const
92 bool VisualizationTabWidget::canDrop(const Variable &variable) const
90 {
93 {
91 // A tab can always accomodate a variable
94 // A tab can always accomodate a variable
92 Q_UNUSED(variable);
95 Q_UNUSED(variable);
93 return true;
96 return true;
94 }
97 }
95
98
96 QString VisualizationTabWidget::name() const
99 QString VisualizationTabWidget::name() const
97 {
100 {
98 return impl->m_Name;
101 return impl->m_Name;
99 }
102 }
@@ -1,125 +1,131
1 #include "Visualization/VisualizationWidget.h"
1 #include "Visualization/VisualizationWidget.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 #include "Visualization/VisualizationGraphWidget.h"
3 #include "Visualization/VisualizationGraphWidget.h"
4 #include "Visualization/VisualizationTabWidget.h"
4 #include "Visualization/VisualizationTabWidget.h"
5 #include "Visualization/VisualizationZoneWidget.h"
5 #include "Visualization/VisualizationZoneWidget.h"
6 #include "Visualization/operations/GenerateVariableMenuOperation.h"
6 #include "Visualization/operations/GenerateVariableMenuOperation.h"
7 #include "Visualization/qcustomplot.h"
7 #include "Visualization/qcustomplot.h"
8
8
9 #include "ui_VisualizationWidget.h"
9 #include "ui_VisualizationWidget.h"
10
10
11 #include <QToolButton>
11 #include <QToolButton>
12
12
13 Q_LOGGING_CATEGORY(LOG_VisualizationWidget, "VisualizationWidget")
13 Q_LOGGING_CATEGORY(LOG_VisualizationWidget, "VisualizationWidget")
14
14
15 VisualizationWidget::VisualizationWidget(QWidget *parent)
15 VisualizationWidget::VisualizationWidget(QWidget *parent)
16 : QWidget{parent}, ui{new Ui::VisualizationWidget}
16 : QWidget{parent}, ui{new Ui::VisualizationWidget}
17 {
17 {
18 ui->setupUi(this);
18 ui->setupUi(this);
19
19
20 auto addTabViewButton = new QToolButton{ui->tabWidget};
20 auto addTabViewButton = new QToolButton{ui->tabWidget};
21 addTabViewButton->setText(tr("Add View"));
21 addTabViewButton->setText(tr("Add View"));
22 addTabViewButton->setCursor(Qt::ArrowCursor);
22 addTabViewButton->setCursor(Qt::ArrowCursor);
23 ui->tabWidget->setCornerWidget(addTabViewButton, Qt::TopRightCorner);
23 ui->tabWidget->setCornerWidget(addTabViewButton, Qt::TopRightCorner);
24
24
25 auto enableMinimumCornerWidgetSize = [this](bool enable) {
25 auto enableMinimumCornerWidgetSize = [this](bool enable) {
26
26
27 auto tabViewCornerWidget = ui->tabWidget->cornerWidget();
27 auto tabViewCornerWidget = ui->tabWidget->cornerWidget();
28 auto width = enable ? tabViewCornerWidget->width() : 0;
28 auto width = enable ? tabViewCornerWidget->width() : 0;
29 auto height = enable ? tabViewCornerWidget->height() : 0;
29 auto height = enable ? tabViewCornerWidget->height() : 0;
30 tabViewCornerWidget->setMinimumHeight(height);
30 tabViewCornerWidget->setMinimumHeight(height);
31 tabViewCornerWidget->setMinimumWidth(width);
31 tabViewCornerWidget->setMinimumWidth(width);
32 ui->tabWidget->setMinimumHeight(height);
32 ui->tabWidget->setMinimumHeight(height);
33 ui->tabWidget->setMinimumWidth(width);
33 ui->tabWidget->setMinimumWidth(width);
34 };
34 };
35
35
36 auto addTabView = [this, enableMinimumCornerWidgetSize]() {
36 auto addTabView = [this, enableMinimumCornerWidgetSize]() {
37 auto widget = new VisualizationTabWidget{QString{"View %1"}.arg(ui->tabWidget->count() + 1),
37 auto widget = new VisualizationTabWidget{QString{"View %1"}.arg(ui->tabWidget->count() + 1),
38 ui->tabWidget};
38 ui->tabWidget};
39 auto index = ui->tabWidget->addTab(widget, widget->name());
39 auto index = ui->tabWidget->addTab(widget, widget->name());
40 if (ui->tabWidget->count() > 0) {
40 if (ui->tabWidget->count() > 0) {
41 enableMinimumCornerWidgetSize(false);
41 enableMinimumCornerWidgetSize(false);
42 }
42 }
43 qCInfo(LOG_VisualizationWidget()) << tr("add the tab of index %1").arg(index);
43 qCInfo(LOG_VisualizationWidget()) << tr("add the tab of index %1").arg(index);
44 };
44 };
45
45
46 auto removeTabView = [this, enableMinimumCornerWidgetSize](int index) {
46 auto removeTabView = [this, enableMinimumCornerWidgetSize](int index) {
47 if (ui->tabWidget->count() == 1) {
47 if (ui->tabWidget->count() == 1) {
48 enableMinimumCornerWidgetSize(true);
48 enableMinimumCornerWidgetSize(true);
49 }
49 }
50
50
51 // Removes widget from tab and closes it
52 auto widget = ui->tabWidget->widget(index);
51 ui->tabWidget->removeTab(index);
53 ui->tabWidget->removeTab(index);
54 if (widget) {
55 widget->close();
56 }
57
52 qCInfo(LOG_VisualizationWidget()) << tr("remove the tab of index %1").arg(index);
58 qCInfo(LOG_VisualizationWidget()) << tr("remove the tab of index %1").arg(index);
53
59
54 };
60 };
55
61
56 ui->tabWidget->setTabsClosable(true);
62 ui->tabWidget->setTabsClosable(true);
57
63
58 connect(addTabViewButton, &QToolButton::clicked, addTabView);
64 connect(addTabViewButton, &QToolButton::clicked, addTabView);
59 connect(ui->tabWidget, &QTabWidget::tabCloseRequested, removeTabView);
65 connect(ui->tabWidget, &QTabWidget::tabCloseRequested, removeTabView);
60
66
61 // Adds default tab
67 // Adds default tab
62 addTabView();
68 addTabView();
63 }
69 }
64
70
65 VisualizationWidget::~VisualizationWidget()
71 VisualizationWidget::~VisualizationWidget()
66 {
72 {
67 delete ui;
73 delete ui;
68 }
74 }
69
75
70 void VisualizationWidget::addTab(VisualizationTabWidget *tabWidget)
76 void VisualizationWidget::addTab(VisualizationTabWidget *tabWidget)
71 {
77 {
72 // NOTE: check is this method has to be deleted because of its dupplicated version visible as
78 // NOTE: check is this method has to be deleted because of its dupplicated version visible as
73 // lambda function (in the constructor)
79 // lambda function (in the constructor)
74 }
80 }
75
81
76 VisualizationTabWidget *VisualizationWidget::createTab()
82 VisualizationTabWidget *VisualizationWidget::createTab()
77 {
83 {
78 }
84 }
79
85
80 void VisualizationWidget::removeTab(VisualizationTabWidget *tab)
86 void VisualizationWidget::removeTab(VisualizationTabWidget *tab)
81 {
87 {
82 // NOTE: check is this method has to be deleted because of its dupplicated version visible as
88 // NOTE: check is this method has to be deleted because of its dupplicated version visible as
83 // lambda function (in the constructor)
89 // lambda function (in the constructor)
84 }
90 }
85
91
86 void VisualizationWidget::accept(IVisualizationWidgetVisitor *visitor)
92 void VisualizationWidget::accept(IVisualizationWidgetVisitor *visitor)
87 {
93 {
88 if (visitor) {
94 if (visitor) {
89 visitor->visitEnter(this);
95 visitor->visitEnter(this);
90
96
91 // Apply visitor for tab children
97 // Apply visitor for tab children
92 for (auto i = 0; i < ui->tabWidget->count(); ++i) {
98 for (auto i = 0; i < ui->tabWidget->count(); ++i) {
93 // Widgets different from tabs are not visited (no action)
99 // Widgets different from tabs are not visited (no action)
94 if (auto visualizationTabWidget
100 if (auto visualizationTabWidget
95 = dynamic_cast<VisualizationTabWidget *>(ui->tabWidget->widget(i))) {
101 = dynamic_cast<VisualizationTabWidget *>(ui->tabWidget->widget(i))) {
96 visualizationTabWidget->accept(visitor);
102 visualizationTabWidget->accept(visitor);
97 }
103 }
98 }
104 }
99
105
100 visitor->visitLeave(this);
106 visitor->visitLeave(this);
101 }
107 }
102 else {
108 else {
103 qCCritical(LOG_VisualizationWidget()) << tr("Can't visit widget : the visitor is null");
109 qCCritical(LOG_VisualizationWidget()) << tr("Can't visit widget : the visitor is null");
104 }
110 }
105 }
111 }
106
112
107 bool VisualizationWidget::canDrop(const Variable &variable) const
113 bool VisualizationWidget::canDrop(const Variable &variable) const
108 {
114 {
109 // The main widget can never accomodate a variable
115 // The main widget can never accomodate a variable
110 Q_UNUSED(variable);
116 Q_UNUSED(variable);
111 return false;
117 return false;
112 }
118 }
113
119
114 QString VisualizationWidget::name() const
120 QString VisualizationWidget::name() const
115 {
121 {
116 return QStringLiteral("MainView");
122 return QStringLiteral("MainView");
117 }
123 }
118
124
119 void VisualizationWidget::attachVariableMenu(QMenu *menu,
125 void VisualizationWidget::attachVariableMenu(QMenu *menu,
120 std::shared_ptr<Variable> variable) noexcept
126 std::shared_ptr<Variable> variable) noexcept
121 {
127 {
122 // Generates the actions that make it possible to visualize the variable
128 // Generates the actions that make it possible to visualize the variable
123 auto generateVariableMenuOperation = GenerateVariableMenuOperation{menu, variable};
129 auto generateVariableMenuOperation = GenerateVariableMenuOperation{menu, variable};
124 accept(&generateVariableMenuOperation);
130 accept(&generateVariableMenuOperation);
125 }
131 }
General Comments 0
You need to be logged in to leave comments. Login now