File indexing completed on 2024-05-12 15:28:21

0001 /***************************************************************************
0002 File                 : DynamicPresenterWidget.cpp
0003 Project              : LabPlot
0004 Description          : Widget for dynamic presenting of worksheets
0005 --------------------------------------------------------------------
0006 Copyright            : (C) 2016 by Fabian Kristof (fkristofszabolcs@gmail.com)
0007 Copyright            : (C) 2018-2020 Alexander Semke (alexander.semke@web.de)
0008 ***************************************************************************/
0009 
0010 /***************************************************************************
0011 *                                                                         *
0012 *  This program is free software; you can redistribute it and/or modify   *
0013 *  it under the terms of the GNU General Public License as published by   *
0014 *  the Free Software Foundation; either version 2 of the License, or      *
0015 *  (at your option) any later version.                                    *
0016 *                                                                         *
0017 *  This program is distributed in the hope that it will be useful,        *
0018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020 *  GNU General Public License for more details.                           *
0021 *                                                                         *
0022 *   You should have received a copy of the GNU General Public License     *
0023 *   along with this program; if not, write to the Free Software           *
0024 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025 *   Boston, MA  02110-1301  USA                                           *
0026 *                                                                         *
0027 ***************************************************************************/
0028 #include "DynamicPresenterWidget.h"
0029 #include "commonfrontend/worksheet/WorksheetView.h"
0030 #include "SlidingPanel.h"
0031 
0032 #include <QKeyEvent>
0033 #include <QPushButton>
0034 #include <QScreen>
0035 #include <QTimeLine>
0036 
0037 DynamicPresenterWidget::DynamicPresenterWidget(Worksheet* worksheet, QWidget* parent) : QWidget(parent),
0038     m_view(new WorksheetView(worksheet)), m_timeLine(new QTimeLine(600)) {
0039 
0040     setAttribute(Qt::WA_DeleteOnClose);
0041     setFocus();
0042 
0043     m_view->setParent(this);
0044     m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0045     m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0046 
0047     m_view->fitInView(m_view->sceneRect(), Qt::KeepAspectRatio);
0048     m_view->adjustSize();
0049 
0050     const QRect& screenSize = QGuiApplication::primaryScreen()->availableGeometry();
0051 
0052     const int moveRight = (screenSize.width() - m_view->width()) / 2.0;
0053     const int moveDown = (screenSize.height() - m_view->height()) / 2.0;
0054     m_view->move(moveRight, moveDown);
0055     m_view->show();
0056 
0057     m_panel = new SlidingPanel(this, worksheet->name());
0058     qApp->installEventFilter(this);
0059     connect(m_timeLine, &QTimeLine::valueChanged, m_panel, &SlidingPanel::movePanel);
0060     connect(m_panel->quitButton(), &QPushButton::clicked, this, [=]() {close();});
0061 }
0062 
0063 DynamicPresenterWidget::~DynamicPresenterWidget() {
0064     delete m_timeLine;
0065     delete m_view;
0066 }
0067 
0068 bool DynamicPresenterWidget::eventFilter(QObject* watched, QEvent* event) {
0069     Q_UNUSED(watched);
0070     if (event->type() == QEvent::MouseMove) {
0071         if (m_panel->y() != 0 && m_panel->rect().contains(QCursor::pos()))
0072             slideDown();
0073         else if (m_panel->y() == 0 && !m_panel->rect().contains(QCursor::pos()))
0074             slideUp();
0075     }
0076 
0077     return false;
0078 }
0079 
0080 void DynamicPresenterWidget::keyPressEvent(QKeyEvent *event) {
0081     if (event->key() == Qt::Key_Escape)
0082         close();
0083 }
0084 
0085 void DynamicPresenterWidget::slideDown() {
0086     m_timeLine->setDirection(QTimeLine::Forward);
0087     startTimeline();
0088 }
0089 
0090 void DynamicPresenterWidget::slideUp() {
0091     m_timeLine->setDirection(QTimeLine::Backward);
0092     startTimeline();
0093 }
0094 
0095 void DynamicPresenterWidget::startTimeline() {
0096     if (m_timeLine->state() != QTimeLine::Running)
0097         m_timeLine->start();
0098 }
0099 
0100 void DynamicPresenterWidget::focusOutEvent(QFocusEvent *e) {
0101     if (m_view->hasFocus())
0102         setFocus();
0103 
0104     if (e->reason() & Qt::BacktabFocusReason)
0105         close();
0106 }