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