File indexing completed on 2024-04-21 03:41:35

0001 /*************************************************************************************
0002  *  Copyright (C) 2009 by Aleix Pol <aleixpol@kde.org>                               *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #include "viewportwidget.h"
0020 
0021 #include <QDoubleSpinBox>
0022 #include <QFormLayout>
0023 #include <QPushButton>
0024 #include <klocalizedstring.h>
0025 #include <limits>
0026 
0027 ViewportWidget::ViewportWidget(QWidget *parent)
0028     : QWidget(parent)
0029 {
0030     m_top = new QDoubleSpinBox(this);
0031     m_left = new QDoubleSpinBox(this);
0032     m_width = new QDoubleSpinBox(this);
0033     m_height = new QDoubleSpinBox(this);
0034 
0035     //     const double LIMIT=std::numeric_limits<double>::max();
0036     // Can't use limit, because otherwise Qt uses the value for the sizeHint and
0037     // we get a huge window
0038     const double LIMIT = 5000;
0039     m_top->setRange(-LIMIT, LIMIT);
0040     m_left->setRange(-LIMIT, LIMIT);
0041     m_width->setRange(0, LIMIT);
0042     m_height->setRange(0, LIMIT);
0043 
0044     QVBoxLayout *upperLayout = new QVBoxLayout;
0045     QFormLayout *layout = new QFormLayout;
0046     layout->addRow(i18n("Left:"), m_left);
0047     layout->addRow(i18n("Top:"), m_top);
0048     layout->addRow(i18n("Width:"), m_width);
0049     layout->addRow(i18n("Height:"), m_height);
0050 
0051     QPushButton *apply = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-ok-apply")), i18n("Apply"), this);
0052     connect(apply, &QAbstractButton::clicked, this, &ViewportWidget::emitViewport);
0053 
0054     upperLayout->addLayout(layout);
0055     upperLayout->addWidget(apply);
0056     setLayout(upperLayout);
0057 }
0058 
0059 QRectF ViewportWidget::viewport() const
0060 {
0061     return QRectF(m_left->value(), m_top->value(), m_width->value(), -m_height->value());
0062 }
0063 
0064 void ViewportWidget::setViewport(const QRectF &current)
0065 {
0066     m_top->setValue(current.top());
0067     m_left->setValue(current.left());
0068     m_width->setValue(current.width());
0069     m_height->setValue(-current.height());
0070 }
0071 
0072 void ViewportWidget::emitViewport()
0073 {
0074     Q_EMIT viewportChange(viewport());
0075 }