File indexing completed on 2024-05-19 05:42:27

0001 // ct_lvtqtw_tabwidget.cpp                                           -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtprj_projectfile.h>
0021 #include <ct_lvtqtw_splitterview.h>
0022 
0023 #include <ct_lvtclr_colormanagement.h>
0024 #include <ct_lvtqtc_undo_manager.h>
0025 #include <ct_lvtqtw_tabwidget.h>
0026 
0027 #include <QEvent>
0028 #include <QString>
0029 
0030 #include <optional>
0031 #include <set>
0032 
0033 namespace Codethink::lvtqtw {
0034 
0035 struct SplitterView::Private {
0036     Codethink::lvtqtw::TabWidget *currentTab = nullptr;
0037     std::shared_ptr<lvtclr::ColorManagement> colorManagement;
0038     lvtldr::NodeStorage& nodeStorage;
0039     lvtqtc::UndoManager *undoManager = nullptr;
0040     lvtprj::ProjectFile& projectFile;
0041     lvtplg::PluginManager *pluginManager = nullptr;
0042 
0043     explicit Private(lvtldr::NodeStorage& nodeStorage,
0044                      lvtprj::ProjectFile& projectFile,
0045                      lvtplg::PluginManager *pluginManager):
0046         colorManagement(std::make_shared<lvtclr::ColorManagement>()),
0047         nodeStorage(nodeStorage),
0048         projectFile(projectFile),
0049         pluginManager(pluginManager)
0050     {
0051     }
0052 };
0053 
0054 // --------------------------------------------
0055 // class SplitterView
0056 // --------------------------------------------
0057 
0058 SplitterView::SplitterView(lvtldr::NodeStorage& nodeStorage,
0059                            lvtprj::ProjectFile& projectFile,
0060                            lvtplg::PluginManager *pluginManager,
0061                            QWidget *parent):
0062     QSplitter(parent), d(std::make_unique<SplitterView::Private>(nodeStorage, projectFile, pluginManager))
0063 {
0064     auto *wdg = new TabWidget(nodeStorage, projectFile, d->colorManagement, d->pluginManager);
0065     wdg->installEventFilter(this);
0066     d->currentTab = wdg;
0067     addWidget(wdg);
0068 }
0069 
0070 SplitterView::~SplitterView() = default;
0071 
0072 void SplitterView::toggle()
0073 {
0074     if (count() == 1) {
0075         auto *second_tabwdg = new TabWidget(d->nodeStorage, d->projectFile, d->colorManagement, d->pluginManager);
0076         second_tabwdg->installEventFilter(this);
0077         if (d->undoManager) {
0078             second_tabwdg->setUndoManager(d->undoManager);
0079         }
0080         addWidget(second_tabwdg);
0081     } else {
0082         assert(count() == 2);
0083         auto *second_tabwdg = qobject_cast<TabWidget *>(widget(1));
0084         assert(second_tabwdg);
0085         second_tabwdg->deleteLater();
0086         d->currentTab = qobject_cast<TabWidget *>(widget(0));
0087         Q_EMIT currentTabChanged(d->currentTab);
0088     }
0089 }
0090 
0091 int SplitterView::totalOpenTabs()
0092 {
0093     int total = 0;
0094     for (int i = 0; i < count(); i++) {
0095         auto *tab = qobject_cast<Codethink::lvtqtw::TabWidget *>(widget(i));
0096         if (!tab) {
0097             continue;
0098         }
0099         total += tab->count();
0100     }
0101     return total;
0102 }
0103 
0104 void SplitterView::setCurrentIndex(int idx)
0105 {
0106     if (count() < idx) {
0107         return;
0108     }
0109 
0110     d->currentTab = qobject_cast<Codethink::lvtqtw::TabWidget *>(widget(idx));
0111     Q_EMIT currentTabChanged(d->currentTab);
0112 }
0113 
0114 void SplitterView::setUndoManager(lvtqtc::UndoManager *undoManager)
0115 {
0116     d->undoManager = undoManager;
0117     for (int i = 0; i < count(); ++i) {
0118         auto *tabWdg = qobject_cast<TabWidget *>(widget(i));
0119         if (tabWdg) {
0120             tabWdg->setUndoManager(undoManager);
0121         }
0122     }
0123 }
0124 
0125 bool SplitterView::eventFilter(QObject *watched, QEvent *event)
0126 {
0127     if (std::set<QEvent::Type>{QEvent::MouseButtonPress, QEvent::ChildAdded, QEvent::FocusIn}.count(event->type())) {
0128         d->currentTab = qobject_cast<Codethink::lvtqtw::TabWidget *>(watched);
0129         Q_EMIT currentTabChanged(d->currentTab);
0130     }
0131     return QSplitter::eventFilter(watched, event);
0132 }
0133 
0134 void SplitterView::closeAllTabs()
0135 {
0136     for (int i = 0; i < count(); ++i) {
0137         auto *wdg = qobject_cast<Codethink::lvtqtw::TabWidget *>(widget(i));
0138         for (int j = 0; j < wdg->count(); ++j) {
0139             wdg->closeTab(0);
0140         }
0141     }
0142 }
0143 
0144 lvtqtc::GraphicsView *SplitterView::graphicsView()
0145 {
0146     if (!d->currentTab) {
0147         return nullptr; // RETURN
0148     }
0149 
0150     return d->currentTab->graphicsView();
0151 }
0152 
0153 } // end namespace Codethink::lvtqtw