aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_bsplugins/src/MOPlugin/IPluginPanel.cpp
blob: 904f049193f43da6a391b607714c650cb25594fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "IPluginPanel.h"
#include "MOPanelInterface.h"

#include <QTabWidget>

using namespace Qt::Literals::StringLiterals;

IPluginPanel::Position IPluginPanel::Position::before(const QString& panelName)
{
  return {Order::Before, panelName};
}

IPluginPanel::Position IPluginPanel::Position::after(const QString& panelName)
{
  return {Order::After, panelName};
}

IPluginPanel::Position IPluginPanel::Position::inPlaceOf(const QString& panelName)
{
  return {Order::InPlaceOf, panelName};
}

IPluginPanel::Position IPluginPanel::Position::atStart()
{
  return {Order::AtStart, {}};
}

IPluginPanel::Position IPluginPanel::Position::atEnd()
{
  return {Order::AtEnd, {}};
}

bool IPluginPanel::init(MOBase::IOrganizer* organizer)
{
  if (organizer == nullptr) {
    return false;
  }

  organizer->onUserInterfaceInitialized([this, organizer](QMainWindow* mainWindow) {
    const auto tabWidget = mainWindow->findChild<QTabWidget*>(u"tabWidget"_s);

    if (tabWidget == nullptr) {
      return;
    }

    const auto intfc    = new MOPanelInterface(organizer, mainWindow);
    const auto widget   = this->createWidget(intfc, tabWidget);
    const auto label    = this->label();
    const auto position = this->position();

    switch (position.order_) {
    case Order::Before: {
      const auto refTab = !position.reference_.isNull()
                              ? tabWidget->findChild<QWidget*>(position.reference_)
                              : nullptr;
      if (refTab != nullptr) {
        if (const int index = tabWidget->indexOf(refTab); index != -1) {
          const int currentIndex = tabWidget->currentIndex();
          tabWidget->insertTab(index, widget, label);
          if (index <= currentIndex) {
            tabWidget->setCurrentIndex(currentIndex);
          }
        }
        break;
      }
    }
      [[fallthrough]];
    case Order::AtStart: {
      const int currentIndex = tabWidget->currentIndex();
      tabWidget->insertTab(0, widget, label);
      tabWidget->setCurrentIndex(currentIndex);
    } break;
    case Order::InPlaceOf:
    case Order::After: {
      const auto refTab = !position.reference_.isNull()
                              ? tabWidget->findChild<QWidget*>(position.reference_)
                              : nullptr;

      if (refTab != nullptr) {
        if (const int index = tabWidget->indexOf(refTab); index != -1) {
          tabWidget->insertTab(index + 1, widget, label);
          if (position.order_ == Order::InPlaceOf) {
            tabWidget->removeTab(index);
          }
        }
        break;
      }
    }
      [[fallthrough]];
    case Order::AtEnd: {
      tabWidget->addTab(widget, label);
    } break;
    }

    intfc->assignWidget(tabWidget, widget);
  });

  return this->initPlugin(organizer);
}