File indexing completed on 2024-05-12 15:27:36

0001 /***************************************************************************
0002     File                 : PartMdiView.cpp
0003     Project              : LabPlot
0004     Description          : QMdiSubWindow wrapper for aspect views.
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2013-2019 by Alexander Semke (alexander.semke@web.de)
0007     Copyright            : (C) 2007,2008 Tilman Benkert (thzs@gmx.net)
0008 
0009  ***************************************************************************/
0010 
0011 /***************************************************************************
0012  *                                                                         *
0013  *  This program is free software; you can redistribute it and/or modify   *
0014  *  it under the terms of the GNU General Public License as published by   *
0015  *  the Free Software Foundation; either version 2 of the License, or      *
0016  *  (at your option) any later version.                                    *
0017  *                                                                         *
0018  *  This program is distributed in the hope that it will be useful,        *
0019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0021  *  GNU General Public License for more details.                           *
0022  *                                                                         *
0023  *   You should have received a copy of the GNU General Public License     *
0024  *   along with this program; if not, write to the Free Software           *
0025  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0026  *   Boston, MA  02110-1301  USA                                           *
0027  *                                                                         *
0028  ***************************************************************************/
0029 #include "commonfrontend/core/PartMdiView.h"
0030 #include "backend/core/AbstractPart.h"
0031 #include "backend/worksheet/Worksheet.h"
0032 
0033 #include <QCloseEvent>
0034 #include <QIcon>
0035 
0036 /*!
0037  * \class PartMdiView
0038  *
0039  * \brief QMdiSubWindow wrapper for aspect views.
0040  *
0041  * In addition to the functionality provided by QMdiSubWindow,
0042  * this class automatically updates the window title when AbstractAspect::caption() is changed
0043  * and holds the connection to the actual data visualized in this window via the pointer to \c AbstractPart.
0044  */
0045 PartMdiView::PartMdiView(AbstractPart* part) : QMdiSubWindow(nullptr), m_part(part) {
0046     setWindowIcon(m_part->icon());
0047     setWidget(m_part->view());
0048     setAttribute(Qt::WA_DeleteOnClose);
0049     handleAspectDescriptionChanged(m_part);
0050 
0051     //resize the MDI sub window to fit the content of the view
0052     resize(m_part->view()->size());
0053 
0054     connect(m_part, &AbstractPart::aspectDescriptionChanged, this, &PartMdiView::handleAspectDescriptionChanged);
0055     connect(m_part, &AbstractPart::aspectAboutToBeRemoved, this, &PartMdiView::handleAspectAboutToBeRemoved);
0056     connect(this, &QMdiSubWindow::windowStateChanged, this, &PartMdiView::slotWindowStateChanged);
0057 }
0058 
0059 PartMdiView::~PartMdiView() {
0060     m_closing = true;
0061 }
0062 
0063 AbstractPart* PartMdiView::part() const {
0064     return m_part;
0065 }
0066 
0067 void PartMdiView::handleAspectDescriptionChanged(const AbstractAspect* aspect) {
0068     if (aspect != m_part)
0069         return;
0070 
0071     setWindowTitle(m_part->name());
0072 }
0073 
0074 void PartMdiView::handleAspectAboutToBeRemoved(const AbstractAspect* aspect) {
0075     if (aspect != m_part)
0076         return;
0077 
0078     close();
0079 }
0080 
0081 void PartMdiView::closeEvent(QCloseEvent *event) {
0082     m_part->deleteView();
0083     event->accept();
0084 }
0085 
0086 void PartMdiView::slotWindowStateChanged(Qt::WindowStates oldState, Qt::WindowStates newState) {
0087     Q_UNUSED(oldState);
0088 
0089     if (m_closing)
0090         return;
0091 
0092     if (newState.testFlag(Qt::WindowActive) || newState.testFlag(Qt::WindowMaximized))
0093         m_part->registerShortcuts();
0094     else
0095         m_part->unregisterShortcuts();
0096 }