File indexing completed on 2024-04-28 04:37:33

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Alexander Dymo <adymo@kdevelop.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "view.h"
0008 
0009 #include <QWidget>
0010 
0011 #include "document.h"
0012 #include "tooldocument.h"
0013 
0014 namespace Sublime {
0015 
0016 class View;
0017 class Document;
0018 
0019 class ViewPrivate
0020 {
0021 public:
0022     ViewPrivate(Document* doc, View::WidgetOwnership ws);
0023 
0024     void unsetWidget();
0025 
0026     QWidget* widget = nullptr;
0027     Document* const doc;
0028     const View::WidgetOwnership ws;
0029 };
0030 
0031 ViewPrivate::ViewPrivate(Document* doc, View::WidgetOwnership ws)
0032     : doc(doc)
0033     , ws(ws)
0034 {
0035 }
0036 
0037 void ViewPrivate::unsetWidget()
0038 {
0039     widget = nullptr;
0040 }
0041 
0042 View::View(Document *doc, WidgetOwnership ws )
0043     : QObject(doc)
0044     , d_ptr(new ViewPrivate(doc, ws))
0045 {
0046 }
0047 
0048 View::~View()
0049 {
0050     Q_D(View);
0051 
0052     if (d->widget && d->ws == View::TakeOwnership ) {
0053         d->widget->hide();
0054         d->widget->setParent(nullptr);
0055         delete d->widget;
0056     }
0057 }
0058 
0059 Document *View::document() const
0060 {
0061     Q_D(const View);
0062 
0063     return d->doc;
0064 }
0065 
0066 QWidget *View::widget(QWidget *parent)
0067 {
0068     Q_D(View);
0069 
0070     if (!d->widget)
0071     {
0072         d->widget = createWidget(parent);
0073         // if we own this widget, we will also delete it and ideally would disconnect
0074         // the following connect before doing that. For that though we would need to store
0075         // a reference to the connection.
0076         // As the d object still exists in the destructor when we delete the widget
0077         // this lambda method though can be still safely executed, so we spare ourselves such disconnect.
0078         connect(d->widget, &QWidget::destroyed,
0079                 this, [this] { Q_D(View); d->unsetWidget(); });
0080     }
0081     return d->widget;
0082 }
0083 
0084 QWidget *View::createWidget(QWidget *parent)
0085 {
0086     Q_D(View);
0087 
0088     return d->doc->createViewWidget(parent);
0089 }
0090 
0091 bool View::hasWidget() const
0092 {
0093     Q_D(const View);
0094 
0095     return d->widget != nullptr;
0096 }
0097 
0098 void View::requestRaise()
0099 {
0100     emit raise(this);
0101 }
0102 
0103 void View::readSessionConfig(KConfigGroup& config)
0104 {
0105     Q_UNUSED(config);
0106 }
0107 
0108 void View::writeSessionConfig(KConfigGroup& config)
0109 {
0110     Q_UNUSED(config);
0111 }
0112 
0113 QList<QAction*> View::toolBarActions() const
0114 {
0115     Q_D(const View);
0116 
0117     auto* tooldoc = qobject_cast<ToolDocument*>(document());
0118     if( tooldoc )
0119     {
0120         return tooldoc->factory()->toolBarActions( d->widget );
0121     }
0122     return QList<QAction*>();
0123 }
0124 
0125 QList< QAction* > View::contextMenuActions() const
0126 {
0127     Q_D(const View);
0128 
0129     auto* tooldoc = qobject_cast<ToolDocument*>(document());
0130     if( tooldoc )
0131     {
0132         return tooldoc->factory()->contextMenuActions( d->widget );
0133     }
0134     return QList<QAction*>();
0135 }
0136 
0137 QString View::viewStatus() const
0138 {
0139     return QString();
0140 }
0141 
0142 void View::notifyPositionChanged(int newPositionInArea)
0143 {
0144     emit positionChanged(this, newPositionInArea);
0145 }
0146 
0147 }
0148 
0149 #include "moc_view.cpp"